Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add document pip support #1325

Merged
merged 5 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/strange-pans-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': minor
---

Add support for detecting video element visibility in Document PiP (can be tested on the examples/demo)
62 changes: 51 additions & 11 deletions examples/demo/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,43 @@ const appActions = {
await currentRoom.setE2EEEnabled(!currentRoom.isE2EEEnabled);
},

togglePiP: async () => {
if (window.documentPictureInPicture?.window) {
window.documentPictureInPicture?.window.close();
return;
}

const pipWindow = await window.documentPictureInPicture?.requestWindow();
// Copy style sheets over from the initial document
// so that the views look the same.
[...document.styleSheets].forEach((styleSheet) => {
try {
const cssRules = [...styleSheet.cssRules].map((rule) => rule.cssText).join('');
const style = document.createElement('style');
style.textContent = cssRules;
pipWindow.document.head.appendChild(style);
} catch (e) {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.type = styleSheet.type;
link.media = styleSheet.media;
link.href = styleSheet.href;
pipWindow.document.head.appendChild(link);
}
});
// Move participant videos to the Picture-in-Picture window
const participantsArea = $('participants-area');
const pipParticipantsArea = document.createElement('div');
pipParticipantsArea.id = 'participants-area';
pipWindow.document.body.append(pipParticipantsArea);
[...participantsArea.children].forEach((child) => pipParticipantsArea.append(child));

// Move participant videos back when the Picture-in-Picture window closes.
pipWindow.addEventListener('pagehide', (event) => {
[...pipParticipantsArea.children].forEach((child) => participantsArea.append(child));
});
},

