Solution for video bandwidth problem using Octopi
 
Notifications
Clear all

[Closed] Solution for video bandwidth problem using Octopi  

  RSS
tobias.k3
(@tobias-k3)
Trusted Member
Solution for video bandwidth problem using Octopi

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

Posted : 19/02/2018 3:02 pm
Nikolai
(@nikolai)
Noble Member
Re: Solution for video bandwidth problem using Octopi

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.

Often linked posts:
Going small with MMU2
Real Multi Material
My prints on Instagram

Posted : 19/02/2018 11:50 pm
tobias.k3
(@tobias-k3)
Trusted Member
Topic starter answered:
Re: Solution for video bandwidth problem using Octopi

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

Posted : 20/02/2018 11:04 am
Nikolai
(@nikolai)
Noble Member
Re: Solution for video bandwidth problem using Octopi

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.

Often linked posts:
Going small with MMU2
Real Multi Material
My prints on Instagram

Posted : 20/02/2018 6:46 pm
tobias.k3
(@tobias-k3)
Trusted Member
Topic starter answered:
Re: Solution for video bandwidth problem using Octopi

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

Posted : 20/02/2018 9:42 pm
Nikolai
(@nikolai)
Noble Member
Re: Solution for video bandwidth problem using Octopi

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/Logitech_C920,_streaming_H.264
It will not work in the browser, but you would have your h264 stream 😉

Often linked posts:
Going small with MMU2
Real Multi Material
My prints on Instagram

Posted : 21/02/2018 6:28 pm
desaster
(@desaster)
New Member
Re: Solution for video bandwidth problem using Octopi

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:
    [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:

    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"

    Posted : 22/02/2018 3:08 pm
    Nikolai
    (@nikolai)
    Noble Member
    Re: Solution for video bandwidth problem using Octopi

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

    Often linked posts:
    Going small with MMU2
    Real Multi Material
    My prints on Instagram

    Posted : 22/02/2018 6:29 pm
    desaster
    (@desaster)
    New Member
    Re: Solution for video bandwidth problem using Octopi


    @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.

    Posted : 22/02/2018 6:55 pm
    tobias.k3
    (@tobias-k3)
    Trusted Member
    Topic starter answered:
    Re: Solution for video bandwidth problem using Octopi

    That looks good. I'll try it. Thanks!

    ----------------------------------------------
    PRUSA MK2
    - Noctua fan 40mm
    - MK2/MK3 hybrid extruder

    Posted : 25/02/2018 1:19 pm
    TallDonkey
    (@talldonkey)
    Active Member
    Re: Solution for video bandwidth problem using Octopi

    Thank you urho!

    Can you go into a bit more beginner mode on how to get this working? I know how to ssh into the Pi, cd, rm, cp, but that's about it.

    Questions in particular

    1) How do we register the script with systemd? Do we just create the file and copy and paste your snippet (adjusting for dev/video0 if necessary)

    /etc/systemd/system/cvlc.service:

    EDIT - Googling for a while said yes, just create the file and place it in the directory, and then do this
    sudo systemctl enable cvlc.service
    and reboot the pi

    2) How do we know if we need to change the user from "octoprint" and use as is? does it map to an actual login user? ie, pi

    EDIT - I set my user to pi to be safe

    Thanks in advance!

    Also, for others in this thread, I discovered that


    v4l2-ctl --list-devices

    will return and confirm what your device is and where it is mapped.


    Logitech Webcam C930e (usb-3f980000.usb-1.3):
    /dev/video0

    And here's my
    /etc/systemd/system/cvlc.service script


    [Unit]
    Description=cvlc video stream
    After=network.target

    [Service]
    Type=simple
    SyslogIdentifier=cvlc

    ExecStartPre=/usr/bin/v4l2-ctl -d /dev/video0 -c power_line_frequency=1
    #ExecStartPre=/usr/bin/v4l2-ctl -d /dev/video0 -c focus_auto=1
    ExecStartPre=/usr/bin/v4l2-ctl -d /dev/video0 -c focus_auto=0
    ExecStartPre=/usr/bin/v4l2-ctl -d /dev/video0 -c focus_absolute=35

    ExecStart=/usr/bin/cvlc v4l2:///dev/video0: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=pi

    [Install]
    WantedBy=multi-user.target

    Posted : 31/08/2018 7:51 am
    Share: