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

Unified Diff: webrtc/api/objc/RTCDataChannel.mm

Issue 1545393003: Update API for Objective-C RTCDataChannel. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Update against master Created 4 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: webrtc/api/objc/RTCDataChannel.mm
diff --git a/webrtc/api/objc/RTCDataChannel.mm b/webrtc/api/objc/RTCDataChannel.mm
new file mode 100644
index 0000000000000000000000000000000000000000..be6d6e769b1832d954723103346455600c065592
--- /dev/null
+++ b/webrtc/api/objc/RTCDataChannel.mm
@@ -0,0 +1,221 @@
+/*
+ * Copyright 2015 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#import "RTCDataChannel.h"
+
+#import "webrtc/api/objc/RTCDataChannel+Private.h"
+#import "webrtc/base/objc/NSString+StdString.h"
+
+namespace webrtc {
+
+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.
+ public:
+ RTCDataChannelObserver(RTCDataChannel *channel) { _channel = channel; }
+
+ void OnStateChange() override {
+ [_channel.delegate dataChannelDidChangeState:_channel];
+ }
+
+ void OnMessage(const DataBuffer& buffer) override {
+ RTCDataBuffer *dataBuffer =
tkchin_webrtc 2016/01/22 00:04:26 data_buffer
hjon 2016/01/22 17:57:48 Done.
+ [[RTCDataBuffer alloc] initWithNativeBuffer:buffer];
+ [_channel.delegate dataChannel:_channel
+ didReceiveMessageWithBuffer:dataBuffer];
+ }
+
+ void OnBufferedAmountChange(uint64_t previousAmount) override {
+ id<RTCDataChannelDelegate> delegate = _channel.delegate;
+ if ([delegate
+ respondsToSelector:@selector(channel:didChangeBufferedAmount:)]) {
+ [delegate dataChannel:_channel didChangeBufferedAmount:previousAmount];
+ }
+ }
+
+ private:
+ __weak RTCDataChannel *_channel;
tkchin_webrtc 2016/01/22 00:04:26 channel_
hjon 2016/01/22 17:57:47 Done.
+};
+}
+
+
+@implementation RTCDataBuffer {
+ rtc::scoped_ptr<webrtc::DataBuffer> _dataBuffer;
+}
+
+- (instancetype)initWithData:(NSData *)data isBinary:(BOOL)isBinary {
+ NSParameterAssert(data);
+ if (self = [super init]) {
+ 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.
+ [data length]);
+ _dataBuffer.reset(new webrtc::DataBuffer(buffer, isBinary));
+ }
+ return self;
+}
+
+- (NSData *)data {
+ return [NSData dataWithBytes:_dataBuffer->data.data()
+ length:_dataBuffer->data.size()];
+}
+
+- (BOOL)isBinary {
+ return _dataBuffer->binary;
+}
+
+#pragma mark - Private
+
+- (instancetype)initWithNativeBuffer:(const webrtc::DataBuffer&)nativeBuffer {
+ if (self = [super init]) {
+ _dataBuffer.reset(new webrtc::DataBuffer(nativeBuffer));
+ }
+ return self;
+}
+
+- (const webrtc::DataBuffer *)nativeDataBuffer {
+ return _dataBuffer.get();
+}
+
+@end
+
+
+@implementation RTCDataChannel {
+ rtc::scoped_refptr<webrtc::DataChannelInterface> _dataChannel;
+ rtc::scoped_ptr<webrtc::RTCDataChannelObserver> _observer;
+ BOOL _isObserverRegistered;
+}
+
+@synthesize delegate = _delegate;
+
+- (void)dealloc {
+ // Handles unregistering the observer properly. We need to do this because
+ // there may still be other references to the underlying data channel.
+ self.delegate = nil;
+}
+
+- (NSString *)label {
+ return [NSString stringForStdString:_dataChannel->label()];
+}
+
+- (BOOL)isOrdered {
+ return _dataChannel->ordered();
+}
+
+- (NSUInteger)maxPacketLifeTime {
tkchin_webrtc 2016/01/22 00:04:26 use same types as _dataChannel
hjon 2016/01/22 17:57:48 Done.
+ return _dataChannel->maxRetransmitTime();
+}
+
+- (NSUInteger)maxRetransmits {
+ return _dataChannel->maxRetransmits();
+}
+
+- (NSString *)protocol {
+ return [NSString stringForStdString:_dataChannel->protocol()];
+}
+
+- (BOOL)isNegotiated {
+ return _dataChannel->negotiated();
+}
+
+- (NSInteger)id {
+ return _dataChannel->id();
+}
+
+- (RTCDataChannelState)readyState {
+ return [[self class] dataChannelStateForNativeState:
+ _dataChannel->state()];
+}
+
+- (NSUInteger)bufferedAmount {
+ return _dataChannel->buffered_amount();
+}
+
+- (void)setDelegate:(id<RTCDataChannelDelegate>)delegate {
+ if (_delegate == delegate) {
+ return;
+ }
+ if (_isObserverRegistered) {
+ _dataChannel->UnregisterObserver();
+ _isObserverRegistered = NO;
+ }
+ _delegate = delegate;
+ if (_delegate) {
+ _dataChannel->RegisterObserver(_observer.get());
+ _isObserverRegistered = YES;
+ }
+}
+
+- (void)close {
+ _dataChannel->Close();
+}
+
+- (BOOL)sendData:(RTCDataBuffer *)data {
+ return _dataChannel->Send(*data.nativeDataBuffer);
+}
+
+- (NSString *)description {
+ return [NSString stringWithFormat:@"RTCDataChannel:\n%ld\n%@\n%@",
+ (long)self.id,
+ self.label,
+ [[self class]
+ stringForState:self.readyState]];
+}
+
+#pragma mark - Private
+
+- (instancetype)initWithNativeDataChannel:
+ (rtc::scoped_refptr<webrtc::DataChannelInterface>)nativeDataChannel {
+ NSParameterAssert(nativeDataChannel);
+ if (self = [super init]) {
+ _dataChannel = nativeDataChannel;
tkchin_webrtc 2016/01/22 00:04:26 _nativeDataChannel
hjon 2016/01/22 17:57:47 Done.
+ _observer.reset(new webrtc::RTCDataChannelObserver(self));
+ }
+ return self;
+}
+
++ (webrtc::DataChannelInterface::DataState)
+ nativeDataChannelStateForState:(RTCDataChannelState)state {
+ switch (state) {
+ case RTCDataChannelStateConnecting:
+ return webrtc::DataChannelInterface::DataState::kConnecting;
+ case RTCDataChannelStateOpen:
+ return webrtc::DataChannelInterface::DataState::kOpen;
+ case RTCDataChannelStateClosing:
+ return webrtc::DataChannelInterface::DataState::kClosing;
+ case RTCDataChannelStateClosed:
+ return webrtc::DataChannelInterface::DataState::kClosed;
+ }
+}
+
++ (RTCDataChannelState)dataChannelStateForNativeState:
+ (webrtc::DataChannelInterface::DataState)nativeState {
+ switch (nativeState) {
+ case webrtc::DataChannelInterface::DataState::kConnecting:
+ return RTCDataChannelStateConnecting;
+ case webrtc::DataChannelInterface::DataState::kOpen:
+ return RTCDataChannelStateOpen;
+ case webrtc::DataChannelInterface::DataState::kClosing:
+ return RTCDataChannelStateClosing;
+ case webrtc::DataChannelInterface::DataState::kClosed:
+ return RTCDataChannelStateClosed;
+ }
+}
+
++ (NSString *)stringForState:(RTCDataChannelState)state {
+ switch (state) {
+ case RTCDataChannelStateConnecting:
+ return @"Connecting";
+ case RTCDataChannelStateOpen:
+ return @"Open";
+ case RTCDataChannelStateClosing:
+ return @"Closing";
+ case RTCDataChannelStateClosed:
+ return @"Closed";
+ }
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698