Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: webrtc/sdk/objc/Framework/Classes/RTCDataChannel.mm

Issue 2862543002: Split iOS sdk in to separate targets (Closed)
Patch Set: rebase and minor fixes Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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+Private.h"
12
13 #import "NSString+StdString.h"
14
15 #include <memory>
16
17 namespace webrtc {
18
19 class DataChannelDelegateAdapter : public DataChannelObserver {
20 public:
21 DataChannelDelegateAdapter(RTCDataChannel *channel) { channel_ = channel; }
22
23 void OnStateChange() override {
24 [channel_.delegate dataChannelDidChangeState:channel_];
25 }
26
27 void OnMessage(const DataBuffer& buffer) override {
28 RTCDataBuffer *data_buffer =
29 [[RTCDataBuffer alloc] initWithNativeBuffer:buffer];
30 [channel_.delegate dataChannel:channel_
31 didReceiveMessageWithBuffer:data_buffer];
32 }
33
34 void OnBufferedAmountChange(uint64_t previousAmount) override {
35 id<RTCDataChannelDelegate> delegate = channel_.delegate;
36 SEL sel = @selector(dataChannel:didChangeBufferedAmount:);
37 if ([delegate respondsToSelector:sel]) {
38 [delegate dataChannel:channel_ didChangeBufferedAmount:previousAmount];
39 }
40 }
41
42 private:
43 __weak RTCDataChannel *channel_;
44 };
45 }
46
47
48 @implementation RTCDataBuffer {
49 std::unique_ptr<webrtc::DataBuffer> _dataBuffer;
50 }
51
52 - (instancetype)initWithData:(NSData *)data isBinary:(BOOL)isBinary {
53 NSParameterAssert(data);
54 if (self = [super init]) {
55 rtc::CopyOnWriteBuffer buffer(
56 reinterpret_cast<const uint8_t*>(data.bytes), data.length);
57 _dataBuffer.reset(new webrtc::DataBuffer(buffer, isBinary));
58 }
59 return self;
60 }
61
62 - (NSData *)data {
63 return [NSData dataWithBytes:_dataBuffer->data.data()
64 length:_dataBuffer->data.size()];
65 }
66
67 - (BOOL)isBinary {
68 return _dataBuffer->binary;
69 }
70
71 #pragma mark - Private
72
73 - (instancetype)initWithNativeBuffer:(const webrtc::DataBuffer&)nativeBuffer {
74 if (self = [super init]) {
75 _dataBuffer.reset(new webrtc::DataBuffer(nativeBuffer));
76 }
77 return self;
78 }
79
80 - (const webrtc::DataBuffer *)nativeDataBuffer {
81 return _dataBuffer.get();
82 }
83
84 @end
85
86
87 @implementation RTCDataChannel {
88 rtc::scoped_refptr<webrtc::DataChannelInterface> _nativeDataChannel;
89 std::unique_ptr<webrtc::DataChannelDelegateAdapter> _observer;
90 BOOL _isObserverRegistered;
91 }
92
93 @synthesize delegate = _delegate;
94
95 - (void)dealloc {
96 // Handles unregistering the observer properly. We need to do this because
97 // there may still be other references to the underlying data channel.
98 _nativeDataChannel->UnregisterObserver();
99 }
100
101 - (NSString *)label {
102 return [NSString stringForStdString:_nativeDataChannel->label()];
103 }
104
105 - (BOOL)isReliable {
106 return _nativeDataChannel->reliable();
107 }
108
109 - (BOOL)isOrdered {
110 return _nativeDataChannel->ordered();
111 }
112
113 - (NSUInteger)maxRetransmitTime {
114 return self.maxPacketLifeTime;
115 }
116
117 - (uint16_t)maxPacketLifeTime {
118 return _nativeDataChannel->maxRetransmitTime();
119 }
120
121 - (uint16_t)maxRetransmits {
122 return _nativeDataChannel->maxRetransmits();
123 }
124
125 - (NSString *)protocol {
126 return [NSString stringForStdString:_nativeDataChannel->protocol()];
127 }
128
129 - (BOOL)isNegotiated {
130 return _nativeDataChannel->negotiated();
131 }
132
133 - (NSInteger)streamId {
134 return self.channelId;
135 }
136
137 - (int)channelId {
138 return _nativeDataChannel->id();
139 }
140
141 - (RTCDataChannelState)readyState {
142 return [[self class] dataChannelStateForNativeState:
143 _nativeDataChannel->state()];
144 }
145
146 - (uint64_t)bufferedAmount {
147 return _nativeDataChannel->buffered_amount();
148 }
149
150 - (void)close {
151 _nativeDataChannel->Close();
152 }
153
154 - (BOOL)sendData:(RTCDataBuffer *)data {
155 return _nativeDataChannel->Send(*data.nativeDataBuffer);
156 }
157
158 - (NSString *)description {
159 return [NSString stringWithFormat:@"RTCDataChannel:\n%ld\n%@\n%@",
160 (long)self.channelId,
161 self.label,
162 [[self class]
163 stringForState:self.readyState]];
164 }
165
166 #pragma mark - Private
167
168 - (instancetype)initWithNativeDataChannel:
169 (rtc::scoped_refptr<webrtc::DataChannelInterface>)nativeDataChannel {
170 NSParameterAssert(nativeDataChannel);
171 if (self = [super init]) {
172 _nativeDataChannel = nativeDataChannel;
173 _observer.reset(new webrtc::DataChannelDelegateAdapter(self));
174 _nativeDataChannel->RegisterObserver(_observer.get());
175 }
176 return self;
177 }
178
179 + (webrtc::DataChannelInterface::DataState)
180 nativeDataChannelStateForState:(RTCDataChannelState)state {
181 switch (state) {
182 case RTCDataChannelStateConnecting:
183 return webrtc::DataChannelInterface::DataState::kConnecting;
184 case RTCDataChannelStateOpen:
185 return webrtc::DataChannelInterface::DataState::kOpen;
186 case RTCDataChannelStateClosing:
187 return webrtc::DataChannelInterface::DataState::kClosing;
188 case RTCDataChannelStateClosed:
189 return webrtc::DataChannelInterface::DataState::kClosed;
190 }
191 }
192
193 + (RTCDataChannelState)dataChannelStateForNativeState:
194 (webrtc::DataChannelInterface::DataState)nativeState {
195 switch (nativeState) {
196 case webrtc::DataChannelInterface::DataState::kConnecting:
197 return RTCDataChannelStateConnecting;
198 case webrtc::DataChannelInterface::DataState::kOpen:
199 return RTCDataChannelStateOpen;
200 case webrtc::DataChannelInterface::DataState::kClosing:
201 return RTCDataChannelStateClosing;
202 case webrtc::DataChannelInterface::DataState::kClosed:
203 return RTCDataChannelStateClosed;
204 }
205 }
206
207 + (NSString *)stringForState:(RTCDataChannelState)state {
208 switch (state) {
209 case RTCDataChannelStateConnecting:
210 return @"Connecting";
211 case RTCDataChannelStateOpen:
212 return @"Open";
213 case RTCDataChannelStateClosing:
214 return @"Closing";
215 case RTCDataChannelStateClosed:
216 return @"Closed";
217 }
218 }
219
220 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698