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-2012 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 
00078   void getBytes(u_int8_t* to, unsigned numBytes) {
00079     testBytes(to, numBytes);
00080     fCurParserIndex += numBytes;
00081     fRemainingUnparsedBits = 0;
00082   }
00083   void testBytes(u_int8_t* to, unsigned numBytes) { // as above, but doesn't advance ptr
00084     ensureValidBytes(numBytes);
00085     memmove(to, nextToParse(), numBytes);
00086   }
00087   void skipBytes(unsigned numBytes) {
00088     ensureValidBytes(numBytes);
00089     fCurParserIndex += numBytes;
00090   }
00091 
00092   void skipBits(unsigned numBits);
00093   unsigned getBits(unsigned numBits);
00094       // numBits <= 32; returns data into low-order bits of result
00095 
00096   unsigned curOffset() const { return fCurParserIndex; }
00097 
00098   unsigned& totNumValidBytes() { return fTotNumValidBytes; }
00099 
00100   Boolean haveSeenEOF() const { return fHaveSeenEOF; }
00101 
00102   unsigned bankSize() const;
00103 
00104 private:
00105   unsigned char* curBank() { return fCurBank; }
00106   unsigned char* nextToParse() { return &curBank()[fCurParserIndex]; }
00107   unsigned char* lastParsed() { return &curBank()[fCurParserIndex-1]; }
00108 
00109   // makes sure that at least "numBytes" valid bytes remain:
00110   void ensureValidBytes(unsigned numBytesNeeded) {
00111     // common case: inlined:
00112     if (fCurParserIndex + numBytesNeeded <= fTotNumValidBytes) return;
00113 
00114     ensureValidBytes1(numBytesNeeded);
00115   }
00116   void ensureValidBytes1(unsigned numBytesNeeded);
00117 
00118   static void afterGettingBytes(void* clientData, unsigned numBytesRead,
00119                                 unsigned numTruncatedBytes,
00120                                 struct timeval presentationTime,
00121                                 unsigned durationInMicroseconds);
00122   void afterGettingBytes1(unsigned numBytesRead, struct timeval presentationTime);
00123 
00124   static void onInputClosure(void* clientData);
00125   void onInputClosure1();
00126 
00127 private:
00128   FramedSource* fInputSource; // should be a byte-stream source??
00129   FramedSource::onCloseFunc* fClientOnInputCloseFunc;
00130   void* fClientOnInputCloseClientData;
00131   clientContinueFunc* fClientContinueFunc;
00132   void* fClientContinueClientData;
00133 
00134   // Use a pair of 'banks', and swap between them as they fill up:
00135   unsigned char* fBank[2];
00136   unsigned char fCurBankNum;
00137   unsigned char* fCurBank;
00138 
00139   // The most recent 'saved' parse position:
00140   unsigned fSavedParserIndex; // <= fCurParserIndex
00141   unsigned char fSavedRemainingUnparsedBits;
00142 
00143   // The current position of the parser within the current bank:
00144   unsigned fCurParserIndex; // <= fTotNumValidBytes
00145   unsigned char fRemainingUnparsedBits; // in previous byte: [0,7]
00146 
00147   // The total number of valid bytes stored in the current bank:
00148   unsigned fTotNumValidBytes; // <= BANK_SIZE
00149 
00150   // Whether we have seen EOF on the input source:
00151   Boolean fHaveSeenEOF;
00152 
00153   struct timeval fLastSeenPresentationTime; // hack used for EOF handling
00154 };
00155 
00156 #endif

Generated on Thu May 17 07:11:47 2012 for live by  doxygen 1.5.2