OPAL  Version 3.14.3
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: 31688 $
28  * $Author: rjongbloed $
29  * $Date: 2014-04-07 15:10:24 +1000 (Mon, 07 Apr 2014) $
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_config.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  bool ExecuteCommand(
194  const OpalMediaCommand & command
195  ) const;
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  bool SetPaused(
385  bool pause
386  ) { return InternalSetPaused(pause, true, false); }
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  P_DECLARE_BITWISE_ENUM(Details, 6,(
417  DetailMinimum,
418  DetailEOL, // end of line
419  DetailNAT,
420  DetailSecured,
421  DetailFEC,
422  DetailAudio,
423  DetailAddresses
424  ));
425  virtual void PrintDetail(
426  ostream & strm,
427  const char * prefix = NULL,
428  Details details = Details::All()
429  ) const;
431 
432  virtual bool InternalUpdateMediaFormat(const OpalMediaFormat & mediaFormat);
433  virtual bool InternalSetPaused(bool pause, bool fromUser, bool fromPatch);
434  virtual bool InternalExecuteCommand(const OpalMediaCommand & command);
435 
436  protected:
437  void IncrementTimestamp(PINDEX size);
438  bool InternalWriteData(const BYTE * data, PINDEX length, PINDEX & written);
439  OpalMediaPatchPtr InternalSetPatchPart1(OpalMediaPatch * newPatch);
440  void InternalSetPatchPart2(const OpalMediaPatchPtr & oldPatch);
441  virtual bool InternalSetJitterBuffer(const OpalJitterBuffer::Init & init) const;
442 
448  virtual void InternalClose() = 0;
449 
451  unsigned sessionID;
453  PString identifier;
455  bool m_paused;
457  bool m_isOpen;
459  unsigned timestamp;
460  bool marker;
461 
462  OpalMediaPatchPtr m_mediaPatch;
463 
465  unsigned m_frameTime;
466  PINDEX m_frameSize;
467 
468  typedef OpalMediaPatchPtr PatchPtr; // For backward compatibility
469 
470  private:
471  P_REMOVE_VIRTUAL_VOID(OnPatchStart());
472  P_REMOVE_VIRTUAL_VOID(OnPatchStop());
473  P_REMOVE_VIRTUAL_VOID(OnStopMediaPatch());
474  P_REMOVE_VIRTUAL_VOID(RemovePatch(OpalMediaPatch *));
475 
476  friend class OpalMediaPatch;
477 };
478 
479 typedef PSafePtr<OpalMediaStream> OpalMediaStreamPtr;
480 
481 
485 {
486  public:
488  const OpalMediaFormat & mediaFormat
489  );
490 
492  void Pace(
493  bool reading,
494  PINDEX bytes,
495  bool & marker
496  );
497 
498  bool UpdateMediaFormat(
499  const OpalMediaFormat & mediaFormat
500  );
501 
502  protected:
504  unsigned m_frameTime;
505  PINDEX m_frameSize;
506  unsigned m_timeUnits;
507  PAdaptiveDelay m_delay;
508 };
509 
510 
514 {
516  public:
522  OpalConnection & conn,
523  const OpalMediaFormat & mediaFormat,
524  unsigned sessionID,
525  bool isSource,
526  bool isSynchronous = false
527  );
529  OpalConnection & conn,
530  const OpalMediaFormat & mediaFormat,
531  unsigned sessionID,
532  bool isSource,
533  bool usePacingDelay,
534  bool requiresPatchThread
535  );
537 
543  virtual PBoolean ReadData(
544  BYTE * data,
545  PINDEX size,
546  PINDEX & length
547  );
548 
552  virtual PBoolean WriteData(
553  const BYTE * data,
554  PINDEX length,
555  PINDEX & written
556  );
557 
561  virtual PBoolean RequiresPatchThread() const;
562 
566  virtual PBoolean IsSynchronous() const;
568 
569  protected:
570  virtual void InternalClose() { }
571  virtual bool InternalUpdateMediaFormat(const OpalMediaFormat & newMediaFormat);
572  virtual bool InternalSetPaused(bool pause, bool fromUser, bool fromPatch);
573 
576 };
577 
578 
583 {
584  PCLASSINFO(OpalRTPMediaStream, OpalMediaStream);
585  public:
592  OpalRTPConnection & conn,
593  const OpalMediaFormat & mediaFormat,
594  bool isSource,
596  );
597 
603 
610  virtual PBoolean Open();
611 
614  virtual bool IsOpen() const;
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  virtual bool InternalSetPaused(bool pause, bool fromUser, bool fromPatch);
671 
673 #if OPAL_VIDEO
676 #endif
677 };
678 
679 
680 
684 {
685  PCLASSINFO(OpalRawMediaStream, OpalMediaStream);
686  protected:
692  OpalConnection & conn,
693  const OpalMediaFormat & mediaFormat,
694  unsigned sessionID,
695  bool isSource,
696  PChannel * channel,
697  bool autoDelete
698  );
699 
704 
705  public:
711  virtual PBoolean ReadData(
712  BYTE * data,
713  PINDEX size,
714  PINDEX & length
715  );
716 
720  virtual PBoolean WriteData(
721  const BYTE * data,
722  PINDEX length,
723  PINDEX & written
724  );
725 
728  PChannel * GetChannel() { return m_channel; }
729 
732  bool SetChannel(
733  PChannel * channel,
734  bool autoDelete = true
735  );
736 
739  virtual unsigned GetAverageSignalLevel();
741 
742  protected:
743  virtual void InternalClose();
744 
745  PChannel * m_channel;
748 
749  PBYTEArray m_silence;
750 
754 
755  void CollectAverage(const BYTE * buffer, PINDEX size);
756 };
757 
758 
759 
763 {
765  public:
771  OpalConnection & conn,
772  const OpalMediaFormat & mediaFormat,
773  unsigned sessionID,
774  bool isSource,
775  PFile * file,
776  bool autoDelete = true
777  );
778 
782  OpalConnection & conn,
783  const OpalMediaFormat & mediaFormat,
784  unsigned sessionID,
785  bool isSource,
786  const PFilePath & path
787  );
789 
795  virtual PBoolean IsSynchronous() const;
796 
797  virtual PBoolean ReadData(
798  BYTE * data,
799  PINDEX size,
800  PINDEX & length
801  );
802 
806  virtual PBoolean WriteData(
807  const BYTE * data,
808  PINDEX length,
809  PINDEX & written
810  );
812 
813  protected:
814  PFile file;
815 };
816 
817 
818 #if OPAL_PTLIB_AUDIO
819 
823 class PSoundChannel;
824 
825 class OpalAudioMediaStream : public OpalRawMediaStream
826 {
827  PCLASSINFO(OpalAudioMediaStream, OpalRawMediaStream);
828  public:
833  OpalAudioMediaStream(
834  OpalConnection & conn,
835  const OpalMediaFormat & mediaFormat,
836  unsigned sessionID,
837  bool isSource,
838  PINDEX buffers,
839  unsigned bufferTime,
840  PSoundChannel * channel,
841  bool autoDelete = true
842  );
843 
846  OpalAudioMediaStream(
847  OpalConnection & conn,
848  const OpalMediaFormat & mediaFormat,
849  unsigned sessionID,
850  bool isSource,
851  PINDEX buffers,
852  unsigned bufferTime,
853  const PString & deviceName
854  );
856 
864  virtual PBoolean SetDataSize(
865  PINDEX dataSize,
866  PINDEX frameTime
867  );
868 
872  virtual PBoolean IsSynchronous() const;
874 
875  protected:
876  PINDEX m_soundChannelBuffers;
877  unsigned m_soundChannelBufferTime;
878 };
879 
880 #endif // OPAL_PTLIB_AUDIO
881 
882 #if OPAL_VIDEO
883 
887 class PVideoInputDevice;
888 class PVideoOutputDevice;
889 
891 {
893  public:
899  OpalConnection & conn,
900  const OpalMediaFormat & mediaFormat,
901  unsigned sessionID,
902  PVideoInputDevice * inputDevice,
903  PVideoOutputDevice * outputDevice,
904  bool autoDeleteInput = true,
905  bool autoDeleteOutput = true
906  );
907 
912 
920  virtual PBoolean Open();
921 
927  virtual PBoolean ReadData(
928  BYTE * data,
929  PINDEX size,
930  PINDEX & length
931  );
932 
938  virtual PBoolean WriteData(
939  const BYTE * data,
940  PINDEX length,
941  PINDEX & written
942  );
943 
947  virtual PBoolean IsSynchronous() const;
948 
951  virtual PBoolean SetDataSize(
952  PINDEX dataSize,
953  PINDEX frameTime
954  );
955 
958  virtual void SetVideoInputDevice(
959  PVideoInputDevice * device,
960  bool autoDelete = true
961  );
962 
965  virtual PVideoInputDevice * GetVideoInputDevice() const
966  {
967  return m_inputDevice;
968  }
969 
972  virtual void SetVideoOutputDevice(
973  PVideoOutputDevice * device,
974  bool autoDelete = true
975  );
976 
979  virtual PVideoOutputDevice * GetVideoOutputDevice() const
980  {
981  return m_outputDevice;
982  }
983 
985 
986  protected:
987  virtual void InternalClose();
988  virtual bool InternalUpdateMediaFormat(const OpalMediaFormat & newMediaFormat);
989  virtual bool InternalExecuteCommand(const OpalMediaCommand & command);
990  bool InternalAdjustDevices();
991 
992  PVideoInputDevice * m_inputDevice;
993  PVideoOutputDevice * m_outputDevice;
996  PTimeInterval m_lastGrabTime;
999 };
1000 
1001 #endif // OPAL_VIDEO
1002 
1003 class OpalTransportUDP;
1004 
1008 {
1009  PCLASSINFO(OpalUDPMediaStream, OpalMediaStream);
1010  public:
1016  OpalConnection & conn,
1017  const OpalMediaFormat & mediaFormat,
1018  unsigned sessionID,
1019  bool isSource,
1020  OpalTransportUDP & transport
1021  );
1023 
1025 
1028 
1032  virtual PBoolean ReadPacket(
1033  RTP_DataFrame & packet
1034  );
1035 
1039  virtual PBoolean WritePacket(
1040  RTP_DataFrame & packet
1041  );
1042 
1046  virtual PBoolean IsSynchronous() const;
1048 
1049  private:
1050  virtual void InternalClose();
1051 
1052  OpalTransportUDP & udpTransport;
1053 };
1054 
1055 
1056 #endif //OPAL_OPAL_MEDIASTRM_H
1057 
1058 
1059 // End of File ///////////////////////////////////////////////////////////////