All files / Rindu/utils callPictureInPicture.ts

91.66% Statements 22/24
66.66% Branches 2/3
100% Functions 1/1
91.66% Lines 22/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 458x       4x 1x 1x     3x 3x 3x 1x 1x     2x 2x 2x 2x 2x   1x 1x         1x           1x   1x 1x   1x   1x      
export async function callPictureInPicture(
  pictureInPictureCanvas: HTMLCanvasElement,
  video: HTMLVideoElement
): Promise<void> {
  if (!navigator.mediaSession?.metadata?.artwork) {
    console.warn("No artwork metadata");
    return;
  }
 
  const artwork = navigator.mediaSession.metadata.artwork;
  const imgSrc = [...artwork].pop()?.src;
  if (!imgSrc) {
    console.warn("No valid artwork URL");
    return;
  }
 
  try {
    const image = new Image();
    image.crossOrigin = "Anonymous";
    image.src = imgSrc;
    await image.decode();
 
    const ctx = pictureInPictureCanvas.getContext("2d");
    Iif (!ctx) {
      console.error("Canvas context is null");
      return;
    }
 
    ctx.clearRect(
      0,
      0,
      pictureInPictureCanvas.width,
      pictureInPictureCanvas.height
    );
    ctx.drawImage(image, 0, 0, 512, 512);
 
    video.pause();
    video.play();
 
    await video.requestPictureInPicture();
  } catch (err) {
    console.error("Error processing PiP:", err);
  }
}