MPEG2TransportStreamMultiplexor.hh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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-2020 Live Networks, Inc. All rights reserved.
  16. // A class for generating MPEG-2 Transport Stream from one or more input
  17. // Elementary Stream data sources
  18. // C++ header
  19. #ifndef _MPEG2_TRANSPORT_STREAM_MULTIPLEXOR_HH
  20. #define _MPEG2_TRANSPORT_STREAM_MULTIPLEXOR_HH
  21. #ifndef _FRAMED_SOURCE_HH
  22. #include "FramedSource.hh"
  23. #endif
  24. #ifndef _MPEG_1OR2_DEMUX_HH
  25. #include "MPEG1or2Demux.hh" // for SCR
  26. #endif
  27. #define PID_TABLE_SIZE 0x2000 // 2^13
  28. class MPEG2TransportStreamMultiplexor: public FramedSource {
  29. public:
  30. typedef void (onEndOfSegmentFunc)(void* clientData, double segmentDuration);
  31. void setTimedSegmentation(unsigned segmentationDuration,
  32. onEndOfSegmentFunc* onEndOfSegmentFunc = NULL,
  33. void* onEndOfSegmentClientData = NULL);
  34. // Specifies that PAT and PMT packets should be output every "segmentationDuration" seconds.
  35. // (If "segmentationDuration" is 0 (the default value), then PAT and PMT packets are output
  36. // at a preset frequency.)
  37. // The optional function "onEndOfSegmentFunc" is called after each segment is output.
  38. double currentSegmentDuration() const { return fCurrentSegmentDuration; }
  39. // Valid only if "setTimedSegmentation()" was previously called with "segmentationDuration" > 0
  40. Boolean canDeliverNewFrameImmediately() const { return fInputBufferBytesUsed < fInputBufferSize; }
  41. // Can be used by a downstream reader to test whether the next call to "doGetNextFrame()"
  42. // will deliver data immediately).
  43. protected:
  44. MPEG2TransportStreamMultiplexor(UsageEnvironment& env);
  45. virtual ~MPEG2TransportStreamMultiplexor();
  46. virtual void awaitNewBuffer(unsigned char* oldBuffer) = 0;
  47. // implemented by subclasses
  48. void handleNewBuffer(unsigned char* buffer, unsigned bufferSize,
  49. int mpegVersion, MPEG1or2Demux::SCR scr, int16_t PID = -1);
  50. // called by "awaitNewBuffer()"
  51. // Note: For MPEG-4 video, set "mpegVersion" to 4; for H.264 video, set "mpegVersion" to 5;
  52. // for H.265 video, set "mpegVersion" to 6.
  53. // For Opus audio, set "mpegVersion" to 3.
  54. // The buffer is assumed to be a PES packet, with a proper PES header.
  55. // If "PID" is not -1, then it (currently, only the low 8 bits) is used as the stream's PID,
  56. // otherwise the "stream_id" in the PES header is reused to be the stream's PID.
  57. private:
  58. // Redefined virtual functions:
  59. virtual Boolean isMPEG2TransportStreamMultiplexor() const;
  60. virtual void doGetNextFrame();
  61. private:
  62. void deliverDataToClient(u_int16_t pid, unsigned char* buffer, unsigned bufferSize,
  63. unsigned& startPositionInBuffer);
  64. void deliverPATPacket();
  65. void deliverPMTPacket(Boolean hasChanged);
  66. void setProgramStreamMap(unsigned frameSize);
  67. protected:
  68. Boolean fHaveVideoStreams;
  69. private:
  70. unsigned fOutgoingPacketCounter;
  71. unsigned fProgramMapVersion;
  72. u_int8_t fPreviousInputProgramMapVersion, fCurrentInputProgramMapVersion;
  73. // These two fields are used if we see "program_stream_map"s in the input.
  74. struct {
  75. unsigned counter;
  76. u_int8_t streamType; // for use in Program Maps
  77. } fPIDState[PID_TABLE_SIZE];
  78. u_int16_t fPCR_PID, fCurrentPID; // only the low 13 bits are used
  79. MPEG1or2Demux::SCR fPCR;
  80. unsigned char* fInputBuffer;
  81. unsigned fInputBufferSize, fInputBufferBytesUsed;
  82. Boolean fIsFirstAdaptationField;
  83. unsigned fSegmentationDuration;
  84. // if nonzero, this is the number of seconds between successive 'segments'. Each 'segment'
  85. // begins with a PAT, followed by a PMT.
  86. // if zero (the default value), then the frequency of PATs and PMTs depends on the constants
  87. // PAT_PERIOD_IF_UNTIMED and PMT_PERIOD_IF_UNTIMED, defined in the .cpp file.
  88. Boolean segmentationIsTimed() const { return fSegmentationDuration > 0; }
  89. u_int8_t fSegmentationIndication;
  90. // used only if fSegmentationDuration > 0:
  91. // 1 if a segment has just ended and the next packet is to be a PAT
  92. // 2 if a segment has just ended and the following PAT has been sent; a PMT is next
  93. // 0 otherwise
  94. double fCurrentSegmentDuration, fPreviousPTS; // used only if fSegmentationDuration > 0
  95. onEndOfSegmentFunc* fOnEndOfSegmentFunc; // used only if fSegmentationDuration > 0
  96. void* fOnEndOfSegmentClientData; // ditto
  97. };
  98. // The CRC calculation function that Transport Streams use. We make this function public
  99. // here in case it's useful elsewhere:
  100. u_int32_t calculateCRC(u_int8_t const* data, unsigned dataLength, u_int32_t initialValue = 0xFFFFFFFF);
  101. #endif