blob: 8ada166d6becca2e1566513cc0a4f7b899bb10fb [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"
fischman@webrtc.orgc693a2a2014-03-24 18:56:37 +000044#import "RTCVideoCapturer+Internal.h"
45#import "RTCVideoSource+Internal.h"
46#import "RTCVideoTrack+Internal.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047
48#include "talk/app/webrtc/audiotrack.h"
49#include "talk/app/webrtc/mediastreaminterface.h"
50#include "talk/app/webrtc/peerconnectionfactory.h"
51#include "talk/app/webrtc/peerconnectioninterface.h"
52#include "talk/app/webrtc/videosourceinterface.h"
53#include "talk/app/webrtc/videotrack.h"
54#include "talk/base/logging.h"
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000055#include "talk/base/ssladapter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056
57@interface RTCPeerConnectionFactory ()
58
59@property(nonatomic, assign) talk_base::scoped_refptr<
60 webrtc::PeerConnectionFactoryInterface> nativeFactory;
61
62@end
63
64@implementation RTCPeerConnectionFactory
65
fischman@webrtc.org9ca93a82013-10-29 00:14:15 +000066@synthesize nativeFactory = _nativeFactory;
67
fischman@webrtc.org1bc19542013-08-01 18:29:45 +000068+ (void)initializeSSL {
69 BOOL initialized = talk_base::InitializeSSL();
70 NSAssert(initialized, @"Failed to initialize SSL library");
71}
72
73+ (void)deinitializeSSL {
74 BOOL deinitialized = talk_base::CleanupSSL();
75 NSAssert(deinitialized, @"Failed to deinitialize SSL library");
76}
77
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078- (id)init {
79 if ((self = [super init])) {
80 _nativeFactory = webrtc::CreatePeerConnectionFactory();
81 NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
82 // Uncomment to get sensitive logs emitted (to stderr or logcat).
83 // talk_base::LogMessage::LogToDebug(talk_base::LS_SENSITIVE);
84 }
85 return self;
86}
87
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000088- (RTCPeerConnection*)
89 peerConnectionWithICEServers:(NSArray*)servers
90 constraints:(RTCMediaConstraints*)constraints
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 delegate:(id<RTCPeerConnectionDelegate>)delegate {
92 webrtc::PeerConnectionInterface::IceServers iceServers;
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000093 for (RTCICEServer* server in servers) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 iceServers.push_back(server.iceServer);
95 }
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000096 RTCPeerConnection* pc =
tkchin@webrtc.orgacca6752014-05-30 22:26:06 +000097 [[RTCPeerConnection alloc] initWithFactory:self.nativeFactory.get()
98 iceServers:iceServers
99 constraints:constraints.constraints];
100 pc.delegate = delegate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 return pc;
102}
103
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000104- (RTCMediaStream*)mediaStreamWithLabel:(NSString*)label {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 talk_base::scoped_refptr<webrtc::MediaStreamInterface> nativeMediaStream =
106 self.nativeFactory->CreateLocalMediaStream([label UTF8String]);
107 return [[RTCMediaStream alloc] initWithMediaStream:nativeMediaStream];
108}
109
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000110- (RTCVideoSource*)videoSourceWithCapturer:(RTCVideoCapturer*)capturer
111 constraints:(RTCMediaConstraints*)constraints {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 if (!capturer) {
113 return nil;
114 }
115 talk_base::scoped_refptr<webrtc::VideoSourceInterface> source =
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000116 self.nativeFactory->CreateVideoSource([capturer takeNativeCapturer],
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 constraints.constraints);
118 return [[RTCVideoSource alloc] initWithMediaSource:source];
119}
120
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000121- (RTCVideoTrack*)videoTrackWithID:(NSString*)videoId
122 source:(RTCVideoSource*)source {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 talk_base::scoped_refptr<webrtc::VideoTrackInterface> track =
124 self.nativeFactory->CreateVideoTrack([videoId UTF8String],
125 source.videoSource);
126 return [[RTCVideoTrack alloc] initWithMediaTrack:track];
127}
128
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000129- (RTCAudioTrack*)audioTrackWithID:(NSString*)audioId {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 talk_base::scoped_refptr<webrtc::AudioTrackInterface> track =
131 self.nativeFactory->CreateAudioTrack([audioId UTF8String], NULL);
132 return [[RTCAudioTrack alloc] initWithMediaTrack:track];
133}
134
135@end