blob: 2f324f7289a6a2c89f4a727b20f1f9e0ff017b0a [file] [log] [blame]
Jon Hjelleda99da82016-01-20 13:40:30 -08001/*
2 * Copyright 2015 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 Bonadei317a1f02019-09-17 17:06:18 +020011#include <memory>
12
magjedb8853ca2017-08-29 03:57:22 -070013#import "RTCPeerConnectionFactory+Native.h"
tkchin9eeb6242016-04-27 01:54:20 -070014#import "RTCPeerConnectionFactory+Private.h"
Yura Yaroshevichbf567122018-01-02 13:33:16 +030015#import "RTCPeerConnectionFactoryOptions+Private.h"
Jon Hjelleda99da82016-01-20 13:40:30 -080016
tkchind4bfbfc2016-08-30 11:56:05 -070017#import "RTCAudioSource+Private.h"
tkchin9eeb6242016-04-27 01:54:20 -070018#import "RTCAudioTrack+Private.h"
tkchind4bfbfc2016-08-30 11:56:05 -070019#import "RTCMediaConstraints+Private.h"
tkchin9eeb6242016-04-27 01:54:20 -070020#import "RTCMediaStream+Private.h"
21#import "RTCPeerConnection+Private.h"
22#import "RTCVideoSource+Private.h"
23#import "RTCVideoTrack+Private.h"
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020024#import "base/RTCLogging.h"
25#import "base/RTCVideoDecoderFactory.h"
26#import "base/RTCVideoEncoderFactory.h"
27#import "helpers/NSString+StdString.h"
Taylor Brandstetterea7fbfb2020-08-19 16:41:54 -070028#include "sdk/objc/native/api/network_monitor_factory.h"
29#include "system_wrappers/include/field_trial.h"
30
kthelgasonfb143122017-07-25 07:55:58 -070031#ifndef HAVE_NO_MEDIA
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020032#import "components/video_codec/RTCVideoDecoderFactoryH264.h"
33#import "components/video_codec/RTCVideoEncoderFactoryH264.h"
magjedb8853ca2017-08-29 03:57:22 -070034// The no-media version PeerConnectionFactory doesn't depend on these files, but the gn check tool
35// is not smart enough to take the #ifdef into account.
Anders Carlsson7e042812017-10-05 16:55:38 +020036#include "api/audio_codecs/builtin_audio_decoder_factory.h" // nogncheck
37#include "api/audio_codecs/builtin_audio_encoder_factory.h" // nogncheck
Danil Chapovalov4ba04b72019-06-26 15:49:47 +020038#include "api/rtc_event_log/rtc_event_log_factory.h"
Danil Chapovalovaaa11432019-05-17 13:20:14 +020039#include "api/task_queue/default_task_queue_factory.h"
Erik Språngceb44952020-09-22 11:36:35 +020040#include "api/transport/field_trial_based_config.h"
Anders Carlsson7e042812017-10-05 16:55:38 +020041#include "modules/audio_device/include/audio_device.h" // nogncheck
42#include "modules/audio_processing/include/audio_processing.h" // nogncheck
Anders Carlsson3ff50fb2018-02-01 15:47:05 +010043
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020044#include "sdk/objc/native/api/video_decoder_factory.h"
45#include "sdk/objc/native/api/video_encoder_factory.h"
46#include "sdk/objc/native/src/objc_video_decoder_factory.h"
47#include "sdk/objc/native/src/objc_video_encoder_factory.h"
kthelgasonfb143122017-07-25 07:55:58 -070048#endif
kwibergbfefb032016-05-01 14:53:46 -070049
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020050#if defined(WEBRTC_IOS)
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020051#import "sdk/objc/native/api/audio_device_module.h"
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020052#endif
53
zhihuanga4c113a2017-06-28 14:05:44 -070054// Adding the nogncheck to disable the including header check.
55// The no-media version PeerConnectionFactory doesn't depend on media related
56// C++ target.
57// TODO(zhihuang): Remove nogncheck once MediaEngineInterface is moved to C++
58// API layer.
Steve Anton10542f22019-01-11 09:11:00 -080059#include "media/engine/webrtc_media_engine.h" // nogncheck
Kári Tristan Helgasoncbe74352016-11-09 10:43:26 +010060
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020061@implementation RTC_OBJC_TYPE (RTCPeerConnectionFactory) {
danilchape9021a32016-05-17 01:52:02 -070062 std::unique_ptr<rtc::Thread> _networkThread;
kwibergbfefb032016-05-01 14:53:46 -070063 std::unique_ptr<rtc::Thread> _workerThread;
danilchape9021a32016-05-17 01:52:02 -070064 std::unique_ptr<rtc::Thread> _signalingThread;
tkchinfce0e2c2016-08-30 12:58:11 -070065 BOOL _hasStartedAecDump;
Jon Hjelleda99da82016-01-20 13:40:30 -080066}
67
68@synthesize nativeFactory = _nativeFactory;
69
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020070- (rtc::scoped_refptr<webrtc::AudioDeviceModule>)audioDeviceModule {
71#if defined(WEBRTC_IOS)
72 return webrtc::CreateAudioDeviceModule();
73#else
74 return nullptr;
75#endif
76}
77
Jon Hjelleda99da82016-01-20 13:40:30 -080078- (instancetype)init {
kthelgasonfb143122017-07-25 07:55:58 -070079#ifdef HAVE_NO_MEDIA
Anders Carlsson7e042812017-10-05 16:55:38 +020080 return [self initWithNoMedia];
Magnus Jedvert8b4e92d2018-04-13 15:36:43 +020081#else
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020082 return [self
83 initWithNativeAudioEncoderFactory:webrtc::CreateBuiltinAudioEncoderFactory()
84 nativeAudioDecoderFactory:webrtc::CreateBuiltinAudioDecoderFactory()
85 nativeVideoEncoderFactory:webrtc::ObjCToNativeVideoEncoderFactory([[RTC_OBJC_TYPE(
86 RTCVideoEncoderFactoryH264) alloc] init])
87 nativeVideoDecoderFactory:webrtc::ObjCToNativeVideoDecoderFactory([[RTC_OBJC_TYPE(
88 RTCVideoDecoderFactoryH264) alloc] init])
89 audioDeviceModule:[self audioDeviceModule]
Niels Möller938bc332020-06-10 17:36:17 +020090 audioProcessingModule:nullptr];
kthelgasonfb143122017-07-25 07:55:58 -070091#endif
92}
93
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020094- (instancetype)
95 initWithEncoderFactory:(nullable id<RTC_OBJC_TYPE(RTCVideoEncoderFactory)>)encoderFactory
Niels Möller938bc332020-06-10 17:36:17 +020096 decoderFactory:(nullable id<RTC_OBJC_TYPE(RTCVideoDecoderFactory)>)decoderFactory {
magjedb8853ca2017-08-29 03:57:22 -070097#ifdef HAVE_NO_MEDIA
Anders Carlsson7e042812017-10-05 16:55:38 +020098 return [self initWithNoMedia];
magjedb8853ca2017-08-29 03:57:22 -070099#else
Anders Carlsson7e042812017-10-05 16:55:38 +0200100 std::unique_ptr<webrtc::VideoEncoderFactory> native_encoder_factory;
101 std::unique_ptr<webrtc::VideoDecoderFactory> native_decoder_factory;
102 if (encoderFactory) {
Anders Carlsson3ff50fb2018-02-01 15:47:05 +0100103 native_encoder_factory = webrtc::ObjCToNativeVideoEncoderFactory(encoderFactory);
Anders Carlsson7e042812017-10-05 16:55:38 +0200104 }
105 if (decoderFactory) {
Anders Carlsson3ff50fb2018-02-01 15:47:05 +0100106 native_decoder_factory = webrtc::ObjCToNativeVideoDecoderFactory(decoderFactory);
Anders Carlsson7e042812017-10-05 16:55:38 +0200107 }
magjedb8853ca2017-08-29 03:57:22 -0700108 return [self initWithNativeAudioEncoderFactory:webrtc::CreateBuiltinAudioEncoderFactory()
109 nativeAudioDecoderFactory:webrtc::CreateBuiltinAudioDecoderFactory()
Anders Carlsson7e042812017-10-05 16:55:38 +0200110 nativeVideoEncoderFactory:std::move(native_encoder_factory)
Sean Rosenbaume5c42652017-10-30 07:50:17 -0700111 nativeVideoDecoderFactory:std::move(native_decoder_factory)
Peter Hanspers8d95e3b2018-05-15 10:22:36 +0200112 audioDeviceModule:[self audioDeviceModule]
Niels Möller938bc332020-06-10 17:36:17 +0200113 audioProcessingModule:nullptr];
magjedb8853ca2017-08-29 03:57:22 -0700114#endif
115}
Anders Carlsson7e042812017-10-05 16:55:38 +0200116- (instancetype)initNative {
kthelgasonfb143122017-07-25 07:55:58 -0700117 if (self = [super init]) {
danilchape9021a32016-05-17 01:52:02 -0700118 _networkThread = rtc::Thread::CreateWithSocketServer();
Yura Yaroshevichcef06502018-05-01 00:58:43 +0300119 _networkThread->SetName("network_thread", _networkThread.get());
danilchape9021a32016-05-17 01:52:02 -0700120 BOOL result = _networkThread->Start();
121 NSAssert(result, @"Failed to start network thread.");
122
123 _workerThread = rtc::Thread::Create();
Yura Yaroshevichcef06502018-05-01 00:58:43 +0300124 _workerThread->SetName("worker_thread", _workerThread.get());
Jon Hjelleda99da82016-01-20 13:40:30 -0800125 result = _workerThread->Start();
126 NSAssert(result, @"Failed to start worker thread.");
127
danilchape9021a32016-05-17 01:52:02 -0700128 _signalingThread = rtc::Thread::Create();
Yura Yaroshevichcef06502018-05-01 00:58:43 +0300129 _signalingThread->SetName("signaling_thread", _signalingThread.get());
danilchape9021a32016-05-17 01:52:02 -0700130 result = _signalingThread->Start();
131 NSAssert(result, @"Failed to start signaling thread.");
Anders Carlsson7e042812017-10-05 16:55:38 +0200132 }
133 return self;
134}
135
136- (instancetype)initWithNoMedia {
137 if (self = [self initNative]) {
Danil Chapovalovaaa11432019-05-17 13:20:14 +0200138 webrtc::PeerConnectionFactoryDependencies dependencies;
139 dependencies.network_thread = _networkThread.get();
140 dependencies.worker_thread = _workerThread.get();
141 dependencies.signaling_thread = _signalingThread.get();
Taylor Brandstetterea7fbfb2020-08-19 16:41:54 -0700142 if (webrtc::field_trial::IsEnabled("WebRTC-Network-UseNWPathMonitor")) {
143 dependencies.network_monitor_factory = webrtc::CreateNetworkMonitorFactory();
144 }
Danil Chapovalovaaa11432019-05-17 13:20:14 +0200145 _nativeFactory = webrtc::CreateModularPeerConnectionFactory(std::move(dependencies));
Anders Carlsson7e042812017-10-05 16:55:38 +0200146 NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
147 }
148 return self;
149}
150
151- (instancetype)initWithNativeAudioEncoderFactory:
152 (rtc::scoped_refptr<webrtc::AudioEncoderFactory>)audioEncoderFactory
153 nativeAudioDecoderFactory:
154 (rtc::scoped_refptr<webrtc::AudioDecoderFactory>)audioDecoderFactory
155 nativeVideoEncoderFactory:
156 (std::unique_ptr<webrtc::VideoEncoderFactory>)videoEncoderFactory
157 nativeVideoDecoderFactory:
Sean Rosenbaume5c42652017-10-30 07:50:17 -0700158 (std::unique_ptr<webrtc::VideoDecoderFactory>)videoDecoderFactory
Sebastian Jansson77c0a622019-04-23 10:24:03 +0200159 audioDeviceModule:(webrtc::AudioDeviceModule *)audioDeviceModule
Sam Zackrisson6124aac2017-11-13 14:56:02 +0100160 audioProcessingModule:
161 (rtc::scoped_refptr<webrtc::AudioProcessing>)audioProcessingModule {
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700162 return [self initWithNativeAudioEncoderFactory:audioEncoderFactory
163 nativeAudioDecoderFactory:audioDecoderFactory
164 nativeVideoEncoderFactory:std::move(videoEncoderFactory)
165 nativeVideoDecoderFactory:std::move(videoDecoderFactory)
166 audioDeviceModule:audioDeviceModule
167 audioProcessingModule:audioProcessingModule
Niels Möller938bc332020-06-10 17:36:17 +0200168 networkControllerFactory:nullptr];
Sebastian Jansson77c0a622019-04-23 10:24:03 +0200169}
170- (instancetype)initWithNativeAudioEncoderFactory:
171 (rtc::scoped_refptr<webrtc::AudioEncoderFactory>)audioEncoderFactory
172 nativeAudioDecoderFactory:
173 (rtc::scoped_refptr<webrtc::AudioDecoderFactory>)audioDecoderFactory
174 nativeVideoEncoderFactory:
175 (std::unique_ptr<webrtc::VideoEncoderFactory>)videoEncoderFactory
176 nativeVideoDecoderFactory:
177 (std::unique_ptr<webrtc::VideoDecoderFactory>)videoDecoderFactory
178 audioDeviceModule:(webrtc::AudioDeviceModule *)audioDeviceModule
179 audioProcessingModule:
180 (rtc::scoped_refptr<webrtc::AudioProcessing>)audioProcessingModule
181 networkControllerFactory:
182 (std::unique_ptr<webrtc::NetworkControllerFactoryInterface>)
Niels Möller938bc332020-06-10 17:36:17 +0200183 networkControllerFactory {
Anders Carlsson7e042812017-10-05 16:55:38 +0200184 if (self = [self initNative]) {
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700185 webrtc::PeerConnectionFactoryDependencies dependencies;
186 dependencies.network_thread = _networkThread.get();
187 dependencies.worker_thread = _workerThread.get();
188 dependencies.signaling_thread = _signalingThread.get();
Taylor Brandstetterea7fbfb2020-08-19 16:41:54 -0700189 if (webrtc::field_trial::IsEnabled("WebRTC-Network-UseNWPathMonitor")) {
190 dependencies.network_monitor_factory = webrtc::CreateNetworkMonitorFactory();
191 }
Danil Chapovalovaaa11432019-05-17 13:20:14 +0200192#ifndef HAVE_NO_MEDIA
193 dependencies.task_queue_factory = webrtc::CreateDefaultTaskQueueFactory();
Erik Språngceb44952020-09-22 11:36:35 +0200194 dependencies.trials = std::make_unique<webrtc::FieldTrialBasedConfig>();
Danil Chapovalovaaa11432019-05-17 13:20:14 +0200195 cricket::MediaEngineDependencies media_deps;
196 media_deps.adm = std::move(audioDeviceModule);
197 media_deps.task_queue_factory = dependencies.task_queue_factory.get();
198 media_deps.audio_encoder_factory = std::move(audioEncoderFactory);
199 media_deps.audio_decoder_factory = std::move(audioDecoderFactory);
200 media_deps.video_encoder_factory = std::move(videoEncoderFactory);
201 media_deps.video_decoder_factory = std::move(videoDecoderFactory);
202 if (audioProcessingModule) {
203 media_deps.audio_processing = std::move(audioProcessingModule);
204 } else {
205 media_deps.audio_processing = webrtc::AudioProcessingBuilder().Create();
206 }
Erik Språngceb44952020-09-22 11:36:35 +0200207 media_deps.trials = dependencies.trials.get();
Danil Chapovalovaaa11432019-05-17 13:20:14 +0200208 dependencies.media_engine = cricket::CreateMediaEngine(std::move(media_deps));
209 dependencies.call_factory = webrtc::CreateCallFactory();
210 dependencies.event_log_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200211 std::make_unique<webrtc::RtcEventLogFactory>(dependencies.task_queue_factory.get());
Sebastian Jansson77c0a622019-04-23 10:24:03 +0200212 dependencies.network_controller_factory = std::move(networkControllerFactory);
Danil Chapovalovaaa11432019-05-17 13:20:14 +0200213#endif
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700214 _nativeFactory = webrtc::CreateModularPeerConnectionFactory(std::move(dependencies));
Anders Carlsson7e042812017-10-05 16:55:38 +0200215 NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
216 }
217 return self;
Anders Carlsson7e042812017-10-05 16:55:38 +0200218}
219
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200220- (RTC_OBJC_TYPE(RTCAudioSource) *)audioSourceWithConstraints:
221 (nullable RTC_OBJC_TYPE(RTCMediaConstraints) *)constraints {
tkchind4bfbfc2016-08-30 11:56:05 -0700222 std::unique_ptr<webrtc::MediaConstraints> nativeConstraints;
223 if (constraints) {
224 nativeConstraints = constraints.nativeConstraints;
225 }
Niels Möller2d02e082018-05-21 11:23:35 +0200226 cricket::AudioOptions options;
227 CopyConstraintsIntoAudioOptions(nativeConstraints.get(), &options);
228
tkchind4bfbfc2016-08-30 11:56:05 -0700229 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200230 _nativeFactory->CreateAudioSource(options);
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200231 return [[RTC_OBJC_TYPE(RTCAudioSource) alloc] initWithFactory:self nativeAudioSource:source];
tkchind4bfbfc2016-08-30 11:56:05 -0700232}
233
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200234- (RTC_OBJC_TYPE(RTCAudioTrack) *)audioTrackWithTrackId:(NSString *)trackId {
235 RTC_OBJC_TYPE(RTCAudioSource) *audioSource = [self audioSourceWithConstraints:nil];
tkchind4bfbfc2016-08-30 11:56:05 -0700236 return [self audioTrackWithSource:audioSource trackId:trackId];
237}
238
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200239- (RTC_OBJC_TYPE(RTCAudioTrack) *)audioTrackWithSource:(RTC_OBJC_TYPE(RTCAudioSource) *)source
240 trackId:(NSString *)trackId {
241 return [[RTC_OBJC_TYPE(RTCAudioTrack) alloc] initWithFactory:self source:source trackId:trackId];
tkchind4bfbfc2016-08-30 11:56:05 -0700242}
243
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200244- (RTC_OBJC_TYPE(RTCVideoSource) *)videoSource {
245 return [[RTC_OBJC_TYPE(RTCVideoSource) alloc] initWithFactory:self
246 signalingThread:_signalingThread.get()
247 workerThread:_workerThread.get()];
magjedabb84b82017-03-28 01:56:41 -0700248}
249
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200250- (RTC_OBJC_TYPE(RTCVideoTrack) *)videoTrackWithSource:(RTC_OBJC_TYPE(RTCVideoSource) *)source
251 trackId:(NSString *)trackId {
252 return [[RTC_OBJC_TYPE(RTCVideoTrack) alloc] initWithFactory:self source:source trackId:trackId];
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700253}
254
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200255- (RTC_OBJC_TYPE(RTCMediaStream) *)mediaStreamWithStreamId:(NSString *)streamId {
256 return [[RTC_OBJC_TYPE(RTCMediaStream) alloc] initWithFactory:self streamId:streamId];
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700257}
258
Yura Yaroshevichd140c8f2021-03-02 23:25:10 +0300259- (nullable RTC_OBJC_TYPE(RTCPeerConnection) *)
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200260 peerConnectionWithConfiguration:(RTC_OBJC_TYPE(RTCConfiguration) *)configuration
261 constraints:(RTC_OBJC_TYPE(RTCMediaConstraints) *)constraints
262 delegate:
263 (nullable id<RTC_OBJC_TYPE(RTCPeerConnectionDelegate)>)delegate {
264 return [[RTC_OBJC_TYPE(RTCPeerConnection) alloc] initWithFactory:self
265 configuration:configuration
266 constraints:constraints
267 delegate:delegate];
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700268}
269
Yura Yaroshevichd140c8f2021-03-02 23:25:10 +0300270- (nullable RTC_OBJC_TYPE(RTCPeerConnection) *)
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200271 peerConnectionWithDependencies:(RTC_OBJC_TYPE(RTCConfiguration) *)configuration
272 constraints:(RTC_OBJC_TYPE(RTCMediaConstraints) *)constraints
Jonas Oreland285f83d2020-02-07 10:30:08 +0100273 dependencies:(std::unique_ptr<webrtc::PeerConnectionDependencies>)dependencies
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200274 delegate:(id<RTC_OBJC_TYPE(RTCPeerConnectionDelegate)>)delegate {
275 return [[RTC_OBJC_TYPE(RTCPeerConnection) alloc] initWithDependencies:self
276 configuration:configuration
277 constraints:constraints
278 dependencies:std::move(dependencies)
279 delegate:delegate];
Jonas Oreland285f83d2020-02-07 10:30:08 +0100280}
281
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200282- (void)setOptions:(nonnull RTC_OBJC_TYPE(RTCPeerConnectionFactoryOptions) *)options {
Yura Yaroshevichbf567122018-01-02 13:33:16 +0300283 RTC_DCHECK(options != nil);
284 _nativeFactory->SetOptions(options.nativeOptions);
285}
286
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000287- (BOOL)startAecDumpWithFilePath:(NSString *)filePath
288 maxSizeInBytes:(int64_t)maxSizeInBytes {
tkchinfce0e2c2016-08-30 12:58:11 -0700289 RTC_DCHECK(filePath.length);
290 RTC_DCHECK_GT(maxSizeInBytes, 0);
291
292 if (_hasStartedAecDump) {
293 RTCLogError(@"Aec dump already started.");
294 return NO;
295 }
Niels Möllere4ac7232019-06-24 09:39:56 +0200296 FILE *f = fopen(filePath.UTF8String, "wb");
297 if (!f) {
298 RTCLogError(@"Error opening file: %@. Error: %s", filePath, strerror(errno));
tkchinfce0e2c2016-08-30 12:58:11 -0700299 return NO;
300 }
Niels Möllere4ac7232019-06-24 09:39:56 +0200301 _hasStartedAecDump = _nativeFactory->StartAecDump(f, maxSizeInBytes);
tkchinfce0e2c2016-08-30 12:58:11 -0700302 return _hasStartedAecDump;
303}
304
305- (void)stopAecDump {
306 _nativeFactory->StopAecDump();
307 _hasStartedAecDump = NO;
308}
309
Jon Hjelleda99da82016-01-20 13:40:30 -0800310@end