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