1b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org/*
2b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
4b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Use of this source code is governed by a BSD-style license
5b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  that can be found in the LICENSE file in the root of the source
6b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  tree. An additional intellectual property rights grant can be found
7b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  in the file PATENTS.  All contributing project authors may
8b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org */
10b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
11b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Note: the class cannot be used for reading and writing at the same time.
12b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#ifndef WEBRTC_MODULES_MEDIA_FILE_SOURCE_MEDIA_FILE_UTILITY_H_
13b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#define WEBRTC_MODULES_MEDIA_FILE_SOURCE_MEDIA_FILE_UTILITY_H_
14b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
15b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#include <stdio.h>
16b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
173eba48c58401b1b8421dd99d10abb47555e2f56cpbos@webrtc.org#include "webrtc/common_types.h"
183eba48c58401b1b8421dd99d10abb47555e2f56cpbos@webrtc.org#include "webrtc/modules/media_file/interface/media_file_defines.h"
19b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
20b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgnamespace webrtc {
21b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgclass AviFile;
22b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgclass InStream;
23b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgclass OutStream;
24b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
25b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgclass ModuleFileUtility
26b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org{
27b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgpublic:
28b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
29283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    ModuleFileUtility(const int32_t id);
30b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    ~ModuleFileUtility();
31b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
32b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#ifdef WEBRTC_MODULE_UTILITY_VIDEO
33b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Open the file specified by fileName for reading (relative path is
34b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // allowed). If loop is true the file will be played until StopPlaying() is
35b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // called. When end of file is reached the file is read from the start.
36b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Only video will be read if videoOnly is true.
37283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t InitAviReading(const char* fileName, bool videoOnly, bool loop);
38b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
39b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Put 10-60ms of audio data from file into the outBuffer depending on
40b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // codec frame size. bufferLengthInBytes indicates the size of outBuffer.
41b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // The return value is the number of bytes written to audioBuffer.
42b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Note: This API only play mono audio but can be used on file containing
43b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // audio with more channels (in which case the audio will be coverted to
44b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // mono).
45283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t ReadAviAudioData(int8_t* outBuffer,
46283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                             const uint32_t bufferLengthInBytes);
47b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
48b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Put one video frame into outBuffer. bufferLengthInBytes indicates the
49b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // size of outBuffer.
50b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // The return value is the number of bytes written to videoBuffer.
51283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t ReadAviVideoData(int8_t* videoBuffer,
52283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                             const uint32_t bufferLengthInBytes);
53b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
54b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Open/create the file specified by fileName for writing audio/video data
55b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // (relative path is allowed). codecInst specifies the encoding of the audio
56b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // data. videoCodecInst specifies the encoding of the video data. Only video
57b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // data will be recorded if videoOnly is true.
58283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t InitAviWriting(const char* filename,
59283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                           const CodecInst& codecInst,
60283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                           const VideoCodec& videoCodecInst,
61283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                           const bool videoOnly);
62b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
63b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Write one audio frame, i.e. the bufferLengthinBytes first bytes of
64b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // audioBuffer, to file. The audio frame size is determined by the
65b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // codecInst.pacsize parameter of the last sucessfull
66b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // InitAviWriting(..) call.
67b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Note: bufferLength must be exactly one frame.
68283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t WriteAviAudioData(const int8_t* audioBuffer,
69283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                              uint32_t bufferLengthInBytes);
70b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
71b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
72b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Write one video frame, i.e. the bufferLength first bytes of videoBuffer,
73b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // to file.
74b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Note: videoBuffer can contain encoded data. The codec used must be the
75b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // same as what was specified by videoCodecInst for the last successfull
76b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // InitAviWriting(..) call. The videoBuffer must contain exactly
77b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // one video frame.
78283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t WriteAviVideoData(const int8_t* videoBuffer,
79283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                              uint32_t bufferLengthInBytes);
80b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
81b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Stop recording to file or stream.
82283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t CloseAviFile();
83b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
84283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t VideoCodecInst(VideoCodec& codecInst);
85b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#endif // #ifdef WEBRTC_MODULE_UTILITY_VIDEO
86b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
87b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Prepare for playing audio from stream.
88b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // startPointMs and stopPointMs, unless zero, specify what part of the file
89b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // should be read. From startPointMs ms to stopPointMs ms.
90283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t InitWavReading(InStream& stream,
91283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                           const uint32_t startPointMs = 0,
92283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                           const uint32_t stopPointMs = 0);
93b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
94b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Put 10-60ms of audio data from stream into the audioBuffer depending on
95b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // codec frame size. dataLengthInBytes indicates the size of audioBuffer.
96b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // The return value is the number of bytes written to audioBuffer.
97b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Note: This API only play mono audio but can be used on file containing
98b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // audio with more channels (in which case the audio will be converted to
99b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // mono).
100283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t ReadWavDataAsMono(InStream& stream, int8_t* audioBuffer,
101283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                              const uint32_t dataLengthInBytes);
102b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
103b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Put 10-60ms, depending on codec frame size, of audio data from file into
104b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // audioBufferLeft and audioBufferRight. The buffers contain the left and
105b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // right channel of played out stereo audio.
106b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // dataLengthInBytes  indicates the size of both audioBufferLeft and
107b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // audioBufferRight.
108b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // The return value is the number of bytes read for each buffer.
109b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Note: This API can only be successfully called for WAV files with stereo
110b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // audio.
111283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t ReadWavDataAsStereo(InStream& wav,
112283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                                int8_t* audioBufferLeft,
113283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                                int8_t* audioBufferRight,
114283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                                const uint32_t bufferLength);
115b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
116b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Prepare for recording audio to stream.
117b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // codecInst specifies the encoding of the audio data.
118b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Note: codecInst.channels should be set to 2 for stereo (and 1 for
119b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // mono). Stereo is only supported for WAV files.
120283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t InitWavWriting(OutStream& stream, const CodecInst& codecInst);
121b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
122b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Write one audio frame, i.e. the bufferLength first bytes of audioBuffer,
123b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // to file. The audio frame size is determined by the codecInst.pacsize
124b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // parameter of the last sucessfull StartRecordingAudioFile(..) call.
125b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // The return value is the number of bytes written to audioBuffer.
126283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t WriteWavData(OutStream& stream,
127283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                         const int8_t* audioBuffer,
128283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                         const uint32_t bufferLength);
129b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
130b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Finalizes the WAV header so that it is correct if nothing more will be
131b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // written to stream.
132b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Note: this API must be called before closing stream to ensure that the
133b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    //       WAVE header is updated with the file size. Don't call this API
134b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    //       if more samples are to be written to stream.
135283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t UpdateWavHeader(OutStream& stream);
136b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
137b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Prepare for playing audio from stream.
138b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // startPointMs and stopPointMs, unless zero, specify what part of the file
139b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // should be read. From startPointMs ms to stopPointMs ms.
140b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // freqInHz is the PCM sampling frequency.
141b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // NOTE, allowed frequencies are 8000, 16000 and 32000 (Hz)
142283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t InitPCMReading(InStream& stream,
143283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                           const uint32_t startPointMs = 0,
144283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                           const uint32_t stopPointMs = 0,
145283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                           const uint32_t freqInHz = 16000);
146b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
147b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Put 10-60ms of audio data from stream into the audioBuffer depending on
148b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // codec frame size. dataLengthInBytes indicates the size of audioBuffer.
149b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // The return value is the number of bytes written to audioBuffer.
150283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t ReadPCMData(InStream& stream, int8_t* audioBuffer,
151283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                        const uint32_t dataLengthInBytes);
152b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
153b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Prepare for recording audio to stream.
154b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // freqInHz is the PCM sampling frequency.
155b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // NOTE, allowed frequencies are 8000, 16000 and 32000 (Hz)
156283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t InitPCMWriting(OutStream& stream, const uint32_t freqInHz = 16000);
157b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
158b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Write one 10ms audio frame, i.e. the bufferLength first bytes of
159b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // audioBuffer, to file. The audio frame size is determined by the freqInHz
160b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // parameter of the last sucessfull InitPCMWriting(..) call.
161b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // The return value is the number of bytes written to audioBuffer.
162283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t WritePCMData(OutStream& stream,
163283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                         const int8_t* audioBuffer,
164283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                         uint32_t bufferLength);
165b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
166b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Prepare for playing audio from stream.
167b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // startPointMs and stopPointMs, unless zero, specify what part of the file
168b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // should be read. From startPointMs ms to stopPointMs ms.
169283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t InitCompressedReading(InStream& stream,
170283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                                  const uint32_t startPointMs = 0,
171283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                                  const uint32_t stopPointMs = 0);
172b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
173b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Put 10-60ms of audio data from stream into the audioBuffer depending on
174b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // codec frame size. dataLengthInBytes indicates the size of audioBuffer.
175b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // The return value is the number of bytes written to audioBuffer.
176283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t ReadCompressedData(InStream& stream,
177283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                               int8_t* audioBuffer,
178283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                               const uint32_t dataLengthInBytes);
179b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
180b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Prepare for recording audio to stream.
181b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // codecInst specifies the encoding of the audio data.
182283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t InitCompressedWriting(OutStream& stream,
183283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                                  const CodecInst& codecInst);
184b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
185b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Write one audio frame, i.e. the bufferLength first bytes of audioBuffer,
186b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // to file. The audio frame size is determined by the codecInst.pacsize
187b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // parameter of the last sucessfull InitCompressedWriting(..) call.
188b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // The return value is the number of bytes written to stream.
189b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Note: bufferLength must be exactly one frame.
190283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t WriteCompressedData(OutStream& stream,
191283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                                const int8_t* audioBuffer,
192283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                                const uint32_t bufferLength);
193b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
194b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Prepare for playing audio from stream.
195b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // codecInst specifies the encoding of the audio data.
196283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t InitPreEncodedReading(InStream& stream,
197283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                                  const CodecInst& codecInst);
198b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
199b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Put 10-60ms of audio data from stream into the audioBuffer depending on
200b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // codec frame size. dataLengthInBytes indicates the size of audioBuffer.
201b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // The return value is the number of bytes written to audioBuffer.
202283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t ReadPreEncodedData(InStream& stream,
203283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                               int8_t* audioBuffer,
204283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                               const uint32_t dataLengthInBytes);
205b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
206b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Prepare for recording audio to stream.
207b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // codecInst specifies the encoding of the audio data.
208283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t InitPreEncodedWriting(OutStream& stream,
209283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                                  const CodecInst& codecInst);
210b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
211b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Write one audio frame, i.e. the bufferLength first bytes of audioBuffer,
212b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // to stream. The audio frame size is determined by the codecInst.pacsize
213b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // parameter of the last sucessfull InitPreEncodedWriting(..) call.
214b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org   // The return value is the number of bytes written to stream.
215b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Note: bufferLength must be exactly one frame.
216283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t WritePreEncodedData(OutStream& stream,
217283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                                const int8_t* inData,
218283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                                const uint32_t dataLengthInBytes);
219b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
220b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Set durationMs to the size of the file (in ms) specified by fileName.
221b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // freqInHz specifies the sampling frequency of the file.
222283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t FileDurationMs(const char* fileName,
223283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                           const FileFormats fileFormat,
224283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                           const uint32_t freqInHz = 16000);
225b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
226b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Return the number of ms that have been played so far.
227283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    uint32_t PlayoutPositionMs();
228b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
229b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Update codecInst according to the current audio codec being used for
230b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // reading or writing.
231283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t codec_info(CodecInst& codecInst);
232b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
233b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgprivate:
234b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Biggest WAV frame supported is 10 ms at 48kHz of 2 channel, 16 bit audio.
235b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    enum{WAV_MAX_BUFFER_SIZE = 480*2*2};
236b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
237b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
238283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t InitWavCodec(uint32_t samplesPerSec,
239283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                         uint32_t channels,
240283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                         uint32_t bitsPerSample,
241283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                         uint32_t formatTag);
242b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
243b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Parse the WAV header in stream.
244283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t ReadWavHeader(InStream& stream);
245b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
246b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Update the WAV header. freqInHz, bytesPerSample, channels, format,
247b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // lengthInBytes specify characterists of the audio data.
248b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // freqInHz is the sampling frequency. bytesPerSample is the sample size in
249b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // bytes. channels is the number of channels, e.g. 1 is mono and 2 is
250b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // stereo. format is the encode format (e.g. PCMU, PCMA, PCM etc).
251b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // lengthInBytes is the number of bytes the audio samples are using up.
252283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t WriteWavHeader(OutStream& stream,
253283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                           const uint32_t freqInHz,
254283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                           const uint32_t bytesPerSample,
255283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                           const uint32_t channels,
256283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                           const uint32_t format,
257283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                           const uint32_t lengthInBytes);
258b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
259b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Put dataLengthInBytes of audio data from stream into the audioBuffer.
260b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // The return value is the number of bytes written to audioBuffer.
261283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t ReadWavData(InStream& stream, uint8_t* audioBuffer,
262283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org                        const uint32_t dataLengthInBytes);
263b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
264b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Update the current audio codec being used for reading or writing
265b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // according to codecInst.
266283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t set_codec_info(const CodecInst& codecInst);
267b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
268b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    struct WAVE_FMTINFO_header
269b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    {
270283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org        int16_t formatTag;
271283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org        int16_t nChannels;
272283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org        int32_t nSamplesPerSec;
273283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org        int32_t nAvgBytesPerSec;
274283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org        int16_t nBlockAlign;
275283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org        int16_t nBitsPerSample;
276b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    };
277b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Identifiers for preencoded files.
278b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    enum MediaFileUtility_CodecType
279b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    {
280b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecNoCodec  = 0,
281b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecIsac,
282b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecIsacSwb,
283b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecIsacLc,
284b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecL16_8Khz,
285b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecL16_16kHz,
286b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecL16_32Khz,
287b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecPcmu,
288b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecPcma,
289b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecIlbc20Ms,
290b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecIlbc30Ms,
291b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecG722,
292b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecG722_1_32Kbps,
293b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecG722_1_24Kbps,
294b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecG722_1_16Kbps,
295b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecG722_1c_48,
296b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecG722_1c_32,
297b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecG722_1c_24,
298b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecAmr,
299b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecAmrWb,
300b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecG729,
301b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecG729_1,
302b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecG726_40,
303b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecG726_32,
304b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecG726_24,
305b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecG726_16,
306b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecSpeex8Khz,
307b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        kCodecSpeex16Khz
308b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    };
309b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
310b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // TODO (hellner): why store multiple formats. Just store either codec_info_
311b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    //                 or _wavFormatObj and supply conversion functions.
312b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    WAVE_FMTINFO_header _wavFormatObj;
313283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t _dataSize;      // Chunk size if reading a WAV file
314b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Number of bytes to read. I.e. frame size in bytes. May be multiple
315b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // chunks if reading WAV.
316283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t _readSizeBytes;
317b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
318283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t _id;
319b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
320283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    uint32_t _stopPointInMs;
321283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    uint32_t _startPointInMs;
322283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    uint32_t _playoutPositionMs;
323283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    uint32_t _bytesWritten;
324b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
325b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    CodecInst codec_info_;
326b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    MediaFileUtility_CodecType _codecId;
327b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
328b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // The amount of bytes, on average, used for one audio sample.
329283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t  _bytesPerSample;
330283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    int32_t  _readPos;
331b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
332b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Only reading or writing can be enabled, not both.
333b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    bool _reading;
334b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    bool _writing;
335b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
336b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    // Scratch buffer used for turning stereo audio to mono.
337283c29a5c34c18c5ef6860a32d1b7ce2eeb0ab5epbos@webrtc.org    uint8_t _tempData[WAV_MAX_BUFFER_SIZE];
338b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
339b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#ifdef WEBRTC_MODULE_UTILITY_VIDEO
340b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    AviFile* _aviAudioInFile;
341b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    AviFile* _aviVideoInFile;
342b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    AviFile* _aviOutFile;
343b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    VideoCodec _videoCodec;
344b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#endif
345b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
3463b89e10f31160da35b408fd00cb8f89d2b08862dpbos@webrtc.org}  // namespace webrtc
347b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#endif // WEBRTC_MODULES_MEDIA_FILE_SOURCE_MEDIA_FILE_UTILITY_H_
348