10000 Support multiple videos · model-lib/flutter-webrtc@94a2720 · GitHub
[go: up one dir, main page]

Skip to content

Commit 94a2720

Browse files
authored
Support multiple videos
1 parent 21ecc71 commit 94a2720

File tree

1 file changed

+26
-36
lines changed

1 file changed

+26
-36
lines changed

lib/web/rtc_video_view.dart

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import 'dart:async';
22
// ignore: uri_does_not_exist
33
import 'dart:html' as HTML;
4-
// ignore: uri_does_not_exist
5-
import 'dart:js' as JS;
6-
import 'dart:ui' as ui;
7-
84
import 'package:flutter/material.dart';
9-
105
import 'media_stream.dart';
11-
import '../enums.dart';
6+
import 'dart:ui' as ui;
7+
8+
enum RTCVideoViewObjectFit {
9+
RTCVideoViewObjectFitContain,
10+
RTCVideoViewObjectFitCover,
11+
}
1212

1313
typedef void VideoRotationChangeCallback(int textureId, int rotation);
1414
typedef void VideoSizeChangeCallback(
@@ -31,38 +31,16 @@ class RTCVideoRenderer {
3131
HtmlElementView htmlElementView;
3232
HTML.VideoElement _htmlVideoElement;
3333

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>();
3735

3836
bool get isMuted => _htmlVideoElement?.muted ?? true;
3937
set isMuted(bool i) => _htmlVideoElement?.muted = i;
4038

4139
static void fixVideoElements() =>
42-
_videoViews.values.forEach((v) => v.play());
40+
_videoViews.forEach((v) => v.play());
4341

44-
/// Currently contains hacky solution
45-
/// Multiple videos won't work
46-
/// Waiting for onPlatformViewCreated callback implementation
4742
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");
6644
}
6745

6846
int get rotation => 0;
@@ -96,16 +74,28 @@ class RTCVideoRenderer {
9674

9775
set srcObject(MediaStream stream) {
9876
_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();
10092
}
10193

10294
void findAndApply(Size size) {
10395
final htmlView = findHtmlView();
10496
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())
10798
return;
108-
}
10999
htmlView.srcObject = _srcObject.jsStream;
110100
htmlView.width = size.width.toInt();
111101
htmlView.height = size.height.toInt();
@@ -200,7 +190,7 @@ class _RTCVideoViewState extends State<RTCVideoView> {
200190
child: new SizedBox(
201191
width: constraints.maxHeight * _aspectRatio,
202192
height: constraints.maxHeight,
203-
child: _renderer.htmlElementView
193+
child: _renderer.htmlElementView ?? Container()
204194
)
205195
);
206196
}

0 commit comments

Comments
 (0)
0