OPAL  Version 3.10.10
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: 27587 $
28  * $Author: rjongbloed $
29  * $Date: 2012-05-07 21:26:25 -0500 (Mon, 07 May 2012) $
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 <ptlib/safecoll.h>
46 #include <ptclib/guid.h>
47 
48 
49 class RTP_Session;
50 class OpalMediaPatch;
51 class OpalLine;
52 class OpalConnection;
53 class OpalRTPConnection;
55 
111 class OpalMediaStream : public PSafeObject
112 {
113  PCLASSINFO(OpalMediaStream, PSafeObject);
114  protected:
120  OpalConnection & conn,
121  const OpalMediaFormat & mediaFormat,
122  unsigned sessionID,
123  bool isSource
124  );
125 
126  public:
132 
133  public:
140  void PrintOn(
141  ostream & strm
142  ) const;
144 
154  virtual OpalMediaFormat GetMediaFormat() const;
155 
165  bool UpdateMediaFormat(
166  const OpalMediaFormat & mediaFormat
167  );
168  virtual bool InternalUpdateMediaFormat(
169  const OpalMediaFormat & mediaFormat
170  );
171 
180  virtual PBoolean ExecuteCommand(
181  const OpalMediaCommand & command
182  );
183 
188  virtual PBoolean Open();
189 
195  virtual PBoolean Start();
196 
202  virtual PBoolean Close();
203 
207  virtual void OnStartMediaPatch();
208 
212  virtual void OnStopMediaPatch(
213  OpalMediaPatch & patch
214  );
215 
220  virtual PBoolean WritePackets(
221  RTP_DataFrameList & packets
222  );
223 
229  virtual PBoolean ReadPacket(
230  RTP_DataFrame & packet
231  );
232 
238  virtual PBoolean WritePacket(
239  RTP_DataFrame & packet
240  );
241 
247  virtual PBoolean ReadData(
248  BYTE * data,
249  PINDEX size,
250  PINDEX & length
251  );
252 
258  virtual PBoolean WriteData(
259  const BYTE * data,
260  PINDEX length,
261  PINDEX & written
262  );
263 
266  bool PushPacket(
267  RTP_DataFrame & packet
268  );
269 
275  virtual PBoolean SetDataSize(
276  PINDEX dataSize,
277  PINDEX frameTime
278  );
279 
283  PINDEX GetDataSize() const { return defaultDataSize; }
284 
291  virtual PBoolean IsSynchronous() const = 0;
292 
302  virtual PBoolean RequiresPatchThread(
303  OpalMediaStream * stream
304  ) const;
305  virtual PBoolean RequiresPatchThread() const; // For backward compatibility
306 
313  virtual bool EnableJitterBuffer(bool enab = true) const;
315 
321 
324  bool IsSource() const { return isSource; }
325 
328  bool IsSink() const { return !isSource; }
329 
332  unsigned GetSessionID() const { return sessionID; }
333 
336  void SetSessionID(unsigned id) { sessionID = id; }
337 
341  PString GetID() const { return identifier; }
342 
345  unsigned GetTimestamp() const { return timestamp; }
346 
349  void SetTimestamp(unsigned ts) { timestamp = ts; }
350 
353  bool GetMarker() const { return marker; }
354 
357  void SetMarker(bool m) { marker = m; }
358 
361  bool IsPaused() const { return m_paused; }
362 
367  virtual bool SetPaused(
368  bool pause,
369  bool fromPatch = false
370  );
371 
374  bool IsOpen() const { return isOpen; }
375 
378  virtual PBoolean SetPatch(
379  OpalMediaPatch * patch
380  );
381 
384  OpalMediaPatch * GetPatch() const { return m_mediaPatch; }
385 
388  void AddFilter(
389  const PNotifier & filter,
390  const OpalMediaFormat & stage = OpalMediaFormat()
391  ) const;
392 
395  bool RemoveFilter(
396  const PNotifier & filter,
397  const OpalMediaFormat & stage = OpalMediaFormat()
398  ) const;
399 
400 #if OPAL_STATISTICS
401  virtual void GetStatistics(OpalMediaStatistics & statistics, bool fromPatch = false) const;
402 #endif
403 
404 
405  protected:
406  void IncrementTimestamp(PINDEX size);
407  bool InternalWriteData(const BYTE * data, PINDEX length, PINDEX & written);
408 
414  virtual void InternalClose() = 0;
415 
417  unsigned sessionID;
418  PString identifier;
420  bool m_paused;
421  bool isSource;
422  bool isOpen;
424  unsigned timestamp;
425  bool marker;
427 
428  typedef PSafePtr<OpalMediaPatch, PSafePtrMultiThreaded> PatchPtr;
430 
432  unsigned m_frameTime;
433  PINDEX m_frameSize;
434 
435  private:
436  P_REMOVE_VIRTUAL_VOID(OnPatchStart());
437  P_REMOVE_VIRTUAL_VOID(OnPatchStop());
438  P_REMOVE_VIRTUAL_VOID(OnStopMediaPatch());
439  P_REMOVE_VIRTUAL_VOID(RemovePatch(OpalMediaPatch *));
440 };
441 
442 typedef PSafePtr<OpalMediaStream> OpalMediaStreamPtr;
443 
444 
448 {
449  public:
451  const OpalMediaFormat & mediaFormat
452  );
453 
455  void Pace(
456  bool reading,
457  PINDEX bytes,
458  bool & marker
459  );
460 
461  protected:
462  bool m_isAudio;
463  unsigned m_frameTime;
464  PINDEX m_frameSize;
465  unsigned m_timeUnits;
466  PAdaptiveDelay m_delay;
467 };
468 
469 
473 {
475  public:
481  OpalConnection & conn,
482  const OpalMediaFormat & mediaFormat,
483  unsigned sessionID,
484  bool isSource,
485  bool isSynchronous = false
486  );
488  OpalConnection & conn,
489  const OpalMediaFormat & mediaFormat,
490  unsigned sessionID,
491  bool isSource,
492  bool usePacingDelay,
493  bool requiresPatchThread
494  );
496 
502  virtual PBoolean ReadData(
503  BYTE * data,
504  PINDEX size,
505  PINDEX & length
506  );
507 
511  virtual PBoolean WriteData(
512  const BYTE * data,
513  PINDEX length,
514  PINDEX & written
515  );
516 
521  virtual bool SetPaused(
522  bool pause,
523  bool fromPatch = false
524  );
525 
529  virtual PBoolean RequiresPatchThread() const;
530 
534  virtual PBoolean IsSynchronous() const;
536 
537  protected:
538  virtual void InternalClose() { }
539 
542 };
543 
544 
549 {
550  PCLASSINFO(OpalRTPMediaStream, OpalMediaStream);
551  public:
558  OpalRTPConnection & conn,
559  const OpalMediaFormat & mediaFormat,
560  bool isSource,
562  unsigned minAudioJitterDelay,
563  unsigned maxAudioJitterDelay
564  );
565 
571 
578  virtual PBoolean Open();
579 
583  virtual bool SetPaused(
584  bool pause,
585  bool fromPatch = false
586  );
587 
591  virtual PBoolean ReadPacket(
592  RTP_DataFrame & packet
593  );
594 
598  virtual PBoolean WritePacket(
599  RTP_DataFrame & packet
600  );
601 
604  virtual PBoolean SetDataSize(
605  PINDEX dataSize,
606  PINDEX frameTime
607  );
608 
612  virtual PBoolean IsSynchronous() const;
613 
621  virtual PBoolean RequiresPatchThread() const;
622 
630  virtual bool EnableJitterBuffer(bool enab = true) const;
631 
634  virtual PBoolean SetPatch(
635  OpalMediaPatch * patch
636  );
637 
640  virtual RTP_Session & GetRtpSession() const
641  { return rtpSession; }
642 
643 #if OPAL_STATISTICS
644  virtual void GetStatistics(OpalMediaStatistics & statistics, bool fromPatch = false) const;
645 #endif
646 
647 
648  protected:
649  virtual void InternalClose();
650 
654 };
655 
656 
657 
661 {
662  PCLASSINFO(OpalRawMediaStream, OpalMediaStream);
663  protected:
669  OpalConnection & conn,
670  const OpalMediaFormat & mediaFormat,
671  unsigned sessionID,
672  bool isSource,
673  PChannel * channel,
674  bool autoDelete
675  );
676 
681 
682  public:
688  virtual PBoolean ReadData(
689  BYTE * data,
690  PINDEX size,
691  PINDEX & length
692  );
693 
697  virtual PBoolean WriteData(
698  const BYTE * data,
699  PINDEX length,
700  PINDEX & written
701  );
702 
705  PChannel * GetChannel() { return m_channel; }
706 
709  bool SetChannel(
710  PChannel * channel,
711  bool autoDelete = true
712  );
713 
716  virtual unsigned GetAverageSignalLevel();
718 
719  protected:
720  virtual void InternalClose();
721 
722  PChannel * m_channel;
725 
726  PBYTEArray m_silence;
727 
731 
732  void CollectAverage(const BYTE * buffer, PINDEX size);
733 };
734 
735 
736 
740 {
742  public:
748  OpalConnection & conn,
749  const OpalMediaFormat & mediaFormat,
750  unsigned sessionID,
751  bool isSource,
752  PFile * file,
753  bool autoDelete = true
754  );
755 
759  OpalConnection & conn,
760  const OpalMediaFormat & mediaFormat,
761  unsigned sessionID,
762  bool isSource,
763  const PFilePath & path
764  );
766 
772  virtual PBoolean IsSynchronous() const;
773 
774  virtual PBoolean ReadData(
775  BYTE * data,
776  PINDEX size,
777  PINDEX & length
778  );
779 
783  virtual PBoolean WriteData(
784  const BYTE * data,
785  PINDEX length,
786  PINDEX & written
787  );
789 
790  protected:
791  PFile file;
792 };
793 
794 
795 #if OPAL_PTLIB_AUDIO
796 
800 class PSoundChannel;
801 
802 class OpalAudioMediaStream : public OpalRawMediaStream
803 {
804  PCLASSINFO(OpalAudioMediaStream, OpalRawMediaStream);
805  public:
810  OpalAudioMediaStream(
811  OpalConnection & conn,
812  const OpalMediaFormat & mediaFormat,
813  unsigned sessionID,
814  bool isSource,
815  PINDEX buffers,
816  unsigned bufferTime,
817  PSoundChannel * channel,
818  bool autoDelete = true
819  );
820 
823  OpalAudioMediaStream(
824  OpalConnection & conn,
825  const OpalMediaFormat & mediaFormat,
826  unsigned sessionID,
827  bool isSource,
828  PINDEX buffers,
829  unsigned bufferTime,
830  const PString & deviceName
831  );
833 
841  virtual PBoolean SetDataSize(
842  PINDEX dataSize,
843  PINDEX frameTime
844  );
845 
849  virtual PBoolean IsSynchronous() const;
851 
852  protected:
853  PINDEX m_soundChannelBuffers;
854  unsigned m_soundChannelBufferTime;
855 };
856 
857 #endif // OPAL_PTLIB_AUDIO
858 
859 #if OPAL_VIDEO
860 
864 class PVideoInputDevice;
865 class PVideoOutputDevice;
866 
868 {
870  public:
876  OpalConnection & conn,
877  const OpalMediaFormat & mediaFormat,
878  unsigned sessionID,
879  PVideoInputDevice * inputDevice,
880  PVideoOutputDevice * outputDevice,
881  bool autoDeleteInput = true,
882  bool autoDeleteOutput = true
883  );
884 
889 
898  virtual bool InternalUpdateMediaFormat(
899  const OpalMediaFormat & mediaFormat
900  );
901 
907  virtual PBoolean Open();
908 
914  virtual PBoolean ReadData(
915  BYTE * data,
916  PINDEX size,
917  PINDEX & length
918  );
919 
925  virtual PBoolean WriteData(
926  const BYTE * data,
927  PINDEX length,
928  PINDEX & written
929  );
930 
934  virtual PBoolean IsSynchronous() const;
935 
938  virtual PBoolean SetDataSize(
939  PINDEX dataSize,
940  PINDEX frameTime
941  );
942 
945  virtual PVideoInputDevice * GetVideoInputDevice() const
946  {
947  return m_inputDevice;
948  }
949 
952  virtual PVideoOutputDevice * GetVideoOutputDevice() const
953  {
954  return m_outputDevice;
955  }
956 
958 
959  protected:
960  virtual void InternalClose();
961 
962  PVideoInputDevice * m_inputDevice;
963  PVideoOutputDevice * m_outputDevice;
966  PTimeInterval m_lastGrabTime;
967 };
968 
969 #endif // OPAL_VIDEO
970 
971 class OpalTransportUDP;
972 
976 {
977  PCLASSINFO(OpalUDPMediaStream, OpalMediaStream);
978  public:
984  OpalConnection & conn,
985  const OpalMediaFormat & mediaFormat,
986  unsigned sessionID,
987  bool isSource,
988  OpalTransportUDP & transport
989  );
991 
993 
996 
1000  virtual PBoolean ReadPacket(
1001  RTP_DataFrame & packet
1002  );
1003 
1007  virtual PBoolean WritePacket(
1008  RTP_DataFrame & packet
1009  );
1010 
1014  virtual PBoolean IsSynchronous() const;
1016 
1017  private:
1018  virtual void InternalClose();
1019 
1020  OpalTransportUDP & udpTransport;
1021 };
1022 
1023 
1024 #endif //OPAL_OPAL_MEDIASTRM_H
1025 
1026 
1027 // End of File ///////////////////////////////////////////////////////////////