blob: 78db3cd1731f0b3b7351605e89d8eef4beef8274 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2013, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#if !defined(__has_feature) || !__has_feature(objc_arc)
29#error "This file requires ARC support."
30#endif
31
32#import "RTCPeerConnectionFactory.h"
33
34#include <vector>
35
fischman@webrtc.orgc693a2a2014-03-24 18:56:37 +000036#import "RTCAudioTrack+Internal.h"
37#import "RTCICEServer+Internal.h"
38#import "RTCMediaConstraints+Internal.h"
39#import "RTCMediaSource+Internal.h"
40#import "RTCMediaStream+Internal.h"
41#import "RTCMediaStreamTrack+Internal.h"
42#import "RTCPeerConnection+Internal.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043#import "RTCPeerConnectionDelegate.h"
44#import "RTCPeerConnectionObserver.h"
fischman@webrtc.orgc693a2a2014-03-24 18:56:37 +000045#import "RTCVideoCapturer+Internal.h"
46#import "RTCVideoSource+Internal.h"
47#import "RTCVideoTrack+Internal.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
49#include "talk/app/webrtc/audiotrack.h"
50#include "talk/app/webrtc/mediastreaminterface.h"
51#include "talk/app/webrtc/peerconnectionfactory.h"
52#include "talk/app/webrtc/peerconnectioninterface.h"
53#include "talk/app/webrtc/videosourceinterface.h"
54#include "talk/app/webrtc/videotrack.h"
55#include "talk/base/logging.h"
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000056#include "talk/base/ssladapter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057
58@interface RTCPeerConnectionFactory ()
59
60@property(nonatomic, assign) talk_base::scoped_refptr<
61 webrtc::PeerConnectionFactoryInterface> nativeFactory;
62
63@end
64
65@implementation RTCPeerConnectionFactory
66
fischman@webrtc.org9ca93a82013-10-29 00:14:15 +000067@synthesize nativeFactory = _nativeFactory;
68
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000069+ (void)initializeSSL {
70 BOOL initialized = talk_base::InitializeSSL();
71 NSAssert(initialized, @"Failed to initialize SSL library");
72}
73
74+ (void)deinitializeSSL {
75 BOOL deinitialized = talk_base::CleanupSSL();
76 NSAssert(deinitialized, @"Failed to deinitialize SSL library");
77}
78
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079- (id)init {
80 if ((self = [super init])) {
81 _nativeFactory = webrtc::CreatePeerConnectionFactory();
82 NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
83 // Uncomment to get sensitive logs emitted (to stderr or logcat).
84 // talk_base::LogMessage::LogToDebug(talk_base::LS_SENSITIVE);
85 }
86 return self;
87}
88
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000089- (RTCPeerConnection*)
90 peerConnectionWithICEServers:(NSArray*)servers
91 constraints:(RTCMediaConstraints*)constraints
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 delegate:(id<RTCPeerConnectionDelegate>)delegate {
93 webrtc::PeerConnectionInterface::IceServers iceServers;
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000094 for (RTCICEServer* server in servers) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 iceServers.push_back(server.iceServer);
96 }
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000097 webrtc::RTCPeerConnectionObserver* observer =
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 new webrtc::RTCPeerConnectionObserver(delegate);
henrike@webrtc.org723d6832013-07-12 16:04:50 +000099 webrtc::DTLSIdentityServiceInterface* dummy_dtls_identity_service = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 talk_base::scoped_refptr<webrtc::PeerConnectionInterface> peerConnection =
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000101 self.nativeFactory->CreatePeerConnection(iceServers,
102 constraints.constraints,
mallinath@webrtc.orga0d30672014-04-26 00:00:15 +0000103 NULL,
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000104 dummy_dtls_identity_service,
105 observer);
106 RTCPeerConnection* pc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 [[RTCPeerConnection alloc] initWithPeerConnection:peerConnection
108 observer:observer];
109 observer->SetPeerConnection(pc);
110 return pc;
111}
112
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000113- (RTCMediaStream*)mediaStreamWithLabel:(NSString*)label {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 talk_base::scoped_refptr<webrtc::MediaStreamInterface> nativeMediaStream =
115 self.nativeFactory->CreateLocalMediaStream([label UTF8String]);
116 return [[RTCMediaStream alloc] initWithMediaStream:nativeMediaStream];
117}
118
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000119- (RTCVideoSource*)videoSourceWithCapturer:(RTCVideoCapturer*)capturer
120 constraints:(RTCMediaConstraints*)constraints {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 if (!capturer) {
122 return nil;
123 }
124 talk_base::scoped_refptr<webrtc::VideoSourceInterface> source =
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000125 self.nativeFactory->CreateVideoSource([capturer takeNativeCapturer],
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 constraints.constraints);
127 return [[RTCVideoSource alloc] initWithMediaSource:source];
128}
129
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000130- (RTCVideoTrack*)videoTrackWithID:(NSString*)videoId
131 source:(RTCVideoSource*)source {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 talk_base::scoped_refptr<webrtc::VideoTrackInterface> track =
133 self.nativeFactory->CreateVideoTrack([videoId UTF8String],
134 source.videoSource);
135 return [[RTCVideoTrack alloc] initWithMediaTrack:track];
136}
137
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000138- (RTCAudioTrack*)audioTrackWithID:(NSString*)audioId {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 talk_base::scoped_refptr<webrtc::AudioTrackInterface> track =
140 self.nativeFactory->CreateAudioTrack([audioId UTF8String], NULL);
141 return [[RTCAudioTrack alloc] initWithMediaTrack:track];
142}
143
144@end