15

I am developing a java/selenium based application and need to pass media i.e. audio and/or video files to the browser.

I have been able to successfully do this in Chrome using the below code snippet:

DesiredCapabilities capabilities = DesiredCapabilities.chrome(); 
ChromeOptions options = new ChromeOptions();
options.addArguments("--allow-file-access-from-files",
            "--use-fake-ui-for-media-stream",
            "--allow-file-access",
            "--use-file-for-fake-audio-capture=D:\\PATH\\TO\\WAV\\xxx.wav",
            "--use-fake-device-for-media-stream");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);

Are there similar options available in Firefox 33.1 to achieve the same capability?

If not, how can this be done?

Cœur
  • 37,241
  • 25
  • 195
  • 267
John Sylvester
  • 257
  • 1
  • 2
  • 8

2 Answers2

7

Fake Webcam (Chrome & Firefox)

Here's a way setup a fake webcam from the command-line on Linux.

Dependencies

  • Linux (I'm using Ubuntu)
  • FFMpeg (video streaming tool)
  • v4l2loopback (enables a fake video device)

Installing v4l2loopback (Fake Webcam)

# 1. Install v4l2loopback kernel module from apt
sudo apt install v4l2loopback-dkms

# 2. Enable the "fake webcam", via v4l2loopback kernel module
sudo modprobe v4l2loopback devices=1 card_label="My Fake Webcam" exclusive_caps=1

Video File (Looped) as Fake Webcam

# Link video file (on loop) to the "fake webcam"
ffmpeg -stream_loop -1 -re -i ./MYFILE.mp4 -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 /dev/video0

Desktop Video as Fake Webcam

# Link desktop video stream to the "fake webcam"
ffmpeg -f x11grab -r 15 -s 1280x720 -i :0.0+0,0 -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 /dev/video0

The great part about this is that it works for both Firefox and Chrome! As it fakes the webcam at the kernel level.

Test your fake webcam here: https://webcamtests.com/

Chrome Test (Desktop Feed) enter image description here

Firefox Test (Desktop Feed) enter image description here

Disable Fake Webcam

If you need to disable it...

# To disable the fake webcam, simply remove it from the kernel
sudo modprobe --remove v4l2loopback

Anyway, hope someone finds this useful!

Ben Winding
  • 10,208
  • 4
  • 80
  • 67
  • Could you elaborate on your answer? I tried this: `sudo modprobe v4l2loopback devices=1 card_label="My Fake Webcam" exclusive_caps=1 | ffmpeg -f x11grab -r 15 -s 1280x720 -i :0.0+0,0 -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 /dev/video0` but Chrome still doesn't recognize my camera. Usually I'm using this approach, which works for Firefox only: https://askubuntu.com/a/972831/54817, but I'm looking for a Chrome solution to be able to use my camera for gotomeeting. – Torsten Dec 18 '20 at 12:19
  • Btw, I'm using Chromium Version 87.0.4280.66. – Torsten Dec 18 '20 at 12:31
  • Got it to work now: The exclusive_caps=1 only got recognized by chrome after I restarted my pc! – Torsten Dec 18 '20 at 13:56
  • You shouldn't have to restart the computer (at least I didn't) but you do need to ensure that all instances of Chrome are terminated first, use the System Monitor to check if any are running. – Ben Winding Dec 22 '20 at 07:03
  • @BenWinding: it's not working on the server where we don't have a webcam. Can you please help – SeleniumUser May 18 '22 at 14:49
5

Firefox has only these capabilities, browserName, browserVersion, platformName, acceptInsecureCerts, pageLoadStrategy, proxy, setWindowRect, timeouts, unhandledPromptBehavior

But as a option, there is one as in a below code,

options.addPreference("media.navigator.streams.fake", true);

Hope it will help you to solve this situation.

sayhan
  • 1,168
  • 1
  • 16
  • 22