blob: def4d1ae7e74042f7ae78cfabef9992e46307e35 [file] [log] [blame]
Stefan Holmer8bffba72015-09-23 15:53:52 +02001/*
2 * Copyright (c) 2015 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
kwiberg1c07c702017-03-27 07:15:49 -070011#include <map>
Fredrik Solenbergea073732015-12-01 11:26:34 +010012#include <string>
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +010013#include <utility>
stefan3313ec92016-01-21 06:32:43 -080014#include <vector>
Fredrik Solenbergea073732015-12-01 11:26:34 +010015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/test/mock_audio_mixer.h"
Benjamin Wright78410ad2018-10-25 09:52:57 -070017#include "api/test/mock_frame_decryptor.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "audio/audio_receive_stream.h"
19#include "audio/conversion.h"
Fredrik Solenberga8b7c7f2018-01-17 11:18:31 +010020#include "audio/mock_voe_channel_proxy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "call/rtp_stream_receiver_controller.h"
22#include "logging/rtc_event_log/mock/mock_rtc_event_log.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010023#include "modules/audio_device/include/mock_audio_device.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/pacing/packet_router.h"
26#include "modules/rtp_rtcp/source/byte_io.h"
Bjorn Terelius5c2f1f02019-01-16 17:45:05 +010027#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "test/gtest.h"
29#include "test/mock_audio_decoder_factory.h"
Niels Möllerae4237e2018-10-05 11:28:38 +020030#include "test/mock_transport.h"
Stefan Holmer8bffba72015-09-23 15:53:52 +020031
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010032namespace webrtc {
33namespace test {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020034namespace {
35
solenberg3a941542015-11-16 07:34:50 -080036using testing::_;
solenberg217fb662016-06-17 08:30:54 -070037using testing::FloatEq;
solenberg3a941542015-11-16 07:34:50 -080038using testing::Return;
39
solenberg566ef242015-11-06 15:34:49 -080040AudioDecodingCallStats MakeAudioDecodeStatsForTest() {
41 AudioDecodingCallStats audio_decode_stats;
42 audio_decode_stats.calls_to_silence_generator = 234;
43 audio_decode_stats.calls_to_neteq = 567;
44 audio_decode_stats.decoded_normal = 890;
45 audio_decode_stats.decoded_plc = 123;
46 audio_decode_stats.decoded_cng = 456;
47 audio_decode_stats.decoded_plc_cng = 789;
henrik.lundin63489782016-09-20 01:47:12 -070048 audio_decode_stats.decoded_muted_output = 987;
solenberg566ef242015-11-06 15:34:49 -080049 return audio_decode_stats;
50}
51
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010052const uint32_t kRemoteSsrc = 1234;
53const uint32_t kLocalSsrc = 5678;
Stefan Holmer3842c5c2016-01-12 13:55:00 +010054const size_t kOneByteExtensionHeaderLength = 4;
55const size_t kOneByteExtensionLength = 4;
solenberg7add0582015-11-20 09:59:34 -080056const int kAudioLevelId = 3;
Stefan Holmer3842c5c2016-01-12 13:55:00 +010057const int kTransportSequenceNumberId = 4;
solenberg566ef242015-11-06 15:34:49 -080058const int kJitterBufferDelay = -7;
59const int kPlayoutBufferDelay = 302;
60const unsigned int kSpeechOutputLevel = 99;
zsteine76bd3a2017-07-14 12:17:49 -070061const double kTotalOutputEnergy = 0.25;
62const double kTotalOutputDuration = 0.5;
63
Niels Möller530ead42018-10-04 14:28:39 +020064const CallReceiveStatistics kCallStats = {345, 678, 901, 234,
65 -12, 567, 890, 123};
Benjamin Wright17b050f2019-03-13 17:35:46 -070066const std::pair<int, SdpAudioFormat> kReceiveCodec = {
67 123,
68 {"codec_name_recv", 96000, 0}};
Gustaf Ullbergb0a02072017-10-02 12:00:34 +020069const NetworkStatistics kNetworkStats = {
Chen Xing0acffb52019-01-15 15:46:29 +010070 123, 456, false, 789012, 3456, 123, 456, 789, 0, {}, 789,
71 12, 345, 678, 901, 0, -1, -1, -1, -1, -1, 0};
solenberg566ef242015-11-06 15:34:49 -080072const AudioDecodingCallStats kAudioDecodeStats = MakeAudioDecodeStatsForTest();
73
74struct ConfigHelper {
Yves Gerey665174f2018-06-19 15:03:05 +020075 ConfigHelper() : ConfigHelper(new rtc::RefCountedObject<MockAudioMixer>()) {}
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010076
77 explicit ConfigHelper(rtc::scoped_refptr<MockAudioMixer> audio_mixer)
78 : audio_mixer_(audio_mixer) {
solenberg13725082015-11-25 08:16:52 -080079 using testing::Invoke;
80
solenberg566ef242015-11-06 15:34:49 -080081 AudioState::Config config;
aleloi04c07222016-11-22 06:42:53 -080082 config.audio_mixer = audio_mixer_;
peaha9cc40b2017-06-29 08:32:09 -070083 config.audio_processing = new rtc::RefCountedObject<MockAudioProcessing>();
Fredrik Solenberg2a877972017-12-15 16:42:15 +010084 config.audio_device_module =
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010085 new rtc::RefCountedObject<testing::NiceMock<MockAudioDeviceModule>>();
solenberg566ef242015-11-06 15:34:49 -080086 audio_state_ = AudioState::Create(config);
solenberg7add0582015-11-20 09:59:34 -080087
Niels Möller349ade32018-11-16 09:50:42 +010088 channel_receive_ = new testing::StrictMock<MockChannelReceive>();
89 EXPECT_CALL(*channel_receive_, SetLocalSSRC(kLocalSsrc)).Times(1);
90 EXPECT_CALL(*channel_receive_, SetNACKStatus(true, 15)).Times(1);
91 EXPECT_CALL(*channel_receive_,
Yves Gerey665174f2018-06-19 15:03:05 +020092 RegisterReceiverCongestionControlObjects(&packet_router_))
93 .Times(1);
Niels Möller349ade32018-11-16 09:50:42 +010094 EXPECT_CALL(*channel_receive_, ResetReceiverCongestionControlObjects())
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010095 .Times(1);
Niels Möller349ade32018-11-16 09:50:42 +010096 EXPECT_CALL(*channel_receive_, SetAssociatedSendChannel(nullptr)).Times(1);
97 EXPECT_CALL(*channel_receive_, SetReceiveCodecs(_))
Yves Gerey665174f2018-06-19 15:03:05 +020098 .WillRepeatedly(Invoke([](const std::map<int, SdpAudioFormat>& codecs) {
99 EXPECT_THAT(codecs, testing::IsEmpty());
100 }));
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100101
solenberg566ef242015-11-06 15:34:49 -0800102 stream_config_.rtp.local_ssrc = kLocalSsrc;
103 stream_config_.rtp.remote_ssrc = kRemoteSsrc;
solenberg8189b022016-06-14 12:13:00 -0700104 stream_config_.rtp.nack.rtp_history_ms = 300;
solenberg7add0582015-11-20 09:59:34 -0800105 stream_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700106 RtpExtension(RtpExtension::kAudioLevelUri, kAudioLevelId));
stefan3313ec92016-01-21 06:32:43 -0800107 stream_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -0700108 RtpExtension::kTransportSequenceNumberUri, kTransportSequenceNumberId));
Niels Möllerae4237e2018-10-05 11:28:38 +0200109 stream_config_.rtcp_send_transport = &rtcp_send_transport_;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100110 stream_config_.decoder_factory =
111 new rtc::RefCountedObject<MockAudioDecoderFactory>;
solenberg13725082015-11-25 08:16:52 -0800112 }
solenberg566ef242015-11-06 15:34:49 -0800113
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100114 std::unique_ptr<internal::AudioReceiveStream> CreateAudioReceiveStream() {
115 return std::unique_ptr<internal::AudioReceiveStream>(
116 new internal::AudioReceiveStream(
Sebastian Jansson977b3352019-03-04 17:43:34 +0100117 Clock::GetRealTimeClock(), &rtp_stream_receiver_controller_,
118 &packet_router_, stream_config_, audio_state_, &event_log_,
Niels Möller349ade32018-11-16 09:50:42 +0100119 std::unique_ptr<voe::ChannelReceiveInterface>(channel_receive_)));
nisse0f15f922017-06-21 01:05:22 -0700120 }
solenberg566ef242015-11-06 15:34:49 -0800121
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100122 AudioReceiveStream::Config& config() { return stream_config_; }
123 rtc::scoped_refptr<MockAudioMixer> audio_mixer() { return audio_mixer_; }
Niels Möller349ade32018-11-16 09:50:42 +0100124 MockChannelReceive* channel_receive() { return channel_receive_; }
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100125
solenberg566ef242015-11-06 15:34:49 -0800126 void SetupMockForGetStats() {
solenberg566ef242015-11-06 15:34:49 -0800127 using testing::DoAll;
solenbergbd9a77f2017-02-06 12:53:57 -0800128 using testing::SetArgPointee;
solenberg13725082015-11-25 08:16:52 -0800129
Niels Möller349ade32018-11-16 09:50:42 +0100130 ASSERT_TRUE(channel_receive_);
131 EXPECT_CALL(*channel_receive_, GetRTCPStatistics())
solenberg358057b2015-11-27 10:46:42 -0800132 .WillOnce(Return(kCallStats));
Niels Möller349ade32018-11-16 09:50:42 +0100133 EXPECT_CALL(*channel_receive_, GetDelayEstimate())
solenberg358057b2015-11-27 10:46:42 -0800134 .WillOnce(Return(kJitterBufferDelay + kPlayoutBufferDelay));
Niels Möller349ade32018-11-16 09:50:42 +0100135 EXPECT_CALL(*channel_receive_, GetSpeechOutputLevelFullRange())
solenberg358057b2015-11-27 10:46:42 -0800136 .WillOnce(Return(kSpeechOutputLevel));
Niels Möller349ade32018-11-16 09:50:42 +0100137 EXPECT_CALL(*channel_receive_, GetTotalOutputEnergy())
zsteine76bd3a2017-07-14 12:17:49 -0700138 .WillOnce(Return(kTotalOutputEnergy));
Niels Möller349ade32018-11-16 09:50:42 +0100139 EXPECT_CALL(*channel_receive_, GetTotalOutputDuration())
zsteine76bd3a2017-07-14 12:17:49 -0700140 .WillOnce(Return(kTotalOutputDuration));
Niels Möller349ade32018-11-16 09:50:42 +0100141 EXPECT_CALL(*channel_receive_, GetNetworkStatistics())
solenberg358057b2015-11-27 10:46:42 -0800142 .WillOnce(Return(kNetworkStats));
Niels Möller349ade32018-11-16 09:50:42 +0100143 EXPECT_CALL(*channel_receive_, GetDecodingCallStatistics())
solenberg358057b2015-11-27 10:46:42 -0800144 .WillOnce(Return(kAudioDecodeStats));
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100145 EXPECT_CALL(*channel_receive_, GetReceiveCodec())
146 .WillOnce(Return(kReceiveCodec));
solenberg566ef242015-11-06 15:34:49 -0800147 }
148
149 private:
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100150 PacketRouter packet_router_;
ivoc14d5dbe2016-07-04 07:06:55 -0700151 MockRtcEventLog event_log_;
solenberg566ef242015-11-06 15:34:49 -0800152 rtc::scoped_refptr<AudioState> audio_state_;
aleloi04c07222016-11-22 06:42:53 -0800153 rtc::scoped_refptr<MockAudioMixer> audio_mixer_;
solenberg566ef242015-11-06 15:34:49 -0800154 AudioReceiveStream::Config stream_config_;
Niels Möller349ade32018-11-16 09:50:42 +0100155 testing::StrictMock<MockChannelReceive>* channel_receive_ = nullptr;
nisse0f15f922017-06-21 01:05:22 -0700156 RtpStreamReceiverController rtp_stream_receiver_controller_;
Niels Möllerae4237e2018-10-05 11:28:38 +0200157 MockTransport rtcp_send_transport_;
solenberg566ef242015-11-06 15:34:49 -0800158};
Stefan Holmer8bffba72015-09-23 15:53:52 +0200159
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100160void BuildOneByteExtension(std::vector<uint8_t>::iterator it,
161 int id,
162 uint32_t extension_value,
163 size_t value_length) {
Stefan Holmer8bffba72015-09-23 15:53:52 +0200164 const uint16_t kRtpOneByteHeaderExtensionId = 0xBEDE;
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100165 ByteWriter<uint16_t>::WriteBigEndian(&(*it), kRtpOneByteHeaderExtensionId);
166 it += 2;
Stefan Holmer8bffba72015-09-23 15:53:52 +0200167
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100168 ByteWriter<uint16_t>::WriteBigEndian(&(*it), kOneByteExtensionLength / 4);
169 it += 2;
170 const size_t kExtensionDataLength = kOneByteExtensionLength - 1;
171 uint32_t shifted_value = extension_value
172 << (8 * (kExtensionDataLength - value_length));
Peter Boström02083222016-06-14 12:52:54 +0200173 *it = (id << 4) + (static_cast<uint8_t>(value_length) - 1);
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100174 ++it;
175 ByteWriter<uint32_t, kExtensionDataLength>::WriteBigEndian(&(*it),
176 shifted_value);
Stefan Holmer8bffba72015-09-23 15:53:52 +0200177}
178
mflodman3d7db262016-04-29 00:57:13 -0700179const std::vector<uint8_t> CreateRtpHeaderWithOneByteExtension(
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100180 int extension_id,
181 uint32_t extension_value,
182 size_t value_length) {
183 std::vector<uint8_t> header;
184 header.resize(webrtc::kRtpHeaderSize + kOneByteExtensionHeaderLength +
185 kOneByteExtensionLength);
Stefan Holmer8bffba72015-09-23 15:53:52 +0200186 header[0] = 0x80; // Version 2.
187 header[0] |= 0x10; // Set extension bit.
188 header[1] = 100; // Payload type.
189 header[1] |= 0x80; // Marker bit is set.
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100190 ByteWriter<uint16_t>::WriteBigEndian(&header[2], 0x1234); // Sequence number.
191 ByteWriter<uint32_t>::WriteBigEndian(&header[4], 0x5678); // Timestamp.
192 ByteWriter<uint32_t>::WriteBigEndian(&header[8], 0x4321); // SSRC.
Stefan Holmer8bffba72015-09-23 15:53:52 +0200193
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100194 BuildOneByteExtension(header.begin() + webrtc::kRtpHeaderSize, extension_id,
195 extension_value, value_length);
196 return header;
Stefan Holmer8bffba72015-09-23 15:53:52 +0200197}
mflodman3d7db262016-04-29 00:57:13 -0700198
199const std::vector<uint8_t> CreateRtcpSenderReport() {
200 std::vector<uint8_t> packet;
201 const size_t kRtcpSrLength = 28; // In bytes.
202 packet.resize(kRtcpSrLength);
203 packet[0] = 0x80; // Version 2.
204 packet[1] = 0xc8; // PT = 200, SR.
205 // Length in number of 32-bit words - 1.
206 ByteWriter<uint16_t>::WriteBigEndian(&packet[2], 6);
207 ByteWriter<uint32_t>::WriteBigEndian(&packet[4], kLocalSsrc);
208 return packet;
209}
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200210} // namespace
211
solenberg85a04962015-10-27 03:35:21 -0700212TEST(AudioReceiveStreamTest, ConfigToString) {
solenberg85a04962015-10-27 03:35:21 -0700213 AudioReceiveStream::Config config;
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100214 config.rtp.remote_ssrc = kRemoteSsrc;
215 config.rtp.local_ssrc = kLocalSsrc;
stefanb521aa72016-11-01 03:17:16 -0700216 config.rtp.extensions.push_back(
217 RtpExtension(RtpExtension::kAudioLevelUri, kAudioLevelId));
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100218 EXPECT_EQ(
stefanb521aa72016-11-01 03:17:16 -0700219 "{rtp: {remote_ssrc: 1234, local_ssrc: 5678, transport_cc: off, nack: "
220 "{rtp_history_ms: 0}, extensions: [{uri: "
221 "urn:ietf:params:rtp-hdrext:ssrc-audio-level, id: 3}]}, "
Niels Möller7d76a312018-10-26 12:57:07 +0200222 "rtcp_send_transport: null, media_transport: null}",
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100223 config.ToString());
solenberg85a04962015-10-27 03:35:21 -0700224}
225
226TEST(AudioReceiveStreamTest, ConstructDestruct) {
solenberg566ef242015-11-06 15:34:49 -0800227 ConfigHelper helper;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100228 auto recv_stream = helper.CreateAudioReceiveStream();
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100229}
230
mflodman3d7db262016-04-29 00:57:13 -0700231TEST(AudioReceiveStreamTest, ReceiveRtpPacket) {
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100232 ConfigHelper helper;
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100233 helper.config().rtp.transport_cc = true;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100234 auto recv_stream = helper.CreateAudioReceiveStream();
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100235 const int kTransportSequenceNumberValue = 1234;
236 std::vector<uint8_t> rtp_packet = CreateRtpHeaderWithOneByteExtension(
237 kTransportSequenceNumberId, kTransportSequenceNumberValue, 2);
Niels Möller70082872018-08-07 11:03:12 +0200238 constexpr int64_t packet_time_us = 5678000;
nisse657bab22017-02-21 06:28:10 -0800239
240 RtpPacketReceived parsed_packet;
241 ASSERT_TRUE(parsed_packet.Parse(&rtp_packet[0], rtp_packet.size()));
Niels Möller70082872018-08-07 11:03:12 +0200242 parsed_packet.set_arrival_time_ms((packet_time_us + 500) / 1000);
nisse657bab22017-02-21 06:28:10 -0800243
Niels Möller349ade32018-11-16 09:50:42 +0100244 EXPECT_CALL(*helper.channel_receive(),
nisse657bab22017-02-21 06:28:10 -0800245 OnRtpPacket(testing::Ref(parsed_packet)));
246
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100247 recv_stream->OnRtpPacket(parsed_packet);
Stefan Holmer8bffba72015-09-23 15:53:52 +0200248}
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200249
mflodman3d7db262016-04-29 00:57:13 -0700250TEST(AudioReceiveStreamTest, ReceiveRtcpPacket) {
251 ConfigHelper helper;
252 helper.config().rtp.transport_cc = true;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100253 auto recv_stream = helper.CreateAudioReceiveStream();
mflodman3d7db262016-04-29 00:57:13 -0700254 std::vector<uint8_t> rtcp_packet = CreateRtcpSenderReport();
Niels Möller349ade32018-11-16 09:50:42 +0100255 EXPECT_CALL(*helper.channel_receive(),
mflodman3d7db262016-04-29 00:57:13 -0700256 ReceivedRTCPPacket(&rtcp_packet[0], rtcp_packet.size()))
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100257 .WillOnce(Return());
258 recv_stream->DeliverRtcp(&rtcp_packet[0], rtcp_packet.size());
mflodman3d7db262016-04-29 00:57:13 -0700259}
260
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200261TEST(AudioReceiveStreamTest, GetStats) {
solenberg566ef242015-11-06 15:34:49 -0800262 ConfigHelper helper;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100263 auto recv_stream = helper.CreateAudioReceiveStream();
solenberg566ef242015-11-06 15:34:49 -0800264 helper.SetupMockForGetStats();
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100265 AudioReceiveStream::Stats stats = recv_stream->GetStats();
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100266 EXPECT_EQ(kRemoteSsrc, stats.remote_ssrc);
267 EXPECT_EQ(static_cast<int64_t>(kCallStats.bytesReceived), stats.bytes_rcvd);
268 EXPECT_EQ(static_cast<uint32_t>(kCallStats.packetsReceived),
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200269 stats.packets_rcvd);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100270 EXPECT_EQ(kCallStats.cumulativeLost, stats.packets_lost);
271 EXPECT_EQ(Q8ToFloat(kCallStats.fractionLost), stats.fraction_lost);
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100272 EXPECT_EQ(kReceiveCodec.second.name, stats.codec_name);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100273 EXPECT_EQ(kCallStats.extendedMax, stats.ext_seqnum);
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100274 EXPECT_EQ(
275 kCallStats.jitterSamples / (kReceiveCodec.second.clockrate_hz / 1000),
276 stats.jitter_ms);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100277 EXPECT_EQ(kNetworkStats.currentBufferSize, stats.jitter_buffer_ms);
278 EXPECT_EQ(kNetworkStats.preferredBufferSize,
279 stats.jitter_buffer_preferred_ms);
280 EXPECT_EQ(static_cast<uint32_t>(kJitterBufferDelay + kPlayoutBufferDelay),
281 stats.delay_estimate_ms);
282 EXPECT_EQ(static_cast<int32_t>(kSpeechOutputLevel), stats.audio_level);
zsteine76bd3a2017-07-14 12:17:49 -0700283 EXPECT_EQ(kTotalOutputEnergy, stats.total_output_energy);
Steve Anton2dbc69f2017-08-24 17:15:13 -0700284 EXPECT_EQ(kNetworkStats.totalSamplesReceived, stats.total_samples_received);
zsteine76bd3a2017-07-14 12:17:49 -0700285 EXPECT_EQ(kTotalOutputDuration, stats.total_output_duration);
Steve Anton2dbc69f2017-08-24 17:15:13 -0700286 EXPECT_EQ(kNetworkStats.concealedSamples, stats.concealed_samples);
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200287 EXPECT_EQ(kNetworkStats.concealmentEvents, stats.concealment_events);
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200288 EXPECT_EQ(static_cast<double>(kNetworkStats.jitterBufferDelayMs) /
289 static_cast<double>(rtc::kNumMillisecsPerSec),
290 stats.jitter_buffer_delay_seconds);
Chen Xing0acffb52019-01-15 15:46:29 +0100291 EXPECT_EQ(kNetworkStats.jitterBufferEmittedCount,
292 stats.jitter_buffer_emitted_count);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100293 EXPECT_EQ(Q14ToFloat(kNetworkStats.currentExpandRate), stats.expand_rate);
294 EXPECT_EQ(Q14ToFloat(kNetworkStats.currentSpeechExpandRate),
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200295 stats.speech_expand_rate);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100296 EXPECT_EQ(Q14ToFloat(kNetworkStats.currentSecondaryDecodedRate),
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200297 stats.secondary_decoded_rate);
minyue-webrtc0e320ec2017-08-28 13:51:27 +0200298 EXPECT_EQ(Q14ToFloat(kNetworkStats.currentSecondaryDiscardedRate),
299 stats.secondary_discarded_rate);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100300 EXPECT_EQ(Q14ToFloat(kNetworkStats.currentAccelerateRate),
301 stats.accelerate_rate);
302 EXPECT_EQ(Q14ToFloat(kNetworkStats.currentPreemptiveRate),
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200303 stats.preemptive_expand_rate);
solenberg566ef242015-11-06 15:34:49 -0800304 EXPECT_EQ(kAudioDecodeStats.calls_to_silence_generator,
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200305 stats.decoding_calls_to_silence_generator);
solenberg566ef242015-11-06 15:34:49 -0800306 EXPECT_EQ(kAudioDecodeStats.calls_to_neteq, stats.decoding_calls_to_neteq);
307 EXPECT_EQ(kAudioDecodeStats.decoded_normal, stats.decoding_normal);
308 EXPECT_EQ(kAudioDecodeStats.decoded_plc, stats.decoding_plc);
309 EXPECT_EQ(kAudioDecodeStats.decoded_cng, stats.decoding_cng);
310 EXPECT_EQ(kAudioDecodeStats.decoded_plc_cng, stats.decoding_plc_cng);
henrik.lundin63489782016-09-20 01:47:12 -0700311 EXPECT_EQ(kAudioDecodeStats.decoded_muted_output,
312 stats.decoding_muted_output);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100313 EXPECT_EQ(kCallStats.capture_start_ntp_time_ms_,
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200314 stats.capture_start_ntp_time_ms);
315}
solenberg217fb662016-06-17 08:30:54 -0700316
317TEST(AudioReceiveStreamTest, SetGain) {
318 ConfigHelper helper;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100319 auto recv_stream = helper.CreateAudioReceiveStream();
Niels Möller349ade32018-11-16 09:50:42 +0100320 EXPECT_CALL(*helper.channel_receive(),
Yves Gerey665174f2018-06-19 15:03:05 +0200321 SetChannelOutputVolumeScaling(FloatEq(0.765f)));
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100322 recv_stream->SetGain(0.765f);
solenberg217fb662016-06-17 08:30:54 -0700323}
aleloi04c07222016-11-22 06:42:53 -0800324
Fredrik Solenbergd5247512017-12-18 22:41:03 +0100325TEST(AudioReceiveStreamTest, StreamsShouldBeAddedToMixerOnceOnStart) {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100326 ConfigHelper helper1;
327 ConfigHelper helper2(helper1.audio_mixer());
328 auto recv_stream1 = helper1.CreateAudioReceiveStream();
329 auto recv_stream2 = helper2.CreateAudioReceiveStream();
aleloi04c07222016-11-22 06:42:53 -0800330
Niels Möller349ade32018-11-16 09:50:42 +0100331 EXPECT_CALL(*helper1.channel_receive(), StartPlayout()).Times(1);
332 EXPECT_CALL(*helper2.channel_receive(), StartPlayout()).Times(1);
333 EXPECT_CALL(*helper1.channel_receive(), StopPlayout()).Times(1);
334 EXPECT_CALL(*helper2.channel_receive(), StopPlayout()).Times(1);
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100335 EXPECT_CALL(*helper1.audio_mixer(), AddSource(recv_stream1.get()))
aleloi04c07222016-11-22 06:42:53 -0800336 .WillOnce(Return(true));
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100337 EXPECT_CALL(*helper1.audio_mixer(), AddSource(recv_stream2.get()))
Fredrik Solenbergd5247512017-12-18 22:41:03 +0100338 .WillOnce(Return(true));
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100339 EXPECT_CALL(*helper1.audio_mixer(), RemoveSource(recv_stream1.get()))
340 .Times(1);
341 EXPECT_CALL(*helper1.audio_mixer(), RemoveSource(recv_stream2.get()))
342 .Times(1);
aleloi04c07222016-11-22 06:42:53 -0800343
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100344 recv_stream1->Start();
345 recv_stream2->Start();
Fredrik Solenbergd5247512017-12-18 22:41:03 +0100346
347 // One more should not result in any more mixer sources added.
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100348 recv_stream1->Start();
Fredrik Solenbergd5247512017-12-18 22:41:03 +0100349
350 // Stop stream before it is being destructed.
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100351 recv_stream2->Stop();
aleloi04c07222016-11-22 06:42:53 -0800352}
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100353
354TEST(AudioReceiveStreamTest, ReconfigureWithSameConfig) {
355 ConfigHelper helper;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100356 auto recv_stream = helper.CreateAudioReceiveStream();
357 recv_stream->Reconfigure(helper.config());
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100358}
359
360TEST(AudioReceiveStreamTest, ReconfigureWithUpdatedConfig) {
361 ConfigHelper helper;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100362 auto recv_stream = helper.CreateAudioReceiveStream();
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100363
364 auto new_config = helper.config();
365 new_config.rtp.local_ssrc = kLocalSsrc + 1;
366 new_config.rtp.nack.rtp_history_ms = 300 + 20;
367 new_config.rtp.extensions.clear();
368 new_config.rtp.extensions.push_back(
Yves Gerey665174f2018-06-19 15:03:05 +0200369 RtpExtension(RtpExtension::kAudioLevelUri, kAudioLevelId + 1));
370 new_config.rtp.extensions.push_back(
371 RtpExtension(RtpExtension::kTransportSequenceNumberUri,
372 kTransportSequenceNumberId + 1));
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100373 new_config.decoder_map.emplace(1, SdpAudioFormat("foo", 8000, 1));
374
Niels Möller349ade32018-11-16 09:50:42 +0100375 MockChannelReceive& channel_receive = *helper.channel_receive();
376 EXPECT_CALL(channel_receive, SetLocalSSRC(kLocalSsrc + 1)).Times(1);
377 EXPECT_CALL(channel_receive, SetNACKStatus(true, 15 + 1)).Times(1);
378 EXPECT_CALL(channel_receive, SetReceiveCodecs(new_config.decoder_map));
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100379
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100380 recv_stream->Reconfigure(new_config);
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100381}
Benjamin Wright78410ad2018-10-25 09:52:57 -0700382
383TEST(AudioReceiveStreamTest, ReconfigureWithFrameDecryptor) {
384 ConfigHelper helper;
385 auto recv_stream = helper.CreateAudioReceiveStream();
386
387 auto new_config_0 = helper.config();
388 rtc::scoped_refptr<FrameDecryptorInterface> mock_frame_decryptor_0(
389 new rtc::RefCountedObject<MockFrameDecryptor>());
390 new_config_0.frame_decryptor = mock_frame_decryptor_0;
391
392 recv_stream->Reconfigure(new_config_0);
393
394 auto new_config_1 = helper.config();
395 rtc::scoped_refptr<FrameDecryptorInterface> mock_frame_decryptor_1(
396 new rtc::RefCountedObject<MockFrameDecryptor>());
397 new_config_1.frame_decryptor = mock_frame_decryptor_1;
398 new_config_1.crypto_options.sframe.require_frame_encryption = true;
399 recv_stream->Reconfigure(new_config_1);
400}
401
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200402} // namespace test
Stefan Holmer8bffba72015-09-23 15:53:52 +0200403} // namespace webrtc