< All posts

April 14, 2023, 3:52 a.m.

Streaming Webcam with OpenWRT(mjpeg-streamer)

If your OpenWRT router has a USB port and enought storage and memory, we can create a simple closed circuit television (CCTV) with a supported USB Web Camera. You can even stream it outside of your home network if you configure reverse proxy on your OpwnWRT but this would be on a different tutorial. To recreate this tutorial we need a OpenWRT based computer/router with a USB port, a webcamera that is supported by OpenWRT. You may check the references at the end of this post for further information.

Hardware Requirements:
- A router/computer with OpenWRT installed (Tested on OpenWrt 22.03.3)
- A USB Webcam
- Power supply for our OpenWRT powered router

Software Requirements:
- USB kernel modules and Video kernel modules
- mjpeg-streamer
- SSH Access to our OpenWRT router
- A Web Browser to test the live stream

We will now install the required software on our OpenWRT router:
Before you begin you should test if your OpenWRT device already supports USB devices by plugging in the webcam or a flash drive, get a console to your router and execute dmesg.
You can download PuTTY for accessingt the SSH of your device or just use Command Prompt for Windows. For Linux and Mac, you should just open Terminal and type ssh root@192.168.1.1
192.168.1.1 being the IP of your router.

# dmesg | tail

You should get a similar output below if the USB devices are already configured.

...
[ 1051.440118] usb 1-1: new high-speed USB device number 2 using ehci-platform
[ 1051.669338] hub 1-1:1.0: USB hub found
[ 1051.680969] hub 1-1:1.0: 4 ports detected
[ 1052.010035] usb 1-1.1: new full-speed USB device number 3 using ehci-platform
[ 1052.280106] usb 1-1.2: new high-speed USB device number 4 using ehci-platform

If not, then you must install the kernel modules for USB devices as shown in the OpenWRT wiki article. Follow the article before proceeding: https://openwrt.org/docs/guide-user/storage/usb-installing

In order for us to install new packages, we need to update the repository first by executing: # opkg update via console. Make sure you have internet connection on your device.

Now, we need to install the video kernel modules in order to use the supported USB web camera. You may follow the wiki for more information: https://openwrt.org/docs/guide-user/hardware/video/usb.video

In my test case, I used the Logitech Web Camera and my installed kernel modules are as follows:

# opkg install kmod-video-uvc

Installing kmod-video-uvc (5.10.161-1) to root...
..
Installing kmod-dma-buf (5.10.161-1) to root...
..
Installing kmod-i2c-core (5.10.161-1) to root...
..
Installing kmod-video-core (5.10.161-1) to root...
..
Installing kmod-video-videobuf2 (5.10.161-1) to root...
..
Configuring kmod-dma-buf.
Configuring kmod-i2c-core.
Configuring kmod-video-core.
Configuring kmod-input-core.
Configuring kmod-video-videobuf2.
Configuring kmod-video-uvc.

To test this, we need to replug the camera on the USB port and by executing dmesg | tail and ls /dev/video*you should see the output similar below to indicate that our camera is being detected by OpenWRT.

# dmesg | tail

[ 1404.961536] kmodloader: loading kernel modules from /etc/modules.d/*
[ 1405.007846] i2c /dev entries driver
[ 1405.043156] videodev: Linux video capture interface: v2.00
[ 1405.132552] uvcvideo: Found UVC 1.00 device ConferenceCam Connect (046d:084b)
[ 1405.143842] input: ConferenceCam Connect as /devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.0/input/input0
[ 1405.154645] usbcore: registered new interface driver uvcvideo
[ 1405.160475] USB Video Class driver (1.1.1)

# ls /dev|grep video*
video0
video1

We are now ready to install mjpeg-streamer:

# opkg install mjpg-streamer mjpg-streamer-input-uvc mjpg-streamer-output-http mjpg-streamer-www-

Configuring mjpeg-streamer, edit the configuration file with your preferred editor.

# vi /etc/config/mjpg-streamer

Set enabled to 1, and change the other info based on your needs.

Sample config:

config mjpg-streamer 'core'
        option enabled '1'
        option input 'uvc'
        option output 'http'
        option device '/dev/video0'
        option resolution '640x480'
        option yuv '0'
        option quality '80'
        option fps '5'
        option led 'auto'
        option www '/www/webcam'
        option port '8080'
        option listen_ip '192.168.1.1'
        option username 'openwrt'
        option password 'openwrt'

The following configuration states that:
- The stream can be accessed at http://192.168.1.1:8080 with the user/pass openwrt.
- Resolution is 640x480, quality 80% and frames per second is 5. Setting these to higher value may use more CPU.
- We are using the video device video0

To check the stream, execute: /etc/init.d/mjpg-streamer start

Visit now http://192.168.1.1:8080 and login with the username/password of openwrt.
You should now see the stream!

live_mjpeg_20230413.png

If you want to auto start the stream on every reboot, execute: /etc/init.d/mjpg-streamer enable

Very light CPU usage:

htop_mjpeg_20230413.png

References:
- https://openwrt.org/docs/guide-user/hardware/video/webcam
- https://openwrt.org/docs/guide-user/storage/usb-installing
- https://openwrt.org/docs/guide-user/hardware/video/usb.video

Except where otherwise noted, this work is licensed under Creative Commons Attribution-ShareAlike 4.0 International License (http://creativecommons.org/licenses/by-sa/4.0/).