liveMedia/include/MatroskaFile.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 // A class that encapsulates a Matroska file.
00019 // C++ header
00020 
00021 #ifndef _MATROSKA_FILE_HH
00022 #define _MATROSKA_FILE_HH
00023 
00024 #ifndef _MEDIA_HH
00025 #include "Media.hh"
00026 #endif
00027 #ifndef _HASH_TABLE_HH
00028 #include "HashTable.hh"
00029 #endif
00030 
00031 class MatroskaTrack; // forward
00032 class MatroskaDemux; // forward
00033 
00034 class MatroskaFile: public Medium {
00035 public:
00036   typedef void (onCreationFunc)(MatroskaFile* newFile, void* clientData);
00037   static void createNew(UsageEnvironment& env, char const* fileName, onCreationFunc* onCreation, void* onCreationClientData,
00038                         char const* preferredLanguage = "eng");
00039     // Note: Unlike most "createNew()" functions, this one doesn't return a new object immediately.  Instead, because this class
00040     // requires file reading (to parse the Matroska 'Track' headers) before a new object can be initialized, the creation of a new
00041     // object is signalled by calling - from the event loop - an 'onCreationFunc' that is passed as a parameter to "createNew()".
00042 
00043   // For looking up and iterating over the file's tracks:
00044   class TrackTable {
00045   public:
00046     TrackTable();
00047     virtual ~TrackTable();
00048 
00049     void add(MatroskaTrack* newTrack, unsigned trackNumber);
00050     MatroskaTrack* lookup(unsigned trackNumber);
00051 
00052     unsigned numTracks() const;
00053 
00054     class Iterator {
00055     public:
00056       Iterator(TrackTable& ourTable);
00057       virtual ~Iterator();
00058       MatroskaTrack* next();
00059     private:
00060       HashTable::Iterator* fIter;
00061     };
00062 
00063   private:
00064     friend class Iterator;
00065     HashTable* fTable;
00066   };
00067 
00068   MatroskaTrack* lookup(unsigned trackNumber) { return fTracks.lookup(trackNumber); } // shortcut
00069 
00070   // Create a demultiplexor for extracting tracks from this file.  (Separate clients will typically have separate demultiplexors.)
00071   MatroskaDemux* newDemux();
00072 
00073   // Parameters of the file ('Segment'); set when the file is parsed:
00074   unsigned timecodeScale() { return fTimecodeScale; } // in nanoseconds
00075   float segmentDuration() { return fSegmentDuration; } // in units of "timecodeScale()"
00076   float fileDuration(); // in seconds
00077   TrackTable& tracks() { return fTracks; }
00078   
00079   char const* fileName() const { return fFileName; }
00080 
00081   unsigned chosenVideoTrackNumber() { return fChosenVideoTrackNumber; }
00082   unsigned chosenAudioTrackNumber() { return fChosenAudioTrackNumber; }
00083   unsigned chosenSubtitleTrackNumber() { return fChosenSubtitleTrackNumber; }
00084 
00085 private:
00086   MatroskaFile(UsageEnvironment& env, char const* fileName, onCreationFunc* onCreation, void* onCreationClientData,
00087                char const* preferredLanguage);
00088       // called only by createNew()
00089   virtual ~MatroskaFile();
00090 
00091   static void handleEndOfTrackHeaderParsing(void* clientData);
00092   void handleEndOfTrackHeaderParsing();
00093 
00094   void addCuePoint(double cueTime, u_int64_t clusterOffsetInFile, unsigned blockNumWithinCluster);
00095   Boolean lookupCuePoint(double& cueTime, u_int64_t& resultClusterOffsetInFile, unsigned& resultBlockNumWithinCluster);
00096   void printCuePoints(FILE* fid);
00097 
00098   void removeDemux(MatroskaDemux* demux);
00099 
00100 private:
00101   friend class MatroskaFileParser;
00102   friend class MatroskaDemux;
00103   char const* fFileName;
00104   onCreationFunc* fOnCreation;
00105   void* fOnCreationClientData;
00106   char const* fPreferredLanguage;
00107 
00108   unsigned fTimecodeScale; // in nanoseconds
00109   float fSegmentDuration; // in units of "fTimecodeScale"
00110   u_int64_t fSegmentDataOffset, fClusterOffset, fCuesOffset;
00111 
00112   TrackTable fTracks;
00113   HashTable* fDemuxesTable;
00114   class CuePoint* fCuePoints;
00115   unsigned fChosenVideoTrackNumber, fChosenAudioTrackNumber, fChosenSubtitleTrackNumber;
00116   class MatroskaFileParser* fParserForInitialization;
00117 };
00118 
00119 // We define our own track type codes as bits (powers of 2), so we can use the set of track types as a bitmap, representing a set:
00120 // (Note that MATROSKA_TRACK_TYPE_OTHER must be last, and have the largest value.)
00121 #define MATROSKA_TRACK_TYPE_VIDEO 0x01
00122 #define MATROSKA_TRACK_TYPE_AUDIO 0x02
00123 #define MATROSKA_TRACK_TYPE_SUBTITLE 0x04
00124 #define MATROSKA_TRACK_TYPE_OTHER 0x08
00125 
00126 class MatroskaTrack {
00127 public:
00128   MatroskaTrack();
00129   virtual ~MatroskaTrack();
00130 
00131   // track parameters
00132   unsigned trackNumber;
00133   u_int8_t trackType;
00134   Boolean isEnabled, isDefault, isForced;
00135   unsigned defaultDuration;
00136   char* name;
00137   char* language;
00138   char* codecID;
00139   unsigned samplingFrequency;
00140   unsigned numChannels;
00141   char const* mimeType;
00142   unsigned codecPrivateSize;
00143   u_int8_t* codecPrivate;
00144   unsigned headerStrippedBytesSize;
00145   u_int8_t* headerStrippedBytes;
00146   unsigned subframeSizeSize; // 0 means: frames do not have subframes (the default behavior)
00147   Boolean haveSubframes() const { return subframeSizeSize > 0; }
00148 };
00149 
00150 class MatroskaDemux: public Medium {
00151 public:
00152   FramedSource* newDemuxedTrack(unsigned trackNumber);
00153     // Note: We assume that:
00154     // - Every track created by "newDemuxedTrack()" is later read
00155     // - All calls to "newDemuxedTrack()" are made before any track is read
00156 
00157   class MatroskaDemuxedTrack* lookupDemuxedTrack(unsigned trackNumber);
00158 
00159 protected:
00160   friend class MatroskaFile;
00161   MatroskaDemux(MatroskaFile& ourFile); // we're created only by a "MatroskaFile" (a friend)
00162   virtual ~MatroskaDemux();
00163 
00164 private:
00165   friend class MatroskaDemuxedTrack;
00166   void removeTrack(unsigned trackNumber);
00167   void continueReading(); // called by a demuxed track to tell us that it has a pending read ("doGetNextFrame()")
00168   void seekToTime(double& seekNPT);
00169 
00170   static void handleEndOfFile(void* clientData);
00171   void handleEndOfFile();
00172 
00173 private:
00174   MatroskaFile& fOurFile;
00175   class MatroskaFileParser* fOurParser;
00176   HashTable* fDemuxedTracksTable;
00177 };
00178 
00179 #endif

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