liveMedia/StreamParser.hh

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // "liveMedia"
00017 // Copyright (c) 1996-2013 Live Networks, Inc.  All rights reserved.
00018 // Abstract class for parsing a byte stream
00019 // C++ header
00020 
00021 #ifndef _STREAM_PARSER_HH
00022 #define _STREAM_PARSER_HH
00023 
00024 #ifndef _FRAMED_SOURCE_HH
00025 #include "FramedSource.hh"
00026 #endif
00027 
00028 class StreamParser {
00029 public:
00030   virtual void flushInput();
00031 
00032 protected: // we're a virtual base class
00033   typedef void (clientContinueFunc)(void* clientData,
00034                                     unsigned char* ptr, unsigned size,
00035                                     struct timeval presentationTime);
00036   StreamParser(FramedSource* inputSource,
00037                FramedSource::onCloseFunc* onInputCloseFunc,
00038                void* onInputCloseClientData,
00039                clientContinueFunc* clientContinueFunc,
00040                void* clientContinueClientData);
00041   virtual ~StreamParser();
00042 
00043   void saveParserState();
00044   virtual void restoreSavedParserState();
00045 
00046   u_int32_t get4Bytes() { // byte-aligned; returned in big-endian order
00047     u_int32_t result = test4Bytes();
00048     fCurParserIndex += 4;
00049     fRemainingUnparsedBits = 0;
00050 
00051     return result;
00052   }
00053   u_int32_t test4Bytes() { // as above, but doesn't advance ptr
00054     ensureValidBytes(4);
00055 
00056     unsigned char const* ptr = nextToParse();
00057     return (ptr[0]<<24)|(ptr[1]<<16)|(ptr[2]<<8)|ptr[3];
00058   }
00059 
00060   u_int16_t get2Bytes() {
00061     ensureValidBytes(2);
00062 
00063     unsigned char const* ptr = nextToParse();
00064     u_int16_t result = (ptr[0]<<8)|ptr[1];
00065 
00066     fCurParserIndex += 2;
00067     fRemainingUnparsedBits = 0;
00068 
00069     return result;
00070   }
00071 
00072   u_int8_t get1Byte() { // byte-aligned
00073     ensureValidBytes(1);
00074     fRemainingUnparsedBits = 0;
00075     return curBank()[fCurParserIndex++];
00076   }
00077   u_int8_t test1Byte(unsigned numBytes) { // as above, but doesn't advance ptr
00078     ensureValidBytes(1);
00079     return nextToParse()[0];
00080   }
00081 
00082   void getBytes(u_int8_t* to, unsigned numBytes) {
00083     testBytes(to, numBytes);
00084     fCurParserIndex += numBytes;
00085     fRemainingUnparsedBits = 0;
00086   }
00087   void testBytes(u_int8_t* to, unsigned numBytes) { // as above, but doesn't advance ptr
00088     ensureValidBytes(numBytes);
00089     memmove(to, nextToParse(), numBytes);
00090   }
00091   void skipBytes(unsigned numBytes) {
00092     ensureValidBytes(numBytes);
00093     fCurParserIndex += numBytes;
00094   }
00095 
00096   void skipBits(unsigned numBits);
00097   unsigned getBits(unsigned numBits);
00098       // numBits <= 32; returns data into low-order bits of result
00099 
00100   unsigned curOffset() const { return fCurParserIndex; }
00101 
00102   unsigned& totNumValidBytes() { return fTotNumValidBytes; }
00103 
00104   Boolean haveSeenEOF() const { return fHaveSeenEOF; }
00105 
00106   unsigned bankSize() const;
00107 
00108 private:
00109   unsigned char* curBank() { return fCurBank; }
00110   unsigned char* nextToParse() { return &curBank()[fCurParserIndex]; }
00111   unsigned char* lastParsed() { return &curBank()[fCurParserIndex-1]; }
00112 
00113   // makes sure that at least "numBytes" valid bytes remain:
00114   void ensureValidBytes(unsigned numBytesNeeded) {
00115     // common case: inlined:
00116     if (fCurParserIndex + numBytesNeeded <= fTotNumValidBytes) return;
00117 
00118     ensureValidBytes1(numBytesNeeded);
00119   }
00120   void ensureValidBytes1(unsigned numBytesNeeded);
00121 
00122   static void afterGettingBytes(void* clientData, unsigned numBytesRead,
00123                                 unsigned numTruncatedBytes,
00124                                 struct timeval presentationTime,
00125                                 unsigned durationInMicroseconds);
00126   void afterGettingBytes1(unsigned numBytesRead, struct timeval presentationTime);
00127 
00128   static void onInputClosure(void* clientData);
00129   void onInputClosure1();
00130 
00131 private:
00132   FramedSource* fInputSource; // should be a byte-stream source??
00133   FramedSource::onCloseFunc* fClientOnInputCloseFunc;
00134   void* fClientOnInputCloseClientData;
00135   clientContinueFunc* fClientContinueFunc;
00136   void* fClientContinueClientData;
00137 
00138   // Use a pair of 'banks', and swap between them as they fill up:
00139   unsigned char* fBank[2];
00140   unsigned char fCurBankNum;
00141   unsigned char* fCurBank;
00142 
00143   // The most recent 'saved' parse position:
00144   unsigned fSavedParserIndex; // <= fCurParserIndex
00145   unsigned char fSavedRemainingUnparsedBits;
00146 
00147   // The current position of the parser within the current bank:
00148   unsigned fCurParserIndex; // <= fTotNumValidBytes
00149   unsigned char fRemainingUnparsedBits; // in previous byte: [0,7]
00150 
00151   // The total number of valid bytes stored in the current bank:
00152   unsigned fTotNumValidBytes; // <= BANK_SIZE
00153 
00154   // Whether we have seen EOF on the input source:
00155   Boolean fHaveSeenEOF;
00156 
00157   struct timeval fLastSeenPresentationTime; // hack used for EOF handling
00158 };
00159 
00160 #endif

Generated on Mon Apr 29 13:28:03 2013 for live by  doxygen 1.5.2