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/ortcfactoryinterface.h" |
| 14 #include "webrtc/base/fakenetwork.h" |
| 15 #include "webrtc/base/gunit.h" |
| 16 #include "webrtc/base/physicalsocketserver.h" |
| 17 #include "webrtc/base/virtualsocketserver.h" |
| 18 #include "webrtc/p2p/base/udptransport.h" |
| 19 #include "webrtc/pc/test/fakeperiodicvideocapturer.h" |
| 20 #include "webrtc/pc/test/fakeaudiocapturemodule.h" |
| 21 |
| 22 namespace { |
| 23 |
| 24 const int kDefaultTimeout = 10000; // 10 seconds. |
| 25 static const rtc::IPAddress kIPv4LocalHostAddress = |
| 26 rtc::IPAddress(0x7F000001); // 127.0.0.1 |
| 27 |
| 28 class PacketReceiver : public sigslot::has_slots<> { |
| 29 public: |
| 30 explicit PacketReceiver(rtc::PacketTransportInternal* transport) { |
| 31 transport->SignalReadPacket.connect(this, &PacketReceiver::OnReadPacket); |
| 32 } |
| 33 int packets_read() const { return packets_read_; } |
| 34 |
| 35 private: |
| 36 void OnReadPacket(rtc::PacketTransportInternal*, |
| 37 const char*, |
| 38 size_t, |
| 39 const rtc::PacketTime&, |
| 40 int) { |
| 41 ++packets_read_; |
| 42 } |
| 43 |
| 44 int packets_read_ = 0; |
| 45 }; |
| 46 |
| 47 } // namespace |
| 48 |
| 49 namespace webrtc { |
| 50 |
| 51 // Used to test that things work end-to-end when using the default |
| 52 // implementations of threads/etc. provided by OrtcFactory, with the exception |
| 53 // of using a virtual network. |
| 54 // |
| 55 // By default, the virtual network manager doesn't enumerate any networks, but |
| 56 // sockets can still be created in this state. |
| 57 class OrtcFactoryTest : public testing::Test { |
| 58 public: |
| 59 OrtcFactoryTest() |
| 60 : virtual_socket_server_(&physical_socket_server_), |
| 61 network_thread_(&virtual_socket_server_), |
| 62 fake_audio_capture_module1_(FakeAudioCaptureModule::Create()), |
| 63 fake_audio_capture_module2_(FakeAudioCaptureModule::Create()) { |
| 64 // Sockets are bound to the ANY address, so this is needed to tell the |
| 65 // virtual network which address to use in this case. |
| 66 virtual_socket_server_.SetDefaultRoute(kIPv4LocalHostAddress); |
| 67 network_thread_.Start(); |
| 68 // Need to create after network thread is started. |
| 69 ortc_factory1_ = OrtcFactoryInterface::Create( |
| 70 &network_thread_, nullptr, &fake_network_manager_, |
| 71 nullptr, fake_audio_capture_module1_) |
| 72 .moved_value(); |
| 73 ortc_factory2_ = OrtcFactoryInterface::Create( |
| 74 &network_thread_, nullptr, &fake_network_manager_, |
| 75 nullptr, fake_audio_capture_module2_) |
| 76 .moved_value(); |
| 77 } |
| 78 |
| 79 protected: |
| 80 // Ends up using fake audio capture module, which was passed into OrtcFactory |
| 81 // on creation. |
| 82 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack( |
| 83 const std::string& id, |
| 84 OrtcFactoryInterface* ortc_factory) { |
| 85 // Disable echo cancellation to make test more efficient. |
| 86 cricket::AudioOptions options; |
| 87 options.echo_cancellation.emplace(true); |
| 88 rtc::scoped_refptr<webrtc::AudioSourceInterface> source = |
| 89 ortc_factory->CreateAudioSource(options); |
| 90 return ortc_factory->CreateAudioTrack(id, source); |
| 91 } |
| 92 |
| 93 // Stores created capturer in |fake_video_capturers_|. |
| 94 rtc::scoped_refptr<webrtc::VideoTrackInterface> |
| 95 CreateLocalVideoTrackAndFakeCapturer(const std::string& id, |
| 96 OrtcFactoryInterface* ortc_factory) { |
| 97 cricket::FakeVideoCapturer* fake_capturer = |
| 98 new webrtc::FakePeriodicVideoCapturer(); |
| 99 fake_video_capturers_.push_back(fake_capturer); |
| 100 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source = |
| 101 ortc_factory->CreateVideoSource( |
| 102 std::unique_ptr<cricket::VideoCapturer>(fake_capturer)); |
| 103 return rtc::scoped_refptr<webrtc::VideoTrackInterface>( |
| 104 ortc_factory->CreateVideoTrack(id, source)); |
| 105 } |
| 106 |
| 107 rtc::PhysicalSocketServer physical_socket_server_; |
| 108 rtc::VirtualSocketServer virtual_socket_server_; |
| 109 rtc::Thread network_thread_; |
| 110 rtc::FakeNetworkManager fake_network_manager_; |
| 111 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module1_; |
| 112 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module2_; |
| 113 std::unique_ptr<OrtcFactoryInterface> ortc_factory1_; |
| 114 std::unique_ptr<OrtcFactoryInterface> ortc_factory2_; |
| 115 // Actually owned by video tracks. |
| 116 std::vector<cricket::FakeVideoCapturer*> fake_video_capturers_; |
| 117 }; |
| 118 |
| 119 TEST_F(OrtcFactoryTest, EndToEndUdpTransport) { |
| 120 std::unique_ptr<UdpTransportInterface> transport1 = |
| 121 ortc_factory1_->CreateUdpTransport(AF_INET).moved_value(); |
| 122 std::unique_ptr<UdpTransportInterface> transport2 = |
| 123 ortc_factory2_->CreateUdpTransport(AF_INET).moved_value(); |
| 124 // Sockets are bound to the ANY address, so we need to provide the IP address |
| 125 // explicitly. |
| 126 transport1->SetRemoteAddress( |
| 127 rtc::SocketAddress(virtual_socket_server_.GetDefaultRoute(AF_INET), |
| 128 transport2->GetLocalAddress().port())); |
| 129 transport2->SetRemoteAddress( |
| 130 rtc::SocketAddress(virtual_socket_server_.GetDefaultRoute(AF_INET), |
| 131 transport1->GetLocalAddress().port())); |
| 132 |
| 133 // TODO(deadbeef): Once there's something (RTP senders/receivers) that can |
| 134 // use UdpTransport end-to-end, use that for this end-to-end test instead of |
| 135 // making assumptions about the implementation. |
| 136 // |
| 137 // For now, this assumes the returned object is a UdpTransportProxy that wraps |
| 138 // a UdpTransport. |
| 139 cricket::UdpTransport* internal_transport1 = |
| 140 static_cast<cricket::UdpTransport*>(transport1->GetInternal()); |
| 141 cricket::UdpTransport* internal_transport2 = |
| 142 static_cast<cricket::UdpTransport*>(transport2->GetInternal()); |
| 143 // Need to call internal "SendPacket" method on network thread. |
| 144 network_thread_.Invoke<void>( |
| 145 RTC_FROM_HERE, [internal_transport1, internal_transport2]() { |
| 146 PacketReceiver receiver1(internal_transport1); |
| 147 PacketReceiver receiver2(internal_transport2); |
| 148 internal_transport1->SendPacket("foo", sizeof("foo"), |
| 149 rtc::PacketOptions(), 0); |
| 150 internal_transport2->SendPacket("foo", sizeof("foo"), |
| 151 rtc::PacketOptions(), 0); |
| 152 EXPECT_EQ_WAIT(1, receiver1.packets_read(), kDefaultTimeout); |
| 153 EXPECT_EQ_WAIT(1, receiver2.packets_read(), kDefaultTimeout); |
| 154 }); |
| 155 } |
| 156 |
| 157 // Very basic end-to-end test with a single pair of audio RTP sender and |
| 158 // receiver. |
| 159 // |
| 160 // Uses muxed RTCP, and minimal parameters with a hard-coded config that's |
| 161 // known to work. |
| 162 TEST_F(OrtcFactoryTest, UnidirectionalAudioRtpSenderAndReceiver) { |
| 163 // Start by creating underlying UDP transports. |
| 164 std::unique_ptr<UdpTransportInterface> sender_udp_transport = |
| 165 ortc_factory1_->CreateUdpTransport(AF_INET).moved_value(); |
| 166 std::unique_ptr<UdpTransportInterface> receiver_udp_transport = |
| 167 ortc_factory2_->CreateUdpTransport(AF_INET).moved_value(); |
| 168 // Sockets are bound to the ANY address, so we need to provide the IP address |
| 169 // explicitly. |
| 170 sender_udp_transport->SetRemoteAddress( |
| 171 rtc::SocketAddress(virtual_socket_server_.GetDefaultRoute(AF_INET), |
| 172 receiver_udp_transport->GetLocalAddress().port())); |
| 173 receiver_udp_transport->SetRemoteAddress( |
| 174 rtc::SocketAddress(virtual_socket_server_.GetDefaultRoute(AF_INET), |
| 175 sender_udp_transport->GetLocalAddress().port())); |
| 176 |
| 177 // Create RTP transports. |
| 178 RtcpParameters rtcp_parameters; |
| 179 rtcp_parameters.mux = true; |
| 180 std::unique_ptr<RtpTransportControllerInterface> |
| 181 sender_rtp_transport_controller = |
| 182 ortc_factory1_->CreateRtpTransportController().moved_value(); |
| 183 std::unique_ptr<RtpTransportInterface> sender_rtp_transport = |
| 184 ortc_factory1_ |
| 185 ->CreateRtpTransport(rtcp_parameters, sender_udp_transport.get(), |
| 186 nullptr, sender_rtp_transport_controller.get()) |
| 187 .moved_value(); |
| 188 std::unique_ptr<RtpTransportControllerInterface> |
| 189 receiver_rtp_transport_controller = |
| 190 ortc_factory2_->CreateRtpTransportController().moved_value(); |
| 191 std::unique_ptr<RtpTransportInterface> receiver_rtp_transport = |
| 192 ortc_factory2_ |
| 193 ->CreateRtpTransport(rtcp_parameters, receiver_udp_transport.get(), |
| 194 nullptr, receiver_rtp_transport_controller.get()) |
| 195 .moved_value(); |
| 196 |
| 197 RtpParameters parameters; |
| 198 RtpCodecParameters opus_codec; |
| 199 opus_codec.name = "opus"; |
| 200 opus_codec.kind = cricket::MEDIA_TYPE_AUDIO; |
| 201 opus_codec.payload_type = 111; |
| 202 opus_codec.clock_rate.emplace(48000); |
| 203 opus_codec.num_channels.emplace(2); |
| 204 parameters.codecs.push_back(std::move(opus_codec)); |
| 205 RtpEncodingParameters encoding; |
| 206 encoding.ssrc.emplace(0xdeadbeef); |
| 207 encoding.codec_payload_type.emplace(111); |
| 208 parameters.encodings.push_back(std::move(encoding)); |
| 209 |
| 210 auto receiver_result = ortc_factory2_->CreateRtpReceiver( |
| 211 cricket::MEDIA_TYPE_AUDIO, parameters, receiver_rtp_transport.get()); |
| 212 ASSERT_TRUE(receiver_result.ok()); |
| 213 auto sender_result = ortc_factory1_->CreateRtpSender( |
| 214 cricket::MEDIA_TYPE_AUDIO, parameters, sender_rtp_transport.get()); |
| 215 ASSERT_TRUE(sender_result.ok()); |
| 216 |
| 217 auto sender = sender_result.moved_value(); |
| 218 auto receiver = receiver_result.moved_value(); |
| 219 sender->SetTrack(CreateLocalAudioTrack("audio", ortc_factory1_.get())); |
| 220 // Sender and receiver are connected and configured; audio frames should be |
| 221 // able to flow at this point. |
| 222 EXPECT_TRUE_WAIT(fake_audio_capture_module2_->frames_received() > 10, |
| 223 kDefaultTimeout); |
| 224 } |
| 225 |
| 226 } // namespace webrtc |
OLD | NEW |