|
1 | 1 | import 'dart:async';
|
2 | 2 | import 'dart:js_interop';
|
3 | 3 | import 'dart:js_util' as jsutil;
|
| 4 | +import 'dart:ui_web' as web_ui; |
4 | 5 |
|
5 | 6 | import 'package:flutter/foundation.dart';
|
6 | 7 | import 'package:flutter/services.dart';
|
@@ -213,7 +214,9 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue>
|
213 | 214 | }
|
214 | 215 |
|
215 | 216 | web.VideoElement? findHtmlView() {
|
216 |
| - return videoElement; |
| 217 | + final element = web.document.getElementById(_elementIdForVideo); |
| 218 | + if (null != element) return element as web.VideoElement; |
| 219 | + return null; |
217 | 220 | }
|
218 | 221 |
|
219 | 222 | @override
|
@@ -250,62 +253,62 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue>
|
250 | 253 | return false;
|
251 | 254 | }
|
252 | 255 |
|
253 |
| - web.VideoElement? videoElement; |
254 |
| - |
255 | 256 | @override
|
256 | 257 | Future<void> initialize() async {
|
257 |
| - // ignore: undefined_prefixed_name |
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; |
| 258 | + web_ui.platformViewRegistry.registerViewFactory(viewType, (int viewId) { |
| 259 | + for (var s in _subscriptions) { |
| 260 | + s.cancel(); |
| 261 | + } |
| 262 | + _subscriptions.clear(); |
| 263 | + |
| 264 | + final element = web.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((web.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-
F438
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 | + }); |
309 | 312 | }
|
310 | 313 |
|
311 | 314 | void _applyDefaultVideoStyles(web.VideoElement element) {
|
|
0 commit comments