QuickTimeFileSink.hh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 sink that generates a QuickTime file from a composite media session
  17. // C++ header
  18. #ifndef _QUICKTIME_FILE_SINK_HH
  19. #define _QUICKTIME_FILE_SINK_HH
  20. #ifndef _MEDIA_SESSION_HH
  21. #include "MediaSession.hh"
  22. #endif
  23. class QuickTimeFileSink: public Medium {
  24. public:
  25. static QuickTimeFileSink* createNew(UsageEnvironment& env,
  26. MediaSession& inputSession,
  27. char const* outputFileName,
  28. unsigned bufferSize = 20000,
  29. unsigned short movieWidth = 240,
  30. unsigned short movieHeight = 180,
  31. unsigned movieFPS = 15,
  32. Boolean packetLossCompensate = False,
  33. Boolean syncStreams = False,
  34. Boolean generateHintTracks = False,
  35. Boolean generateMP4Format = False);
  36. typedef void (afterPlayingFunc)(void* clientData);
  37. Boolean startPlaying(afterPlayingFunc* afterFunc,
  38. void* afterClientData);
  39. unsigned numActiveSubsessions() const { return fNumSubsessions; }
  40. protected:
  41. QuickTimeFileSink(UsageEnvironment& env, MediaSession& inputSession,
  42. char const* outputFileName, unsigned bufferSize,
  43. unsigned short movieWidth, unsigned short movieHeight,
  44. unsigned movieFPS, Boolean packetLossCompensate,
  45. Boolean syncStreams, Boolean generateHintTracks,
  46. Boolean generateMP4Format);
  47. // called only by createNew()
  48. virtual ~QuickTimeFileSink();
  49. virtual void noteRecordedFrame(MediaSubsession& inputSubsession,
  50. unsigned packetDataSize, struct timeval const& presentationTime);
  51. private:
  52. Boolean continuePlaying();
  53. static void afterGettingFrame(void* clientData, unsigned frameSize,
  54. unsigned numTruncatedBytes,
  55. struct timeval presentationTime,
  56. unsigned durationInMicroseconds);
  57. static void onSourceClosure(void* clientData);
  58. void onSourceClosure1();
  59. static void onRTCPBye(void* clientData);
  60. void completeOutputFile();
  61. private:
  62. friend class SubsessionIOState;
  63. MediaSession& fInputSession;
  64. FILE* fOutFid;
  65. unsigned fBufferSize;
  66. Boolean fPacketLossCompensate;
  67. Boolean fSyncStreams, fGenerateMP4Format;
  68. struct timeval fNewestSyncTime, fFirstDataTime;
  69. Boolean fAreCurrentlyBeingPlayed;
  70. afterPlayingFunc* fAfterFunc;
  71. void* fAfterClientData;
  72. unsigned fAppleCreationTime;
  73. unsigned fLargestRTPtimestampFrequency;
  74. unsigned fNumSubsessions, fNumSyncedSubsessions;
  75. struct timeval fStartTime;
  76. Boolean fHaveCompletedOutputFile;
  77. private:
  78. ///// Definitions specific to the QuickTime file format:
  79. unsigned addWord64(u_int64_t word);
  80. unsigned addWord(unsigned word);
  81. unsigned addHalfWord(unsigned short halfWord);
  82. unsigned addByte(unsigned char byte) {
  83. putc(byte, fOutFid);
  84. return 1;
  85. }
  86. unsigned addZeroWords(unsigned numWords);
  87. unsigned add4ByteString(char const* str);
  88. unsigned addArbitraryString(char const* str,
  89. Boolean oneByteLength = True);
  90. unsigned addAtomHeader(char const* atomName);
  91. unsigned addAtomHeader64(char const* atomName);
  92. // strlen(atomName) must be 4
  93. void setWord(int64_t filePosn, unsigned size);
  94. void setWord64(int64_t filePosn, u_int64_t size);
  95. unsigned movieTimeScale() const {return fLargestRTPtimestampFrequency;}
  96. // Define member functions for outputting various types of atom:
  97. #define _atom(name) unsigned addAtom_##name()
  98. _atom(ftyp); // for MP4 format files
  99. _atom(moov);
  100. _atom(mvhd);
  101. _atom(iods); // for MP4 format files
  102. _atom(trak);
  103. _atom(tkhd);
  104. _atom(edts);
  105. _atom(elst);
  106. _atom(tref);
  107. _atom(hint);
  108. _atom(mdia);
  109. _atom(mdhd);
  110. _atom(hdlr);
  111. _atom(minf);
  112. _atom(smhd);
  113. _atom(vmhd);
  114. _atom(gmhd);
  115. _atom(gmin);
  116. unsigned addAtom_hdlr2();
  117. _atom(dinf);
  118. _atom(dref);
  119. _atom(alis);
  120. _atom(stbl);
  121. _atom(stsd);
  122. unsigned addAtom_genericMedia();
  123. unsigned addAtom_soundMediaGeneral();
  124. _atom(ulaw);
  125. _atom(alaw);
  126. _atom(Qclp);
  127. _atom(wave);
  128. _atom(frma);
  129. _atom(Fclp);
  130. _atom(Hclp);
  131. _atom(mp4a);
  132. // _atom(wave);
  133. // _atom(frma);
  134. _atom(esds);
  135. _atom(srcq);
  136. _atom(h263);
  137. _atom(avc1);
  138. _atom(avcC);
  139. _atom(mp4v);
  140. _atom(rtp);
  141. _atom(tims);
  142. _atom(stts);
  143. _atom(stss);
  144. _atom(stsc);
  145. _atom(stsz);
  146. _atom(co64);
  147. _atom(udta);
  148. _atom(name);
  149. _atom(hnti);
  150. _atom(sdp);
  151. _atom(hinf);
  152. _atom(totl);
  153. _atom(npck);
  154. _atom(tpay);
  155. _atom(trpy);
  156. _atom(nump);
  157. _atom(tpyl);
  158. _atom(dmed);
  159. _atom(dimm);
  160. _atom(drep);
  161. _atom(tmin);
  162. _atom(tmax);
  163. _atom(pmax);
  164. _atom(dmax);
  165. _atom(payt);
  166. unsigned addAtom_dummy();
  167. private:
  168. unsigned short fMovieWidth, fMovieHeight;
  169. unsigned fMovieFPS;
  170. int64_t fMDATposition;
  171. int64_t fMVHD_durationPosn;
  172. unsigned fMaxTrackDurationM; // in movie time units
  173. class SubsessionIOState* fCurrentIOState;
  174. };
  175. #endif