live
StreamParser.hh
Go to the documentation of this file.
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-2024 Live Networks, Inc. All rights reserved.
18// Abstract class for parsing a byte stream
19// C++ header
20
21#ifndef _STREAM_PARSER_HH
22#define _STREAM_PARSER_HH
23
24#ifndef _FRAMED_SOURCE_HH
25#include "FramedSource.hh"
26#endif
27
29public:
30 virtual void flushInput();
31
32protected: // we're a virtual base class
33 typedef void (clientContinueFunc)(void* clientData,
34 unsigned char* ptr, unsigned size,
35 struct timeval presentationTime);
37 FramedSource::onCloseFunc* onInputCloseFunc,
38 void* onInputCloseClientData,
40 void* clientContinueClientData);
41 virtual ~StreamParser();
42
45
46 u_int32_t get4Bytes() { // byte-aligned; returned in big-endian order
47 u_int32_t result = test4Bytes();
48 fCurParserIndex += 4;
50
51 return result;
52 }
53 u_int32_t test4Bytes() { // as above, but doesn't advance ptr
55
56 unsigned char const* ptr = nextToParse();
57 return (ptr[0]<<24)|(ptr[1]<<16)|(ptr[2]<<8)|ptr[3];
58 }
59
60 u_int16_t get2Bytes() {
62
63 unsigned char const* ptr = nextToParse();
64 u_int16_t result = (ptr[0]<<8)|ptr[1];
65
66 fCurParserIndex += 2;
68
69 return result;
70 }
71 u_int16_t test2Bytes() {
73
74 unsigned char const* ptr = nextToParse();
75 return (ptr[0]<<8)|ptr[1];
76 }
77
78
79 u_int8_t get1Byte() { // byte-aligned
82 return curBank()[fCurParserIndex++];
83 }
84 u_int8_t test1Byte() { // as above, but doesn't advance ptr
86 return nextToParse()[0];
87 }
88
89 void getBytes(u_int8_t* to, unsigned numBytes) {
90 testBytes(to, numBytes);
91 fCurParserIndex += numBytes;
93 }
94 void testBytes(u_int8_t* to, unsigned numBytes) { // as above, but doesn't advance ptr
95 ensureValidBytes(numBytes);
96 memmove(to, nextToParse(), numBytes);
97 }
98 void skipBytes(unsigned numBytes) {
99 ensureValidBytes(numBytes);
100 fCurParserIndex += numBytes;
101 }
102
103 void skipBits(unsigned numBits);
104 unsigned getBits(unsigned numBits);
105 // numBits <= 32; returns data into low-order bits of result
106
107 unsigned curOffset() const { return fCurParserIndex; }
108
109 unsigned& totNumValidBytes() { return fTotNumValidBytes; }
110
111 Boolean haveSeenEOF() const { return fHaveSeenEOF; }
112
113 unsigned bankSize() const;
114
115private:
116 unsigned char* curBank() { return fCurBank; }
117 unsigned char* nextToParse() { return &curBank()[fCurParserIndex]; }
118 unsigned char* lastParsed() { return &curBank()[fCurParserIndex-1]; }
119
120 // makes sure that at least "numBytes" valid bytes remain:
121 void ensureValidBytes(unsigned numBytesNeeded) {
122 // common case: inlined:
123 if (fCurParserIndex + numBytesNeeded <= fTotNumValidBytes) return;
124
125 ensureValidBytes1(numBytesNeeded);
126 }
127 void ensureValidBytes1(unsigned numBytesNeeded);
128
129 static void afterGettingBytes(void* clientData, unsigned numBytesRead,
130 unsigned numTruncatedBytes,
131 struct timeval presentationTime,
132 unsigned durationInMicroseconds);
133 void afterGettingBytes1(unsigned numBytesRead, struct timeval presentationTime);
134
135 static void onInputClosure(void* clientData);
137
138private:
139 FramedSource* fInputSource; // should be a byte-stream source??
144
145 // Use a pair of 'banks', and swap between them as they fill up:
146 unsigned char* fBank[2];
147 unsigned char fCurBankNum;
148 unsigned char* fCurBank;
149
150 // The most recent 'saved' parse position:
151 unsigned fSavedParserIndex; // <= fCurParserIndex
153
154 // The current position of the parser within the current bank:
155 unsigned fCurParserIndex; // <= fTotNumValidBytes
156 unsigned char fRemainingUnparsedBits; // in previous byte: [0,7]
157
158 // The total number of valid bytes stored in the current bank:
159 unsigned fTotNumValidBytes; // <= BANK_SIZE
160
161 // Whether we have seen EOF on the input source:
163
164 struct timeval fLastSeenPresentationTime; // hack used for EOF handling
165};
166
167#endif
unsigned char Boolean
Definition: Boolean.hh:25
void() onCloseFunc(void *clientData)
Definition: FramedSource.hh:40
FramedSource::onCloseFunc * fClientOnInputCloseFunc
unsigned fTotNumValidBytes
unsigned fSavedParserIndex
unsigned char * fCurBank
unsigned fCurParserIndex
u_int16_t test2Bytes()
Definition: StreamParser.hh:71
void testBytes(u_int8_t *to, unsigned numBytes)
Definition: StreamParser.hh:94
virtual void flushInput()
unsigned char fCurBankNum
void afterGettingBytes1(unsigned numBytesRead, struct timeval presentationTime)
static void onInputClosure(void *clientData)
unsigned getBits(unsigned numBits)
StreamParser(FramedSource *inputSource, FramedSource::onCloseFunc *onInputCloseFunc, void *onInputCloseClientData, clientContinueFunc *clientContinueFunc, void *clientContinueClientData)
void getBytes(u_int8_t *to, unsigned numBytes)
Definition: StreamParser.hh:89
unsigned & totNumValidBytes()
void * fClientOnInputCloseClientData
void * fClientContinueClientData
clientContinueFunc * fClientContinueFunc
u_int8_t test1Byte()
Definition: StreamParser.hh:84
virtual ~StreamParser()
unsigned bankSize() const
void ensureValidBytes(unsigned numBytesNeeded)
unsigned char fSavedRemainingUnparsedBits
u_int8_t get1Byte()
Definition: StreamParser.hh:79
void onInputClosure1()
u_int16_t get2Bytes()
Definition: StreamParser.hh:60
void skipBytes(unsigned numBytes)
Definition: StreamParser.hh:98
unsigned char fRemainingUnparsedBits
void skipBits(unsigned numBits)
u_int32_t test4Bytes()
Definition: StreamParser.hh:53
struct timeval fLastSeenPresentationTime
FramedSource * fInputSource
Boolean fHaveSeenEOF
virtual void restoreSavedParserState()
void() clientContinueFunc(void *clientData, unsigned char *ptr, unsigned size, struct timeval presentationTime)
Definition: StreamParser.hh:33
Boolean haveSeenEOF() const
u_int32_t get4Bytes()
Definition: StreamParser.hh:46
unsigned char * curBank()
void ensureValidBytes1(unsigned numBytesNeeded)
static void afterGettingBytes(void *clientData, unsigned numBytesRead, unsigned numTruncatedBytes, struct timeval presentationTime, unsigned durationInMicroseconds)
unsigned char * lastParsed()
unsigned char * fBank[2]
unsigned curOffset() const
void saveParserState()
unsigned char * nextToParse()