1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
/**********
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**********/
// "liveMedia"
// Copyright (c) 1996-2005 Live Networks, Inc. All rights reserved.
// Abstract class for parsing a byte stream
// Implementation
#include "StreamParser.hh"
#include <string.h>
#include <stdlib.h>
#define BANK_SIZE 150000
StreamParser::StreamParser(FramedSource* inputSource,
FramedSource::onCloseFunc* onInputCloseFunc,
void* onInputCloseClientData,
clientContinueFunc* clientContinueFunc,
void* clientContinueClientData)
: fInputSource(inputSource), fOnInputCloseFunc(onInputCloseFunc),
fOnInputCloseClientData(onInputCloseClientData),
fClientContinueFunc(clientContinueFunc),
fClientContinueClientData(clientContinueClientData),
fSavedParserIndex(0), fSavedRemainingUnparsedBits(0),
fCurParserIndex(0), fRemainingUnparsedBits(0),
fTotNumValidBytes(0) {
fBank[0] = new unsigned char[BANK_SIZE];
fBank[1] = new unsigned char[BANK_SIZE];
fCurBankNum = 0;
fCurBank = fBank[fCurBankNum];
}
StreamParser::~StreamParser() {
delete[] fBank[0]; delete[] fBank[1];
}
#define NO_MORE_BUFFERED_INPUT 1
void StreamParser::ensureValidBytes1(unsigned numBytesNeeded) {
// We need to read some more bytes from the input source.
// First, clarify how much data to ask for:
unsigned maxInputFrameSize = fInputSource->maxFrameSize();
if (maxInputFrameSize > numBytesNeeded) numBytesNeeded = maxInputFrameSize;
// First, check whether these new bytes would overflow the current
// bank. If so, start using a new bank now.
if (fCurParserIndex + numBytesNeeded > BANK_SIZE) {
// Swap banks, but save any still-needed bytes from the old bank:
unsigned numBytesToSave = fTotNumValidBytes - fSavedParserIndex;
unsigned char const* from = &curBank()[fSavedParserIndex];
fCurBankNum = (fCurBankNum + 1)%2;
fCurBank = fBank[fCurBankNum];
memmove(curBank(), from, numBytesToSave);
fCurParserIndex = fCurParserIndex - fSavedParserIndex;
fSavedParserIndex = 0;
fTotNumValidBytes = numBytesToSave;
}
// ASSERT: fCurParserIndex + numBytesNeeded > fTotNumValidBytes
// && fCurParserIndex + numBytesNeeded <= BANK_SIZE
if (fCurParserIndex + numBytesNeeded > BANK_SIZE) {
// If this happens, it means that we have too much saved parser state.
// To fix this, increase BANK_SIZE as appropriate.
fInputSource->envir() << "StreamParser internal error ("
<< fCurParserIndex << "+ "
<< numBytesNeeded << " > "
<< BANK_SIZE << ")\n";
exit(1);
}
// Try to read as many new bytes as will fit in the current bank:
unsigned maxNumBytesToRead = BANK_SIZE - fTotNumValidBytes;
fInputSource->getNextFrame(&curBank()[fTotNumValidBytes],
maxNumBytesToRead,
afterGettingBytes, this,
fOnInputCloseFunc, fOnInputCloseClientData);
throw NO_MORE_BUFFERED_INPUT;
}
void StreamParser::afterGettingBytes(void* clientData,
unsigned numBytesRead,
unsigned /*numTruncatedBytes*/,
struct timeval presentationTime,
unsigned /*durationInMicroseconds*/){
StreamParser* buffer = (StreamParser*)clientData;
// Sanity check: Make sure we didn't get too many bytes for our bank:
if (buffer->fTotNumValidBytes + numBytesRead > BANK_SIZE) {
buffer->fInputSource->envir()
<< "StreamParser::afterGettingBytes() warning: read "
<< numBytesRead << " bytes; expected no more than "
<< BANK_SIZE - buffer->fTotNumValidBytes << "\n";
}
unsigned char* ptr = &buffer->curBank()[buffer->fTotNumValidBytes];
buffer->fTotNumValidBytes += numBytesRead;
// Continue our original calling source where it left off:
buffer->restoreSavedParserState();
// Sigh... this is a crock; things would have been a lot simpler
// here if we were using threads, with synchronous I/O...
buffer->fClientContinueFunc(buffer->fClientContinueClientData,
ptr, numBytesRead, presentationTime);
}
void StreamParser::saveParserState() {
fSavedParserIndex = fCurParserIndex;
fSavedRemainingUnparsedBits = fRemainingUnparsedBits;
}
void StreamParser::restoreSavedParserState() {
fCurParserIndex = fSavedParserIndex;
fRemainingUnparsedBits = fSavedRemainingUnparsedBits;
}
void StreamParser::skipBits(unsigned numBits) {
if (numBits <= fRemainingUnparsedBits) {
fRemainingUnparsedBits -= numBits;
} else {
numBits -= fRemainingUnparsedBits;
unsigned numBytesToExamine = (numBits+7)/8; // round up
ensureValidBytes(numBytesToExamine);
fCurParserIndex += numBytesToExamine;
fRemainingUnparsedBits = 8*numBytesToExamine - numBits;
}
}
unsigned StreamParser::getBits(unsigned numBits) {
if (numBits <= fRemainingUnparsedBits) {
unsigned char lastByte = *lastParsed();
lastByte >>= (fRemainingUnparsedBits - numBits);
fRemainingUnparsedBits -= numBits;
return (unsigned)lastByte &~ ((~0)<<numBits);
} else {
unsigned char lastByte;
if (fRemainingUnparsedBits > 0) {
lastByte = *lastParsed();
} else {
lastByte = 0;
}
unsigned remainingBits = numBits - fRemainingUnparsedBits; // > 0
// For simplicity, read the next 4 bytes, even though we might not
// need all of them here:
unsigned result = test4Bytes();
result >>= (32 - remainingBits);
result |= (lastByte << remainingBits);
if (numBits < 32) result &=~ ((~0)<<numBits);
unsigned const numRemainingBytes = (remainingBits+7)/8;
fCurParserIndex += numRemainingBytes;
fRemainingUnparsedBits = 8*numRemainingBytes - remainingBits;
return result;
}
}
void StreamParser::flushInput() {
fCurParserIndex = fSavedParserIndex = 0;
fSavedRemainingUnparsedBits = fRemainingUnparsedBits = 0;
fTotNumValidBytes = 0;
}
|