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