Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTSP Feed for printer cameras #1536

Open
greghesp opened this issue Mar 25, 2023 · 426 comments
Open

RTSP Feed for printer cameras #1536

greghesp opened this issue Mar 25, 2023 · 426 comments
Assignees
Labels
feature New feature or request Printer issues of Printer

Comments

@greghesp
Copy link

greghesp commented Mar 25, 2023

Is your feature request related to a problem? Please describe.
As a user, I cannot access the camera stream of my printer without using the Handy app or Bambu Studio

Describe the solution you'd like
As a user, I would like to be able to access my devices camera via a RTSP stream locally. This should be available whilst the device is in LAN or Cloud Mode, and should have the option to be secured using credentials, without the need for Bambu Studio camera tools to forward on the stream

Describe alternatives you've considered
There are no alternatives, due to the way this is currently implemented

Additional context
This was originally raised here
#1372, however the original poster decided to edit and close the issue for some reason. They then became toxic and it seems theirs GitHub account has been closed.

It had around 60 +1 comments and quite a lot of votes on the original post, so a much requested feature.

This feature is for the printer team, and is not a request for Bambu Studio.

As a developer of the Home Assistant integration, this is a much requested feature, but we are unable to implement this without a RTSP feed.

@greghesp greghesp added the feature New feature or request label Mar 25, 2023
@WolfwithSword
Copy link

+1 For an easily accessible RTSP camera stream feed, such that we can access it without needing bambu studio or the camera tools plugin running as a middleman. This way, I could open it in a browser or hook it up to several home automation platforms easily, or open it conveniently on my phone whether or not it is in LAN only mode.

@greghesp
Copy link
Author

Congrats. Know you copied the best and the feature's got my name on it.

I didn't copy the best, I took the drama out of it. End of. Stop being a child.

@AdrianGarside
Copy link

+1 for webcam style access direct from the printer.

@Ali229
Copy link

Ali229 commented Mar 25, 2023

+1 would love this feature as I now have multiple bambus connected to home assistant - being able to view the camera for each from one place and without internet would be useful

@KyleTeal
Copy link

+1 this would be a great feature

@jngmrks
Copy link

jngmrks commented Mar 25, 2023

+1 for a RTSP Stream direct from 3D-Printer

@RyanEwen
Copy link

+1 I think anyone coming from a printer using OctoPrint or Klipper expected RTSP out of the box

@trati629
Copy link

+1 for RTSP, would love to be able to see what my printer is doing from my TV and steam.

@Cobra-421
Copy link

+1 for RTSP direct from 3D-Printer

@andreifinski
Copy link

need it! hate to use wyze instead of existing camera

@FlyingHippo
Copy link

+1 for RTSP direct from 3D-Printer

@LayerCakeMakes
Copy link

+1 for a video stream locally from the 3d printer.

@uSyzex
Copy link

uSyzex commented Mar 26, 2023

+1 for LAN RTSP

@nejc-cc
Copy link

nejc-cc commented Mar 26, 2023

+1 for RTSP on LAN

@ice-brainvoid
Copy link

+1 for stream

@TriskelionTech
Copy link

+1 This would be awesome! would be great to have this linked to my home automation dashboard!

@davidt94
Copy link

+1 for this. Would be great

@sprior
Copy link

sprior commented Mar 26, 2023

Just a reminder that people who are interested in RTSP may also be interested in this one: #1504

@kazolar
Copy link

kazolar commented Mar 26, 2023

+1 would be awesome to add to BlueIris.

@Metallica22
Copy link

+1 for this.

@Turtletrumpet
Copy link

+1 for RTSP on LAN

@lorenzlars
Copy link

+1

2 similar comments
@dmcw999
Copy link

dmcw999 commented Mar 30, 2023

+1

@TheRealSimon42
Copy link

+1

@PatrickAmann
Copy link

+1 For an accessible RTSP camera stream on LAN.

@zVaz
Copy link

zVaz commented Nov 16, 2023

After a bit of research I found this: https://github.com/slynn1324/BambuP1Streamer
Was able to successfully add the stream to home assistant as a MJPEG IP Camera

Working for me too, thanks!

Screenshot_2023-11-16-06-19-43-23_c3a231c25ed346e59462e84656a70e50.jpg

@capsel22
Copy link

After a bit of research I found this: https://github.com/slynn1324/BambuP1Streamer
Was able to successfully add the stream to home assistant as a MJPEG IP Camera

Working for me too, thanks!

Screenshot_2023-11-16-06-19-43-23_c3a231c25ed346e59462e84656a70e50.jpg

