OggFile.hh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. This library is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  9. more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with this library; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  13. **********/
  14. // "liveMedia"
  15. // Copyright (c) 1996-2019 Live Networks, Inc. All rights reserved.
  16. // A class that encapsulates an Ogg file
  17. // C++ header
  18. #ifndef _OGG_FILE_HH
  19. #define _OGG_FILE_HH
  20. #ifndef _RTP_SINK_HH
  21. #include "RTPSink.hh"
  22. #endif
  23. #ifndef _HASH_TABLE_HH
  24. #include "HashTable.hh"
  25. #endif
  26. class OggTrack; // forward
  27. class OggDemux; // forward
  28. class OggFile: public Medium {
  29. public:
  30. typedef void (onCreationFunc)(OggFile* newFile, void* clientData);
  31. static void createNew(UsageEnvironment& env, char const* fileName,
  32. onCreationFunc* onCreation, void* onCreationClientData);
  33. // Note: Unlike most "createNew()" functions, this one doesn't return a new object
  34. // immediately. Instead, because this class requires file reading (to parse the
  35. // Ogg track headers) before a new object can be initialized, the creation of a new object
  36. // is signalled by calling - from the event loop - an 'onCreationFunc' that is passed as
  37. // a parameter to "createNew()".
  38. OggTrack* lookup(u_int32_t trackNumber);
  39. OggDemux* newDemux();
  40. // Creates a demultiplexor for extracting tracks from this file.
  41. // (Separate clients will typically have separate demultiplexors.)
  42. char const* fileName() const { return fFileName; }
  43. unsigned numTracks() const;
  44. FramedSource*
  45. createSourceForStreaming(FramedSource* baseSource, u_int32_t trackNumber,
  46. unsigned& estBitrate, unsigned& numFiltersInFrontOfTrack);
  47. // Takes a data source (which must be a demultiplexed track from this file) and returns
  48. // a (possibly modified) data source that can be used for streaming.
  49. RTPSink* createRTPSinkForTrackNumber(u_int32_t trackNumber, Groupsock* rtpGroupsock,
  50. unsigned char rtpPayloadTypeIfDynamic);
  51. // Creates a "RTPSink" object that would be appropriate for streaming the specified track,
  52. // or NULL if no appropriate "RTPSink" exists
  53. class OggTrackTable& trackTable() { return *fTrackTable; }
  54. private:
  55. OggFile(UsageEnvironment& env, char const* fileName, onCreationFunc* onCreation, void* onCreationClientData);
  56. // called only by createNew()
  57. virtual ~OggFile();
  58. static void handleEndOfBosPageParsing(void* clientData);
  59. void handleEndOfBosPageParsing();
  60. void addTrack(OggTrack* newTrack);
  61. void removeDemux(OggDemux* demux);
  62. private:
  63. friend class OggFileParser;
  64. friend class OggDemux;
  65. char const* fFileName;
  66. onCreationFunc* fOnCreation;
  67. void* fOnCreationClientData;
  68. class OggTrackTable* fTrackTable;
  69. HashTable* fDemuxesTable;
  70. class OggFileParser* fParserForInitialization;
  71. };
  72. class OggTrack {
  73. public:
  74. OggTrack();
  75. virtual ~OggTrack();
  76. // track parameters
  77. u_int32_t trackNumber; // bitstream serial number
  78. char const* mimeType; // NULL if not known
  79. unsigned samplingFrequency, numChannels; // for audio tracks
  80. unsigned estBitrate; // estimate, in kbps (for RTCP)
  81. // Special headers for Vorbis audio, Theora video, and Opus audio tracks:
  82. struct _vtoHdrs {
  83. u_int8_t* header[3]; // "identification", "comment", "setup"
  84. unsigned headerSize[3];
  85. // Fields specific to Vorbis audio:
  86. unsigned blocksize[2]; // samples per frame (packet)
  87. unsigned uSecsPerPacket[2]; // computed as (blocksize[i]*1000000)/samplingFrequency
  88. unsigned vorbis_mode_count;
  89. unsigned ilog_vorbis_mode_count_minus_1;
  90. u_int8_t* vorbis_mode_blockflag;
  91. // an array (of size "vorbis_mode_count") of indexes into the (2-entry) "blocksize" array
  92. // Fields specific to Theora video:
  93. u_int8_t KFGSHIFT;
  94. unsigned uSecsPerFrame;
  95. } vtoHdrs;
  96. Boolean weNeedHeaders() const {
  97. return
  98. vtoHdrs.header[0] == NULL ||
  99. vtoHdrs.header[1] == NULL ||
  100. (vtoHdrs.header[2] == NULL && strcmp(mimeType, "audio/OPUS") != 0);
  101. }
  102. };
  103. class OggTrackTableIterator {
  104. public:
  105. OggTrackTableIterator(class OggTrackTable& ourTable);
  106. virtual ~OggTrackTableIterator();
  107. OggTrack* next();
  108. private:
  109. HashTable::Iterator* fIter;
  110. };
  111. class OggDemux: public Medium {
  112. public:
  113. FramedSource* newDemuxedTrack(u_int32_t& resultTrackNumber);
  114. // Returns a new stream ("FramedSource" subclass) that represents the next media track
  115. // from the file. This function returns NULL when no more media tracks exist.
  116. FramedSource* newDemuxedTrackByTrackNumber(unsigned trackNumber);
  117. // As above, but creates a new stream for a specific track number within the Matroska file.
  118. // (You should not call this function more than once with the same track number.)
  119. // Note: We assume that:
  120. // - Every track created by "newDemuxedTrack()" is later read
  121. // - All calls to "newDemuxedTrack()" are made before any track is read
  122. protected:
  123. friend class OggFile;
  124. friend class OggFileParser;
  125. class OggDemuxedTrack* lookupDemuxedTrack(u_int32_t trackNumber);
  126. OggDemux(OggFile& ourFile);
  127. virtual ~OggDemux();
  128. private:
  129. friend class OggDemuxedTrack;
  130. void removeTrack(u_int32_t trackNumber);
  131. void continueReading(); // called by a demuxed track to tell us that it has a pending read ("doGetNextFrame()")
  132. static void handleEndOfFile(void* clientData);
  133. void handleEndOfFile();
  134. private:
  135. OggFile& fOurFile;
  136. class OggFileParser* fOurParser;
  137. HashTable* fDemuxedTracksTable;
  138. OggTrackTableIterator* fIter;
  139. };
  140. #endif