| 1 | /********** |
| 2 | This library is free software; you can redistribute it and/or modify it under |
| 3 | the terms of the GNU Lesser General Public License as published by the |
| 4 | Free Software Foundation; either version 3 of the License, or (at your |
| 5 | option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.) |
| 6 | |
| 7 | This library is distributed in the hope that it will be useful, but WITHOUT |
| 8 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 9 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 10 | more details. |
| 11 | |
| 12 | You should have received a copy of the GNU Lesser General Public License |
| 13 | along with this library; if not, write to the Free Software Foundation, Inc., |
| 14 | 51 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 an Ogg file |
| 19 | // C++ header |
| 20 | |
| 21 | #ifndef _OGG_FILE_PARSER_HH |
| 22 | |
| 23 | #ifndef _STREAM_PARSER_HH |
| 24 | #include "StreamParser.hh" |
| 25 | #endif |
| 26 | #ifndef _OGG_FILE_HH |
| 27 | #include "OggFile.hh" |
| 28 | #endif |
| 29 | |
| 30 | // An enum representing the current state of the parser: |
| 31 | enum OggParseState { |
| 32 | PARSING_START_OF_FILE, |
| 33 | PARSING_AND_DELIVERING_PAGES, |
| 34 | DELIVERING_PACKET_WITHIN_PAGE |
| 35 | }; |
| 36 | |
| 37 | // A structure that counts the sizes of 'packets' given by each page's "segment_table": |
| 38 | class PacketSizeTable { |
| 39 | public: |
| 40 | PacketSizeTable(unsigned number_page_segments); |
| 41 | ~PacketSizeTable(); |
| 42 | |
| 43 | unsigned numCompletedPackets; // will be <= "number_page_segments" |
| 44 | unsigned* size; // an array of sizes of each of the packets |
| 45 | unsigned totSizes; |
| 46 | unsigned nextPacketNumToDeliver; |
| 47 | Boolean lastPacketIsIncomplete; // iff the last segment's 'lacing' was 255 |
| 48 | }; |
| 49 | |
| 50 | class OggFileParser: public StreamParser { |
| 51 | public: |
| 52 | OggFileParser(OggFile& ourFile, FramedSource* inputSource, |
| 53 | FramedSource::onCloseFunc* onEndFunc, void* onEndClientData, |
| 54 | OggDemux* ourDemux = NULL); |
| 55 | virtual ~OggFileParser(); |
| 56 | |
| 57 | // StreamParser 'client continue' function: |
| 58 | static void continueParsing(void* clientData, unsigned char* ptr, unsigned size, struct timeval presentationTime); |
| 59 | void continueParsing(); |
| 60 | |
| 61 | private: |
| 62 | Boolean () { return fNumUnfulfilledTracks > 0; } |
| 63 | |
| 64 | // Parsing functions: |
| 65 | Boolean parse(); // returns True iff we have finished parsing all BOS pages (on initialization) |
| 66 | |
| 67 | Boolean parseStartOfFile(); |
| 68 | u_int8_t parseInitialPage(); // returns the 'header_type_flag' byte |
| 69 | void parseAndDeliverPages(); |
| 70 | Boolean parseAndDeliverPage(); |
| 71 | Boolean deliverPacketWithinPage(); |
| 72 | void parseStartOfPage(u_int8_t& , u_int32_t& bitstream_serial_number); |
| 73 | |
| 74 | Boolean (OggTrack* track, u_int8_t const* p, unsigned ); |
| 75 | |
| 76 | private: |
| 77 | // General state for parsing: |
| 78 | OggFile& fOurFile; |
| 79 | FramedSource* fInputSource; |
| 80 | FramedSource::onCloseFunc* fOnEndFunc; |
| 81 | void* fOnEndClientData; |
| 82 | OggDemux* fOurDemux; |
| 83 | OggParseState fCurrentParseState; |
| 84 | |
| 85 | unsigned fNumUnfulfilledTracks; |
| 86 | PacketSizeTable* fPacketSizeTable; |
| 87 | u_int32_t fCurrentTrackNumber; |
| 88 | u_int8_t* fSavedPacket; // used to temporarily save a copy of a 'packet' from a page |
| 89 | }; |
| 90 | |
| 91 | #endif |
| 92 | |