MPEG2TransportStreamMultiplexor.hh 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // 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 256
  28. class MPEG2TransportStreamMultiplexor: public FramedSource {
  29. public:
  30. Boolean canDeliverNewFrameImmediately() const { return fInputBufferBytesUsed < fInputBufferSize; }
  31. // Can be used by a downstream reader to test whether the next call to "doGetNextFrame()"
  32. // will deliver data immediately).
  33. protected:
  34. MPEG2TransportStreamMultiplexor(UsageEnvironment& env);
  35. virtual ~MPEG2TransportStreamMultiplexor();
  36. virtual void awaitNewBuffer(unsigned char* oldBuffer) = 0;
  37. // implemented by subclasses
  38. void handleNewBuffer(unsigned char* buffer, unsigned bufferSize,
  39. int mpegVersion, MPEG1or2Demux::SCR scr, int16_t PID = -1);
  40. // called by "awaitNewBuffer()"
  41. // Note: For MPEG-4 video, set "mpegVersion" to 4; for H.264 video, set "mpegVersion" to 5;
  42. // for H.265 video, set "mpegVersion" to 6.
  43. // For Opus audio, set "mpegVersion" to 3.
  44. // The buffer is assumed to be a PES packet, with a proper PES header.
  45. // If "PID" is not -1, then it (currently, only the low 8 bits) is used as the stream's PID,
  46. // otherwise the "stream_id" in the PES header is reused to be the stream's PID.
  47. private:
  48. // Redefined virtual functions:
  49. virtual void doGetNextFrame();
  50. private:
  51. void deliverDataToClient(u_int8_t pid, unsigned char* buffer, unsigned bufferSize,
  52. unsigned& startPositionInBuffer);
  53. void deliverPATPacket();
  54. void deliverPMTPacket(Boolean hasChanged);
  55. void setProgramStreamMap(unsigned frameSize);
  56. protected:
  57. Boolean fHaveVideoStreams;
  58. private:
  59. unsigned fOutgoingPacketCounter;
  60. unsigned fProgramMapVersion;
  61. u_int8_t fPreviousInputProgramMapVersion, fCurrentInputProgramMapVersion;
  62. // These two fields are used if we see "program_stream_map"s in the input.
  63. struct {
  64. unsigned counter;
  65. u_int8_t streamType; // for use in Program Maps
  66. } fPIDState[PID_TABLE_SIZE];
  67. u_int8_t fPCR_PID, fCurrentPID;
  68. // Note: We map 8-bit stream_ids directly to PIDs
  69. MPEG1or2Demux::SCR fPCR;
  70. unsigned char* fInputBuffer;
  71. unsigned fInputBufferSize, fInputBufferBytesUsed;
  72. Boolean fIsFirstAdaptationField;
  73. };
  74. // The CRC calculation function that Transport Streams use. We make this function public
  75. // here in case it's useful elsewhere:
  76. u_int32_t calculateCRC(u_int8_t const* data, unsigned dataLength, u_int32_t initialValue = 0xFFFFFFFF);
  77. #endif