00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 #ifndef __CODECS_H
00305 #define __CODECS_H
00306
00307 #ifdef P_USE_PRAGMA
00308 #pragma interface
00309 #endif
00310
00311
00312 #include <mediafmt.h>
00313 #include <rtp.h>
00314 #include <channels.h>
00315 #include "openh323buildopts.h"
00316
00317
00318
00319
00320
00321
00322
00323 class H245_MiscellaneousCommand_type;
00324 class H245_MiscellaneousIndication_type;
00325 class H323Connection;
00326
00327
00328
00330
00338 class PAec;
00339 class H323Codec : public PObject
00340 {
00341 PCLASSINFO(H323Codec, PObject);
00342
00343 public:
00344 enum Direction {
00345 Encoder,
00346 Decoder
00347 };
00348
00349 H323Codec(
00350 const char * mediaFormat,
00351 Direction direction
00352 );
00353
00354
00367 virtual BOOL Open(
00368 H323Connection & connection
00369 );
00370
00373 virtual void Close() = 0;
00374
00392 virtual BOOL Read(
00393 BYTE * buffer,
00394 unsigned & length,
00395 RTP_DataFrame & rtpFrame
00396 ) = 0;
00397
00409 virtual BOOL Write(
00410 const BYTE * buffer,
00411 unsigned length,
00412 const RTP_DataFrame & frame,
00413 unsigned & written
00414 ) = 0;
00415
00418 virtual unsigned GetFrameRate() const;
00419
00423 virtual void OnFlowControl(
00424 long bitRateRestriction
00425 );
00426
00430 virtual void OnMiscellaneousCommand(
00431 const H245_MiscellaneousCommand_type & type
00432 );
00433
00437 virtual void OnMiscellaneousIndication(
00438 const H245_MiscellaneousIndication_type & type
00439 );
00440
00441 Direction GetDirection() const { return direction; }
00442
00443 const OpalMediaFormat & GetMediaFormat() const { return mediaFormat; }
00444
00452 virtual BOOL AttachChannel(
00453 PChannel * channel,
00454 BOOL autoDelete = TRUE
00455 );
00456
00461 virtual PChannel * SwapChannel(
00462 PChannel * newChannel,
00463 BOOL autoDelete = TRUE
00464 );
00465
00468 virtual BOOL CloseRawDataChannel();
00469
00473 PChannel *GetRawDataChannel()
00474 { return rawDataChannel; }
00475
00482 virtual BOOL IsRawDataChannelNative() const;
00483
00486 BOOL ReadRaw(
00487 void * data,
00488 PINDEX size,
00489 PINDEX & length
00490 );
00491
00494 BOOL WriteRaw(
00495 void * data,
00496 PINDEX length
00497 );
00498
00505 BOOL AttachLogicalChannel(H323Channel *channel);
00506
00507 class FilterInfo : public PObject {
00508 PCLASSINFO(FilterInfo, PObject);
00509 public:
00510 FilterInfo(H323Codec & c, void * b, PINDEX s, PINDEX l)
00511 : codec(c), buffer(b), bufferSize(s), bufferLength(l) { }
00512
00513 H323Codec & codec;
00514 void * buffer;
00515 PINDEX bufferSize;
00516 PINDEX bufferLength;
00517 };
00518
00540 void AddFilter(
00541 const PNotifier & notifier
00542 );
00543
00547 virtual BOOL SetRawDataHeld(BOOL hold );
00548
00549 protected:
00550 Direction direction;
00551 OpalMediaFormat mediaFormat;
00552
00553 H323Channel * logicalChannel;
00554
00555 PChannel * rawDataChannel;
00556 BOOL deleteChannel;
00557 PMutex rawChannelMutex;
00558
00559 PINDEX lastSequenceNumber;
00560
00561 PLIST(FilterList, PNotifier);
00562 FilterList filters;
00563 };
00564
00565 #ifndef NO_H323_AUDIO_CODECS
00566
00567
00574 class PAec;
00575 class H323AudioCodec : public H323Codec
00576 {
00577 PCLASSINFO(H323AudioCodec, H323Codec);
00578
00579 public:
00585 H323AudioCodec(
00586 const char * mediaFormat,
00587 Direction direction
00588 );
00589
00590 ~H323AudioCodec();
00591
00600 virtual BOOL Open(
00601 H323Connection & connection
00602 );
00603
00611 virtual void Close();
00612
00615 virtual unsigned GetFrameRate() const;
00616
00617 enum SilenceDetectionMode {
00618 NoSilenceDetection,
00619 FixedSilenceDetection,
00620 AdaptiveSilenceDetection
00621 };
00622
00626 void SetSilenceDetectionMode(
00627 SilenceDetectionMode mode,
00628 unsigned threshold = 0,
00629 unsigned signalDeadband = 80,
00630 unsigned silenceDeadband = 3200,
00631 unsigned adaptivePeriod = 4800
00632 );
00633
00642 SilenceDetectionMode GetSilenceDetectionMode(
00643 BOOL * isInTalkBurst = NULL,
00644 unsigned * currentThreshold = NULL
00645 ) const;
00646
00647
00654 virtual void SetTxQualityLevel(int ) {}
00655
00659 virtual int GetTxQualityLevel(int ) { return 1; }
00660
00667 virtual BOOL DetectSilence();
00668
00676 virtual unsigned GetAverageSignalLevel();
00677
00681 virtual BOOL SetRawDataHeld(BOOL hold);
00682
00683 #if HAS_AEC
00684
00686 virtual void AttachAEC(
00687 PAec *
00688 ) {};
00689 #endif
00690
00691 protected:
00692 unsigned samplesPerFrame;
00693
00694 SilenceDetectionMode silenceDetectMode;
00695
00696 unsigned signalDeadbandFrames;
00697 unsigned silenceDeadbandFrames;
00698 unsigned adaptiveThresholdFrames;
00699
00700 BOOL inTalkBurst;
00701 unsigned framesReceived;
00702 unsigned levelThreshold;
00703 unsigned signalMinimum;
00704 unsigned silenceMaximum;
00705 unsigned signalFramesReceived;
00706 unsigned silenceFramesReceived;
00707 BOOL IsRawDataHeld;
00708 };
00709
00710
00719 class PAec;
00720 class H323FramedAudioCodec : public H323AudioCodec
00721 {
00722 PCLASSINFO(H323FramedAudioCodec, H323AudioCodec);
00723
00724 public:
00730 H323FramedAudioCodec(
00731 const char * mediaFormat,
00732 Direction direction
00733 );
00734
00752 virtual BOOL Read(
00753 BYTE * buffer,
00754 unsigned & length,
00755 RTP_DataFrame & rtpFrame
00756 );
00757
00770 virtual BOOL Write(
00771 const BYTE * buffer,
00772 unsigned length,
00773 const RTP_DataFrame & rtpFrame,
00774 unsigned & written
00775 );
00776
00777
00782 virtual unsigned GetAverageSignalLevel();
00783
00784
00790 virtual BOOL EncodeFrame(
00791 BYTE * buffer,
00792 unsigned & length
00793 ) = 0;
00794
00799 virtual BOOL DecodeFrame(
00800 const BYTE * buffer,
00801 unsigned length,
00802 unsigned & written,
00803 unsigned & bytesOutput
00804 );
00805 virtual BOOL DecodeFrame(
00806 const BYTE * buffer,
00807 unsigned length,
00808 unsigned & written
00809 );
00810
00815 virtual void DecodeSilenceFrame(
00816 void * buffer,
00817 unsigned length
00818 )
00819 { memset(buffer, 0, length); }
00820
00821 #if HAS_AEC
00822
00824 virtual void AttachAEC(
00825 PAec * _ARC
00826 );
00827 #endif
00828
00829 protected:
00830 PAec * aec;
00831 PShortArray sampleBuffer;
00832 unsigned bytesPerFrame;
00833 };
00834
00835
00844 class H323StreamedAudioCodec : public H323FramedAudioCodec
00845 {
00846 PCLASSINFO(H323StreamedAudioCodec, H323FramedAudioCodec);
00847
00848 public:
00854 H323StreamedAudioCodec(
00855 const char * mediaFormat,
00856 Direction direction,
00857 unsigned samplesPerFrame,
00858 unsigned bits
00859 );
00860
00866 virtual BOOL EncodeFrame(
00867 BYTE * buffer,
00868 unsigned & length
00869 );
00870
00876 virtual BOOL DecodeFrame(
00877 const BYTE * buffer,
00878 unsigned length,
00879 unsigned & written,
00880 unsigned & samples
00881 );
00882
00885 virtual int Encode(short sample) const = 0;
00886
00889 virtual short Decode(int sample) const = 0;
00890
00891 protected:
00892 unsigned bitsPerSample;
00893 };
00894
00895 #endif // NO_H323_AUDIO_CODECS
00896
00897
00898 #ifndef NO_H323_VIDEO
00899
00906 class H323VideoCodec : public H323Codec
00907 {
00908 PCLASSINFO(H323VideoCodec, H323Codec);
00909
00910 public:
00916 H323VideoCodec(
00917 const char * mediaFormat,
00918 Direction direction
00919 );
00920
00921 ~H323VideoCodec();
00922
00931 virtual BOOL Open(
00932 H323Connection & connection
00933 );
00934
00942 virtual void Close();
00943
00944
00948 virtual void OnMiscellaneousCommand(
00949 const H245_MiscellaneousCommand_type & type
00950 );
00951
00955 virtual void OnMiscellaneousIndication(
00956 const H245_MiscellaneousIndication_type & type
00957 );
00958
00959
00960
00961
00962
00963
00964
00965
00966
00970 virtual void OnFreezePicture();
00971
00975 virtual void OnFastUpdatePicture();
00976
00980 virtual void OnFastUpdateGOB(unsigned firstGOB, unsigned numberOfGOBs);
00981
00985 virtual void OnFastUpdateMB(int firstGOB, int firstMB, unsigned numberOfMBs);
00986
00990 virtual void OnVideoIndicateReadyToActivate();
00991
00995 virtual void OnVideoTemporalSpatialTradeOffCommand(int newQuality);
00996
01000 virtual void OnVideoTemporalSpatialTradeOffIndication(int newQuality);
01001
01005 virtual void OnVideoNotDecodedMBs(
01006 unsigned firstMB,
01007 unsigned numberOfMBs,
01008 unsigned temporalReference
01009 );
01010
01014 virtual void OnLostPartialPicture();
01015
01019 virtual void OnLostPicture();
01020
01023 int GetWidth() const { return frameWidth; }
01024
01027 int GetHeight() const { return frameHeight; }
01028
01031 virtual void SetTxQualityLevel(int qlevel) {videoQuality = qlevel; }
01032
01036 virtual void SetTxMinQuality(int qlevel) {videoQMin = qlevel; }
01037
01041 virtual void SetTxMaxQuality(int qlevel) {videoQMax = qlevel; }
01042
01045 virtual void SetBackgroundFill(int idle) {fillLevel= idle; }
01046
01047 enum BitRateModeBits {
01048 None = 0x00,
01049 DynamicVideoQuality = 0x01,
01050 AdaptivePacketDelay = 0x02
01051 };
01052
01055 unsigned GetVideoMode(void) {return videoBitRateControlModes;}
01056
01060 unsigned SetVideoMode(unsigned mode) {return videoBitRateControlModes = mode;}
01061
01069 virtual BOOL SetMaxBitRate(
01070 unsigned bitRate
01071 );
01072
01077 virtual unsigned GetMaxBitRate() const { return bitRateHighLimit; }
01078
01087 virtual BOOL SetTargetFrameTimeMs(
01088 unsigned ms
01089 );
01090
01095 void SendMiscCommand(unsigned command);
01096
01100 virtual int GetFrameNum() { return frameNum; }
01101
01102
01103 protected:
01104
01105 int frameWidth;
01106 int frameHeight;
01107 int fillLevel;
01108
01109
01110 unsigned videoBitRateControlModes;
01111
01112 int bitRateHighLimit;
01113 unsigned oldLength;
01114 PTimeInterval oldTime;
01115 PTimeInterval newTime;
01116
01117 int targetFrameTimeMs;
01118 int frameBytes;
01119 int sumFrameTimeMs, sumAdjFrameTimeMs, sumFrameBytes;
01120 int videoQMax, videoQMin;
01121 int videoQuality;
01122 PTimeInterval frameStartTime;
01123 PTimeInterval grabInterval;
01124
01125 int frameNum, packetNum, oldPacketNum;
01126 int framesPerSec;
01127
01128 PMutex videoHandlerActive;
01129 };
01130
01131 #endif // NO_H323_VIDEO
01132
01133 #ifndef NO_H323_AUDIO_CODECS
01134
01136
01137
01140 class H323_ALawCodec : public H323StreamedAudioCodec
01141 {
01142 PCLASSINFO(H323_ALawCodec, H323StreamedAudioCodec)
01143
01144 public:
01149 H323_ALawCodec(
01150 Direction direction,
01151 BOOL at56kbps,
01152 unsigned frameSize
01153 );
01155
01156 virtual int Encode(short sample) const { return EncodeSample(sample); }
01157 virtual short Decode(int sample) const { return DecodeSample(sample); }
01158
01159 static int EncodeSample(short sample);
01160 static short DecodeSample(int sample);
01161
01162 protected:
01163 BOOL sevenBit;
01164 };
01165
01166
01169 class H323_muLawCodec : public H323StreamedAudioCodec
01170 {
01171 PCLASSINFO(H323_muLawCodec, H323StreamedAudioCodec)
01172
01173 public:
01178 H323_muLawCodec(
01179 Direction direction,
01180 BOOL at56kbps,
01181 unsigned frameSize
01182 );
01184
01185 virtual int Encode(short sample) const { return EncodeSample(sample); }
01186 virtual short Decode(int sample) const { return DecodeSample(sample); }
01187
01188 static int EncodeSample(short sample);
01189 static short DecodeSample(int sample);
01190
01191 protected:
01192 BOOL sevenBit;
01193 };
01194
01195 #endif // NO_H323_AUDIO_CODECS
01196
01197
01198 #endif // __CODECS_H
01199
01200