blob: 26ef88bf1c1e38feab2840cccf85397f6cfb9f0e [file] [log] [blame]
Harald Alvestrand98462622019-01-30 14:57:03 +01001/*
2 * Copyright 2019 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 "api/ice_transport_factory.h"
12
13#include <memory>
14#include <utility>
15
Harald Alvestrand98462622019-01-30 14:57:03 +010016#include "p2p/base/ice_transport_internal.h"
Philipp Hancke2f5f5fa2021-03-21 20:37:35 +010017#include "p2p/base/p2p_constants.h"
Harald Alvestrand98462622019-01-30 14:57:03 +010018#include "p2p/base/p2p_transport_channel.h"
19#include "p2p/base/port_allocator.h"
20#include "rtc_base/thread.h"
21
22namespace webrtc {
23
24namespace {
25
26// This implementation of IceTransportInterface is used in cases where
27// the only reference to the P2PTransport will be through this class.
28// It must be constructed, accessed and destroyed on the signaling thread.
29class IceTransportWithTransportChannel : public IceTransportInterface {
30 public:
31 IceTransportWithTransportChannel(
32 std::unique_ptr<cricket::IceTransportInternal> internal)
33 : internal_(std::move(internal)) {}
34
35 ~IceTransportWithTransportChannel() override {
36 RTC_DCHECK_RUN_ON(&thread_checker_);
37 }
38
39 cricket::IceTransportInternal* internal() override {
40 RTC_DCHECK_RUN_ON(&thread_checker_);
41 return internal_.get();
42 }
43
44 private:
Artem Titovc8421c42021-02-02 10:57:19 +010045 const SequenceChecker thread_checker_{};
Harald Alvestrand98462622019-01-30 14:57:03 +010046 const std::unique_ptr<cricket::IceTransportInternal> internal_
47 RTC_GUARDED_BY(thread_checker_);
48};
49
50} // namespace
51
52rtc::scoped_refptr<IceTransportInterface> CreateIceTransport(
53 cricket::PortAllocator* port_allocator) {
Steve Anton6fdfec12019-07-01 14:07:33 -070054 IceTransportInit init;
55 init.set_port_allocator(port_allocator);
56 return CreateIceTransport(std::move(init));
57}
58
59rtc::scoped_refptr<IceTransportInterface> CreateIceTransport(
60 IceTransportInit init) {
Harald Alvestrand0ccfbd22021-04-08 07:25:04 +000061 if (init.async_resolver_factory()) {
62 // Backwards compatibility mode
Tomas Gunnarssonc1d58912021-04-22 19:21:43 +020063 return rtc::make_ref_counted<IceTransportWithTransportChannel>(
Harald Alvestrand0ccfbd22021-04-08 07:25:04 +000064 std::make_unique<cricket::P2PTransportChannel>(
65 "", cricket::ICE_CANDIDATE_COMPONENT_RTP, init.port_allocator(),
66 init.async_resolver_factory(), init.event_log()));
67 } else {
Tomas Gunnarssonc1d58912021-04-22 19:21:43 +020068 return rtc::make_ref_counted<IceTransportWithTransportChannel>(
Harald Alvestrand0ccfbd22021-04-08 07:25:04 +000069 cricket::P2PTransportChannel::Create(
70 "", cricket::ICE_CANDIDATE_COMPONENT_RTP, init.port_allocator(),
71 init.async_dns_resolver_factory(), init.event_log()));
72 }
Harald Alvestrand98462622019-01-30 14:57:03 +010073}
74
75} // namespace webrtc