audio_frame_unittest.cc (6581B)
1 /* 2 * Copyright 2018 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 "api/audio/audio_frame.h" 12 13 #include <cstdint> 14 #include <cstring> 15 16 #include "api/audio/audio_view.h" 17 #include "api/audio/channel_layout.h" 18 #include "rtc_base/checks.h" 19 #include "test/gtest.h" 20 21 namespace webrtc { 22 23 namespace { 24 25 bool AllSamplesAre(int16_t sample, InterleavedView<const int16_t> samples) { 26 for (const auto s : samples) { 27 if (s != sample) { 28 return false; 29 } 30 } 31 return true; 32 } 33 34 bool AllSamplesAre(int16_t sample, const AudioFrame& frame) { 35 return AllSamplesAre(sample, frame.data_view()); 36 } 37 38 // Checks the values of samples in the AudioFrame buffer, regardless of whether 39 // they're valid or not, and disregard the `muted()` state of the frame. 40 // I.e. use `max_16bit_samples()` instead of `data_view().size()` 41 bool AllBufferSamplesAre(int16_t sample, const AudioFrame& frame) { 42 auto view = frame.data_view(); 43 RTC_DCHECK(!view.empty()); 44 const int16_t* data = &view.data()[0]; 45 for (size_t i = 0; i < frame.max_16bit_samples(); ++i) { 46 if (data[i] != sample) { 47 return false; 48 } 49 } 50 return true; 51 } 52 53 constexpr uint32_t kTimestamp = 27; 54 constexpr int kSampleRateHz = 16000; 55 constexpr size_t kNumChannelsMono = 1; 56 constexpr size_t kNumChannelsStereo = 2; 57 constexpr size_t kNumChannels5_1 = 6; 58 constexpr size_t kSamplesPerChannel = kSampleRateHz / 100; 59 60 } // namespace 61 62 TEST(AudioFrameTest, FrameStartsZeroedAndMuted) { 63 AudioFrame frame; 64 EXPECT_TRUE(frame.muted()); 65 EXPECT_TRUE(frame.data_view().empty()); 66 EXPECT_TRUE(AllSamplesAre(0, frame)); 67 } 68 69 // TODO: b/335805780 - Delete test when `mutable_data()` returns ArrayView. 70 TEST(AudioFrameTest, UnmutedFrameIsInitiallyZeroedLegacy) { 71 AudioFrame frame(kSampleRateHz, kNumChannelsMono, CHANNEL_LAYOUT_NONE); 72 frame.mutable_data(); 73 EXPECT_FALSE(frame.muted()); 74 EXPECT_TRUE(AllSamplesAre(0, frame)); 75 EXPECT_TRUE(AllBufferSamplesAre(0, frame)); 76 } 77 78 TEST(AudioFrameTest, UnmutedFrameIsInitiallyZeroed) { 79 AudioFrame frame; 80 auto data = frame.mutable_data(kSamplesPerChannel, kNumChannelsMono); 81 EXPECT_FALSE(frame.muted()); 82 EXPECT_TRUE(IsMono(data)); 83 EXPECT_EQ(frame.data_view().size(), kSamplesPerChannel); 84 EXPECT_EQ(SamplesPerChannel(data), kSamplesPerChannel); 85 EXPECT_TRUE(AllSamplesAre(0, frame)); 86 } 87 88 TEST(AudioFrameTest, MutedFrameBufferIsZeroed) { 89 AudioFrame frame; 90 int16_t* frame_data = 91 frame.mutable_data(kSamplesPerChannel, kNumChannelsMono).begin(); 92 EXPECT_FALSE(frame.muted()); 93 // Fill the reserved buffer with non-zero data. 94 for (size_t i = 0; i < frame.max_16bit_samples(); i++) { 95 frame_data[i] = 17; 96 } 97 ASSERT_TRUE(AllSamplesAre(17, frame)); 98 ASSERT_TRUE(AllBufferSamplesAre(17, frame)); 99 frame.Mute(); 100 EXPECT_TRUE(frame.muted()); 101 EXPECT_TRUE(AllSamplesAre(0, frame)); 102 ASSERT_TRUE(AllBufferSamplesAre(0, frame)); 103 } 104 105 TEST(AudioFrameTest, UpdateFrameMono) { 106 AudioFrame frame; 107 int16_t samples[kNumChannelsMono * kSamplesPerChannel] = {17}; 108 frame.UpdateFrame(kTimestamp, samples, kSamplesPerChannel, kSampleRateHz, 109 AudioFrame::kPLC, AudioFrame::kVadActive, kNumChannelsMono); 110 111 EXPECT_EQ(kTimestamp, frame.timestamp_); 112 EXPECT_EQ(kSamplesPerChannel, frame.samples_per_channel()); 113 EXPECT_EQ(kSampleRateHz, frame.sample_rate_hz()); 114 EXPECT_EQ(AudioFrame::kPLC, frame.speech_type_); 115 EXPECT_EQ(AudioFrame::kVadActive, frame.vad_activity_); 116 EXPECT_EQ(kNumChannelsMono, frame.num_channels()); 117 EXPECT_EQ(CHANNEL_LAYOUT_MONO, frame.channel_layout()); 118 119 EXPECT_FALSE(frame.muted()); 120 EXPECT_EQ(0, memcmp(samples, frame.data(), sizeof(samples))); 121 122 frame.UpdateFrame(kTimestamp, nullptr /* data*/, kSamplesPerChannel, 123 kSampleRateHz, AudioFrame::kPLC, AudioFrame::kVadActive, 124 kNumChannelsMono); 125 EXPECT_TRUE(frame.muted()); 126 EXPECT_TRUE(AllSamplesAre(0, frame)); 127 } 128 129 TEST(AudioFrameTest, UpdateFrameMultiChannel) { 130 AudioFrame frame; 131 frame.UpdateFrame(kTimestamp, nullptr /* data */, kSamplesPerChannel, 132 kSampleRateHz, AudioFrame::kPLC, AudioFrame::kVadActive, 133 kNumChannelsStereo); 134 EXPECT_EQ(kSamplesPerChannel, frame.samples_per_channel()); 135 EXPECT_EQ(kNumChannelsStereo, frame.num_channels()); 136 EXPECT_EQ(CHANNEL_LAYOUT_STEREO, frame.channel_layout()); 137 EXPECT_TRUE(frame.muted()); 138 139 // Initialize the frame with valid `kNumChannels5_1` data to make sure we 140 // get an unmuted frame with valid samples. 141 int16_t samples[kSamplesPerChannel * kNumChannels5_1] = {17}; 142 frame.UpdateFrame(kTimestamp, samples /* data */, kSamplesPerChannel, 143 kSampleRateHz, AudioFrame::kPLC, AudioFrame::kVadActive, 144 kNumChannels5_1); 145 EXPECT_FALSE(frame.muted()); 146 EXPECT_EQ(kSamplesPerChannel, frame.samples_per_channel()); 147 EXPECT_EQ(kSamplesPerChannel * kNumChannels5_1, frame.data_view().size()); 148 EXPECT_EQ(kNumChannels5_1, frame.num_channels()); 149 EXPECT_EQ(CHANNEL_LAYOUT_5_1, frame.channel_layout()); 150 } 151 152 TEST(AudioFrameTest, CopyFrom) { 153 AudioFrame frame1; 154 AudioFrame frame2; 155 156 int16_t samples[kNumChannelsMono * kSamplesPerChannel] = {17}; 157 frame2.UpdateFrame(kTimestamp, samples, kSamplesPerChannel, kSampleRateHz, 158 AudioFrame::kPLC, AudioFrame::kVadActive, 159 kNumChannelsMono); 160 frame1.CopyFrom(frame2); 161 162 EXPECT_EQ(frame2.timestamp_, frame1.timestamp_); 163 EXPECT_EQ(frame2.samples_per_channel_, frame1.samples_per_channel_); 164 EXPECT_EQ(frame2.sample_rate_hz_, frame1.sample_rate_hz_); 165 EXPECT_EQ(frame2.speech_type_, frame1.speech_type_); 166 EXPECT_EQ(frame2.vad_activity_, frame1.vad_activity_); 167 EXPECT_EQ(frame2.num_channels_, frame1.num_channels_); 168 169 EXPECT_EQ(frame2.data_view().size(), frame1.data_view().size()); 170 EXPECT_EQ(frame2.muted(), frame1.muted()); 171 EXPECT_EQ(0, memcmp(frame2.data(), frame1.data(), sizeof(samples))); 172 173 frame2.UpdateFrame(kTimestamp, nullptr /* data */, kSamplesPerChannel, 174 kSampleRateHz, AudioFrame::kPLC, AudioFrame::kVadActive, 175 kNumChannelsMono); 176 frame1.CopyFrom(frame2); 177 178 EXPECT_EQ(frame2.muted(), frame1.muted()); 179 EXPECT_EQ(0, memcmp(frame2.data(), frame1.data(), sizeof(samples))); 180 } 181 182 } // namespace webrtc