Implement Picture-in-Picture support #22
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements Picture-in-Picture support using the Document Picture-in-Picture API, and extension of the Picture-in-Picture API.
While this is an experimental technology, and browser-support is limited for now, I thought this would be cool to try out. Using the Picture-in-Picture API seems less optimal because it's only supported on the video element, but we are currently drawing our video to a canvas for playback. The Document Picture-in-Picture API makes it possible to open an always-on-top window that can be populated with arbitrary HTML content — for example a video with custom controls or a set of streams showing the participants of a video conference call.
The core logic to support this function exists in the Picture-in-Picture Button component.
togglePictureInPicture
handles callingdocumentPictureInPicture.requestWindow
, which opens the Picture-in-Picture window for the current main browsing context. We then take our canvas and append it to the picture-in-picture window, and update its width/height to 100% to fit the window. We then add an event listener forpagehide
, which handles re-appending the canvas to the player container when the PiP window is closed.Other additions:
store.ts
file, which could potentially be used to share state in multiple places. Right now, we're sharingpipState
betweenwatch.tsx
andPictureInPictureButton.tsx
global.d.ts
file, so we could add declare a global type forDocumentPictureInPicture
watch.tsx
to display in place of the canvas when the video is in PiP modeFollow-up items: