OPAL  Version 3.12.5
mediastrm.h
Go to the documentation of this file.
1 /*
2  * mediastrm.h
3  *
4  * Media Stream classes
5  *
6  * Open Phone Abstraction Library (OPAL)
7  * Formally known as the Open H323 project.
8  *
9  * Copyright (c) 2001 Equivalence Pty. Ltd.
10  *
11  * The contents of this file are subject to the Mozilla Public License
12  * Version 1.0 (the "License"); you may not use this file except in
13  * compliance with the License. You may obtain a copy of the License at
14  * http://www.mozilla.org/MPL/
15  *
16  * Software distributed under the License is distributed on an "AS IS"
17  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
18  * the License for the specific language governing rights and limitations
19  * under the License.
20  *
21  * The Original Code is Open Phone Abstraction Library.
22  *
23  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24  *
25  * Contributor(s): ______________________________________.
26  *
27  * $Revision: 29330 $
28  * $Author: rjongbloed $
29  * $Date: 2013-03-26 19:55:21 -0500 (Tue, 26 Mar 2013) $
30  */
31 
32 #ifndef OPAL_OPAL_MEDIASTRM_H
33 #define OPAL_OPAL_MEDIASTRM_H
34 
35 #ifdef P_USE_PRAGMA
36 #pragma interface
37 #endif
38 
39 #include <opal/buildopts.h>
40 
41 #include <ptclib/delaychan.h>
42 
43 #include <opal/mediafmt.h>
44 #include <opal/mediacmd.h>
45 #include <rtp/jitter.h>
46 #include <ptlib/safecoll.h>
47 #include <ptclib/guid.h>
48 
49 
50 class OpalRTPSession;
51 class OpalMediaPatch;
52 class OpalLine;
53 class OpalConnection;
54 class OpalRTPConnection;
56 
57 
58 typedef PSafePtr<OpalMediaPatch, PSafePtrMultiThreaded> OpalMediaPatchPtr;
59 
60 
116 class OpalMediaStream : public PSafeObject
117 {
118  PCLASSINFO(OpalMediaStream, PSafeObject);
119  protected:
125  OpalConnection & conn,
126  const OpalMediaFormat & mediaFormat,
127  unsigned sessionID,
128  bool isSource
129  );
130 
131  public:
137 
138  public:
145  void PrintOn(
146  ostream & strm
147  ) const;
149 
159  virtual OpalMediaFormat GetMediaFormat() const;
160 
167  virtual bool SetMediaFormat(
168  const OpalMediaFormat & mediaFormat
169  );
170 
180  bool UpdateMediaFormat(
181  const OpalMediaFormat & mediaFormat
182  );
183 
192  virtual PBoolean ExecuteCommand(
193  const OpalMediaCommand & command
194  );
195 
200  virtual PBoolean Open();
201 
204  virtual bool IsOpen() const;
205 
211  virtual PBoolean Start();
212 
218  virtual PBoolean Close();
219 
223  virtual void OnStartMediaPatch();
224 
228  virtual void OnStopMediaPatch(
229  OpalMediaPatch & patch
230  );
231 
236  virtual PBoolean WritePackets(
237  RTP_DataFrameList & packets
238  );
239 
245  virtual PBoolean ReadPacket(
246  RTP_DataFrame & packet
247  );
248 
254  virtual PBoolean WritePacket(
255  RTP_DataFrame & packet
256  );
257 
263  virtual PBoolean ReadData(
264  BYTE * data,
265  PINDEX size,
266  PINDEX & length
267  );
268 
274  virtual PBoolean WriteData(
275  const BYTE * data,
276  PINDEX length,
277  PINDEX & written
278  );
279 
282  bool PushPacket(
283  RTP_DataFrame & packet
284  );
285 
291  virtual PBoolean SetDataSize(
292  PINDEX dataSize,
293  PINDEX frameTime
294  );
295 
299  PINDEX GetDataSize() const { return defaultDataSize; }
300 
307  virtual PBoolean IsSynchronous() const = 0;
308 
318  virtual PBoolean RequiresPatchThread(
319  OpalMediaStream * stream
320  ) const;
321  virtual PBoolean RequiresPatchThread() const; // For backward compatibility
322 
329  virtual bool EnableJitterBuffer(bool enab = true) const;
331 
337 
340  bool IsSource() const { return m_isSource; }
341 
344  bool IsSink() const { return !m_isSource; }
345 
348  unsigned GetSessionID() const { return sessionID; }
349 
352  void SetSessionID(unsigned id) { sessionID = id; }
353 
357  PString GetID() const { return identifier; }
358 
361  unsigned GetTimestamp() const { return timestamp; }
362 
365  void SetTimestamp(unsigned ts) { timestamp = ts; }
366 
369  bool GetMarker() const { return marker; }
370 
373  void SetMarker(bool m) { marker = m; }
374 
377  bool IsPaused() const { return m_paused; }
378 
383  virtual bool SetPaused(
384  bool pause,
385  bool fromPatch = false
386  );
387 
390  virtual PBoolean SetPatch(
391  OpalMediaPatch * patch
392  );
393 
396  OpalMediaPatchPtr GetPatch() const { return m_mediaPatch; }
397 
400  void AddFilter(
401  const PNotifier & filter,
402  const OpalMediaFormat & stage = OpalMediaFormat()
403  ) const;
404 
407  bool RemoveFilter(
408  const PNotifier & filter,
409  const OpalMediaFormat & stage = OpalMediaFormat()
410  ) const;
411 
412 #if OPAL_STATISTICS
413  virtual void GetStatistics(OpalMediaStatistics & statistics, bool fromPatch = false) const;
414 #endif
415 
416 
417  virtual bool InternalUpdateMediaFormat(const OpalMediaFormat & mediaFormat);
418 
419  protected:
420  void IncrementTimestamp(PINDEX size);
421  bool InternalWriteData(const BYTE * data, PINDEX length, PINDEX & written);
422  OpalMediaPatchPtr InternalSetPatchPart1(OpalMediaPatch * newPatch);
423  void InternalSetPatchPart2(const OpalMediaPatchPtr & oldPatch);
424  virtual bool InternalSetJitterBuffer(const OpalJitterBuffer::Init & init) const;
425 
431  virtual void InternalClose() = 0;
432 
434  unsigned sessionID;
435  PString identifier;
437  bool m_paused;
439  bool m_isOpen;
441  unsigned timestamp;
442  bool marker;
443 
444  OpalMediaPatchPtr m_mediaPatch;
445 
447  unsigned m_frameTime;
448  PINDEX m_frameSize;
449 
450  typedef OpalMediaPatchPtr PatchPtr; // For backward compatibility
451 
452  private:
453  P_REMOVE_VIRTUAL_VOID(OnPatchStart());
454  P_REMOVE_VIRTUAL_VOID(OnPatchStop());
455  P_REMOVE_VIRTUAL_VOID(OnStopMediaPatch());
456  P_REMOVE_VIRTUAL_VOID(RemovePatch(OpalMediaPatch *));
457 
458  friend class OpalMediaPatch;
459 };
460 
461 typedef PSafePtr<OpalMediaStream> OpalMediaStreamPtr;
462 
463 
467 {
468  public:
470  const OpalMediaFormat & mediaFormat
471  );
472 
474  void Pace(
475  bool reading,
476  PINDEX bytes,
477  bool & marker
478  );
479 
480  bool UpdateMediaFormat(
481  const OpalMediaFormat & mediaFormat
482  );
483 
484  protected:
486  unsigned m_frameTime;
487  PINDEX m_frameSize;
488  unsigned m_timeUnits;
489  PAdaptiveDelay m_delay;
490 };
491 
492 
496 {
498  public:
504  OpalConnection & conn,
505  const OpalMediaFormat & mediaFormat,
506  unsigned sessionID,
507  bool isSource,
508  bool isSynchronous = false
509  );
511  OpalConnection & conn,
512  const OpalMediaFormat & mediaFormat,
513  unsigned sessionID,
514  bool isSource,
515  bool usePacingDelay,
516  bool requiresPatchThread
517  );
519 
525  virtual PBoolean ReadData(
526  BYTE * data,
527  PINDEX size,
528  PINDEX & length
529  );
530 
534  virtual PBoolean WriteData(
535  const BYTE * data,
536  PINDEX length,
537  PINDEX & written
538  );
539 
544  virtual bool SetPaused(
545  bool pause,
546  bool fromPatch = false
547  );
548 
552  virtual PBoolean RequiresPatchThread() const;
553 
557  virtual PBoolean IsSynchronous() const;
559 
560  protected:
561  virtual void InternalClose() { }
562  virtual bool InternalUpdateMediaFormat(const OpalMediaFormat & newMediaFormat);
563 
566 };
567 
568 
573 {
574  PCLASSINFO(OpalRTPMediaStream, OpalMediaStream);
575  public:
582  OpalRTPConnection & conn,
583  const OpalMediaFormat & mediaFormat,
584  bool isSource,
586  );
587 
593 
600  virtual PBoolean Open();
601 
604  virtual bool IsOpen() const;
605 
609  virtual bool SetPaused(
610  bool pause,
611  bool fromPatch = false
612  );
613 
617  virtual PBoolean ReadPacket(
618  RTP_DataFrame & packet
619  );
620 
624  virtual PBoolean WritePacket(
625  RTP_DataFrame & packet
626  );
627 
630  virtual PBoolean SetDataSize(
631  PINDEX dataSize,
632  PINDEX frameTime
633  );
634 
638  virtual PBoolean IsSynchronous() const;
639 
647  virtual PBoolean RequiresPatchThread() const;
648 
651  virtual PBoolean SetPatch(
652  OpalMediaPatch * patch
653  );
654 
657  virtual OpalRTPSession & GetRtpSession() const
658  { return rtpSession; }
659 
660 #if OPAL_STATISTICS
661  virtual void GetStatistics(OpalMediaStatistics & statistics, bool fromPatch = false) const;
662 #endif
663 
664 
665  protected:
666  virtual void InternalClose();
667  virtual bool InternalSetJitterBuffer(const OpalJitterBuffer::Init & init) const;
668 
670 #if OPAL_VIDEO
673 #endif
674 };
675 
676 
677 
681 {
682  PCLASSINFO(OpalRawMediaStream, OpalMediaStream);
683  protected:
689  OpalConnection & conn,
690  const OpalMediaFormat & mediaFormat,
691  unsigned sessionID,
692  bool isSource,
693  PChannel * channel,
694  bool autoDelete
695  );
696 
701 
702  public:
708  virtual PBoolean ReadData(
709  BYTE * data,
710  PINDEX size,
711  PINDEX & length
712  );
713 
717  virtual PBoolean WriteData(
718  const BYTE * data,
719  PINDEX length,
720  PINDEX & written
721  );
722 
725  PChannel * GetChannel() { return m_channel; }
726 
729  bool SetChannel(
730  PChannel * channel,
731  bool autoDelete = true
732  );
733 
736  virtual unsigned GetAverageSignalLevel();
738 
739  protected:
740  virtual void InternalClose();
741 
742  PChannel * m_channel;
745 
746  PBYTEArray m_silence;
747 
751 
752  void CollectAverage(const BYTE * buffer, PINDEX size);
753 };
754 
755 
756 
760 {
762  public:
768  OpalConnection & conn,
769  const OpalMediaFormat & mediaFormat,
770  unsigned sessionID,
771  bool isSource,
772  PFile * file,
773  bool autoDelete = true
774  );
775 
779  OpalConnection & conn,
780  const OpalMediaFormat & mediaFormat,
781  unsigned sessionID,
782  bool isSource,
783  const PFilePath & path
784  );
786 
792  virtual PBoolean IsSynchronous() const;
793 
794  virtual PBoolean ReadData(
795  BYTE * data,
796  PINDEX size,
797  PINDEX & length
798  );
799 
803  virtual PBoolean WriteData(
804  const BYTE * data,
805  PINDEX length,
806  PINDEX & written
807  );
809 
810  protected:
811  PFile file;
812 };
813 
814 
815 #if OPAL_PTLIB_AUDIO
816 
820 class PSoundChannel;
821 
822 class OpalAudioMediaStream : public OpalRawMediaStream
823 {
824  PCLASSINFO(OpalAudioMediaStream, OpalRawMediaStream);
825  public:
830  OpalAudioMediaStream(
831  OpalConnection & conn,
832  const OpalMediaFormat & mediaFormat,
833  unsigned sessionID,
834  bool isSource,
835  PINDEX buffers,
836  unsigned bufferTime,
837  PSoundChannel * channel,
838  bool autoDelete = true
839  );
840 
843  OpalAudioMediaStream(
844  OpalConnection & conn,
845  const OpalMediaFormat & mediaFormat,
846  unsigned sessionID,
847  bool isSource,
848  PINDEX buffers,
849  unsigned bufferTime,
850  const PString & deviceName
851  );
853 
861  virtual PBoolean SetDataSize(
862  PINDEX dataSize,
863  PINDEX frameTime
864  );
865 
869  virtual PBoolean IsSynchronous() const;
871 
872  protected:
873  PINDEX m_soundChannelBuffers;
874  unsigned m_soundChannelBufferTime;
875 };
876 
877 #endif // OPAL_PTLIB_AUDIO
878 
879 #if OPAL_VIDEO
880 
884 class PVideoInputDevice;
885 class PVideoOutputDevice;
886 
888 {
890  public:
896  OpalConnection & conn,
897  const OpalMediaFormat & mediaFormat,
898  unsigned sessionID,
899  PVideoInputDevice * inputDevice,
900  PVideoOutputDevice * outputDevice,
901  bool autoDeleteInput = true,
902  bool autoDeleteOutput = true
903  );
904 
909 
917  virtual PBoolean Open();
918 
926  virtual PBoolean ExecuteCommand(
927  const OpalMediaCommand & command
928  );
929 
935  virtual PBoolean ReadData(
936  BYTE * data,
937  PINDEX size,
938  PINDEX & length
939  );
940 
946  virtual PBoolean WriteData(
947  const BYTE * data,
948  PINDEX length,
949  PINDEX & written
950  );
951 
955  virtual PBoolean IsSynchronous() const;
956 
959  virtual PBoolean SetDataSize(
960  PINDEX dataSize,
961  PINDEX frameTime
962  );
963 
966  virtual void SetVideoInputDevice(
967  PVideoInputDevice * device,
968  bool autoDelete = true
969  );
970 
973  virtual PVideoInputDevice * GetVideoInputDevice() const
974  {
975  return m_inputDevice;
976  }
977 
980  virtual void SetVideoOutputDevice(
981  PVideoOutputDevice * device,
982  bool autoDelete = true
983  );
984 
987  virtual PVideoOutputDevice * GetVideoOutputDevice() const
988  {
989  return m_outputDevice;
990  }
991 
993 
994  protected:
995  virtual void InternalClose();
996  virtual bool InternalUpdateMediaFormat(const OpalMediaFormat & newMediaFormat);
997  bool InternalAdjustDevices();
998 
999  PVideoInputDevice * m_inputDevice;
1000  PVideoOutputDevice * m_outputDevice;
1003  PTimeInterval m_lastGrabTime;
1006 };
1007 
1008 #endif // OPAL_VIDEO
1009 
1010 class OpalTransportUDP;
1011 
1015 {
1016  PCLASSINFO(OpalUDPMediaStream, OpalMediaStream);
1017  public:
1023  OpalConnection & conn,
1024  const OpalMediaFormat & mediaFormat,
1025  unsigned sessionID,
1026  bool isSource,
1027  OpalTransportUDP & transport
1028  );
1030 
1032 
1035 
1039  virtual PBoolean ReadPacket(
1040  RTP_DataFrame & packet
1041  );
1042 
1046  virtual PBoolean WritePacket(
1047  RTP_DataFrame & packet
1048  );
1049 
1053  virtual PBoolean IsSynchronous() const;
1055 
1056  private:
1057  virtual void InternalClose();
1058 
1059  OpalTransportUDP & udpTransport;
1060 };
1061 
1062 
1063 #endif //OPAL_OPAL_MEDIASTRM_H
1064 
1065 
1066 // End of File ///////////////////////////////////////////////////////////////