tobias.k3
Posts: 75
Joined: Fri Mar 24, 2017 1:34 pm
Location: Germany, Mainz
Contact: Send private message

Solution for video bandwidth problem using Octopi

Mon Feb 19, 2018 2:02 pm

This topic is discussed several times in the Octoprint/Octopi github page. Currently the mjpeg stream from any USB Webcam on Octopi is just streamed through mjpeg-streamer. This approach puts least load on the CPU but results in huge bandwiths (about 5 MB/s at 640x480 @ 10 fps), which makes remote access to the stream complicated.

Has anyone here created a way to properly encode the web stream with h.264 or to pass through the h.264 directly from a suitable camera (e.g. Logitech C920)?
----------------------------------------------
PRUSA MK2
- Noctua fan 40mm
- MK2/MK3 hybrid extruder

User avatar
nikolai.r
Posts: 317
Joined: Wed Nov 09, 2016 8:32 am
Topics answered: 2
Location: USA - California
Contact: Send private message

Re: Solution for video bandwidth problem using Octopi

Mon Feb 19, 2018 10:50 pm

Hello Tobias,

Your calculation is not correct. 640x480x10 = 3,072,000 ( ~3MB/s) in raw data. Compressed MJPEG stream is usually around 300-600k in my setup. It highly depends where you mount the camera.

If you want to have h264 stream I would suggest to:
1. Buy a camera which streams in h264
2. Take a look at the UV4L project. I don't think Raspi will be able to handle live reencoding, serving and handling Octopi. But this would be a software approach.

tobias.k3
Posts: 75
Joined: Fri Mar 24, 2017 1:34 pm
Location: Germany, Mainz
Contact: Send private message

Re: Solution for video bandwidth problem using Octopi

Tue Feb 20, 2018 10:04 am

My fault. I meant about 5 Mb/s = 5 Mbit/s (which corresponds to 600 kB/s = 600 kByte/s, which you are seeing). At leasts that is the additional network traffic, when I open the webcam stream.

What exactly do you mean, with it highly depends where I mount the camera?

(By the way, raw data for a RGB (3 color channles with 8bit=1byte resolution each) video stream should be 640x480x3x10=~9.2 MByte/s)
----------------------------------------------
PRUSA MK2
- Noctua fan 40mm
- MK2/MK3 hybrid extruder

User avatar
nikolai.r
Posts: 317
Joined: Wed Nov 09, 2016 8:32 am
Topics answered: 2
Location: USA - California
Contact: Send private message

Re: Solution for video bandwidth problem using Octopi

Tue Feb 20, 2018 5:46 pm

You're right, I forgot to count in the color information in the RAW data.
But we're on the same page regarding the real data transfer anyway. :)

JPEG codec compress best on a consistent, even surfaces. So the best result you would achieve if you would have a white background and your model pretty big in foreground. You can also check this practically by adjusting/changing the camera position and taking one snapshot. Compare the size of the images.

I think this way you might get 10-20% better compression. If your goal tends more towards 100-1000%, then the two mentioned options would be required.

tobias.k3
Posts: 75
Joined: Fri Mar 24, 2017 1:34 pm
Location: Germany, Mainz
Contact: Send private message

Re: Solution for video bandwidth problem using Octopi

Tue Feb 20, 2018 8:42 pm

Yes you are right and my enclosure is painted evenly white from the inside ;-) However I would appreciate a 730p @ 30 fps stream at the same bitrate -> somehow get h.264 to work
----------------------------------------------
PRUSA MK2
- Noctua fan 40mm
- MK2/MK3 hybrid extruder

User avatar
nikolai.r
Posts: 317
Joined: Wed Nov 09, 2016 8:32 am
Topics answered: 2
Location: USA - California
Contact: Send private message

Re: Solution for video bandwidth problem using Octopi

Wed Feb 21, 2018 5:28 pm

