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