blob: 97882b93b7f82baf1ee4de840ded08b38afbec8f [file] [log] [blame]
Jakob Ivarssondcb09ff2023-01-25 20:03:56 +01001/*
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
27namespace webrtc {
28namespace voe {
29namespace {
30
Jakob Ivarssondb208312023-01-27 15:13:22 +010031using ::testing::Invoke;
32using ::testing::NiceMock;
33using ::testing::Return;
34
Jakob Ivarssondcb09ff2023-01-25 20:03:56 +010035constexpr int kRtcpIntervalMs = 1000;
36constexpr int kSsrc = 333;
37constexpr int kPayloadType = 1;
Jakob Ivarssondb208312023-01-27 15:13:22 +010038constexpr int kSampleRateHz = 48000;
39constexpr int kRtpRateHz = 48000;
Jakob Ivarssondcb09ff2023-01-25 20:03:56 +010040
41BitrateConstraints 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 Ivarssondcb09ff2023-01-25 20:03:56 +010049class 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 Ivarssondb208312023-01-27 15:13:22 +010061 channel_ = voe::CreateChannelSend(
Jakob Ivarssondcb09ff2023-01-25 20:03:56 +010062 time_controller_.GetClock(), time_controller_.GetTaskQueueFactory(),
63 &transport_, nullptr, &event_log_, nullptr, crypto_options_, false,
64 kRtcpIntervalMs, kSsrc, nullptr, nullptr, field_trials_);
Jakob Ivarssondb208312023-01-27 15:13:22 +010065 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 Ivarssondcb09ff2023-01-25 20:03:56 +010090 }
91
92 GlobalSimulatedTimeController time_controller_;
93 webrtc::test::ScopedKeyValueConfig field_trials_;
94 RtcEventLogNull event_log_;
Jakob Ivarssondb208312023-01-27 15:13:22 +010095 NiceMock<MockTransport> transport_;
Jakob Ivarssondcb09ff2023-01-25 20:03:56 +010096 CryptoOptions crypto_options_;
Jakob Ivarssondb208312023-01-27 15:13:22 +010097 RtpTransportControllerSend transport_controller_;
98 std::unique_ptr<ChannelSendInterface> channel_;
99 rtc::scoped_refptr<AudioEncoderFactory> encoder_factory_;
Jakob Ivarssondcb09ff2023-01-25 20:03:56 +0100100};
101
102TEST_F(ChannelSendTest, StopSendShouldResetEncoder) {
Jakob Ivarssondb208312023-01-27 15:13:22 +0100103 channel_->StartSend();
Jakob Ivarssondcb09ff2023-01-25 20:03:56 +0100104 // Insert two frames which should trigger a new packet.
105 EXPECT_CALL(transport_, SendRtp).Times(1);
Jakob Ivarssondb208312023-01-27 15:13:22 +0100106 ProcessNextFrame();
107 ProcessNextFrame();
Jakob Ivarssondcb09ff2023-01-25 20:03:56 +0100108
109 EXPECT_CALL(transport_, SendRtp).Times(0);
Jakob Ivarssondb208312023-01-27 15:13:22 +0100110 ProcessNextFrame();
Jakob Ivarssondcb09ff2023-01-25 20:03:56 +0100111 // StopSend should clear the previous audio frame stored in the encoder.
Jakob Ivarssondb208312023-01-27 15:13:22 +0100112 channel_->StopSend();
113 channel_->StartSend();
Jakob Ivarssondcb09ff2023-01-25 20:03:56 +0100114 // The following frame should not trigger a new packet since the encoder
115 // needs 20 ms audio.
Jakob Ivarssondb208312023-01-27 15:13:22 +0100116 EXPECT_CALL(transport_, SendRtp).Times(0);
117 ProcessNextFrame();
118}
119
120TEST_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 Ivarssondcb09ff2023-01-25 20:03:56 +0100147}
148
149} // namespace
150} // namespace voe
151} // namespace webrtc