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#include "testing/gtest/include/gtest/gtest.h"
12#include "webrtc/modules/media_file/media_file.h"
13#include "webrtc/system_wrappers/include/sleep.h"
14#include "webrtc/test/testsupport/fileutils.h"
15
16class 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
30#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) {
38  // 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() +
41      "data/voice_engine/audio_tiny48.wav";
42  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
50  webrtc::SleepMs(1);
51
52  ASSERT_EQ(0, media_file_->StopPlaying());
53}
54
55#if defined(WEBRTC_IOS)
56#define MAYBE_WriteWavFile DISABLED_WriteWavFile
57#else
58#define MAYBE_WriteWavFile WriteWavFile
59#endif
60TEST_F(MediaFileTest, MAYBE_WriteWavFile) {
61  // Write file.
62  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  };
67  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  };
91  static_assert(sizeof(kExpectedHeader) == kHeaderSize, "header size");
92
93  EXPECT_EQ(kHeaderSize + kPayloadSize, webrtc::test::GetFileSize(outfile));
94  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}
107