What I'm building is a Video Conference app and I'm developing the app so we can use Picture in Picture mode (PiP). I want the PiP window to always show the video of the person that is currently speaking or is sharing screen. However, I'm not sure how I can switches the video source of this PiP window. The flow that I can think about is to first exitPictureInPicture(), then from the new video tag which is "pinned/active", I call requestPictureInPicture() again. But I felt like this is a bad approach, I think it would like we are closing the PiP mode and turning it back on. Definitely not what we want. Is there alternative way I can switch the PiP mode video source without having to exit the PiP mode first?
How to change the video source for Picture in Picture mode JavaScript?
311 Views Asked by Owenn At
1
In Chrome and Safari, you can simply change the
srcObjectof your<video>to point to the newMediaStream, it should work on the fly without noticeable interruption.In Firefox's own picture in picture mode however, they will close the popup a few seconds after the video got interrupted, this is true for when you switch the
srcObject, or when you change theMediaStreamTrackof youMediaStream. So to support this browser (which still doesn't support the officialrequestPictureInPictureAPI, btw), you'd have to resort to an ugly hack where you'd draw you video content over a<canvas>and display this canvas'sMediaStream, got through its.captureStream()method. This means that you need to draw every frame on that canvas, and you may also face issues with task throttling when the original tab isn't visible anymore... Least to say it's not great. And there is no easy way to feature detect this, apart from failing the first time, or using the heuristics that Firefox is the only main browser exposing this behavior, and also the only one not supportingrequestPictureInPicture.