1/*
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
36#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"
43#import "RTCPeerConnectionDelegate.h"
44#import "RTCVideoCapturer+Internal.h"
45#import "RTCVideoSource+Internal.h"
46#import "RTCVideoTrack+Internal.h"
47
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 "webrtc/base/logging.h"
55#include "webrtc/base/ssladapter.h"
56
57@interface RTCPeerConnectionFactory ()
58
59@property(nonatomic, assign) rtc::scoped_refptr<
60    webrtc::PeerConnectionFactoryInterface> nativeFactory;
61
62@end
63
64@implementation RTCPeerConnectionFactory
65
66@synthesize nativeFactory = _nativeFactory;
67
68+ (void)initializeSSL {
69  BOOL initialized = rtc::InitializeSSL();
70  NSAssert(initialized, @"Failed to initialize SSL library");
71}
72
73+ (void)deinitializeSSL {
74  BOOL deinitialized = rtc::CleanupSSL();
75  NSAssert(deinitialized, @"Failed to deinitialize SSL library");
76}
77
78- (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    // rtc::LogMessage::LogToDebug(rtc::LS_SENSITIVE);
84  }
85  return self;
86}
87
88- (RTCPeerConnection*)
89    peerConnectionWithICEServers:(NSArray*)servers
90                     constraints:(RTCMediaConstraints*)constraints
91                        delegate:(id<RTCPeerConnectionDelegate>)delegate {
92  webrtc::PeerConnectionInterface::IceServers iceServers;
93  for (RTCICEServer* server in servers) {
94    iceServers.push_back(server.iceServer);
95  }
96  RTCPeerConnection* pc =
97      [[RTCPeerConnection alloc] initWithFactory:self.nativeFactory.get()
98                                      iceServers:iceServers
99                                     constraints:constraints.constraints];
100  pc.delegate = delegate;
101  return pc;
102}
103
104- (RTCMediaStream*)mediaStreamWithLabel:(NSString*)label {
105  rtc::scoped_refptr<webrtc::MediaStreamInterface> nativeMediaStream =
106      self.nativeFactory->CreateLocalMediaStream([label UTF8String]);
107  return [[RTCMediaStream alloc] initWithMediaStream:nativeMediaStream];
108}
109
110- (RTCVideoSource*)videoSourceWithCapturer:(RTCVideoCapturer*)capturer
111                               constraints:(RTCMediaConstraints*)constraints {
112  if (!capturer) {
113    return nil;
114  }
115  rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
116      self.nativeFactory->CreateVideoSource([capturer takeNativeCapturer],
117                                            constraints.constraints);
118  return [[RTCVideoSource alloc] initWithMediaSource:source];
119}
120
121- (RTCVideoTrack*)videoTrackWithID:(NSString*)videoId
122                            source:(RTCVideoSource*)source {
123  rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
124      self.nativeFactory->CreateVideoTrack([videoId UTF8String],
125                                           source.videoSource);
126  return [[RTCVideoTrack alloc] initWithMediaTrack:track];
127}
128
129- (RTCAudioTrack*)audioTrackWithID:(NSString*)audioId {
130  rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
131      self.nativeFactory->CreateAudioTrack([audioId UTF8String], NULL);
132  return [[RTCAudioTrack alloc] initWithMediaTrack:track];
133}
134
135@end
136