3 * Copyright 2013, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #if !defined(__has_feature) || !__has_feature(objc_arc)
29 #error "This file requires ARC support."
32 #import "RTCPeerConnection+Internal.h"
34 #import "RTCDataChannel+Internal.h"
35 #import "RTCEnumConverter.h"
36 #import "RTCICECandidate+Internal.h"
37 #import "RTCICEServer+Internal.h"
38 #import "RTCMediaConstraints+Internal.h"
39 #import "RTCMediaStream+Internal.h"
40 #import "RTCMediaStreamTrack+Internal.h"
41 #import "RTCPeerConnectionObserver.h"
42 #import "RTCSessionDescription+Internal.h"
43 #import "RTCSessionDescription.h"
44 #import "RTCSessionDescriptionDelegate.h"
45 #import "RTCStatsDelegate.h"
46 #import "RTCStatsReport+Internal.h"
48 #include "talk/app/webrtc/jsep.h"
50 NSString* const kRTCSessionDescriptionDelegateErrorDomain = @"RTCSDPError";
51 int const kRTCSessionDescriptionDelegateErrorCode = -1;
55 class RTCCreateSessionDescriptionObserver
56 : public CreateSessionDescriptionObserver {
58 RTCCreateSessionDescriptionObserver(
59 id<RTCSessionDescriptionDelegate> delegate,
60 RTCPeerConnection* peerConnection) {
62 _peerConnection = peerConnection;
65 virtual void OnSuccess(SessionDescriptionInterface* desc) OVERRIDE {
66 RTCSessionDescription* session =
67 [[RTCSessionDescription alloc] initWithSessionDescription:desc];
68 [_delegate peerConnection:_peerConnection
69 didCreateSessionDescription:session
74 virtual void OnFailure(const std::string& error) OVERRIDE {
75 NSString* str = @(error.c_str());
77 [NSError errorWithDomain:kRTCSessionDescriptionDelegateErrorDomain
78 code:kRTCSessionDescriptionDelegateErrorCode
79 userInfo:@{@"error" : str}];
80 [_delegate peerConnection:_peerConnection
81 didCreateSessionDescription:nil
86 id<RTCSessionDescriptionDelegate> _delegate;
87 RTCPeerConnection* _peerConnection;
90 class RTCSetSessionDescriptionObserver : public SetSessionDescriptionObserver {
92 RTCSetSessionDescriptionObserver(id<RTCSessionDescriptionDelegate> delegate,
93 RTCPeerConnection* peerConnection) {
95 _peerConnection = peerConnection;
98 virtual void OnSuccess() OVERRIDE {
99 [_delegate peerConnection:_peerConnection
100 didSetSessionDescriptionWithError:nil];
103 virtual void OnFailure(const std::string& error) OVERRIDE {
104 NSString* str = @(error.c_str());
106 [NSError errorWithDomain:kRTCSessionDescriptionDelegateErrorDomain
107 code:kRTCSessionDescriptionDelegateErrorCode
108 userInfo:@{@"error" : str}];
109 [_delegate peerConnection:_peerConnection
110 didSetSessionDescriptionWithError:err];
114 id<RTCSessionDescriptionDelegate> _delegate;
115 RTCPeerConnection* _peerConnection;
118 class RTCStatsObserver : public StatsObserver {
120 RTCStatsObserver(id<RTCStatsDelegate> delegate,
121 RTCPeerConnection* peerConnection) {
122 _delegate = delegate;
123 _peerConnection = peerConnection;
126 virtual void OnComplete(const std::vector<StatsReport>& reports) OVERRIDE {
127 NSMutableArray* stats = [NSMutableArray arrayWithCapacity:reports.size()];
128 std::vector<StatsReport>::const_iterator it = reports.begin();
129 for (; it != reports.end(); ++it) {
130 RTCStatsReport* statsReport =
131 [[RTCStatsReport alloc] initWithStatsReport:*it];
132 [stats addObject:statsReport];
134 [_delegate peerConnection:_peerConnection didGetStats:stats];
138 id<RTCStatsDelegate> _delegate;
139 RTCPeerConnection* _peerConnection;
143 @implementation RTCPeerConnection {
144 NSMutableArray* _localStreams;
145 rtc::scoped_ptr<webrtc::RTCPeerConnectionObserver> _observer;
146 rtc::scoped_refptr<webrtc::PeerConnectionInterface> _peerConnection;
149 - (BOOL)addICECandidate:(RTCICECandidate*)candidate {
150 rtc::scoped_ptr<const webrtc::IceCandidateInterface> iceCandidate(
151 candidate.candidate);
152 return self.peerConnection->AddIceCandidate(iceCandidate.get());
155 - (BOOL)addStream:(RTCMediaStream*)stream {
156 BOOL ret = self.peerConnection->AddStream(stream.mediaStream);
160 [_localStreams addObject:stream];
164 - (RTCDataChannel*)createDataChannelWithLabel:(NSString*)label
165 config:(RTCDataChannelInit*)config {
166 std::string labelString([label UTF8String]);
167 rtc::scoped_refptr<webrtc::DataChannelInterface> dataChannel =
168 self.peerConnection->CreateDataChannel(labelString,
169 config.dataChannelInit);
170 return [[RTCDataChannel alloc] initWithDataChannel:dataChannel];
173 - (void)createAnswerWithDelegate:(id<RTCSessionDescriptionDelegate>)delegate
174 constraints:(RTCMediaConstraints*)constraints {
175 rtc::scoped_refptr<webrtc::RTCCreateSessionDescriptionObserver>
176 observer(new rtc::RefCountedObject<
177 webrtc::RTCCreateSessionDescriptionObserver>(delegate, self));
178 self.peerConnection->CreateAnswer(observer, constraints.constraints);
181 - (void)createOfferWithDelegate:(id<RTCSessionDescriptionDelegate>)delegate
182 constraints:(RTCMediaConstraints*)constraints {
183 rtc::scoped_refptr<webrtc::RTCCreateSessionDescriptionObserver>
184 observer(new rtc::RefCountedObject<
185 webrtc::RTCCreateSessionDescriptionObserver>(delegate, self));
186 self.peerConnection->CreateOffer(observer, constraints.constraints);
189 - (void)removeStream:(RTCMediaStream*)stream {
190 self.peerConnection->RemoveStream(stream.mediaStream);
191 [_localStreams removeObject:stream];
194 - (void)setLocalDescriptionWithDelegate:
195 (id<RTCSessionDescriptionDelegate>)delegate
196 sessionDescription:(RTCSessionDescription*)sdp {
197 rtc::scoped_refptr<webrtc::RTCSetSessionDescriptionObserver> observer(
198 new rtc::RefCountedObject<webrtc::RTCSetSessionDescriptionObserver>(
200 self.peerConnection->SetLocalDescription(observer, sdp.sessionDescription);
203 - (void)setRemoteDescriptionWithDelegate:
204 (id<RTCSessionDescriptionDelegate>)delegate
205 sessionDescription:(RTCSessionDescription*)sdp {
206 rtc::scoped_refptr<webrtc::RTCSetSessionDescriptionObserver> observer(
207 new rtc::RefCountedObject<webrtc::RTCSetSessionDescriptionObserver>(
209 self.peerConnection->SetRemoteDescription(observer, sdp.sessionDescription);
212 - (BOOL)updateICEServers:(NSArray*)servers
213 constraints:(RTCMediaConstraints*)constraints {
214 webrtc::PeerConnectionInterface::IceServers iceServers;
215 for (RTCICEServer* server in servers) {
216 iceServers.push_back(server.iceServer);
218 return self.peerConnection->UpdateIce(iceServers, constraints.constraints);
221 - (RTCSessionDescription*)localDescription {
222 const webrtc::SessionDescriptionInterface* sdi =
223 self.peerConnection->local_description();
224 return sdi ? [[RTCSessionDescription alloc] initWithSessionDescription:sdi]
228 - (NSArray*)localStreams {
229 return [_localStreams copy];
232 - (RTCSessionDescription*)remoteDescription {
233 const webrtc::SessionDescriptionInterface* sdi =
234 self.peerConnection->remote_description();
235 return sdi ? [[RTCSessionDescription alloc] initWithSessionDescription:sdi]
239 - (RTCICEConnectionState)iceConnectionState {
240 return [RTCEnumConverter
241 convertIceConnectionStateToObjC:self.peerConnection
242 ->ice_connection_state()];
245 - (RTCICEGatheringState)iceGatheringState {
246 return [RTCEnumConverter
247 convertIceGatheringStateToObjC:self.peerConnection
248 ->ice_gathering_state()];
251 - (RTCSignalingState)signalingState {
252 return [RTCEnumConverter
253 convertSignalingStateToObjC:self.peerConnection->signaling_state()];
257 self.peerConnection->Close();
260 - (BOOL)getStatsWithDelegate:(id<RTCStatsDelegate>)delegate
261 mediaStreamTrack:(RTCMediaStreamTrack*)mediaStreamTrack
262 statsOutputLevel:(RTCStatsOutputLevel)statsOutputLevel {
263 rtc::scoped_refptr<webrtc::RTCStatsObserver> observer(
264 new rtc::RefCountedObject<webrtc::RTCStatsObserver>(delegate,
266 webrtc::PeerConnectionInterface::StatsOutputLevel nativeOutputLevel =
267 [RTCEnumConverter convertStatsOutputLevelToNative:statsOutputLevel];
268 return self.peerConnection->GetStats(
269 observer, mediaStreamTrack.mediaTrack, nativeOutputLevel);
274 @implementation RTCPeerConnection (Internal)
276 - (instancetype)initWithFactory:(webrtc::PeerConnectionFactoryInterface*)factory
277 iceServers:(const webrtc::PeerConnectionInterface::IceServers&)iceServers
278 constraints:(const webrtc::MediaConstraintsInterface*)constraints {
279 NSParameterAssert(factory != NULL);
280 if (self = [super init]) {
281 _observer.reset(new webrtc::RTCPeerConnectionObserver(self));
282 _peerConnection = factory->CreatePeerConnection(
283 iceServers, constraints, NULL, NULL, _observer.get());
284 _localStreams = [[NSMutableArray alloc] init];
289 - (rtc::scoped_refptr<webrtc::PeerConnectionInterface>)peerConnection {
290 return _peerConnection;