2014-04-30 18:32:33 +00:00
|
|
|
/*
|
|
|
|
|
* libjingle
|
2015-01-20 21:36:13 +00:00
|
|
|
* Copyright 2014 Google Inc.
|
2014-04-30 18:32:33 +00:00
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
|
|
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#if !defined(__has_feature) || !__has_feature(objc_arc)
|
|
|
|
|
#error "This file requires ARC support."
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#import "RTCDataChannel+Internal.h"
|
|
|
|
|
|
2016-02-10 10:53:12 +01:00
|
|
|
#include "webrtc/api/datachannelinterface.h"
|
2016-02-11 13:36:43 -08:00
|
|
|
#include "webrtc/base/scoped_ptr.h"
|
2014-04-30 18:32:33 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
class RTCDataChannelObserver : public DataChannelObserver {
|
|
|
|
|
public:
|
|
|
|
|
RTCDataChannelObserver(RTCDataChannel* channel) { _channel = channel; }
|
|
|
|
|
|
2015-03-04 12:58:35 +00:00
|
|
|
void OnStateChange() override {
|
2014-04-30 18:32:33 +00:00
|
|
|
[_channel.delegate channelDidChangeState:_channel];
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
void OnBufferedAmountChange(uint64_t previousAmount) override {
|
2015-07-01 13:34:33 -07:00
|
|
|
RTCDataChannel* channel = _channel;
|
|
|
|
|
id<RTCDataChannelDelegate> delegate = channel.delegate;
|
|
|
|
|
if ([delegate
|
|
|
|
|
respondsToSelector:@selector(channel:didChangeBufferedAmount:)]) {
|
|
|
|
|
[delegate channel:channel didChangeBufferedAmount:previousAmount];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-04 12:58:35 +00:00
|
|
|
void OnMessage(const DataBuffer& buffer) override {
|
2014-04-30 18:32:33 +00:00
|
|
|
if (!_channel.delegate) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
RTCDataBuffer* dataBuffer =
|
|
|
|
|
[[RTCDataBuffer alloc] initWithDataBuffer:buffer];
|
|
|
|
|
[_channel.delegate channel:_channel didReceiveMessageWithBuffer:dataBuffer];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
__weak RTCDataChannel* _channel;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-16 10:04:20 +02:00
|
|
|
// TODO(henrika): move to shared location.
|
|
|
|
|
// See https://code.google.com/p/webrtc/issues/detail?id=4773 for details.
|
2014-04-30 18:32:33 +00:00
|
|
|
NSString* NSStringFromStdString(const std::string& stdString) {
|
|
|
|
|
// std::string may contain null termination character so we construct
|
|
|
|
|
// using length.
|
|
|
|
|
return [[NSString alloc] initWithBytes:stdString.data()
|
|
|
|
|
length:stdString.length()
|
|
|
|
|
encoding:NSUTF8StringEncoding];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string StdStringFromNSString(NSString* nsString) {
|
|
|
|
|
NSData* charData = [nsString dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
|
return std::string(reinterpret_cast<const char*>([charData bytes]),
|
|
|
|
|
[charData length]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@implementation RTCDataChannelInit {
|
|
|
|
|
webrtc::DataChannelInit _dataChannelInit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL)isOrdered {
|
|
|
|
|
return _dataChannelInit.ordered;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)setIsOrdered:(BOOL)isOrdered {
|
|
|
|
|
_dataChannelInit.ordered = isOrdered;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSInteger)maxRetransmitTime {
|
|
|
|
|
return _dataChannelInit.maxRetransmitTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)setMaxRetransmitTime:(NSInteger)maxRetransmitTime {
|
|
|
|
|
_dataChannelInit.maxRetransmitTime = maxRetransmitTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSInteger)maxRetransmits {
|
|
|
|
|
return _dataChannelInit.maxRetransmits;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)setMaxRetransmits:(NSInteger)maxRetransmits {
|
|
|
|
|
_dataChannelInit.maxRetransmits = maxRetransmits;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*)protocol {
|
|
|
|
|
return NSStringFromStdString(_dataChannelInit.protocol);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)setProtocol:(NSString*)protocol {
|
|
|
|
|
_dataChannelInit.protocol = StdStringFromNSString(protocol);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL)isNegotiated {
|
|
|
|
|
return _dataChannelInit.negotiated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)setIsNegotiated:(BOOL)isNegotiated {
|
|
|
|
|
_dataChannelInit.negotiated = isNegotiated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSInteger)streamId {
|
|
|
|
|
return _dataChannelInit.id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)setStreamId:(NSInteger)streamId {
|
|
|
|
|
_dataChannelInit.id = streamId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation RTCDataChannelInit (Internal)
|
|
|
|
|
|
|
|
|
|
- (const webrtc::DataChannelInit*)dataChannelInit {
|
|
|
|
|
return &_dataChannelInit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation RTCDataBuffer {
|
2014-07-29 17:36:52 +00:00
|
|
|
rtc::scoped_ptr<webrtc::DataBuffer> _dataBuffer;
|
2014-04-30 18:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithData:(NSData*)data isBinary:(BOOL)isBinary {
|
|
|
|
|
NSAssert(data, @"data cannot be nil");
|
|
|
|
|
if (self = [super init]) {
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
rtc::Buffer buffer(reinterpret_cast<const uint8_t*>([data bytes]),
|
|
|
|
|
[data length]);
|
2014-04-30 18:32:33 +00:00
|
|
|
_dataBuffer.reset(new webrtc::DataBuffer(buffer, isBinary));
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSData*)data {
|
|
|
|
|
return [NSData dataWithBytes:_dataBuffer->data.data()
|
2015-03-24 09:19:06 +00:00
|
|
|
length:_dataBuffer->data.size()];
|
2014-04-30 18:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL)isBinary {
|
|
|
|
|
return _dataBuffer->binary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation RTCDataBuffer (Internal)
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithDataBuffer:(const webrtc::DataBuffer&)buffer {
|
|
|
|
|
if (self = [super init]) {
|
|
|
|
|
_dataBuffer.reset(new webrtc::DataBuffer(buffer));
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const webrtc::DataBuffer*)dataBuffer {
|
|
|
|
|
return _dataBuffer.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation RTCDataChannel {
|
2014-07-29 17:36:52 +00:00
|
|
|
rtc::scoped_refptr<webrtc::DataChannelInterface> _dataChannel;
|
|
|
|
|
rtc::scoped_ptr<webrtc::RTCDataChannelObserver> _observer;
|
2014-04-30 18:32:33 +00:00
|
|
|
BOOL _isObserverRegistered;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-31 11:06:26 -07:00
|
|
|
- (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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-30 18:32:33 +00:00
|
|
|
- (NSString*)label {
|
|
|
|
|
return NSStringFromStdString(_dataChannel->label());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL)isReliable {
|
|
|
|
|
return _dataChannel->reliable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL)isOrdered {
|
|
|
|
|
return _dataChannel->ordered();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSUInteger)maxRetransmitTimeMs {
|
|
|
|
|
return _dataChannel->maxRetransmitTime();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSUInteger)maxRetransmits {
|
|
|
|
|
return _dataChannel->maxRetransmits();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*)protocol {
|
|
|
|
|
return NSStringFromStdString(_dataChannel->protocol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL)isNegotiated {
|
|
|
|
|
return _dataChannel->negotiated();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSInteger)streamId {
|
|
|
|
|
return _dataChannel->id();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (RTCDataChannelState)state {
|
|
|
|
|
switch (_dataChannel->state()) {
|
|
|
|
|
case webrtc::DataChannelInterface::DataState::kConnecting:
|
|
|
|
|
return kRTCDataChannelStateConnecting;
|
|
|
|
|
case webrtc::DataChannelInterface::DataState::kOpen:
|
|
|
|
|
return kRTCDataChannelStateOpen;
|
|
|
|
|
case webrtc::DataChannelInterface::DataState::kClosing:
|
|
|
|
|
return kRTCDataChannelStateClosing;
|
|
|
|
|
case webrtc::DataChannelInterface::DataState::kClosed:
|
|
|
|
|
return kRTCDataChannelStateClosed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (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.dataBuffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation RTCDataChannel (Internal)
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithDataChannel:
|
2014-07-29 17:36:52 +00:00
|
|
|
(rtc::scoped_refptr<webrtc::DataChannelInterface>)
|
2014-04-30 18:32:33 +00:00
|
|
|
dataChannel {
|
|
|
|
|
NSAssert(dataChannel != NULL, @"dataChannel cannot be NULL");
|
|
|
|
|
if (self = [super init]) {
|
|
|
|
|
_dataChannel = dataChannel;
|
|
|
|
|
_observer.reset(new webrtc::RTCDataChannelObserver(self));
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-29 17:36:52 +00:00
|
|
|
- (rtc::scoped_refptr<webrtc::DataChannelInterface>)dataChannel {
|
2014-04-30 18:32:33 +00:00
|
|
|
return _dataChannel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|