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