1/**********
2This library is free software; you can redistribute it and/or modify it under
3the terms of the GNU Lesser General Public License as published by the
4Free Software Foundation; either version 3 of the License, or (at your
5option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
6
7This library is distributed in the hope that it will be useful, but WITHOUT
8ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10more details.
11
12You should have received a copy of the GNU Lesser General Public License
13along with this library; if not, write to the Free Software Foundation, Inc.,
1451 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:
34enum PIDType { PAT, PMT, STREAM };
35
36class PIDState {
37protected: // we're a virtual base class
38 PIDState(MPEG2TransportStreamParser& parser, u_int16_t pid, PIDType pidType);
39public:
40 virtual ~PIDState();
41
42public:
43 MPEG2TransportStreamParser& ourParser;
44 u_int16_t PID;
45 PIDType type;
46};
47
48class PIDState_PAT : public PIDState {
49public:
50 PIDState_PAT(MPEG2TransportStreamParser& parser, u_int16_t pid);
51protected:
52 virtual ~PIDState_PAT();
53};
54
55class PIDState_PMT : public PIDState {
56public:
57 PIDState_PMT(MPEG2TransportStreamParser& parser, u_int16_t pid, u_int16_t programNumber);
58protected:
59 virtual ~PIDState_PMT();
60
61public:
62 u_int16_t program_number;
63};
64
65class PIDState_STREAM : public PIDState {
66public:
67 PIDState_STREAM(MPEG2TransportStreamParser& parser, u_int16_t pid, u_int16_t programNumber, u_int8_t streamType);
68protected:
69 virtual ~PIDState_STREAM();
70
71public:
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:
81class StreamType {
82public:
83 char const* description;
84 enum dataType { AUDIO, VIDEO, DATA, TEXT, UNKNOWN } dataType;
85 char const* filenameSuffix;
86
87public:
88 StreamType(char const* description = "unknown", enum dataType dataType = UNKNOWN,
89 char const* filenameSuffix = "");
90};
91
92
93class MPEG2TransportStreamParser: public StreamParser {
94public:
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
105private:
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 parsePESHeader(PIDState_STREAM* pidState, unsigned numDataBytes);
118
119private: // redefined virtual functions
120 virtual void restoreSavedParserState();
121
122private:
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