1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef WEBRTC_MODULES_MEDIA_FILE_SOURCE_MEDIA_FILE_IMPL_H_ 12 #define WEBRTC_MODULES_MEDIA_FILE_SOURCE_MEDIA_FILE_IMPL_H_ 13 14 #include "webrtc/common_types.h" 15 #include "webrtc/modules/interface/module_common_types.h" 16 #include "webrtc/modules/media_file/interface/media_file.h" 17 #include "webrtc/modules/media_file/interface/media_file_defines.h" 18 #include "webrtc/modules/media_file/source/media_file_utility.h" 19 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" 20 21 namespace webrtc { 22 class MediaFileImpl : public MediaFile 23 { 24 25 public: 26 MediaFileImpl(const int32_t id); 27 ~MediaFileImpl(); 28 29 int32_t ChangeUniqueId(const int32_t id); 30 int32_t Process(); 31 int32_t TimeUntilNextProcess(); 32 33 // MediaFile functions 34 int32_t PlayoutAudioData(int8_t* audioBuffer, uint32_t& dataLengthInBytes); 35 int32_t PlayoutAVIVideoData(int8_t* videoBuffer, 36 uint32_t& dataLengthInBytes); 37 int32_t PlayoutStereoData(int8_t* audioBufferLeft, int8_t* audioBufferRight, 38 uint32_t& dataLengthInBytes); 39 virtual int32_t StartPlayingAudioFile( 40 const char* fileName, 41 const uint32_t notificationTimeMs = 0, 42 const bool loop = false, 43 const FileFormats format = kFileFormatPcm16kHzFile, 44 const CodecInst* codecInst = NULL, 45 const uint32_t startPointMs = 0, 46 const uint32_t stopPointMs = 0); 47 int32_t StartPlayingVideoFile(const char* fileName, const bool loop, 48 bool videoOnly, const FileFormats format); 49 int32_t StartPlayingAudioStream(InStream& stream, 50 const uint32_t notificationTimeMs = 0, 51 const FileFormats format = kFileFormatPcm16kHzFile, 52 const CodecInst* codecInst = NULL, 53 const uint32_t startPointMs = 0, 54 const uint32_t stopPointMs = 0); 55 int32_t StopPlaying(); 56 bool IsPlaying(); 57 int32_t PlayoutPositionMs(uint32_t& positionMs) const; 58 int32_t IncomingAudioData(const int8_t* audioBuffer, 59 const uint32_t bufferLength); 60 int32_t IncomingAVIVideoData(const int8_t* audioBuffer, 61 const uint32_t bufferLength); 62 int32_t StartRecordingAudioFile( 63 const char* fileName, 64 const FileFormats format, 65 const CodecInst& codecInst, 66 const uint32_t notificationTimeMs = 0, 67 const uint32_t maxSizeBytes = 0); 68 int32_t StartRecordingVideoFile( 69 const char* fileName, 70 const FileFormats format, 71 const CodecInst& codecInst, 72 const VideoCodec& videoCodecInst, 73 bool videoOnly = false); 74 int32_t StartRecordingAudioStream( 75 OutStream& stream, 76 const FileFormats format, 77 const CodecInst& codecInst, 78 const uint32_t notificationTimeMs = 0); 79 int32_t StopRecording(); 80 bool IsRecording(); 81 int32_t RecordDurationMs(uint32_t& durationMs); 82 bool IsStereo(); 83 int32_t SetModuleFileCallback(FileCallback* callback); 84 int32_t FileDurationMs( 85 const char* fileName, 86 uint32_t& durationMs, 87 const FileFormats format, 88 const uint32_t freqInHz = 16000); 89 int32_t codec_info(CodecInst& codecInst) const; 90 int32_t VideoCodecInst(VideoCodec& codecInst) const; 91 92 private: 93 // Returns true if the combination of format and codecInst is valid. 94 static bool ValidFileFormat(const FileFormats format, 95 const CodecInst* codecInst); 96 97 98 // Returns true if the filename is valid 99 static bool ValidFileName(const char* fileName); 100 101 // Returns true if the combination of startPointMs and stopPointMs is valid. 102 static bool ValidFilePositions(const uint32_t startPointMs, 103 const uint32_t stopPointMs); 104 105 // Open the file specified by fileName for reading (relative path is 106 // allowed). FileCallback::PlayNotification(..) will be called after 107 // notificationTimeMs of the file has been played if notificationTimeMs is 108 // greater than zero. If loop is true the file will be played until 109 // StopPlaying() is called. When end of file is reached the file is read 110 // from the start. format specifies the type of file fileName refers to. 111 // codecInst specifies the encoding of the audio data. Note that 112 // file formats that contain this information (like WAV files) don't need to 113 // provide a non-NULL codecInst. Only video will be read if videoOnly is 114 // true. startPointMs and stopPointMs, unless zero, 115 // specify what part of the file should be read. From startPointMs ms to 116 // stopPointMs ms. 117 int32_t StartPlayingFile( 118 const char* fileName, 119 const uint32_t notificationTimeMs = 0, 120 const bool loop = false, 121 bool videoOnly = false, 122 const FileFormats format = kFileFormatPcm16kHzFile, 123 const CodecInst* codecInst = NULL, 124 const uint32_t startPointMs = 0, 125 const uint32_t stopPointMs = 0); 126 127 // Opens the file specified by fileName for reading (relative path is 128 // allowed) if format is kFileFormatAviFile otherwise use stream for 129 // reading. FileCallback::PlayNotification(..) will be called after 130 // notificationTimeMs of the file has been played if notificationTimeMs is 131 // greater than zero. If loop is true the file will be played until 132 // StopPlaying() is called. When end of file is reached the file is read 133 // from the start. format specifies the type of file fileName refers to. 134 // codecInst specifies the encoding of the audio data. Note that 135 // file formats that contain this information (like WAV files) don't need to 136 // provide a non-NULL codecInst. Only video will be read if videoOnly is 137 // true. startPointMs and stopPointMs, unless zero, 138 // specify what part of the file should be read. From startPointMs ms to 139 // stopPointMs ms. 140 // TODO (hellner): there is no reason why fileName should be needed here. 141 int32_t StartPlayingStream( 142 InStream& stream, 143 const char* fileName, 144 bool loop, 145 const uint32_t notificationTimeMs = 0, 146 const FileFormats format = kFileFormatPcm16kHzFile, 147 const CodecInst* codecInst = NULL, 148 const uint32_t startPointMs = 0, 149 const uint32_t stopPointMs = 0, 150 bool videoOnly = true); 151 152 // Writes one frame into dataBuffer. dataLengthInBytes is both an input and 153 // output parameter. As input parameter it indicates the size of 154 // audioBuffer. As output parameter it indicates the number of bytes 155 // written to audioBuffer. If video is true the data written is a video 156 // frame otherwise it is an audio frame. 157 int32_t PlayoutData(int8_t* dataBuffer, uint32_t& dataLengthInBytes, 158 bool video); 159 160 // Write one frame, i.e. the bufferLength first bytes of audioBuffer, 161 // to file. The frame is an audio frame if video is true otherwise it is an 162 // audio frame. 163 int32_t IncomingAudioVideoData(const int8_t* buffer, 164 const uint32_t bufferLength, 165 const bool video); 166 167 // Open/creates file specified by fileName for writing (relative path is 168 // allowed) if format is kFileFormatAviFile otherwise use stream for 169 // writing. FileCallback::RecordNotification(..) will be called after 170 // notificationTimeMs of audio data has been recorded if 171 // notificationTimeMs is greater than zero. 172 // format specifies the type of file that should be created/opened. 173 // codecInst specifies the encoding of the audio data. videoCodecInst 174 // specifies the encoding of the video data. maxSizeBytes specifies the 175 // number of bytes allowed to be written to file if it is greater than zero. 176 // If format is kFileFormatAviFile and videoOnly is true the AVI file will 177 // only contain video frames. 178 // Note: codecInst.channels should be set to 2 for stereo (and 1 for 179 // mono). Stereo is only supported for WAV files. 180 int32_t StartRecordingFile( 181 const char* fileName, 182 const FileFormats format, 183 const CodecInst& codecInst, 184 const VideoCodec& videoCodecInst, 185 const uint32_t notificationTimeMs = 0, 186 const uint32_t maxSizeBytes = 0, 187 bool videoOnly = false); 188 189 // Open/creates file specified by fileName for writing (relative path is 190 // allowed). FileCallback::RecordNotification(..) will be called after 191 // notificationTimeMs of audio data has been recorded if 192 // notificationTimeMs is greater than zero. 193 // format specifies the type of file that should be created/opened. 194 // codecInst specifies the encoding of the audio data. videoCodecInst 195 // specifies the encoding of the video data. maxSizeBytes specifies the 196 // number of bytes allowed to be written to file if it is greater than zero. 197 // If format is kFileFormatAviFile and videoOnly is true the AVI file will 198 // only contain video frames. 199 // Note: codecInst.channels should be set to 2 for stereo (and 1 for 200 // mono). Stereo is only supported for WAV files. 201 // TODO (hellner): there is no reason why fileName should be needed here. 202 int32_t StartRecordingStream( 203 OutStream& stream, 204 const char* fileName, 205 const FileFormats format, 206 const CodecInst& codecInst, 207 const VideoCodec& videoCodecInst, 208 const uint32_t notificationTimeMs = 0, 209 const bool videoOnly = false); 210 211 // Returns true if frequencyInHz is a supported frequency. 212 static bool ValidFrequency(const uint32_t frequencyInHz); 213 214 void HandlePlayCallbacks(int32_t bytesRead); 215 216 int32_t _id; 217 CriticalSectionWrapper* _crit; 218 CriticalSectionWrapper* _callbackCrit; 219 220 ModuleFileUtility* _ptrFileUtilityObj; 221 CodecInst codec_info_; 222 223 InStream* _ptrInStream; 224 OutStream* _ptrOutStream; 225 226 FileFormats _fileFormat; 227 uint32_t _recordDurationMs; 228 uint32_t _playoutPositionMs; 229 uint32_t _notificationMs; 230 231 bool _playingActive; 232 bool _recordingActive; 233 bool _isStereo; 234 bool _openFile; 235 236 char _fileName[512]; 237 238 FileCallback* _ptrCallback; 239 }; 240 } // namespace webrtc 241 242 #endif // WEBRTC_MODULES_MEDIA_FILE_SOURCE_MEDIA_FILE_IMPL_H_ 243