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 "RTCSessionDescription+Internal.h"
42 #import "RTCSessionDescriptionDelegate.h"
43 #import "RTCSessionDescription.h"
44 #import "RTCStatsDelegate.h"
45 #import "RTCStatsReport+Internal.h"
47 #include "talk/app/webrtc/jsep.h"
49 NSString* const kRTCSessionDescriptionDelegateErrorDomain = @"RTCSDPError";
50 int const kRTCSessionDescriptionDelegateErrorCode = -1;
54 class RTCCreateSessionDescriptionObserver
55 : public CreateSessionDescriptionObserver {
57 RTCCreateSessionDescriptionObserver(
58 id<RTCSessionDescriptionDelegate> delegate,
59 RTCPeerConnection* peerConnection) {
61 _peerConnection = peerConnection;
64 virtual void OnSuccess(SessionDescriptionInterface* desc) OVERRIDE {
65 RTCSessionDescription* session =
66 [[RTCSessionDescription alloc] initWithSessionDescription:desc];
67 [_delegate peerConnection:_peerConnection
68 didCreateSessionDescription:session
72 virtual void OnFailure(const std::string& error) OVERRIDE {
73 NSString* str = @(error.c_str());
75 [NSError errorWithDomain:kRTCSessionDescriptionDelegateErrorDomain
76 code:kRTCSessionDescriptionDelegateErrorCode
77 userInfo:@{@"error" : str}];
78 [_delegate peerConnection:_peerConnection
79 didCreateSessionDescription:nil
84 id<RTCSessionDescriptionDelegate> _delegate;
85 RTCPeerConnection* _peerConnection;
88 class RTCSetSessionDescriptionObserver : public SetSessionDescriptionObserver {
90 RTCSetSessionDescriptionObserver(id<RTCSessionDescriptionDelegate> delegate,
91 RTCPeerConnection* peerConnection) {
93 _peerConnection = peerConnection;
96 virtual void OnSuccess() OVERRIDE {
97 [_delegate peerConnection:_peerConnection
98 didSetSessionDescriptionWithError:nil];
101 virtual void OnFailure(const std::string& error) OVERRIDE {
102 NSString* str = @(error.c_str());
104 [NSError errorWithDomain:kRTCSessionDescriptionDelegateErrorDomain
105 code:kRTCSessionDescriptionDelegateErrorCode
106 userInfo:@{@"error" : str}];
107 [_delegate peerConnection:_peerConnection
108 didSetSessionDescriptionWithError:err];
112 id<RTCSessionDescriptionDelegate> _delegate;
113 RTCPeerConnection* _peerConnection;
116 class RTCStatsObserver : public StatsObserver {
118 RTCStatsObserver(id<RTCStatsDelegate> delegate,
119 RTCPeerConnection* peerConnection) {
120 _delegate = delegate;
121 _peerConnection = peerConnection;
124 virtual void OnComplete(const std::vector<StatsReport>& reports) OVERRIDE {
125 NSMutableArray* stats = [NSMutableArray arrayWithCapacity:reports.size()];
126 std::vector<StatsReport>::const_iterator it = reports.begin();
127 for (; it != reports.end(); ++it) {
128 RTCStatsReport* statsReport =
129 [[RTCStatsReport alloc] initWithStatsReport:*it];
130 [stats addObject:statsReport];
132 [_delegate peerConnection:_peerConnection didGetStats:stats];
136 id<RTCStatsDelegate> _delegate;
137 RTCPeerConnection* _peerConnection;
141 @implementation RTCPeerConnection {
142 NSMutableArray* _localStreams;
143 talk_base::scoped_ptr<webrtc::RTCPeerConnectionObserver> _observer;
144 talk_base::scoped_refptr<webrtc::PeerConnectionInterface> _peerConnection;
147 - (BOOL)addICECandidate:(RTCICECandidate*)candidate {
148 talk_base::scoped_ptr<const webrtc::IceCandidateInterface> iceCandidate(
149 candidate.candidate);
150 return self.peerConnection->AddIceCandidate(iceCandidate.get());
153 - (BOOL)addStream:(RTCMediaStream*)stream
154 constraints:(RTCMediaConstraints*)constraints {
155 BOOL ret = self.peerConnection->AddStream(stream.mediaStream,
156 constraints.constraints);
160 [_localStreams addObject:stream];
164 - (RTCDataChannel*)createDataChannelWithLabel:(NSString*)label
165 config:(RTCDataChannelInit*)config {
166 std::string labelString([label UTF8String]);
167 talk_base::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 talk_base::scoped_refptr<webrtc::RTCCreateSessionDescriptionObserver>
176 observer(new talk_base::RefCountedObject<
177 webrtc::RTCCreateSessionDescriptionObserver>(delegate, self));
178 self.peerConnection->CreateAnswer(observer, constraints.constraints);
181 - (void)createOfferWithDelegate:(id<RTCSessionDescriptionDelegate>)delegate
182 constraints:(RTCMediaConstraints*)constraints {
183 talk_base::scoped_refptr<webrtc::RTCCreateSessionDescriptionObserver>
184 observer(new talk_base::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 talk_base::scoped_refptr<webrtc::RTCSetSessionDescriptionObserver> observer(
198 new talk_base::RefCountedObject<webrtc::RTCSetSessionDescriptionObserver>(
200 self.peerConnection->SetLocalDescription(observer, sdp.sessionDescription);
203 - (void)setRemoteDescriptionWithDelegate:
204 (id<RTCSessionDescriptionDelegate>)delegate
205 sessionDescription:(RTCSessionDescription*)sdp {
206 talk_base::scoped_refptr<webrtc::RTCSetSessionDescriptionObserver> observer(
207 new talk_base::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 talk_base::scoped_refptr<webrtc::RTCStatsObserver> observer(
264 new talk_base::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 - (id)initWithPeerConnection:
277 (talk_base::scoped_refptr<webrtc::PeerConnectionInterface>)
279 observer:(webrtc::RTCPeerConnectionObserver*)observer {
280 if (!peerConnection || !observer) {
281 NSAssert(NO, @"nil arguments not allowed");
285 if ((self = [super init])) {
286 _peerConnection = peerConnection;
287 _localStreams = [[NSMutableArray alloc] init];
288 _observer.reset(observer);
293 - (talk_base::scoped_refptr<webrtc::PeerConnectionInterface>)peerConnection {
294 return _peerConnection;