@@ -82,6 +82,7 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue> {
82
82
final int textureId;
83
83
html.VideoElement videoElement;
84
84
MediaStream _srcObject;
85
+ final _subscriptions = < StreamSubscription > [];
85
86
86
87
bool get muted => videoElement? .muted ?? true ;
87
88
@@ -104,40 +105,57 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue> {
104
105
ui.platformViewRegistry.registerViewFactory (
105
106
'RTCVideoRenderer-$textureId ' , (int viewId) => videoElement);
106
107
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
+ );
115
120
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
+ );
124
133
125
134
// 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
+ );
137
151
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
+ );
141
159
}
142
160
143
161
MediaStream get srcObject => _srcObject;
@@ -161,6 +179,9 @@ class RTCVideoRenderer extends ValueNotifier<RTCVideoValue> {
161
179
super .dispose ();
162
180
await _srcObject? .dispose ();
163
181
_srcObject = null ;
182
+ _subscriptions.forEach ((s) {
183
+ s.cancel ();
184
+ });
164
185
videoElement.removeAttribute ('src' );
165
186
videoElement.load ();
166
187
}
0 commit comments