MPEG1or2Demux.hh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // Demultiplexer for a MPEG 1 or 2 Program Stream
  17. // C++ header
  18. #ifndef _MPEG_1OR2_DEMUX_HH
  19. #define _MPEG_1OR2_DEMUX_HH
  20. #ifndef _FRAMED_SOURCE_HH
  21. #include "FramedSource.hh"
  22. #endif
  23. class MPEG1or2DemuxedElementaryStream; // forward
  24. class MPEG1or2Demux: public Medium {
  25. public:
  26. static MPEG1or2Demux* createNew(UsageEnvironment& env,
  27. FramedSource* inputSource,
  28. Boolean reclaimWhenLastESDies = False);
  29. // If "reclaimWhenLastESDies" is True, the the demux is deleted when
  30. // all "MPEG1or2DemuxedElementaryStream"s that we created get deleted.
  31. MPEG1or2DemuxedElementaryStream* newElementaryStream(u_int8_t streamIdTag);
  32. // Specialized versions of the above for audio and video:
  33. MPEG1or2DemuxedElementaryStream* newAudioStream();
  34. MPEG1or2DemuxedElementaryStream* newVideoStream();
  35. // A hack for getting raw, undemuxed PES packets from the Program Stream:
  36. MPEG1or2DemuxedElementaryStream* newRawPESStream();
  37. void getNextFrame(u_int8_t streamIdTag,
  38. unsigned char* to, unsigned maxSize,
  39. FramedSource::afterGettingFunc* afterGettingFunc,
  40. void* afterGettingClientData,
  41. FramedSource::onCloseFunc* onCloseFunc,
  42. void* onCloseClientData);
  43. // similar to FramedSource::getNextFrame(), except that it also
  44. // takes a stream id tag as parameter.
  45. void stopGettingFrames(u_int8_t streamIdTag);
  46. // similar to FramedSource::stopGettingFrames(), except that it also
  47. // takes a stream id tag as parameter.
  48. static void handleClosure(void* clientData);
  49. // This should be called (on ourself) if the source is discovered
  50. // to be closed (i.e., no longer readable)
  51. FramedSource* inputSource() const { return fInputSource; }
  52. class SCR {
  53. public:
  54. SCR();
  55. u_int8_t highBit;
  56. u_int32_t remainingBits;
  57. u_int16_t extension;
  58. Boolean isValid;
  59. };
  60. SCR& lastSeenSCR() { return fLastSeenSCR; }
  61. unsigned char mpegVersion() const { return fMPEGversion; }
  62. void flushInput(); // should be called before any 'seek' on the underlying source
  63. private:
  64. MPEG1or2Demux(UsageEnvironment& env,
  65. FramedSource* inputSource, Boolean reclaimWhenLastESDies);
  66. // called only by createNew()
  67. virtual ~MPEG1or2Demux();
  68. void registerReadInterest(u_int8_t streamIdTag,
  69. unsigned char* to, unsigned maxSize,
  70. FramedSource::afterGettingFunc* afterGettingFunc,
  71. void* afterGettingClientData,
  72. FramedSource::onCloseFunc* onCloseFunc,
  73. void* onCloseClientData);
  74. Boolean useSavedData(u_int8_t streamIdTag,
  75. unsigned char* to, unsigned maxSize,
  76. FramedSource::afterGettingFunc* afterGettingFunc,
  77. void* afterGettingClientData);
  78. static void continueReadProcessing(void* clientData,
  79. unsigned char* ptr, unsigned size,
  80. struct timeval presentationTime);
  81. void continueReadProcessing();
  82. private:
  83. friend class MPEG1or2DemuxedElementaryStream;
  84. void noteElementaryStreamDeletion(MPEG1or2DemuxedElementaryStream* es);
  85. private:
  86. FramedSource* fInputSource;
  87. SCR fLastSeenSCR;
  88. unsigned char fMPEGversion;
  89. unsigned char fNextAudioStreamNumber;
  90. unsigned char fNextVideoStreamNumber;
  91. Boolean fReclaimWhenLastESDies;
  92. unsigned fNumOutstandingESs;
  93. // A descriptor for each possible stream id tag:
  94. typedef struct OutputDescriptor {
  95. // input parameters
  96. unsigned char* to; unsigned maxSize;
  97. FramedSource::afterGettingFunc* fAfterGettingFunc;
  98. void* afterGettingClientData;
  99. FramedSource::onCloseFunc* fOnCloseFunc;
  100. void* onCloseClientData;
  101. // output parameters
  102. unsigned frameSize; struct timeval presentationTime;
  103. class SavedData; // forward
  104. SavedData* savedDataHead;
  105. SavedData* savedDataTail;
  106. unsigned savedDataTotalSize;
  107. // status parameters
  108. Boolean isPotentiallyReadable;
  109. Boolean isCurrentlyActive;
  110. Boolean isCurrentlyAwaitingData;
  111. } OutputDescriptor_t;
  112. OutputDescriptor_t fOutput[256];
  113. unsigned fNumPendingReads;
  114. Boolean fHaveUndeliveredData;
  115. private: // parsing state
  116. class MPEGProgramStreamParser* fParser;
  117. friend class MPEGProgramStreamParser; // hack
  118. };
  119. #endif