blob: 3fa0afcd3dd5f06b5f37a2a4ec22758ab09b4d75 [file] [log] [blame]
kjellander@webrtc.orgd492f722011-11-24 07:20:00 +00001/*
leozwang@webrtc.org0dc8efe2012-04-03 15:11:01 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
kjellander@webrtc.orgd492f722011-11-24 07:20:00 +00003 *
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
kwiberg77eab702016-09-28 17:42:01 -070011#include "webrtc/test/gtest.h"
Henrik Kjellander0b9e29c2015-11-16 11:12:24 +010012#include "webrtc/modules/media_file/media_file.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010013#include "webrtc/system_wrappers/include/sleep.h"
andrew@webrtc.org00c7c432013-01-02 16:06:39 +000014#include "webrtc/test/testsupport/fileutils.h"
kjellander@webrtc.orgd492f722011-11-24 07:20:00 +000015
leozwang@webrtc.org0dc8efe2012-04-03 15:11:01 +000016class MediaFileTest : public testing::Test {
17 protected:
18 void SetUp() {
19 // Use number 0 as the the identifier and pass to CreateMediaFile.
20 media_file_ = webrtc::MediaFile::CreateMediaFile(0);
21 ASSERT_TRUE(media_file_ != NULL);
22 }
23 void TearDown() {
24 webrtc::MediaFile::DestroyMediaFile(media_file_);
25 media_file_ = NULL;
26 }
27 webrtc::MediaFile* media_file_;
28};
29
Peter Boströme2976c82016-01-04 22:44:05 +010030#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
31#define MAYBE_StartPlayingAudioFileWithoutError \
32 DISABLED_StartPlayingAudioFileWithoutError
33#else
34#define MAYBE_StartPlayingAudioFileWithoutError \
35 StartPlayingAudioFileWithoutError
36#endif
37TEST_F(MediaFileTest, MAYBE_StartPlayingAudioFileWithoutError) {
leozwang@webrtc.org0dc8efe2012-04-03 15:11:01 +000038 // TODO(leozwang): Use hard coded filename here, we want to
39 // loop through all audio files in future
40 const std::string audio_file = webrtc::test::ProjectRootPath() +
andrew@webrtc.org9dc45da2012-05-23 15:39:01 +000041 "data/voice_engine/audio_tiny48.wav";
leozwang@webrtc.org0dc8efe2012-04-03 15:11:01 +000042 ASSERT_EQ(0, media_file_->StartPlayingAudioFile(
43 audio_file.c_str(),
44 0,
45 false,
46 webrtc::kFileFormatWavFile));
47
48 ASSERT_EQ(true, media_file_->IsPlaying());
49
andrew@webrtc.org00c7c432013-01-02 16:06:39 +000050 webrtc::SleepMs(1);
leozwang@webrtc.org0dc8efe2012-04-03 15:11:01 +000051
52 ASSERT_EQ(0, media_file_->StopPlaying());
53}
kwiberg@webrtc.org2ade42b2014-07-17 08:11:32 +000054
Peter Boströme2976c82016-01-04 22:44:05 +010055#if defined(WEBRTC_IOS)
56#define MAYBE_WriteWavFile DISABLED_WriteWavFile
57#else
58#define MAYBE_WriteWavFile WriteWavFile
59#endif
60TEST_F(MediaFileTest, MAYBE_WriteWavFile) {
kwiberg@webrtc.org2ade42b2014-07-17 08:11:32 +000061 // Write file.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000062 static const size_t kHeaderSize = 44;
63 static const size_t kPayloadSize = 320;
64 webrtc::CodecInst codec = {
65 0, "L16", 16000, static_cast<int>(kPayloadSize), 1
66 };
kwiberg@webrtc.org2ade42b2014-07-17 08:11:32 +000067 std::string outfile = webrtc::test::OutputPath() + "wavtest.wav";
68 ASSERT_EQ(0,
69 media_file_->StartRecordingAudioFile(
70 outfile.c_str(), webrtc::kFileFormatWavFile, codec));
71 static const int8_t kFakeData[kPayloadSize] = {0};
72 ASSERT_EQ(0, media_file_->IncomingAudioData(kFakeData, kPayloadSize));
73 ASSERT_EQ(0, media_file_->StopRecording());
74
75 // Check the file we just wrote.
76 static const uint8_t kExpectedHeader[] = {
77 'R', 'I', 'F', 'F',
78 0x64, 0x1, 0, 0, // size of whole file - 8: 320 + 44 - 8
79 'W', 'A', 'V', 'E',
80 'f', 'm', 't', ' ',
81 0x10, 0, 0, 0, // size of fmt block - 8: 24 - 8
82 0x1, 0, // format: PCM (1)
83 0x1, 0, // channels: 1
84 0x80, 0x3e, 0, 0, // sample rate: 16000
85 0, 0x7d, 0, 0, // byte rate: 2 * 16000
86 0x2, 0, // block align: NumChannels * BytesPerSample
87 0x10, 0, // bits per sample: 2 * 8
88 'd', 'a', 't', 'a',
89 0x40, 0x1, 0, 0, // size of payload: 320
90 };
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000091 static_assert(sizeof(kExpectedHeader) == kHeaderSize, "header size");
kwiberg@webrtc.org2ade42b2014-07-17 08:11:32 +000092
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000093 EXPECT_EQ(kHeaderSize + kPayloadSize, webrtc::test::GetFileSize(outfile));
kwiberg@webrtc.org2ade42b2014-07-17 08:11:32 +000094 FILE* f = fopen(outfile.c_str(), "rb");
95 ASSERT_TRUE(f);
96
97 uint8_t header[kHeaderSize];
98 ASSERT_EQ(1u, fread(header, kHeaderSize, 1, f));
99 EXPECT_EQ(0, memcmp(kExpectedHeader, header, kHeaderSize));
100
101 uint8_t payload[kPayloadSize];
102 ASSERT_EQ(1u, fread(payload, kPayloadSize, 1, f));
103 EXPECT_EQ(0, memcmp(kFakeData, payload, kPayloadSize));
104
105 EXPECT_EQ(0, fclose(f));
106}