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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "ortc/ortcfactory.h" |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 12 | |
| 13 | #include <sstream> |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 14 | #include <utility> // For std::move. |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 15 | #include <vector> |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 16 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 17 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/mediastreamtrackproxy.h" |
| 19 | #include "api/proxy.h" |
| 20 | #include "api/rtcerror.h" |
Anders Carlsson | b330688 | 2018-05-14 10:11:42 +0200 | [diff] [blame] | 21 | #include "api/video_codecs/builtin_video_decoder_factory.h" |
| 22 | #include "api/video_codecs/builtin_video_encoder_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "api/videosourceproxy.h" |
| 24 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 25 | #include "media/base/mediaconstants.h" |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 26 | #include "media/base/rtpdataengine.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "modules/audio_processing/include/audio_processing.h" |
| 28 | #include "ortc/ortcrtpreceiveradapter.h" |
| 29 | #include "ortc/ortcrtpsenderadapter.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "ortc/rtptransportadapter.h" |
| 31 | #include "ortc/rtptransportcontrolleradapter.h" |
| 32 | #include "p2p/base/basicpacketsocketfactory.h" |
| 33 | #include "p2p/base/udptransport.h" |
| 34 | #include "pc/audiotrack.h" |
| 35 | #include "pc/channelmanager.h" |
| 36 | #include "pc/localaudiosource.h" |
Florent Castelli | 72b751a | 2018-06-28 14:09:33 +0200 | [diff] [blame] | 37 | #include "pc/rtpparametersconversion.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 38 | #include "pc/videotrack.h" |
| 39 | #include "rtc_base/asyncpacketsocket.h" |
| 40 | #include "rtc_base/bind.h" |
| 41 | #include "rtc_base/checks.h" |
| 42 | #include "rtc_base/helpers.h" |
| 43 | #include "rtc_base/logging.h" |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 44 | |
| 45 | namespace { |
| 46 | |
| 47 | const int kDefaultRtcpCnameLength = 16; |
| 48 | |
| 49 | // Asserts that all of the built-in capabilities can be converted to |
| 50 | // RtpCapabilities. If they can't, something's wrong (for example, maybe a new |
| 51 | // feedback mechanism is supported, but an enum value wasn't added to |
| 52 | // rtpparameters.h). |
| 53 | template <typename C> |
| 54 | webrtc::RtpCapabilities ToRtpCapabilitiesWithAsserts( |
| 55 | const std::vector<C>& cricket_codecs, |
| 56 | const cricket::RtpHeaderExtensions& cricket_extensions) { |
| 57 | webrtc::RtpCapabilities capabilities = |
| 58 | webrtc::ToRtpCapabilities(cricket_codecs, cricket_extensions); |
| 59 | RTC_DCHECK_EQ(capabilities.codecs.size(), cricket_codecs.size()); |
| 60 | for (size_t i = 0; i < capabilities.codecs.size(); ++i) { |
| 61 | RTC_DCHECK_EQ(capabilities.codecs[i].rtcp_feedback.size(), |
| 62 | cricket_codecs[i].feedback_params.params().size()); |
| 63 | } |
| 64 | RTC_DCHECK_EQ(capabilities.header_extensions.size(), |
| 65 | cricket_extensions.size()); |
| 66 | return capabilities; |
| 67 | } |
| 68 | |
| 69 | } // namespace |
| 70 | |
| 71 | namespace webrtc { |
| 72 | |
| 73 | // Note that this proxy class uses the network thread as the "worker" thread. |
| 74 | BEGIN_OWNED_PROXY_MAP(OrtcFactory) |
| 75 | PROXY_SIGNALING_THREAD_DESTRUCTOR() |
| 76 | PROXY_METHOD0(RTCErrorOr<std::unique_ptr<RtpTransportControllerInterface>>, |
| 77 | CreateRtpTransportController) |
| 78 | PROXY_METHOD4(RTCErrorOr<std::unique_ptr<RtpTransportInterface>>, |
| 79 | CreateRtpTransport, |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 80 | const RtpTransportParameters&, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 81 | PacketTransportInterface*, |
| 82 | PacketTransportInterface*, |
| 83 | RtpTransportControllerInterface*) |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 84 | |
| 85 | PROXY_METHOD4(RTCErrorOr<std::unique_ptr<SrtpTransportInterface>>, |
| 86 | CreateSrtpTransport, |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 87 | const RtpTransportParameters&, |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 88 | PacketTransportInterface*, |
| 89 | PacketTransportInterface*, |
| 90 | RtpTransportControllerInterface*) |
| 91 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 92 | PROXY_CONSTMETHOD1(RtpCapabilities, |
| 93 | GetRtpSenderCapabilities, |
| 94 | cricket::MediaType) |
| 95 | PROXY_METHOD2(RTCErrorOr<std::unique_ptr<OrtcRtpSenderInterface>>, |
| 96 | CreateRtpSender, |
| 97 | rtc::scoped_refptr<MediaStreamTrackInterface>, |
| 98 | RtpTransportInterface*) |
| 99 | PROXY_METHOD2(RTCErrorOr<std::unique_ptr<OrtcRtpSenderInterface>>, |
| 100 | CreateRtpSender, |
| 101 | cricket::MediaType, |
| 102 | RtpTransportInterface*) |
| 103 | PROXY_CONSTMETHOD1(RtpCapabilities, |
| 104 | GetRtpReceiverCapabilities, |
| 105 | cricket::MediaType) |
| 106 | PROXY_METHOD2(RTCErrorOr<std::unique_ptr<OrtcRtpReceiverInterface>>, |
| 107 | CreateRtpReceiver, |
| 108 | cricket::MediaType, |
| 109 | RtpTransportInterface*) |
| 110 | PROXY_WORKER_METHOD3(RTCErrorOr<std::unique_ptr<UdpTransportInterface>>, |
| 111 | CreateUdpTransport, |
| 112 | int, |
| 113 | uint16_t, |
| 114 | uint16_t) |
| 115 | PROXY_METHOD1(rtc::scoped_refptr<AudioSourceInterface>, |
| 116 | CreateAudioSource, |
| 117 | const cricket::AudioOptions&) |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 118 | PROXY_METHOD2(rtc::scoped_refptr<VideoTrackInterface>, |
| 119 | CreateVideoTrack, |
| 120 | const std::string&, |
| 121 | VideoTrackSourceInterface*) |
| 122 | PROXY_METHOD2(rtc::scoped_refptr<AudioTrackInterface>, |
| 123 | CreateAudioTrack, |
| 124 | const std::string&, |
| 125 | AudioSourceInterface*) |
| 126 | END_PROXY_MAP() |
| 127 | |
| 128 | // static |
| 129 | RTCErrorOr<std::unique_ptr<OrtcFactoryInterface>> OrtcFactory::Create( |
| 130 | rtc::Thread* network_thread, |
| 131 | rtc::Thread* signaling_thread, |
| 132 | rtc::NetworkManager* network_manager, |
| 133 | rtc::PacketSocketFactory* socket_factory, |
| 134 | AudioDeviceModule* adm, |
Karl Wiberg | 3e9e5b3 | 2017-11-06 05:01:56 +0100 | [diff] [blame] | 135 | std::unique_ptr<cricket::MediaEngineInterface> media_engine, |
| 136 | rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory, |
| 137 | rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory) { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 138 | // Hop to signaling thread if needed. |
| 139 | if (signaling_thread && !signaling_thread->IsCurrent()) { |
| 140 | return signaling_thread |
| 141 | ->Invoke<RTCErrorOr<std::unique_ptr<OrtcFactoryInterface>>>( |
| 142 | RTC_FROM_HERE, |
| 143 | rtc::Bind(&OrtcFactory::Create_s, network_thread, signaling_thread, |
| 144 | network_manager, socket_factory, adm, |
Karl Wiberg | 3e9e5b3 | 2017-11-06 05:01:56 +0100 | [diff] [blame] | 145 | media_engine.release(), audio_encoder_factory, |
| 146 | audio_decoder_factory)); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 147 | } |
| 148 | return Create_s(network_thread, signaling_thread, network_manager, |
Karl Wiberg | 3e9e5b3 | 2017-11-06 05:01:56 +0100 | [diff] [blame] | 149 | socket_factory, adm, media_engine.release(), |
| 150 | audio_encoder_factory, audio_decoder_factory); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | RTCErrorOr<std::unique_ptr<OrtcFactoryInterface>> OrtcFactoryInterface::Create( |
| 154 | rtc::Thread* network_thread, |
| 155 | rtc::Thread* signaling_thread, |
| 156 | rtc::NetworkManager* network_manager, |
| 157 | rtc::PacketSocketFactory* socket_factory, |
Karl Wiberg | 3e9e5b3 | 2017-11-06 05:01:56 +0100 | [diff] [blame] | 158 | AudioDeviceModule* adm, |
| 159 | rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory, |
| 160 | rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory) { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 161 | return OrtcFactory::Create(network_thread, signaling_thread, network_manager, |
Karl Wiberg | 3e9e5b3 | 2017-11-06 05:01:56 +0100 | [diff] [blame] | 162 | socket_factory, adm, nullptr, |
| 163 | audio_encoder_factory, audio_decoder_factory); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Karl Wiberg | 3e9e5b3 | 2017-11-06 05:01:56 +0100 | [diff] [blame] | 166 | OrtcFactory::OrtcFactory( |
| 167 | rtc::Thread* network_thread, |
| 168 | rtc::Thread* signaling_thread, |
| 169 | rtc::NetworkManager* network_manager, |
| 170 | rtc::PacketSocketFactory* socket_factory, |
| 171 | AudioDeviceModule* adm, |
| 172 | rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory, |
| 173 | rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory) |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 174 | : network_thread_(network_thread), |
| 175 | signaling_thread_(signaling_thread), |
| 176 | network_manager_(network_manager), |
| 177 | socket_factory_(socket_factory), |
| 178 | adm_(adm), |
| 179 | null_event_log_(RtcEventLog::CreateNull()), |
Karl Wiberg | 3e9e5b3 | 2017-11-06 05:01:56 +0100 | [diff] [blame] | 180 | audio_encoder_factory_(audio_encoder_factory), |
| 181 | audio_decoder_factory_(audio_decoder_factory) { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 182 | if (!rtc::CreateRandomString(kDefaultRtcpCnameLength, &default_cname_)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 183 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME?"; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 184 | RTC_NOTREACHED(); |
| 185 | } |
| 186 | if (!network_thread_) { |
| 187 | owned_network_thread_ = rtc::Thread::CreateWithSocketServer(); |
| 188 | owned_network_thread_->Start(); |
| 189 | network_thread_ = owned_network_thread_.get(); |
| 190 | } |
| 191 | |
| 192 | // The worker thread is created internally because it's an implementation |
| 193 | // detail, and consumers of the API don't need to really know about it. |
| 194 | worker_thread_ = rtc::Thread::Create(); |
Tommi | 1c3509f | 2018-02-06 08:49:12 +0100 | [diff] [blame] | 195 | worker_thread_->SetName("ORTC-worker", this); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 196 | worker_thread_->Start(); |
| 197 | |
| 198 | if (signaling_thread_) { |
| 199 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 200 | } else { |
| 201 | signaling_thread_ = rtc::Thread::Current(); |
| 202 | if (!signaling_thread_) { |
| 203 | // If this thread isn't already wrapped by an rtc::Thread, create a |
| 204 | // wrapper and own it in this class. |
| 205 | signaling_thread_ = rtc::ThreadManager::Instance()->WrapCurrentThread(); |
| 206 | wraps_signaling_thread_ = true; |
| 207 | } |
| 208 | } |
Tommi | 1c3509f | 2018-02-06 08:49:12 +0100 | [diff] [blame] | 209 | |
| 210 | if (signaling_thread_->name().empty()) { |
| 211 | signaling_thread_->SetName("ORTC-signaling", this); |
| 212 | } |
| 213 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 214 | if (!network_manager_) { |
| 215 | owned_network_manager_.reset(new rtc::BasicNetworkManager()); |
| 216 | network_manager_ = owned_network_manager_.get(); |
| 217 | } |
| 218 | if (!socket_factory_) { |
| 219 | owned_socket_factory_.reset( |
| 220 | new rtc::BasicPacketSocketFactory(network_thread_)); |
| 221 | socket_factory_ = owned_socket_factory_.get(); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | OrtcFactory::~OrtcFactory() { |
| 226 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 227 | if (wraps_signaling_thread_) { |
| 228 | rtc::ThreadManager::Instance()->UnwrapCurrentThread(); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | RTCErrorOr<std::unique_ptr<RtpTransportControllerInterface>> |
| 233 | OrtcFactory::CreateRtpTransportController() { |
| 234 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 235 | return RtpTransportControllerAdapter::CreateProxied( |
| 236 | cricket::MediaConfig(), channel_manager_.get(), null_event_log_.get(), |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 237 | signaling_thread_, worker_thread_.get(), network_thread_); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | RTCErrorOr<std::unique_ptr<RtpTransportInterface>> |
| 241 | OrtcFactory::CreateRtpTransport( |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 242 | const RtpTransportParameters& parameters, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 243 | PacketTransportInterface* rtp, |
| 244 | PacketTransportInterface* rtcp, |
| 245 | RtpTransportControllerInterface* transport_controller) { |
| 246 | RTC_DCHECK_RUN_ON(signaling_thread_); |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 247 | RtpTransportParameters copied_parameters = parameters; |
| 248 | if (copied_parameters.rtcp.cname.empty()) { |
| 249 | copied_parameters.rtcp.cname = default_cname_; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 250 | } |
| 251 | if (transport_controller) { |
| 252 | return transport_controller->GetInternal()->CreateProxiedRtpTransport( |
| 253 | copied_parameters, rtp, rtcp); |
| 254 | } else { |
| 255 | // If |transport_controller| is null, create one automatically, which the |
| 256 | // returned RtpTransport will own. |
| 257 | auto controller_result = CreateRtpTransportController(); |
| 258 | if (!controller_result.ok()) { |
| 259 | return controller_result.MoveError(); |
| 260 | } |
| 261 | auto controller = controller_result.MoveValue(); |
| 262 | auto transport_result = |
| 263 | controller->GetInternal()->CreateProxiedRtpTransport(copied_parameters, |
| 264 | rtp, rtcp); |
| 265 | // If RtpTransport was successfully created, transfer ownership of |
| 266 | // |rtp_transport_controller|. Otherwise it will go out of scope and be |
| 267 | // deleted automatically. |
| 268 | if (transport_result.ok()) { |
| 269 | transport_result.value() |
| 270 | ->GetInternal() |
| 271 | ->TakeOwnershipOfRtpTransportController(std::move(controller)); |
| 272 | } |
| 273 | return transport_result; |
| 274 | } |
| 275 | } |
| 276 | |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 277 | RTCErrorOr<std::unique_ptr<SrtpTransportInterface>> |
| 278 | OrtcFactory::CreateSrtpTransport( |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 279 | const RtpTransportParameters& parameters, |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 280 | PacketTransportInterface* rtp, |
| 281 | PacketTransportInterface* rtcp, |
| 282 | RtpTransportControllerInterface* transport_controller) { |
| 283 | RTC_DCHECK_RUN_ON(signaling_thread_); |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 284 | RtpTransportParameters copied_parameters = parameters; |
| 285 | if (copied_parameters.rtcp.cname.empty()) { |
| 286 | copied_parameters.rtcp.cname = default_cname_; |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 287 | } |
| 288 | if (transport_controller) { |
| 289 | return transport_controller->GetInternal()->CreateProxiedSrtpTransport( |
| 290 | copied_parameters, rtp, rtcp); |
| 291 | } else { |
| 292 | // If |transport_controller| is null, create one automatically, which the |
| 293 | // returned SrtpTransport will own. |
| 294 | auto controller_result = CreateRtpTransportController(); |
| 295 | if (!controller_result.ok()) { |
| 296 | return controller_result.MoveError(); |
| 297 | } |
| 298 | auto controller = controller_result.MoveValue(); |
| 299 | auto transport_result = |
| 300 | controller->GetInternal()->CreateProxiedSrtpTransport(copied_parameters, |
| 301 | rtp, rtcp); |
| 302 | // If SrtpTransport was successfully created, transfer ownership of |
| 303 | // |rtp_transport_controller|. Otherwise it will go out of scope and be |
| 304 | // deleted automatically. |
| 305 | if (transport_result.ok()) { |
| 306 | transport_result.value() |
| 307 | ->GetInternal() |
| 308 | ->TakeOwnershipOfRtpTransportController(std::move(controller)); |
| 309 | } |
| 310 | return transport_result; |
| 311 | } |
| 312 | } |
| 313 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 314 | RtpCapabilities OrtcFactory::GetRtpSenderCapabilities( |
| 315 | cricket::MediaType kind) const { |
| 316 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 317 | switch (kind) { |
| 318 | case cricket::MEDIA_TYPE_AUDIO: { |
| 319 | cricket::AudioCodecs cricket_codecs; |
| 320 | cricket::RtpHeaderExtensions cricket_extensions; |
| 321 | channel_manager_->GetSupportedAudioSendCodecs(&cricket_codecs); |
| 322 | channel_manager_->GetSupportedAudioRtpHeaderExtensions( |
| 323 | &cricket_extensions); |
| 324 | return ToRtpCapabilitiesWithAsserts(cricket_codecs, cricket_extensions); |
| 325 | } |
| 326 | case cricket::MEDIA_TYPE_VIDEO: { |
| 327 | cricket::VideoCodecs cricket_codecs; |
| 328 | cricket::RtpHeaderExtensions cricket_extensions; |
| 329 | channel_manager_->GetSupportedVideoCodecs(&cricket_codecs); |
| 330 | channel_manager_->GetSupportedVideoRtpHeaderExtensions( |
| 331 | &cricket_extensions); |
| 332 | return ToRtpCapabilitiesWithAsserts(cricket_codecs, cricket_extensions); |
| 333 | } |
| 334 | case cricket::MEDIA_TYPE_DATA: |
| 335 | return RtpCapabilities(); |
| 336 | } |
| 337 | // Not reached; avoids compile warning. |
| 338 | FATAL(); |
| 339 | } |
| 340 | |
| 341 | RTCErrorOr<std::unique_ptr<OrtcRtpSenderInterface>> |
| 342 | OrtcFactory::CreateRtpSender( |
| 343 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 344 | RtpTransportInterface* transport) { |
| 345 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 346 | if (!track) { |
| 347 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 348 | "Cannot pass null track into CreateRtpSender."); |
| 349 | } |
| 350 | auto result = |
| 351 | CreateRtpSender(cricket::MediaTypeFromString(track->kind()), transport); |
| 352 | if (!result.ok()) { |
| 353 | return result; |
| 354 | } |
| 355 | auto err = result.value()->SetTrack(track); |
| 356 | if (!err.ok()) { |
| 357 | return std::move(err); |
| 358 | } |
| 359 | return result; |
| 360 | } |
| 361 | |
| 362 | RTCErrorOr<std::unique_ptr<OrtcRtpSenderInterface>> |
| 363 | OrtcFactory::CreateRtpSender(cricket::MediaType kind, |
| 364 | RtpTransportInterface* transport) { |
| 365 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 366 | if (kind == cricket::MEDIA_TYPE_DATA) { |
| 367 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 368 | "Cannot create data RtpSender."); |
| 369 | } |
| 370 | if (!transport) { |
| 371 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 372 | "Cannot pass null transport into CreateRtpSender."); |
| 373 | } |
| 374 | return transport->GetInternal() |
| 375 | ->rtp_transport_controller() |
| 376 | ->CreateProxiedRtpSender(kind, transport); |
| 377 | } |
| 378 | |
| 379 | RtpCapabilities OrtcFactory::GetRtpReceiverCapabilities( |
| 380 | cricket::MediaType kind) const { |
| 381 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 382 | switch (kind) { |
| 383 | case cricket::MEDIA_TYPE_AUDIO: { |
| 384 | cricket::AudioCodecs cricket_codecs; |
| 385 | cricket::RtpHeaderExtensions cricket_extensions; |
| 386 | channel_manager_->GetSupportedAudioReceiveCodecs(&cricket_codecs); |
| 387 | channel_manager_->GetSupportedAudioRtpHeaderExtensions( |
| 388 | &cricket_extensions); |
| 389 | return ToRtpCapabilitiesWithAsserts(cricket_codecs, cricket_extensions); |
| 390 | } |
| 391 | case cricket::MEDIA_TYPE_VIDEO: { |
| 392 | cricket::VideoCodecs cricket_codecs; |
| 393 | cricket::RtpHeaderExtensions cricket_extensions; |
| 394 | channel_manager_->GetSupportedVideoCodecs(&cricket_codecs); |
| 395 | channel_manager_->GetSupportedVideoRtpHeaderExtensions( |
| 396 | &cricket_extensions); |
| 397 | return ToRtpCapabilitiesWithAsserts(cricket_codecs, cricket_extensions); |
| 398 | } |
| 399 | case cricket::MEDIA_TYPE_DATA: |
| 400 | return RtpCapabilities(); |
| 401 | } |
| 402 | // Not reached; avoids compile warning. |
| 403 | FATAL(); |
| 404 | } |
| 405 | |
| 406 | RTCErrorOr<std::unique_ptr<OrtcRtpReceiverInterface>> |
| 407 | OrtcFactory::CreateRtpReceiver(cricket::MediaType kind, |
| 408 | RtpTransportInterface* transport) { |
| 409 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 410 | if (kind == cricket::MEDIA_TYPE_DATA) { |
| 411 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 412 | "Cannot create data RtpReceiver."); |
| 413 | } |
| 414 | if (!transport) { |
| 415 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 416 | "Cannot pass null transport into CreateRtpReceiver."); |
| 417 | } |
| 418 | return transport->GetInternal() |
| 419 | ->rtp_transport_controller() |
| 420 | ->CreateProxiedRtpReceiver(kind, transport); |
| 421 | } |
| 422 | |
| 423 | // UdpTransport expects all methods to be called on one thread, which needs to |
| 424 | // be the network thread, since that's where its socket can safely be used. So |
| 425 | // return a proxy to the created UdpTransport. |
| 426 | BEGIN_OWNED_PROXY_MAP(UdpTransport) |
| 427 | PROXY_WORKER_THREAD_DESTRUCTOR() |
| 428 | PROXY_WORKER_CONSTMETHOD0(rtc::SocketAddress, GetLocalAddress) |
| 429 | PROXY_WORKER_METHOD1(bool, SetRemoteAddress, const rtc::SocketAddress&) |
| 430 | PROXY_WORKER_CONSTMETHOD0(rtc::SocketAddress, GetRemoteAddress) |
| 431 | protected: |
| 432 | rtc::PacketTransportInternal* GetInternal() override { |
| 433 | return internal(); |
| 434 | } |
| 435 | END_PROXY_MAP() |
| 436 | |
| 437 | RTCErrorOr<std::unique_ptr<UdpTransportInterface>> |
| 438 | OrtcFactory::CreateUdpTransport(int family, |
| 439 | uint16_t min_port, |
| 440 | uint16_t max_port) { |
| 441 | RTC_DCHECK_RUN_ON(network_thread_); |
| 442 | if (family != AF_INET && family != AF_INET6) { |
| 443 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 444 | "Address family must be AF_INET or AF_INET6."); |
| 445 | } |
| 446 | if (min_port > max_port) { |
| 447 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, |
| 448 | "Port range invalid; minimum port must be less than " |
| 449 | "or equal to max port."); |
| 450 | } |
| 451 | std::unique_ptr<rtc::AsyncPacketSocket> socket( |
| 452 | socket_factory_->CreateUdpSocket( |
| 453 | rtc::SocketAddress(rtc::GetAnyIP(family), 0), min_port, max_port)); |
| 454 | if (!socket) { |
| 455 | // Only log at warning level, because this method may be called with |
| 456 | // specific port ranges to determine if a port is available, expecting the |
| 457 | // possibility of an error. |
| 458 | LOG_AND_RETURN_ERROR_EX(RTCErrorType::RESOURCE_EXHAUSTED, |
| 459 | "Local socket allocation failure.", LS_WARNING); |
| 460 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 461 | RTC_LOG(LS_INFO) << "Created UDP socket with address " |
| 462 | << socket->GetLocalAddress().ToSensitiveString() << "."; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 463 | // Make a unique debug name (for logging/diagnostics only). |
| 464 | std::ostringstream oss; |
| 465 | static int udp_id = 0; |
| 466 | oss << "udp" << udp_id++; |
| 467 | return UdpTransportProxyWithInternal<cricket::UdpTransport>::Create( |
| 468 | signaling_thread_, network_thread_, |
| 469 | std::unique_ptr<cricket::UdpTransport>( |
| 470 | new cricket::UdpTransport(oss.str(), std::move(socket)))); |
| 471 | } |
| 472 | |
| 473 | rtc::scoped_refptr<AudioSourceInterface> OrtcFactory::CreateAudioSource( |
| 474 | const cricket::AudioOptions& options) { |
| 475 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 476 | return rtc::scoped_refptr<LocalAudioSource>( |
| 477 | LocalAudioSource::Create(&options)); |
| 478 | } |
| 479 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 480 | rtc::scoped_refptr<VideoTrackInterface> OrtcFactory::CreateVideoTrack( |
| 481 | const std::string& id, |
| 482 | VideoTrackSourceInterface* source) { |
| 483 | RTC_DCHECK_RUN_ON(signaling_thread_); |
perkj | 773be36 | 2017-07-31 23:22:01 -0700 | [diff] [blame] | 484 | rtc::scoped_refptr<VideoTrackInterface> track( |
| 485 | VideoTrack::Create(id, source, worker_thread_.get())); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 486 | return VideoTrackProxy::Create(signaling_thread_, worker_thread_.get(), |
| 487 | track); |
| 488 | } |
| 489 | |
| 490 | rtc::scoped_refptr<AudioTrackInterface> OrtcFactory::CreateAudioTrack( |
| 491 | const std::string& id, |
| 492 | AudioSourceInterface* source) { |
| 493 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 494 | rtc::scoped_refptr<AudioTrackInterface> track(AudioTrack::Create(id, source)); |
| 495 | return AudioTrackProxy::Create(signaling_thread_, track); |
| 496 | } |
| 497 | |
| 498 | // static |
| 499 | RTCErrorOr<std::unique_ptr<OrtcFactoryInterface>> OrtcFactory::Create_s( |
| 500 | rtc::Thread* network_thread, |
| 501 | rtc::Thread* signaling_thread, |
| 502 | rtc::NetworkManager* network_manager, |
| 503 | rtc::PacketSocketFactory* socket_factory, |
| 504 | AudioDeviceModule* adm, |
Karl Wiberg | 3e9e5b3 | 2017-11-06 05:01:56 +0100 | [diff] [blame] | 505 | cricket::MediaEngineInterface* media_engine, |
| 506 | rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory, |
| 507 | rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory) { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 508 | // Add the unique_ptr wrapper back. |
| 509 | std::unique_ptr<cricket::MediaEngineInterface> owned_media_engine( |
| 510 | media_engine); |
| 511 | std::unique_ptr<OrtcFactory> new_factory(new OrtcFactory( |
Karl Wiberg | 3e9e5b3 | 2017-11-06 05:01:56 +0100 | [diff] [blame] | 512 | network_thread, signaling_thread, network_manager, socket_factory, adm, |
| 513 | audio_encoder_factory, audio_decoder_factory)); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 514 | RTCError err = new_factory->Initialize(std::move(owned_media_engine)); |
| 515 | if (!err.ok()) { |
| 516 | return std::move(err); |
| 517 | } |
| 518 | // Return a proxy so that any calls on the returned object (including |
| 519 | // destructor) happen on the signaling thread. |
| 520 | rtc::Thread* signaling = new_factory->signaling_thread(); |
| 521 | rtc::Thread* network = new_factory->network_thread(); |
| 522 | return OrtcFactoryProxy::Create(signaling, network, std::move(new_factory)); |
| 523 | } |
| 524 | |
| 525 | RTCError OrtcFactory::Initialize( |
| 526 | std::unique_ptr<cricket::MediaEngineInterface> media_engine) { |
| 527 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 528 | // TODO(deadbeef): Get rid of requirement to hop to worker thread here. |
| 529 | if (!media_engine) { |
| 530 | media_engine = |
| 531 | worker_thread_->Invoke<std::unique_ptr<cricket::MediaEngineInterface>>( |
| 532 | RTC_FROM_HERE, rtc::Bind(&OrtcFactory::CreateMediaEngine_w, this)); |
| 533 | } |
| 534 | |
| 535 | channel_manager_.reset(new cricket::ChannelManager( |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 536 | std::move(media_engine), absl::make_unique<cricket::RtpDataEngine>(), |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 537 | worker_thread_.get(), network_thread_)); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 538 | channel_manager_->SetVideoRtxEnabled(true); |
| 539 | if (!channel_manager_->Init()) { |
| 540 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 541 | "Failed to initialize ChannelManager."); |
| 542 | } |
| 543 | return RTCError::OK(); |
| 544 | } |
| 545 | |
| 546 | std::unique_ptr<cricket::MediaEngineInterface> |
| 547 | OrtcFactory::CreateMediaEngine_w() { |
| 548 | RTC_DCHECK_RUN_ON(worker_thread_.get()); |
| 549 | // The null arguments are optional factories that could be passed into the |
| 550 | // OrtcFactory, but aren't yet. |
| 551 | // |
| 552 | // Note that |adm_| may be null, in which case the platform-specific default |
| 553 | // AudioDeviceModule will be used. |
| 554 | return std::unique_ptr<cricket::MediaEngineInterface>( |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 555 | cricket::WebRtcMediaEngineFactory::Create( |
Anders Carlsson | b330688 | 2018-05-14 10:11:42 +0200 | [diff] [blame] | 556 | rtc::scoped_refptr<webrtc::AudioDeviceModule>(adm_), |
| 557 | audio_encoder_factory_, audio_decoder_factory_, |
| 558 | webrtc::CreateBuiltinVideoEncoderFactory(), |
| 559 | webrtc::CreateBuiltinVideoDecoderFactory(), nullptr, |
| 560 | webrtc::AudioProcessingBuilder().Create())); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | } // namespace webrtc |