Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
8.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. //----------------------------------------------------------------------------------------
  4. #ifndef QUICKTIME_RECORDER_H
  5. #define QUICKTIME_RECORDER_H
  6. #ifdef _WIN32
  7. #pragma once
  8. #endif
  9. //--------------------------------------------------------------------------------
  10. #if defined( OSX )
  11. #include <quicktime/QTML.h>
  12. #include <quicktime/Movies.h>
  13. #include <quicktime/QuickTimeComponents.h>
  14. #elif defined( WIN32 )
  15. #include "QTML.h"
  16. #include "Movies.h"
  17. #include "QuickTimeComponents.h"
  18. #else
  19. #error "Quicktime encoding is not supported on this platform"
  20. #endif
  21. #include "video/ivideoservices.h"
  22. #include "video_macros.h"
  23. #include "quicktime_common.h"
  24. // comment out to prevent logging of creation data
  25. //#define LOG_ENCODER_OPERATIONS
  26. //#define LOG_ENCODER_AUDIO_OPERATIONS
  27. // comment out to log images of frames
  28. //#define LOG_FRAMES_TO_TGA
  29. //#define LOG_FRAMES_TO_TGA_INTERVAL 16
  30. #if defined( LOG_ENCODER_OPERATIONS ) || defined( LOG_ENCODER_AUDIO_OPERATIONS ) || defined ( LOG_FRAMES_TO_TGA ) || defined ( ENABLE_EXTERNAL_ENCODER_LOGGING )
  31. #include <filesystem.h>
  32. #endif
  33. // ------------------------------------------------------------------------
  34. // CQTVideoFileComposer
  35. // Class to manages to creation of a video file from a series of
  36. // supplied bitmap images.
  37. //
  38. // At this time, the time interval between frames must be the same for all
  39. // frames of the movie
  40. // ------------------------------------------------------------------------
  41. class CQTVideoFileComposer
  42. {
  43. public:
  44. static const CodecType DEFAULT_CODEC = kH264CodecType;
  45. static const CodecQ DEFAULT_ENCODE_QUALITY = codecNormalQuality;
  46. static const Fixed DEFAULT_GAMMA = kQTUseSourceGammaLevel;
  47. static const int MIN_AUDIO_SAMPLE_GROUP_SIZE = 64;
  48. static const int MAX_AUDIO_GROUP_SIZE_IN_SEC = 4;
  49. CQTVideoFileComposer();
  50. ~CQTVideoFileComposer();
  51. bool CreateNewMovie( const char *fileName, bool hasAudio );
  52. bool SetMovieVideoParameters( int width, int height, VideoFrameRate_t movieFPS, VideoEncodeCodec_t desiredCodec, int encodeQuality, VideoEncodeGamma_t gamma );
  53. bool SetMovieSourceImageParameters( int srcWidth, int srcHeight, VideoEncodeSourceFormat_t srcImageFormat );
  54. bool SetMovieSourceAudioParameters( AudioEncodeSourceFormat_t srcAudioFormat = AudioEncodeSourceFormat::AUDIO_NONE, int audioSampleRate = 0, AudioEncodeOptions_t audioOptions = AudioEncodeOptions::NO_AUDIO_OPTIONS, int audioSampleGroupSize = 0 );
  55. bool AppendVideoFrameToMedia( void *ImageBuffer, int strideAdjustBytes );
  56. bool AppendAudioSamplesToMedia( void *soundBuffer, size_t bufferSize );
  57. bool AbortMovie();
  58. bool FinishMovie( bool SaveMovieToDisk = true );
  59. size_t GetFrameBufferSize() { return m_SrcImageSize; }
  60. bool CheckFilename( const char *filename );
  61. void SetResult( VideoResult_t status );
  62. VideoResult_t GetResult();
  63. bool IsReadyToRecord();
  64. int GetFrameCount() { return m_nFramesAdded; }
  65. int GetSampleCount() { return m_nSamplesAdded; }
  66. VideoFrameRate_t GetFPS() { return m_MovieRecordFPS; }
  67. int GetSampleRate() { return m_AudioSourceFrequency; }
  68. bool HasAudio() { return m_bHasAudioTrack; }
  69. #ifdef ENABLE_EXTERNAL_ENCODER_LOGGING
  70. bool LogMessage( const char *msg );
  71. #endif
  72. private:
  73. enum AudioGrouping_t
  74. {
  75. AG_NONE = 0,
  76. AG_FIXED_SIZE,
  77. AG_PER_FRAME
  78. };
  79. // disable copy constructor and copy assignment
  80. CQTVideoFileComposer( CQTVideoFileComposer &rhs );
  81. CQTVideoFileComposer& operator= ( CQTVideoFileComposer &rhs );
  82. bool CheckForReadyness();
  83. bool BeginMovieCreation();
  84. bool EndMovieCreation( bool saveMovieData );
  85. bool SyncAndFlushAudio();
  86. int GetAudioSampleCountThruFrame( int frameNo );
  87. VideoResult_t m_LastResult;
  88. // Current State of Movie Creation;
  89. bool m_bMovieCreated;
  90. bool m_bHasAudioTrack;
  91. bool m_bMovieConfigured;
  92. bool m_bSourceImagesConfigured;
  93. bool m_bSourceAudioConfigured;
  94. bool m_bComposingMovie;
  95. bool m_bMovieCompleted;
  96. int m_nFramesAdded;
  97. int m_nAudioFramesAdded;
  98. int m_nSamplesAdded;
  99. int m_nSamplesAddedToMedia;
  100. // parameters of the movie to create;
  101. int m_MovieFrameWidth;
  102. int m_MovieFrameHeight;
  103. VideoFrameRate_t m_MovieRecordFPS;
  104. TimeScale m_MovieTimeScale;
  105. TimeValue m_DurationPerFrame;
  106. // Audio recording options
  107. AudioEncodeOptions_t m_AudioOptions; // Option flags specifed by user
  108. AudioGrouping_t m_SampleGrouping; // Mode to group samples
  109. int m_nAudioSampleGroupSize; // number of samples to collect per sample group
  110. int m_AudioSourceFrequency; // Source frequency of the supplied audio
  111. int m_AudioBytesPerSample;
  112. bool m_bBufferSourceAudio;
  113. bool m_bLimitAudioDurationToVideo;
  114. byte *m_srcAudioBuffer; // buffer to hold audio samples
  115. size_t m_srcAudioBufferSize;
  116. size_t m_srcAudioBufferCurrentSize;
  117. int m_AudioSampleFrameCounter;
  118. char *m_FileName;
  119. int m_SrcImageWidth;
  120. int m_SrcImageHeight;
  121. size_t m_SrcImageSize;
  122. int m_ScrImageMaxCompressedSize;
  123. byte *m_SrcImageBuffer;
  124. Handle m_SrcImageCompressedBuffer;
  125. OSType m_SrcPixelFormat;
  126. int m_SrcBytesPerPixel;
  127. OSType m_GWorldPixelFormat;
  128. int m_GWorldBytesPerPixel;
  129. int m_GWorldImageWidth;
  130. int m_GWorldImageHeight;
  131. Handle m_srcSoundDescription;
  132. // parameters used by QuickTime
  133. CodecQ m_EncodeQuality;
  134. CodecType m_VideoCodecToUse;
  135. Fixed m_EncodeGamma;
  136. Rect m_GWorldRect;
  137. GWorldPtr m_theSrcGWorld;
  138. // short m_ResRefNum; // QuickTime Movie Resource Ref number
  139. Handle m_MovieFileDataRef;
  140. OSType m_MovieFileDataRefType;
  141. DataHandler m_MovieFileDataHandler;
  142. Movie m_theMovie;
  143. Track m_theVideoTrack;
  144. Track m_theAudioTrack;
  145. Media m_theVideoMedia;
  146. Media m_theAudioMedia;
  147. #ifdef LOG_ENCODER_OPERATIONS
  148. FileHandle_t m_LogFile;
  149. void LogMsg( PRINTF_FORMAT_STRING const char* pMsg, ... );
  150. #endif
  151. #ifdef LOG_FRAMES_TO_TGA
  152. char m_TGAFileBase[MAX_PATH];
  153. #endif
  154. };
  155. class CQuickTimeVideoRecorder : public IVideoRecorder
  156. {
  157. public:
  158. CQuickTimeVideoRecorder();
  159. ~CQuickTimeVideoRecorder();
  160. virtual bool EstimateMovieFileSize( size_t *pEstSize, int movieWidth, int movieHeight, VideoFrameRate_t movieFps, float movieDuration, VideoEncodeCodec_t theCodec, int videoQuality, AudioEncodeSourceFormat_t srcAudioFormat = AudioEncodeSourceFormat::AUDIO_NONE, int audioSampleRate = 0 );
  161. virtual bool CreateNewMovieFile( const char *pFilename, bool hasAudioTrack = false );
  162. virtual bool SetMovieVideoParameters( VideoEncodeCodec_t theCodec, int videoQuality, int movieFrameWidth, int movieFrameHeight, VideoFrameRate_t movieFPS, VideoEncodeGamma_t gamma = VideoEncodeGamma::NO_GAMMA_ADJUST );
  163. virtual bool SetMovieSourceImageParameters( VideoEncodeSourceFormat_t srcImageFormat, int imgWidth, int imgHeight );
  164. virtual bool SetMovieSourceAudioParameters( AudioEncodeSourceFormat_t srcAudioFormat = AudioEncodeSourceFormat::AUDIO_NONE, int audioSampleRate = 0, AudioEncodeOptions_t audioOptions = AudioEncodeOptions::NO_AUDIO_OPTIONS, int audioSampleGroupSize = 0 );
  165. virtual bool IsReadyToRecord();
  166. virtual VideoResult_t GetLastResult();
  167. virtual bool AppendVideoFrame( void *pFrameBuffer, int nStrideAdjustBytes = 0 );
  168. virtual bool AppendAudioSamples( void *pSampleBuffer, size_t sampleSize );
  169. virtual int GetFrameCount();
  170. virtual int GetSampleCount();
  171. virtual int GetSampleRate();
  172. virtual VideoFrameRate_t GetFPS();
  173. virtual bool AbortMovie();
  174. virtual bool FinishMovie( bool SaveMovieToDisk = true );
  175. #ifdef ENABLE_EXTERNAL_ENCODER_LOGGING
  176. virtual bool LogMessage( const char *msg );
  177. #endif
  178. private:
  179. void SetResult( VideoResult_t resultCode );
  180. float GetDataRate( int quality, int width, int height );
  181. CQTVideoFileComposer *m_pEncoder;
  182. VideoResult_t m_LastResult;
  183. bool m_bHasAudio;
  184. bool m_bMovieFinished;
  185. };
  186. #endif // QUICKTIME_RECORDER_H