blob: 2ce9e52d52ac8952b8489891f091dc8fb5395c94 [file] [log] [blame]
Harald Alvestrande61d4c82021-09-16 08:59:11 +00001/*
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 Alvestrande61d4c82021-09-16 08:59:11 +000011#include <memory>
Harald Alvestrande61d4c82021-09-16 08:59:11 +000012#include <utility>
13#include <vector>
14
Harald Alvestrande61d4c82021-09-16 08:59:11 +000015#include "api/audio/audio_mixer.h"
Harald Alvestrande61d4c82021-09-16 08:59:11 +000016#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 Alvestrande61d4c82021-09-16 08:59:11 +000019#include "api/media_types.h"
20#include "api/peer_connection_interface.h"
Harald Alvestrande61d4c82021-09-16 08:59:11 +000021#include "api/rtp_transceiver_interface.h"
22#include "api/scoped_refptr.h"
Harald Alvestrande61d4c82021-09-16 08:59:11 +000023#include "api/video_codecs/builtin_video_decoder_factory.h"
24#include "api/video_codecs/builtin_video_encoder_factory.h"
Harald Alvestrande61d4c82021-09-16 08:59:11 +000025#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 Alvestrande61d4c82021-09-16 08:59:11 +000028#include "pc/peer_connection_wrapper.h"
Harald Alvestrande61d4c82021-09-16 08:59:11 +000029#include "pc/test/fake_audio_capture_module.h"
30#include "pc/test/mock_peer_connection_observers.h"
Harald Alvestrande61d4c82021-09-16 08:59:11 +000031#include "rtc_base/rtc_certificate_generator.h"
32#include "rtc_base/thread.h"
33#include "system_wrappers/include/metrics.h"
Harald Alvestrande61d4c82021-09-16 08:59:11 +000034#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
42namespace webrtc {
43
44using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
45
46namespace {
47
48std::unique_ptr<rtc::Thread> CreateAndStartThread() {
49 auto thread = rtc::Thread::Create();
50 thread->Start();
51 return thread;
52}
53
54} // namespace
55
56class 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 Castelli72424402022-04-06 03:45:10 +020085 auto result = pc_factory_->CreatePeerConnectionOrError(
86 config, PeerConnectionDependencies(observer.get()));
87 EXPECT_TRUE(result.ok());
Niels Möllerafb246b2022-04-20 14:26:50 +020088 observer->SetPeerConnectionInterface(result.value().get());
Florent Castelli72424402022-04-06 03:45:10 +020089 return std::make_unique<PeerConnectionWrapper>(
90 pc_factory_, result.MoveValue(), std::move(observer));
Harald Alvestrande61d4c82021-09-16 08:59:11 +000091 }
92
93 protected:
94 std::unique_ptr<rtc::Thread> signaling_thread_;
95 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
96
97 private:
98};
99
100TEST_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