blob: 81af8e11febd39e089b80ff47e369b792f3179de [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,
103 dummy_dtls_identity_service,
104 observer);
105 RTCPeerConnection* pc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 [[RTCPeerConnection alloc] initWithPeerConnection:peerConnection
107 observer:observer];
108 observer->SetPeerConnection(pc);
109 return pc;
110}
111
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000112- (RTCMediaStream*)mediaStreamWithLabel:(NSString*)label {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 talk_base::scoped_refptr<webrtc::MediaStreamInterface> nativeMediaStream =
114 self.nativeFactory->CreateLocalMediaStream([label UTF8String]);
115 return [[RTCMediaStream alloc] initWithMediaStream:nativeMediaStream];
116}
117
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000118- (RTCVideoSource*)videoSourceWithCapturer:(RTCVideoCapturer*)capturer
119 constraints:(RTCMediaConstraints*)constraints {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 if (!capturer) {
121 return nil;
122 }
123 talk_base::scoped_refptr<webrtc::VideoSourceInterface> source =
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000124 self.nativeFactory->CreateVideoSource([capturer takeNativeCapturer],
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 constraints.constraints);
126 return [[RTCVideoSource alloc] initWithMediaSource:source];
127}
128
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000129- (RTCVideoTrack*)videoTrackWithID:(NSString*)videoId
130 source:(RTCVideoSource*)source {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 talk_base::scoped_refptr<webrtc::VideoTrackInterface> track =
132 self.nativeFactory->CreateVideoTrack([videoId UTF8String],
133 source.videoSource);
134 return [[RTCVideoTrack alloc] initWithMediaTrack:track];
135}
136
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000137- (RTCAudioTrack*)audioTrackWithID:(NSString*)audioId {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 talk_base::scoped_refptr<webrtc::AudioTrackInterface> track =
139 self.nativeFactory->CreateAudioTrack([audioId UTF8String], NULL);
140 return [[RTCAudioTrack alloc] initWithMediaTrack:track];
141}
142
143@end