blob: 4fb03c284ebbb7e2e1ca8427bfabfee882cfc54f [file] [log] [blame]
tkchin@webrtc.orgff273322014-04-30 18:32:33 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2014 Google Inc.
tkchin@webrtc.orgff273322014-04-30 18:32:33 +00004 *
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 "RTCDataChannel+Internal.h"
33
34#include "talk/app/webrtc/datachannelinterface.h"
35
36namespace webrtc {
37
38class RTCDataChannelObserver : public DataChannelObserver {
39 public:
40 RTCDataChannelObserver(RTCDataChannel* channel) { _channel = channel; }
41
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000042 void OnStateChange() override {
tkchin@webrtc.orgff273322014-04-30 18:32:33 +000043 [_channel.delegate channelDidChangeState:_channel];
44 }
45
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000046 void OnMessage(const DataBuffer& buffer) override {
tkchin@webrtc.orgff273322014-04-30 18:32:33 +000047 if (!_channel.delegate) {
48 return;
49 }
50 RTCDataBuffer* dataBuffer =
51 [[RTCDataBuffer alloc] initWithDataBuffer:buffer];
52 [_channel.delegate channel:_channel didReceiveMessageWithBuffer:dataBuffer];
53 }
54
55 private:
56 __weak RTCDataChannel* _channel;
57};
58}
59
henrika1d34fe92015-06-16 10:04:20 +020060// TODO(henrika): move to shared location.
61// See https://code.google.com/p/webrtc/issues/detail?id=4773 for details.
tkchin@webrtc.orgff273322014-04-30 18:32:33 +000062NSString* NSStringFromStdString(const std::string& stdString) {
63 // std::string may contain null termination character so we construct
64 // using length.
65 return [[NSString alloc] initWithBytes:stdString.data()
66 length:stdString.length()
67 encoding:NSUTF8StringEncoding];
68}
69
70std::string StdStringFromNSString(NSString* nsString) {
71 NSData* charData = [nsString dataUsingEncoding:NSUTF8StringEncoding];
72 return std::string(reinterpret_cast<const char*>([charData bytes]),
73 [charData length]);
74}
75
76@implementation RTCDataChannelInit {
77 webrtc::DataChannelInit _dataChannelInit;
78}
79
80- (BOOL)isOrdered {
81 return _dataChannelInit.ordered;
82}
83
84- (void)setIsOrdered:(BOOL)isOrdered {
85 _dataChannelInit.ordered = isOrdered;
86}
87
88- (NSInteger)maxRetransmitTime {
89 return _dataChannelInit.maxRetransmitTime;
90}
91
92- (void)setMaxRetransmitTime:(NSInteger)maxRetransmitTime {
93 _dataChannelInit.maxRetransmitTime = maxRetransmitTime;
94}
95
96- (NSInteger)maxRetransmits {
97 return _dataChannelInit.maxRetransmits;
98}
99
100- (void)setMaxRetransmits:(NSInteger)maxRetransmits {
101 _dataChannelInit.maxRetransmits = maxRetransmits;
102}
103
104- (NSString*)protocol {
105 return NSStringFromStdString(_dataChannelInit.protocol);
106}
107
108- (void)setProtocol:(NSString*)protocol {
109 _dataChannelInit.protocol = StdStringFromNSString(protocol);
110}
111
112- (BOOL)isNegotiated {
113 return _dataChannelInit.negotiated;
114}
115
116- (void)setIsNegotiated:(BOOL)isNegotiated {
117 _dataChannelInit.negotiated = isNegotiated;
118}
119
120- (NSInteger)streamId {
121 return _dataChannelInit.id;
122}
123
124- (void)setStreamId:(NSInteger)streamId {
125 _dataChannelInit.id = streamId;
126}
127
128@end
129
130@implementation RTCDataChannelInit (Internal)
131
132- (const webrtc::DataChannelInit*)dataChannelInit {
133 return &_dataChannelInit;
134}
135
136@end
137
138@implementation RTCDataBuffer {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000139 rtc::scoped_ptr<webrtc::DataBuffer> _dataBuffer;
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000140}
141
142- (instancetype)initWithData:(NSData*)data isBinary:(BOOL)isBinary {
143 NSAssert(data, @"data cannot be nil");
144 if (self = [super init]) {
Karl Wiberg94784372015-04-20 14:03:07 +0200145 rtc::Buffer buffer(reinterpret_cast<const uint8_t*>([data bytes]),
146 [data length]);
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000147 _dataBuffer.reset(new webrtc::DataBuffer(buffer, isBinary));
148 }
149 return self;
150}
151
152- (NSData*)data {
153 return [NSData dataWithBytes:_dataBuffer->data.data()
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000154 length:_dataBuffer->data.size()];
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000155}
156
157- (BOOL)isBinary {
158 return _dataBuffer->binary;
159}
160
161@end
162
163@implementation RTCDataBuffer (Internal)
164
165- (instancetype)initWithDataBuffer:(const webrtc::DataBuffer&)buffer {
166 if (self = [super init]) {
167 _dataBuffer.reset(new webrtc::DataBuffer(buffer));
168 }
169 return self;
170}
171
172- (const webrtc::DataBuffer*)dataBuffer {
173 return _dataBuffer.get();
174}
175
176@end
177
178@implementation RTCDataChannel {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000179 rtc::scoped_refptr<webrtc::DataChannelInterface> _dataChannel;
180 rtc::scoped_ptr<webrtc::RTCDataChannelObserver> _observer;
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000181 BOOL _isObserverRegistered;
182}
183
Zeke Chin48253562015-03-31 11:06:26 -0700184- (void)dealloc {
185 // Handles unregistering the observer properly. We need to do this because
186 // there may still be other references to the underlying data channel.
187 self.delegate = nil;
188}
189
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000190- (NSString*)label {
191 return NSStringFromStdString(_dataChannel->label());
192}
193
194- (BOOL)isReliable {
195 return _dataChannel->reliable();
196}
197
198- (BOOL)isOrdered {
199 return _dataChannel->ordered();
200}
201
202- (NSUInteger)maxRetransmitTimeMs {
203 return _dataChannel->maxRetransmitTime();
204}
205
206- (NSUInteger)maxRetransmits {
207 return _dataChannel->maxRetransmits();
208}
209
210- (NSString*)protocol {
211 return NSStringFromStdString(_dataChannel->protocol());
212}
213
214- (BOOL)isNegotiated {
215 return _dataChannel->negotiated();
216}
217
218- (NSInteger)streamId {
219 return _dataChannel->id();
220}
221
222- (RTCDataChannelState)state {
223 switch (_dataChannel->state()) {
224 case webrtc::DataChannelInterface::DataState::kConnecting:
225 return kRTCDataChannelStateConnecting;
226 case webrtc::DataChannelInterface::DataState::kOpen:
227 return kRTCDataChannelStateOpen;
228 case webrtc::DataChannelInterface::DataState::kClosing:
229 return kRTCDataChannelStateClosing;
230 case webrtc::DataChannelInterface::DataState::kClosed:
231 return kRTCDataChannelStateClosed;
232 }
233}
234
235- (NSUInteger)bufferedAmount {
236 return _dataChannel->buffered_amount();
237}
238
239- (void)setDelegate:(id<RTCDataChannelDelegate>)delegate {
240 if (_delegate == delegate) {
241 return;
242 }
243 if (_isObserverRegistered) {
244 _dataChannel->UnregisterObserver();
245 _isObserverRegistered = NO;
246 }
247 _delegate = delegate;
248 if (_delegate) {
249 _dataChannel->RegisterObserver(_observer.get());
250 _isObserverRegistered = YES;
251 }
252}
253
254- (void)close {
255 _dataChannel->Close();
256}
257
258- (BOOL)sendData:(RTCDataBuffer*)data {
259 return _dataChannel->Send(*data.dataBuffer);
260}
261
262@end
263
264@implementation RTCDataChannel (Internal)
265
266- (instancetype)initWithDataChannel:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000267 (rtc::scoped_refptr<webrtc::DataChannelInterface>)
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000268 dataChannel {
269 NSAssert(dataChannel != NULL, @"dataChannel cannot be NULL");
270 if (self = [super init]) {
271 _dataChannel = dataChannel;
272 _observer.reset(new webrtc::RTCDataChannelObserver(self));
273 }
274 return self;
275}
276
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000277- (rtc::scoped_refptr<webrtc::DataChannelInterface>)dataChannel {
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000278 return _dataChannel;
279}
280
281@end