aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/media_file/media_file_unittest.cc
blob: 6541a8fb7c3834d34d9c2e28b03c121070cc1053 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "testing/gtest/include/gtest/gtest.h"
#include "webrtc/modules/media_file/media_file.h"
#include "webrtc/system_wrappers/include/sleep.h"
#include "webrtc/test/testsupport/fileutils.h"

class MediaFileTest : public testing::Test {
 protected:
  void SetUp() {
    // Use number 0 as the the identifier and pass to CreateMediaFile.
    media_file_ = webrtc::MediaFile::CreateMediaFile(0);
    ASSERT_TRUE(media_file_ != NULL);
  }
  void TearDown() {
    webrtc::MediaFile::DestroyMediaFile(media_file_);
    media_file_ = NULL;
  }
  webrtc::MediaFile* media_file_;
};

#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
#define MAYBE_StartPlayingAudioFileWithoutError \
  DISABLED_StartPlayingAudioFileWithoutError
#else
#define MAYBE_StartPlayingAudioFileWithoutError \
  StartPlayingAudioFileWithoutError
#endif
TEST_F(MediaFileTest, MAYBE_StartPlayingAudioFileWithoutError) {
  // TODO(leozwang): Use hard coded filename here, we want to
  // loop through all audio files in future
  const std::string audio_file = webrtc::test::ProjectRootPath() +
      "data/voice_engine/audio_tiny48.wav";
  ASSERT_EQ(0, media_file_->StartPlayingAudioFile(
      audio_file.c_str(),
      0,
      false,
      webrtc::kFileFormatWavFile));

  ASSERT_EQ(true, media_file_->IsPlaying());

  webrtc::SleepMs(1);

  ASSERT_EQ(0, media_file_->StopPlaying());
}

#if defined(WEBRTC_IOS)
#define MAYBE_WriteWavFile DISABLED_WriteWavFile
#else
#define MAYBE_WriteWavFile WriteWavFile
#endif
TEST_F(MediaFileTest, MAYBE_WriteWavFile) {
  // Write file.
  static const size_t kHeaderSize = 44;
  static const size_t kPayloadSize = 320;
  webrtc::CodecInst codec = {
    0, "L16", 16000, static_cast<int>(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
  };
  static_assert(sizeof(kExpectedHeader) == kHeaderSize, "header size");

  EXPECT_EQ(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));
}