Would someone be so kind and explain how to add this to HA from scratch? I've only ever added stuff from HACS, never anything manually, no idea where to begin with this.

@Faasteer
Copy link

Would someone be so kind and explain how to add this to HA from scratch? I've only ever added stuff from HACS, never anything manually, no idea where to begin with this.

That would be really great. I'm pretty good at implementing a lot of things. But unfortunately this is beyond what I can do without at least a basic guide. 😢

@zVaz
Copy link

zVaz commented Nov 16, 2023

After a bit of research I found this: https://github.com/slynn1324/BambuP1Streamer
Was able to successfully add the stream to home assistant as a MJPEG IP Camera

Working for me too, thanks!
Screenshot_2023-11-16-06-19-43-23_c3a231c25ed346e59462e84656a70e50.jpg

Would someone be so kind and explain how to add this to HA from scratch? I've only ever added stuff from HACS, never anything manually, no idea where to begin with this.

  1. Follow the steps in https://github.com/slynn1324/BambuP1Streamer build and run the docker on your HA host
  2. In HA add the MJPEG IP Camera integration (no need for HACS)
    • MJPEG URL: http://<HA host IP>:1984/api/stream.mjpeg?src=p1s

@Faasteer
Copy link

  1. Follow the steps in https://github.com/slynn1324/BambuP1Streamer build and run the docker on your HA host

  2. In HA add the MJPEG IP Camera integration (no need for HACS)

    • MJPEG URL: http://:1984/api/stream.mjpeg?src=p1s

I'm stuck on here where I continually get the error of not being able to find the path.
scripts/build_all.sh : build it all

Maybe someone can think of a better way, this is too complicated for me.

@zVaz
Copy link

zVaz commented Nov 16, 2023

  1. Follow the steps in https://github.com/slynn1324/BambuP1Streamer build and run the docker on your HA host

  2. In HA add the MJPEG IP Camera integration (no need for HACS)

    • MJPEG URL: http://:1984/api/stream.mjpeg?src=p1s

I'm stuck on here where I continually get the error of not being able to find the path.
scripts/build_all.sh : build it all

Maybe someone can think of a better way, this is too complicated for me.

There is an option to build it with docker

run --rm -v $(pwd):/work docker.io/gcc:12 gcc /work/src/BambuP1Streamer.cpp -o /work/BambuP1Streamer

@greghesp
Copy link
Author

I think there might be a bug somewhere with this. My IP address is now all of a sudden different to whats returned in the rtsp_url: #2915

@kurim
Copy link

kurim commented Nov 20, 2023

  1. Follow the steps in https://github.com/slynn1324/BambuP1Streamer build and run the docker on your HA host

  2. In HA add the MJPEG IP Camera integration (no need for HACS)

    • MJPEG URL: http://:1984/api/stream.mjpeg?src=p1s

I'm stuck on here where I continually get the error of not being able to find the path. scripts/build_all.sh : build it all

Maybe someone can think of a better way, this is too complicated for me.

Check:
https://github.com/greghesp/ha-bambulab/releases/tag/v1.9.0-dev1
Add new image sensor for A1/P1P/P1S camera feed

So there is no need for the other solution.

@AdrianGarside
Copy link

AdrianGarside commented Nov 20, 2023

Very nice! Just tested here this method.

#!/usr/bin/python3

Thanks for this (and especially @hisptoot for cracking the format). I was able to quickly use it to bootstrap a new image sensor in the above bambulab home assistant integration. A simple image sensor that updates every couple of seconds direct from the printer is by the far the best of all the solutions I've tried. Unlike all the camera-based approaches there's zero startup delay and no reliability issues working in the home assistant phone app.

@zVaz
Copy link

zVaz commented Nov 20, 2023

  1. Follow the steps in https://github.com/slynn1324/BambuP1Streamer build and run the docker on your HA host

  2. In HA add the MJPEG IP Camera integration (no need for HACS)

    • MJPEG URL: http://:1984/api/stream.mjpeg?src=p1s

I'm stuck on here where I continually get the error of not being able to find the path. scripts/build_all.sh : build it all

Maybe someone can think of a better way, this is too complicated for me.

Check:
https://github.com/greghesp/ha-bambulab/releases/tag/v1.9.0-dev1
Add new image sensor for A1/P1P/P1S camera feed

So there is no need for the other solution.

It is working thanks!

@kurim
Copy link

kurim commented Nov 20, 2023

Very nice! Just tested here this method.

#!/usr/bin/python3

Thanks for this (and especially @hisptoot for cracking the format). I was able to quickly use it to bootstrap a new image sensor in the above bambulab home assistant integration. A simple image sensor that updates every couple of seconds direct from the printer is by the far the best of all the solutions I've tried. Unlike all the camera-based approaches there's zero startup delay and no reliability issues working in the home assistant phone app.

