Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / app / webrtc / objc / RTCPeerConnection.mm
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 "RTCPeerConnection+Internal.h"
33
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"
46
47 #include "talk/app/webrtc/jsep.h"
48
49 NSString* const kRTCSessionDescriptionDelegateErrorDomain = @"RTCSDPError";
50 int const kRTCSessionDescriptionDelegateErrorCode = -1;
51
52 namespace webrtc {
53
54 class RTCCreateSessionDescriptionObserver
55     : public CreateSessionDescriptionObserver {
56  public:
57   RTCCreateSessionDescriptionObserver(
58       id<RTCSessionDescriptionDelegate> delegate,
59       RTCPeerConnection* peerConnection) {
60     _delegate = delegate;
61     _peerConnection = peerConnection;
62   }
63
64   virtual void OnSuccess(SessionDescriptionInterface* desc) OVERRIDE {
65     RTCSessionDescription* session =
66         [[RTCSessionDescription alloc] initWithSessionDescription:desc];
67     [_delegate peerConnection:_peerConnection
68         didCreateSessionDescription:session
69                               error:nil];
70   }
71
72   virtual void OnFailure(const std::string& error) OVERRIDE {
73     NSString* str = @(error.c_str());
74     NSError* err =
75         [NSError errorWithDomain:kRTCSessionDescriptionDelegateErrorDomain
76                             code:kRTCSessionDescriptionDelegateErrorCode
77                         userInfo:@{@"error" : str}];
78     [_delegate peerConnection:_peerConnection
79         didCreateSessionDescription:nil
80                               error:err];
81   }
82
83  private:
84   id<RTCSessionDescriptionDelegate> _delegate;
85   RTCPeerConnection* _peerConnection;
86 };
87
88 class RTCSetSessionDescriptionObserver : public SetSessionDescriptionObserver {
89  public:
90   RTCSetSessionDescriptionObserver(id<RTCSessionDescriptionDelegate> delegate,
91                                    RTCPeerConnection* peerConnection) {
92     _delegate = delegate;
93     _peerConnection = peerConnection;
94   }
95
96   virtual void OnSuccess() OVERRIDE {
97     [_delegate peerConnection:_peerConnection
98         didSetSessionDescriptionWithError:nil];
99   }
100
101   virtual void OnFailure(const std::string& error) OVERRIDE {
102     NSString* str = @(error.c_str());
103     NSError* err =
104         [NSError errorWithDomain:kRTCSessionDescriptionDelegateErrorDomain
105                             code:kRTCSessionDescriptionDelegateErrorCode
106                         userInfo:@{@"error" : str}];
107     [_delegate peerConnection:_peerConnection
108         didSetSessionDescriptionWithError:err];
109   }
110
111  private:
112   id<RTCSessionDescriptionDelegate> _delegate;
113   RTCPeerConnection* _peerConnection;
114 };
115
116 class RTCStatsObserver : public StatsObserver {
117  public:
118   RTCStatsObserver(id<RTCStatsDelegate> delegate,
119                    RTCPeerConnection* peerConnection) {
120     _delegate = delegate;
121     _peerConnection = peerConnection;
122   }
123
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];
131     }
132     [_delegate peerConnection:_peerConnection didGetStats:stats];
133   }
134
135  private:
136   id<RTCStatsDelegate> _delegate;
137   RTCPeerConnection* _peerConnection;
138 };
139 }
140
141 @implementation RTCPeerConnection {
142   NSMutableArray* _localStreams;
143   talk_base::scoped_ptr<webrtc::RTCPeerConnectionObserver> _observer;
144   talk_base::scoped_refptr<webrtc::PeerConnectionInterface> _peerConnection;
145 }
146
147 - (BOOL)addICECandidate:(RTCICECandidate*)candidate {
148   talk_base::scoped_ptr<const webrtc::IceCandidateInterface> iceCandidate(
149       candidate.candidate);
150   return self.peerConnection->AddIceCandidate(iceCandidate.get());
151 }
152
153 - (BOOL)addStream:(RTCMediaStream*)stream
154       constraints:(RTCMediaConstraints*)constraints {
155   BOOL ret = self.peerConnection->AddStream(stream.mediaStream,
156                                             constraints.constraints);
157   if (!ret) {
158     return NO;
159   }
160   [_localStreams addObject:stream];
161   return YES;
162 }
163
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];
171 }
172
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);
179 }
180
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);
187 }
188
189 - (void)removeStream:(RTCMediaStream*)stream {
190   self.peerConnection->RemoveStream(stream.mediaStream);
191   [_localStreams removeObject:stream];
192 }
193
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>(
199           delegate, self));
200   self.peerConnection->SetLocalDescription(observer, sdp.sessionDescription);
201 }
202
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>(
208           delegate, self));
209   self.peerConnection->SetRemoteDescription(observer, sdp.sessionDescription);
210 }
211
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);
217   }
218   return self.peerConnection->UpdateIce(iceServers, constraints.constraints);
219 }
220
221 - (RTCSessionDescription*)localDescription {
222   const webrtc::SessionDescriptionInterface* sdi =
223       self.peerConnection->local_description();
224   return sdi ? [[RTCSessionDescription alloc] initWithSessionDescription:sdi]
225              : nil;
226 }
227
228 - (NSArray*)localStreams {
229   return [_localStreams copy];
230 }
231
232 - (RTCSessionDescription*)remoteDescription {
233   const webrtc::SessionDescriptionInterface* sdi =
234       self.peerConnection->remote_description();
235   return sdi ? [[RTCSessionDescription alloc] initWithSessionDescription:sdi]
236              : nil;
237 }
238
239 - (RTCICEConnectionState)iceConnectionState {
240   return [RTCEnumConverter
241       convertIceConnectionStateToObjC:self.peerConnection
242                                           ->ice_connection_state()];
243 }
244
245 - (RTCICEGatheringState)iceGatheringState {
246   return [RTCEnumConverter
247       convertIceGatheringStateToObjC:self.peerConnection
248                                          ->ice_gathering_state()];
249 }
250
251 - (RTCSignalingState)signalingState {
252   return [RTCEnumConverter
253       convertSignalingStateToObjC:self.peerConnection->signaling_state()];
254 }
255
256 - (void)close {
257   self.peerConnection->Close();
258 }
259
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,
265                                                                 self));
266   webrtc::PeerConnectionInterface::StatsOutputLevel nativeOutputLevel =
267       [RTCEnumConverter convertStatsOutputLevelToNative:statsOutputLevel];
268   return self.peerConnection->GetStats(
269       observer, mediaStreamTrack.mediaTrack, nativeOutputLevel);
270 }
271
272 @end
273
274 @implementation RTCPeerConnection (Internal)
275
276 - (id)initWithPeerConnection:
277           (talk_base::scoped_refptr<webrtc::PeerConnectionInterface>)
278       peerConnection
279                     observer:(webrtc::RTCPeerConnectionObserver*)observer {
280   if (!peerConnection || !observer) {
281     NSAssert(NO, @"nil arguments not allowed");
282     self = nil;
283     return nil;
284   }
285   if ((self = [super init])) {
286     _peerConnection = peerConnection;
287     _localStreams = [[NSMutableArray alloc] init];
288     _observer.reset(observer);
289   }
290   return self;
291 }
292
293 - (talk_base::scoped_refptr<webrtc::PeerConnectionInterface>)peerConnection {
294   return _peerConnection;
295 }
296
297 @end