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