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_MEDIA_FILE_IMPL_H_
12 #define WEBRTC_MODULES_MEDIA_FILE_MEDIA_FILE_IMPL_H_
13 
14 #include "webrtc/common_types.h"
15 #include "webrtc/modules/include/module_common_types.h"
16 #include "webrtc/modules/media_file/media_file.h"
17 #include "webrtc/modules/media_file/media_file_defines.h"
18 #include "webrtc/modules/media_file/media_file_utility.h"
19 #include "webrtc/system_wrappers/include/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 Process() override;
30     int64_t TimeUntilNextProcess() override;
31 
32     // MediaFile functions
33     int32_t PlayoutAudioData(int8_t* audioBuffer,
34                              size_t& dataLengthInBytes) override;
35 
36     int32_t PlayoutStereoData(int8_t* audioBufferLeft,
37                               int8_t* audioBufferRight,
38                               size_t& dataLengthInBytes) override;
39 
40     int32_t StartPlayingAudioFile(
41         const char* fileName,
42         const uint32_t notificationTimeMs = 0,
43         const bool loop = false,
44         const FileFormats format = kFileFormatPcm16kHzFile,
45         const CodecInst* codecInst = NULL,
46         const uint32_t startPointMs = 0,
47         const uint32_t stopPointMs = 0) override;
48 
49     int32_t StartPlayingAudioStream(
50         InStream& stream,
51         const uint32_t notificationTimeMs = 0,
52         const FileFormats format = kFileFormatPcm16kHzFile,
53         const CodecInst* codecInst = NULL,
54         const uint32_t startPointMs = 0,
55         const uint32_t stopPointMs = 0) override;
56 
57     int32_t StopPlaying() override;
58 
59     bool IsPlaying() override;
60 
61     int32_t PlayoutPositionMs(uint32_t& positionMs) const override;
62 
63     int32_t IncomingAudioData(const int8_t* audioBuffer,
64                               const size_t bufferLength) override;
65 
66     int32_t StartRecordingAudioFile(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 
72     int32_t StartRecordingAudioStream(
73         OutStream& stream,
74         const FileFormats format,
75         const CodecInst& codecInst,
76         const uint32_t notificationTimeMs = 0) override;
77 
78     int32_t StopRecording() override;
79 
80     bool IsRecording() override;
81 
82     int32_t RecordDurationMs(uint32_t& durationMs) override;
83 
84     bool IsStereo() override;
85 
86     int32_t SetModuleFileCallback(FileCallback* callback) override;
87 
88     int32_t FileDurationMs(const char* fileName,
89                            uint32_t& durationMs,
90                            const FileFormats format,
91                            const uint32_t freqInHz = 16000) override;
92 
93     int32_t codec_info(CodecInst& 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     // Returns true if frequencyInHz is a supported frequency.
109     static bool ValidFrequency(const uint32_t frequencyInHz);
110 
111     void HandlePlayCallbacks(int32_t bytesRead);
112 
113     int32_t StartPlayingStream(
114         InStream& stream,
115         bool loop,
116         const uint32_t notificationTimeMs,
117         const FileFormats format,
118         const CodecInst*  codecInst,
119         const uint32_t startPointMs,
120         const uint32_t stopPointMs);
121 
122     int32_t _id;
123     CriticalSectionWrapper* _crit;
124     CriticalSectionWrapper* _callbackCrit;
125 
126     ModuleFileUtility* _ptrFileUtilityObj;
127     CodecInst codec_info_;
128 
129     InStream*  _ptrInStream;
130     OutStream* _ptrOutStream;
131 
132     FileFormats _fileFormat;
133     uint32_t _recordDurationMs;
134     uint32_t _playoutPositionMs;
135     uint32_t _notificationMs;
136 
137     bool _playingActive;
138     bool _recordingActive;
139     bool _isStereo;
140     bool _openFile;
141 
142     char _fileName[512];
143 
144     FileCallback* _ptrCallback;
145 };
146 }  // namespace webrtc
147 
148 #endif // WEBRTC_MODULES_MEDIA_FILE_MEDIA_FILE_IMPL_H_
149