blob: 745de5923a8b29b6b5765348fbd0c536927e34f3 [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 Wibergf3850f62017-11-02 13:04:41 +010016#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/test/mock_audio_mixer.h"
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010018#include "audio/audio_send_stream.h"
19#include "audio/audio_receive_stream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "call/audio_state.h"
21#include "call/call.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "logging/rtc_event_log/rtc_event_log.h"
23#include "modules/audio_device/include/mock_audio_device.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010024#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/pacing/mock/mock_paced_sender.h"
26#include "modules/rtp_rtcp/include/rtp_rtcp.h"
27#include "rtc_base/ptr_util.h"
28#include "test/fake_encoder.h"
29#include "test/gtest.h"
30#include "test/mock_audio_decoder_factory.h"
31#include "test/mock_transport.h"
solenbergc7a8b082015-10-16 14:35:07 -070032
33namespace {
34
35struct CallHelper {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010036 CallHelper() {
solenberg566ef242015-11-06 15:34:49 -080037 webrtc::AudioState::Config audio_state_config;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010038 audio_state_config.audio_mixer =
39 new rtc::RefCountedObject<webrtc::test::MockAudioMixer>();
40 audio_state_config.audio_processing =
41 new rtc::RefCountedObject<webrtc::test::MockAudioProcessing>();
42 audio_state_config.audio_device_module =
43 new rtc::RefCountedObject<webrtc::test::MockAudioDeviceModule>();
skvlad11a9cbf2016-10-07 11:53:05 -070044 webrtc::Call::Config config(&event_log_);
solenberg566ef242015-11-06 15:34:49 -080045 config.audio_state = webrtc::AudioState::Create(audio_state_config);
solenbergc7a8b082015-10-16 14:35:07 -070046 call_.reset(webrtc::Call::Create(config));
47 }
48
49 webrtc::Call* operator->() { return call_.get(); }
50
51 private:
skvlad11a9cbf2016-10-07 11:53:05 -070052 webrtc::RtcEventLogNullImpl event_log_;
kwibergb25345e2016-03-12 06:10:44 -080053 std::unique_ptr<webrtc::Call> call_;
solenbergc7a8b082015-10-16 14:35:07 -070054};
55} // namespace
56
57namespace webrtc {
58
59TEST(CallTest, ConstructDestruct) {
60 CallHelper call;
61}
62
63TEST(CallTest, CreateDestroy_AudioSendStream) {
64 CallHelper call;
65 AudioSendStream::Config config(nullptr);
66 config.rtp.ssrc = 42;
solenbergc7a8b082015-10-16 14:35:07 -070067 AudioSendStream* stream = call->CreateAudioSendStream(config);
68 EXPECT_NE(stream, nullptr);
69 call->DestroyAudioSendStream(stream);
70}
71
72TEST(CallTest, CreateDestroy_AudioReceiveStream) {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010073 CallHelper call;
solenbergc7a8b082015-10-16 14:35:07 -070074 AudioReceiveStream::Config config;
75 config.rtp.remote_ssrc = 42;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010076 config.decoder_factory =
77 new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>();
solenbergc7a8b082015-10-16 14:35:07 -070078 AudioReceiveStream* stream = call->CreateAudioReceiveStream(config);
79 EXPECT_NE(stream, nullptr);
80 call->DestroyAudioReceiveStream(stream);
81}
82
83TEST(CallTest, CreateDestroy_AudioSendStreams) {
84 CallHelper call;
85 AudioSendStream::Config config(nullptr);
solenbergc7a8b082015-10-16 14:35:07 -070086 std::list<AudioSendStream*> streams;
87 for (int i = 0; i < 2; ++i) {
88 for (uint32_t ssrc = 0; ssrc < 1234567; ssrc += 34567) {
89 config.rtp.ssrc = ssrc;
90 AudioSendStream* stream = call->CreateAudioSendStream(config);
91 EXPECT_NE(stream, nullptr);
92 if (ssrc & 1) {
93 streams.push_back(stream);
94 } else {
95 streams.push_front(stream);
96 }
97 }
98 for (auto s : streams) {
99 call->DestroyAudioSendStream(s);
100 }
101 streams.clear();
102 }
103}
104
105TEST(CallTest, CreateDestroy_AudioReceiveStreams) {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100106 CallHelper call;
solenbergc7a8b082015-10-16 14:35:07 -0700107 AudioReceiveStream::Config config;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100108 config.decoder_factory =
109 new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>();
solenbergc7a8b082015-10-16 14:35:07 -0700110 std::list<AudioReceiveStream*> streams;
111 for (int i = 0; i < 2; ++i) {
112 for (uint32_t ssrc = 0; ssrc < 1234567; ssrc += 34567) {
113 config.rtp.remote_ssrc = ssrc;
114 AudioReceiveStream* stream = call->CreateAudioReceiveStream(config);
115 EXPECT_NE(stream, nullptr);
116 if (ssrc & 1) {
117 streams.push_back(stream);
118 } else {
119 streams.push_front(stream);
120 }
121 }
122 for (auto s : streams) {
123 call->DestroyAudioReceiveStream(s);
124 }
125 streams.clear();
126 }
127}
brandtr25445d32016-10-23 23:37:14 -0700128
solenberg7602aab2016-11-14 11:30:07 -0800129TEST(CallTest, CreateDestroy_AssociateAudioSendReceiveStreams_RecvFirst) {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100130 CallHelper call;
solenberg7602aab2016-11-14 11:30:07 -0800131 AudioReceiveStream::Config recv_config;
132 recv_config.rtp.remote_ssrc = 42;
133 recv_config.rtp.local_ssrc = 777;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100134 recv_config.decoder_factory =
135 new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>();
solenberg7602aab2016-11-14 11:30:07 -0800136 AudioReceiveStream* recv_stream = call->CreateAudioReceiveStream(recv_config);
137 EXPECT_NE(recv_stream, nullptr);
138
solenberg7602aab2016-11-14 11:30:07 -0800139 AudioSendStream::Config send_config(nullptr);
140 send_config.rtp.ssrc = 777;
solenberg7602aab2016-11-14 11:30:07 -0800141 AudioSendStream* send_stream = call->CreateAudioSendStream(send_config);
142 EXPECT_NE(send_stream, nullptr);
143
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100144 internal::AudioReceiveStream* internal_recv_stream =
145 static_cast<internal::AudioReceiveStream*>(recv_stream);
146 EXPECT_EQ(send_stream,
147 internal_recv_stream->GetAssociatedSendStreamForTesting());
solenberg7602aab2016-11-14 11:30:07 -0800148
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100149 call->DestroyAudioSendStream(send_stream);
150 EXPECT_EQ(nullptr, internal_recv_stream->GetAssociatedSendStreamForTesting());
151
solenberg7602aab2016-11-14 11:30:07 -0800152 call->DestroyAudioReceiveStream(recv_stream);
153}
154
155TEST(CallTest, CreateDestroy_AssociateAudioSendReceiveStreams_SendFirst) {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100156 CallHelper call;
solenberg7602aab2016-11-14 11:30:07 -0800157 AudioSendStream::Config send_config(nullptr);
158 send_config.rtp.ssrc = 777;
solenberg7602aab2016-11-14 11:30:07 -0800159 AudioSendStream* send_stream = call->CreateAudioSendStream(send_config);
160 EXPECT_NE(send_stream, nullptr);
161
162 AudioReceiveStream::Config recv_config;
163 recv_config.rtp.remote_ssrc = 42;
164 recv_config.rtp.local_ssrc = 777;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100165 recv_config.decoder_factory =
166 new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>();
solenberg7602aab2016-11-14 11:30:07 -0800167 AudioReceiveStream* recv_stream = call->CreateAudioReceiveStream(recv_config);
168 EXPECT_NE(recv_stream, nullptr);
169
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100170 internal::AudioReceiveStream* internal_recv_stream =
171 static_cast<internal::AudioReceiveStream*>(recv_stream);
172 EXPECT_EQ(send_stream,
173 internal_recv_stream->GetAssociatedSendStreamForTesting());
174
solenberg7602aab2016-11-14 11:30:07 -0800175 call->DestroyAudioReceiveStream(recv_stream);
176
177 call->DestroyAudioSendStream(send_stream);
178}
179
brandtr25445d32016-10-23 23:37:14 -0700180TEST(CallTest, CreateDestroy_FlexfecReceiveStream) {
181 CallHelper call;
brandtr8313a6f2017-01-13 07:41:19 -0800182 MockTransport rtcp_send_transport;
183 FlexfecReceiveStream::Config config(&rtcp_send_transport);
brandtr1cfbd602016-12-08 04:17:53 -0800184 config.payload_type = 118;
185 config.remote_ssrc = 38837212;
brandtr25445d32016-10-23 23:37:14 -0700186 config.protected_media_ssrcs = {27273};
187
188 FlexfecReceiveStream* stream = call->CreateFlexfecReceiveStream(config);
189 EXPECT_NE(stream, nullptr);
190 call->DestroyFlexfecReceiveStream(stream);
191}
192
193TEST(CallTest, CreateDestroy_FlexfecReceiveStreams) {
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;
brandtr25445d32016-10-23 23:37:14 -0700198 std::list<FlexfecReceiveStream*> streams;
199
200 for (int i = 0; i < 2; ++i) {
201 for (uint32_t ssrc = 0; ssrc < 1234567; ssrc += 34567) {
brandtr1cfbd602016-12-08 04:17:53 -0800202 config.remote_ssrc = ssrc;
brandtr25445d32016-10-23 23:37:14 -0700203 config.protected_media_ssrcs = {ssrc + 1};
204 FlexfecReceiveStream* stream = call->CreateFlexfecReceiveStream(config);
205 EXPECT_NE(stream, nullptr);
206 if (ssrc & 1) {
207 streams.push_back(stream);
208 } else {
209 streams.push_front(stream);
210 }
211 }
212 for (auto s : streams) {
213 call->DestroyFlexfecReceiveStream(s);
214 }
215 streams.clear();
216 }
217}
218
219TEST(CallTest, MultipleFlexfecReceiveStreamsProtectingSingleVideoStream) {
220 CallHelper call;
brandtr8313a6f2017-01-13 07:41:19 -0800221 MockTransport rtcp_send_transport;
222 FlexfecReceiveStream::Config config(&rtcp_send_transport);
brandtr1cfbd602016-12-08 04:17:53 -0800223 config.payload_type = 118;
brandtr25445d32016-10-23 23:37:14 -0700224 config.protected_media_ssrcs = {1324234};
225 FlexfecReceiveStream* stream;
226 std::list<FlexfecReceiveStream*> streams;
227
brandtr1cfbd602016-12-08 04:17:53 -0800228 config.remote_ssrc = 838383;
brandtr25445d32016-10-23 23:37:14 -0700229 stream = call->CreateFlexfecReceiveStream(config);
230 EXPECT_NE(stream, nullptr);
231 streams.push_back(stream);
232
brandtr1cfbd602016-12-08 04:17:53 -0800233 config.remote_ssrc = 424993;
brandtr25445d32016-10-23 23:37:14 -0700234 stream = call->CreateFlexfecReceiveStream(config);
235 EXPECT_NE(stream, nullptr);
236 streams.push_back(stream);
237
brandtr1cfbd602016-12-08 04:17:53 -0800238 config.remote_ssrc = 99383;
brandtr25445d32016-10-23 23:37:14 -0700239 stream = call->CreateFlexfecReceiveStream(config);
240 EXPECT_NE(stream, nullptr);
241 streams.push_back(stream);
242
brandtr1cfbd602016-12-08 04:17:53 -0800243 config.remote_ssrc = 5548;
brandtr25445d32016-10-23 23:37:14 -0700244 stream = call->CreateFlexfecReceiveStream(config);
245 EXPECT_NE(stream, nullptr);
246 streams.push_back(stream);
247
248 for (auto s : streams) {
249 call->DestroyFlexfecReceiveStream(s);
250 }
251}
252
zstein7cb69d52017-05-08 11:52:38 -0700253
ossuc3d4b482017-05-23 06:07:11 -0700254TEST(CallTest, RecreatingAudioStreamWithSameSsrcReusesRtpState) {
255 constexpr uint32_t kSSRC = 12345;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100256 CallHelper call;
ossuc3d4b482017-05-23 06:07:11 -0700257
258 auto create_stream_and_get_rtp_state = [&](uint32_t ssrc) {
259 AudioSendStream::Config config(nullptr);
260 config.rtp.ssrc = ssrc;
ossuc3d4b482017-05-23 06:07:11 -0700261 AudioSendStream* stream = call->CreateAudioSendStream(config);
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100262 const RtpState rtp_state =
263 static_cast<internal::AudioSendStream*>(stream)->GetRtpState();
ossuc3d4b482017-05-23 06:07:11 -0700264 call->DestroyAudioSendStream(stream);
ossuc3d4b482017-05-23 06:07:11 -0700265 return rtp_state;
266 };
267
268 const RtpState rtp_state1 = create_stream_and_get_rtp_state(kSSRC);
269 const RtpState rtp_state2 = create_stream_and_get_rtp_state(kSSRC);
270
271 EXPECT_EQ(rtp_state1.sequence_number, rtp_state2.sequence_number);
272 EXPECT_EQ(rtp_state1.start_timestamp, rtp_state2.start_timestamp);
273 EXPECT_EQ(rtp_state1.timestamp, rtp_state2.timestamp);
274 EXPECT_EQ(rtp_state1.capture_time_ms, rtp_state2.capture_time_ms);
275 EXPECT_EQ(rtp_state1.last_timestamp_time_ms,
276 rtp_state2.last_timestamp_time_ms);
277 EXPECT_EQ(rtp_state1.media_has_been_sent, rtp_state2.media_has_been_sent);
278}
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100279
ossuc3d4b482017-05-23 06:07:11 -0700280
solenbergc7a8b082015-10-16 14:35:07 -0700281} // namespace webrtc