blob: 18d211fb9ec6669209dde8a2083d1622be6ae619 [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
magjedb8853ca2017-08-29 03:57:22 -070011#import "RTCPeerConnectionFactory+Native.h"
tkchin9eeb6242016-04-27 01:54:20 -070012#import "RTCPeerConnectionFactory+Private.h"
Yura Yaroshevichbf567122018-01-02 13:33:16 +030013#import "RTCPeerConnectionFactoryOptions+Private.h"
Jon Hjelleda99da82016-01-20 13:40:30 -080014
tkchind4bfbfc2016-08-30 11:56:05 -070015#import "RTCAudioSource+Private.h"
tkchin9eeb6242016-04-27 01:54:20 -070016#import "RTCAudioTrack+Private.h"
tkchind4bfbfc2016-08-30 11:56:05 -070017#import "RTCMediaConstraints+Private.h"
tkchin9eeb6242016-04-27 01:54:20 -070018#import "RTCMediaStream+Private.h"
19#import "RTCPeerConnection+Private.h"
20#import "RTCVideoSource+Private.h"
21#import "RTCVideoTrack+Private.h"
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020022#import "base/RTCLogging.h"
23#import "base/RTCVideoDecoderFactory.h"
24#import "base/RTCVideoEncoderFactory.h"
25#import "helpers/NSString+StdString.h"
kthelgasonfb143122017-07-25 07:55:58 -070026#ifndef HAVE_NO_MEDIA
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020027#import "components/video_codec/RTCVideoDecoderFactoryH264.h"
28#import "components/video_codec/RTCVideoEncoderFactoryH264.h"
magjedb8853ca2017-08-29 03:57:22 -070029// The no-media version PeerConnectionFactory doesn't depend on these files, but the gn check tool
30// is not smart enough to take the #ifdef into account.
Anders Carlsson7e042812017-10-05 16:55:38 +020031#include "api/audio_codecs/builtin_audio_decoder_factory.h" // nogncheck
32#include "api/audio_codecs/builtin_audio_encoder_factory.h" // nogncheck
Bjorn Tereliusb8b3c992019-01-09 11:15:34 +010033#include "logging/rtc_event_log/rtc_event_log_factory.h"
Anders Carlsson7e042812017-10-05 16:55:38 +020034#include "modules/audio_device/include/audio_device.h" // nogncheck
35#include "modules/audio_processing/include/audio_processing.h" // nogncheck
Anders Carlsson3ff50fb2018-02-01 15:47:05 +010036
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020037#include "sdk/objc/native/api/video_decoder_factory.h"
38#include "sdk/objc/native/api/video_encoder_factory.h"
39#include "sdk/objc/native/src/objc_video_decoder_factory.h"
40#include "sdk/objc/native/src/objc_video_encoder_factory.h"
kthelgasonfb143122017-07-25 07:55:58 -070041#endif
kwibergbfefb032016-05-01 14:53:46 -070042
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020043#if defined(WEBRTC_IOS)
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020044#import "sdk/objc/native/api/audio_device_module.h"
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020045#endif
46
zhihuanga4c113a2017-06-28 14:05:44 -070047// Adding the nogncheck to disable the including header check.
48// The no-media version PeerConnectionFactory doesn't depend on media related
49// C++ target.
50// TODO(zhihuang): Remove nogncheck once MediaEngineInterface is moved to C++
51// API layer.
Karl Wiberg918f50c2018-07-05 11:40:33 +020052#include "absl/memory/memory.h"
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -070053#include "api/media_transport_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080054#include "media/engine/webrtc_media_engine.h" // nogncheck
Kári Tristan Helgasoncbe74352016-11-09 10:43:26 +010055
Jon Hjelleda99da82016-01-20 13:40:30 -080056@implementation RTCPeerConnectionFactory {
danilchape9021a32016-05-17 01:52:02 -070057 std::unique_ptr<rtc::Thread> _networkThread;
kwibergbfefb032016-05-01 14:53:46 -070058 std::unique_ptr<rtc::Thread> _workerThread;
danilchape9021a32016-05-17 01:52:02 -070059 std::unique_ptr<rtc::Thread> _signalingThread;
tkchinfce0e2c2016-08-30 12:58:11 -070060 BOOL _hasStartedAecDump;
Jon Hjelleda99da82016-01-20 13:40:30 -080061}
62
63@synthesize nativeFactory = _nativeFactory;
64
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020065- (rtc::scoped_refptr<webrtc::AudioDeviceModule>)audioDeviceModule {
66#if defined(WEBRTC_IOS)
67 return webrtc::CreateAudioDeviceModule();
68#else
69 return nullptr;
70#endif
71}
72
Jon Hjelleda99da82016-01-20 13:40:30 -080073- (instancetype)init {
kthelgasonfb143122017-07-25 07:55:58 -070074#ifdef HAVE_NO_MEDIA
Anders Carlsson7e042812017-10-05 16:55:38 +020075 return [self initWithNoMedia];
Magnus Jedvert8b4e92d2018-04-13 15:36:43 +020076#else
Anders Carlssondd8c1652018-01-30 10:32:13 +010077 return [self initWithNativeAudioEncoderFactory:webrtc::CreateBuiltinAudioEncoderFactory()
78 nativeAudioDecoderFactory:webrtc::CreateBuiltinAudioDecoderFactory()
Anders Carlsson3ff50fb2018-02-01 15:47:05 +010079 nativeVideoEncoderFactory:webrtc::ObjCToNativeVideoEncoderFactory(
80 [[RTCVideoEncoderFactoryH264 alloc] init])
81 nativeVideoDecoderFactory:webrtc::ObjCToNativeVideoDecoderFactory(
82 [[RTCVideoDecoderFactoryH264 alloc] init])
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020083 audioDeviceModule:[self audioDeviceModule]
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -070084 audioProcessingModule:nullptr
85 mediaTransportFactory:nullptr];
kthelgasonfb143122017-07-25 07:55:58 -070086#endif
87}
88
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +000089- (instancetype)initWithEncoderFactory:(nullable id<RTCVideoEncoderFactory>)encoderFactory
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -070090 decoderFactory:(nullable id<RTCVideoDecoderFactory>)decoderFactory
91 mediaTransportFactory:
92 (std::unique_ptr<webrtc::MediaTransportFactory>)mediaTransportFactory {
magjedb8853ca2017-08-29 03:57:22 -070093#ifdef HAVE_NO_MEDIA
Anders Carlsson7e042812017-10-05 16:55:38 +020094 return [self initWithNoMedia];
magjedb8853ca2017-08-29 03:57:22 -070095#else
Anders Carlsson7e042812017-10-05 16:55:38 +020096 std::unique_ptr<webrtc::VideoEncoderFactory> native_encoder_factory;
97 std::unique_ptr<webrtc::VideoDecoderFactory> native_decoder_factory;
98 if (encoderFactory) {
Anders Carlsson3ff50fb2018-02-01 15:47:05 +010099 native_encoder_factory = webrtc::ObjCToNativeVideoEncoderFactory(encoderFactory);
Anders Carlsson7e042812017-10-05 16:55:38 +0200100 }
101 if (decoderFactory) {
Anders Carlsson3ff50fb2018-02-01 15:47:05 +0100102 native_decoder_factory = webrtc::ObjCToNativeVideoDecoderFactory(decoderFactory);
Anders Carlsson7e042812017-10-05 16:55:38 +0200103 }
magjedb8853ca2017-08-29 03:57:22 -0700104 return [self initWithNativeAudioEncoderFactory:webrtc::CreateBuiltinAudioEncoderFactory()
105 nativeAudioDecoderFactory:webrtc::CreateBuiltinAudioDecoderFactory()
Anders Carlsson7e042812017-10-05 16:55:38 +0200106 nativeVideoEncoderFactory:std::move(native_encoder_factory)
Sean Rosenbaume5c42652017-10-30 07:50:17 -0700107 nativeVideoDecoderFactory:std::move(native_decoder_factory)
Peter Hanspers8d95e3b2018-05-15 10:22:36 +0200108 audioDeviceModule:[self audioDeviceModule]
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700109 audioProcessingModule:nullptr
110 mediaTransportFactory:std::move(mediaTransportFactory)];
magjedb8853ca2017-08-29 03:57:22 -0700111#endif
112}
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700113- (instancetype)initWithEncoderFactory:(nullable id<RTCVideoEncoderFactory>)encoderFactory
114 decoderFactory:(nullable id<RTCVideoDecoderFactory>)decoderFactory {
115 return [self initWithEncoderFactory:encoderFactory
116 decoderFactory:decoderFactory
117 mediaTransportFactory:nullptr];
118}
magjedb8853ca2017-08-29 03:57:22 -0700119
Anders Carlsson7e042812017-10-05 16:55:38 +0200120- (instancetype)initNative {
kthelgasonfb143122017-07-25 07:55:58 -0700121 if (self = [super init]) {
danilchape9021a32016-05-17 01:52:02 -0700122 _networkThread = rtc::Thread::CreateWithSocketServer();
Yura Yaroshevichcef06502018-05-01 00:58:43 +0300123 _networkThread->SetName("network_thread", _networkThread.get());
danilchape9021a32016-05-17 01:52:02 -0700124 BOOL result = _networkThread->Start();
125 NSAssert(result, @"Failed to start network thread.");
126
127 _workerThread = rtc::Thread::Create();
Yura Yaroshevichcef06502018-05-01 00:58:43 +0300128 _workerThread->SetName("worker_thread", _workerThread.get());
Jon Hjelleda99da82016-01-20 13:40:30 -0800129 result = _workerThread->Start();
130 NSAssert(result, @"Failed to start worker thread.");
131
danilchape9021a32016-05-17 01:52:02 -0700132 _signalingThread = rtc::Thread::Create();
Yura Yaroshevichcef06502018-05-01 00:58:43 +0300133 _signalingThread->SetName("signaling_thread", _signalingThread.get());
danilchape9021a32016-05-17 01:52:02 -0700134 result = _signalingThread->Start();
135 NSAssert(result, @"Failed to start signaling thread.");
Anders Carlsson7e042812017-10-05 16:55:38 +0200136 }
137 return self;
138}
139
140- (instancetype)initWithNoMedia {
141 if (self = [self initNative]) {
zhihuanga4c113a2017-06-28 14:05:44 -0700142 _nativeFactory = webrtc::CreateModularPeerConnectionFactory(
143 _networkThread.get(),
144 _workerThread.get(),
145 _signalingThread.get(),
zhihuanga4c113a2017-06-28 14:05:44 -0700146 std::unique_ptr<cricket::MediaEngineInterface>(),
147 std::unique_ptr<webrtc::CallFactoryInterface>(),
148 std::unique_ptr<webrtc::RtcEventLogFactoryInterface>());
Anders Carlsson7e042812017-10-05 16:55:38 +0200149 NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
150 }
151 return self;
152}
153
154- (instancetype)initWithNativeAudioEncoderFactory:
155 (rtc::scoped_refptr<webrtc::AudioEncoderFactory>)audioEncoderFactory
156 nativeAudioDecoderFactory:
157 (rtc::scoped_refptr<webrtc::AudioDecoderFactory>)audioDecoderFactory
158 nativeVideoEncoderFactory:
159 (std::unique_ptr<webrtc::VideoEncoderFactory>)videoEncoderFactory
160 nativeVideoDecoderFactory:
Sean Rosenbaume5c42652017-10-30 07:50:17 -0700161 (std::unique_ptr<webrtc::VideoDecoderFactory>)videoDecoderFactory
Sebastian Jansson77c0a622019-04-23 10:24:03 +0200162 audioDeviceModule:(webrtc::AudioDeviceModule *)audioDeviceModule
Sam Zackrisson6124aac2017-11-13 14:56:02 +0100163 audioProcessingModule:
164 (rtc::scoped_refptr<webrtc::AudioProcessing>)audioProcessingModule {
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700165 return [self initWithNativeAudioEncoderFactory:audioEncoderFactory
166 nativeAudioDecoderFactory:audioDecoderFactory
167 nativeVideoEncoderFactory:std::move(videoEncoderFactory)
168 nativeVideoDecoderFactory:std::move(videoDecoderFactory)
169 audioDeviceModule:audioDeviceModule
170 audioProcessingModule:audioProcessingModule
171 mediaTransportFactory:nullptr];
172}
173
Sebastian Jansson77c0a622019-04-23 10:24:03 +0200174- (instancetype)initWithNativeAudioEncoderFactory:
175 (rtc::scoped_refptr<webrtc::AudioEncoderFactory>)audioEncoderFactory
176 nativeAudioDecoderFactory:
177 (rtc::scoped_refptr<webrtc::AudioDecoderFactory>)audioDecoderFactory
178 nativeVideoEncoderFactory:
179 (std::unique_ptr<webrtc::VideoEncoderFactory>)videoEncoderFactory
180 nativeVideoDecoderFactory:
181 (std::unique_ptr<webrtc::VideoDecoderFactory>)videoDecoderFactory
182 audioDeviceModule:(webrtc::AudioDeviceModule *)audioDeviceModule
183 audioProcessingModule:
184 (rtc::scoped_refptr<webrtc::AudioProcessing>)audioProcessingModule
185 mediaTransportFactory:(std::unique_ptr<webrtc::MediaTransportFactory>)
186 mediaTransportFactory {
187 return [self initWithNativeAudioEncoderFactory:audioEncoderFactory
188 nativeAudioDecoderFactory:audioDecoderFactory
189 nativeVideoEncoderFactory:std::move(videoEncoderFactory)
190 nativeVideoDecoderFactory:std::move(videoDecoderFactory)
191 audioDeviceModule:audioDeviceModule
192 audioProcessingModule:audioProcessingModule
193 networkControllerFactory:nullptr
194 mediaTransportFactory:std::move(mediaTransportFactory)];
195}
196- (instancetype)initWithNativeAudioEncoderFactory:
197 (rtc::scoped_refptr<webrtc::AudioEncoderFactory>)audioEncoderFactory
198 nativeAudioDecoderFactory:
199 (rtc::scoped_refptr<webrtc::AudioDecoderFactory>)audioDecoderFactory
200 nativeVideoEncoderFactory:
201 (std::unique_ptr<webrtc::VideoEncoderFactory>)videoEncoderFactory
202 nativeVideoDecoderFactory:
203 (std::unique_ptr<webrtc::VideoDecoderFactory>)videoDecoderFactory
204 audioDeviceModule:(webrtc::AudioDeviceModule *)audioDeviceModule
205 audioProcessingModule:
206 (rtc::scoped_refptr<webrtc::AudioProcessing>)audioProcessingModule
207 networkControllerFactory:
208 (std::unique_ptr<webrtc::NetworkControllerFactoryInterface>)
209 networkControllerFactory
210 mediaTransportFactory:(std::unique_ptr<webrtc::MediaTransportFactory>)
211 mediaTransportFactory {
Anders Carlsson7e042812017-10-05 16:55:38 +0200212#ifdef HAVE_NO_MEDIA
213 return [self initWithNoMedia];
zhihuanga4c113a2017-06-28 14:05:44 -0700214#else
Anders Carlsson7e042812017-10-05 16:55:38 +0200215 if (self = [self initNative]) {
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700216 if (!audioProcessingModule) audioProcessingModule = webrtc::AudioProcessingBuilder().Create();
217
218 std::unique_ptr<cricket::MediaEngineInterface> media_engine =
Qingsi Wang59844ce2018-11-01 04:45:53 +0000219 cricket::WebRtcMediaEngineFactory::Create(audioDeviceModule,
220 audioEncoderFactory,
221 audioDecoderFactory,
222 std::move(videoEncoderFactory),
223 std::move(videoDecoderFactory),
224 nullptr, // audio mixer
225 audioProcessingModule);
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700226
227 std::unique_ptr<webrtc::CallFactoryInterface> call_factory = webrtc::CreateCallFactory();
228
229 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
230 webrtc::CreateRtcEventLogFactory();
231 webrtc::PeerConnectionFactoryDependencies dependencies;
232 dependencies.network_thread = _networkThread.get();
233 dependencies.worker_thread = _workerThread.get();
234 dependencies.signaling_thread = _signalingThread.get();
235 dependencies.media_engine = std::move(media_engine);
236 dependencies.call_factory = std::move(call_factory);
237 dependencies.event_log_factory = std::move(event_log_factory);
Sebastian Jansson77c0a622019-04-23 10:24:03 +0200238 dependencies.network_controller_factory = std::move(networkControllerFactory);
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700239 dependencies.media_transport_factory = std::move(mediaTransportFactory);
240 _nativeFactory = webrtc::CreateModularPeerConnectionFactory(std::move(dependencies));
Anders Carlsson7e042812017-10-05 16:55:38 +0200241 NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
242 }
243 return self;
244#endif
245}
246
tkchind4bfbfc2016-08-30 11:56:05 -0700247- (RTCAudioSource *)audioSourceWithConstraints:(nullable RTCMediaConstraints *)constraints {
248 std::unique_ptr<webrtc::MediaConstraints> nativeConstraints;
249 if (constraints) {
250 nativeConstraints = constraints.nativeConstraints;
251 }
Niels Möller2d02e082018-05-21 11:23:35 +0200252 cricket::AudioOptions options;
253 CopyConstraintsIntoAudioOptions(nativeConstraints.get(), &options);
254
tkchind4bfbfc2016-08-30 11:56:05 -0700255 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200256 _nativeFactory->CreateAudioSource(options);
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300257 return [[RTCAudioSource alloc] initWithFactory:self nativeAudioSource:source];
tkchind4bfbfc2016-08-30 11:56:05 -0700258}
259
260- (RTCAudioTrack *)audioTrackWithTrackId:(NSString *)trackId {
261 RTCAudioSource *audioSource = [self audioSourceWithConstraints:nil];
262 return [self audioTrackWithSource:audioSource trackId:trackId];
263}
264
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000265- (RTCAudioTrack *)audioTrackWithSource:(RTCAudioSource *)source
266 trackId:(NSString *)trackId {
267 return [[RTCAudioTrack alloc] initWithFactory:self
268 source:source
269 trackId:trackId];
tkchind4bfbfc2016-08-30 11:56:05 -0700270}
271
magjedabb84b82017-03-28 01:56:41 -0700272- (RTCVideoSource *)videoSource {
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300273 return [[RTCVideoSource alloc] initWithFactory:self
274 signalingThread:_signalingThread.get()
275 workerThread:_workerThread.get()];
magjedabb84b82017-03-28 01:56:41 -0700276}
277
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000278- (RTCVideoTrack *)videoTrackWithSource:(RTCVideoSource *)source
279 trackId:(NSString *)trackId {
280 return [[RTCVideoTrack alloc] initWithFactory:self
281 source:source
282 trackId:trackId];
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700283}
284
285- (RTCMediaStream *)mediaStreamWithStreamId:(NSString *)streamId {
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000286 return [[RTCMediaStream alloc] initWithFactory:self
287 streamId:streamId];
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700288}
289
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000290- (RTCPeerConnection *)peerConnectionWithConfiguration:
291 (RTCConfiguration *)configuration
292 constraints:
293 (RTCMediaConstraints *)constraints
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700294 delegate:
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000295 (nullable id<RTCPeerConnectionDelegate>)delegate {
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700296 return [[RTCPeerConnection alloc] initWithFactory:self
297 configuration:configuration
298 constraints:constraints
299 delegate:delegate];
300}
301
Yura Yaroshevichbf567122018-01-02 13:33:16 +0300302- (void)setOptions:(nonnull RTCPeerConnectionFactoryOptions *)options {
303 RTC_DCHECK(options != nil);
304 _nativeFactory->SetOptions(options.nativeOptions);
305}
306
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000307- (BOOL)startAecDumpWithFilePath:(NSString *)filePath
308 maxSizeInBytes:(int64_t)maxSizeInBytes {
tkchinfce0e2c2016-08-30 12:58:11 -0700309 RTC_DCHECK(filePath.length);
310 RTC_DCHECK_GT(maxSizeInBytes, 0);
311
312 if (_hasStartedAecDump) {
313 RTCLogError(@"Aec dump already started.");
314 return NO;
315 }
316 int fd = open(filePath.UTF8String, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
317 if (fd < 0) {
318 RTCLogError(@"Error opening file: %@. Error: %d", filePath, errno);
319 return NO;
320 }
321 _hasStartedAecDump = _nativeFactory->StartAecDump(fd, maxSizeInBytes);
322 return _hasStartedAecDump;
323}
324
325- (void)stopAecDump {
326 _nativeFactory->StopAecDump();
327 _hasStartedAecDump = NO;
328}
329
Jon Hjelleda99da82016-01-20 13:40:30 -0800330@end