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 | // A parser for a MPEG Transport Stream |
19 | // C++ header |
20 | |
21 | #ifndef _MPEG2_TRANSPORT_STREAM_PARSER_HH |
22 | |
23 | #ifndef _STREAM_PARSER_HH |
24 | #include "StreamParser.hh" |
25 | #endif |
26 | #ifndef _MPEG2_TRANSPORT_STREAM_DEMUXED_TRACK_HH |
27 | #include "MPEG2TransportStreamDemuxedTrack.hh" |
28 | #endif |
29 | #ifndef _MEDIA_SINK_HH |
30 | #include "MediaSink.hh" |
31 | #endif |
32 | |
33 | // A descriptor that describes the state of each known PID: |
34 | enum PIDType { PAT, PMT, STREAM }; |
35 | |
36 | class PIDState { |
37 | protected: // we're a virtual base class |
38 | PIDState(MPEG2TransportStreamParser& parser, u_int16_t pid, PIDType pidType); |
39 | public: |
40 | virtual ~PIDState(); |
41 | |
42 | public: |
43 | MPEG2TransportStreamParser& ourParser; |
44 | u_int16_t PID; |
45 | PIDType type; |
46 | }; |
47 | |
48 | class PIDState_PAT : public PIDState { |
49 | public: |
50 | PIDState_PAT(MPEG2TransportStreamParser& parser, u_int16_t pid); |
51 | protected: |
52 | virtual ~PIDState_PAT(); |
53 | }; |
54 | |
55 | class PIDState_PMT : public PIDState { |
56 | public: |
57 | PIDState_PMT(MPEG2TransportStreamParser& parser, u_int16_t pid, u_int16_t programNumber); |
58 | protected: |
59 | virtual ~PIDState_PMT(); |
60 | |
61 | public: |
62 | u_int16_t program_number; |
63 | }; |
64 | |
65 | class PIDState_STREAM : public PIDState { |
66 | public: |
67 | PIDState_STREAM(MPEG2TransportStreamParser& parser, u_int16_t pid, u_int16_t programNumber, u_int8_t streamType); |
68 | protected: |
69 | virtual ~PIDState_STREAM(); |
70 | |
71 | public: |
72 | u_int16_t program_number; |
73 | u_int8_t stream_type; |
74 | double lastSeenPTS; |
75 | MPEG2TransportStreamDemuxedTrack* streamSource; |
76 | MediaSink* streamSink; |
77 | }; |
78 | |
79 | |
80 | // Descriptions of known "stream_type"s: |
81 | class StreamType { |
82 | public: |
83 | char const* description; |
84 | enum dataType { AUDIO, VIDEO, DATA, TEXT, UNKNOWN } dataType; |
85 | char const* filenameSuffix; |
86 | |
87 | public: |
88 | StreamType(char const* description = "unknown" , enum dataType dataType = UNKNOWN, |
89 | char const* filenameSuffix = "" ); |
90 | }; |
91 | |
92 | |
93 | class MPEG2TransportStreamParser: public StreamParser { |
94 | public: |
95 | MPEG2TransportStreamParser(FramedSource* inputSource, |
96 | FramedSource::onCloseFunc* onEndFunc, void* onEndClientData); |
97 | virtual ~MPEG2TransportStreamParser(); |
98 | |
99 | UsageEnvironment& envir(); |
100 | |
101 | // StreamParser 'client continue' function: |
102 | static void continueParsing(void* clientData, unsigned char* ptr, unsigned size, struct timeval presentationTime); |
103 | void continueParsing(); |
104 | |
105 | private: |
106 | // Parsing functions: |
107 | friend class MPEG2TransportStreamDemuxedTrack; |
108 | Boolean parse(); // returns True iff we have finished parsing all BOS pages (on initialization) |
109 | |
110 | u_int8_t parseAdaptationField(); |
111 | Boolean processDataBytes(u_int16_t PID, Boolean pusi, unsigned numDataBytes); |
112 | |
113 | void parsePAT(Boolean pusi, unsigned numDataBytes); |
114 | void parsePMT(PIDState_PMT* pidState, Boolean pusi, unsigned numDataBytes); |
115 | void parseStreamDescriptors(unsigned numDescriptorBytes); |
116 | Boolean processStreamPacket(PIDState_STREAM* pidState, Boolean pusi, unsigned numDataBytes); |
117 | unsigned (PIDState_STREAM* pidState, unsigned numDataBytes); |
118 | |
119 | private: // redefined virtual functions |
120 | virtual void restoreSavedParserState(); |
121 | |
122 | private: |
123 | // General state for parsing: |
124 | FramedSource* fInputSource; |
125 | Boolean fAmCurrentlyParsing; |
126 | FramedSource::onCloseFunc* fOnEndFunc; |
127 | void* fOnEndClientData; |
128 | PIDState** fPIDState; |
129 | double fLastSeenPCR; |
130 | }; |
131 | |
132 | #endif |
133 | |