1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/ref_counted.h"
6 #include "media/cast/audio_receiver/audio_decoder.h"
7 #include "testing/gmock/include/gmock/gmock.h"
12 class AudioDecoderTest
: public ::testing::Test
{
15 virtual ~AudioDecoderTest() {}
17 void Configure(const AudioReceiverConfig
& audio_config
) {
18 audio_decoder_
= new AudioDecoder(audio_config
);
21 scoped_refptr
<AudioDecoder
> audio_decoder_
;
24 TEST_F(AudioDecoderTest
, Pcm16MonoNoResampleOnePacket
) {
25 AudioReceiverConfig audio_config
;
26 audio_config
.rtp_payload_type
= 127;
27 audio_config
.frequency
= 16000;
28 audio_config
.channels
= 1;
29 audio_config
.codec
= kPcm16
;
30 audio_config
.use_external_decoder
= false;
31 Configure(audio_config
);
33 RtpCastHeader rtp_header
;
34 rtp_header
.webrtc
.header
.payloadType
= 127;
35 rtp_header
.webrtc
.header
.sequenceNumber
= 1234;
36 rtp_header
.webrtc
.header
.timestamp
= 0x87654321;
37 rtp_header
.webrtc
.header
.ssrc
= 0x12345678;
38 rtp_header
.webrtc
.header
.paddingLength
= 0;
39 rtp_header
.webrtc
.header
.headerLength
= 12;
40 rtp_header
.webrtc
.type
.Audio
.channel
= 1;
41 rtp_header
.webrtc
.type
.Audio
.isCNG
= false;
43 std::vector
<int16
> payload(640, 0x1234);
45 uint8
* payload_data
= reinterpret_cast<uint8
*>(&payload
[0]);
46 size_t payload_size
= payload
.size() * sizeof(int16
);
48 audio_decoder_
->IncomingParsedRtpPacket(payload_data
,
49 payload_size
, rtp_header
);
51 int number_of_10ms_blocks
= 4;
52 int desired_frequency
= 16000;
53 PcmAudioFrame audio_frame
;
56 EXPECT_TRUE(audio_decoder_
->GetRawAudioFrame(number_of_10ms_blocks
,
61 EXPECT_EQ(1, audio_frame
.channels
);
62 EXPECT_EQ(16000, audio_frame
.frequency
);
63 EXPECT_EQ(640ul, audio_frame
.samples
.size());
64 // First 10 samples per channel are 0 from NetEq.
65 for (size_t i
= 10; i
< audio_frame
.samples
.size(); ++i
) {
66 EXPECT_EQ(0x3412, audio_frame
.samples
[i
]);
70 TEST_F(AudioDecoderTest
, Pcm16StereoNoResampleTwoPackets
) {
71 AudioReceiverConfig audio_config
;
72 audio_config
.rtp_payload_type
= 127;
73 audio_config
.frequency
= 16000;
74 audio_config
.channels
= 2;
75 audio_config
.codec
= kPcm16
;
76 audio_config
.use_external_decoder
= false;
77 Configure(audio_config
);
79 RtpCastHeader rtp_header
;
80 rtp_header
.webrtc
.header
.payloadType
= 127;
81 rtp_header
.webrtc
.header
.sequenceNumber
= 1234;
82 rtp_header
.webrtc
.header
.timestamp
= 0x87654321;
83 rtp_header
.webrtc
.header
.ssrc
= 0x12345678;
84 rtp_header
.webrtc
.header
.paddingLength
= 0;
85 rtp_header
.webrtc
.header
.headerLength
= 12;
87 rtp_header
.webrtc
.type
.Audio
.isCNG
= false;
88 rtp_header
.webrtc
.type
.Audio
.channel
= 2;
90 std::vector
<int16
> payload(640, 0x1234);
92 uint8
* payload_data
= reinterpret_cast<uint8
*>(&payload
[0]);
93 size_t payload_size
= payload
.size() * sizeof(int16
);
95 audio_decoder_
->IncomingParsedRtpPacket(payload_data
,
96 payload_size
, rtp_header
);
98 int number_of_10ms_blocks
= 2;
99 int desired_frequency
= 16000;
100 PcmAudioFrame audio_frame
;
101 uint32 rtp_timestamp
;
103 EXPECT_TRUE(audio_decoder_
->GetRawAudioFrame(number_of_10ms_blocks
,
108 EXPECT_EQ(2, audio_frame
.channels
);
109 EXPECT_EQ(16000, audio_frame
.frequency
);
110 EXPECT_EQ(640ul, audio_frame
.samples
.size());
111 // First 10 samples per channel are 0 from NetEq.
112 for (size_t i
= 10 * audio_config
.channels
; i
< audio_frame
.samples
.size();
114 EXPECT_EQ(0x3412, audio_frame
.samples
[i
]);
117 rtp_header
.webrtc
.header
.sequenceNumber
++;
118 rtp_header
.webrtc
.header
.timestamp
+= (audio_config
.frequency
/ 100) * 2 * 2;
120 audio_decoder_
->IncomingParsedRtpPacket(payload_data
,
121 payload_size
, rtp_header
);
123 EXPECT_TRUE(audio_decoder_
->GetRawAudioFrame(number_of_10ms_blocks
,
127 EXPECT_EQ(2, audio_frame
.channels
);
128 EXPECT_EQ(16000, audio_frame
.frequency
);
129 EXPECT_EQ(640ul, audio_frame
.samples
.size());
130 for (size_t i
= 0; i
< audio_frame
.samples
.size(); ++i
) {
131 EXPECT_NEAR(0x3412, audio_frame
.samples
[i
], 1000);
135 TEST_F(AudioDecoderTest
, Pcm16Resample
) {
136 AudioReceiverConfig audio_config
;
137 audio_config
.rtp_payload_type
= 127;
138 audio_config
.frequency
= 16000;
139 audio_config
.channels
= 2;
140 audio_config
.codec
= kPcm16
;
141 audio_config
.use_external_decoder
= false;
142 Configure(audio_config
);
144 RtpCastHeader rtp_header
;
145 rtp_header
.webrtc
.header
.payloadType
= 127;
146 rtp_header
.webrtc
.header
.sequenceNumber
= 1234;
147 rtp_header
.webrtc
.header
.timestamp
= 0x87654321;
148 rtp_header
.webrtc
.header
.ssrc
= 0x12345678;
149 rtp_header
.webrtc
.header
.paddingLength
= 0;
150 rtp_header
.webrtc
.header
.headerLength
= 12;
152 rtp_header
.webrtc
.type
.Audio
.isCNG
= false;
153 rtp_header
.webrtc
.type
.Audio
.channel
= 2;
155 std::vector
<int16
> payload(640, 0x1234);
157 uint8
* payload_data
= reinterpret_cast<uint8
*>(&payload
[0]);
158 size_t payload_size
= payload
.size() * sizeof(int16
);
160 audio_decoder_
->IncomingParsedRtpPacket(payload_data
,
161 payload_size
, rtp_header
);
163 int number_of_10ms_blocks
= 2;
164 int desired_frequency
= 48000;
165 PcmAudioFrame audio_frame
;
166 uint32 rtp_timestamp
;
168 EXPECT_TRUE(audio_decoder_
->GetRawAudioFrame(number_of_10ms_blocks
,
173 EXPECT_EQ(2, audio_frame
.channels
);
174 EXPECT_EQ(48000, audio_frame
.frequency
);
175 EXPECT_EQ(1920ul, audio_frame
.samples
.size()); // Upsampled to 48 KHz.
177 // Resampling makes the variance worse.
178 for (size_t i
= 100 * audio_config
.channels
; i
< audio_frame
.samples
.size();
180 EXPECT_NEAR(0x3412, audio_frame
.samples
[i
], 400);
181 if (0x3412 == audio_frame
.samples
[i
]) count
++;