Screen recording fails on Ubuntu: MediaRecorder API throws NotReadableError

I’m hitting a snag with screen recording on Ubuntu. It’s weird because it works fine on Windows, but Linux is giving me grief. Here’s what’s happening:

const screenStream = await navigator.mediaDevices.getDisplayMedia({
  video: {
    screen: true,
    resolution: { width: 1280, height: 720 },
  },
});

const recorder = new MediaRecorder(screenStream);

recorder.ondata = (chunk) => {
  // Handle video data
};

When I run this, I get:

Error: Can't access display media: NotReadableError

I’ve tried a bunch of stuff:

  • Added flags like --use-fake-ui-for-media-stream
  • Set --auto-select-desktop-capture-source=Entire screen
  • Turned on AudioServiceOutOfProcess
  • Disabled some sandbox settings

None of it helped. The browser’s running in non-headless mode with Puppeteer. Any ideas why this is happening on Ubuntu but not Windows? I just want to record the whole screen. Help!

Hey there, I’ve run into similar issues with screen recording on Ubuntu before. It can be a real pain! One thing that worked for me was using xdg-desktop-portal. It’s a system service that helps with screen sharing on Linux.

First, make sure it’s installed:

sudo apt install xdg-desktop-portal xdg-desktop-portal-gtk

Then, try running your browser with these flags:

--enable-features=UseOzonePlatform --ozone-platform=wayland

This forces the browser to use Wayland, which tends to play nicer with screen sharing on Ubuntu.

Another trick is to use a virtual display like Xvfb. It creates a fake display that you can record from. It’s a bit of a workaround, but it’s saved me a few times when nothing else worked.

Hope this helps! Let me know if you need any more details on setting these up.

yo, ubuntu can be a real pain with screen recording. have u tried using OBS Studio? it’s pretty solid for capturing screens on linux. might be easier than messing with browser APIs. just install it, set up a display capture source, and you’re good to go. could save u a headache, ya know?

Have you considered using a different approach altogether? I’ve found that leveraging WebRTC for screen capture can be more reliable across different platforms, including Ubuntu. Here’s a basic example:

navigator.mediaDevices.getDisplayMedia({ video: true })
  .then(stream => {
    const videoTrack = stream.getVideoTracks()[0];
    const peer = new RTCPeerConnection();
    peer.addTrack(videoTrack, stream);
    // Set up your connection logic here
  })
  .catch(error => console.error('Error accessing display media:', error));

This method bypasses some of the issues with MediaRecorder and might work better on Ubuntu. Make sure you have the necessary WebRTC libraries installed and configured in your project. Also, check if your Ubuntu version is fully up to date, as older versions sometimes have quirks with screen sharing APIs. Let me know if this helps or if you need more guidance!