Dev.to WebDev 🛠 Dev 👁 0 📖 5 min read

Mirroring the webcam other people see in Google Meet, Zoom and Teams (from a Chrome extension)

Google Meet, Zoom and Teams show you a mirrored self-view by default, but send everyone else the unflipped camera. So the face others see is not the one you know from the mirror, and text on a shirt or a whiteboard reads

Google Meet, Zoom and Teams show you a mirrored self-view by default, but send everyone else the unflipped camera. So the face others see is not the one you know from the mirror, and text on a shirt or a whiteboard reads backwards in your own preview. None of the three big apps lets you choose what the others get.

I built a small extension, MirrorMyCam, that adds that choice to the web versions of Google Meet, Zoom and Teams. This post is about how it works, because most of the hard parts are not the flip itself.

The flip is the easy part

Chrome has had Insertable Streams for video (MediaStreamTrackProcessor and MediaStreamTrackGenerator) since version 94. You get each camera frame as a VideoFrame, do something with it, and write it into a new track:

function mirrorTrack(source) {
  const processor = new MediaStreamTrackProcessor({ track: source });
  const generator = new MediaStreamTrackGenerator({ kind: 'video' });
  let canvas, ctx;

  const flip = new TransformStream({
    transform(frame, controller) {
      const w = frame.displayWidth, h = frame.displayHeight;
      if (!canvas || canvas.width !== w || canvas.height !== h) {
        canvas = new OffscreenCanvas(w, h);
        ctx = canvas.getContext('2d', { alpha: false, desynchronized: true });
      }
      ctx.setTransform(-1, 0, 0, 1, w, 0); // horizontal flip
      ctx.drawImage(frame, 0, 0, w, h);
      const flipped = new VideoFrame(canvas, { timestamp: frame.timestamp });
      frame.close(); // forget this and the camera can stall
      controller.enqueue(flipped);
    },
  });

  processor.readable.pipeThrough(flip).pipeTo(generator.writable).catch(() => {});
  return generator; // a MediaStreamTrack you can send instead of the camera
}

Two details matter. A VideoFrame made from a canvas needs a timestamp (the constructor throws a TypeError without one), so reuse the camera frame's to keep timing intact. And always close() the incoming frame: written frames are closed for you, incoming ones are not.

Getting in before the call app does

The page has to receive the flipped track instead of the real one. The way to do that is to wrap navigator.mediaDevices.getUserMedia before any page script runs. Manifest V3 can inject straight into the page's own JavaScript world ("world": "MAIN" in the manifest needs Chrome 111+):

{
  "content_scripts": [{
    "matches": ["https://meet.google.com/*", "https://*.zoom.us/*", "https://teams.microsoft.com/*"],
    "js": ["src/inject.js"],
    "run_at": "document_start",
    "all_frames": true,
    "match_origin_as_fallback": true,
    "world": "MAIN"
  }]
}

all_frames and match_origin_as_fallback are there because parts of a call can run inside iframes, including about:blank ones.

The wrapper itself is short:

const native = MediaDevices.prototype.getUserMedia;
Object.defineProperty(MediaDevices.prototype, 'getUserMedia', {
  configurable: true, writable: true,
  value: async function getUserMedia(...args) {
    const stream = await native.apply(this, args);
    if (!args[0]?.video) return stream;
    for (const source of stream.getVideoTracks()) {
      try {
        const replacement = mirrorTrack(source);
        stream.removeTrack(source);
        stream.addTrack(replacement);
      } catch {
        // leave the camera untouched rather than break the call
      }
    }
    return stream;
  },
});

A MAIN world script can't read chrome.storage, so a second content script in the isolated world reads the setting and passes it over with window.postMessage. In the full version, transform() checks that setting on every frame and passes the frame through untouched when it is "normal", so switching takes effect at once, without restarting the camera.

Making the new track behave like a camera

This is where most of the work went. A MediaStreamTrackGenerator isn't a camera, and the call apps notice:

  • getSettings(): a generator is not a device, so it doesn't report the real camera's deviceId and settings, which apps use to show which camera is active. So the replacement track forwards getSettings, getConstraints, getCapabilities and applyConstraints to the real camera track, and its label too.
  • stop() must turn the camera light off. Stopping the generator does not stop the source, so the camera stays on after you leave the call. The fix is to wrap stop() and stop the real camera when the last user is gone.
  • clone(): apps can clone the track, for example for the self-view or effects. Each clone has to get the same forwarding, and the camera may only stop when the original and every clone are stopped. One record per real camera with a reference count handles that.

The self-view should show what others see

After the flip, the apps' own self-view is wrong the other way: they still mirror the preview in CSS, so a mirrored outgoing picture shows up unmirrored to you. Your preview should show exactly what the others get.

Finding the self-view <video> is the tricky part. The extension watches srcObject assignments, then decides for each video:

  1. Does it play one of our tracks (or a clone)? Then it's the self-view.
  2. Meet's background blur creates a new track from ours, so rule 1 misses it. For that case a video counts as the self-view only if the page itself mirrors it, and the track is not from a remote peer (tracked by hooking RTCPeerConnection's track event) or a screen share (tracked by wrapping getDisplayMedia).

To know whether the page mirrors a video, it walks up the tree (through shadow roots too) and multiplies the sign of every horizontal flip: the determinant of each computed transform matrix, plus the CSS scale and rotate properties. Then it adds scale: -1 1 to the video when needed, so the preview cancels out to what is actually sent. A sweep re-checks this every half second, because the apps change their classes after the stream is attached.

This only works where the self-view is a <video> element, as in Meet. Zoom draws every video, the self-view included, on one canvas, and in a Teams call the self-view isn't reached this way either. So in Zoom and Teams the extension shows a tip to turn off the app's own "Mirror my video", and then the preview matches what others get.

Failing safe

The extension runs inside someone's live call, so every step falls back to doing nothing:

  • No Insertable Streams support: the original stream is returned.
  • A frame fails to draw: that frame goes out unflipped.
  • Settings don't arrive within a second: the default is used.
  • The bookkeeping inside each hook is wrapped in try/catch, so a bug in the extension falls back to the native behaviour instead of breaking the call.

There is no MirrorMyCam server. Frames are flipped inside the tab and go only where the call app sends them anyway; nothing is sent to us. The extension asks only for storage and activeTab, plus access to the three call sites.

What it doesn't cover

It only works in the web versions of Meet, Zoom and Teams, in Chrome and Edge on a computer. The Zoom and Teams desktop apps don't run extensions.

If you just want to see your un-mirrored face without joining a call, there is also a free true mirror on the site that shows your camera the way others see it.

Questions about the WebRTC side are welcome in the comments.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.