You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
6.0 KiB

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