1
1
import 'dart:async' ;
2
2
// ignore: uri_does_not_exist
3
3
import 'dart:html' as HTML;
4
- // ignore: uri_does_not_exist
5
- import 'dart:js' as JS;
6
- import 'dart:ui' as ui;
7
-
8
4
import 'package:flutter/material.dart' ;
9
-
10
5
import 'media_stream.dart' ;
11
- import '../enums.dart' ;
6
+ import 'dart:ui' as ui;
7
+
8
+ enum RTCVideoViewObjectFit {
9
+ RTCVideoViewObjectFitContain ,
10
+ RTCVideoViewObjectFitCover ,
11
+ }
12
12
13
13
typedef void VideoRotationChangeCallback (int textureId, int rotation);
14
14
typedef void VideoSizeChangeCallback (
@@ -31,38 +31,16 @@ class RTCVideoRenderer {
31
31
HtmlElementView htmlElementView;
32
32
HTML .VideoElement _htmlVideoElement;
33
33
34
- static var _isViewFactoryRegistered = false ;
35
- static final _videoViews = Map <int , HTML .VideoElement >();
36
- static Function (HTML .VideoElement ) _nextCallback;
34
+ static final _videoViews = List <HTML .VideoElement >();
37
35
38
36
bool get isMuted => _htmlVideoElement? .muted ?? true ;
39
37
set isMuted (bool i) => _htmlVideoElement? .muted = i;
40
38
41
39
static void fixVideoElements () =>
42
- _videoViews.values. forEach ((v) => v.play ());
40
+ _videoViews.forEach ((v) => v.play ());
43
41
44
- /// Currently contains hacky solution
45
- /// Multiple videos won't work
46
- /// Waiting for onPlatformViewCreated callback implementation
47
42
initialize () async {
48
- if (! _isViewFactoryRegistered) {
49
- // ignore: implementation_imports
50
- ui.platformViewRegistry.registerViewFactory ('webrtc_video' , (int viewId) {
51
- print ("Platform view creation" );
52
- final x = HTML .VideoElement ();
53
- x.autoplay = true ;
54
- x.muted = true ;
55
- if (_srcObject != null )
56
- x.srcObject = _srcObject.jsStream;
57
- _videoViews[viewId] = x;
58
- if (_nextCallback != null )
59
- _nextCallback (x);
60
- return x;
61
- });
62
- _isViewFactoryRegistered = true ;
63
- }
64
- _nextCallback = (v) => _htmlVideoElement = v;
65
- htmlElementView = HtmlElementView (viewType: 'webrtc_video' );
43
+ print ("You don't have to call RTCVideoRenderer.initialize on Flutter Web" );
66
44
}
67
45
68
46
int get rotation => 0 ;
@@ -96,16 +74,28 @@ class RTCVideoRenderer {
96
74
97
75
set srcObject (MediaStream stream) {
98
76
_srcObject = stream;
99
- findHtmlView ()? .srcObject = stream.jsStream;
77
+ if (htmlElementView != null ) {
78
+ findHtmlView ()? .srcObject = stream.jsStream;
79
+ }
80
+ ui.platformViewRegistry.registerViewFactory (stream.id, (int viewId) {
81
+ final x = HTML .VideoElement ();
82
+ x.autoplay = true ;
83
+ x.muted = true ;
84
+ x.srcObject = stream.jsStream;
85
+ _htmlVideoElement = x;
86
+ _videoViews.add (x);
87
+ return x;
88
+ });
89
+ htmlElementView = HtmlElementView (viewType: stream.id);
90
+ if (this .onStateChanged != null )
91
+ this .onStateChanged ();
100
92
}
101
93
102
94
void findAndApply (Size size) {
103
95
final htmlView = findHtmlView ();
104
96
if (_srcObject != null && htmlView != null ) {
105
- if (htmlView.width == size.width.toInt () && htmlView.height == size.height.toInt ()) {
106
- print ("Same size, return" );
97
+ if (htmlView.width == size.width.toInt () && htmlView.height == size.height.toInt ())
107
98
return ;
108
- }
109
99
htmlView.srcObject = _srcObject.jsStream;
110
100
htmlView.width = size.width.toInt ();
111
101
htmlView.height = size.height.toInt ();
@@ -200,7 +190,7 @@ class _RTCVideoViewState extends State<RTCVideoView> {
200
190
child: new SizedBox (
201
191
width: constraints.maxHeight * _aspectRatio,
202
192
height: constraints.maxHeight,
203
- child: _renderer.htmlElementView
193
+ child: _renderer.htmlElementView ?? Container ()
204
194
)
205
195
);
206
196
}
0 commit comments