|
140 | 140 | image: this.shadowRoot.getElementById("mjpeg-output"),
|
141 | 141 | };
|
142 | 142 |
|
143 |
| - // Initialize the WebRTC media stream. |
144 |
| - this.elements.video.srcObject = new MediaStream(); |
| 143 | + // Note that we can’t assign a persistent `MediaStream` object to the |
| 144 | + // `srcObject` attribute that we would retain over the lifecycle of |
| 145 | + // this class, because otherwise Chrome shows an infinite loading |
| 146 | + // spinner for the browser tab. Therefore, we need to initialize the |
| 147 | + // source object lazily whenever we want to access it, and clear it |
| 148 | + // when we don’t need it anymore. |
| 149 | + this.elements.video.srcObject = null; |
145 | 150 |
|
146 | 151 | this._addScreenEventListeners(this.elements.video);
|
147 | 152 | this._addScreenEventListeners(this.elements.image);
|
|
347 | 352 | */
|
348 | 353 | async enableWebrtcStreamTrack(mediaStreamTrack) {
|
349 | 354 | const video = this.elements.video;
|
| 355 | + if (!video.srcObject) { |
| 356 | + // Lazy-initialize the media stream. (See comment in |
| 357 | + // `connectedCallback`.) |
| 358 | + video.srcObject = new MediaStream(); |
| 359 | + } |
350 | 360 | const stream = video.srcObject;
|
351 | 361 |
|
352 | 362 | // Ensure that the stream doesn't contain multiple tracks of the same
|
|
401 | 411 | */
|
402 | 412 | disableWebrtcStreamTrack(mediaStreamTrack) {
|
403 | 413 | const video = this.elements.video;
|
| 414 | + if (!video.srcObject) { |
| 415 | + return; |
| 416 | + } |
404 | 417 | const stream = video.srcObject;
|
405 | 418 | stream.removeTrack(mediaStreamTrack);
|
406 | 419 | if (stream.getVideoTracks().length === 0) {
|
|
488 | 501 | }
|
489 | 502 | this.elements.image.src = "/stream?advance_headers=1";
|
490 | 503 | this.webrtcEnabled = false;
|
| 504 | + // Clean up the media stream. (See comment in `connectedCallback`.) |
| 505 | + this.elements.video.srcObject = null; |
491 | 506 | this.dispatchEvent(new VideoStreamingModeChangedEvent("MJPEG"));
|
492 | 507 | }
|
493 | 508 |
|
|
0 commit comments