8000 FIX - #349 · flutter-webrtc/flutter-webrtc@6ba13fe · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ba13fe

Browse files
committed
FIX - #349
1 parent dce7e97 commit 6ba13fe

File tree

2 files changed

+65
-30
lines changed

2 files changed

+65
-30
lines changed

lib/src/web/rtc_video_view.dart

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue> {
8282
final int textureId;
8383
html.VideoElement videoElement;
8484
MediaStream _srcObject;
85+
final _subscriptions = <StreamSubscription>[];
8586

8687
bool get muted => videoElement?.muted ?? true;
8788

@@ -104,40 +105,57 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue> {
104105
ui.platformViewRegistry.registerViewFactory(
105106
'RTCVideoRenderer-$textureId', (int viewId) => videoElement);
106107

107-
videoElement.onCanPlay.listen((dynamic _) {
108-
value = value.copyWith(
109-
rotation: 0,
110-
width: videoElement.videoWidth.toDouble() ?? 0.0,
111-
height: videoElement.videoHeight.toDouble() ?? 0.0,
112-
renderVideo: renderVideo);
113-
print('RTCVideoRenderer: videoElement.onCanPlay ${value.toString()}');
114-
});
108+
_subscriptions.add(
109+
videoElement.onCanPlay.listen(
110+
(dynamic _) {
111+
value = value.copyWith(
112+
rotation: 0,
113+
width: videoElement.videoWidth.toDouble() ?? 0.0,
114+
height: videoElement.videoHeight.toDouble() ?? 0.0,
115+
renderVideo: renderVideo);
116+
print('RTCVideoRenderer: videoElement.onCanPlay ${value.toString()}');
117+
},
118+
),
119+
);
115120

116-
videoElement.onResize.listen((dynamic _) {
117-
value = value.copyWith(
118-
rotation: 0,
119-
width: videoElement.videoWidth.toDouble() ?? 0.0,
120-
height: videoElement.videoHeight.toDouble() ?? 0.0,
121-
renderVideo: renderVideo);
122-
print('RTCVideoRenderer: videoElement.onResize ${value.toString()}');
123-
});
121+
_subscriptions.add(
122+
videoElement.onResize.listen(
123+
(dynamic _) {
124+
value = value.copyWith(
125+
rotation: 0,
126+
width: videoElement.videoWidth.toDouble() ?? 0.0,
127+
height: videoElement.videoHeight.toDouble() ?? 0.0,
128+
renderVideo: renderVideo);
129+
print('RTCVideoRenderer: videoElement.onResize ${value.toString()}');
130+
},
131+
),
132+
);
124133

125134
// The error event fires when some form of error occurs while attempting to load or perform the media.
126-
videoElement.onError.listen((html.Event _) {
127-
// The Event itself (_) doesn't contain info about the actual error.
128-
// We need to look at the HTMLMediaElement.error.
129-
// See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/error
130-
var error = videoElement.error;
131-
throw PlatformException(
132-
code: _kErrorValueToErrorName[error.code],
133-
message: error.message != '' ? error.message : _kDefaultErrorMessage,
134-
details: _kErrorValueToErrorDescription[error.code],
135-
);
136-
});
135+
_subscriptions.add(
136+
videoElement.onError.listen(
137+
(html.Event _) {
138+
// The Event itself (_) doesn't contain info about the actual error.
139+
// We need to look at the HTMLMediaElement.error.
140+
// See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/error
141+
var error = videoElement.error;
142+
throw PlatformException(
143+
code: _kErrorValueToErrorName[error.code],
144+
message:
145+
error.message != '' ? error.message : _kDefaultErrorMessage,
146+
details: _kErrorValueToErrorDescription[error.code],
147+
);
148+
},
149+
),
150+
);
137151

138-
videoElement.onEnded.listen((dynamic _) {
139-
print('RTCVideoRenderer: videoElement.onEnded');
140-
});
152+
_subscriptions.add(
153+
videoElement.onEnded.listen(
154+
(dynamic _) {
155+
print('RTCVideoRenderer: videoElement.onEnded');
156+
},
157+
),
158+
);
141159
}
142160

143161
MediaStream get srcObject => _srcObject;
@@ -161,6 +179,9 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue> {
161179
super.dispose();
162180
await _srcObject?.dispose();
163181
_srcObject = null;
182+
_subscriptions.forEach((s) {
183+
s.cancel();
184+
});
164185
videoElement.removeAttribute('src');
165186
videoElement.load();
166187
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@TestOn('browser')
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:flutter_webrtc/src/web/get_user_media.dart';
4+
import 'package:flutter_webrtc/src/web/rtc_video_view.dart';
5+
6+
void main() {
7+
// TODO(wer-mathurin): should revisit after this bug is resolved, https://github.com/flutter/flutter/issues/66045.
8+
test('should complete succesfully', () async {
9+
var renderer = RTCVideoRenderer();
10+
await renderer.initialize();
11+
renderer.srcObject = await MediaDevices.getUserMedia({});
12+
await renderer.dispose();
13+
});
14+
}

0 commit comments

Comments
 (0)
0