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" |
kjellander@webrtc.org | d492f72 | 2011-11-24 07:20:00 +0000 | [diff] [blame] | 15 | |
leozwang@webrtc.org | 0dc8efe | 2012-04-03 15:11:01 +0000 | [diff] [blame] | 16 | class 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öm | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame^] | 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 |
| 37 | TEST_F(MediaFileTest, MAYBE_StartPlayingAudioFileWithoutError) { |
leozwang@webrtc.org | 0dc8efe | 2012-04-03 15:11:01 +0000 | [diff] [blame] | 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() + |
andrew@webrtc.org | 9dc45da | 2012-05-23 15:39:01 +0000 | [diff] [blame] | 41 | "data/voice_engine/audio_tiny48.wav"; |
leozwang@webrtc.org | 0dc8efe | 2012-04-03 15:11:01 +0000 | [diff] [blame] | 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 | |
andrew@webrtc.org | 00c7c43 | 2013-01-02 16:06:39 +0000 | [diff] [blame] | 50 | webrtc::SleepMs(1); |
leozwang@webrtc.org | 0dc8efe | 2012-04-03 15:11:01 +0000 | [diff] [blame] | 51 | |
| 52 | ASSERT_EQ(0, media_file_->StopPlaying()); |
| 53 | } |
kwiberg@webrtc.org | 2ade42b | 2014-07-17 08:11:32 +0000 | [diff] [blame] | 54 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame^] | 55 | #if defined(WEBRTC_IOS) |
| 56 | #define MAYBE_WriteWavFile DISABLED_WriteWavFile |
| 57 | #else |
| 58 | #define MAYBE_WriteWavFile WriteWavFile |
| 59 | #endif |
| 60 | TEST_F(MediaFileTest, MAYBE_WriteWavFile) { |
kwiberg@webrtc.org | 2ade42b | 2014-07-17 08:11:32 +0000 | [diff] [blame] | 61 | // Write file. |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 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 | }; |
kwiberg@webrtc.org | 2ade42b | 2014-07-17 08:11:32 +0000 | [diff] [blame] | 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 | }; |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 91 | static_assert(sizeof(kExpectedHeader) == kHeaderSize, "header size"); |
kwiberg@webrtc.org | 2ade42b | 2014-07-17 08:11:32 +0000 | [diff] [blame] | 92 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 93 | EXPECT_EQ(kHeaderSize + kPayloadSize, webrtc::test::GetFileSize(outfile)); |
kwiberg@webrtc.org | 2ade42b | 2014-07-17 08:11:32 +0000 | [diff] [blame] | 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 | } |