blob: 48476f2368f0145240deb50e454ce6938979cfe4 [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
Anders Carlsson565e3e02018-01-19 11:36:48 +010033#include "media/engine/convert_legacy_video_factory.h" // nogncheck
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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020054#include "media/engine/webrtcmediaengine.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
162 audioDeviceModule:
Sam Zackrisson6124aac2017-11-13 14:56:02 +0100163 (nullable webrtc::AudioDeviceModule *)audioDeviceModule
164 audioProcessingModule:
165 (rtc::scoped_refptr<webrtc::AudioProcessing>)audioProcessingModule {
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700166 return [self initWithNativeAudioEncoderFactory:audioEncoderFactory
167 nativeAudioDecoderFactory:audioDecoderFactory
168 nativeVideoEncoderFactory:std::move(videoEncoderFactory)
169 nativeVideoDecoderFactory:std::move(videoDecoderFactory)
170 audioDeviceModule:audioDeviceModule
171 audioProcessingModule:audioProcessingModule
172 mediaTransportFactory:nullptr];
173}
174
175- (instancetype)
176 initWithNativeAudioEncoderFactory:
177 (rtc::scoped_refptr<webrtc::AudioEncoderFactory>)audioEncoderFactory
178 nativeAudioDecoderFactory:
179 (rtc::scoped_refptr<webrtc::AudioDecoderFactory>)audioDecoderFactory
180 nativeVideoEncoderFactory:
181 (std::unique_ptr<webrtc::VideoEncoderFactory>)videoEncoderFactory
182 nativeVideoDecoderFactory:
183 (std::unique_ptr<webrtc::VideoDecoderFactory>)videoDecoderFactory
184 audioDeviceModule:(nullable webrtc::AudioDeviceModule *)audioDeviceModule
185 audioProcessingModule:
186 (rtc::scoped_refptr<webrtc::AudioProcessing>)audioProcessingModule
187 mediaTransportFactory:
188 (std::unique_ptr<webrtc::MediaTransportFactory>)mediaTransportFactory {
Anders Carlsson7e042812017-10-05 16:55:38 +0200189#ifdef HAVE_NO_MEDIA
190 return [self initWithNoMedia];
zhihuanga4c113a2017-06-28 14:05:44 -0700191#else
Anders Carlsson7e042812017-10-05 16:55:38 +0200192 if (self = [self initNative]) {
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700193 if (!audioProcessingModule) audioProcessingModule = webrtc::AudioProcessingBuilder().Create();
194
195 std::unique_ptr<cricket::MediaEngineInterface> media_engine =
Qingsi Wang59844ce2018-11-01 04:45:53 +0000196 cricket::WebRtcMediaEngineFactory::Create(audioDeviceModule,
197 audioEncoderFactory,
198 audioDecoderFactory,
199 std::move(videoEncoderFactory),
200 std::move(videoDecoderFactory),
201 nullptr, // audio mixer
202 audioProcessingModule);
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -0700203
204 std::unique_ptr<webrtc::CallFactoryInterface> call_factory = webrtc::CreateCallFactory();
205
206 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
207 webrtc::CreateRtcEventLogFactory();
208 webrtc::PeerConnectionFactoryDependencies dependencies;
209 dependencies.network_thread = _networkThread.get();
210 dependencies.worker_thread = _workerThread.get();
211 dependencies.signaling_thread = _signalingThread.get();
212 dependencies.media_engine = std::move(media_engine);
213 dependencies.call_factory = std::move(call_factory);
214 dependencies.event_log_factory = std::move(event_log_factory);
215 dependencies.media_transport_factory = std::move(mediaTransportFactory);
216 _nativeFactory = webrtc::CreateModularPeerConnectionFactory(std::move(dependencies));
Anders Carlsson7e042812017-10-05 16:55:38 +0200217 NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
218 }
219 return self;
220#endif
221}
222
tkchind4bfbfc2016-08-30 11:56:05 -0700223- (RTCAudioSource *)audioSourceWithConstraints:(nullable RTCMediaConstraints *)constraints {
224 std::unique_ptr<webrtc::MediaConstraints> nativeConstraints;
225 if (constraints) {
226 nativeConstraints = constraints.nativeConstraints;
227 }
Niels Möller2d02e082018-05-21 11:23:35 +0200228 cricket::AudioOptions options;
229 CopyConstraintsIntoAudioOptions(nativeConstraints.get(), &options);
230
tkchind4bfbfc2016-08-30 11:56:05 -0700231 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200232 _nativeFactory->CreateAudioSource(options);
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300233 return [[RTCAudioSource alloc] initWithFactory:self nativeAudioSource:source];
tkchind4bfbfc2016-08-30 11:56:05 -0700234}
235
236- (RTCAudioTrack *)audioTrackWithTrackId:(NSString *)trackId {
237 RTCAudioSource *audioSource = [self audioSourceWithConstraints:nil];
238 return [self audioTrackWithSource:audioSource trackId:trackId];
239}
240
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000241- (RTCAudioTrack *)audioTrackWithSource:(RTCAudioSource *)source
242 trackId:(NSString *)trackId {
243 return [[RTCAudioTrack alloc] initWithFactory:self
244 source:source
245 trackId:trackId];
tkchind4bfbfc2016-08-30 11:56:05 -0700246}
247
magjedabb84b82017-03-28 01:56:41 -0700248- (RTCVideoSource *)videoSource {
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300249 return [[RTCVideoSource alloc] initWithFactory:self
250 signalingThread:_signalingThread.get()
251 workerThread:_workerThread.get()];
magjedabb84b82017-03-28 01:56:41 -0700252}
253
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000254- (RTCVideoTrack *)videoTrackWithSource:(RTCVideoSource *)source
255 trackId:(NSString *)trackId {
256 return [[RTCVideoTrack alloc] initWithFactory:self
257 source:source
258 trackId:trackId];
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700259}
260
261- (RTCMediaStream *)mediaStreamWithStreamId:(NSString *)streamId {
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000262 return [[RTCMediaStream alloc] initWithFactory:self
263 streamId:streamId];
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700264}
265
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000266- (RTCPeerConnection *)peerConnectionWithConfiguration:
267 (RTCConfiguration *)configuration
268 constraints:
269 (RTCMediaConstraints *)constraints
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700270 delegate:
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000271 (nullable id<RTCPeerConnectionDelegate>)delegate {
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700272 return [[RTCPeerConnection alloc] initWithFactory:self
273 configuration:configuration
274 constraints:constraints
275 delegate:delegate];
276}
277
Yura Yaroshevichbf567122018-01-02 13:33:16 +0300278- (void)setOptions:(nonnull RTCPeerConnectionFactoryOptions *)options {
279 RTC_DCHECK(options != nil);
280 _nativeFactory->SetOptions(options.nativeOptions);
281}
282
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000283- (BOOL)startAecDumpWithFilePath:(NSString *)filePath
284 maxSizeInBytes:(int64_t)maxSizeInBytes {
tkchinfce0e2c2016-08-30 12:58:11 -0700285 RTC_DCHECK(filePath.length);
286 RTC_DCHECK_GT(maxSizeInBytes, 0);
287
288 if (_hasStartedAecDump) {
289 RTCLogError(@"Aec dump already started.");
290 return NO;
291 }
292 int fd = open(filePath.UTF8String, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
293 if (fd < 0) {
294 RTCLogError(@"Error opening file: %@. Error: %d", filePath, errno);
295 return NO;
296 }
297 _hasStartedAecDump = _nativeFactory->StartAecDump(fd, maxSizeInBytes);
298 return _hasStartedAecDump;
299}
300
301- (void)stopAecDump {
302 _nativeFactory->StopAecDump();
303 _hasStartedAecDump = NO;
304}
305
Jon Hjelleda99da82016-01-20 13:40:30 -0800306@end