-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathrtc_data_channel.dart
102 lines (82 loc) · 2.92 KB
/
rtc_data_channel.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import 'dart:async';
import 'dart:typed_data';
import 'enums.dart';
class RTCDataChannelInit {
bool ordered = true;
int maxRetransmitTime = -1;
int maxRetransmits = -1;
DataChannelProtocol protocol = DataChannelProtocol.sctp;
MessageType binaryType = MessageType.text;
bool negotiated = false;
int id = 0;
Map<String, dynamic> toMap() {
return {
'ordered': ordered,
if (maxRetransmitTime > 0)
//https://www.chromestatus.com/features/5198350873788416
'maxPacketLifeTime': maxRetransmitTime,
if (maxRetransmits > 0) 'maxRetransmits': maxRetransmits,
'protocol': typeDataChannelProtocolToString[protocol],
'negotiated': negotiated,
'id': id
};
}
}
/// A class that represents a datachannel message.
/// Can either contain binary data as a [Uint8List] or
/// text data as a [String].
class RTCDataChannelMessage {
/// Construct a text message with a [String].
RTCDataChannelMessage(String text) {
_data = text;
_isBinary = false;
}
/// Construct a binary message with a [Uint8List].
RTCDataChannelMessage.fromBinary(Uint8List binary) {
_data = binary;
_isBinary = true;
}
late dynamic _data;
late bool _isBinary;
/// Tells whether this message contains binary.
/// If this is false, it's a text message.
bool get isBinary => _isBinary;
MessageType get type => isBinary ? MessageType.binary : MessageType.text;
/// Text contents of this message as [String].
/// Use only on text messages.
/// See: [isBinary].
String get text => _data;
/// Binary contents of this message as [Uint8List].
/// Use only on binary messages.
/// See: [isBinary].
Uint8List get binary => _data;
}
abstract class RTCDataChannel {
RTCDataChannel();
Function(RTCDataChannelState state)? onDataChannelState;
Function(RTCDataChannelMessage data)? onMessage;
Function(int currentAmount, int changedAmount)? onBufferedAmountChange;
Function(int currentAmount)? onBufferedAmountLow;
/// Get current state.
RTCDataChannelState? get state;
/// Get channel id
int? get id;
/// Get channel label
String? get label;
int? get bufferedAmount;
/// Set threshold to trigger onBufferedAmountLow callback
int? bufferedAmountLowThreshold;
/// Stream of state change events. Emits the new state on change.
/// Closes when the [RTCDataChannel] is closed.
late Stream<RTCDataChannelState> stateChangeStream;
/// Stream of incoming messages. Emits the message.
/// Closes when the [RTCDataChannel] is closed.
late Stream<RTCDataChannelMessage> messageStream;
/// Send a message to this datachannel.
/// To send a text message, use the default constructor to instantiate a text [RTCDataChannelMessage]
/// for the [message] parameter.
/// To send a binary message, pass a binary [RTCDataChannelMessage]
/// constructed with [RTCDataChannelMessage.fromBinary]
Future<void> send(RTCDataChannelMessage message);
Future<void> close();
}