Jakob Ivarsson | dcb09ff | 2023-01-25 20:03:56 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2023 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/channel_send.h" |
| 12 | |
| 13 | #include <utility> |
| 14 | |
| 15 | #include "api/audio/audio_frame.h" |
| 16 | #include "api/audio_codecs/builtin_audio_encoder_factory.h" |
| 17 | #include "api/rtc_event_log/rtc_event_log.h" |
| 18 | #include "api/scoped_refptr.h" |
| 19 | #include "api/units/time_delta.h" |
| 20 | #include "api/units/timestamp.h" |
| 21 | #include "call/rtp_transport_controller_send.h" |
| 22 | #include "test/gtest.h" |
| 23 | #include "test/mock_transport.h" |
| 24 | #include "test/scoped_key_value_config.h" |
| 25 | #include "test/time_controller/simulated_time_controller.h" |
| 26 | |
| 27 | namespace webrtc { |
| 28 | namespace voe { |
| 29 | namespace { |
| 30 | |
| 31 | constexpr int kRtcpIntervalMs = 1000; |
| 32 | constexpr int kSsrc = 333; |
| 33 | constexpr int kPayloadType = 1; |
| 34 | |
| 35 | BitrateConstraints GetBitrateConfig() { |
| 36 | BitrateConstraints bitrate_config; |
| 37 | bitrate_config.min_bitrate_bps = 10000; |
| 38 | bitrate_config.start_bitrate_bps = 100000; |
| 39 | bitrate_config.max_bitrate_bps = 1000000; |
| 40 | return bitrate_config; |
| 41 | } |
| 42 | |
| 43 | std::unique_ptr<AudioFrame> CreateAudioFrame() { |
| 44 | auto frame = std::make_unique<AudioFrame>(); |
| 45 | frame->samples_per_channel_ = 480; |
| 46 | frame->sample_rate_hz_ = 48000; |
| 47 | frame->num_channels_ = 1; |
| 48 | return frame; |
| 49 | } |
| 50 | |
| 51 | class ChannelSendTest : public ::testing::Test { |
| 52 | protected: |
| 53 | ChannelSendTest() |
| 54 | : time_controller_(Timestamp::Seconds(1)), |
| 55 | transport_controller_( |
| 56 | time_controller_.GetClock(), |
| 57 | RtpTransportConfig{ |
| 58 | .bitrate_config = GetBitrateConfig(), |
| 59 | .event_log = &event_log_, |
| 60 | .task_queue_factory = time_controller_.GetTaskQueueFactory(), |
| 61 | .trials = &field_trials_, |
| 62 | }) { |
| 63 | transport_controller_.EnsureStarted(); |
| 64 | } |
| 65 | |
| 66 | std::unique_ptr<ChannelSendInterface> CreateChannelSend() { |
| 67 | return voe::CreateChannelSend( |
| 68 | time_controller_.GetClock(), time_controller_.GetTaskQueueFactory(), |
| 69 | &transport_, nullptr, &event_log_, nullptr, crypto_options_, false, |
| 70 | kRtcpIntervalMs, kSsrc, nullptr, nullptr, field_trials_); |
| 71 | } |
| 72 | |
| 73 | GlobalSimulatedTimeController time_controller_; |
| 74 | webrtc::test::ScopedKeyValueConfig field_trials_; |
| 75 | RtcEventLogNull event_log_; |
| 76 | MockTransport transport_; |
| 77 | RtpTransportControllerSend transport_controller_; |
| 78 | CryptoOptions crypto_options_; |
| 79 | }; |
| 80 | |
| 81 | TEST_F(ChannelSendTest, StopSendShouldResetEncoder) { |
| 82 | std::unique_ptr<ChannelSendInterface> channel = CreateChannelSend(); |
| 83 | rtc::scoped_refptr<AudioEncoderFactory> encoder_factory = |
| 84 | CreateBuiltinAudioEncoderFactory(); |
| 85 | std::unique_ptr<AudioEncoder> encoder = encoder_factory->MakeAudioEncoder( |
| 86 | kPayloadType, SdpAudioFormat("opus", 48000, 2), {}); |
| 87 | channel->SetEncoder(kPayloadType, std::move(encoder)); |
| 88 | channel->RegisterSenderCongestionControlObjects(&transport_controller_, |
| 89 | nullptr); |
| 90 | channel->StartSend(); |
| 91 | |
| 92 | // Insert two frames which should trigger a new packet. |
| 93 | EXPECT_CALL(transport_, SendRtp).Times(1); |
| 94 | channel->ProcessAndEncodeAudio(CreateAudioFrame()); |
| 95 | time_controller_.AdvanceTime(webrtc::TimeDelta::Zero()); |
| 96 | channel->ProcessAndEncodeAudio(CreateAudioFrame()); |
| 97 | time_controller_.AdvanceTime(webrtc::TimeDelta::Zero()); |
| 98 | |
| 99 | EXPECT_CALL(transport_, SendRtp).Times(0); |
| 100 | channel->ProcessAndEncodeAudio(CreateAudioFrame()); |
| 101 | time_controller_.AdvanceTime(webrtc::TimeDelta::Zero()); |
| 102 | // StopSend should clear the previous audio frame stored in the encoder. |
| 103 | channel->StopSend(); |
| 104 | channel->StartSend(); |
| 105 | // The following frame should not trigger a new packet since the encoder |
| 106 | // needs 20 ms audio. |
| 107 | channel->ProcessAndEncodeAudio(CreateAudioFrame()); |
| 108 | time_controller_.AdvanceTime(webrtc::TimeDelta::Zero()); |
| 109 | } |
| 110 | |
| 111 | } // namespace |
| 112 | } // namespace voe |
| 113 | } // namespace webrtc |