audio_receive_stream_unittest.cc (19852B)
1 /* 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 11 #include "audio/audio_receive_stream.h" 12 13 #include <cstddef> 14 #include <cstdint> 15 #include <map> 16 #include <memory> 17 #include <utility> 18 #include <vector> 19 20 #include "api/audio_codecs/audio_format.h" 21 #include "api/crypto/frame_decryptor_interface.h" 22 #include "api/environment/environment_factory.h" 23 #include "api/make_ref_counted.h" 24 #include "api/rtp_headers.h" 25 #include "api/scoped_refptr.h" 26 #include "api/test/mock_audio_mixer.h" 27 #include "api/test/mock_frame_decryptor.h" 28 #include "audio/channel_receive.h" 29 #include "audio/conversion.h" 30 #include "audio/mock_voe_channel_proxy.h" 31 #include "call/audio_receive_stream.h" 32 #include "call/audio_state.h" 33 #include "call/rtp_stream_receiver_controller.h" 34 #include "modules/audio_coding/include/audio_coding_module_typedefs.h" 35 #include "modules/audio_device/include/mock_audio_device.h" 36 #include "modules/audio_processing/include/mock_audio_processing.h" 37 #include "modules/pacing/packet_router.h" 38 #include "modules/rtp_rtcp/source/byte_io.h" 39 #include "rtc_base/time_utils.h" 40 #include "test/gmock.h" 41 #include "test/gtest.h" 42 #include "test/mock_audio_decoder_factory.h" 43 #include "test/mock_transport.h" 44 #include "test/run_loop.h" 45 46 namespace webrtc { 47 namespace test { 48 namespace { 49 50 using ::testing::_; 51 using ::testing::FloatEq; 52 using ::testing::NiceMock; 53 using ::testing::Return; 54 55 AudioDecodingCallStats MakeAudioDecodeStatsForTest() { 56 AudioDecodingCallStats audio_decode_stats; 57 audio_decode_stats.calls_to_silence_generator = 234; 58 audio_decode_stats.calls_to_neteq = 567; 59 audio_decode_stats.decoded_normal = 890; 60 audio_decode_stats.decoded_neteq_plc = 123; 61 audio_decode_stats.decoded_codec_plc = 124; 62 audio_decode_stats.decoded_cng = 456; 63 audio_decode_stats.decoded_plc_cng = 789; 64 audio_decode_stats.decoded_muted_output = 987; 65 return audio_decode_stats; 66 } 67 68 constexpr uint32_t kRemoteSsrc = 1234; 69 constexpr uint32_t kLocalSsrc = 5678; 70 constexpr int kJitterBufferDelay = -7; 71 constexpr int kPlayoutBufferDelay = 302; 72 constexpr unsigned int kSpeechOutputLevel = 99; 73 constexpr double kTotalOutputEnergy = 0.25; 74 constexpr double kTotalOutputDuration = 0.5; 75 constexpr int64_t kPlayoutNtpTimestampMs = 5678; 76 77 const ChannelReceiveStatistics kChannelStats = { 78 .packets_lost = 678, 79 .jitter_ms = 234, 80 .payload_bytes_received = -12, 81 .header_and_padding_bytes_received = 567, 82 .packets_received = 78, 83 .packets_received_with_ect1 = 890, 84 .packets_received_with_ce = 123}; 85 const std::pair<int, SdpAudioFormat> kReceiveCodec = { 86 123, 87 {"codec_name_recv", 96000, 0}}; 88 const NetworkStatistics kNetworkStats = {.currentBufferSize = 123, 89 .preferredBufferSize = 456, 90 .jitterPeaksFound = false, 91 .totalSamplesReceived = 789012, 92 .concealedSamples = 3456, 93 .silentConcealedSamples = 123, 94 .concealmentEvents = 456, 95 .jitterBufferDelayMs = 789, 96 .jitterBufferTargetDelayMs = 543, 97 .jitterBufferMinimumDelayMs = 123, 98 .jitterBufferEmittedCount = 222, 99 .insertedSamplesForDeceleration = 432, 100 .removedSamplesForAcceleration = 321, 101 .fecPacketsReceived = 123, 102 .fecPacketsDiscarded = 101, 103 .totalProcessingDelayUs = 154, 104 .packetsDiscarded = 989, 105 .currentExpandRate = 789, 106 .currentSpeechExpandRate = 12, 107 .currentPreemptiveRate = 345, 108 .currentAccelerateRate = 678, 109 .currentSecondaryDecodedRate = 901, 110 .currentSecondaryDiscardedRate = 0, 111 .meanWaitingTimeMs = -1, 112 .maxWaitingTimeMs = -1, 113 .packetBufferFlushes = 0, 114 .delayedPacketOutageSamples = 0, 115 .relativePacketArrivalDelayMs = 135, 116 .interruptionCount = -1, 117 .totalInterruptionDurationMs = -1}; 118 const AudioDecodingCallStats kAudioDecodeStats = MakeAudioDecodeStatsForTest(); 119 120 struct ConfigHelper { 121 explicit ConfigHelper(bool use_null_audio_processing) 122 : ConfigHelper(make_ref_counted<MockAudioMixer>(), 123 use_null_audio_processing) {} 124 125 ConfigHelper(scoped_refptr<MockAudioMixer> audio_mixer, 126 bool use_null_audio_processing) 127 : audio_mixer_(audio_mixer) { 128 using ::testing::Invoke; 129 130 AudioState::Config config; 131 config.audio_mixer = audio_mixer_; 132 config.audio_processing = 133 use_null_audio_processing 134 ? nullptr 135 : make_ref_counted<NiceMock<MockAudioProcessing>>(); 136 config.audio_device_module = 137 make_ref_counted<testing::NiceMock<MockAudioDeviceModule>>(); 138 audio_state_ = AudioState::Create(config); 139 140 channel_receive_ = new ::testing::StrictMock<MockChannelReceive>(); 141 EXPECT_CALL(*channel_receive_, SetNACKStatus(true, 15)).Times(1); 142 EXPECT_CALL(*channel_receive_, SetRtcpMode(_)).Times(1); 143 EXPECT_CALL(*channel_receive_, 144 RegisterReceiverCongestionControlObjects(&packet_router_)) 145 .Times(1); 146 EXPECT_CALL(*channel_receive_, ResetReceiverCongestionControlObjects()) 147 .Times(1); 148 EXPECT_CALL(*channel_receive_, SetReceiveCodecs(_)) 149 .WillRepeatedly(Invoke([](const std::map<int, SdpAudioFormat>& codecs) { 150 EXPECT_THAT(codecs, ::testing::IsEmpty()); 151 })); 152 153 stream_config_.rtp.local_ssrc = kLocalSsrc; 154 stream_config_.rtp.remote_ssrc = kRemoteSsrc; 155 stream_config_.rtp.nack.rtp_history_ms = 300; 156 stream_config_.rtcp_send_transport = &rtcp_send_transport_; 157 stream_config_.decoder_factory = 158 make_ref_counted<MockAudioDecoderFactory>(); 159 } 160 161 std::unique_ptr<AudioReceiveStreamImpl> CreateAudioReceiveStream() { 162 auto ret = std::make_unique<AudioReceiveStreamImpl>( 163 CreateEnvironment(), &packet_router_, stream_config_, audio_state_, 164 std::unique_ptr<voe::ChannelReceiveInterface>(channel_receive_)); 165 ret->RegisterWithTransport(&rtp_stream_receiver_controller_); 166 return ret; 167 } 168 169 AudioReceiveStreamInterface::Config& config() { return stream_config_; } 170 scoped_refptr<MockAudioMixer> audio_mixer() { return audio_mixer_; } 171 MockChannelReceive* channel_receive() { return channel_receive_; } 172 173 void SetupMockForGetStats() { 174 using ::testing::DoAll; 175 using ::testing::SetArgPointee; 176 177 ASSERT_TRUE(channel_receive_); 178 EXPECT_CALL(*channel_receive_, GetRTCPStatistics()) 179 .WillOnce(Return(kChannelStats)); 180 EXPECT_CALL(*channel_receive_, GetDelayEstimate()) 181 .WillOnce(Return(kJitterBufferDelay + kPlayoutBufferDelay)); 182 EXPECT_CALL(*channel_receive_, GetSpeechOutputLevelFullRange()) 183 .WillOnce(Return(kSpeechOutputLevel)); 184 EXPECT_CALL(*channel_receive_, GetTotalOutputEnergy()) 185 .WillOnce(Return(kTotalOutputEnergy)); 186 EXPECT_CALL(*channel_receive_, GetTotalOutputDuration()) 187 .WillOnce(Return(kTotalOutputDuration)); 188 EXPECT_CALL(*channel_receive_, GetNetworkStatistics(_)) 189 .WillOnce(Return(kNetworkStats)); 190 EXPECT_CALL(*channel_receive_, GetDecodingCallStatistics()) 191 .WillOnce(Return(kAudioDecodeStats)); 192 EXPECT_CALL(*channel_receive_, GetReceiveCodec()) 193 .WillOnce(Return(kReceiveCodec)); 194 EXPECT_CALL(*channel_receive_, GetCurrentEstimatedPlayoutNtpTimestampMs(_)) 195 .WillOnce(Return(kPlayoutNtpTimestampMs)); 196 } 197 198 private: 199 PacketRouter packet_router_; 200 scoped_refptr<AudioState> audio_state_; 201 scoped_refptr<MockAudioMixer> audio_mixer_; 202 AudioReceiveStreamInterface::Config stream_config_; 203 ::testing::StrictMock<MockChannelReceive>* channel_receive_ = nullptr; 204 RtpStreamReceiverController rtp_stream_receiver_controller_; 205 MockTransport rtcp_send_transport_; 206 }; 207 208 std::vector<uint8_t> CreateRtcpSenderReport() { 209 std::vector<uint8_t> packet; 210 const size_t kRtcpSrLength = 28; // In bytes. 211 packet.resize(kRtcpSrLength); 212 packet[0] = 0x80; // Version 2. 213 packet[1] = 0xc8; // PT = 200, SR. 214 // Length in number of 32-bit words - 1. 215 ByteWriter<uint16_t>::WriteBigEndian(&packet[2], 6); 216 ByteWriter<uint32_t>::WriteBigEndian(&packet[4], kLocalSsrc); 217 return packet; 218 } 219 } // namespace 220 221 TEST(AudioReceiveStreamTest, ConfigToString) { 222 AudioReceiveStreamInterface::Config config; 223 config.rtp.remote_ssrc = kRemoteSsrc; 224 config.rtp.local_ssrc = kLocalSsrc; 225 config.rtp.rtcp_mode = RtcpMode::kOff; 226 EXPECT_EQ( 227 "{rtp: {remote_ssrc: 1234, local_ssrc: 5678, nack: " 228 "{rtp_history_ms: 0}, rtcp: off}, " 229 "rtcp_send_transport: null}", 230 config.ToString()); 231 } 232 233 TEST(AudioReceiveStreamTest, ConstructDestruct) { 234 test::RunLoop loop; 235 for (bool use_null_audio_processing : {false, true}) { 236 ConfigHelper helper(use_null_audio_processing); 237 auto recv_stream = helper.CreateAudioReceiveStream(); 238 recv_stream->UnregisterFromTransport(); 239 } 240 } 241 242 TEST(AudioReceiveStreamTest, ReceiveRtcpPacket) { 243 test::RunLoop loop; 244 for (bool use_null_audio_processing : {false, true}) { 245 ConfigHelper helper(use_null_audio_processing); 246 auto recv_stream = helper.CreateAudioReceiveStream(); 247 std::vector<uint8_t> rtcp_packet = CreateRtcpSenderReport(); 248 EXPECT_CALL(*helper.channel_receive(), 249 ReceivedRTCPPacket(&rtcp_packet[0], rtcp_packet.size())) 250 .WillOnce(Return()); 251 recv_stream->DeliverRtcp(rtcp_packet); 252 recv_stream->UnregisterFromTransport(); 253 } 254 } 255 256 TEST(AudioReceiveStreamTest, GetStats) { 257 test::RunLoop loop; 258 for (bool use_null_audio_processing : {false, true}) { 259 ConfigHelper helper(use_null_audio_processing); 260 auto recv_stream = helper.CreateAudioReceiveStream(); 261 helper.SetupMockForGetStats(); 262 AudioReceiveStreamInterface::Stats stats = 263 recv_stream->GetStats(/*get_and_clear_legacy_stats=*/true); 264 EXPECT_EQ(kRemoteSsrc, stats.remote_ssrc); 265 EXPECT_EQ(kChannelStats.payload_bytes_received, 266 stats.payload_bytes_received); 267 EXPECT_EQ(kChannelStats.header_and_padding_bytes_received, 268 stats.header_and_padding_bytes_received); 269 EXPECT_EQ(static_cast<uint32_t>(kChannelStats.packets_received), 270 stats.packets_received); 271 EXPECT_EQ(kChannelStats.packets_lost, stats.packets_lost); 272 EXPECT_EQ(kReceiveCodec.second.name, stats.codec_name); 273 EXPECT_EQ(kChannelStats.jitter_ms, stats.jitter_ms); 274 EXPECT_EQ(kNetworkStats.currentBufferSize, stats.jitter_buffer_ms); 275 EXPECT_EQ(kNetworkStats.preferredBufferSize, 276 stats.jitter_buffer_preferred_ms); 277 EXPECT_EQ(static_cast<uint32_t>(kJitterBufferDelay + kPlayoutBufferDelay), 278 stats.delay_estimate_ms); 279 EXPECT_EQ(static_cast<int32_t>(kSpeechOutputLevel), stats.audio_level); 280 EXPECT_EQ(kTotalOutputEnergy, stats.total_output_energy); 281 EXPECT_EQ(kNetworkStats.totalSamplesReceived, stats.total_samples_received); 282 EXPECT_EQ(kTotalOutputDuration, stats.total_output_duration); 283 EXPECT_EQ(kNetworkStats.concealedSamples, stats.concealed_samples); 284 EXPECT_EQ(kNetworkStats.concealmentEvents, stats.concealment_events); 285 EXPECT_EQ(static_cast<double>(kNetworkStats.jitterBufferDelayMs) / 286 static_cast<double>(kNumMillisecsPerSec), 287 stats.jitter_buffer_delay_seconds); 288 EXPECT_EQ(kNetworkStats.jitterBufferEmittedCount, 289 stats.jitter_buffer_emitted_count); 290 EXPECT_EQ(static_cast<double>(kNetworkStats.jitterBufferTargetDelayMs) / 291 static_cast<double>(kNumMillisecsPerSec), 292 stats.jitter_buffer_target_delay_seconds); 293 EXPECT_EQ(static_cast<double>(kNetworkStats.jitterBufferMinimumDelayMs) / 294 static_cast<double>(kNumMillisecsPerSec), 295 stats.jitter_buffer_minimum_delay_seconds); 296 EXPECT_EQ(kNetworkStats.insertedSamplesForDeceleration, 297 stats.inserted_samples_for_deceleration); 298 EXPECT_EQ(kNetworkStats.removedSamplesForAcceleration, 299 stats.removed_samples_for_acceleration); 300 EXPECT_EQ(kNetworkStats.fecPacketsReceived, stats.fec_packets_received); 301 EXPECT_EQ(kNetworkStats.fecPacketsDiscarded, stats.fec_packets_discarded); 302 EXPECT_EQ(static_cast<double>(kNetworkStats.totalProcessingDelayUs) / 303 static_cast<double>(kNumMicrosecsPerSec), 304 stats.total_processing_delay_seconds); 305 EXPECT_EQ(kNetworkStats.packetsDiscarded, stats.packets_discarded); 306 EXPECT_EQ(Q14ToFloat(kNetworkStats.currentExpandRate), stats.expand_rate); 307 EXPECT_EQ(Q14ToFloat(kNetworkStats.currentSpeechExpandRate), 308 stats.speech_expand_rate); 309 EXPECT_EQ(Q14ToFloat(kNetworkStats.currentSecondaryDecodedRate), 310 stats.secondary_decoded_rate); 311 EXPECT_EQ(Q14ToFloat(kNetworkStats.currentSecondaryDiscardedRate), 312 stats.secondary_discarded_rate); 313 EXPECT_EQ(Q14ToFloat(kNetworkStats.currentAccelerateRate), 314 stats.accelerate_rate); 315 EXPECT_EQ(Q14ToFloat(kNetworkStats.currentPreemptiveRate), 316 stats.preemptive_expand_rate); 317 EXPECT_EQ(kNetworkStats.packetBufferFlushes, stats.jitter_buffer_flushes); 318 EXPECT_EQ(kNetworkStats.delayedPacketOutageSamples, 319 stats.delayed_packet_outage_samples); 320 EXPECT_EQ(static_cast<double>(kNetworkStats.relativePacketArrivalDelayMs) / 321 static_cast<double>(kNumMillisecsPerSec), 322 stats.relative_packet_arrival_delay_seconds); 323 EXPECT_EQ(kNetworkStats.interruptionCount, stats.interruption_count); 324 EXPECT_EQ(kNetworkStats.totalInterruptionDurationMs, 325 stats.total_interruption_duration_ms); 326 327 EXPECT_EQ(kAudioDecodeStats.calls_to_silence_generator, 328 stats.decoding_calls_to_silence_generator); 329 EXPECT_EQ(kAudioDecodeStats.calls_to_neteq, stats.decoding_calls_to_neteq); 330 EXPECT_EQ(kAudioDecodeStats.decoded_normal, stats.decoding_normal); 331 EXPECT_EQ(kAudioDecodeStats.decoded_neteq_plc, stats.decoding_plc); 332 EXPECT_EQ(kAudioDecodeStats.decoded_codec_plc, stats.decoding_codec_plc); 333 EXPECT_EQ(kAudioDecodeStats.decoded_cng, stats.decoding_cng); 334 EXPECT_EQ(kAudioDecodeStats.decoded_plc_cng, stats.decoding_plc_cng); 335 EXPECT_EQ(kAudioDecodeStats.decoded_muted_output, 336 stats.decoding_muted_output); 337 EXPECT_EQ(kChannelStats.capture_start_ntp_time_ms, 338 stats.capture_start_ntp_time_ms); 339 EXPECT_EQ(kPlayoutNtpTimestampMs, stats.estimated_playout_ntp_timestamp_ms); 340 recv_stream->UnregisterFromTransport(); 341 } 342 } 343 344 TEST(AudioReceiveStreamTest, SetGain) { 345 test::RunLoop loop; 346 for (bool use_null_audio_processing : {false, true}) { 347 ConfigHelper helper(use_null_audio_processing); 348 auto recv_stream = helper.CreateAudioReceiveStream(); 349 EXPECT_CALL(*helper.channel_receive(), 350 SetChannelOutputVolumeScaling(FloatEq(0.765f))); 351 recv_stream->SetGain(0.765f); 352 recv_stream->UnregisterFromTransport(); 353 } 354 } 355 356 TEST(AudioReceiveStreamTest, StreamsShouldBeAddedToMixerOnceOnStart) { 357 test::RunLoop loop; 358 for (bool use_null_audio_processing : {false, true}) { 359 ConfigHelper helper1(use_null_audio_processing); 360 ConfigHelper helper2(helper1.audio_mixer(), use_null_audio_processing); 361 auto recv_stream1 = helper1.CreateAudioReceiveStream(); 362 auto recv_stream2 = helper2.CreateAudioReceiveStream(); 363 364 EXPECT_CALL(*helper1.channel_receive(), StartPlayout()).Times(1); 365 EXPECT_CALL(*helper2.channel_receive(), StartPlayout()).Times(1); 366 EXPECT_CALL(*helper1.channel_receive(), StopPlayout()).Times(1); 367 EXPECT_CALL(*helper2.channel_receive(), StopPlayout()).Times(1); 368 EXPECT_CALL(*helper1.audio_mixer(), AddSource(recv_stream1.get())) 369 .WillOnce(Return(true)); 370 EXPECT_CALL(*helper1.audio_mixer(), AddSource(recv_stream2.get())) 371 .WillOnce(Return(true)); 372 EXPECT_CALL(*helper1.audio_mixer(), RemoveSource(recv_stream1.get())) 373 .Times(1); 374 EXPECT_CALL(*helper1.audio_mixer(), RemoveSource(recv_stream2.get())) 375 .Times(1); 376 377 recv_stream1->Start(); 378 recv_stream2->Start(); 379 380 // One more should not result in any more mixer sources added. 381 recv_stream1->Start(); 382 383 // Stop stream before it is being destructed. 384 recv_stream2->Stop(); 385 386 recv_stream1->UnregisterFromTransport(); 387 recv_stream2->UnregisterFromTransport(); 388 } 389 } 390 391 TEST(AudioReceiveStreamTest, ReconfigureWithUpdatedConfig) { 392 test::RunLoop loop; 393 for (bool use_null_audio_processing : {false, true}) { 394 ConfigHelper helper(use_null_audio_processing); 395 auto recv_stream = helper.CreateAudioReceiveStream(); 396 397 auto new_config = helper.config(); 398 399 MockChannelReceive& channel_receive = *helper.channel_receive(); 400 401 // TODO(tommi, nisse): This applies new extensions to the internal config, 402 // but there's nothing that actually verifies that the changes take effect. 403 // In fact Call manages the extensions separately in Call::ReceiveRtpConfig 404 // and changing this config value (there seem to be a few copies), doesn't 405 // affect that logic. 406 recv_stream->ReconfigureForTesting(new_config); 407 408 new_config.decoder_map.emplace(1, SdpAudioFormat("foo", 8000, 1)); 409 EXPECT_CALL(channel_receive, SetReceiveCodecs(new_config.decoder_map)); 410 recv_stream->SetDecoderMap(new_config.decoder_map); 411 412 EXPECT_CALL(channel_receive, SetNACKStatus(true, 15 + 1)).Times(1); 413 recv_stream->SetNackHistory(300 + 20); 414 415 recv_stream->UnregisterFromTransport(); 416 } 417 } 418 419 TEST(AudioReceiveStreamTest, ReconfigureWithFrameDecryptor) { 420 test::RunLoop loop; 421 for (bool use_null_audio_processing : {false, true}) { 422 ConfigHelper helper(use_null_audio_processing); 423 auto recv_stream = helper.CreateAudioReceiveStream(); 424 425 auto new_config_0 = helper.config(); 426 scoped_refptr<FrameDecryptorInterface> mock_frame_decryptor_0( 427 make_ref_counted<MockFrameDecryptor>()); 428 new_config_0.frame_decryptor = mock_frame_decryptor_0; 429 430 // TODO(tommi): While this changes the internal config value, it doesn't 431 // actually change what frame_decryptor is used. WebRtcAudioReceiveStream 432 // recreates the whole instance in order to change this value. 433 // So, it's not clear if changing this post initialization needs to be 434 // supported. 435 recv_stream->ReconfigureForTesting(new_config_0); 436 437 auto new_config_1 = helper.config(); 438 scoped_refptr<FrameDecryptorInterface> mock_frame_decryptor_1( 439 make_ref_counted<MockFrameDecryptor>()); 440 new_config_1.frame_decryptor = mock_frame_decryptor_1; 441 new_config_1.crypto_options.sframe.require_frame_encryption = true; 442 recv_stream->ReconfigureForTesting(new_config_1); 443 recv_stream->UnregisterFromTransport(); 444 } 445 } 446 447 } // namespace test 448 } // namespace webrtc