You have both done awesome work.
Best case will be to make it also usable while cloud connect, or as Independent package, with local connect in use xtouch does not work any more.

@AdrianGarside
Copy link

AdrianGarside commented Nov 20, 2023

I started down that path but it made the initial configuration very confusing (needing to provide both bambu cloud password and the local IP/access code). And I had to try to update the stored data about the printer which I'm not sure HA lets me actually do (although requiring a delete/re-add of the integration might be acceptable in this case).

I dropped that for now - I'm crossing my fingers Bambu fixes the bug soon since they've acknowledged it as a bug recently.

@AdrianGarside
Copy link

@sodachen22 is there any update when the RTSP feed will be reliable? It works for short while after the printer is rebooted. But usually during the first print it stops working and then will remain broken until the printer is rebooted again.

@ondrovic
Copy link

+1 for this would be amazing to have it natively work in both handy / desktop apps without having to have home assistant running

@synman
Copy link

synman commented Jan 9, 2024

Very nice! Just tested here this method.

#!/usr/bin/python3

import struct
import sys
import os
import socket
import ssl

d = bytearray()

username = 'bblp'
access_code = 'xxx'
hostname = '192.xxx'
port = 6000

d += struct.pack("IIL", 0x40, 0x3000, 0x0)
for i in range(0, len(username)):
    d += struct.pack("<c", username[i].encode('ascii'))
for i in range(0, 32-len(username)):
    d += struct.pack("<x")
for i in range(0, len(access_code)):
    d += struct.pack("<c", access_code[i].encode('ascii'))
for i in range(0, 32-len(access_code)):
    d += struct.pack("<x")

ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

jpeg_start = "ff d8 ff e0"
jpeg_end = "ff d9"

read_chunk_size = 1024

with socket.create_connection((hostname, port)) as sock:
    with ctx.wrap_socket(sock, server_hostname=hostname) as ssock:
        ssock.write(d)
        buf = bytearray()
        start = False
        while 1:
            dr = ssock.recv(read_chunk_size);
            if not dr:
                break

            buf += dr

            if not start:
                i = buf.find(bytearray.fromhex(jpeg_start))
                if i >= 0:
                    start = True
                    buf = buf[i:]
                continue

            i = buf.find(bytearray.fromhex(jpeg_end))
            if i >= 0:
                img = buf[:i+len(jpeg_end)]
                buf = buf[i+len(jpeg_end):]
                start = False

                print(".", file=sys.stderr, end="", flush=True)

                os.write(1, b"--boundary\n")
                os.write(1, b"Content-Type: image/jpeg\n")
                os.write(1, b"Content-Length: %d\n\n" % len(img))
                os.write(1, img)
                os.write(1, b"\n");

and

% python3 p1p.py | mplayer -demuxer lavf -
MPlayer 1.5-15.0.0 (C) 2000-2022 MPlayer Team
Can't init Apple Remote.

Playing -.
Reading from stdin...
libavformat version 59.17.102 (internal)
.libavformat file format detected.
[lavf] stream 0: video (mjpeg), -vid 0
VIDEO:  [MJPG]  1280x720  0bpp  25.000 fps    0.0 kbps ( 0.0 kbyte/s)
[gl] using extended formats. Use -vo gl:nomanyfmts if playback fails.
==========================================================================
Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
libavcodec version 59.21.100 (internal)
[mjpeg @ 0x10a847a20]Requested frame threading with a custom get_buffer2() implementation which is not marked as thread safe. This is not supported anymore, make your callback thread-safe.
Selected video codec: [ffmjpeg] vfm: ffmpeg (FFmpeg MJPEG)
==========================================================================
Load subtitles in ./
Audio: no sound
Starting playback...
Movie-Aspect is 1.78:1 - prescaling to correct movie aspect.
VO: [gl_nosw] 1280x720 => 1280x720 Planar YV12
Movie-Aspect is 1.78:1 - prescaling to correct movie aspect.
VO: [gl_nosw] 1280x720 => 1280x720 Planar YV12
.V:   0.1   0/  0 ??% ??% ??,?% 0 0

it plays.

