blob: 770f255313cc1d47b8989c43c31008605e20e11d [file] [log] [blame]
solenbergc7a8b082015-10-16 14:35:07 -07001/*
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 <list>
kwiberg1c07c702017-03-27 07:15:49 -070012#include <map>
kwibergb25345e2016-03-12 06:10:44 -080013#include <memory>
zstein7cb69d52017-05-08 11:52:38 -070014#include <utility>
solenbergc7a8b082015-10-16 14:35:07 -070015
Karl Wiberg918f50c2018-07-05 11:40:33 +020016#include "absl/memory/memory.h"
Karl Wibergf3850f62017-11-02 13:04:41 +010017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -080018#include "api/test/fake_media_transport.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/test/mock_audio_mixer.h"
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010020#include "audio/audio_receive_stream.h"
Yves Gerey665174f2018-06-19 15:03:05 +020021#include "audio/audio_send_stream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "call/audio_state.h"
23#include "call/call.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "logging/rtc_event_log/rtc_event_log.h"
25#include "modules/audio_device/include/mock_audio_device.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010026#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/pacing/mock/mock_paced_sender.h"
28#include "modules/rtp_rtcp/include/rtp_rtcp.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "test/fake_encoder.h"
30#include "test/gtest.h"
31#include "test/mock_audio_decoder_factory.h"
32#include "test/mock_transport.h"
solenbergc7a8b082015-10-16 14:35:07 -070033
34namespace {
35
36struct CallHelper {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010037 CallHelper() {
solenberg566ef242015-11-06 15:34:49 -080038 webrtc::AudioState::Config audio_state_config;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010039 audio_state_config.audio_mixer =
40 new rtc::RefCountedObject<webrtc::test::MockAudioMixer>();
41 audio_state_config.audio_processing =
42 new rtc::RefCountedObject<webrtc::test::MockAudioProcessing>();
43 audio_state_config.audio_device_module =
44 new rtc::RefCountedObject<webrtc::test::MockAudioDeviceModule>();
skvlad11a9cbf2016-10-07 11:53:05 -070045 webrtc::Call::Config config(&event_log_);
solenberg566ef242015-11-06 15:34:49 -080046 config.audio_state = webrtc::AudioState::Create(audio_state_config);
solenbergc7a8b082015-10-16 14:35:07 -070047 call_.reset(webrtc::Call::Create(config));
48 }
49
50 webrtc::Call* operator->() { return call_.get(); }
51
52 private:
skvlad11a9cbf2016-10-07 11:53:05 -070053 webrtc::RtcEventLogNullImpl event_log_;
kwibergb25345e2016-03-12 06:10:44 -080054 std::unique_ptr<webrtc::Call> call_;
solenbergc7a8b082015-10-16 14:35:07 -070055};
56} // namespace
57
58namespace webrtc {
59
60TEST(CallTest, ConstructDestruct) {
61 CallHelper call;
62}
63
64TEST(CallTest, CreateDestroy_AudioSendStream) {
65 CallHelper call;
Niels Möller7d76a312018-10-26 12:57:07 +020066 AudioSendStream::Config config(/*send_transport=*/nullptr,
67 /*media_transport=*/nullptr);
solenbergc7a8b082015-10-16 14:35:07 -070068 config.rtp.ssrc = 42;
solenbergc7a8b082015-10-16 14:35:07 -070069 AudioSendStream* stream = call->CreateAudioSendStream(config);
70 EXPECT_NE(stream, nullptr);
71 call->DestroyAudioSendStream(stream);
72}
73
74TEST(CallTest, CreateDestroy_AudioReceiveStream) {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010075 CallHelper call;
solenbergc7a8b082015-10-16 14:35:07 -070076 AudioReceiveStream::Config config;
Niels Möllerae4237e2018-10-05 11:28:38 +020077 MockTransport rtcp_send_transport;
solenbergc7a8b082015-10-16 14:35:07 -070078 config.rtp.remote_ssrc = 42;
Niels Möllerae4237e2018-10-05 11:28:38 +020079 config.rtcp_send_transport = &rtcp_send_transport;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010080 config.decoder_factory =
81 new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>();
solenbergc7a8b082015-10-16 14:35:07 -070082 AudioReceiveStream* stream = call->CreateAudioReceiveStream(config);
83 EXPECT_NE(stream, nullptr);
84 call->DestroyAudioReceiveStream(stream);
85}
86
87TEST(CallTest, CreateDestroy_AudioSendStreams) {
88 CallHelper call;
Niels Möller7d76a312018-10-26 12:57:07 +020089 AudioSendStream::Config config(/*send_transport=*/nullptr,
90 /*media_transport=*/nullptr);
solenbergc7a8b082015-10-16 14:35:07 -070091 std::list<AudioSendStream*> streams;
92 for (int i = 0; i < 2; ++i) {
93 for (uint32_t ssrc = 0; ssrc < 1234567; ssrc += 34567) {
94 config.rtp.ssrc = ssrc;
95 AudioSendStream* stream = call->CreateAudioSendStream(config);
96 EXPECT_NE(stream, nullptr);
97 if (ssrc & 1) {
98 streams.push_back(stream);
99 } else {
100 streams.push_front(stream);
101 }
102 }
103 for (auto s : streams) {
104 call->DestroyAudioSendStream(s);
105 }
106 streams.clear();
107 }
108}
109
110TEST(CallTest, CreateDestroy_AudioReceiveStreams) {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100111 CallHelper call;
solenbergc7a8b082015-10-16 14:35:07 -0700112 AudioReceiveStream::Config config;
Niels Möllerae4237e2018-10-05 11:28:38 +0200113 MockTransport rtcp_send_transport;
114 config.rtcp_send_transport = &rtcp_send_transport;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100115 config.decoder_factory =
116 new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>();
solenbergc7a8b082015-10-16 14:35:07 -0700117 std::list<AudioReceiveStream*> streams;
118 for (int i = 0; i < 2; ++i) {
119 for (uint32_t ssrc = 0; ssrc < 1234567; ssrc += 34567) {
120 config.rtp.remote_ssrc = ssrc;
121 AudioReceiveStream* stream = call->CreateAudioReceiveStream(config);
122 EXPECT_NE(stream, nullptr);
123 if (ssrc & 1) {
124 streams.push_back(stream);
125 } else {
126 streams.push_front(stream);
127 }
128 }
129 for (auto s : streams) {
130 call->DestroyAudioReceiveStream(s);
131 }
132 streams.clear();
133 }
134}
brandtr25445d32016-10-23 23:37:14 -0700135
solenberg7602aab2016-11-14 11:30:07 -0800136TEST(CallTest, CreateDestroy_AssociateAudioSendReceiveStreams_RecvFirst) {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100137 CallHelper call;
solenberg7602aab2016-11-14 11:30:07 -0800138 AudioReceiveStream::Config recv_config;
Niels Möllerae4237e2018-10-05 11:28:38 +0200139 MockTransport rtcp_send_transport;
solenberg7602aab2016-11-14 11:30:07 -0800140 recv_config.rtp.remote_ssrc = 42;
141 recv_config.rtp.local_ssrc = 777;
Niels Möllerae4237e2018-10-05 11:28:38 +0200142 recv_config.rtcp_send_transport = &rtcp_send_transport;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100143 recv_config.decoder_factory =
144 new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>();
solenberg7602aab2016-11-14 11:30:07 -0800145 AudioReceiveStream* recv_stream = call->CreateAudioReceiveStream(recv_config);
146 EXPECT_NE(recv_stream, nullptr);
147
Niels Möller7d76a312018-10-26 12:57:07 +0200148 AudioSendStream::Config send_config(/*send_transport=*/nullptr,
149 /*media_transport=*/nullptr);
solenberg7602aab2016-11-14 11:30:07 -0800150 send_config.rtp.ssrc = 777;
solenberg7602aab2016-11-14 11:30:07 -0800151 AudioSendStream* send_stream = call->CreateAudioSendStream(send_config);
152 EXPECT_NE(send_stream, nullptr);
153
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100154 internal::AudioReceiveStream* internal_recv_stream =
155 static_cast<internal::AudioReceiveStream*>(recv_stream);
156 EXPECT_EQ(send_stream,
157 internal_recv_stream->GetAssociatedSendStreamForTesting());
solenberg7602aab2016-11-14 11:30:07 -0800158
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100159 call->DestroyAudioSendStream(send_stream);
160 EXPECT_EQ(nullptr, internal_recv_stream->GetAssociatedSendStreamForTesting());
161
solenberg7602aab2016-11-14 11:30:07 -0800162 call->DestroyAudioReceiveStream(recv_stream);
163}
164
165TEST(CallTest, CreateDestroy_AssociateAudioSendReceiveStreams_SendFirst) {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100166 CallHelper call;
Niels Möller7d76a312018-10-26 12:57:07 +0200167 AudioSendStream::Config send_config(/*send_transport=*/nullptr,
168 /*media_transport=*/nullptr);
solenberg7602aab2016-11-14 11:30:07 -0800169 send_config.rtp.ssrc = 777;
solenberg7602aab2016-11-14 11:30:07 -0800170 AudioSendStream* send_stream = call->CreateAudioSendStream(send_config);
171 EXPECT_NE(send_stream, nullptr);
172
173 AudioReceiveStream::Config recv_config;
Niels Möllerae4237e2018-10-05 11:28:38 +0200174 MockTransport rtcp_send_transport;
solenberg7602aab2016-11-14 11:30:07 -0800175 recv_config.rtp.remote_ssrc = 42;
176 recv_config.rtp.local_ssrc = 777;
Niels Möllerae4237e2018-10-05 11:28:38 +0200177 recv_config.rtcp_send_transport = &rtcp_send_transport;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100178 recv_config.decoder_factory =
179 new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>();
solenberg7602aab2016-11-14 11:30:07 -0800180 AudioReceiveStream* recv_stream = call->CreateAudioReceiveStream(recv_config);
181 EXPECT_NE(recv_stream, nullptr);
182
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100183 internal::AudioReceiveStream* internal_recv_stream =
184 static_cast<internal::AudioReceiveStream*>(recv_stream);
185 EXPECT_EQ(send_stream,
186 internal_recv_stream->GetAssociatedSendStreamForTesting());
187
solenberg7602aab2016-11-14 11:30:07 -0800188 call->DestroyAudioReceiveStream(recv_stream);
189
190 call->DestroyAudioSendStream(send_stream);
191}
192
brandtr25445d32016-10-23 23:37:14 -0700193TEST(CallTest, CreateDestroy_FlexfecReceiveStream) {
194 CallHelper call;
brandtr8313a6f2017-01-13 07:41:19 -0800195 MockTransport rtcp_send_transport;
196 FlexfecReceiveStream::Config config(&rtcp_send_transport);
brandtr1cfbd602016-12-08 04:17:53 -0800197 config.payload_type = 118;
198 config.remote_ssrc = 38837212;
brandtr25445d32016-10-23 23:37:14 -0700199 config.protected_media_ssrcs = {27273};
200
201 FlexfecReceiveStream* stream = call->CreateFlexfecReceiveStream(config);
202 EXPECT_NE(stream, nullptr);
203 call->DestroyFlexfecReceiveStream(stream);
204}
205
206TEST(CallTest, CreateDestroy_FlexfecReceiveStreams) {
207 CallHelper call;
brandtr8313a6f2017-01-13 07:41:19 -0800208 MockTransport rtcp_send_transport;
209 FlexfecReceiveStream::Config config(&rtcp_send_transport);
brandtr1cfbd602016-12-08 04:17:53 -0800210 config.payload_type = 118;
brandtr25445d32016-10-23 23:37:14 -0700211 std::list<FlexfecReceiveStream*> streams;
212
213 for (int i = 0; i < 2; ++i) {
214 for (uint32_t ssrc = 0; ssrc < 1234567; ssrc += 34567) {
brandtr1cfbd602016-12-08 04:17:53 -0800215 config.remote_ssrc = ssrc;
brandtr25445d32016-10-23 23:37:14 -0700216 config.protected_media_ssrcs = {ssrc + 1};
217 FlexfecReceiveStream* stream = call->CreateFlexfecReceiveStream(config);
218 EXPECT_NE(stream, nullptr);
219 if (ssrc & 1) {
220 streams.push_back(stream);
221 } else {
222 streams.push_front(stream);
223 }
224 }
225 for (auto s : streams) {
226 call->DestroyFlexfecReceiveStream(s);
227 }
228 streams.clear();
229 }
230}
231
232TEST(CallTest, MultipleFlexfecReceiveStreamsProtectingSingleVideoStream) {
233 CallHelper call;
brandtr8313a6f2017-01-13 07:41:19 -0800234 MockTransport rtcp_send_transport;
235 FlexfecReceiveStream::Config config(&rtcp_send_transport);
brandtr1cfbd602016-12-08 04:17:53 -0800236 config.payload_type = 118;
brandtr25445d32016-10-23 23:37:14 -0700237 config.protected_media_ssrcs = {1324234};
238 FlexfecReceiveStream* stream;
239 std::list<FlexfecReceiveStream*> streams;
240
brandtr1cfbd602016-12-08 04:17:53 -0800241 config.remote_ssrc = 838383;
brandtr25445d32016-10-23 23:37:14 -0700242 stream = call->CreateFlexfecReceiveStream(config);
243 EXPECT_NE(stream, nullptr);
244 streams.push_back(stream);
245
brandtr1cfbd602016-12-08 04:17:53 -0800246 config.remote_ssrc = 424993;
brandtr25445d32016-10-23 23:37:14 -0700247 stream = call->CreateFlexfecReceiveStream(config);
248 EXPECT_NE(stream, nullptr);
249 streams.push_back(stream);
250
brandtr1cfbd602016-12-08 04:17:53 -0800251 config.remote_ssrc = 99383;
brandtr25445d32016-10-23 23:37:14 -0700252 stream = call->CreateFlexfecReceiveStream(config);
253 EXPECT_NE(stream, nullptr);
254 streams.push_back(stream);
255
brandtr1cfbd602016-12-08 04:17:53 -0800256 config.remote_ssrc = 5548;
brandtr25445d32016-10-23 23:37:14 -0700257 stream = call->CreateFlexfecReceiveStream(config);
258 EXPECT_NE(stream, nullptr);
259 streams.push_back(stream);
260
261 for (auto s : streams) {
262 call->DestroyFlexfecReceiveStream(s);
263 }
264}
265
ossuc3d4b482017-05-23 06:07:11 -0700266TEST(CallTest, RecreatingAudioStreamWithSameSsrcReusesRtpState) {
267 constexpr uint32_t kSSRC = 12345;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100268 CallHelper call;
ossuc3d4b482017-05-23 06:07:11 -0700269
270 auto create_stream_and_get_rtp_state = [&](uint32_t ssrc) {
Niels Möller7d76a312018-10-26 12:57:07 +0200271 AudioSendStream::Config config(/*send_transport=*/nullptr,
272 /*media_transport=*/nullptr);
ossuc3d4b482017-05-23 06:07:11 -0700273 config.rtp.ssrc = ssrc;
ossuc3d4b482017-05-23 06:07:11 -0700274 AudioSendStream* stream = call->CreateAudioSendStream(config);
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100275 const RtpState rtp_state =
276 static_cast<internal::AudioSendStream*>(stream)->GetRtpState();
ossuc3d4b482017-05-23 06:07:11 -0700277 call->DestroyAudioSendStream(stream);
ossuc3d4b482017-05-23 06:07:11 -0700278 return rtp_state;
279 };
280
281 const RtpState rtp_state1 = create_stream_and_get_rtp_state(kSSRC);
282 const RtpState rtp_state2 = create_stream_and_get_rtp_state(kSSRC);
283
284 EXPECT_EQ(rtp_state1.sequence_number, rtp_state2.sequence_number);
285 EXPECT_EQ(rtp_state1.start_timestamp, rtp_state2.start_timestamp);
286 EXPECT_EQ(rtp_state1.timestamp, rtp_state2.timestamp);
287 EXPECT_EQ(rtp_state1.capture_time_ms, rtp_state2.capture_time_ms);
288 EXPECT_EQ(rtp_state1.last_timestamp_time_ms,
289 rtp_state2.last_timestamp_time_ms);
290 EXPECT_EQ(rtp_state1.media_has_been_sent, rtp_state2.media_has_been_sent);
291}
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100292
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800293TEST(CallTest, RegisterMediaTransportBitrateCallbacksInCreateStream) {
294 CallHelper call;
295 MediaTransportSettings settings;
296 webrtc::FakeMediaTransport fake_media_transport(settings);
297
298 EXPECT_EQ(0, fake_media_transport.target_rate_observers_size());
299 AudioSendStream::Config config(/*send_transport=*/nullptr,
300 /*media_transport=*/&fake_media_transport);
301
302 call->MediaTransportChange(&fake_media_transport);
303 AudioSendStream* stream = call->CreateAudioSendStream(config);
304
305 // We get 2 subscribers: one subscriber from call.cc, and one from
306 // ChannelSend.
307 EXPECT_EQ(2, fake_media_transport.target_rate_observers_size());
308
309 call->DestroyAudioSendStream(stream);
310 EXPECT_EQ(1, fake_media_transport.target_rate_observers_size());
311
312 call->MediaTransportChange(nullptr);
313 EXPECT_EQ(0, fake_media_transport.target_rate_observers_size());
314}
315
solenbergc7a8b082015-10-16 14:35:07 -0700316} // namespace webrtc