Experimental HLS support

Hi!
I have added experimental HLS support both to OctoPi (see devel branch) and OctoPrint.


I think it's impossible to add it as a plugin, so it's will be a pull request to the main source.

@fossel, please, tell if you think this way is a the proper way of adding HLS support.

This is awesome @Chudsaviet/ I know many people have been wanting to integrate h264 video streaming. This will go a long way I think. I'm going to check out your other posts, but in case it's not listed elsewhere, what are you using on the server backend to handle the stream?

Oh, and by the way I think I can integrate this into a plugin if you want, in case it's not approved via PR to OctoPrint.

I cannot confirm if this is working for HLS, but it definitely does not break the mjpgstreamer stream in my testing. Hopefully this is helpful in some way. Feel free to fork, edit, etc. Again great addition for the community I think.

@jneilliii, that’s just WOW. It was fast :slight_smile:
I’m using ffmpeg as HLS streamer, and nginx for serving the content.
However, HLS have a caveat by design - lag. I was able to bring it down to 3-5 sec.

See the commit to OctoPi - https://github.com/guysoft/OctoPi/commit/35f334033524c763cc54293e103315b8900740fb
For the command line, see the ‘ffmpeg_hls.service’ file

You had all the logic done already, so making it a plugin was fairly easy. I just had to tweak some of the id references in javascript and strip out the css for the control tab into it's own css file. Bundle it all together and bam plugin enabled. The trick with this one is the get_template_configs and using the replaces and name option to make it look like the original even though it's not. I really hope it helps. Feel free to remove my name from the author and replace the email address, I was just throwing those in the cookiecutter template when I created the plugin framework. Like I said you did all the programming, I just packaged it for you.

Quick question, how is CPU/Network load with the HLS support versus mjpegstreamer?

These questions are hard to answer. :slight_smile:
But generally:

  1. I’m using hardware h264, but CPU load is still significant. Like 20% of each core on Rpi 4. It’s doing JPEG encoding for the timelapses, and moving frames back and forth, filesystem work, etc... We are able to pull JPEG frames directly from camera, but I wanted to avoid double compression, so I pull raw video. I set ‘nice’ to ffmpeg, so it shouldn’t affect printing.
  2. Network bandwidth depends on how much h264 bitrate you set. I set 2048 kbit/s for 800x600, but we can optimize it further. I haven’t measured MJPEG, but it’s like 4 times more bandwidth I think.

So I'm struggling a little with the setup on my test pi without the octopi image. Maybe I should look for a nightly build....these are the commands I am using to try to manually add this to my already installed octopi and am getting errors randomly.

sudo su
apt-get update
apt-get install -y --force-yes --no-install-recommends ffmpeg nginx
mkdir -p /etc/octopi_streamer/hls
wget https://raw.githubusercontent.com/guysoft/OctoPi/35f334033524c763cc54293e103315b8900740fb/src/modules/octopi/filesystem/root/etc/systemd/system/ffmpeg_hls.service
cp ./ffmpeg_hls.service /etc/systemd/system/ffmpeg_hls.service
wget https://raw.githubusercontent.com/guysoft/OctoPi/35f334033524c763cc54293e103315b8900740fb/src/modules/octopi/filesystem/root/etc/nginx/sites-available/default
cp ./default /etc/nginx/sites-available/default
wget https://raw.githubusercontent.com/guysoft/OctoPi/35f334033524c763cc54293e103315b8900740fb/src/modules/octopi/filesystem/root/lib/systemd/system/nginx.service
cp ./nginx.service /lib/systemd/system/nginx.service
usermod -aG video pi
systemctl disable webcamd
systemctl enable nginx
systemctl enable ffmpeg_hls
service nginx start
service ffmpeg_hls start
exit

the error I see is when starting the ffmpeg_hls service...

Job for ffmpeg_hls.service failed because the control process exited with error code.

systemctl status ffmpeg_hls.service

