mediastrm.h

Go to the documentation of this file.
00001 /*
00002  * mediastrm.h
00003  *
00004  * Media Stream classes
00005  *
00006  * Open Phone Abstraction Library (OPAL)
00007  * Formally known as the Open H323 project.
00008  *
00009  * Copyright (c) 2001 Equivalence Pty. Ltd.
00010  *
00011  * The contents of this file are subject to the Mozilla Public License
00012  * Version 1.0 (the "License"); you may not use this file except in
00013  * compliance with the License. You may obtain a copy of the License at
00014  * http://www.mozilla.org/MPL/
00015  *
00016  * Software distributed under the License is distributed on an "AS IS"
00017  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00018  * the License for the specific language governing rights and limitations
00019  * under the License.
00020  *
00021  * The Original Code is Open Phone Abstraction Library.
00022  *
00023  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00024  *
00025  * Contributor(s): ______________________________________.
00026  *
00027  * $Revision: 19887 $
00028  * $Author: rjongbloed $
00029  * $Date: 2008-04-02 02:37:57 +0000 (Wed, 02 Apr 2008) $
00030  */
00031 
00032 #ifndef __OPAL_MEDIASTRM_H
00033 #define __OPAL_MEDIASTRM_H
00034 
00035 #ifdef P_USE_PRAGMA
00036 #pragma interface
00037 #endif
00038 
00039 #include <ptclib/delaychan.h>
00040 
00041 #include <opal/buildopts.h>
00042 #include <opal/mediafmt.h>
00043 #include <opal/mediacmd.h>
00044 #include <ptlib/safecoll.h>
00045 #include <ptclib/guid.h>
00046 
00047 
00048 class RTP_Session;
00049 class OpalMediaPatch;
00050 class OpalLine;
00051 class OpalConnection;
00052 class OpalMediaStatistics;
00053 
00054 
00060 class OpalMediaStream : public PSafeObject
00061 {
00062     PCLASSINFO(OpalMediaStream, PSafeObject);
00063   protected:
00068     OpalMediaStream(
00069       OpalConnection & conn,
00070       const OpalMediaFormat & mediaFormat, 
00071       unsigned sessionID,                  
00072       bool isSource                        
00073     );
00074 
00075   public:
00079     ~OpalMediaStream();
00081 
00082   public:
00089     void PrintOn(
00090       ostream & strm    
00091     ) const;
00093 
00103     virtual OpalMediaFormat GetMediaFormat() const;
00104 
00113     virtual PBoolean UpdateMediaFormat(
00114       const OpalMediaFormat & mediaFormat  
00115     );
00116 
00123     virtual PBoolean ExecuteCommand(
00124       const OpalMediaCommand & command    
00125     );
00126 
00134     virtual void SetCommandNotifier(
00135       const PNotifier & notifier    
00136     );
00137 
00142     virtual PBoolean Open();
00143 
00149     virtual PBoolean Start();
00150 
00155     virtual PBoolean Close();
00156 
00160     virtual void OnPatchStart();
00161 
00165     virtual void OnPatchStop();
00166 
00171     virtual PBoolean WritePackets(
00172       RTP_DataFrameList & packets
00173     );
00174 
00180     virtual PBoolean ReadPacket(
00181       RTP_DataFrame & packet
00182     );
00183 
00189     virtual PBoolean WritePacket(
00190       RTP_DataFrame & packet
00191     );
00192 
00198     virtual PBoolean ReadData(
00199       BYTE * data,      
00200       PINDEX size,      
00201       PINDEX & length   
00202     );
00203 
00209     virtual PBoolean WriteData(
00210       const BYTE * data,   
00211       PINDEX length,       
00212       PINDEX & written     
00213     );
00214 
00217     bool PushPacket(
00218       RTP_DataFrame & packet
00219     );
00220 
00226     virtual PBoolean SetDataSize(
00227       PINDEX dataSize  
00228     );
00229 
00233     PINDEX GetDataSize() const { return defaultDataSize; }
00234 
00241     virtual PBoolean IsSynchronous() const = 0;
00242         
00246     virtual PBoolean RequiresPatchThread() const;
00247 
00252     virtual void EnableJitterBuffer() const;
00254 
00259     OpalConnection & GetConnection() const { return connection; }
00260 
00263     bool IsSource() const { return isSource; }
00264 
00267     bool IsSink() const { return !isSource; }
00268 
00271     unsigned GetSessionID() const { return sessionID; }
00272 
00276     PString GetID() const { return identifier; }
00277 
00280     unsigned GetTimestamp() const { return timestamp; }
00281 
00284     void SetTimestamp(unsigned ts) { timestamp = ts; }
00285 
00288     bool GetMarker() const { return marker; }
00289 
00292     void SetMarker(bool m) { marker = m; }
00293 
00296     bool IsPaused() const { return paused; }
00297 
00300     void SetPaused(bool p) { paused = p; }
00301 
00304     bool IsOpen() { return isOpen; }
00305     
00308     virtual PBoolean SetPatch(
00309       OpalMediaPatch * patch  
00310     );
00311 
00318     virtual void RemovePatch(OpalMediaPatch * patch);
00319 
00322     OpalMediaPatch * GetPatch() const { return mediaPatch; }
00323 
00326     void AddFilter(const PNotifier & Filter, const OpalMediaFormat & Stage =  OpalMediaFormat());
00327 
00330     bool RemoveFilter(const PNotifier & Filter, const OpalMediaFormat & Stage);
00331 
00332 #ifdef OPAL_STATISTICS
00333     virtual void GetStatistics(OpalMediaStatistics & statistics) const;
00334 #endif
00335 
00336 
00337   protected:
00338 
00339     virtual void BitRateLimit (PINDEX byteCount, PBoolean mayDelay);
00340 
00341     OpalConnection & connection;
00342     unsigned         sessionID;
00343     PString          identifier;
00344     OpalMediaFormat  mediaFormat;
00345     bool             paused;
00346     bool             isSource;
00347     bool             isOpen;
00348     PINDEX           defaultDataSize;
00349     unsigned         timestamp;
00350     bool             marker;
00351     unsigned         mismatchedPayloadTypes;
00352 
00353     OpalMediaPatch * mediaPatch;
00354     PNotifier        commandNotifier;
00355 
00356     unsigned targetBitRateKbit;
00357     PINDEX totalLength;
00358     PTimeInterval newTime;
00359 };
00360 
00361 typedef PSafePtr<OpalMediaStream> OpalMediaStreamPtr;
00362 
00363 
00366 class OpalNullMediaStream : public OpalMediaStream
00367 {
00368     PCLASSINFO(OpalNullMediaStream, OpalMediaStream);
00369   public:
00374     OpalNullMediaStream(
00375       OpalConnection & conn,
00376       const OpalMediaFormat & mediaFormat, 
00377       unsigned sessionID,                  
00378       bool isSource                        
00379     );
00381 
00387     virtual PBoolean ReadData(
00388       BYTE * data,      
00389       PINDEX size,      
00390       PINDEX & length   
00391     );
00392 
00396     virtual PBoolean WriteData(
00397       const BYTE * data,   
00398       PINDEX length,       
00399       PINDEX & written     
00400     );
00401         
00405     virtual PBoolean RequiresPatchThread() const;
00406 
00410     virtual PBoolean IsSynchronous() const;
00412 
00413 };
00414 
00415 
00419 class OpalRTPMediaStream : public OpalMediaStream
00420 {
00421     PCLASSINFO(OpalRTPMediaStream, OpalMediaStream);
00422   public:
00428     OpalRTPMediaStream(
00429       OpalConnection & conn,
00430       const OpalMediaFormat & mediaFormat, 
00431       bool isSource,                       
00432       RTP_Session & rtpSession,            
00433       unsigned minAudioJitterDelay,        
00434       unsigned maxAudioJitterDelay         
00435     );
00436 
00440     ~OpalRTPMediaStream();
00442 
00449     virtual PBoolean Open();
00450 
00455     virtual PBoolean Close();
00456 
00460     virtual PBoolean ReadPacket(
00461       RTP_DataFrame & packet
00462     );
00463 
00467     virtual PBoolean WritePacket(
00468       RTP_DataFrame & packet
00469     );
00470 
00473     virtual PBoolean SetDataSize(
00474       PINDEX dataSize  
00475     );
00476 
00480     virtual PBoolean IsSynchronous() const;
00481 
00486     virtual void EnableJitterBuffer() const;
00487 
00490     virtual RTP_Session & GetRtpSession() const
00491     { return rtpSession; }
00492 
00493 #ifdef OPAL_STATISTICS
00494     virtual void GetStatistics(OpalMediaStatistics & statistics) const;
00495 #endif
00496 
00497 
00498   protected:
00499     RTP_Session & rtpSession;
00500     unsigned      minAudioJitterDelay;
00501     unsigned      maxAudioJitterDelay;
00502 };
00503 
00504 
00505 
00508 class OpalRawMediaStream : public OpalMediaStream
00509 {
00510     PCLASSINFO(OpalRawMediaStream, OpalMediaStream);
00511   protected:
00516     OpalRawMediaStream(
00517       OpalConnection & conn,
00518       const OpalMediaFormat & mediaFormat, 
00519       unsigned sessionID,                  
00520       bool isSource,                       
00521       PChannel * channel,                  
00522       bool autoDelete                      
00523     );
00524 
00527     ~OpalRawMediaStream();
00529 
00530   public:
00536     virtual PBoolean ReadData(
00537       BYTE * data,      
00538       PINDEX size,      
00539       PINDEX & length   
00540     );
00541 
00545     virtual PBoolean WriteData(
00546       const BYTE * data,   
00547       PINDEX length,       
00548       PINDEX & written     
00549     );
00550 
00553     PChannel * GetChannel() { return channel; }
00554     
00559     virtual PBoolean Close();
00560 
00563     virtual unsigned GetAverageSignalLevel();
00565 
00566   protected:
00567     PChannel * channel;
00568     bool       autoDelete;
00569 
00570     PUInt64    averageSignalSum;
00571     unsigned   averageSignalSamples;
00572     PMutex     averagingMutex;
00573     void CollectAverage(const BYTE * buffer, PINDEX size);
00574 };
00575 
00576 
00577 
00580 class OpalFileMediaStream : public OpalRawMediaStream
00581 {
00582     PCLASSINFO(OpalFileMediaStream, OpalRawMediaStream);
00583   public:
00588     OpalFileMediaStream(
00589       OpalConnection &,
00590       const OpalMediaFormat & mediaFormat, 
00591       unsigned sessionID,                  
00592       bool isSource,                       
00593       PFile * file,                        
00594       bool autoDelete = true               
00595     );
00596 
00599     OpalFileMediaStream(
00600       OpalConnection & ,
00601       const OpalMediaFormat & mediaFormat, 
00602       unsigned sessionID,                  
00603       bool isSource,                       
00604       const PFilePath & path               
00605     );
00607 
00613     virtual PBoolean IsSynchronous() const;
00615 
00616     virtual PBoolean ReadData(
00617       BYTE * data,      
00618       PINDEX size,      
00619       PINDEX & length   
00620     );
00621 
00625     virtual PBoolean WriteData(
00626       const BYTE * data,   
00627       PINDEX length,       
00628       PINDEX & written     
00629     );
00630 
00631   protected:
00632     PFile file;
00633     PAdaptiveDelay fileDelay;
00634 };
00635 
00636 #if OPAL_AUDIO
00637 #if P_AUDIO
00638 
00642 class PSoundChannel;
00643 
00644 class OpalAudioMediaStream : public OpalRawMediaStream
00645 {
00646     PCLASSINFO(OpalAudioMediaStream, OpalRawMediaStream);
00647   public:
00652     OpalAudioMediaStream(
00653       OpalConnection & conn,
00654       const OpalMediaFormat & mediaFormat, 
00655       unsigned sessionID,                  
00656       bool isSource,                       
00657       PINDEX buffers,                      
00658       PSoundChannel * channel,             
00659       bool autoDelete = true               
00660     );
00661 
00664     OpalAudioMediaStream(
00665       OpalConnection & conn,
00666       const OpalMediaFormat & mediaFormat, 
00667       unsigned sessionID,                  
00668       bool isSource,                       
00669       PINDEX buffers,                      
00670       const PString & deviceName           
00671     );
00673 
00681     virtual PBoolean SetDataSize(
00682       PINDEX dataSize  
00683     );
00684 
00688     virtual PBoolean IsSynchronous() const;
00690 
00691   protected:
00692     PINDEX soundChannelBuffers;
00693 };
00694 
00695 #endif
00696 
00697 #endif // OPAL_AUDIO
00698 
00699 #if OPAL_VIDEO
00700 
00704 class PVideoInputDevice;
00705 class PVideoOutputDevice;
00706 
00707 class OpalVideoMediaStream : public OpalMediaStream
00708 {
00709     PCLASSINFO(OpalVideoMediaStream, OpalMediaStream);
00710   public:
00715     OpalVideoMediaStream(
00716       OpalConnection & conn,
00717       const OpalMediaFormat & mediaFormat, 
00718       unsigned sessionID,                  
00719       PVideoInputDevice * inputDevice,     
00720       PVideoOutputDevice * outputDevice,   
00721       bool autoDelete = true               
00722     );
00723 
00726     ~OpalVideoMediaStream();
00728 
00736     virtual PBoolean Open();
00737 
00742     virtual PBoolean Close();
00743 
00749     virtual PBoolean ReadData(
00750       BYTE * data,      
00751       PINDEX size,      
00752       PINDEX & length   
00753     );
00754 
00760     virtual PBoolean WriteData(
00761       const BYTE * data,   
00762       PINDEX length,       
00763       PINDEX & written     
00764     );
00765 
00769     virtual PBoolean IsSynchronous() const;
00770 
00773     virtual PBoolean SetDataSize(
00774      PINDEX dataSize  
00775     );
00776 
00779     virtual PVideoInputDevice * GetVideoInputDevice() const {
00780       return inputDevice;
00781     }
00782 
00785     virtual PVideoOutputDevice * GetVideoOutputDevice() const {
00786       return outputDevice;
00787     }
00788 
00790 
00791   protected:
00792     PVideoInputDevice  * inputDevice;
00793     PVideoOutputDevice * outputDevice;
00794     bool                 autoDelete;
00795     PTimeInterval        lastGrabTime;
00796 };
00797 
00798 #endif // OPAL_VIDEO
00799 
00800 class OpalTransportUDP;
00801 
00804 class OpalUDPMediaStream : public OpalMediaStream
00805 {
00806     PCLASSINFO(OpalUDPMediaStream, OpalMediaStream);
00807   public:
00812     OpalUDPMediaStream(
00813       OpalConnection & conn,
00814       const OpalMediaFormat & mediaFormat, 
00815       unsigned sessionID,                  
00816       bool isSource,                       
00817       OpalTransportUDP & transport         
00818     );
00820 
00823 
00827     virtual PBoolean ReadPacket(
00828       RTP_DataFrame & packet
00829     );
00830 
00834     virtual PBoolean WritePacket(
00835       RTP_DataFrame & packet
00836     );
00837 
00841     virtual PBoolean IsSynchronous() const;
00842 
00846     virtual PBoolean Close();
00847 
00849 
00850   private:
00851     OpalTransportUDP & udpTransport;
00852 };
00853 
00854 #endif //__OPAL_MEDIASTRM_H
00855 
00856 
00857 // End of File ///////////////////////////////////////////////////////////////

Generated on Wed May 7 00:56:02 2008 for OPAL by  doxygen 1.5.1