| 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 | |
| 7 | This library is distributed in the hope that it will be useful, but WITHOUT |
| 8 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 9 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 10 | more details. |
| 11 | |
| 12 | You should have received a copy of the GNU Lesser General Public License |
| 13 | along with this library; if not, write to the Free Software Foundation, Inc., |
| 14 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 15 | **********/ |
| 16 | // "liveMedia" |
| 17 | // Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved. |
| 18 | // Demultiplexer for a MPEG 1 or 2 Program Stream |
| 19 | // C++ header |
| 20 | |
| 21 | #ifndef _MPEG_1OR2_DEMUX_HH |
| 22 | #define _MPEG_1OR2_DEMUX_HH |
| 23 | |
| 24 | #ifndef _FRAMED_SOURCE_HH |
| 25 | #include "FramedSource.hh" |
| 26 | #endif |
| 27 | |
| 28 | class MPEG1or2DemuxedElementaryStream; // forward |
| 29 | |
| 30 | class MPEG1or2Demux: public Medium { |
| 31 | public: |
| 32 | static MPEG1or2Demux* createNew(UsageEnvironment& env, |
| 33 | FramedSource* inputSource, |
| 34 | Boolean reclaimWhenLastESDies = False); |
| 35 | // If "reclaimWhenLastESDies" is True, the the demux is deleted when |
| 36 | // all "MPEG1or2DemuxedElementaryStream"s that we created get deleted. |
| 37 | |
| 38 | MPEG1or2DemuxedElementaryStream* newElementaryStream(u_int8_t streamIdTag); |
| 39 | |
| 40 | // Specialized versions of the above for audio and video: |
| 41 | MPEG1or2DemuxedElementaryStream* newAudioStream(); |
| 42 | MPEG1or2DemuxedElementaryStream* newVideoStream(); |
| 43 | |
| 44 | // A hack for getting raw, undemuxed PES packets from the Program Stream: |
| 45 | MPEG1or2DemuxedElementaryStream* newRawPESStream(); |
| 46 | |
| 47 | void getNextFrame(u_int8_t streamIdTag, |
| 48 | unsigned char* to, unsigned maxSize, |
| 49 | FramedSource::afterGettingFunc* afterGettingFunc, |
| 50 | void* afterGettingClientData, |
| 51 | FramedSource::onCloseFunc* onCloseFunc, |
| 52 | void* onCloseClientData); |
| 53 | // similar to FramedSource::getNextFrame(), except that it also |
| 54 | // takes a stream id tag as parameter. |
| 55 | |
| 56 | void stopGettingFrames(u_int8_t streamIdTag); |
| 57 | // similar to FramedSource::stopGettingFrames(), except that it also |
| 58 | // takes a stream id tag as parameter. |
| 59 | |
| 60 | static void handleClosure(void* clientData); |
| 61 | // This should be called (on ourself) if the source is discovered |
| 62 | // to be closed (i.e., no longer readable) |
| 63 | |
| 64 | FramedSource* inputSource() const { return fInputSource; } |
| 65 | |
| 66 | class SCR { |
| 67 | public: |
| 68 | SCR(); |
| 69 | |
| 70 | u_int8_t highBit; |
| 71 | u_int32_t remainingBits; |
| 72 | u_int16_t extension; |
| 73 | |
| 74 | Boolean isValid; |
| 75 | }; |
| 76 | SCR& lastSeenSCR() { return fLastSeenSCR; } |
| 77 | |
| 78 | unsigned char mpegVersion() const { return fMPEGversion; } |
| 79 | |
| 80 | void flushInput(); // should be called before any 'seek' on the underlying source |
| 81 | |
| 82 | private: |
| 83 | MPEG1or2Demux(UsageEnvironment& env, |
| 84 | FramedSource* inputSource, Boolean reclaimWhenLastESDies); |
| 85 | // called only by createNew() |
| 86 | virtual ~MPEG1or2Demux(); |
| 87 | |
| 88 | void registerReadInterest(u_int8_t streamIdTag, |
| 89 | unsigned char* to, unsigned maxSize, |
| 90 | FramedSource::afterGettingFunc* afterGettingFunc, |
| 91 | void* afterGettingClientData, |
| 92 | FramedSource::onCloseFunc* onCloseFunc, |
| 93 | void* onCloseClientData); |
| 94 | |
| 95 | Boolean useSavedData(u_int8_t streamIdTag, |
| 96 | unsigned char* to, unsigned maxSize, |
| 97 | FramedSource::afterGettingFunc* afterGettingFunc, |
| 98 | void* afterGettingClientData); |
| 99 | |
| 100 | static void continueReadProcessing(void* clientData, |
| 101 | unsigned char* ptr, unsigned size, |
| 102 | struct timeval presentationTime); |
| 103 | void continueReadProcessing(); |
| 104 | |
| 105 | private: |
| 106 | friend class MPEG1or2DemuxedElementaryStream; |
| 107 | void noteElementaryStreamDeletion(MPEG1or2DemuxedElementaryStream* es); |
| 108 | |
| 109 | private: |
| 110 | FramedSource* fInputSource; |
| 111 | SCR fLastSeenSCR; |
| 112 | unsigned char fMPEGversion; |
| 113 | |
| 114 | unsigned char fNextAudioStreamNumber; |
| 115 | unsigned char fNextVideoStreamNumber; |
| 116 | Boolean fReclaimWhenLastESDies; |
| 117 | unsigned fNumOutstandingESs; |
| 118 | |
| 119 | // A descriptor for each possible stream id tag: |
| 120 | typedef struct OutputDescriptor { |
| 121 | // input parameters |
| 122 | unsigned char* to; unsigned maxSize; |
| 123 | FramedSource::afterGettingFunc* fAfterGettingFunc; |
| 124 | void* afterGettingClientData; |
| 125 | FramedSource::onCloseFunc* fOnCloseFunc; |
| 126 | void* onCloseClientData; |
| 127 | |
| 128 | // output parameters |
| 129 | unsigned frameSize; struct timeval presentationTime; |
| 130 | class SavedData; // forward |
| 131 | SavedData* savedDataHead; |
| 132 | SavedData* savedDataTail; |
| 133 | unsigned savedDataTotalSize; |
| 134 | |
| 135 | // status parameters |
| 136 | Boolean isPotentiallyReadable; |
| 137 | Boolean isCurrentlyActive; |
| 138 | Boolean isCurrentlyAwaitingData; |
| 139 | } OutputDescriptor_t; |
| 140 | OutputDescriptor_t fOutput[256]; |
| 141 | |
| 142 | unsigned fNumPendingReads; |
| 143 | Boolean fHaveUndeliveredData; |
| 144 | |
| 145 | private: // parsing state |
| 146 | class MPEGProgramStreamParser* fParser; |
| 147 | friend class MPEGProgramStreamParser; // hack |
| 148 | }; |
| 149 | |
| 150 | #endif |
| 151 | |