blob: 3fa5159ec30b1f93fa5badc96d9cba44362af728 [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"
Anders Carlsson7e042812017-10-05 16:55:38 +020040#include "modules/audio_device/include/audio_device.h" // nogncheck
41#include "modules/audio_processing/include/audio_processing.h" // nogncheck
Anders Carlsson3ff50fb2018-02-01 15:47:05 +010042
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020043#include "sdk/objc/native/api/video_decoder_factory.h"
44#include "sdk/objc/native/api/video_encoder_factory.h"
45#include "sdk/objc/native/src/objc_video_decoder_factory.h"
46#include "sdk/objc/native/src/objc_video_encoder_factory.h"
kthelgasonfb143122017-07-25 07:55:58 -070047#endif
kwibergbfefb032016-05-01 14:53:46 -070048
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020049#if defined(WEBRTC_IOS)
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020050#import "sdk/objc/native/api/audio_device_module.h"
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020051#endif
52
zhihuanga4c113a2017-06-28 14:05:44 -070053// Adding the nogncheck to disable the including header check.
54// The no-media version PeerConnectionFactory doesn't depend on media related
55// C++ target.
56// TODO(zhihuang): Remove nogncheck once MediaEngineInterface is moved to C++
57// API layer.
Steve Anton10542f22019-01-11 09:11:00 -080058#include "media/engine/webrtc_media_engine.h" // nogncheck
Kári Tristan Helgasoncbe74352016-11-09 10:43:26 +010059
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020060@implementation RTC_OBJC_TYPE (RTCPeerConnectionFactory) {
danilchape9021a32016-05-17 01:52:02 -070061 std::unique_ptr<rtc::Thread> _networkThread;
kwibergbfefb032016-05-01 14:53:46 -070062 std::unique_ptr<rtc::Thread> _workerThread;
danilchape9021a32016-05-17 01:52:02 -070063 std::unique_ptr<rtc::Thread> _signalingThread;
tkchinfce0e2c2016-08-30 12:58:11 -070064 BOOL _hasStartedAecDump;
Jon Hjelleda99da82016-01-20 13:40:30 -080065}
66
67@synthesize nativeFactory = _nativeFactory;
68
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020069- (rtc::scoped_refptr<webrtc::AudioDeviceModule>)audioDeviceModule {
70#if defined(WEBRTC_IOS)
71 return webrtc::CreateAudioDeviceModule();
72#else
73 return nullptr;
74#endif
75}
76
Jon Hjelleda99da82016-01-20 13:40:30 -080077- (instancetype)init {
kthelgasonfb143122017-07-25 07:55:58 -070078#ifdef HAVE_NO_MEDIA
Anders Carlsson7e042812017-10-05 16:55:38 +020079 return [self initWithNoMedia];
Magnus Jedvert8b4e92d2018-04-13 15:36:43 +020080#else
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020081 return [self
82 initWithNativeAudioEncoderFactory:webrtc::CreateBuiltinAudioEncoderFactory()
83 nativeAudioDecoderFactory:webrtc::CreateBuiltinAudioDecoderFactory()
84 nativeVideoEncoderFactory:webrtc::ObjCToNativeVideoEncoderFactory([[RTC_OBJC_TYPE(
85 RTCVideoEncoderFactoryH264) alloc] init])
86 nativeVideoDecoderFactory:webrtc::ObjCToNativeVideoDecoderFactory([[RTC_OBJC_TYPE(
87 RTCVideoDecoderFactoryH264) alloc] init])
88 audioDeviceModule:[self audioDeviceModule]
Niels Möller938bc332020-06-10 17:36:17 +020089 audioProcessingModule:nullptr];
kthelgasonfb143122017-07-25 07:55:58 -070090#endif
91}
92
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020093- (instancetype)
94 initWithEncoderFactory:(nullable id<RTC_OBJC_TYPE(RTCVideoEncoderFactory)>)encoderFactory
Niels Möller938bc332020-06-10 17:36:17 +020095 decoderFactory:(nullable id<RTC_OBJC_TYPE(RTCVideoDecoderFactory)>)decoderFactory {
magjedb8853ca2017-08-29 03:57:22 -070096#ifdef HAVE_NO_MEDIA
Anders Carlsson7e042812017-10-05 16:55:38 +020097 return [self initWithNoMedia];
magjedb8853ca2017-08-29 03:57:22 -070098#else
Anders Carlsson7e042812017-10-05 16:55:38 +020099 std::unique_ptr<webrtc::VideoEncoderFactory> native_encoder_factory;
100 std::unique_ptr<webrtc::VideoDecoderFactory> native_decoder_factory;
101 if (encoderFactory) {
Anders Carlsson3ff50fb2018-02-01 15:47:05 +0100102 native_encoder_factory = webrtc::ObjCToNativeVideoEncoderFactory(encoderFactory);
Anders Carlsson7e042812017-10-05 16:55:38 +0200103 }
104 if (decoderFactory) {
Anders Carlsson3ff50fb2018-02-01 15:47:05 +0100105 native_decoder_factory = webrtc::ObjCToNativeVideoDecoderFactory(decoderFactory);
Anders Carlsson7e042812017-10-05 16:55:38 +0200106 }
magjedb8853ca2017-08-29 03:57:22 -0700107 return [self initWithNativeAudioEncoderFactory:webrtc::CreateBuiltinAudioEncoderFactory()
108 nativeAudioDecoderFactory:webrtc::CreateBuiltinAudioDecoderFactory()
Anders Carlsson7e042812017-10-05 16:55:38 +0200109 nativeVideoEncoderFactory:std::move(native_encoder_factory)
Sean Rosenbaume5c42652017-10-30 07:50:17 -0700110 nativeVideoDecoderFactory:std::move(native_decoder_factory)
Peter Hanspers8d95e3b2018-05-15 10:22:36 +0200111 audioDeviceModule:[self audioDeviceModule]
Niels Möller938bc332020-06-10 17:36:17 +0200112 audioProcessingModule:nullptr];
magjedb8853ca2017-08-29 03:57:22 -0700113#endif
114}
Anders Carlsson7e042812017-10-05 16:55:38 +0200115- (instancetype)initNative {
kthelgasonfb143122017-07-25 07:55:58 -0700116 if (self = [super init]) {
danilchape9021a32016-05-17 01:52:02 -0700117 _networkThread = rtc::Thread::CreateWithSocketServer();
Yura Yaroshevichcef06502018-05-01 00:58:43 +0300118 _networkThread->SetName("network_thread", _networkThread.get());
danilchape9021a32016-05-17 01:52:02 -0700119 BOOL result = _networkThread->Start();
120 NSAssert(result, @"Failed to start network thread.");
121
122 _workerThread = rtc::Thread::Create();
Yura Yaroshevichcef06502018-05-01 00:58:43 +0300123 _workerThread->SetName("worker_thread", _workerThread.get());
Jon Hjelleda99da82016-01-20 13:40:30 -0800124 result = _workerThread->Start();
125 NSAssert(result, @"Failed to start worker thread.");
126
danilchape9021a32016-05-17 01:52:02 -0700127 _signalingThread = rtc::Thread::Create();
Yura Yaroshevichcef06502018-05-01 00:58:43 +0300128 _signalingThread->SetName("signaling_thread", _signalingThread.get());
danilchape9021a32016-05-17 01:52:02 -0700129 result = _signalingThread->Start();
130 NSAssert(result, @"Failed to start signaling thread.");
Anders Carlsson7e042812017-10-05 16:55:38 +0200131 }
132 return self;
133}
134
135- (instancetype)initWithNoMedia {
136 if (self = [self initNative]) {
Danil Chapovalovaaa11432019-05-17 13:20:14 +0200137 webrtc::PeerConnectionFactoryDependencies dependencies;
138 dependencies.network_thread = _networkThread.get();
139 dependencies.worker_thread = _workerThread.get();
140 dependencies.signaling_thread = _signalingThread.get();
Taylor Brandstetterea7fbfb2020-08-19 16:41:54 -0700141 if (webrtc::field_trial::IsEnabled("WebRTC-Network-UseNWPathMonitor")) {
142 dependencies.network_monitor_factory = webrtc::CreateNetworkMonitorFactory();
143 }
Danil Chapovalovaaa11432019-05-17 13:20:14 +0200144 _nativeFactory = webrtc::CreateModularPeerConnectionFactory(std::move(dependencies));
Anders Carlsson7e042812017-10-05 16:55:38 +0200145 NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
146 }
147 return self;
148}
149
150- (instancetype)initWithNativeAudioEncoderFactory:
151 (rtc::scoped_refptr<webrtc::AudioEncoderFactory>)audioEncoderFactory
152 nativeAudioDecoderFactory:
153 (rtc::scoped_refptr<webrtc::AudioDecoderFactory>)audioDecoderFactory
154 nativeVideoEncoderFactory:
155 (std::unique_ptr<webrtc::VideoEncoderFactory>)videoEncoderFactory
156 nativeVideoDecoderFactory:
Sean Rosenbaume5c42652017-10-30 07:50:17 -0700157 (std::unique_ptr<webrtc::VideoDecoderFactory>)videoDecoderFactory
Sebastian Jansson77c0a622019-04-23 10:24:03 +0200158 audioDeviceModule:(webrtc::AudioDeviceModule *)audioDeviceModule
Sam Zackrisson6124aac2017-11-13 14:56:02 +0100159 audioProcessingModule:
160 (rtc::scoped_refptr<webrtc::AudioProcessing>)audioProcessingModule {
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700161 return [self initWithNativeAudioEncoderFactory:audioEncoderFactory
162 nativeAudioDecoderFactory:audioDecoderFactory
163 nativeVideoEncoderFactory:std::move(videoEncoderFactory)
164 nativeVideoDecoderFactory:std::move(videoDecoderFactory)
165 audioDeviceModule:audioDeviceModule
166 audioProcessingModule:audioProcessingModule
Niels Möller938bc332020-06-10 17:36:17 +0200167 networkControllerFactory:nullptr];
Sebastian Jansson77c0a622019-04-23 10:24:03 +0200168}
169- (instancetype)initWithNativeAudioEncoderFactory:
170 (rtc::scoped_refptr<webrtc::AudioEncoderFactory>)audioEncoderFactory
171 nativeAudioDecoderFactory:
172 (rtc::scoped_refptr<webrtc::AudioDecoderFactory>)audioDecoderFactory
173 nativeVideoEncoderFactory:
174 (std::unique_ptr<webrtc::VideoEncoderFactory>)videoEncoderFactory
175 nativeVideoDecoderFactory:
176 (std::unique_ptr<webrtc::VideoDecoderFactory>)videoDecoderFactory
177 audioDeviceModule:(webrtc::AudioDeviceModule *)audioDeviceModule
178 audioProcessingModule:
179 (rtc::scoped_refptr<webrtc::AudioProcessing>)audioProcessingModule
180 networkControllerFactory:
181 (std::unique_ptr<webrtc::NetworkControllerFactoryInterface>)
Niels Möller938bc332020-06-10 17:36:17 +0200182 networkControllerFactory {
Anders Carlsson7e042812017-10-05 16:55:38 +0200183 if (self = [self initNative]) {
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700184 webrtc::PeerConnectionFactoryDependencies dependencies;
185 dependencies.network_thread = _networkThread.get();
186 dependencies.worker_thread = _workerThread.get();
187 dependencies.signaling_thread = _signalingThread.get();
Taylor Brandstetterea7fbfb2020-08-19 16:41:54 -0700188 if (webrtc::field_trial::IsEnabled("WebRTC-Network-UseNWPathMonitor")) {
189 dependencies.network_monitor_factory = webrtc::CreateNetworkMonitorFactory();
190 }
Danil Chapovalovaaa11432019-05-17 13:20:14 +0200191#ifndef HAVE_NO_MEDIA
192 dependencies.task_queue_factory = webrtc::CreateDefaultTaskQueueFactory();
193 cricket::MediaEngineDependencies media_deps;
194 media_deps.adm = std::move(audioDeviceModule);
195 media_deps.task_queue_factory = dependencies.task_queue_factory.get();
196 media_deps.audio_encoder_factory = std::move(audioEncoderFactory);
197 media_deps.audio_decoder_factory = std::move(audioDecoderFactory);
198 media_deps.video_encoder_factory = std::move(videoEncoderFactory);
199 media_deps.video_decoder_factory = std::move(videoDecoderFactory);
200 if (audioProcessingModule) {
201 media_deps.audio_processing = std::move(audioProcessingModule);
202 } else {
203 media_deps.audio_processing = webrtc::AudioProcessingBuilder().Create();
204 }
205 dependencies.media_engine = cricket::CreateMediaEngine(std::move(media_deps));
206 dependencies.call_factory = webrtc::CreateCallFactory();
207 dependencies.event_log_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200208 std::make_unique<webrtc::RtcEventLogFactory>(dependencies.task_queue_factory.get());
Sebastian Jansson77c0a622019-04-23 10:24:03 +0200209 dependencies.network_controller_factory = std::move(networkControllerFactory);
Danil Chapovalovaaa11432019-05-17 13:20:14 +0200210#endif
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700211 _nativeFactory = webrtc::CreateModularPeerConnectionFactory(std::move(dependencies));
Anders Carlsson7e042812017-10-05 16:55:38 +0200212 NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
213 }
214 return self;
Anders Carlsson7e042812017-10-05 16:55:38 +0200215}
216
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200217- (RTC_OBJC_TYPE(RTCAudioSource) *)audioSourceWithConstraints:
218 (nullable RTC_OBJC_TYPE(RTCMediaConstraints) *)constraints {
tkchind4bfbfc2016-08-30 11:56:05 -0700219 std::unique_ptr<webrtc::MediaConstraints> nativeConstraints;
220 if (constraints) {
221 nativeConstraints = constraints.nativeConstraints;
222 }
Niels Möller2d02e082018-05-21 11:23:35 +0200223 cricket::AudioOptions options;
224 CopyConstraintsIntoAudioOptions(nativeConstraints.get(), &options);
225
tkchind4bfbfc2016-08-30 11:56:05 -0700226 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200227 _nativeFactory->CreateAudioSource(options);
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200228 return [[RTC_OBJC_TYPE(RTCAudioSource) alloc] initWithFactory:self nativeAudioSource:source];
tkchind4bfbfc2016-08-30 11:56:05 -0700229}
230
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200231- (RTC_OBJC_TYPE(RTCAudioTrack) *)audioTrackWithTrackId:(NSString *)trackId {
232 RTC_OBJC_TYPE(RTCAudioSource) *audioSource = [self audioSourceWithConstraints:nil];
tkchind4bfbfc2016-08-30 11:56:05 -0700233 return [self audioTrackWithSource:audioSource trackId:trackId];
234}
235
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200236- (RTC_OBJC_TYPE(RTCAudioTrack) *)audioTrackWithSource:(RTC_OBJC_TYPE(RTCAudioSource) *)source
237 trackId:(NSString *)trackId {
238 return [[RTC_OBJC_TYPE(RTCAudioTrack) alloc] initWithFactory:self source:source trackId:trackId];
tkchind4bfbfc2016-08-30 11:56:05 -0700239}
240
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200241- (RTC_OBJC_TYPE(RTCVideoSource) *)videoSource {
242 return [[RTC_OBJC_TYPE(RTCVideoSource) alloc] initWithFactory:self
243 signalingThread:_signalingThread.get()
244 workerThread:_workerThread.get()];
magjedabb84b82017-03-28 01:56:41 -0700245}
246
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200247- (RTC_OBJC_TYPE(RTCVideoTrack) *)videoTrackWithSource:(RTC_OBJC_TYPE(RTCVideoSource) *)source
248 trackId:(NSString *)trackId {
249 return [[RTC_OBJC_TYPE(RTCVideoTrack) alloc] initWithFactory:self source:source trackId:trackId];
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700250}
251
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200252- (RTC_OBJC_TYPE(RTCMediaStream) *)mediaStreamWithStreamId:(NSString *)streamId {
253 return [[RTC_OBJC_TYPE(RTCMediaStream) alloc] initWithFactory:self streamId:streamId];
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700254}
255
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200256- (RTC_OBJC_TYPE(RTCPeerConnection) *)
257 peerConnectionWithConfiguration:(RTC_OBJC_TYPE(RTCConfiguration) *)configuration
258 constraints:(RTC_OBJC_TYPE(RTCMediaConstraints) *)constraints
259 delegate:
260 (nullable id<RTC_OBJC_TYPE(RTCPeerConnectionDelegate)>)delegate {
261 return [[RTC_OBJC_TYPE(RTCPeerConnection) alloc] initWithFactory:self
262 configuration:configuration
263 constraints:constraints
264 delegate:delegate];
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700265}
266
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200267- (RTC_OBJC_TYPE(RTCPeerConnection) *)
268 peerConnectionWithDependencies:(RTC_OBJC_TYPE(RTCConfiguration) *)configuration
269 constraints:(RTC_OBJC_TYPE(RTCMediaConstraints) *)constraints
Jonas Oreland285f83d2020-02-07 10:30:08 +0100270 dependencies:(std::unique_ptr<webrtc::PeerConnectionDependencies>)dependencies
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200271 delegate:(id<RTC_OBJC_TYPE(RTCPeerConnectionDelegate)>)delegate {
272 return [[RTC_OBJC_TYPE(RTCPeerConnection) alloc] initWithDependencies:self
273 configuration:configuration
274 constraints:constraints
275 dependencies:std::move(dependencies)
276 delegate:delegate];
Jonas Oreland285f83d2020-02-07 10:30:08 +0100277}
278
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200279- (void)setOptions:(nonnull RTC_OBJC_TYPE(RTCPeerConnectionFactoryOptions) *)options {
Yura Yaroshevichbf567122018-01-02 13:33:16 +0300280 RTC_DCHECK(options != nil);
281 _nativeFactory->SetOptions(options.nativeOptions);
282}
283
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000284- (BOOL)startAecDumpWithFilePath:(NSString *)filePath
285 maxSizeInBytes:(int64_t)maxSizeInBytes {
tkchinfce0e2c2016-08-30 12:58:11 -0700286 RTC_DCHECK(filePath.length);
287 RTC_DCHECK_GT(maxSizeInBytes, 0);
288
289 if (_hasStartedAecDump) {
290 RTCLogError(@"Aec dump already started.");
291 return NO;
292 }
Niels Möllere4ac7232019-06-24 09:39:56 +0200293 FILE *f = fopen(filePath.UTF8String, "wb");
294 if (!f) {
295 RTCLogError(@"Error opening file: %@. Error: %s", filePath, strerror(errno));
tkchinfce0e2c2016-08-30 12:58:11 -0700296 return NO;
297 }
Niels Möllere4ac7232019-06-24 09:39:56 +0200298 _hasStartedAecDump = _nativeFactory->StartAecDump(f, maxSizeInBytes);
tkchinfce0e2c2016-08-30 12:58:11 -0700299 return _hasStartedAecDump;
300}
301
302- (void)stopAecDump {
303 _nativeFactory->StopAecDump();
304 _hasStartedAecDump = NO;
305}
306
Jon Hjelleda99da82016-01-20 13:40:30 -0800307@end