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