blob: 2a41e76f2c591cbc1201bfae656c5e917cf47f59 [file] [log] [blame]
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001/*
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 <string.h> // memcmp
12
13#include "api/audio/audio_frame.h"
14#include "test/gtest.h"
15
16namespace webrtc {
17
18namespace {
19
20bool AllSamplesAre(int16_t sample, const AudioFrame& frame) {
21 const int16_t* frame_data = frame.data();
22 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
23 if (frame_data[i] != sample) {
24 return false;
25 }
26 }
27 return true;
28}
29
30constexpr uint32_t kTimestamp = 27;
31constexpr int kSampleRateHz = 16000;
32constexpr size_t kNumChannels = 1;
33constexpr size_t kSamplesPerChannel = kSampleRateHz / 100;
34
35} // namespace
36
37TEST(AudioFrameTest, FrameStartsMuted) {
38 AudioFrame frame;
39 EXPECT_TRUE(frame.muted());
40 EXPECT_TRUE(AllSamplesAre(0, frame));
41}
42
43TEST(AudioFrameTest, UnmutedFrameIsInitiallyZeroed) {
44 AudioFrame frame;
45 frame.mutable_data();
46 EXPECT_FALSE(frame.muted());
47 EXPECT_TRUE(AllSamplesAre(0, frame));
48}
49
50TEST(AudioFrameTest, MutedFrameBufferIsZeroed) {
51 AudioFrame frame;
52 int16_t* frame_data = frame.mutable_data();
53 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) {
54 frame_data[i] = 17;
55 }
56 ASSERT_TRUE(AllSamplesAre(17, frame));
57 frame.Mute();
58 EXPECT_TRUE(frame.muted());
59 EXPECT_TRUE(AllSamplesAre(0, frame));
60}
61
62TEST(AudioFrameTest, UpdateFrame) {
63 AudioFrame frame;
64 int16_t samples[kNumChannels * kSamplesPerChannel] = {17};
65 frame.UpdateFrame(kTimestamp, samples, kSamplesPerChannel, kSampleRateHz,
66 AudioFrame::kPLC, AudioFrame::kVadActive, kNumChannels);
67
68 EXPECT_EQ(kTimestamp, frame.timestamp_);
69 EXPECT_EQ(kSamplesPerChannel, frame.samples_per_channel_);
70 EXPECT_EQ(kSampleRateHz, frame.sample_rate_hz_);
71 EXPECT_EQ(AudioFrame::kPLC, frame.speech_type_);
72 EXPECT_EQ(AudioFrame::kVadActive, frame.vad_activity_);
73 EXPECT_EQ(kNumChannels, frame.num_channels_);
74
75 EXPECT_FALSE(frame.muted());
76 EXPECT_EQ(0, memcmp(samples, frame.data(), sizeof(samples)));
77
78 frame.UpdateFrame(kTimestamp, nullptr /* data*/, kSamplesPerChannel,
79 kSampleRateHz, AudioFrame::kPLC, AudioFrame::kVadActive,
80 kNumChannels);
81 EXPECT_TRUE(frame.muted());
82 EXPECT_TRUE(AllSamplesAre(0, frame));
83}
84
85TEST(AudioFrameTest, CopyFrom) {
86 AudioFrame frame1;
87 AudioFrame frame2;
88
89 int16_t samples[kNumChannels * kSamplesPerChannel] = {17};
90 frame2.UpdateFrame(kTimestamp, samples, kSamplesPerChannel,
91 kSampleRateHz, AudioFrame::kPLC, AudioFrame::kVadActive,
92 kNumChannels);
93 frame1.CopyFrom(frame2);
94
95 EXPECT_EQ(frame2.timestamp_, frame1.timestamp_);
96 EXPECT_EQ(frame2.samples_per_channel_, frame1.samples_per_channel_);
97 EXPECT_EQ(frame2.sample_rate_hz_, frame1.sample_rate_hz_);
98 EXPECT_EQ(frame2.speech_type_, frame1.speech_type_);
99 EXPECT_EQ(frame2.vad_activity_, frame1.vad_activity_);
100 EXPECT_EQ(frame2.num_channels_, frame1.num_channels_);
101
102 EXPECT_EQ(frame2.muted(), frame1.muted());
103 EXPECT_EQ(0, memcmp(frame2.data(), frame1.data(), sizeof(samples)));
104
105 frame2.UpdateFrame(kTimestamp, nullptr /* data */, kSamplesPerChannel,
106 kSampleRateHz, AudioFrame::kPLC, AudioFrame::kVadActive,
107 kNumChannels);
108 frame1.CopyFrom(frame2);
109
110 EXPECT_EQ(frame2.muted(), frame1.muted());
111 EXPECT_EQ(0, memcmp(frame2.data(), frame1.data(), sizeof(samples)));
112}
113
114} // namespace webrtc