blob: b12af9dfa369923eaae2ae4958df239bbf9a5fb3 [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
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 "RTCPeerConnectionObserver.h"
45#import "RTCVideoCapturer+internal.h"
46#import "RTCVideoSource+internal.h"
47#import "RTCVideoTrack+internal.h"
48
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"
56
57@interface RTCPeerConnectionFactory ()
58
59@property(nonatomic, assign) talk_base::scoped_refptr<
60 webrtc::PeerConnectionFactoryInterface> nativeFactory;
61
62@end
63
64@implementation RTCPeerConnectionFactory
65
66- (id)init {
67 if ((self = [super init])) {
68 _nativeFactory = webrtc::CreatePeerConnectionFactory();
69 NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
70 // Uncomment to get sensitive logs emitted (to stderr or logcat).
71 // talk_base::LogMessage::LogToDebug(talk_base::LS_SENSITIVE);
72 }
73 return self;
74}
75
76- (RTCPeerConnection *)
77 peerConnectionWithICEServers:(NSArray *)servers
78 constraints:(RTCMediaConstraints *)constraints
79 delegate:(id<RTCPeerConnectionDelegate>)delegate {
80 webrtc::PeerConnectionInterface::IceServers iceServers;
81 for (RTCICEServer *server in servers) {
82 iceServers.push_back(server.iceServer);
83 }
84 webrtc::RTCPeerConnectionObserver *observer =
85 new webrtc::RTCPeerConnectionObserver(delegate);
86 talk_base::scoped_refptr<webrtc::PeerConnectionInterface> peerConnection =
87 self.nativeFactory->CreatePeerConnection(
88 iceServers, constraints.constraints, observer);
89 RTCPeerConnection *pc =
90 [[RTCPeerConnection alloc] initWithPeerConnection:peerConnection
91 observer:observer];
92 observer->SetPeerConnection(pc);
93 return pc;
94}
95
96- (RTCMediaStream *)mediaStreamWithLabel:(NSString *)label {
97 talk_base::scoped_refptr<webrtc::MediaStreamInterface> nativeMediaStream =
98 self.nativeFactory->CreateLocalMediaStream([label UTF8String]);
99 return [[RTCMediaStream alloc] initWithMediaStream:nativeMediaStream];
100}
101
102- (RTCVideoSource *)videoSourceWithCapturer:(RTCVideoCapturer *)capturer
103 constraints:(RTCMediaConstraints *)constraints {
104 if (!capturer) {
105 return nil;
106 }
107 talk_base::scoped_refptr<webrtc::VideoSourceInterface> source =
108 self.nativeFactory->CreateVideoSource(capturer.capturer.get(),
109 constraints.constraints);
110 return [[RTCVideoSource alloc] initWithMediaSource:source];
111}
112
113- (RTCVideoTrack *)videoTrackWithID:(NSString *)videoId
114 source:(RTCVideoSource *)source {
115 talk_base::scoped_refptr<webrtc::VideoTrackInterface> track =
116 self.nativeFactory->CreateVideoTrack([videoId UTF8String],
117 source.videoSource);
118 return [[RTCVideoTrack alloc] initWithMediaTrack:track];
119}
120
121- (RTCAudioTrack *)audioTrackWithID:(NSString *)audioId {
122 talk_base::scoped_refptr<webrtc::AudioTrackInterface> track =
123 self.nativeFactory->CreateAudioTrack([audioId UTF8String], NULL);
124 return [[RTCAudioTrack alloc] initWithMediaTrack:track];
125}
126
127@end