OPAL  Version 3.10.4
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: 26728 $
28  * $Author: rjongbloed $
29  * $Date: 2011-12-01 22:59:14 -0600 (Thu, 01 Dec 2011) $
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 
201  virtual PBoolean Close();
202 
206  virtual void OnStartMediaPatch();
207 
211  virtual void OnStopMediaPatch(
212  OpalMediaPatch & patch
213  );
214 
219  virtual PBoolean WritePackets(
220  RTP_DataFrameList & packets
221  );
222 
228  virtual PBoolean ReadPacket(
229  RTP_DataFrame & packet
230  );
231 
237  virtual PBoolean WritePacket(
238  RTP_DataFrame & packet
239  );
240 
246  virtual PBoolean ReadData(
247  BYTE * data,
248  PINDEX size,
249  PINDEX & length
250  );
251 
257  virtual PBoolean WriteData(
258  const BYTE * data,
259  PINDEX length,
260  PINDEX & written
261  );
262 
265  bool PushPacket(
266  RTP_DataFrame & packet
267  );
268 
274  virtual PBoolean SetDataSize(
275  PINDEX dataSize,
276  PINDEX frameTime
277  );
278 
282  PINDEX GetDataSize() const { return defaultDataSize; }
283 
290  virtual PBoolean IsSynchronous() const = 0;
291 
301  virtual PBoolean RequiresPatchThread(
302  OpalMediaStream * stream
303  ) const;
304  virtual PBoolean RequiresPatchThread() const; // For backward compatibility
305 
312  virtual bool EnableJitterBuffer(bool enab = true) const;
314 
320 
323  bool IsSource() const { return isSource; }
324 
327  bool IsSink() const { return !isSource; }
328 
331  unsigned GetSessionID() const { return sessionID; }
332 
335  void SetSessionID(unsigned id) { sessionID = id; }
336 
340  PString GetID() const { return identifier; }
341 
344  unsigned GetTimestamp() const { return timestamp; }
345 
348  void SetTimestamp(unsigned ts) { timestamp = ts; }
349 
352  bool GetMarker() const { return marker; }
353 
356  void SetMarker(bool m) { marker = m; }
357 
360  bool IsPaused() const { return m_paused; }
361 
366  virtual bool SetPaused(
367  bool pause,
368  bool fromPatch = false
369  );
370 
373  bool IsOpen() const { return isOpen; }
374 
377  virtual PBoolean SetPatch(
378  OpalMediaPatch * patch
379  );
380 
383  OpalMediaPatch * GetPatch() const { return mediaPatch; }
384 
387  void AddFilter(
388  const PNotifier & filter,
389  const OpalMediaFormat & stage = OpalMediaFormat()
390  ) const;
391 
394  bool RemoveFilter(
395  const PNotifier & filter,
396  const OpalMediaFormat & stage = OpalMediaFormat()
397  ) const;
398 
399 #if OPAL_STATISTICS
400  virtual void GetStatistics(OpalMediaStatistics & statistics, bool fromPatch = false) const;
401 #endif
402 
403 
404  protected:
405  void IncrementTimestamp(PINDEX size);
406  bool InternalWriteData(const BYTE * data, PINDEX length, PINDEX & written);
407 
409  unsigned sessionID;
410  PString identifier;
412  bool m_paused;
413  bool isSource;
414  bool isOpen;
416  unsigned timestamp;
417  bool marker;
419 
421 
423  unsigned m_frameTime;
424  PINDEX m_frameSize;
425 
426  private:
427  P_REMOVE_VIRTUAL_VOID(OnPatchStart());
428  P_REMOVE_VIRTUAL_VOID(OnPatchStop());
429  P_REMOVE_VIRTUAL_VOID(OnStopMediaPatch());
430  P_REMOVE_VIRTUAL_VOID(RemovePatch(OpalMediaPatch *));
431 };
432 
433 typedef PSafePtr<OpalMediaStream> OpalMediaStreamPtr;
434 
435 
439 {
440  public:
442  const OpalMediaFormat & mediaFormat
443  );
444 
446  void Pace(
447  bool reading,
448  PINDEX bytes,
449  bool & marker
450  );
451 
452  protected:
453  bool m_isAudio;
454  unsigned m_frameTime;
455  PINDEX m_frameSize;
456  unsigned m_timeUnits;
457  PAdaptiveDelay m_delay;
458 };
459 
460 
464 {
466  public:
472  OpalConnection & conn,
473  const OpalMediaFormat & mediaFormat,
474  unsigned sessionID,
475  bool isSource,
476  bool isSynchronous = false
477  );
479  OpalConnection & conn,
480  const OpalMediaFormat & mediaFormat,
481  unsigned sessionID,
482  bool isSource,
483  bool usePacingDelay,
484  bool requiresPatchThread
485  );
487 
493  virtual PBoolean ReadData(
494  BYTE * data,
495  PINDEX size,
496  PINDEX & length
497  );
498 
502  virtual PBoolean WriteData(
503  const BYTE * data,
504  PINDEX length,
505  PINDEX & written
506  );
507 
511  virtual PBoolean RequiresPatchThread() const;
512 
516  virtual PBoolean IsSynchronous() const;
518 
519  protected:
522 };
523 
524 
529 {
530  PCLASSINFO(OpalRTPMediaStream, OpalMediaStream);
531  public:
538  OpalRTPConnection & conn,
539  const OpalMediaFormat & mediaFormat,
540  bool isSource,
542  unsigned minAudioJitterDelay,
543  unsigned maxAudioJitterDelay
544  );
545 
551 
558  virtual PBoolean Open();
559 
564  virtual PBoolean Close();
565 
569  virtual bool SetPaused(
570  bool pause,
571  bool fromPatch = false
572  );
573 
577  virtual PBoolean ReadPacket(
578  RTP_DataFrame & packet
579  );
580 
584  virtual PBoolean WritePacket(
585  RTP_DataFrame & packet
586  );
587 
590  virtual PBoolean SetDataSize(
591  PINDEX dataSize,
592  PINDEX frameTime
593  );
594 
598  virtual PBoolean IsSynchronous() const;
599 
607  virtual PBoolean RequiresPatchThread() const;
608 
616  virtual bool EnableJitterBuffer(bool enab = true) const;
617 
620  virtual PBoolean SetPatch(
621  OpalMediaPatch * patch
622  );
623 
626  virtual RTP_Session & GetRtpSession() const
627  { return rtpSession; }
628 
629 #if OPAL_STATISTICS
630  virtual void GetStatistics(OpalMediaStatistics & statistics, bool fromPatch = false) const;
631 #endif
632 
633 
634  protected:
638 };
639 
640 
641 
645 {
646  PCLASSINFO(OpalRawMediaStream, OpalMediaStream);
647  protected:
653  OpalConnection & conn,
654  const OpalMediaFormat & mediaFormat,
655  unsigned sessionID,
656  bool isSource,
657  PChannel * channel,
658  bool autoDelete
659  );
660 
665 
666  public:
672  virtual PBoolean ReadData(
673  BYTE * data,
674  PINDEX size,
675  PINDEX & length
676  );
677 
681  virtual PBoolean WriteData(
682  const BYTE * data,
683  PINDEX length,
684  PINDEX & written
685  );
686 
689  PChannel * GetChannel() { return m_channel; }
690 
693  bool SetChannel(
694  PChannel * channel,
695  bool autoDelete = true
696  );
697 
702  virtual PBoolean Close();
703 
706  virtual unsigned GetAverageSignalLevel();
708 
709  protected:
710  PChannel * m_channel;
713 
714  PBYTEArray m_silence;
715 
719 
720  void CollectAverage(const BYTE * buffer, PINDEX size);
721 };
722 
723 
724 
728 {
730  public:
736  OpalConnection & conn,
737  const OpalMediaFormat & mediaFormat,
738  unsigned sessionID,
739  bool isSource,
740  PFile * file,
741  bool autoDelete = true
742  );
743 
747  OpalConnection & conn,
748  const OpalMediaFormat & mediaFormat,
749  unsigned sessionID,
750  bool isSource,
751  const PFilePath & path
752  );
754 
760  virtual PBoolean IsSynchronous() const;
761 
762  virtual PBoolean ReadData(
763  BYTE * data,
764  PINDEX size,
765  PINDEX & length
766  );
767 
771  virtual PBoolean WriteData(
772  const BYTE * data,
773  PINDEX length,
774  PINDEX & written
775  );
777 
778  protected:
779  PFile file;
780 };
781 
782 
783 #if OPAL_PTLIB_AUDIO
784 
788 class PSoundChannel;
789 
790 class OpalAudioMediaStream : public OpalRawMediaStream
791 {
792  PCLASSINFO(OpalAudioMediaStream, OpalRawMediaStream);
793  public:
798  OpalAudioMediaStream(
799  OpalConnection & conn,
800  const OpalMediaFormat & mediaFormat,
801  unsigned sessionID,
802  bool isSource,
803  PINDEX buffers,
804  unsigned bufferTime,
805  PSoundChannel * channel,
806  bool autoDelete = true
807  );
808 
811  OpalAudioMediaStream(
812  OpalConnection & conn,
813  const OpalMediaFormat & mediaFormat,
814  unsigned sessionID,
815  bool isSource,
816  PINDEX buffers,
817  unsigned bufferTime,
818  const PString & deviceName
819  );
821 
829  virtual PBoolean SetDataSize(
830  PINDEX dataSize,
831  PINDEX frameTime
832  );
833 
837  virtual PBoolean IsSynchronous() const;
839 
840  protected:
841  PINDEX m_soundChannelBuffers;
842  unsigned m_soundChannelBufferTime;
843 };
844 
845 #endif // OPAL_PTLIB_AUDIO
846 
847 #if OPAL_VIDEO
848 
852 class PVideoInputDevice;
853 class PVideoOutputDevice;
854 
856 {
858  public:
864  OpalConnection & conn,
865  const OpalMediaFormat & mediaFormat,
866  unsigned sessionID,
867  PVideoInputDevice * inputDevice,
868  PVideoOutputDevice * outputDevice,
869  bool autoDeleteInput = true,
870  bool autoDeleteOutput = true
871  );
872 
877 
886  virtual bool InternalUpdateMediaFormat(
887  const OpalMediaFormat & mediaFormat
888  );
889 
895  virtual PBoolean Open();
896 
901  virtual PBoolean Close();
902 
908  virtual PBoolean ReadData(
909  BYTE * data,
910  PINDEX size,
911  PINDEX & length
912  );
913 
919  virtual PBoolean WriteData(
920  const BYTE * data,
921  PINDEX length,
922  PINDEX & written
923  );
924 
928  virtual PBoolean IsSynchronous() const;
929 
932  virtual PBoolean SetDataSize(
933  PINDEX dataSize,
934  PINDEX frameTime
935  );
936 
939  virtual PVideoInputDevice * GetVideoInputDevice() const
940  {
941  return m_inputDevice;
942  }
943 
946  virtual PVideoOutputDevice * GetVideoOutputDevice() const
947  {
948  return m_outputDevice;
949  }
950 
952 
953  protected:
954  PVideoInputDevice * m_inputDevice;
955  PVideoOutputDevice * m_outputDevice;
958  PTimeInterval m_lastGrabTime;
959 };
960 
961 #endif // OPAL_VIDEO
962 
963 class OpalTransportUDP;
964 
968 {
969  PCLASSINFO(OpalUDPMediaStream, OpalMediaStream);
970  public:
976  OpalConnection & conn,
977  const OpalMediaFormat & mediaFormat,
978  unsigned sessionID,
979  bool isSource,
980  OpalTransportUDP & transport
981  );
983 
985 
988 
992  virtual PBoolean ReadPacket(
993  RTP_DataFrame & packet
994  );
995 
999  virtual PBoolean WritePacket(
1000  RTP_DataFrame & packet
1001  );
1002 
1006  virtual PBoolean IsSynchronous() const;
1007 
1011  virtual PBoolean Close();
1012 
1014 
1015  private:
1016  OpalTransportUDP & udpTransport;
1017 };
1018 
1019 
1020 #endif //OPAL_OPAL_MEDIASTRM_H
1021 
1022 
1023 // End of File ///////////////////////////////////////////////////////////////