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 | |
Jakob Ivarsson | db20831 | 2023-01-27 15:13:22 +0100 | [diff] [blame] | 31 | using ::testing::Invoke; |
| 32 | using ::testing::NiceMock; |
| 33 | using ::testing::Return; |
| 34 | |
Jakob Ivarsson | dcb09ff | 2023-01-25 20:03:56 +0100 | [diff] [blame] | 35 | constexpr int kRtcpIntervalMs = 1000; |
| 36 | constexpr int kSsrc = 333; |
| 37 | constexpr int kPayloadType = 1; |
Jakob Ivarsson | db20831 | 2023-01-27 15:13:22 +0100 | [diff] [blame] | 38 | constexpr int kSampleRateHz = 48000; |
| 39 | constexpr int kRtpRateHz = 48000; |
Jakob Ivarsson | dcb09ff | 2023-01-25 20:03:56 +0100 | [diff] [blame] | 40 | |
| 41 | BitrateConstraints GetBitrateConfig() { |
| 42 | BitrateConstraints bitrate_config; |
| 43 | bitrate_config.min_bitrate_bps = 10000; |
| 44 | bitrate_config.start_bitrate_bps = 100000; |
| 45 | bitrate_config.max_bitrate_bps = 1000000; |
| 46 | return bitrate_config; |
| 47 | } |
| 48 | |
Jakob Ivarsson | dcb09ff | 2023-01-25 20:03:56 +0100 | [diff] [blame] | 49 | class ChannelSendTest : public ::testing::Test { |
| 50 | protected: |
| 51 | ChannelSendTest() |
| 52 | : time_controller_(Timestamp::Seconds(1)), |
| 53 | transport_controller_( |
| 54 | time_controller_.GetClock(), |
| 55 | RtpTransportConfig{ |
| 56 | .bitrate_config = GetBitrateConfig(), |
| 57 | .event_log = &event_log_, |
| 58 | .task_queue_factory = time_controller_.GetTaskQueueFactory(), |
| 59 | .trials = &field_trials_, |
| 60 | }) { |
Jakob Ivarsson | db20831 | 2023-01-27 15:13:22 +0100 | [diff] [blame] | 61 | channel_ = voe::CreateChannelSend( |
Jakob Ivarsson | dcb09ff | 2023-01-25 20:03:56 +0100 | [diff] [blame] | 62 | time_controller_.GetClock(), time_controller_.GetTaskQueueFactory(), |
| 63 | &transport_, nullptr, &event_log_, nullptr, crypto_options_, false, |
| 64 | kRtcpIntervalMs, kSsrc, nullptr, nullptr, field_trials_); |
Jakob Ivarsson | db20831 | 2023-01-27 15:13:22 +0100 | [diff] [blame] | 65 | encoder_factory_ = CreateBuiltinAudioEncoderFactory(); |
| 66 | std::unique_ptr<AudioEncoder> encoder = encoder_factory_->MakeAudioEncoder( |
| 67 | kPayloadType, SdpAudioFormat("opus", kRtpRateHz, 2), {}); |
| 68 | channel_->SetEncoder(kPayloadType, std::move(encoder)); |
| 69 | transport_controller_.EnsureStarted(); |
| 70 | channel_->RegisterSenderCongestionControlObjects(&transport_controller_, |
| 71 | nullptr); |
| 72 | ON_CALL(transport_, SendRtcp).WillByDefault(Return(true)); |
| 73 | ON_CALL(transport_, SendRtp).WillByDefault(Return(true)); |
| 74 | } |
| 75 | |
| 76 | std::unique_ptr<AudioFrame> CreateAudioFrame() { |
| 77 | auto frame = std::make_unique<AudioFrame>(); |
| 78 | frame->sample_rate_hz_ = kSampleRateHz; |
| 79 | frame->samples_per_channel_ = kSampleRateHz / 100; |
| 80 | frame->num_channels_ = 1; |
| 81 | frame->set_absolute_capture_timestamp_ms( |
| 82 | time_controller_.GetClock()->TimeInMilliseconds()); |
| 83 | return frame; |
| 84 | } |
| 85 | |
| 86 | void ProcessNextFrame() { |
| 87 | channel_->ProcessAndEncodeAudio(CreateAudioFrame()); |
| 88 | // Advance time to process the task queue. |
| 89 | time_controller_.AdvanceTime(TimeDelta::Millis(10)); |
Jakob Ivarsson | dcb09ff | 2023-01-25 20:03:56 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | GlobalSimulatedTimeController time_controller_; |
| 93 | webrtc::test::ScopedKeyValueConfig field_trials_; |
| 94 | RtcEventLogNull event_log_; |
Jakob Ivarsson | db20831 | 2023-01-27 15:13:22 +0100 | [diff] [blame] | 95 | NiceMock<MockTransport> transport_; |
Jakob Ivarsson | dcb09ff | 2023-01-25 20:03:56 +0100 | [diff] [blame] | 96 | CryptoOptions crypto_options_; |
Jakob Ivarsson | db20831 | 2023-01-27 15:13:22 +0100 | [diff] [blame] | 97 | RtpTransportControllerSend transport_controller_; |
| 98 | std::unique_ptr<ChannelSendInterface> channel_; |
| 99 | rtc::scoped_refptr<AudioEncoderFactory> encoder_factory_; |
Jakob Ivarsson | dcb09ff | 2023-01-25 20:03:56 +0100 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | TEST_F(ChannelSendTest, StopSendShouldResetEncoder) { |
Jakob Ivarsson | db20831 | 2023-01-27 15:13:22 +0100 | [diff] [blame] | 103 | channel_->StartSend(); |
Jakob Ivarsson | dcb09ff | 2023-01-25 20:03:56 +0100 | [diff] [blame] | 104 | // Insert two frames which should trigger a new packet. |
| 105 | EXPECT_CALL(transport_, SendRtp).Times(1); |
Jakob Ivarsson | db20831 | 2023-01-27 15:13:22 +0100 | [diff] [blame] | 106 | ProcessNextFrame(); |
| 107 | ProcessNextFrame(); |
Jakob Ivarsson | dcb09ff | 2023-01-25 20:03:56 +0100 | [diff] [blame] | 108 | |
| 109 | EXPECT_CALL(transport_, SendRtp).Times(0); |
Jakob Ivarsson | db20831 | 2023-01-27 15:13:22 +0100 | [diff] [blame] | 110 | ProcessNextFrame(); |
Jakob Ivarsson | dcb09ff | 2023-01-25 20:03:56 +0100 | [diff] [blame] | 111 | // StopSend should clear the previous audio frame stored in the encoder. |
Jakob Ivarsson | db20831 | 2023-01-27 15:13:22 +0100 | [diff] [blame] | 112 | channel_->StopSend(); |
| 113 | channel_->StartSend(); |
Jakob Ivarsson | dcb09ff | 2023-01-25 20:03:56 +0100 | [diff] [blame] | 114 | // The following frame should not trigger a new packet since the encoder |
| 115 | // needs 20 ms audio. |
Jakob Ivarsson | db20831 | 2023-01-27 15:13:22 +0100 | [diff] [blame] | 116 | EXPECT_CALL(transport_, SendRtp).Times(0); |
| 117 | ProcessNextFrame(); |
| 118 | } |
| 119 | |
| 120 | TEST_F(ChannelSendTest, IncreaseRtpTimestampByPauseDuration) { |
| 121 | channel_->StartSend(); |
| 122 | uint32_t timestamp; |
| 123 | int sent_packets = 0; |
| 124 | auto send_rtp = [&](const uint8_t* data, size_t length, |
| 125 | const PacketOptions& options) { |
| 126 | ++sent_packets; |
| 127 | RtpPacketReceived packet; |
| 128 | packet.Parse(data, length); |
| 129 | timestamp = packet.Timestamp(); |
| 130 | return true; |
| 131 | }; |
| 132 | EXPECT_CALL(transport_, SendRtp).WillRepeatedly(Invoke(send_rtp)); |
| 133 | ProcessNextFrame(); |
| 134 | ProcessNextFrame(); |
| 135 | EXPECT_EQ(sent_packets, 1); |
| 136 | uint32_t first_timestamp = timestamp; |
| 137 | channel_->StopSend(); |
| 138 | time_controller_.AdvanceTime(TimeDelta::Seconds(10)); |
| 139 | channel_->StartSend(); |
| 140 | |
| 141 | ProcessNextFrame(); |
| 142 | ProcessNextFrame(); |
| 143 | EXPECT_EQ(sent_packets, 2); |
| 144 | int64_t timestamp_gap_ms = |
| 145 | static_cast<int64_t>(timestamp - first_timestamp) * 1000 / kRtpRateHz; |
| 146 | EXPECT_EQ(timestamp_gap_ms, 10020); |
Jakob Ivarsson | dcb09ff | 2023-01-25 20:03:56 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | } // namespace |
| 150 | } // namespace voe |
| 151 | } // namespace webrtc |