deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 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 | |
Karl Wiberg | 3e9e5b3 | 2017-11-06 05:01:56 +0100 | [diff] [blame] | 13 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
| 14 | #include "api/audio_codecs/builtin_audio_encoder_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "media/base/fakemediaengine.h" |
| 16 | #include "ortc/ortcfactory.h" |
| 17 | #include "ortc/testrtpparameters.h" |
| 18 | #include "p2p/base/fakepackettransport.h" |
| 19 | #include "rtc_base/gunit.h" |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | // This test uses fake packet transports and a fake media engine, in order to |
| 24 | // test the RtpTransportController at only an API level. Any end-to-end test |
| 25 | // should go in ortcfactory_integrationtest.cc instead. |
| 26 | // |
| 27 | // Currently, this test mainly focuses on the limitations of the "adapter" |
| 28 | // RtpTransportController implementation. Only one of each type of |
| 29 | // sender/receiver can be created, and the sender/receiver of the same media |
| 30 | // type must use the same transport. |
| 31 | class RtpTransportControllerTest : public testing::Test { |
| 32 | public: |
| 33 | RtpTransportControllerTest() { |
| 34 | // Note: This doesn't need to use fake network classes, since it uses |
| 35 | // FakePacketTransports. |
Karl Wiberg | 3e9e5b3 | 2017-11-06 05:01:56 +0100 | [diff] [blame] | 36 | auto result = OrtcFactory::Create( |
| 37 | nullptr, nullptr, nullptr, nullptr, nullptr, |
| 38 | std::unique_ptr<cricket::MediaEngineInterface>( |
| 39 | new cricket::FakeMediaEngine()), |
| 40 | CreateBuiltinAudioEncoderFactory(), CreateBuiltinAudioDecoderFactory()); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 41 | ortc_factory_ = result.MoveValue(); |
| 42 | rtp_transport_controller_ = |
| 43 | ortc_factory_->CreateRtpTransportController().MoveValue(); |
| 44 | } |
| 45 | |
| 46 | protected: |
| 47 | std::unique_ptr<OrtcFactoryInterface> ortc_factory_; |
| 48 | std::unique_ptr<RtpTransportControllerInterface> rtp_transport_controller_; |
| 49 | }; |
| 50 | |
| 51 | TEST_F(RtpTransportControllerTest, GetTransports) { |
| 52 | rtc::FakePacketTransport packet_transport1("one"); |
| 53 | rtc::FakePacketTransport packet_transport2("two"); |
| 54 | |
| 55 | auto rtp_transport_result1 = ortc_factory_->CreateRtpTransport( |
| 56 | MakeRtcpMuxParameters(), &packet_transport1, nullptr, |
| 57 | rtp_transport_controller_.get()); |
| 58 | ASSERT_TRUE(rtp_transport_result1.ok()); |
| 59 | |
| 60 | auto rtp_transport_result2 = ortc_factory_->CreateRtpTransport( |
| 61 | MakeRtcpMuxParameters(), &packet_transport2, nullptr, |
| 62 | rtp_transport_controller_.get()); |
| 63 | ASSERT_TRUE(rtp_transport_result2.ok()); |
| 64 | |
| 65 | auto returned_transports = rtp_transport_controller_->GetTransports(); |
| 66 | ASSERT_EQ(2u, returned_transports.size()); |
| 67 | EXPECT_EQ(rtp_transport_result1.value().get(), returned_transports[0]); |
| 68 | EXPECT_EQ(rtp_transport_result2.value().get(), returned_transports[1]); |
| 69 | |
| 70 | // If a transport is deleted, it shouldn't be returned any more. |
| 71 | rtp_transport_result1.MoveValue().reset(); |
| 72 | returned_transports = rtp_transport_controller_->GetTransports(); |
| 73 | ASSERT_EQ(1u, returned_transports.size()); |
| 74 | EXPECT_EQ(rtp_transport_result2.value().get(), returned_transports[0]); |
| 75 | } |
| 76 | |
| 77 | // Create RtpSenders and RtpReceivers on top of RtpTransports controlled by the |
| 78 | // same RtpTransportController. Currently only one each of audio/video is |
| 79 | // supported. |
| 80 | TEST_F(RtpTransportControllerTest, AttachMultipleSendersAndReceivers) { |
| 81 | rtc::FakePacketTransport audio_packet_transport("audio"); |
| 82 | rtc::FakePacketTransport video_packet_transport("video"); |
| 83 | |
| 84 | auto audio_rtp_transport_result = ortc_factory_->CreateRtpTransport( |
| 85 | MakeRtcpMuxParameters(), &audio_packet_transport, nullptr, |
| 86 | rtp_transport_controller_.get()); |
| 87 | ASSERT_TRUE(audio_rtp_transport_result.ok()); |
| 88 | auto audio_rtp_transport = audio_rtp_transport_result.MoveValue(); |
| 89 | |
| 90 | auto video_rtp_transport_result = ortc_factory_->CreateRtpTransport( |
| 91 | MakeRtcpMuxParameters(), &video_packet_transport, nullptr, |
| 92 | rtp_transport_controller_.get()); |
| 93 | ASSERT_TRUE(video_rtp_transport_result.ok()); |
| 94 | auto video_rtp_transport = video_rtp_transport_result.MoveValue(); |
| 95 | |
| 96 | auto audio_sender_result = ortc_factory_->CreateRtpSender( |
| 97 | cricket::MEDIA_TYPE_AUDIO, audio_rtp_transport.get()); |
| 98 | EXPECT_TRUE(audio_sender_result.ok()); |
| 99 | auto audio_receiver_result = ortc_factory_->CreateRtpReceiver( |
| 100 | cricket::MEDIA_TYPE_AUDIO, audio_rtp_transport.get()); |
| 101 | EXPECT_TRUE(audio_receiver_result.ok()); |
| 102 | auto video_sender_result = ortc_factory_->CreateRtpSender( |
| 103 | cricket::MEDIA_TYPE_VIDEO, video_rtp_transport.get()); |
| 104 | EXPECT_TRUE(video_sender_result.ok()); |
| 105 | auto video_receiver_result = ortc_factory_->CreateRtpReceiver( |
| 106 | cricket::MEDIA_TYPE_VIDEO, video_rtp_transport.get()); |
| 107 | EXPECT_TRUE(video_receiver_result.ok()); |
| 108 | |
| 109 | // Now that we have one each of audio/video senders/receivers, trying to |
| 110 | // create more on top of the same controller is expected to fail. |
| 111 | // TODO(deadbeef): Update this test once multiple senders/receivers on top of |
| 112 | // the same controller is supported. |
| 113 | auto failed_sender_result = ortc_factory_->CreateRtpSender( |
| 114 | cricket::MEDIA_TYPE_AUDIO, audio_rtp_transport.get()); |
| 115 | EXPECT_EQ(RTCErrorType::UNSUPPORTED_OPERATION, |
| 116 | failed_sender_result.error().type()); |
| 117 | auto failed_receiver_result = ortc_factory_->CreateRtpReceiver( |
| 118 | cricket::MEDIA_TYPE_AUDIO, audio_rtp_transport.get()); |
| 119 | EXPECT_EQ(RTCErrorType::UNSUPPORTED_OPERATION, |
| 120 | failed_receiver_result.error().type()); |
| 121 | failed_sender_result = ortc_factory_->CreateRtpSender( |
| 122 | cricket::MEDIA_TYPE_VIDEO, video_rtp_transport.get()); |
| 123 | EXPECT_EQ(RTCErrorType::UNSUPPORTED_OPERATION, |
| 124 | failed_sender_result.error().type()); |
| 125 | failed_receiver_result = ortc_factory_->CreateRtpReceiver( |
| 126 | cricket::MEDIA_TYPE_VIDEO, video_rtp_transport.get()); |
| 127 | EXPECT_EQ(RTCErrorType::UNSUPPORTED_OPERATION, |
| 128 | failed_receiver_result.error().type()); |
| 129 | |
| 130 | // If we destroy the existing sender/receiver using a transport controller, |
| 131 | // we should be able to make a new one, despite the above limitation. |
| 132 | audio_sender_result.MoveValue().reset(); |
| 133 | audio_sender_result = ortc_factory_->CreateRtpSender( |
| 134 | cricket::MEDIA_TYPE_AUDIO, audio_rtp_transport.get()); |
| 135 | EXPECT_TRUE(audio_sender_result.ok()); |
| 136 | audio_receiver_result.MoveValue().reset(); |
| 137 | audio_receiver_result = ortc_factory_->CreateRtpReceiver( |
| 138 | cricket::MEDIA_TYPE_AUDIO, audio_rtp_transport.get()); |
| 139 | EXPECT_TRUE(audio_receiver_result.ok()); |
| 140 | video_sender_result.MoveValue().reset(); |
| 141 | video_sender_result = ortc_factory_->CreateRtpSender( |
| 142 | cricket::MEDIA_TYPE_VIDEO, video_rtp_transport.get()); |
| 143 | EXPECT_TRUE(video_sender_result.ok()); |
| 144 | video_receiver_result.MoveValue().reset(); |
| 145 | video_receiver_result = ortc_factory_->CreateRtpReceiver( |
| 146 | cricket::MEDIA_TYPE_VIDEO, video_rtp_transport.get()); |
| 147 | EXPECT_TRUE(video_receiver_result.ok()); |
| 148 | } |
| 149 | |
| 150 | // Given the current limitations of the BaseChannel-based implementation, it's |
| 151 | // not possible for an audio sender and receiver to use different RtpTransports. |
| 152 | // TODO(deadbeef): Once this is supported, update/replace this test. |
| 153 | TEST_F(RtpTransportControllerTest, |
| 154 | SenderAndReceiverUsingDifferentTransportsUnsupported) { |
| 155 | rtc::FakePacketTransport packet_transport1("one"); |
| 156 | rtc::FakePacketTransport packet_transport2("two"); |
| 157 | |
| 158 | auto rtp_transport_result1 = ortc_factory_->CreateRtpTransport( |
| 159 | MakeRtcpMuxParameters(), &packet_transport1, nullptr, |
| 160 | rtp_transport_controller_.get()); |
| 161 | ASSERT_TRUE(rtp_transport_result1.ok()); |
| 162 | auto rtp_transport1 = rtp_transport_result1.MoveValue(); |
| 163 | |
| 164 | auto rtp_transport_result2 = ortc_factory_->CreateRtpTransport( |
| 165 | MakeRtcpMuxParameters(), &packet_transport2, nullptr, |
| 166 | rtp_transport_controller_.get()); |
| 167 | ASSERT_TRUE(rtp_transport_result2.ok()); |
| 168 | auto rtp_transport2 = rtp_transport_result2.MoveValue(); |
| 169 | |
| 170 | // Create an audio sender on transport 1, then try to create a receiver on 2. |
| 171 | auto audio_sender_result = ortc_factory_->CreateRtpSender( |
| 172 | cricket::MEDIA_TYPE_AUDIO, rtp_transport1.get()); |
| 173 | EXPECT_TRUE(audio_sender_result.ok()); |
| 174 | auto audio_receiver_result = ortc_factory_->CreateRtpReceiver( |
| 175 | cricket::MEDIA_TYPE_AUDIO, rtp_transport2.get()); |
| 176 | EXPECT_EQ(RTCErrorType::UNSUPPORTED_OPERATION, |
| 177 | audio_receiver_result.error().type()); |
| 178 | // Delete the sender; now we should be ok to create the receiver on 2. |
| 179 | audio_sender_result.MoveValue().reset(); |
| 180 | audio_receiver_result = ortc_factory_->CreateRtpReceiver( |
| 181 | cricket::MEDIA_TYPE_AUDIO, rtp_transport2.get()); |
| 182 | EXPECT_TRUE(audio_receiver_result.ok()); |
| 183 | |
| 184 | // Do the same thing for video, reversing 1 and 2 (for variety). |
| 185 | auto video_sender_result = ortc_factory_->CreateRtpSender( |
| 186 | cricket::MEDIA_TYPE_VIDEO, rtp_transport2.get()); |
| 187 | EXPECT_TRUE(video_sender_result.ok()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 188 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 189 | auto video_receiver_result = ortc_factory_->CreateRtpReceiver( |
| 190 | cricket::MEDIA_TYPE_VIDEO, rtp_transport1.get()); |
| 191 | EXPECT_EQ(RTCErrorType::UNSUPPORTED_OPERATION, |
| 192 | video_receiver_result.error().type()); |
| 193 | video_sender_result.MoveValue().reset(); |
| 194 | video_receiver_result = ortc_factory_->CreateRtpReceiver( |
| 195 | cricket::MEDIA_TYPE_VIDEO, rtp_transport1.get()); |
| 196 | EXPECT_TRUE(video_receiver_result.ok()); |
| 197 | } |
| 198 | |
| 199 | } // namespace webrtc |