ratchetE2EEKey: async () => {
if (!currentRoom || !currentRoom.options.e2ee) {
return;
Expand Down Expand Up @@ -525,10 +562,12 @@ function appendLog(...args: any[]) {

// updates participant UI
function renderParticipant(participant: Participant, remove: boolean = false) {
const container = $('participants-area');
const container =
window.documentPictureInPicture?.window?.document.querySelector('#participants-area') ||
$('participants-area');
if (!container) return;
const { identity } = participant;
let div = $(`participant-${identity}`);
let div = container.querySelector(`#participant-${identity}`);
if (!div && !remove) {
div = document.createElement('div');
div.id = `participant-${identity}`;
Expand Down Expand Up @@ -570,8 +609,8 @@ function renderParticipant(participant: Participant, remove: boolean = false) {
updateVideoSize(videoElm!, sizeElm!);
};
}
const videoElm = <HTMLVideoElement>$(`video-${identity}`);
const audioELm = <HTMLAudioElement>$(`audio-${identity}`);
const videoElm = <HTMLVideoElement>container.querySelector(`#video-${identity}`);
const audioELm = <HTMLAudioElement>container.querySelector(`#audio-${identity}`);
if (remove) {
div?.remove();
if (videoElm) {
Expand All @@ -586,12 +625,12 @@ function renderParticipant(participant: Participant, remove: boolean = false) {
}

// update properties
$(`name-${identity}`)!.innerHTML = participant.identity;
container.querySelector(`#name-${identity}`)!.innerHTML = participant.identity;
if (participant instanceof LocalParticipant) {
$(`name-${identity}`)!.innerHTML += ' (you)';
container.querySelector(`#name-${identity}`)!.innerHTML += ' (you)';
}
const micElm = $(`mic-${identity}`)!;
const signalElm = $(`signal-${identity}`)!;
const micElm = container.querySelector(`#mic-${identity}`)!;
const signalElm = container.querySelector(`#signal-${identity}`)!;
const cameraPub = participant.getTrackPublication(Track.Source.Camera);
const micPub = participant.getTrackPublication(Track.Source.Microphone);
if (participant.isSpeaking) {
Expand All @@ -601,7 +640,7 @@ function renderParticipant(participant: Participant, remove: boolean = false) {
}

if (participant instanceof RemoteParticipant) {
const volumeSlider = <HTMLInputElement>$(`volume-${identity}`);
const volumeSlider = <HTMLInputElement>container.querySelector(`#volume-${identity}`);
volumeSlider.addEventListener('input', (ev) => {
participant.setVolume(Number.parseFloat((ev.target as HTMLInputElement).value));
});
Expand Down Expand Up @@ -630,7 +669,7 @@ function renderParticipant(participant: Participant, remove: boolean = false) {
cameraPub?.videoTrack?.attach(videoElm);
} else {
// clear information display
$(`size-${identity}`)!.innerHTML = '';
container.querySelector(`#size-${identity}`)!.innerHTML = '';
if (cameraPub?.videoTrack) {
// detach manually whenever possible
cameraPub.videoTrack?.detach(videoElm);
Expand Down Expand Up @@ -659,7 +698,7 @@ function renderParticipant(participant: Participant, remove: boolean = false) {
micElm.innerHTML = '<i class="fas fa-microphone-slash"></i>';
}

const e2eeElm = $(`e2ee-${identity}`)!;
const e2eeElm = container.querySelector(`#e2ee-${identity}`)!;
if (participant.isEncrypted) {
e2eeElm.className = 'e2ee-on';
e2eeElm.innerHTML = '<i class="fas fa-lock"></i>';
Expand Down Expand Up @@ -795,6 +834,7 @@ function setButtonsForState(connected: boolean) {
'toggle-video-button',
'toggle-audio-button',
'share-screen-button',
'toggle-pip-button',
'disconnect-ws-button',
'disconnect-room-button',
'flip-video-button',
Expand Down
9 changes: 9 additions & 0 deletions examples/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ <h2>Livekit Sample App</h2>
>
Share Screen
</button>
<button
id="toggle-pip-button"
class="btn btn-secondary mt-1"
disabled
type="button"
onclick="appActions.togglePiP(document.getElementById('participants-area'))"
>
Toggle PiP
</button>
<button
id="toggle-e2ee-button"
class="btn btn-secondary mt-1"
Expand Down
39 changes: 27 additions & 12 deletions src/room/track/RemoteVideoTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class HTMLElementInfo implements ElementInfo {
constructor(element: HTMLMediaElement, visible?: boolean) {
this.element = element;
this.isIntersecting = visible ?? isElementInViewport(element);
this.isPiP = isWeb() && document.pictureInPictureElement === element;
this.isPiP = isWeb() && isElementInPiP(element);
this.visibilityChangedAt = 0;
}

Expand All @@ -346,7 +346,7 @@ class HTMLElementInfo implements ElementInfo {
observe() {
// make sure we update the current visible state once we start to observe
this.isIntersecting = isElementInViewport(this.element);
this.isPiP = document.pictureInPictureElement === this.element;
this.isPiP = isElementInPiP(this.element);

(this.element as ObservableMediaElement).handleResize = () => {
this.handleResize?.();
Expand All @@ -357,24 +357,28 @@ class HTMLElementInfo implements ElementInfo {
getResizeObserver().observe(this.element);
(this.element as HTMLVideoElement).addEventListener('enterpictureinpicture', this.onEnterPiP);
(this.element as HTMLVideoElement).addEventListener('leavepictureinpicture', this.onLeavePiP);
window.documentPictureInPicture?.addEventListener('enter', this.onEnterPiP);
window.documentPictureInPicture?.window?.addEventListener('pagehide', this.onLeavePiP);
}

private onVisibilityChanged = (entry: IntersectionObserverEntry) => {
const { target, isIntersecting } = entry;
if (target === this.element) {
this.isIntersecting = isIntersecting;
this.isPiP = isElementInPiP(this.element);
this.visibilityChangedAt = Date.now();
this.handleVisibilityChanged?.();
}
};

private onEnterPiP = () => {
this.isPiP = true;
window.documentPictureInPicture?.window?.addEventListener('pagehide', this.onLeavePiP);
this.isPiP = isElementInPiP(this.element);
this.handleVisibilityChanged?.();
};

private onLeavePiP = () => {
this.isPiP = false;
this.isPiP = isElementInPiP(this.element);
this.handleVisibilityChanged?.();
};

Expand All @@ -389,17 +393,29 @@ class HTMLElementInfo implements ElementInfo {
'leavepictureinpicture',
this.onLeavePiP,
);
window.documentPictureInPicture?.removeEventListener('enter', this.onEnterPiP);
window.documentPictureInPicture?.window?.removeEventListener('pagehide', this.onLeavePiP);
}
}

// does not account for occlusion by other elements
function isElementInViewport(el: HTMLElement) {
function isElementInPiP(el: HTMLElement) {
// Simple video PiP
if (document.pictureInPictureElement === el) return true;
// Document PiP
if (window.documentPictureInPicture?.window)
return isElementInViewport(el, window.documentPictureInPicture?.window);
return false;
}

// does not account for occlusion by other elements or opacity property
function isElementInViewport(el: HTMLElement, win?: Window) {
const viewportWindow = win || window;
let top = el.offsetTop;
let left = el.offsetLeft;
const width = el.offsetWidth;
const height = el.offsetHeight;
const { hidden } = el;
const { opacity, display } = getComputedStyle(el);
const { display } = getComputedStyle(el);

while (el.offsetParent) {
el = el.offsetParent as HTMLElement;
Expand All @@ -408,12 +424,11 @@ function isElementInViewport(el: HTMLElement) {
}

return (
top < window.pageYOffset + window.innerHeight &&
left < window.pageXOffset + window.innerWidth &&
top + height > window.pageYOffset &&
left + width > window.pageXOffset &&
top < viewportWindow.pageYOffset + viewportWindow.innerHeight &&
left < viewportWindow.pageXOffset + viewportWindow.innerWidth &&
top + height > viewportWindow.pageYOffset &&
left + width > viewportWindow.pageXOffset &&
!hidden &&
(opacity !== '' ? parseFloat(opacity) > 0 : true) &&
display !== 'none'
);
}
12 changes: 12 additions & 0 deletions src/type-polyfills/document-pip.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
interface Window {
/**
* Currently only available in Chromium based browsers:
* https://developer.mozilla.org/en-US/docs/Web/API/DocumentPictureInPicture
*/
documentPictureInPicture?: DocumentPictureInPicture;
}

interface DocumentPictureInPicture extends EventTarget {
window?: Window;
requestWindow(options?: { width: number; height: number }): Promise<Window>;
}
Loading