@@ -10,7 +10,6 @@ import 'rtc_data_channel.dart';
10
10
import 'rtc_ice_candidate.dart' ;
11
11
import 'rtc_session_description.dart' ;
12
12
import '../rtc_stats_report.dart' ;
13
- import '../utils.dart' ;
14
13
import '../enums.dart' ;
15
14
16
15
/*
@@ -30,12 +29,15 @@ typedef void RTCDataChannelCallback(RTCDataChannel channel);
30
29
* PeerConnection
31
30
*/
32
31
class RTCPeerConnection {
33
- String _peerConnectionId;
32
+ final String _peerConnectionId;
34
33
final HTML .RtcPeerConnection _jsPc;
34
+ final _localStreams = Map <String , MediaStream >();
35
+ final _remoteStreams = Map <String , MediaStream >();
36
+ final _configuration = Map <String , dynamic >();
37
+
35
38
RTCSignalingState _signalingState;
36
39
RTCIceGatheringState _iceGatheringState;
37
40
RTCIceConnectionState _iceConnectionState;
38
- MediaStream _mediaStream;
39
41
40
42
// public: delegate
41
43
SignalingStateCallback onSignalingState;
@@ -49,43 +51,35 @@ class RTCPeerConnection {
49
51
RTCDataChannelCallback onDataChannel;
50
52
dynamic onRenegotiationNeeded;
51
53
52
- final Map <String , dynamic > defaultSdpConstraints = {
53
- "mandatory" : {
54
- "OfferToReceiveAudio" : true ,
55
- "OfferToReceiveVideo" : true ,
56
- },
57
- "optional" : [],
58
- };
59
-
60
54
RTCSignalingState get signalingState => _signalingState;
61
55
62
56
RTCIceGatheringState get iceGatheringState => _iceGatheringState;
63
57
64
58
RTCIceConnectionState get iceConnectionState => _iceConnectionState;
65
59
66
- RTCPeerConnection (this ._jsPc) {
67
- _peerConnectionId = base64Encode (this .toString ().codeUnits);
68
-
60
+ RTCPeerConnection (this ._peerConnectionId, this ._jsPc) {
69
61
_jsPc.onAddStream.listen ((mediaStreamEvent) {
70
62
final jsStream = mediaStreamEvent.stream;
71
- this ._mediaStream = MediaStream (jsStream, _peerConnectionId);
72
- onAddStream? .call (_mediaStream);
63
+ final _remoteStream = _remoteStreams.putIfAbsent (
64
+ jsStream.id, () => MediaStream (jsStream, _peerConnectionId));
65
+
66
+ onAddStream? .call (_remoteStream);
73
67
74
68
jsStream.onAddTrack.listen ((mediaStreamTrackEvent) {
75
69
final jsTrack =
76
70
(mediaStreamTrackEvent as HTML .MediaStreamTrackEvent ).track;
77
- final MediaStreamTrack track = MediaStreamTrack (jsTrack);
78
- _mediaStream .addTrack (track, addToNative: false ).then ((_) {
79
- onAddTrack? .call (_mediaStream , track);
71
+ final track = MediaStreamTrack (jsTrack);
72
+ _remoteStream .addTrack (track, addToNative: false ).then ((_) {
73
+ onAddTrack? .call (_remoteStream , track);
80
74
});
81
75
});
82
76
83
77
jsStream.onRemoveTrack.listen ((mediaStreamTrackEvent) {
84
78
final jsTrack =
85
79
(mediaStreamTrackEvent as HTML .MediaStreamTrackEvent ).track;
86
- final MediaStreamTrack track = MediaStreamTrack (jsTrack);
87
- _mediaStream .removeTrack (track, removeFromNative: false ).then ((_) {
88
- onRemoveTrack? .call (_mediaStream , track);
80
+ final track = MediaStreamTrack (jsTrack);
81
+ _remoteStream .removeTrack (track, removeFromNative: false ).then ((_) {
82
+ onRemoveTrack? .call (_remoteStream , track);
89
83
});
90
84
});
91
85
});
@@ -114,7 +108,8 @@ class RTCPeerConnection {
114
108
});
115
109
116
110
_jsPc.onRemoveStream.listen ((mediaStreamEvent) {
117
- onRemoveStream? .call (_mediaStream);
111
+ final _remoteStream = _remoteStreams.remove (mediaStreamEvent.stream.id);
112
+ onRemoveStream? .call (_remoteStream);
118
113
});
119
114
120
115
_jsPc.onSignalingStateChange.listen ((_) {
@@ -136,10 +131,11 @@ class RTCPeerConnection {
136
131
return Future .value ();
137
132
}
138
133
139
- Map <String , dynamic > get getConfiguration =>
140
- throw "Not implemented" ; // TODO(rostopira)
134
+ Map <String , dynamic > get getConfiguration => _configuration;
141
135
142
136
Future <void > setConfiguration (Map <String , dynamic > configuration) {
137
+ this ._configuration.addAll (configuration);
138
+
143
139
_jsPc.setConfiguration (configuration);
144
140
return Future .value ();
145
141
}
@@ -157,11 +153,14 @@ class RTCPeerConnection {
157
153
}
158
154
159
155
Future <void > addStream (MediaStream stream) {
156
+ _localStreams.putIfAbsent (stream.jsStream.id,
157
+ () => MediaStream (stream.jsStream, _peerConnectionId));
160
158
_jsPc.addStream (stream.jsStream);
161
159
return Future .value ();
162
160
}
163
161
164
162
Future <void > removeStream (MediaStream stream) async {
163
+ _localStreams.remove (stream.jsStream.id);
165
164
_jsPc.removeStream (stream.jsStream);
166
165
return Future .value ();
167
166
}
@@ -199,12 +198,12 @@ class RTCPeerConnection {
199
198
200
199
List <MediaStream > getLocalStreams () => _jsPc
201
200
.getLocalStreams ()
202
- .map ((jsStream) => MediaStream ( jsStream, 'local' ) )
201
+ .map ((jsStream) => _localStreams[ jsStream.id] )
203
202
.toList ();
204
203
205
204
List <MediaStream > getRemoteStreams () => _jsPc
206
205
.getRemoteStreams ()
207
- .map ((jsStream) => MediaStream ( jsStream, _peerConnectionId) )
206
+ .map ((jsStream) => _remoteStreams[ jsStream.id] )
208
207
.toList ();
209
208
210
209
Future <RTCDataChannel > createDataChannel (
0 commit comments