OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #import "RTCDataChannel.h" | |
12 | |
13 #import "webrtc/api/objc/RTCDataChannel+Private.h" | |
14 #import "webrtc/base/objc/NSString+StdString.h" | |
15 | |
16 namespace webrtc { | |
17 | |
18 class RTCDataChannelObserver : public DataChannelObserver { | |
tkchin_webrtc
2016/01/22 00:04:26
DataChannelDelegateAdapter? No need for RTC prefix
hjon
2016/01/22 17:57:47
Done.
| |
19 public: | |
20 RTCDataChannelObserver(RTCDataChannel *channel) { _channel = channel; } | |
21 | |
22 void OnStateChange() override { | |
23 [_channel.delegate dataChannelDidChangeState:_channel]; | |
24 } | |
25 | |
26 void OnMessage(const DataBuffer& buffer) override { | |
27 RTCDataBuffer *dataBuffer = | |
tkchin_webrtc
2016/01/22 00:04:26
data_buffer
hjon
2016/01/22 17:57:48
Done.
| |
28 [[RTCDataBuffer alloc] initWithNativeBuffer:buffer]; | |
29 [_channel.delegate dataChannel:_channel | |
30 didReceiveMessageWithBuffer:dataBuffer]; | |
31 } | |
32 | |
33 void OnBufferedAmountChange(uint64_t previousAmount) override { | |
34 id<RTCDataChannelDelegate> delegate = _channel.delegate; | |
35 if ([delegate | |
36 respondsToSelector:@selector(channel:didChangeBufferedAmount:)]) { | |
37 [delegate dataChannel:_channel didChangeBufferedAmount:previousAmount]; | |
38 } | |
39 } | |
40 | |
41 private: | |
42 __weak RTCDataChannel *_channel; | |
tkchin_webrtc
2016/01/22 00:04:26
channel_
hjon
2016/01/22 17:57:47
Done.
| |
43 }; | |
44 } | |
45 | |
46 | |
47 @implementation RTCDataBuffer { | |
48 rtc::scoped_ptr<webrtc::DataBuffer> _dataBuffer; | |
49 } | |
50 | |
51 - (instancetype)initWithData:(NSData *)data isBinary:(BOOL)isBinary { | |
52 NSParameterAssert(data); | |
53 if (self = [super init]) { | |
54 rtc::Buffer buffer(reinterpret_cast<const uint8_t*>([data bytes]), | |
tkchin_webrtc
2016/01/22 00:04:26
dot syntax for properties
hjon
2016/01/22 17:57:47
Done.
| |
55 [data length]); | |
56 _dataBuffer.reset(new webrtc::DataBuffer(buffer, isBinary)); | |
57 } | |
58 return self; | |
59 } | |
60 | |
61 - (NSData *)data { | |
62 return [NSData dataWithBytes:_dataBuffer->data.data() | |
63 length:_dataBuffer->data.size()]; | |
64 } | |
65 | |
66 - (BOOL)isBinary { | |
67 return _dataBuffer->binary; | |
68 } | |
69 | |
70 #pragma mark - Private | |
71 | |
72 - (instancetype)initWithNativeBuffer:(const webrtc::DataBuffer&)nativeBuffer { | |
73 if (self = [super init]) { | |
74 _dataBuffer.reset(new webrtc::DataBuffer(nativeBuffer)); | |
75 } | |
76 return self; | |
77 } | |
78 | |
79 - (const webrtc::DataBuffer *)nativeDataBuffer { | |
80 return _dataBuffer.get(); | |
81 } | |
82 | |
83 @end | |
84 | |
85 | |
86 @implementation RTCDataChannel { | |
87 rtc::scoped_refptr<webrtc::DataChannelInterface> _dataChannel; | |
88 rtc::scoped_ptr<webrtc::RTCDataChannelObserver> _observer; | |
89 BOOL _isObserverRegistered; | |
90 } | |
91 | |
92 @synthesize delegate = _delegate; | |
93 | |
94 - (void)dealloc { | |
95 // Handles unregistering the observer properly. We need to do this because | |
96 // there may still be other references to the underlying data channel. | |
97 self.delegate = nil; | |
98 } | |
99 | |
100 - (NSString *)label { | |
101 return [NSString stringForStdString:_dataChannel->label()]; | |
102 } | |
103 | |
104 - (BOOL)isOrdered { | |
105 return _dataChannel->ordered(); | |
106 } | |
107 | |
108 - (NSUInteger)maxPacketLifeTime { | |
tkchin_webrtc
2016/01/22 00:04:26
use same types as _dataChannel
hjon
2016/01/22 17:57:48
Done.
| |
109 return _dataChannel->maxRetransmitTime(); | |
110 } | |
111 | |
112 - (NSUInteger)maxRetransmits { | |
113 return _dataChannel->maxRetransmits(); | |
114 } | |
115 | |
116 - (NSString *)protocol { | |
117 return [NSString stringForStdString:_dataChannel->protocol()]; | |
118 } | |
119 | |
120 - (BOOL)isNegotiated { | |
121 return _dataChannel->negotiated(); | |
122 } | |
123 | |
124 - (NSInteger)id { | |
125 return _dataChannel->id(); | |
126 } | |
127 | |
128 - (RTCDataChannelState)readyState { | |
129 return [[self class] dataChannelStateForNativeState: | |
130 _dataChannel->state()]; | |
131 } | |
132 | |
133 - (NSUInteger)bufferedAmount { | |
134 return _dataChannel->buffered_amount(); | |
135 } | |
136 | |
137 - (void)setDelegate:(id<RTCDataChannelDelegate>)delegate { | |
138 if (_delegate == delegate) { | |
139 return; | |
140 } | |
141 if (_isObserverRegistered) { | |
142 _dataChannel->UnregisterObserver(); | |
143 _isObserverRegistered = NO; | |
144 } | |
145 _delegate = delegate; | |
146 if (_delegate) { | |
147 _dataChannel->RegisterObserver(_observer.get()); | |
148 _isObserverRegistered = YES; | |
149 } | |
150 } | |
151 | |
152 - (void)close { | |
153 _dataChannel->Close(); | |
154 } | |
155 | |
156 - (BOOL)sendData:(RTCDataBuffer *)data { | |
157 return _dataChannel->Send(*data.nativeDataBuffer); | |
158 } | |
159 | |
160 - (NSString *)description { | |
161 return [NSString stringWithFormat:@"RTCDataChannel:\n%ld\n%@\n%@", | |
162 (long)self.id, | |
163 self.label, | |
164 [[self class] | |
165 stringForState:self.readyState]]; | |
166 } | |
167 | |
168 #pragma mark - Private | |
169 | |
170 - (instancetype)initWithNativeDataChannel: | |
171 (rtc::scoped_refptr<webrtc::DataChannelInterface>)nativeDataChannel { | |
172 NSParameterAssert(nativeDataChannel); | |
173 if (self = [super init]) { | |
174 _dataChannel = nativeDataChannel; | |
tkchin_webrtc
2016/01/22 00:04:26
_nativeDataChannel
hjon
2016/01/22 17:57:47
Done.
| |
175 _observer.reset(new webrtc::RTCDataChannelObserver(self)); | |
176 } | |
177 return self; | |
178 } | |
179 | |
180 + (webrtc::DataChannelInterface::DataState) | |
181 nativeDataChannelStateForState:(RTCDataChannelState)state { | |
182 switch (state) { | |
183 case RTCDataChannelStateConnecting: | |
184 return webrtc::DataChannelInterface::DataState::kConnecting; | |
185 case RTCDataChannelStateOpen: | |
186 return webrtc::DataChannelInterface::DataState::kOpen; | |
187 case RTCDataChannelStateClosing: | |
188 return webrtc::DataChannelInterface::DataState::kClosing; | |
189 case RTCDataChannelStateClosed: | |
190 return webrtc::DataChannelInterface::DataState::kClosed; | |
191 } | |
192 } | |
193 | |
194 + (RTCDataChannelState)dataChannelStateForNativeState: | |
195 (webrtc::DataChannelInterface::DataState)nativeState { | |
196 switch (nativeState) { | |
197 case webrtc::DataChannelInterface::DataState::kConnecting: | |
198 return RTCDataChannelStateConnecting; | |
199 case webrtc::DataChannelInterface::DataState::kOpen: | |
200 return RTCDataChannelStateOpen; | |
201 case webrtc::DataChannelInterface::DataState::kClosing: | |
202 return RTCDataChannelStateClosing; | |
203 case webrtc::DataChannelInterface::DataState::kClosed: | |
204 return RTCDataChannelStateClosed; | |
205 } | |
206 } | |
207 | |
208 + (NSString *)stringForState:(RTCDataChannelState)state { | |
209 switch (state) { | |
210 case RTCDataChannelStateConnecting: | |
211 return @"Connecting"; | |
212 case RTCDataChannelStateOpen: | |
213 return @"Open"; | |
214 case RTCDataChannelStateClosing: | |
215 return @"Closing"; | |
216 case RTCDataChannelStateClosed: | |
217 return @"Closed"; | |
218 } | |
219 } | |
220 | |
221 @end | |
OLD | NEW |