Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.
TypeScript debiandeep-videojitsijitsi-meetscalable-video-conferencessfuvideovideo-communicationvideo-conferencingwebrtc
fix(pip): Fix PiP not working when enabled via overwriteConfig
Hristo Terezov2d8909911e3a
diff --git a/modules/API/external/external_api.js b/modules/API/external/external_api.jsindex f0c6d66745..05c604b8d4 100644--- a/modules/API/external/external_api.js+++ b/modules/API/external/external_api.js@@ -632,26 +632,6 @@ export default class JitsiMeetExternalAPI extends EventEmitter {// Since this is internal event we don't need to emit it to the consumer of the API.return true;- case 'config-overwrite': {- // Update stored pip config when overwriteConfig is called.- if (data.pip !== undefined) {- const wasEnabled = isPiPEnabled(this._pipConfig);-- this._pipConfig = data.pip;-- const isEnabled = isPiPEnabled(this._pipConfig);-- // Handle enable/disable transitions.- if (!wasEnabled && isEnabled) {- this._setupIntersectionObserver();- } else if (wasEnabled && !isEnabled) {- this._teardownIntersectionObserver();- }- }-- // Since this is internal event we don't need to emit it to the consumer of the API.- return true;- }}const eventName = events[name];@@ -958,10 +938,47 @@ export default class JitsiMeetExternalAPI extends EventEmitter {return;}++ // Handle pip config changes locally.+ // We update local state, send command to iframe, then handle PiP show/hide+ // so the iframe config is updated before we try to show PiP.+ let pipTransition = null;++ if (name === 'overwriteConfig' && args[0]?.pip !== undefined) {+ const wasEnabled = isPiPEnabled(this._pipConfig);++ this._pipConfig = {+ ...this._pipConfig,+ ...args[0].pip+ };++ const isEnabled = isPiPEnabled(this._pipConfig);++ if (!wasEnabled && isEnabled) {+ this._setupIntersectionObserver();+ pipTransition = 'enabled';+ } else if (wasEnabled && !isEnabled) {+ this._teardownIntersectionObserver();+ pipTransition = 'disabled';+ }+ }++ // Send command to iframe first.this._transport.sendEvent({data: args,name: commands[name]});++ // Handle PiP state after command is sent so iframe config is updated.+ if (pipTransition === 'enabled') {+ // Show PiP if iframe is currently not visible.+ if (!this._isIntersecting) {+ this.showPiP();+ }+ } else if (pipTransition === 'disabled') {+ // Hide any open PiP window.+ this.hidePiP();+ }}/**diff --git a/react/features/pip/components/PiPVideoElement.tsx b/react/features/pip/components/PiPVideoElement.tsxindex fe18af6690..14d92f06f1 100644--- a/react/features/pip/components/PiPVideoElement.tsx+++ b/react/features/pip/components/PiPVideoElement.tsx@@ -63,12 +63,11 @@ const PiPVideoElement: React.FC = () => {);const dispatch: IStore['dispatch'] = useDispatch();- const videoElement = videoRef.current;const avatarFont = getAvatarFont(theme);const fontFamily = (avatarFont as any).fontFamily ?? 'Inter, sans-serif';const initialsColor = getAvatarInitialsColor(theme);const displayNameColor = getDisplayNameColor(theme);- const { canvasStream } = useCanvasAvatar({+ const { canvasStreamRef } = useCanvasAvatar({participant,displayName,customAvatarBackgrounds,@@ -87,6 +86,8 @@ const PiPVideoElement: React.FC = () => {* Effect: Handle switching between real video track and canvas avatar stream.*/useEffect(() => {+ const videoElement = videoRef.current;+if (!videoElement) {return;}@@ -104,6 +105,9 @@ const PiPVideoElement: React.FC = () => {if (shouldShowAvatar) {// Use canvas stream for avatar.+ // Access ref inside effect - stream is created in useCanvasAvatar's effect.+ const canvasStream = canvasStreamRef.current;+// Only set srcObject if it's different to avoid interrupting playback.if (canvasStream && videoElement.srcObject !== canvasStream) {videoElement.srcObject = canvasStream;@@ -128,13 +132,15 @@ const PiPVideoElement: React.FC = () => {}}};- }, [ videoTrack, shouldShowAvatar, canvasStream, videoElement ]);+ }, [ videoTrack, shouldShowAvatar ]);/*** Effect: Window blur/focus and visibility change listeners.* Enters PiP on blur, exits on focus (matches old AOT behavior).*/useEffect(() => {+ const videoElement = videoRef.current;+if (!videoElement) {return;}@@ -147,7 +153,7 @@ const PiPVideoElement: React.FC = () => {// which seems to put Chrome into a weird state - document.exitPictureInPicture() never resolves, the// leavepictureinpicture is never triggered and it is not possible to display PiP again.// This is probably a browser bug. To workaround it we have the 100ms timeout here. This way this event- // is triggered after the leavepictureinpicture event and everythink seems to work well.+ // is triggered after the leavepictureinpicture event and everything seems to work well.setTimeout(() => {dispatch(handleWindowFocus());}, 100);@@ -162,18 +168,37 @@ const PiPVideoElement: React.FC = () => {window.addEventListener('focus', onWindowFocus);document.addEventListener('visibilitychange', onVisibilityChange);+ // Check if window is already blurred on mount (handles PiP enable while app is in background).+ // Wait for video to be ready before attempting PiP (canvas stream may not be attached yet).+ const checkFocusAndEnterPiP = () => {+ if (!document.hasFocus()) {+ onWindowBlur();+ }+ };++ if (videoElement.readyState >= 1) {+ // Video already has metadata loaded (e.g., real video track was already attached).+ checkFocusAndEnterPiP();+ } else {+ // Wait for video source to be ready (e.g., canvas stream being created).+ videoElement.addEventListener('loadedmetadata', checkFocusAndEnterPiP, { once: true });+ }+return () => {window.removeEventListener('blur', onWindowBlur);window.removeEventListener('focus', onWindowFocus);document.removeEventListener('visibilitychange', onVisibilityChange);+ videoElement.removeEventListener('loadedmetadata', checkFocusAndEnterPiP);};- }, [ dispatch, videoElement ]);+ }, [ dispatch ]);/*** Effect: PiP enter/leave event listeners.* Updates Redux state when browser PiP events occur.*/useEffect(() => {+ const videoElement = videoRef.current;+if (!videoElement) {return;}@@ -192,7 +217,7 @@ const PiPVideoElement: React.FC = () => {videoElement.removeEventListener('enterpictureinpicture', onEnterPiP);videoElement.removeEventListener('leavepictureinpicture', onLeavePiP);};- }, [ videoElement, dispatch ]);+ }, [ dispatch ]);return (<videodiff --git a/react/features/pip/hooks.ts b/react/features/pip/hooks.tsindex fb2f101542..bcee6b1225 100644--- a/react/features/pip/hooks.ts+++ b/react/features/pip/hooks.ts@@ -1,4 +1,4 @@-import { useEffect, useRef } from 'react';+import React, { useEffect, useRef } from 'react';import IconUserSVG from '../base/icons/svg/user.svg?raw';import { IParticipant } from '../base/participants/types';@@ -34,9 +34,11 @@ interface IUseCanvasAvatarOptions {/*** Result returned by the useCanvasAvatar hook.+ * Returns a ref object so consumers can access .current inside effects+ * (the stream is created in an effect and won't be available at render time).*/interface IUseCanvasAvatarResult {- canvasStream: MediaStream | null;+ canvasStreamRef: React.MutableRefObject<MediaStream | null>;}/**@@ -45,7 +47,6 @@ interface IUseCanvasAvatarResult {interface ICanvasRefs {canvas: HTMLCanvasElement | null;defaultIcon: HTMLImageElement | null;- stream: MediaStream | null;}/**@@ -92,10 +93,18 @@ export function useCanvasAvatar(options: IUseCanvasAvatarOptions): IUseCanvasAvaconst refs = useRef<ICanvasRefs>({canvas: null,- stream: null,defaultIcon: null});+ // Separate ref for the stream to return to consumers.+ // This allows consumers to access .current inside their effects.+ //+ // NOTE: If we ever need to recreate the stream (e.g., different canvas size),+ // consumers' effects won't automatically re-run since refs don't trigger re-renders.+ // To fix this, we could return an additional state flag like `streamReady` that+ // changes when the stream is set, and consumers would add it to their effect deps.+ const streamRef = useRef<MediaStream | null>(null);+/*** Initialize canvas, stream, and default icon on mount.*/@@ -108,7 +117,7 @@ export function useCanvasAvatar(options: IUseCanvasAvatarOptions): IUseCanvasAvarefs.current.canvas = canvas;// Create stream from canvas.- refs.current.stream = canvas.captureStream(CANVAS_FRAME_RATE);+ streamRef.current = canvas.captureStream(CANVAS_FRAME_RATE);// Load default icon.refs.current.defaultIcon = createDefaultIconImage();@@ -117,9 +126,9 @@ export function useCanvasAvatar(options: IUseCanvasAvatarOptions): IUseCanvasAva// Cleanup on unmount.return () => {- if (refs.current.stream) {- refs.current.stream.getTracks().forEach(track => track.stop());- refs.current.stream = null;+ if (streamRef.current) {+ streamRef.current.getTracks().forEach(track => track.stop());+ streamRef.current = null;}refs.current.canvas = null;refs.current.defaultIcon = null;@@ -131,7 +140,7 @@ export function useCanvasAvatar(options: IUseCanvasAvatarOptions): IUseCanvasAva* Re-render avatar when participant or display name changes.*/useEffect(() => {- const { canvas, defaultIcon, stream } = refs.current;+ const { canvas, defaultIcon } = refs.current;if (!canvas) {return;@@ -159,7 +168,7 @@ export function useCanvasAvatar(options: IUseCanvasAvatarOptions): IUseCanvasAva).then(() => {// Request a frame capture after drawing.// For captureStream(0), we need to manually trigger frame capture.- const track = stream?.getVideoTracks()[0] as MediaStreamTrack & { requestFrame?: () => void; };+ const track = streamRef.current?.getVideoTracks()[0] as MediaStreamTrack & { requestFrame?: () => void; };if (track?.requestFrame) {track.requestFrame();@@ -169,6 +178,6 @@ export function useCanvasAvatar(options: IUseCanvasAvatarOptions): IUseCanvasAva}, [ participant?.loadableAvatarUrl, participant?.name, displayName, customAvatarBackgrounds, backgroundColor, fontFamily, initialsColor, displayNameColor ]);return {- canvasStream: refs.current.stream+ canvasStreamRef: streamRef};}