blob: 403db040fb8622648d4832a0ec2086097ab37d78 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020053#include "media/engine/webrtcmediaengine.h" // nogncheck
Kári Tristan Helgasoncbe74352016-11-09 10:43:26 +010054
Jon Hjelleda99da82016-01-20 13:40:30 -080055@implementation RTCPeerConnectionFactory {
danilchape9021a32016-05-17 01:52:02 -070056 std::unique_ptr<rtc::Thread> _networkThread;
kwibergbfefb032016-05-01 14:53:46 -070057 std::unique_ptr<rtc::Thread> _workerThread;
danilchape9021a32016-05-17 01:52:02 -070058 std::unique_ptr<rtc::Thread> _signalingThread;
tkchinfce0e2c2016-08-30 12:58:11 -070059 BOOL _hasStartedAecDump;
Jon Hjelleda99da82016-01-20 13:40:30 -080060}
61
62@synthesize nativeFactory = _nativeFactory;
63
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020064- (rtc::scoped_refptr<webrtc::AudioDeviceModule>)audioDeviceModule {
65#if defined(WEBRTC_IOS)
66 return webrtc::CreateAudioDeviceModule();
67#else
68 return nullptr;
69#endif
70}
71
Jon Hjelleda99da82016-01-20 13:40:30 -080072- (instancetype)init {
kthelgasonfb143122017-07-25 07:55:58 -070073#ifdef HAVE_NO_MEDIA
Anders Carlsson7e042812017-10-05 16:55:38 +020074 return [self initWithNoMedia];
Magnus Jedvert8b4e92d2018-04-13 15:36:43 +020075#else
Anders Carlssondd8c1652018-01-30 10:32:13 +010076 return [self initWithNativeAudioEncoderFactory:webrtc::CreateBuiltinAudioEncoderFactory()
77 nativeAudioDecoderFactory:webrtc::CreateBuiltinAudioDecoderFactory()
Anders Carlsson3ff50fb2018-02-01 15:47:05 +010078 nativeVideoEncoderFactory:webrtc::ObjCToNativeVideoEncoderFactory(
79 [[RTCVideoEncoderFactoryH264 alloc] init])
80 nativeVideoDecoderFactory:webrtc::ObjCToNativeVideoDecoderFactory(
81 [[RTCVideoDecoderFactoryH264 alloc] init])
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020082 audioDeviceModule:[self audioDeviceModule]
Anders Carlssondd8c1652018-01-30 10:32:13 +010083 audioProcessingModule:nullptr];
kthelgasonfb143122017-07-25 07:55:58 -070084#endif
85}
86
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +000087- (instancetype)initWithEncoderFactory:(nullable id<RTCVideoEncoderFactory>)encoderFactory
88 decoderFactory:(nullable id<RTCVideoDecoderFactory>)decoderFactory {
magjedb8853ca2017-08-29 03:57:22 -070089#ifdef HAVE_NO_MEDIA
Anders Carlsson7e042812017-10-05 16:55:38 +020090 return [self initWithNoMedia];
magjedb8853ca2017-08-29 03:57:22 -070091#else
Anders Carlsson7e042812017-10-05 16:55:38 +020092 std::unique_ptr<webrtc::VideoEncoderFactory> native_encoder_factory;
93 std::unique_ptr<webrtc::VideoDecoderFactory> native_decoder_factory;
94 if (encoderFactory) {
Anders Carlsson3ff50fb2018-02-01 15:47:05 +010095 native_encoder_factory = webrtc::ObjCToNativeVideoEncoderFactory(encoderFactory);
Anders Carlsson7e042812017-10-05 16:55:38 +020096 }
97 if (decoderFactory) {
Anders Carlsson3ff50fb2018-02-01 15:47:05 +010098 native_decoder_factory = webrtc::ObjCToNativeVideoDecoderFactory(decoderFactory);
Anders Carlsson7e042812017-10-05 16:55:38 +020099 }
magjedb8853ca2017-08-29 03:57:22 -0700100 return [self initWithNativeAudioEncoderFactory:webrtc::CreateBuiltinAudioEncoderFactory()
101 nativeAudioDecoderFactory:webrtc::CreateBuiltinAudioDecoderFactory()
Anders Carlsson7e042812017-10-05 16:55:38 +0200102 nativeVideoEncoderFactory:std::move(native_encoder_factory)
Sean Rosenbaume5c42652017-10-30 07:50:17 -0700103 nativeVideoDecoderFactory:std::move(native_decoder_factory)
Peter Hanspers8d95e3b2018-05-15 10:22:36 +0200104 audioDeviceModule:[self audioDeviceModule]
Sam Zackrisson6124aac2017-11-13 14:56:02 +0100105 audioProcessingModule:nullptr];
magjedb8853ca2017-08-29 03:57:22 -0700106#endif
107}
108
Anders Carlsson7e042812017-10-05 16:55:38 +0200109- (instancetype)initNative {
kthelgasonfb143122017-07-25 07:55:58 -0700110 if (self = [super init]) {
danilchape9021a32016-05-17 01:52:02 -0700111 _networkThread = rtc::Thread::CreateWithSocketServer();
Yura Yaroshevichcef06502018-05-01 00:58:43 +0300112 _networkThread->SetName("network_thread", _networkThread.get());
danilchape9021a32016-05-17 01:52:02 -0700113 BOOL result = _networkThread->Start();
114 NSAssert(result, @"Failed to start network thread.");
115
116 _workerThread = rtc::Thread::Create();
Yura Yaroshevichcef06502018-05-01 00:58:43 +0300117 _workerThread->SetName("worker_thread", _workerThread.get());
Jon Hjelleda99da82016-01-20 13:40:30 -0800118 result = _workerThread->Start();
119 NSAssert(result, @"Failed to start worker thread.");
120
danilchape9021a32016-05-17 01:52:02 -0700121 _signalingThread = rtc::Thread::Create();
Yura Yaroshevichcef06502018-05-01 00:58:43 +0300122 _signalingThread->SetName("signaling_thread", _signalingThread.get());
danilchape9021a32016-05-17 01:52:02 -0700123 result = _signalingThread->Start();
124 NSAssert(result, @"Failed to start signaling thread.");
Anders Carlsson7e042812017-10-05 16:55:38 +0200125 }
126 return self;
127}
128
129- (instancetype)initWithNoMedia {
130 if (self = [self initNative]) {
zhihuanga4c113a2017-06-28 14:05:44 -0700131 _nativeFactory = webrtc::CreateModularPeerConnectionFactory(
132 _networkThread.get(),
133 _workerThread.get(),
134 _signalingThread.get(),
zhihuanga4c113a2017-06-28 14:05:44 -0700135 std::unique_ptr<cricket::MediaEngineInterface>(),
136 std::unique_ptr<webrtc::CallFactoryInterface>(),
137 std::unique_ptr<webrtc::RtcEventLogFactoryInterface>());
Anders Carlsson7e042812017-10-05 16:55:38 +0200138 NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
139 }
140 return self;
141}
142
143- (instancetype)initWithNativeAudioEncoderFactory:
144 (rtc::scoped_refptr<webrtc::AudioEncoderFactory>)audioEncoderFactory
145 nativeAudioDecoderFactory:
146 (rtc::scoped_refptr<webrtc::AudioDecoderFactory>)audioDecoderFactory
147 nativeVideoEncoderFactory:
148 (std::unique_ptr<webrtc::VideoEncoderFactory>)videoEncoderFactory
149 nativeVideoDecoderFactory:
Sean Rosenbaume5c42652017-10-30 07:50:17 -0700150 (std::unique_ptr<webrtc::VideoDecoderFactory>)videoDecoderFactory
151 audioDeviceModule:
Sam Zackrisson6124aac2017-11-13 14:56:02 +0100152 (nullable webrtc::AudioDeviceModule *)audioDeviceModule
153 audioProcessingModule:
154 (rtc::scoped_refptr<webrtc::AudioProcessing>)audioProcessingModule {
Anders Carlsson7e042812017-10-05 16:55:38 +0200155#ifdef HAVE_NO_MEDIA
156 return [self initWithNoMedia];
zhihuanga4c113a2017-06-28 14:05:44 -0700157#else
Anders Carlsson7e042812017-10-05 16:55:38 +0200158 if (self = [self initNative]) {
159 _nativeFactory = webrtc::CreatePeerConnectionFactory(_networkThread.get(),
160 _workerThread.get(),
161 _signalingThread.get(),
Sean Rosenbaume5c42652017-10-30 07:50:17 -0700162 audioDeviceModule,
Anders Carlsson7e042812017-10-05 16:55:38 +0200163 audioEncoderFactory,
164 audioDecoderFactory,
165 std::move(videoEncoderFactory),
166 std::move(videoDecoderFactory),
167 nullptr, // audio mixer
Sam Zackrisson6124aac2017-11-13 14:56:02 +0100168 audioProcessingModule);
Anders Carlsson7e042812017-10-05 16:55:38 +0200169 NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
170 }
171 return self;
172#endif
173}
174
tkchind4bfbfc2016-08-30 11:56:05 -0700175- (RTCAudioSource *)audioSourceWithConstraints:(nullable RTCMediaConstraints *)constraints {
176 std::unique_ptr<webrtc::MediaConstraints> nativeConstraints;
177 if (constraints) {
178 nativeConstraints = constraints.nativeConstraints;
179 }
Niels Möller2d02e082018-05-21 11:23:35 +0200180 cricket::AudioOptions options;
181 CopyConstraintsIntoAudioOptions(nativeConstraints.get(), &options);
182
tkchind4bfbfc2016-08-30 11:56:05 -0700183 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200184 _nativeFactory->CreateAudioSource(options);
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300185 return [[RTCAudioSource alloc] initWithFactory:self nativeAudioSource:source];
tkchind4bfbfc2016-08-30 11:56:05 -0700186}
187
188- (RTCAudioTrack *)audioTrackWithTrackId:(NSString *)trackId {
189 RTCAudioSource *audioSource = [self audioSourceWithConstraints:nil];
190 return [self audioTrackWithSource:audioSource trackId:trackId];
191}
192
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000193- (RTCAudioTrack *)audioTrackWithSource:(RTCAudioSource *)source
194 trackId:(NSString *)trackId {
195 return [[RTCAudioTrack alloc] initWithFactory:self
196 source:source
197 trackId:trackId];
tkchind4bfbfc2016-08-30 11:56:05 -0700198}
199
magjedabb84b82017-03-28 01:56:41 -0700200- (RTCVideoSource *)videoSource {
Yura Yaroshevich01cee072018-07-11 15:35:40 +0300201 return [[RTCVideoSource alloc] initWithFactory:self
202 signalingThread:_signalingThread.get()
203 workerThread:_workerThread.get()];
magjedabb84b82017-03-28 01:56:41 -0700204}
205
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000206- (RTCVideoTrack *)videoTrackWithSource:(RTCVideoSource *)source
207 trackId:(NSString *)trackId {
208 return [[RTCVideoTrack alloc] initWithFactory:self
209 source:source
210 trackId:trackId];
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700211}
212
213- (RTCMediaStream *)mediaStreamWithStreamId:(NSString *)streamId {
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000214 return [[RTCMediaStream alloc] initWithFactory:self
215 streamId:streamId];
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700216}
217
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000218- (RTCPeerConnection *)peerConnectionWithConfiguration:
219 (RTCConfiguration *)configuration
220 constraints:
221 (RTCMediaConstraints *)constraints
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700222 delegate:
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000223 (nullable id<RTCPeerConnectionDelegate>)delegate {
Tze Kwang Chinf3cb49f2016-03-22 10:57:40 -0700224 return [[RTCPeerConnection alloc] initWithFactory:self
225 configuration:configuration
226 constraints:constraints
227 delegate:delegate];
228}
229
Yura Yaroshevichbf567122018-01-02 13:33:16 +0300230- (void)setOptions:(nonnull RTCPeerConnectionFactoryOptions *)options {
231 RTC_DCHECK(options != nil);
232 _nativeFactory->SetOptions(options.nativeOptions);
233}
234
Magnus Jedvertf83dc8b2017-08-29 09:49:43 +0000235- (BOOL)startAecDumpWithFilePath:(NSString *)filePath
236 maxSizeInBytes:(int64_t)maxSizeInBytes {
tkchinfce0e2c2016-08-30 12:58:11 -0700237 RTC_DCHECK(filePath.length);
238 RTC_DCHECK_GT(maxSizeInBytes, 0);
239
240 if (_hasStartedAecDump) {
241 RTCLogError(@"Aec dump already started.");
242 return NO;
243 }
244 int fd = open(filePath.UTF8String, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
245 if (fd < 0) {
246 RTCLogError(@"Error opening file: %@. Error: %d", filePath, errno);
247 return NO;
248 }
249 _hasStartedAecDump = _nativeFactory->StartAecDump(fd, maxSizeInBytes);
250 return _hasStartedAecDump;
251}
252
253- (void)stopAecDump {
254 _nativeFactory->StopAecDump();
255 _hasStartedAecDump = NO;
256}
257
Jon Hjelleda99da82016-01-20 13:40:30 -0800258@end