(not optimal because it searches whole buffer over and over for jpeg_end but that's just an example)

p1s-stream

wow, just wow... two days of banging my head and I finally stumbled upon this issue and y'all PoCs... 10 minutes later I've got my A1 camera exposed on my browser.

This python script is an awesome starter if you couple it with go2rtc.... remove the sysouts for the http mjpeg boundary headers (just dump the raw data) and configure go2rtc with:

streams:
     bambu_camera: "exec:/home/pi/bin/camera-stream.py"
Screenshot 2024-01-08 at 7 22 38 PM

@AdrianGarside
Copy link

AdrianGarside commented Jan 9, 2024

@synman see the ChamberImageThread() method here for more optimized processing which eliminates the search entirely:
https://github.com/greghesp/ha-bambulab/blob/develop/custom_components/bambu_lab/pybambu/bambu_client.py

It's on my list to see how I can convert the stream of jpegs into an mjpeg video stream so it can be exposed as a camera feed in home assistant. I hadn't got very far into investigating the plethora of tools that could be used but I'm pretty familiar with go2rtc as I'm using it to make the X1 stream reliable (by using it to proxy it so there's only a single connection to the printer that never disconnects).

@synman
Copy link

synman commented Jan 9, 2024

@AdrianGarside thanks for that link... ya know, I had looked at pybambu the other night and glossed over the webcam code. I'll see about adapting it for my specific use case.

@synman
Copy link

synman commented Jan 9, 2024

The pybambu snippet works wonderfully... Better than anything I've got out of handy and/or studio.

Thanks again!

@AdrianGarside
Copy link

Awesome. If you can expand on how you can convert the jpegs into an mjpeg with go2rtc that would be very helpful to me! :)

@synman
Copy link

synman commented Jan 9, 2024

@synman
Copy link

synman commented Jan 10, 2024

here's a pure python solution I pulled together today using a multi-threaded mjpeg web server I suped up a couple years ago and the protocol details provided in this thread.

https://github.com/synman/webcamd/tree/bambu

@0x5e
Copy link

0x5e commented Mar 4, 2024

golang version of @synman 's bambu-go2rtc if anyone need to run on device without python environment. https://gist.github.com/0x5e/dfb03f2ae7335bf03bb4b9cf59971597

does anyone know why a1 mini's camera stream is so slow, only 0.5~1 fps?

@THE-SIMPLE-MARK
Copy link

golang version of @synman 's bambu-go2rtc if anyone need to run on device without python environment. https://gist.github.com/0x5e/dfb03f2ae7335bf03bb4b9cf59971597

does anyone know why a1 mini's camera stream is so slow, only 0.5~1 fps?

It's how it is sadly. A1 and the P1 series' cameras are intentionally frame rate limited due to the processor (an ESP32) not being able to handle a high framerate camera alongside managing the core functions of the printer.

@Schokobecher
Copy link

@sodachen22 is there any update when the RTSP feed will be reliable? It works for short while after the printer is rebooted. But usually during the first print it stops working and then will remain broken until the printer is rebooted again.

Sadly, 6 months later, this is still the case. Had to ditch the camera in HA Automations as it frequently caused them to fail (I/O Error, Timeouts)

@WolfwithSword
Copy link

WolfwithSword commented Apr 29, 2024

Sadly, 6 months later, this is still the case. Had to ditch the camera in HA Automations as it frequently caused them to fail (I/O Error, Timeouts)

I did hear recently that the 1.8 firmware supposedly is going to bring reliability improvements to camera streams via live-view and wifi. Only a rumour though but wouldn't be surprised.

@sodachen22
Copy link

@sodachen22 is there any update when the RTSP feed will be reliable? It works for short while after the printer is rebooted. But usually during the first print it stops working and then will remain broken until the printer is rebooted again.

Sadly, 6 months later, this is still the case. Had to ditch the camera in HA Automations as it frequently caused them to fail (I/O Error, Timeouts)

RTSP stability issue may be related to the client, network performance and the server on the printer. To narrow down if this issue is related to the client, would you please check if it is OK with BambuStudio? If there issue is still there, please share the LOG of Studio and Printer with us. Thanks!

@Schokobecher
Copy link

Handy and Bambu Studio keep the connection, but querying the printer directly (LAN Live View turned on) results in no image.
The issue only arises after a while, and is only fixed by a printer restart - after that the connections works immediatley.

Please also see:
greghesp/ha-bambulab#221

Most common error for me is:

Error from stream worker: Error opening stream (ERRORTYPE_5, I/O error) rtsps://:@192.168.1.45/streaming/live/1

@AlexxIT
Copy link

AlexxIT commented May 4, 2024

@synman Hi. Looks like you using python to convert data. I can add this code inside go2rtc. So it will be less CPU cost and more native.

Just needs to access to stream. For example via Ngrok
https://github.com/AlexxIT/go2rtc/wiki/Tunnel-RTSP-camera-to-Intenet

You or anyone else can PM me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request Printer issues of Printer
Projects
None yet
Development

No branches or pull requests