Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2017 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 <memory> | |
| 12 | |
| 13 #include "webrtc/api/ortc/ortcfactoryinterface.h" | |
| 14 #include "webrtc/base/criticalsection.h" | |
| 15 #include "webrtc/base/fakenetwork.h" | |
| 16 #include "webrtc/base/gunit.h" | |
| 17 #include "webrtc/base/physicalsocketserver.h" | |
| 18 #include "webrtc/base/virtualsocketserver.h" | |
| 19 #include "webrtc/p2p/base/udptransport.h" | |
| 20 #include "webrtc/pc/test/fakeaudiocapturemodule.h" | |
| 21 #include "webrtc/pc/test/fakeperiodicvideocapturer.h" | |
| 22 #include "webrtc/pc/test/fakevideotrackrenderer.h" | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 const int kDefaultTimeout = 10000; // 10 seconds. | |
|
pthatcher1
2017/02/10 22:41:13
Any particular reason?
Taylor Brandstetter
2017/02/14 06:55:05
It's just what we've been generally using.
| |
| 27 static const rtc::IPAddress kIPv4LocalHostAddress = | |
| 28 rtc::IPAddress(0x7F000001); // 127.0.0.1 | |
| 29 | |
| 30 class PacketReceiver : public sigslot::has_slots<> { | |
| 31 public: | |
| 32 explicit PacketReceiver(rtc::PacketTransportInternal* transport) { | |
| 33 transport->SignalReadPacket.connect(this, &PacketReceiver::OnReadPacket); | |
| 34 } | |
| 35 int packets_read() const { | |
|
pthatcher1
2017/02/10 22:41:13
I think I would call this something like count_rec
Taylor Brandstetter
2017/02/14 06:55:05
The signal is named "OnReadPacket", and it's used
| |
| 36 rtc::CritScope cs(&critsec_); | |
| 37 return packets_read_; | |
| 38 } | |
| 39 | |
| 40 private: | |
| 41 void OnReadPacket(rtc::PacketTransportInternal*, | |
| 42 const char*, | |
| 43 size_t, | |
| 44 const rtc::PacketTime&, | |
| 45 int) { | |
| 46 rtc::CritScope cs(&critsec_); | |
| 47 ++packets_read_; | |
| 48 } | |
| 49 | |
| 50 int packets_read_ = 0; | |
| 51 rtc::CriticalSection critsec_; | |
| 52 }; | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 56 namespace webrtc { | |
| 57 | |
| 58 // Used to test that things work end-to-end when using the default | |
| 59 // implementations of threads/etc. provided by OrtcFactory, with the exception | |
| 60 // of using a virtual network. | |
| 61 // | |
| 62 // By default, the virtual network manager doesn't enumerate any networks, but | |
| 63 // sockets can still be created in this state. | |
| 64 class OrtcFactoryTest : public testing::Test { | |
| 65 public: | |
| 66 OrtcFactoryTest() | |
| 67 : virtual_socket_server_(&physical_socket_server_), | |
| 68 network_thread_(&virtual_socket_server_), | |
| 69 fake_audio_capture_module1_(FakeAudioCaptureModule::Create()), | |
| 70 fake_audio_capture_module2_(FakeAudioCaptureModule::Create()) { | |
| 71 // Sockets are bound to the ANY address, so this is needed to tell the | |
| 72 // virtual network which address to use in this case. | |
| 73 virtual_socket_server_.SetDefaultRoute(kIPv4LocalHostAddress); | |
| 74 network_thread_.Start(); | |
| 75 // Need to create after network thread is started. | |
| 76 ortc_factory1_ = OrtcFactoryInterface::Create( | |
| 77 &network_thread_, nullptr, &fake_network_manager_, | |
| 78 nullptr, fake_audio_capture_module1_) | |
| 79 .ConsumeValue(); | |
| 80 ortc_factory2_ = OrtcFactoryInterface::Create( | |
| 81 &network_thread_, nullptr, &fake_network_manager_, | |
| 82 nullptr, fake_audio_capture_module2_) | |
| 83 .ConsumeValue(); | |
| 84 } | |
| 85 | |
| 86 protected: | |
| 87 // Ends up using fake audio capture module, which was passed into OrtcFactory | |
| 88 // on creation. | |
| 89 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack( | |
| 90 const std::string& id, | |
| 91 OrtcFactoryInterface* ortc_factory) { | |
| 92 // Disable echo cancellation to make test more efficient. | |
| 93 cricket::AudioOptions options; | |
| 94 options.echo_cancellation.emplace(true); | |
| 95 rtc::scoped_refptr<webrtc::AudioSourceInterface> source = | |
| 96 ortc_factory->CreateAudioSource(options); | |
| 97 return ortc_factory->CreateAudioTrack(id, source); | |
| 98 } | |
| 99 | |
| 100 // Stores created capturer in |fake_video_capturers_|. | |
| 101 rtc::scoped_refptr<webrtc::VideoTrackInterface> | |
| 102 CreateLocalVideoTrackAndFakeCapturer(const std::string& id, | |
| 103 OrtcFactoryInterface* ortc_factory) { | |
| 104 cricket::FakeVideoCapturer* fake_capturer = | |
| 105 new webrtc::FakePeriodicVideoCapturer(); | |
| 106 fake_video_capturers_.push_back(fake_capturer); | |
| 107 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source = | |
| 108 ortc_factory->CreateVideoSource( | |
| 109 std::unique_ptr<cricket::VideoCapturer>(fake_capturer)); | |
| 110 return rtc::scoped_refptr<webrtc::VideoTrackInterface>( | |
| 111 ortc_factory->CreateVideoTrack(id, source)); | |
| 112 } | |
| 113 | |
| 114 rtc::PhysicalSocketServer physical_socket_server_; | |
| 115 rtc::VirtualSocketServer virtual_socket_server_; | |
| 116 rtc::Thread network_thread_; | |
| 117 rtc::FakeNetworkManager fake_network_manager_; | |
| 118 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module1_; | |
| 119 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module2_; | |
| 120 std::unique_ptr<OrtcFactoryInterface> ortc_factory1_; | |
| 121 std::unique_ptr<OrtcFactoryInterface> ortc_factory2_; | |
| 122 // Actually owned by video tracks. | |
| 123 std::vector<cricket::FakeVideoCapturer*> fake_video_capturers_; | |
| 124 }; | |
| 125 | |
| 126 TEST_F(OrtcFactoryTest, EndToEndUdpTransport) { | |
| 127 std::unique_ptr<UdpTransportInterface> transport1 = | |
| 128 ortc_factory1_->CreateUdpTransport(AF_INET).ConsumeValue(); | |
| 129 std::unique_ptr<UdpTransportInterface> transport2 = | |
| 130 ortc_factory2_->CreateUdpTransport(AF_INET).ConsumeValue(); | |
| 131 // Sockets are bound to the ANY address, so we need to provide the IP address | |
| 132 // explicitly. | |
| 133 transport1->SetRemoteAddress( | |
| 134 rtc::SocketAddress(virtual_socket_server_.GetDefaultRoute(AF_INET), | |
| 135 transport2->GetLocalAddress().port())); | |
| 136 transport2->SetRemoteAddress( | |
| 137 rtc::SocketAddress(virtual_socket_server_.GetDefaultRoute(AF_INET), | |
| 138 transport1->GetLocalAddress().port())); | |
| 139 | |
| 140 // TODO(deadbeef): Once there's something (RTP senders/receivers) that can | |
| 141 // use UdpTransport end-to-end, use that for this end-to-end test instead of | |
| 142 // making assumptions about the implementation. | |
| 143 // | |
| 144 // For now, this assumes the returned object is a UdpTransportProxy that wraps | |
| 145 // a UdpTransport. | |
| 146 cricket::UdpTransport* internal_transport1 = | |
| 147 static_cast<cricket::UdpTransport*>(transport1->GetInternal()); | |
| 148 cricket::UdpTransport* internal_transport2 = | |
| 149 static_cast<cricket::UdpTransport*>(transport2->GetInternal()); | |
| 150 PacketReceiver receiver1(internal_transport1); | |
| 151 PacketReceiver receiver2(internal_transport2); | |
| 152 // Need to call internal "SendPacket" method on network thread. | |
| 153 network_thread_.Invoke<void>( | |
| 154 RTC_FROM_HERE, [internal_transport1, internal_transport2]() { | |
| 155 internal_transport1->SendPacket("foo", sizeof("foo"), | |
| 156 rtc::PacketOptions(), 0); | |
| 157 internal_transport2->SendPacket("foo", sizeof("foo"), | |
| 158 rtc::PacketOptions(), 0); | |
|
pthatcher1
2017/02/10 22:41:13
Just to be safe, you might want to make them send
Taylor Brandstetter
2017/02/14 06:55:05
Done.
| |
| 159 }); | |
| 160 EXPECT_EQ_WAIT(1, receiver1.packets_read(), kDefaultTimeout); | |
| 161 EXPECT_EQ_WAIT(1, receiver2.packets_read(), kDefaultTimeout); | |
| 162 } | |
| 163 | |
| 164 // Very basic end-to-end test with a single pair of audio RTP sender and | |
| 165 // receiver. | |
| 166 // | |
| 167 // Uses muxed RTCP, and minimal parameters with a hard-coded config that's | |
| 168 // known to work. | |
| 169 TEST_F(OrtcFactoryTest, UnidirectionalAudioRtpSenderAndReceiver) { | |
| 170 // Start by creating underlying UDP transports. | |
| 171 std::unique_ptr<UdpTransportInterface> sender_udp_transport = | |
| 172 ortc_factory1_->CreateUdpTransport(AF_INET).ConsumeValue(); | |
| 173 std::unique_ptr<UdpTransportInterface> receiver_udp_transport = | |
| 174 ortc_factory2_->CreateUdpTransport(AF_INET).ConsumeValue(); | |
| 175 // Sockets are bound to the ANY address, so we need to provide the IP address | |
| 176 // explicitly. | |
| 177 sender_udp_transport->SetRemoteAddress( | |
| 178 rtc::SocketAddress(virtual_socket_server_.GetDefaultRoute(AF_INET), | |
| 179 receiver_udp_transport->GetLocalAddress().port())); | |
| 180 receiver_udp_transport->SetRemoteAddress( | |
| 181 rtc::SocketAddress(virtual_socket_server_.GetDefaultRoute(AF_INET), | |
| 182 sender_udp_transport->GetLocalAddress().port())); | |
| 183 | |
| 184 // Create RTP transports. | |
| 185 RtcpParameters rtcp_parameters; | |
| 186 rtcp_parameters.mux = true; | |
| 187 std::unique_ptr<RtpTransportInterface> sender_rtp_transport = | |
| 188 ortc_factory1_ | |
| 189 ->CreateRtpTransport(rtcp_parameters, sender_udp_transport.get(), | |
| 190 nullptr, nullptr) | |
| 191 .ConsumeValue(); | |
| 192 std::unique_ptr<RtpTransportInterface> receiver_rtp_transport = | |
| 193 ortc_factory2_ | |
| 194 ->CreateRtpTransport(rtcp_parameters, receiver_udp_transport.get(), | |
| 195 nullptr, nullptr) | |
| 196 .ConsumeValue(); | |
| 197 | |
| 198 RtpParameters parameters; | |
| 199 RtpCodecParameters opus_codec; | |
| 200 opus_codec.name = "opus"; | |
| 201 opus_codec.kind = cricket::MEDIA_TYPE_AUDIO; | |
| 202 opus_codec.payload_type = 111; | |
| 203 opus_codec.clock_rate.emplace(48000); | |
| 204 opus_codec.num_channels.emplace(2); | |
| 205 parameters.codecs.push_back(std::move(opus_codec)); | |
| 206 RtpEncodingParameters encoding; | |
| 207 encoding.ssrc.emplace(0xdeadbeef); | |
|
pthatcher1
2017/02/10 22:41:13
:)
| |
| 208 encoding.codec_payload_type.emplace(111); | |
| 209 parameters.encodings.push_back(std::move(encoding)); | |
| 210 | |
| 211 auto sender_result = ortc_factory1_->CreateRtpSender( | |
| 212 cricket::MEDIA_TYPE_AUDIO, sender_rtp_transport.get()); | |
| 213 auto receiver_result = ortc_factory2_->CreateRtpReceiver( | |
| 214 cricket::MEDIA_TYPE_AUDIO, receiver_rtp_transport.get()); | |
| 215 ASSERT_TRUE(sender_result.ok()); | |
| 216 ASSERT_TRUE(receiver_result.ok()); | |
| 217 | |
| 218 auto sender = sender_result.ConsumeValue(); | |
| 219 auto receiver = receiver_result.ConsumeValue(); | |
| 220 EXPECT_TRUE(receiver->Receive(parameters).ok()); | |
| 221 EXPECT_TRUE( | |
| 222 sender->SetTrack(CreateLocalAudioTrack("audio", ortc_factory1_.get())) | |
| 223 .ok()); | |
| 224 EXPECT_TRUE(sender->Send(parameters).ok()); | |
| 225 // Sender and receiver are connected and configured; audio frames should be | |
| 226 // able to flow at this point. | |
| 227 EXPECT_TRUE_WAIT(fake_audio_capture_module2_->frames_received() > 10, | |
| 228 kDefaultTimeout); | |
| 229 } | |
| 230 | |
| 231 // Very basic end-to-end test with a single pair of video RTP sender and | |
| 232 // receiver. | |
| 233 // | |
| 234 // Uses muxed RTCP, and minimal parameters with a hard-coded config that's | |
| 235 // known to work. | |
| 236 TEST_F(OrtcFactoryTest, UnidirectionalVideoRtpSenderAndReceiver) { | |
| 237 // Start by creating underlying UDP transports. | |
| 238 std::unique_ptr<UdpTransportInterface> sender_udp_transport = | |
| 239 ortc_factory1_->CreateUdpTransport(AF_INET).ConsumeValue(); | |
| 240 std::unique_ptr<UdpTransportInterface> receiver_udp_transport = | |
| 241 ortc_factory2_->CreateUdpTransport(AF_INET).ConsumeValue(); | |
| 242 // Sockets are bound to the ANY address, so we need to provide the IP address | |
| 243 // explicitly. | |
| 244 sender_udp_transport->SetRemoteAddress( | |
| 245 rtc::SocketAddress(virtual_socket_server_.GetDefaultRoute(AF_INET), | |
| 246 receiver_udp_transport->GetLocalAddress().port())); | |
| 247 receiver_udp_transport->SetRemoteAddress( | |
| 248 rtc::SocketAddress(virtual_socket_server_.GetDefaultRoute(AF_INET), | |
| 249 sender_udp_transport->GetLocalAddress().port())); | |
| 250 | |
| 251 // Create RTP transports. | |
| 252 RtcpParameters rtcp_parameters; | |
| 253 rtcp_parameters.mux = true; | |
| 254 std::unique_ptr<RtpTransportInterface> sender_rtp_transport = | |
| 255 ortc_factory1_ | |
| 256 ->CreateRtpTransport(rtcp_parameters, sender_udp_transport.get(), | |
| 257 nullptr, nullptr) | |
| 258 .ConsumeValue(); | |
| 259 std::unique_ptr<RtpTransportInterface> receiver_rtp_transport = | |
| 260 ortc_factory2_ | |
| 261 ->CreateRtpTransport(rtcp_parameters, receiver_udp_transport.get(), | |
| 262 nullptr, nullptr) | |
| 263 .ConsumeValue(); | |
| 264 | |
| 265 RtpParameters parameters; | |
| 266 RtpCodecParameters vp8_codec; | |
| 267 vp8_codec.name = "VP8"; | |
| 268 vp8_codec.kind = cricket::MEDIA_TYPE_VIDEO; | |
| 269 vp8_codec.payload_type = 111; | |
| 270 parameters.codecs.push_back(std::move(vp8_codec)); | |
| 271 RtpEncodingParameters encoding; | |
| 272 encoding.ssrc.emplace(0xdeadbeef); | |
| 273 encoding.codec_payload_type.emplace(111); | |
| 274 parameters.encodings.push_back(std::move(encoding)); | |
| 275 | |
| 276 auto sender_result = ortc_factory1_->CreateRtpSender( | |
| 277 cricket::MEDIA_TYPE_VIDEO, sender_rtp_transport.get()); | |
| 278 auto receiver_result = ortc_factory2_->CreateRtpReceiver( | |
| 279 cricket::MEDIA_TYPE_VIDEO, receiver_rtp_transport.get()); | |
| 280 ASSERT_TRUE(sender_result.ok()); | |
| 281 ASSERT_TRUE(receiver_result.ok()); | |
| 282 | |
| 283 auto sender = sender_result.ConsumeValue(); | |
| 284 auto receiver = receiver_result.ConsumeValue(); | |
| 285 EXPECT_TRUE(receiver->Receive(parameters).ok()); | |
| 286 FakeVideoTrackRenderer fake_renderer( | |
| 287 static_cast<VideoTrackInterface*>(receiver->GetTrack().get())); | |
| 288 EXPECT_TRUE(sender | |
| 289 ->SetTrack(CreateLocalVideoTrackAndFakeCapturer( | |
| 290 "video", ortc_factory1_.get())) | |
| 291 .ok()); | |
| 292 EXPECT_TRUE(sender->Send(parameters).ok()); | |
| 293 // Sender and receiver are connected and configured; video frames should be | |
| 294 // able to flow at this point. | |
| 295 EXPECT_TRUE_WAIT(fake_renderer.num_rendered_frames() > 10, kDefaultTimeout); | |
| 296 } | |
| 297 | |
| 298 } // namespace webrtc | |
| OLD | NEW |