Harald Alvestrand | e61d4c8 | 2021-09-16 08:59:11 +0000 | [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 | |
Harald Alvestrand | e61d4c8 | 2021-09-16 08:59:11 +0000 | [diff] [blame] | 11 | #include <memory> |
Harald Alvestrand | e61d4c8 | 2021-09-16 08:59:11 +0000 | [diff] [blame] | 12 | #include <utility> |
| 13 | #include <vector> |
| 14 | |
Harald Alvestrand | e61d4c8 | 2021-09-16 08:59:11 +0000 | [diff] [blame] | 15 | #include "api/audio/audio_mixer.h" |
Harald Alvestrand | e61d4c8 | 2021-09-16 08:59:11 +0000 | [diff] [blame] | 16 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
| 17 | #include "api/audio_codecs/builtin_audio_encoder_factory.h" |
| 18 | #include "api/create_peerconnection_factory.h" |
Harald Alvestrand | e61d4c8 | 2021-09-16 08:59:11 +0000 | [diff] [blame] | 19 | #include "api/media_types.h" |
| 20 | #include "api/peer_connection_interface.h" |
Harald Alvestrand | e61d4c8 | 2021-09-16 08:59:11 +0000 | [diff] [blame] | 21 | #include "api/rtp_transceiver_interface.h" |
| 22 | #include "api/scoped_refptr.h" |
Harald Alvestrand | e61d4c8 | 2021-09-16 08:59:11 +0000 | [diff] [blame] | 23 | #include "api/video_codecs/builtin_video_decoder_factory.h" |
| 24 | #include "api/video_codecs/builtin_video_encoder_factory.h" |
Harald Alvestrand | e61d4c8 | 2021-09-16 08:59:11 +0000 | [diff] [blame] | 25 | #include "modules/audio_device/include/audio_device.h" |
| 26 | #include "modules/audio_processing/include/audio_processing.h" |
| 27 | #include "p2p/base/port_allocator.h" |
Harald Alvestrand | e61d4c8 | 2021-09-16 08:59:11 +0000 | [diff] [blame] | 28 | #include "pc/peer_connection_wrapper.h" |
Harald Alvestrand | e61d4c8 | 2021-09-16 08:59:11 +0000 | [diff] [blame] | 29 | #include "pc/test/fake_audio_capture_module.h" |
| 30 | #include "pc/test/mock_peer_connection_observers.h" |
Harald Alvestrand | e61d4c8 | 2021-09-16 08:59:11 +0000 | [diff] [blame] | 31 | #include "rtc_base/rtc_certificate_generator.h" |
| 32 | #include "rtc_base/thread.h" |
| 33 | #include "system_wrappers/include/metrics.h" |
Harald Alvestrand | e61d4c8 | 2021-09-16 08:59:11 +0000 | [diff] [blame] | 34 | #include "test/gtest.h" |
| 35 | |
| 36 | // This file contains unit tests that relate to the behavior of the |
| 37 | // SdpOfferAnswer module. |
| 38 | // Tests are writen as integration tests with PeerConnection, since the |
| 39 | // behaviors are still linked so closely that it is hard to test them in |
| 40 | // isolation. |
| 41 | |
| 42 | namespace webrtc { |
| 43 | |
| 44 | using RTCConfiguration = PeerConnectionInterface::RTCConfiguration; |
| 45 | |
| 46 | namespace { |
| 47 | |
| 48 | std::unique_ptr<rtc::Thread> CreateAndStartThread() { |
| 49 | auto thread = rtc::Thread::Create(); |
| 50 | thread->Start(); |
| 51 | return thread; |
| 52 | } |
| 53 | |
| 54 | } // namespace |
| 55 | |
| 56 | class SdpOfferAnswerTest : public ::testing::Test { |
| 57 | public: |
| 58 | SdpOfferAnswerTest() |
| 59 | // Note: We use a PeerConnectionFactory with a distinct |
| 60 | // signaling thread, so that thread handling can be tested. |
| 61 | : signaling_thread_(CreateAndStartThread()), |
| 62 | pc_factory_( |
| 63 | CreatePeerConnectionFactory(nullptr, |
| 64 | nullptr, |
| 65 | signaling_thread_.get(), |
| 66 | FakeAudioCaptureModule::Create(), |
| 67 | CreateBuiltinAudioEncoderFactory(), |
| 68 | CreateBuiltinAudioDecoderFactory(), |
| 69 | CreateBuiltinVideoEncoderFactory(), |
| 70 | CreateBuiltinVideoDecoderFactory(), |
| 71 | nullptr /* audio_mixer */, |
| 72 | nullptr /* audio_processing */)) { |
| 73 | webrtc::metrics::Reset(); |
| 74 | } |
| 75 | |
| 76 | std::unique_ptr<PeerConnectionWrapper> CreatePeerConnection() { |
| 77 | RTCConfiguration config; |
| 78 | config.sdp_semantics = SdpSemantics::kUnifiedPlan; |
| 79 | return CreatePeerConnection(config); |
| 80 | } |
| 81 | |
| 82 | std::unique_ptr<PeerConnectionWrapper> CreatePeerConnection( |
| 83 | const RTCConfiguration& config) { |
| 84 | auto observer = std::make_unique<MockPeerConnectionObserver>(); |
Florent Castelli | 7242440 | 2022-04-06 03:45:10 +0200 | [diff] [blame] | 85 | auto result = pc_factory_->CreatePeerConnectionOrError( |
| 86 | config, PeerConnectionDependencies(observer.get())); |
| 87 | EXPECT_TRUE(result.ok()); |
Niels Möller | afb246b | 2022-04-20 14:26:50 +0200 | [diff] [blame] | 88 | observer->SetPeerConnectionInterface(result.value().get()); |
Florent Castelli | 7242440 | 2022-04-06 03:45:10 +0200 | [diff] [blame] | 89 | return std::make_unique<PeerConnectionWrapper>( |
| 90 | pc_factory_, result.MoveValue(), std::move(observer)); |
Harald Alvestrand | e61d4c8 | 2021-09-16 08:59:11 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | protected: |
| 94 | std::unique_ptr<rtc::Thread> signaling_thread_; |
| 95 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_; |
| 96 | |
| 97 | private: |
| 98 | }; |
| 99 | |
| 100 | TEST_F(SdpOfferAnswerTest, OnTrackReturnsProxiedObject) { |
| 101 | auto caller = CreatePeerConnection(); |
| 102 | auto callee = CreatePeerConnection(); |
| 103 | |
| 104 | auto audio_transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO); |
| 105 | |
| 106 | ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get())); |
| 107 | // Verify that caller->observer->OnTrack() has been called with a |
| 108 | // proxied transceiver object. |
| 109 | ASSERT_EQ(callee->observer()->on_track_transceivers_.size(), 1u); |
| 110 | auto transceiver = callee->observer()->on_track_transceivers_[0]; |
| 111 | // Since the signaling thread is not the current thread, |
| 112 | // this will DCHECK if the transceiver is not proxied. |
| 113 | transceiver->stopped(); |
| 114 | } |
| 115 | |
| 116 | } // namespace webrtc |