kjellander@webrtc.org | d492f72 | 2011-11-24 07:20:00 +0000 | [diff] [blame] | 1 | /* |
leozwang@webrtc.org | 0dc8efe | 2012-04-03 15:11:01 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
kjellander@webrtc.org | d492f72 | 2011-11-24 07:20:00 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | 0c4e05a | 2013-07-16 13:05:40 +0000 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
Henrik Kjellander | 0b9e29c | 2015-11-16 11:12:24 +0100 | [diff] [blame] | 12 | #include "webrtc/modules/media_file/media_file.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 13 | #include "webrtc/system_wrappers/include/sleep.h" |
andrew@webrtc.org | 00c7c43 | 2013-01-02 16:06:39 +0000 | [diff] [blame] | 14 | #include "webrtc/test/testsupport/fileutils.h" |
henrike@webrtc.org | a950300b | 2013-07-08 18:53:54 +0000 | [diff] [blame] | 15 | #include "webrtc/test/testsupport/gtest_disable.h" |
kjellander@webrtc.org | d492f72 | 2011-11-24 07:20:00 +0000 | [diff] [blame] | 16 | |
leozwang@webrtc.org | 0dc8efe | 2012-04-03 15:11:01 +0000 | [diff] [blame] | 17 | class MediaFileTest : public testing::Test { |
| 18 | protected: |
| 19 | void SetUp() { |
| 20 | // Use number 0 as the the identifier and pass to CreateMediaFile. |
| 21 | media_file_ = webrtc::MediaFile::CreateMediaFile(0); |
| 22 | ASSERT_TRUE(media_file_ != NULL); |
| 23 | } |
| 24 | void TearDown() { |
| 25 | webrtc::MediaFile::DestroyMediaFile(media_file_); |
| 26 | media_file_ = NULL; |
| 27 | } |
| 28 | webrtc::MediaFile* media_file_; |
| 29 | }; |
| 30 | |
henrika | a2c7940 | 2015-06-10 13:24:48 +0200 | [diff] [blame] | 31 | TEST_F(MediaFileTest, DISABLED_ON_IOS( |
| 32 | DISABLED_ON_ANDROID(StartPlayingAudioFileWithoutError))) { |
leozwang@webrtc.org | 0dc8efe | 2012-04-03 15:11:01 +0000 | [diff] [blame] | 33 | // TODO(leozwang): Use hard coded filename here, we want to |
| 34 | // loop through all audio files in future |
| 35 | const std::string audio_file = webrtc::test::ProjectRootPath() + |
andrew@webrtc.org | 9dc45da | 2012-05-23 15:39:01 +0000 | [diff] [blame] | 36 | "data/voice_engine/audio_tiny48.wav"; |
leozwang@webrtc.org | 0dc8efe | 2012-04-03 15:11:01 +0000 | [diff] [blame] | 37 | ASSERT_EQ(0, media_file_->StartPlayingAudioFile( |
| 38 | audio_file.c_str(), |
| 39 | 0, |
| 40 | false, |
| 41 | webrtc::kFileFormatWavFile)); |
| 42 | |
| 43 | ASSERT_EQ(true, media_file_->IsPlaying()); |
| 44 | |
andrew@webrtc.org | 00c7c43 | 2013-01-02 16:06:39 +0000 | [diff] [blame] | 45 | webrtc::SleepMs(1); |
leozwang@webrtc.org | 0dc8efe | 2012-04-03 15:11:01 +0000 | [diff] [blame] | 46 | |
| 47 | ASSERT_EQ(0, media_file_->StopPlaying()); |
| 48 | } |
kwiberg@webrtc.org | 2ade42b | 2014-07-17 08:11:32 +0000 | [diff] [blame] | 49 | |
henrika | a2c7940 | 2015-06-10 13:24:48 +0200 | [diff] [blame] | 50 | TEST_F(MediaFileTest, DISABLED_ON_IOS(WriteWavFile)) { |
kwiberg@webrtc.org | 2ade42b | 2014-07-17 08:11:32 +0000 | [diff] [blame] | 51 | // Write file. |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 52 | static const size_t kHeaderSize = 44; |
| 53 | static const size_t kPayloadSize = 320; |
| 54 | webrtc::CodecInst codec = { |
| 55 | 0, "L16", 16000, static_cast<int>(kPayloadSize), 1 |
| 56 | }; |
kwiberg@webrtc.org | 2ade42b | 2014-07-17 08:11:32 +0000 | [diff] [blame] | 57 | std::string outfile = webrtc::test::OutputPath() + "wavtest.wav"; |
| 58 | ASSERT_EQ(0, |
| 59 | media_file_->StartRecordingAudioFile( |
| 60 | outfile.c_str(), webrtc::kFileFormatWavFile, codec)); |
| 61 | static const int8_t kFakeData[kPayloadSize] = {0}; |
| 62 | ASSERT_EQ(0, media_file_->IncomingAudioData(kFakeData, kPayloadSize)); |
| 63 | ASSERT_EQ(0, media_file_->StopRecording()); |
| 64 | |
| 65 | // Check the file we just wrote. |
| 66 | static const uint8_t kExpectedHeader[] = { |
| 67 | 'R', 'I', 'F', 'F', |
| 68 | 0x64, 0x1, 0, 0, // size of whole file - 8: 320 + 44 - 8 |
| 69 | 'W', 'A', 'V', 'E', |
| 70 | 'f', 'm', 't', ' ', |
| 71 | 0x10, 0, 0, 0, // size of fmt block - 8: 24 - 8 |
| 72 | 0x1, 0, // format: PCM (1) |
| 73 | 0x1, 0, // channels: 1 |
| 74 | 0x80, 0x3e, 0, 0, // sample rate: 16000 |
| 75 | 0, 0x7d, 0, 0, // byte rate: 2 * 16000 |
| 76 | 0x2, 0, // block align: NumChannels * BytesPerSample |
| 77 | 0x10, 0, // bits per sample: 2 * 8 |
| 78 | 'd', 'a', 't', 'a', |
| 79 | 0x40, 0x1, 0, 0, // size of payload: 320 |
| 80 | }; |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 81 | static_assert(sizeof(kExpectedHeader) == kHeaderSize, "header size"); |
kwiberg@webrtc.org | 2ade42b | 2014-07-17 08:11:32 +0000 | [diff] [blame] | 82 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 83 | EXPECT_EQ(kHeaderSize + kPayloadSize, webrtc::test::GetFileSize(outfile)); |
kwiberg@webrtc.org | 2ade42b | 2014-07-17 08:11:32 +0000 | [diff] [blame] | 84 | FILE* f = fopen(outfile.c_str(), "rb"); |
| 85 | ASSERT_TRUE(f); |
| 86 | |
| 87 | uint8_t header[kHeaderSize]; |
| 88 | ASSERT_EQ(1u, fread(header, kHeaderSize, 1, f)); |
| 89 | EXPECT_EQ(0, memcmp(kExpectedHeader, header, kHeaderSize)); |
| 90 | |
| 91 | uint8_t payload[kPayloadSize]; |
| 92 | ASSERT_EQ(1u, fread(payload, kPayloadSize, 1, f)); |
| 93 | EXPECT_EQ(0, memcmp(kFakeData, payload, kPayloadSize)); |
| 94 | |
| 95 | EXPECT_EQ(0, fclose(f)); |
| 96 | } |