MultiFramedRTPSink.hh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // RTP sink for a common kind of payload format: Those which pack multiple,
  17. // complete codec frames (as many as possible) into each RTP packet.
  18. // C++ header
  19. #ifndef _MULTI_FRAMED_RTP_SINK_HH
  20. #define _MULTI_FRAMED_RTP_SINK_HH
  21. #ifndef _RTP_SINK_HH
  22. #include "RTPSink.hh"
  23. #endif
  24. class MultiFramedRTPSink: public RTPSink {
  25. public:
  26. void setPacketSizes(unsigned preferredPacketSize, unsigned maxPacketSize);
  27. typedef void (onSendErrorFunc)(void* clientData);
  28. void setOnSendErrorFunc(onSendErrorFunc* onSendErrorFunc, void* onSendErrorFuncData) {
  29. // Can be used to set a callback function to be called if there's an error sending RTP packets on our socket.
  30. fOnSendErrorFunc = onSendErrorFunc;
  31. fOnSendErrorData = onSendErrorFuncData;
  32. }
  33. protected:
  34. MultiFramedRTPSink(UsageEnvironment& env,
  35. Groupsock* rtpgs, unsigned char rtpPayloadType,
  36. unsigned rtpTimestampFrequency,
  37. char const* rtpPayloadFormatName,
  38. unsigned numChannels = 1);
  39. // we're a virtual base class
  40. virtual ~MultiFramedRTPSink();
  41. virtual void doSpecialFrameHandling(unsigned fragmentationOffset,
  42. unsigned char* frameStart,
  43. unsigned numBytesInFrame,
  44. struct timeval framePresentationTime,
  45. unsigned numRemainingBytes);
  46. // perform any processing specific to the particular payload format
  47. virtual Boolean allowFragmentationAfterStart() const;
  48. // whether a frame can be fragmented if other frame(s) appear earlier
  49. // in the packet (by default: False)
  50. virtual Boolean allowOtherFramesAfterLastFragment() const;
  51. // whether other frames can be packed into a packet following the
  52. // final fragment of a previous, fragmented frame (by default: False)
  53. virtual Boolean frameCanAppearAfterPacketStart(unsigned char const* frameStart,
  54. unsigned numBytesInFrame) const;
  55. // whether this frame can appear in position >1 in a pkt (default: True)
  56. virtual unsigned specialHeaderSize() const;
  57. // returns the size of any special header used (following the RTP header) (default: 0)
  58. virtual unsigned frameSpecificHeaderSize() const;
  59. // returns the size of any frame-specific header used (before each frame
  60. // within the packet) (default: 0)
  61. virtual unsigned computeOverflowForNewFrame(unsigned newFrameSize) const;
  62. // returns the number of overflow bytes that would be produced by adding a new
  63. // frame of size "newFrameSize" to the current RTP packet.
  64. // (By default, this just calls "numOverflowBytes()", but subclasses can redefine
  65. // this to (e.g.) impose a granularity upon RTP payload fragments.)
  66. // Functions that might be called by doSpecialFrameHandling(), or other subclass virtual functions:
  67. Boolean isFirstPacket() const { return fIsFirstPacket; }
  68. Boolean isFirstFrameInPacket() const { return fNumFramesUsedSoFar == 0; }
  69. unsigned curFragmentationOffset() const { return fCurFragmentationOffset; }
  70. void setMarkerBit();
  71. void setTimestamp(struct timeval framePresentationTime);
  72. void setSpecialHeaderWord(unsigned word, /* 32 bits, in host order */
  73. unsigned wordPosition = 0);
  74. void setSpecialHeaderBytes(unsigned char const* bytes, unsigned numBytes,
  75. unsigned bytePosition = 0);
  76. void setFrameSpecificHeaderWord(unsigned word, /* 32 bits, in host order */
  77. unsigned wordPosition = 0);
  78. void setFrameSpecificHeaderBytes(unsigned char const* bytes, unsigned numBytes,
  79. unsigned bytePosition = 0);
  80. void setFramePadding(unsigned numPaddingBytes);
  81. unsigned numFramesUsedSoFar() const { return fNumFramesUsedSoFar; }
  82. unsigned ourMaxPacketSize() const { return fOurMaxPacketSize; }
  83. public: // redefined virtual functions:
  84. virtual void stopPlaying();
  85. protected: // redefined virtual functions:
  86. virtual Boolean continuePlaying();
  87. private:
  88. void buildAndSendPacket(Boolean isFirstPacket);
  89. void packFrame();
  90. void sendPacketIfNecessary();
  91. static void sendNext(void* firstArg);
  92. friend void sendNext(void*);
  93. static void afterGettingFrame(void* clientData,
  94. unsigned numBytesRead, unsigned numTruncatedBytes,
  95. struct timeval presentationTime,
  96. unsigned durationInMicroseconds);
  97. void afterGettingFrame1(unsigned numBytesRead, unsigned numTruncatedBytes,
  98. struct timeval presentationTime,
  99. unsigned durationInMicroseconds);
  100. Boolean isTooBigForAPacket(unsigned numBytes) const;
  101. static void ourHandleClosure(void* clientData);
  102. private:
  103. OutPacketBuffer* fOutBuf;
  104. Boolean fNoFramesLeft;
  105. unsigned fNumFramesUsedSoFar;
  106. unsigned fCurFragmentationOffset;
  107. Boolean fPreviousFrameEndedFragmentation;
  108. Boolean fIsFirstPacket;
  109. struct timeval fNextSendTime;
  110. unsigned fTimestampPosition;
  111. unsigned fSpecialHeaderPosition;
  112. unsigned fSpecialHeaderSize; // size in bytes of any special header used
  113. unsigned fCurFrameSpecificHeaderPosition;
  114. unsigned fCurFrameSpecificHeaderSize; // size in bytes of cur frame-specific header
  115. unsigned fTotalFrameSpecificHeaderSizes; // size of all frame-specific hdrs in pkt
  116. unsigned fOurMaxPacketSize;
  117. onSendErrorFunc* fOnSendErrorFunc;
  118. void* fOnSendErrorData;
  119. };
  120. #endif