MediaSink.hh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**********
  2. This library is free software; you can redistribute it and/or modify it under
  3. the terms of the GNU Lesser General Public License as published by the
  4. Free Software Foundation; either version 3 of the License, or (at your
  5. option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
  6. This library is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  9. more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with this library; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  13. **********/
  14. // "liveMedia"
  15. // Copyright (c) 1996-2019 Live Networks, Inc. All rights reserved.
  16. // Media Sinks
  17. // C++ header
  18. #ifndef _MEDIA_SINK_HH
  19. #define _MEDIA_SINK_HH
  20. #ifndef _FRAMED_SOURCE_HH
  21. #include "FramedSource.hh"
  22. #endif
  23. class MediaSink: public Medium {
  24. public:
  25. static Boolean lookupByName(UsageEnvironment& env, char const* sinkName,
  26. MediaSink*& resultSink);
  27. typedef void (afterPlayingFunc)(void* clientData);
  28. Boolean startPlaying(MediaSource& source,
  29. afterPlayingFunc* afterFunc,
  30. void* afterClientData);
  31. virtual void stopPlaying();
  32. // Test for specific types of sink:
  33. virtual Boolean isRTPSink() const;
  34. FramedSource* source() const {return fSource;}
  35. protected:
  36. MediaSink(UsageEnvironment& env); // abstract base class
  37. virtual ~MediaSink();
  38. virtual Boolean sourceIsCompatibleWithUs(MediaSource& source);
  39. // called by startPlaying()
  40. virtual Boolean continuePlaying() = 0;
  41. // called by startPlaying()
  42. static void onSourceClosure(void* clientData); // can be used in "getNextFrame()" calls
  43. void onSourceClosure();
  44. // should be called (on ourselves) by continuePlaying() when it
  45. // discovers that the source we're playing from has closed.
  46. FramedSource* fSource;
  47. private:
  48. // redefined virtual functions:
  49. virtual Boolean isSink() const;
  50. private:
  51. // The following fields are used when we're being played:
  52. afterPlayingFunc* fAfterFunc;
  53. void* fAfterClientData;
  54. };
  55. // A data structure that a sink may use for an output packet:
  56. class OutPacketBuffer {
  57. public:
  58. OutPacketBuffer(unsigned preferredPacketSize, unsigned maxPacketSize,
  59. unsigned maxBufferSize = 0);
  60. // if "maxBufferSize" is >0, use it - instead of "maxSize" to compute the buffer size
  61. ~OutPacketBuffer();
  62. static unsigned maxSize;
  63. static void increaseMaxSizeTo(unsigned newMaxSize) { if (newMaxSize > OutPacketBuffer::maxSize) OutPacketBuffer::maxSize = newMaxSize; }
  64. unsigned char* curPtr() const {return &fBuf[fPacketStart + fCurOffset];}
  65. unsigned totalBytesAvailable() const {
  66. return fLimit - (fPacketStart + fCurOffset);
  67. }
  68. unsigned totalBufferSize() const { return fLimit; }
  69. unsigned char* packet() const {return &fBuf[fPacketStart];}
  70. unsigned curPacketSize() const {return fCurOffset;}
  71. void increment(unsigned numBytes) {fCurOffset += numBytes;}
  72. void enqueue(unsigned char const* from, unsigned numBytes);
  73. void enqueueWord(u_int32_t word);
  74. void insert(unsigned char const* from, unsigned numBytes, unsigned toPosition);
  75. void insertWord(u_int32_t word, unsigned toPosition);
  76. void extract(unsigned char* to, unsigned numBytes, unsigned fromPosition);
  77. u_int32_t extractWord(unsigned fromPosition);
  78. void skipBytes(unsigned numBytes);
  79. Boolean isPreferredSize() const {return fCurOffset >= fPreferred;}
  80. Boolean wouldOverflow(unsigned numBytes) const {
  81. return (fCurOffset+numBytes) > fMax;
  82. }
  83. unsigned numOverflowBytes(unsigned numBytes) const {
  84. return (fCurOffset+numBytes) - fMax;
  85. }
  86. Boolean isTooBigForAPacket(unsigned numBytes) const {
  87. return numBytes > fMax;
  88. }
  89. void setOverflowData(unsigned overflowDataOffset,
  90. unsigned overflowDataSize,
  91. struct timeval const& presentationTime,
  92. unsigned durationInMicroseconds);
  93. unsigned overflowDataSize() const {return fOverflowDataSize;}
  94. struct timeval overflowPresentationTime() const {return fOverflowPresentationTime;}
  95. unsigned overflowDurationInMicroseconds() const {return fOverflowDurationInMicroseconds;}
  96. Boolean haveOverflowData() const {return fOverflowDataSize > 0;}
  97. void useOverflowData();
  98. void adjustPacketStart(unsigned numBytes);
  99. void resetPacketStart();
  100. void resetOffset() { fCurOffset = 0; }
  101. void resetOverflowData() { fOverflowDataOffset = fOverflowDataSize = 0; }
  102. private:
  103. unsigned fPacketStart, fCurOffset, fPreferred, fMax, fLimit;
  104. unsigned char* fBuf;
  105. unsigned fOverflowDataOffset, fOverflowDataSize;
  106. struct timeval fOverflowPresentationTime;
  107. unsigned fOverflowDurationInMicroseconds;
  108. };
  109. #endif