Add unit test for MediaFile WAV file writing

R=aluebs@webrtc.org, andrew@webrtc.org, minyue@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/16029004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@6713 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
kwiberg@webrtc.org 2014-07-17 08:11:32 +00:00
parent 4a472fb18d
commit 2ade42bd96

View File

@ -10,6 +10,7 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "webrtc/modules/media_file/interface/media_file.h"
#include "webrtc/system_wrappers/interface/compile_assert.h"
#include "webrtc/system_wrappers/interface/sleep.h"
#include "webrtc/test/testsupport/fileutils.h"
#include "webrtc/test/testsupport/gtest_disable.h"
@ -45,3 +46,50 @@ TEST_F(MediaFileTest, DISABLED_ON_ANDROID(StartPlayingAudioFileWithoutError)) {
ASSERT_EQ(0, media_file_->StopPlaying());
}
TEST_F(MediaFileTest, WriteWavFile) {
// Write file.
static const int kHeaderSize = 44;
static const int kPayloadSize = 320;
webrtc::CodecInst codec = {0, "L16", 16000, kPayloadSize, 1};
std::string outfile = webrtc::test::OutputPath() + "wavtest.wav";
ASSERT_EQ(0,
media_file_->StartRecordingAudioFile(
outfile.c_str(), webrtc::kFileFormatWavFile, codec));
static const int8_t kFakeData[kPayloadSize] = {0};
ASSERT_EQ(0, media_file_->IncomingAudioData(kFakeData, kPayloadSize));
ASSERT_EQ(0, media_file_->StopRecording());
// Check the file we just wrote.
static const uint8_t kExpectedHeader[] = {
'R', 'I', 'F', 'F',
0x64, 0x1, 0, 0, // size of whole file - 8: 320 + 44 - 8
'W', 'A', 'V', 'E',
'f', 'm', 't', ' ',
0x10, 0, 0, 0, // size of fmt block - 8: 24 - 8
0x1, 0, // format: PCM (1)
0x1, 0, // channels: 1
0x80, 0x3e, 0, 0, // sample rate: 16000
0, 0x7d, 0, 0, // byte rate: 2 * 16000
0x2, 0, // block align: NumChannels * BytesPerSample
0x10, 0, // bits per sample: 2 * 8
'd', 'a', 't', 'a',
0x40, 0x1, 0, 0, // size of payload: 320
};
COMPILE_ASSERT(sizeof(kExpectedHeader) == kHeaderSize, header_size);
EXPECT_EQ(size_t(kHeaderSize + kPayloadSize),
webrtc::test::GetFileSize(outfile));
FILE* f = fopen(outfile.c_str(), "rb");
ASSERT_TRUE(f);
uint8_t header[kHeaderSize];
ASSERT_EQ(1u, fread(header, kHeaderSize, 1, f));
EXPECT_EQ(0, memcmp(kExpectedHeader, header, kHeaderSize));
uint8_t payload[kPayloadSize];
ASSERT_EQ(1u, fread(payload, kPayloadSize, 1, f));
EXPECT_EQ(0, memcmp(kFakeData, payload, kPayloadSize));
EXPECT_EQ(0, fclose(f));
}