1
1
import 'dart:async' ;
2
- import 'dart:html' as html ;
2
+ import 'dart:js_interop' ;
3
3
import 'dart:js_util' as jsutil;
4
- import 'dart:ui_web' as ui;
5
4
6
5
import 'package:flutter/foundation.dart' ;
7
6
import 'package:flutter/services.dart' ;
8
7
9
8
import 'package:dart_webrtc/dart_webrtc.dart' ;
9
+ import 'package:web/web.dart' as web;
10
10
11
11
// An error code value to error name Map.
12
12
// See: https://developer.mozilla.org/en-US/docs/Web/API/MediaError/code
@@ -39,13 +39,13 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue>
39
39
40
40
static const _elementIdForAudioManager = 'html_webrtc_audio_manager_list' ;
41
41
42
- html .AudioElement ? _audioElement;
42
+ web .AudioElement ? _audioElement;
43
43
44
44
static int _textureCounter = 1 ;
45
45
46
- html .MediaStream ? _videoStream;
46
+ web .MediaStream ? _videoStream;
47
47
48
- html .MediaStream ? _audioStream;
48
+ web .MediaStream ? _audioStream;
49
49
50
50
MediaStreamWeb ? _srcObject;
51
51
@@ -115,14 +115,14 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue>
115
115
116
116
if (null != _srcObject) {
117
117
if (stream.getVideoTracks ().isNotEmpty) {
118
- _videoStream = html .MediaStream ();
119
- for (final track in _srcObject! .jsStream.getVideoTracks ()) {
118
+ _videoStream = web .MediaStream ();
119
+ for (final track in _srcObject! .jsStream.getVideoTracks ().toDart ) {
120
120
_videoStream! .addTrack (track);
121
121
}
122
122
}
123
123
if (stream.getAudioTracks ().isNotEmpty) {
124
- _audioStream = html .MediaStream ();
125
- for (final track in _srcObject! .jsStream.getAudioTracks ()) {
124
+ _audioStream = web .MediaStream ();
125
+ for (final track in _srcObject! .jsStream.getAudioTracks ().toDart ) {
126
126
_audioStream! .addTrack (track);
127
127
}
128
128
}
@@ -133,7 +133,7 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue>
133
133
134
134
if (null != _audioStream) {
135
135
if (null == _audioElement) {
136
- _audioElement = html .AudioElement ()
136
+ _audioElement = web .AudioElement ()
137
137
..id = _elementIdForAudio
138
138
..muted = stream.ownerTag == 'local'
139
139
..autoplay = true ;
@@ -163,16 +163,16 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue>
163
163
164
164
if (null != _srcObject) {
165
165
if (stream.getVideoTracks ().isNotEmpty) {
166
- _videoStream = html .MediaStream ();
167
- for (final track in _srcObject! .jsStream.getVideoTracks ()) {
166
+ _videoStream = web .MediaStream ();
167
+ for (final track in _srcObject! .jsStream.getVideoTracks ().toDart ) {
168
168
if (track.id == trackId) {
169
169
_videoStream! .addTrack (track);
170
170
}
171
171
}
172
172
}
173
173
if (stream.getAudioTracks ().isNotEmpty) {
174
- _audioStream = html .MediaStream ();
175
- for (final track in _srcObject! .jsStream.getAudioTracks ()) {
174
+ _audioStream = web .MediaStream ();
175
+ for (final track in _srcObject! .jsStream.getAudioTracks ().toDart ) {
176
176
_audioStream! .addTrack (track);
177
177
}
178
178
}
@@ -183,7 +183,7 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue>
183
183
184
184
if (null != _audioStream) {
185
185
if (null == _audioElement) {
186
- _audioElement = html .AudioElement ()
186
+ _audioElement = web .AudioElement ()
187
187
..id = _elementIdForAudio
188
188
..muted = stream.ownerTag == 'local'
189
189
..autoplay = true ;
@@ -201,21 +201,19 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue>
201
201
value = value.copyWith (renderVideo: renderVideo);
202
202
}
203
203
204
- html. DivElement _ensureAudioManagerDiv () {
205
- var div = html .document.getElementById (_elementIdForAudioManager);
206
- if (null != div) return div as html. DivElement ;
204
+ web. HTMLDivElement _ensureAudioManagerDiv () {
205
+ var div = web .document.getElementById (_elementIdForAudioManager);
206
+ if (null != div) return div as web. HTMLDivElement ;
207
207
208
- div = html. DivElement ()
208
+ div = web. HTMLDivElement ()
209
209
..id = _elementIdForAudioManager
210
210
..style.display = 'none' ;
211
- html .document.body? .append (div);
212
- return div as html. DivElement ;
211
+ web .document.body? .append (div);
212
+ return div as web. HTMLDivElement ;
213
213
}
214
214
215
- html.VideoElement ? findHtmlView () {
216
- final element = html.document.getElementById (_elementIdForVideo);
217
- if (null != element) return element as html.VideoElement ;
218
- return null ;
215
+ web.VideoElement ? findHtmlView () {
216
+ return videoElement;
219
217
}
220
218
221
219
@override
@@ -228,8 +226,8 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue>
228
226
element? .removeAttribute ('src' );
229
227
element? .load ();
230
228
_audioElement? .remove ();
231
- final audioManager = html .document.getElementById (_elementIdForAudioManager)
232
- as html. DivElement ? ;
229
+ final audioManager = web .document.getElementById (_elementIdForAudioManager)
230
+ as web. HTMLDivElement ? ;
233
231
if (audioManager != null && ! audioManager.hasChildNodes ()) {
234
232
audioManager.remove ();
235
233
}
@@ -252,66 +250,65 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue>
252
250
return false ;
253
251
}
254
252
253
+ web.VideoElement ? videoElement;
254
+
255
255
@override
256
256
Future <void > initialize () async {
257
257
// ignore: undefined_prefixed_name
258
- ui.platformViewRegistry.registerViewFactory (viewType, (int viewId) {
259
- for (var s in _subscriptions) {
260
- s.cancel ();
261
- }
262
- _subscriptions.clear ();
263
-
264
- final element = html.VideoElement ()
265
- ..autoplay = true
266
- ..muted = true
267
- ..controls = false
268
- ..srcObject = _videoStream
269
- ..id = _elementIdForVideo
270
- ..setAttribute ('playsinline' , 'true' );
271
-
272
- _applyDefaultVideoStyles (element);
273
-
274
- _subscriptions.add (
275
- element.onCanPlay.listen ((dynamic _) {
276
- _updateAllValues ();
277
- }),
278
- );
279
-
280
- _subscriptions.add (
281
- element.onResize.listen ((dynamic _) {
282
- _updateAllValues ();
283
- onResize? .call ();
284
- }),
285
- );
286
-
287
- // The error event fires when some form of error occurs while attempting to load or perform the media.
288
- _subscriptions.add (
289
- element.onError.listen ((html.Event _) {
290
- // The Event itself (_) doesn't contain info about the actual error.
291
- // We need to look at the HTMLMediaElement.error.
292
- // See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/error
293
- final error = element.error;
294
- print ('RTCVideoRenderer: videoElement.onError, ${error .toString ()}' );
295
- throw PlatformException (
296
- code: _kErrorValueToErrorName[error! .code]! ,
297
- message:
298
- error.message != '' ? error.message : _kDefaultErrorMessage,
299
- details: _kErrorValueToErrorDescription[error.code],
300
- );
301
- }),
302
- );
303
-
304
- _subscriptions.add (
305
- element.onEnded.listen ((dynamic _) {
306
- // print('RTCVideoRenderer: videoElement.onEnded');
307
- }),
308
- );
309
-
310
- return element;
311
- });
258
+ for (var s in _subscriptions) {
259
+ s.cancel ();
260
+ }
261
+ _subscriptions.clear ();
262
+
263
+ final element = web.VideoElement ()
264
+ ..autoplay = true
265
+ ..muted = true
266
+ ..controls = false
267
+ ..srcObject = _videoStream
268
+ ..id = _elementIdForVideo
269
+ ..setAttribute ('playsinline' , 'true' );
270
+
271
+ _applyDefaultVideoStyles (element);
272
+
273
+ _subscriptions.add (
274
+ element.onCanPlay.listen ((dynamic _) {
275
+ _updateAllValues ();
276
+ }),
277
+ );
278
+
279
+ _subscriptions.add (
280
+ element.onResize.listen ((dynamic _) {
281
+ _updateAllValues ();
282
+ onResize? .call ();
283
+ }),
284
+ );
285
+
286
+ // The error event fires when some form of error occurs while attempting to load or perform the media.
287
+ _subscriptions.add (
288
+ element.onError.listen ((web.Event _) {
289
+ // The Event itself (_) doesn't contain info about the actual error.
290
+ // We need to look at the HTMLMediaElement.error.
291
+ // See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/error
292
+ final error = element.error;
293
+ print ('RTCVideoRenderer: videoElement.onError, ${error .toString ()}' );
294
+ throw PlatformException (
295
+ code: _kErrorValueToErrorName[error! .code]! ,
296
+ message: error.message != '' ? error.message : _kDefaultErrorMessage,
297
+ details: _kErrorValueToErrorDescription[error.code],
298
+ );
299
+ }),
300
+ );
301
+
302
+ _subscriptions.add (
303
+ element.onEnded.listen ((dynamic _) {
304
+ // print('RTCVideoRenderer: videoElement.onEnded');
305
+ }),
306
+ );
307
+
308
+ videoElement = element;
312
309
}
313
310
314
- void _applyDefaultVideoStyles (html .VideoElement element) {
311
+ void _applyDefaultVideoStyles (web .VideoElement element) {
315
312
// Flip the video horizontally if is mirrored.
316
313
if (mirror) {
317
314
element.style.transform = 'scaleX(-1)' ;
0 commit comments