OPAL  Version 3.12.9
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: 31114 $
28  * $Author: rjongbloed $
29  * $Date: 2013-12-19 11:28:31 +1100 (Thu, 19 Dec 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  bool mergeOnly = false
183  );
184 
193  virtual PBoolean ExecuteCommand(
194  const OpalMediaCommand & command
195  );
196 
201  virtual PBoolean Open();
202 
205  virtual bool IsOpen() const;
206 
212  virtual PBoolean Start();
213 
219  virtual PBoolean Close();
220 
224  virtual void OnStartMediaPatch();
225 
229  virtual void OnStopMediaPatch(
230  OpalMediaPatch & patch
231  );
232 
237  virtual PBoolean WritePackets(
238  RTP_DataFrameList & packets
239  );
240 
246  virtual PBoolean ReadPacket(
247  RTP_DataFrame & packet
248  );
249 
255  virtual PBoolean WritePacket(
256  RTP_DataFrame & packet
257  );
258 
264  virtual PBoolean ReadData(
265  BYTE * data,
266  PINDEX size,
267  PINDEX & length
268  );
269 
275  virtual PBoolean WriteData(
276  const BYTE * data,
277  PINDEX length,
278  PINDEX & written
279  );
280 
283  bool PushPacket(
284  RTP_DataFrame & packet
285  );
286 
292  virtual PBoolean SetDataSize(
293  PINDEX dataSize,
294  PINDEX frameTime
295  );
296 
300  PINDEX GetDataSize() const { return m_defaultDataSize; }
301 
308  virtual PBoolean IsSynchronous() const = 0;
309 
319  virtual PBoolean RequiresPatchThread(
320  OpalMediaStream * stream
321  ) const;
322  virtual PBoolean RequiresPatchThread() const; // For backward compatibility
323 
330  virtual bool EnableJitterBuffer(bool enab = true) const;
332 
338 
341  bool IsSource() const { return m_isSource; }
342 
345  bool IsSink() const { return !m_isSource; }
346 
349  unsigned GetSessionID() const { return sessionID; }
350 
353  void SetSessionID(unsigned id) { sessionID = id; }
354 
358  PString GetID() const { return identifier; }
359 
362  unsigned GetTimestamp() const { return timestamp; }
363 
366  void SetTimestamp(unsigned ts) { timestamp = ts; }
367 
370  bool GetMarker() const { return marker; }
371 
374  void SetMarker(bool m) { marker = m; }
375 
378  bool IsPaused() const { return m_paused; }
379 
384  virtual bool SetPaused(
385  bool pause,
386  bool fromPatch = false
387  );
388 
391  virtual PBoolean SetPatch(
392  OpalMediaPatch * patch
393  );
394 
397  OpalMediaPatchPtr GetPatch() const { return m_mediaPatch; }
398 
401  void AddFilter(
402  const PNotifier & filter,
403  const OpalMediaFormat & stage = OpalMediaFormat()
404  ) const;
405 
408  bool RemoveFilter(
409  const PNotifier & filter,
410  const OpalMediaFormat & stage = OpalMediaFormat()
411  ) const;
412 
413 #if OPAL_STATISTICS
414  virtual void GetStatistics(OpalMediaStatistics & statistics, bool fromPatch = false) const;
415 #endif
416 
417 
418  virtual bool InternalUpdateMediaFormat(const OpalMediaFormat & mediaFormat);
419 
420  protected:
421  void IncrementTimestamp(PINDEX size);
422  bool InternalWriteData(const BYTE * data, PINDEX length, PINDEX & written);
423  OpalMediaPatchPtr InternalSetPatchPart1(OpalMediaPatch * newPatch);
424  void InternalSetPatchPart2(const OpalMediaPatchPtr & oldPatch);
425  virtual bool InternalSetJitterBuffer(const OpalJitterBuffer::Init & init) const;
426 
432  virtual void InternalClose() = 0;
433 
435  unsigned sessionID;
437  PString identifier;
439  bool m_paused;
441  bool m_isOpen;
443  unsigned timestamp;
444  bool marker;
445 
446  OpalMediaPatchPtr m_mediaPatch;
447 
449  unsigned m_frameTime;
450  PINDEX m_frameSize;
451 
452  typedef OpalMediaPatchPtr PatchPtr; // For backward compatibility
453 
454  private:
455  P_REMOVE_VIRTUAL_VOID(OnPatchStart());
456  P_REMOVE_VIRTUAL_VOID(OnPatchStop());
457  P_REMOVE_VIRTUAL_VOID(OnStopMediaPatch());
458  P_REMOVE_VIRTUAL_VOID(RemovePatch(OpalMediaPatch *));
459 
460  friend class OpalMediaPatch;
461 };
462 
463 typedef PSafePtr<OpalMediaStream> OpalMediaStreamPtr;
464 
465 
469 {
470  public:
472  const OpalMediaFormat & mediaFormat
473  );
474 
476  void Pace(
477  bool reading,
478  PINDEX bytes,
479  bool & marker
480  );
481 
482  bool UpdateMediaFormat(
483  const OpalMediaFormat & mediaFormat
484  );
485 
486  protected:
488  unsigned m_frameTime;
489  PINDEX m_frameSize;
490  unsigned m_timeUnits;
491  PAdaptiveDelay m_delay;
492 };
493 
494 
498 {
500  public:
506  OpalConnection & conn,
507  const OpalMediaFormat & mediaFormat,
508  unsigned sessionID,
509  bool isSource,
510  bool isSynchronous = false
511  );
513  OpalConnection & conn,
514  const OpalMediaFormat & mediaFormat,
515  unsigned sessionID,
516  bool isSource,
517  bool usePacingDelay,
518  bool requiresPatchThread
519  );
521 
527  virtual PBoolean ReadData(
528  BYTE * data,
529  PINDEX size,
530  PINDEX & length
531  );
532 
536  virtual PBoolean WriteData(
537  const BYTE * data,
538  PINDEX length,
539  PINDEX & written
540  );
541 
546  virtual bool SetPaused(
547  bool pause,
548  bool fromPatch = false
549  );
550 
554  virtual PBoolean RequiresPatchThread() const;
555 
559  virtual PBoolean IsSynchronous() const;
561 
562  protected:
563  virtual void InternalClose() { }
564  virtual bool InternalUpdateMediaFormat(const OpalMediaFormat & newMediaFormat);
565 
568 };
569 
570 
575 {
576  PCLASSINFO(OpalRTPMediaStream, OpalMediaStream);
577  public:
584  OpalRTPConnection & conn,
585  const OpalMediaFormat & mediaFormat,
586  bool isSource,
588  );
589 
595 
602  virtual PBoolean Open();
603 
606  virtual bool IsOpen() const;
607 
611  virtual bool SetPaused(
612  bool pause,
613  bool fromPatch = false
614  );
615 
619  virtual PBoolean ReadPacket(
620  RTP_DataFrame & packet
621  );
622 
626  virtual PBoolean WritePacket(
627  RTP_DataFrame & packet
628  );
629 
632  virtual PBoolean SetDataSize(
633  PINDEX dataSize,
634  PINDEX frameTime
635  );
636 
640  virtual PBoolean IsSynchronous() const;
641 
649  virtual PBoolean RequiresPatchThread() const;
650 
653  virtual PBoolean SetPatch(
654  OpalMediaPatch * patch
655  );
656 
659  virtual OpalRTPSession & GetRtpSession() const
660  { return rtpSession; }
661 
662 #if OPAL_STATISTICS
663  virtual void GetStatistics(OpalMediaStatistics & statistics, bool fromPatch = false) const;
664 #endif
665 
666 
667  protected:
668  virtual void InternalClose();
669  virtual bool InternalSetJitterBuffer(const OpalJitterBuffer::Init & init) const;
670 
672 #if OPAL_VIDEO
675 #endif
676 };
677 
678 
679 
683 {
684  PCLASSINFO(OpalRawMediaStream, OpalMediaStream);
685  protected:
691  OpalConnection & conn,
692  const OpalMediaFormat & mediaFormat,
693  unsigned sessionID,
694  bool isSource,
695  PChannel * channel,
696  bool autoDelete
697  );
698 
703 
704  public:
710  virtual PBoolean ReadData(
711  BYTE * data,
712  PINDEX size,
713  PINDEX & length
714  );
715 
719  virtual PBoolean WriteData(
720  const BYTE * data,
721  PINDEX length,
722  PINDEX & written
723  );
724 
727  PChannel * GetChannel() { return m_channel; }
728 
731  bool SetChannel(
732  PChannel * channel,
733  bool autoDelete = true
734  );
735 
738  virtual unsigned GetAverageSignalLevel();
740 
741  protected:
742  virtual void InternalClose();
743 
744  PChannel * m_channel;
747 
748  PBYTEArray m_silence;
749 
753 
754  void CollectAverage(const BYTE * buffer, PINDEX size);
755 };
756 
757 
758 
762 {
764  public:
770  OpalConnection & conn,
771  const OpalMediaFormat & mediaFormat,
772  unsigned sessionID,
773  bool isSource,
774  PFile * file,
775  bool autoDelete = true
776  );
777 
781  OpalConnection & conn,
782  const OpalMediaFormat & mediaFormat,
783  unsigned sessionID,
784  bool isSource,
785  const PFilePath & path
786  );
788 
794  virtual PBoolean IsSynchronous() const;
795 
796  virtual PBoolean ReadData(
797  BYTE * data,
798  PINDEX size,
799  PINDEX & length
800  );
801 
805  virtual PBoolean WriteData(
806  const BYTE * data,
807  PINDEX length,
808  PINDEX & written
809  );
811 
812  protected:
813  PFile file;
814 };
815 
816 
817 #if OPAL_PTLIB_AUDIO
818 
822 class PSoundChannel;
823 
824 class OpalAudioMediaStream : public OpalRawMediaStream
825 {
826  PCLASSINFO(OpalAudioMediaStream, OpalRawMediaStream);
827  public:
832  OpalAudioMediaStream(
833  OpalConnection & conn,
834  const OpalMediaFormat & mediaFormat,
835  unsigned sessionID,
836  bool isSource,
837  PINDEX buffers,
838  unsigned bufferTime,
839  PSoundChannel * channel,
840  bool autoDelete = true
841  );
842 
845  OpalAudioMediaStream(
846  OpalConnection & conn,
847  const OpalMediaFormat & mediaFormat,
848  unsigned sessionID,
849  bool isSource,
850  PINDEX buffers,
851  unsigned bufferTime,
852  const PString & deviceName
853  );
855 
863  virtual PBoolean SetDataSize(
864  PINDEX dataSize,
865  PINDEX frameTime
866  );
867 
871  virtual PBoolean IsSynchronous() const;
873 
874  protected:
875  PINDEX m_soundChannelBuffers;
876  unsigned m_soundChannelBufferTime;
877 };
878 
879 #endif // OPAL_PTLIB_AUDIO
880 
881 #if OPAL_VIDEO
882 
886 class PVideoInputDevice;
887 class PVideoOutputDevice;
888 
890 {
892  public:
898  OpalConnection & conn,
899  const OpalMediaFormat & mediaFormat,
900  unsigned sessionID,
901  PVideoInputDevice * inputDevice,
902  PVideoOutputDevice * outputDevice,
903  bool autoDeleteInput = true,
904  bool autoDeleteOutput = true
905  );
906 
911 
919  virtual PBoolean Open();
920 
928  virtual PBoolean ExecuteCommand(
929  const OpalMediaCommand & command
930  );
931 
937  virtual PBoolean ReadData(
938  BYTE * data,
939  PINDEX size,
940  PINDEX & length
941  );
942 
948  virtual PBoolean WriteData(
949  const BYTE * data,
950  PINDEX length,
951  PINDEX & written
952  );
953 
957  virtual PBoolean IsSynchronous() const;
958 
961  virtual PBoolean SetDataSize(
962  PINDEX dataSize,
963  PINDEX frameTime
964  );
965 
968  virtual void SetVideoInputDevice(
969  PVideoInputDevice * device,
970  bool autoDelete = true
971  );
972 
975  virtual PVideoInputDevice * GetVideoInputDevice() const
976  {
977  return m_inputDevice;
978  }
979 
982  virtual void SetVideoOutputDevice(
983  PVideoOutputDevice * device,
984  bool autoDelete = true
985  );
986 
989  virtual PVideoOutputDevice * GetVideoOutputDevice() const
990  {
991  return m_outputDevice;
992  }
993 
995 
996  protected:
997  virtual void InternalClose();
998  virtual bool InternalUpdateMediaFormat(const OpalMediaFormat & newMediaFormat);
999  bool InternalAdjustDevices();
1000 
1001  PVideoInputDevice * m_inputDevice;
1002  PVideoOutputDevice * m_outputDevice;
1005  PTimeInterval m_lastGrabTime;
1008 };
1009 
1010 #endif // OPAL_VIDEO
1011 
1012 class OpalTransportUDP;
1013 
1017 {
1018  PCLASSINFO(OpalUDPMediaStream, OpalMediaStream);
1019  public:
1025  OpalConnection & conn,
1026  const OpalMediaFormat & mediaFormat,
1027  unsigned sessionID,
1028  bool isSource,
1029  OpalTransportUDP & transport
1030  );
1032 
1034 
1037 
1041  virtual PBoolean ReadPacket(
1042  RTP_DataFrame & packet
1043  );
1044 
1048  virtual PBoolean WritePacket(
1049  RTP_DataFrame & packet
1050  );
1051 
1055  virtual PBoolean IsSynchronous() const;
1057 
1058  private:
1059  virtual void InternalClose();
1060 
1061  OpalTransportUDP & udpTransport;
1062 };
1063 
1064 
1065 #endif //OPAL_OPAL_MEDIASTRM_H
1066 
1067 
1068 // End of File ///////////////////////////////////////////////////////////////