Update To 11.40.268.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 "RTCPeerConnectionObserver.h"
42 #import "RTCSessionDescription+Internal.h"
43 #import "RTCSessionDescription.h"
44 #import "RTCSessionDescriptionDelegate.h"
45 #import "RTCStatsDelegate.h"
46 #import "RTCStatsReport+Internal.h"
47
48 #include "talk/app/webrtc/jsep.h"
49
50 NSString* const kRTCSessionDescriptionDelegateErrorDomain = @"RTCSDPError";
51 int const kRTCSessionDescriptionDelegateErrorCode = -1;
52
53 namespace webrtc {
54
55 class RTCCreateSessionDescriptionObserver
56     : public CreateSessionDescriptionObserver {
57  public:
58   RTCCreateSessionDescriptionObserver(
59       id<RTCSessionDescriptionDelegate> delegate,
60       RTCPeerConnection* peerConnection) {
61     _delegate = delegate;
62     _peerConnection = peerConnection;
63   }
64
65   virtual void OnSuccess(SessionDescriptionInterface* desc) OVERRIDE {
66     RTCSessionDescription* session =
67         [[RTCSessionDescription alloc] initWithSessionDescription:desc];
68     [_delegate peerConnection:_peerConnection
69         didCreateSessionDescription:session
70                               error:nil];
71     delete desc;
72   }
73
74   virtual void OnFailure(const std::string& error) OVERRIDE {
75     NSString* str = @(error.c_str());
76     NSError* err =
77         [NSError errorWithDomain:kRTCSessionDescriptionDelegateErrorDomain
78                             code:kRTCSessionDescriptionDelegateErrorCode
79                         userInfo:@{@"error" : str}];
80     [_delegate peerConnection:_peerConnection
81         didCreateSessionDescription:nil
82                               error:err];
83   }
84
85  private:
86   id<RTCSessionDescriptionDelegate> _delegate;
87   RTCPeerConnection* _peerConnection;
88 };
89
90 class RTCSetSessionDescriptionObserver : public SetSessionDescriptionObserver {
91  public:
92   RTCSetSessionDescriptionObserver(id<RTCSessionDescriptionDelegate> delegate,
93                                    RTCPeerConnection* peerConnection) {
94     _delegate = delegate;
95     _peerConnection = peerConnection;
96   }
97
98   virtual void OnSuccess() OVERRIDE {
99     [_delegate peerConnection:_peerConnection
100         didSetSessionDescriptionWithError:nil];
101   }
102
103   virtual void OnFailure(const std::string& error) OVERRIDE {
104     NSString* str = @(error.c_str());
105     NSError* err =
106         [NSError errorWithDomain:kRTCSessionDescriptionDelegateErrorDomain
107                             code:kRTCSessionDescriptionDelegateErrorCode
108                         userInfo:@{@"error" : str}];
109     [_delegate peerConnection:_peerConnection
110         didSetSessionDescriptionWithError:err];
111   }
112
113  private:
114   id<RTCSessionDescriptionDelegate> _delegate;
115   RTCPeerConnection* _peerConnection;
116 };
117
118 class RTCStatsObserver : public StatsObserver {
119  public:
120   RTCStatsObserver(id<RTCStatsDelegate> delegate,
121                    RTCPeerConnection* peerConnection) {
122     _delegate = delegate;
123     _peerConnection = peerConnection;
124   }
125
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];
133     }
134     [_delegate peerConnection:_peerConnection didGetStats:stats];
135   }
136
137  private:
138   id<RTCStatsDelegate> _delegate;
139   RTCPeerConnection* _peerConnection;
140 };
141 }
142
143 @implementation RTCPeerConnection {
144   NSMutableArray* _localStreams;
145   rtc::scoped_ptr<webrtc::RTCPeerConnectionObserver> _observer;
146   rtc::scoped_refptr<webrtc::PeerConnectionInterface> _peerConnection;
147 }
148
149 - (BOOL)addICECandidate:(RTCICECandidate*)candidate {
150   rtc::scoped_ptr<const webrtc::IceCandidateInterface> iceCandidate(
151       candidate.candidate);
152   return self.peerConnection->AddIceCandidate(iceCandidate.get());
153 }
154
155 - (BOOL)addStream:(RTCMediaStream*)stream {
156   BOOL ret = self.peerConnection->AddStream(stream.mediaStream);
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   rtc::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   rtc::scoped_refptr<webrtc::RTCCreateSessionDescriptionObserver>
176       observer(new rtc::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   rtc::scoped_refptr<webrtc::RTCCreateSessionDescriptionObserver>
184       observer(new rtc::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   rtc::scoped_refptr<webrtc::RTCSetSessionDescriptionObserver> observer(
198       new rtc::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   rtc::scoped_refptr<webrtc::RTCSetSessionDescriptionObserver> observer(
207       new rtc::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   rtc::scoped_refptr<webrtc::RTCStatsObserver> observer(
264       new rtc::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 - (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];
285   }
286   return self;
287 }
288
289 - (rtc::scoped_refptr<webrtc::PeerConnectionInterface>)peerConnection {
290   return _peerConnection;
291 }
292
293 @end