● ffmpeg_hls.service - FFMPEG HLS webcam streaming service
   Loaded: loaded (/etc/systemd/system/ffmpeg_hls.service; enabled; vendor prese
   Active: activating (auto-restart) (Result: exit-code) since Fri 2020-05-29 05
  Process: 2717 ExecStartPre=/bin/rm -rf /tmp/webcam (code=exited, status=217/US

journalctl -xe

-- Defined-By: systemd
-- Support: https://www.debian.org/support
--
-- An ExecStartPre= process belonging to unit ffmpeg_hls.service has exited.
--
-- The process' exit code is 'exited' and its exit status is 217.
May 29 05:41:42 octopi systemd[1]: ffmpeg_hls.service: Failed with result 'exit-
-- Subject: Unit failed
-- Defined-By: systemd
-- Support: https://www.debian.org/support
--
-- The unit ffmpeg_hls.service has entered the 'failed' state with result 'exit-
May 29 05:41:42 octopi systemd[1]: Failed to start FFMPEG HLS webcam streaming s
-- Subject: A start job for unit ffmpeg_hls.service has failed
-- Defined-By: systemd
-- Support: https://www.debian.org/support
--
-- A start job for unit ffmpeg_hls.service has finished with a failure.
--
-- The job identifier is 2278 and the job result is failed.

any help is appreciated.

testing a nightly build now, hopefully my pi zero w can handle it, lol. Only spare pi I have right now.

looks like the nightly build is working, at least the status is showing good now. what is the URLs I need to use for the webcam settings?

Pi Zero W can handle it. I run a fish camera on one :slight_smile: However, I don't know if it will print with no issues due to high load.
Is it the latest clean OctoPi build?

There is next section in ffmpeg_hls.service file:

[Service]
User=webcam
Restart=always
RestartSec=5
Nice=10
ExecStartPre=/bin/rm -rf /tmp/webcam
ExecStartPre=/bin/mkdir -p /tmp/webcam/hls
ExecStartPre=/bin/mkdir -p /tmp/webcam/jpeg

So please, try to run all ExecStartPre commands in order under webcam user.

It's interesting behavior because I have tested OctoPi build before the commit.

In VLC or Safari, or the experimental OctoPrint, use http://[your octopi]:8051/hls/stream.m3u8.
You can also create a demo page for other browsers as described in HLS.js docs: https://github.com/video-dev/hls.js/

UPD: Oh, you can have issues with CORS in Firefox and Chrome. We might need to update HAProxy config to proxy the webcam.

So it was the latest nightly, here.

https://gnethomelinux.com/OctoPi/nightly/

once extracted listed as 2020-02-13-octopi-buster-lite-0.18.0.img and shows as running octopi 0.18 in OctoPrint.

pi@octopi:~ $ sudo service ffmpeg_hls status
● ffmpeg_hls.service - FFMPEG HLS webcam streaming service
   Loaded: loaded (/etc/systemd/system/ffmpeg_hls.service; enabled; vendor prese
   Active: active (running) since Thu 2020-02-13 16:10:29 GMT; 3 months 14 days
 Main PID: 313 (ffmpeg)
   Memory: 33.5M
   CGroup: /system.slice/ffmpeg_hls.service
           └─313 /usr/bin/ffmpeg -framerate 30 -video_size 800x600 -i /dev/video

May 29 06:30:03 octopi ffmpeg[313]: [174B blob data]
May 29 06:30:03 octopi ffmpeg[313]: [hls @ 0x14f6d00] Opening '/tmp/webcam/hls/s
May 29 06:30:03 octopi ffmpeg[313]: [hls @ 0x14f6d00] Opening '/tmp/webcam/hls/s
May 29 06:30:16 octopi ffmpeg[313]: [174B blob data]
May 29 06:30:16 octopi ffmpeg[313]: [hls @ 0x14f6d00] Opening '/tmp/webcam/hls/s
May 29 06:30:16 octopi ffmpeg[313]: [hls @ 0x14f6d00] Opening '/tmp/webcam/hls/s
May 29 06:30:17 octopi ffmpeg[313]: [hls @ 0x14f6d00] Opening '/tmp/webcam/hls/s
May 29 06:30:29 octopi ffmpeg[313]: [174B blob data]
May 29 06:30:29 octopi ffmpeg[313]: [hls @ 0x14f6d00] Opening '/tmp/webcam/hls/s
May 29 06:30:30 octopi ffmpeg[313]: [hls @ 0x14f6d00] Opening '/tmp/webcam/hls/s

Those directories in ExecStartPre are present and I see m4a files in the hls folder.

what are URLs to use for the webcam/timelapse settings?

thanks crossed over your comment.

That would actually make sense to do, to support https too?

I had an idea of restricting Nginx to run only on local interface and proxying it to HAProxy. Just haven't enough time. So yes, HAProxy in this case will do the HTTPS work too.

And also, did you take into account the test button in the webcam settings for your changes to OctoPrint? That seems to be broken. So I am able to load up the plugin I made, and tried in VLC and it seems my picam connection got messed up or it's not quite working for me.

No, no test button work done. It's a separate page if I'm correct. I just made an MVP to present the idea to the community. If it will be accepted - we will do a proper merge request together :slight_smile:

Which browser is it? Looks like CORS issue. Try using a plugin that can disable CORS.
Radical solution for this problem is to make HAProxy proxy HLS.