h264 is not always better. The big advantage of MJPEG is that many browser can display it native. It's still not working to embed h264 stream with "video" tags.
BTW: You mentioned C920. Have you tried following approach? https://wiki.matthiasbock.net/index.php ... ming_H.264
It will not work in the browser, but you would have your h264 stream ;)

urho.t
Posts: 2
Joined: Sun Dec 18, 2016 7:03 pm
Contact: Send private message

Re: Solution for video bandwidth problem using Octopi

Thu Feb 22, 2018 2:08 pm

I use cvlc myself with Logitech C920, much like how it was described in the link above.

The h264 stream works well at 1080p, and doesn't consume much bandwith or CPU. However it does not work on a web browser, so instead i use a video player such as MPC or tinyCam Pro (android).

Here's my systemd script to start the service.
Things you might have to change in this configuration:
  • My usb device is mapped to /dev/video-c920, but by default it's probably /dev/video0
  • The service is set to run the daemon as user "octoprint". Change as needed.
/etc/systemd/system/cvlc.service:

Code: Select all

[Unit] Description=cvlc video stream After=network.target [Service] Type=simple SyslogIdentifier=cvlc ExecStartPre=/usr/bin/v4l2-ctl -d /dev/video-c920 -c power_line_frequency=1 #ExecStartPre=/usr/bin/v4l2-ctl -d /dev/video-c920 -c focus_auto=1 ExecStartPre=/usr/bin/v4l2-ctl -d /dev/video-c920 -c focus_auto=0 ExecStartPre=/usr/bin/v4l2-ctl -d /dev/video-c920 -c focus_absolute=35 ExecStart=/usr/bin/cvlc v4l2:///dev/video-c920:chroma=h264:width=1920:height=1080 vlc://quit --sout '#standard{access=http,mux=ts,dst=0.0.0.0:9090,name=stream,mime=video/ts}' -vvv RestartSec=10s User=octoprint [Install] WantedBy=multi-user.target

Optionally, custom udev rules to map the device to /dev/video-c920, and to stop the service on device disconnect:
/etc/udev/rules.d/90-video.rules:

Code: Select all

KERNEL=="video[0-9]*", ATTR{name}=="HD Pro Webcam C920", SYMLINK+="video-c920", TAG+="systemd", ENV{SYSTEMD_WANTS}"+="cvlc.service" ACTION=="remove", SUBSYSTEM=="video4linux", ENV{ID_VENDOR_ID}=="046d", ENV{ID_MODEL_ID}=="082d", RUN+="/bin/systemctl stop cvlc.service"

User avatar
nikolai.r
Posts: 317
Joined: Wed Nov 09, 2016 8:32 am
Topics answered: 2
Location: USA - California
Contact: Send private message

Re: Solution for video bandwidth problem using Octopi

Thu Feb 22, 2018 5:29 pm

@urho: Thanks for the good description! Can you see/measure how much bandwidth is required for your 1080p stream?

urho.t
Posts: 2
Joined: Sun Dec 18, 2016 7:03 pm
Contact: Send private message

Re: Solution for video bandwidth problem using Octopi

Thu Feb 22, 2018 5:55 pm

nikolai.r wrote:
Thu Feb 22, 2018 5:29 pm
@urho: Thanks for the good description! Can you see/measure how much bandwidth is required for your 1080p stream?
iftop reports about 3Mbps

Maybe that's not exactly low bandwith, but then again it's a smooth running 1080p stream, and low enough to stream over internet.

tobias.k3
Posts: 75
Joined: Fri Mar 24, 2017 1:34 pm
Location: Germany, Mainz
Contact: Send private message

Re: Solution for video bandwidth problem using Octopi

Sun Feb 25, 2018 12:19 pm

That looks good. I'll try it. Thanks!
----------------------------------------------
PRUSA MK2
- Noctua fan 40mm
- MK2/MK3 hybrid extruder

Return to “Software (Archive)”

Who is online

Users browsing this forum: No registered users and 2 guests