trevos3d / K1-USB-Cam

Add USB Webcam to Creality K1 Printer
47 stars 5 forks source link

only one camera #7

Open bmharon opened 10 months ago

bmharon commented 10 months ago

Hi. K1, rooted, latest fw. Whatever i do i can only see usb or k1 camera, not both streams together. Tried to uninstall and to install multiple times. install only for usb cam, and install both cams. Unplugged and plugged while installing. But always se one stream, and nothing on second. Every install cameras switch. Usb is working, and k1 not. After another install, K1 is working usb not. And in loop now. What to do? Screenshot 2024-01-10 at 15 20 07 Screenshot 2024-01-10 at 15 20 41

ahappysloth commented 9 months ago

Getting the same issue on my end as well. I tried editing the scripts, with no luck.

trevos3d commented 9 months ago

Hello guys, can you please provide what camera (with model) are you trying to use?

dbg17891 commented 9 months ago

Hello guys, can you please provide what camera (with model) are you trying to use?

I have a Logitech webcam and I'm only getting one output after running the script. I will either get the Logitech feed or i will get the k1 cam, but not both at the same time.

slakouz commented 6 months ago

I using two cameras on K1 (embeded and Nebula). Both working fine when I changed video5 to video6 in script and rebooted K1 via power switch.

stevenmcastano commented 2 months ago

Workaround found!

For the normals/non-maniacs reading this... before you spend hours digging through my ramblings, the TL;DR here is to just add a delay into the startup script so everything.

Here's exactly how to do it: In the startup script (https://github.com/trevos3d/K1-USB-Cam/blob/main/script/dual_cam.sh), which ends up installed in /usr/data/startup_usb_camera.sh, on line 2, add this: sleep 2m That's literally it. The resulting for lines of the file are as follows:

#!/bin/sh
sleep 2m

kill -9 $(pidof mjpg_streamer)
kill -9 $(fuser /dev/video4)
kill -9 $(fuser /dev/video5)
kill -9 $(fuser /dev/video6)

mjpg_streamer -b -i "/usr/lib/mjpg-streamer/input_uvc.so -d /dev/video4" -i "/usr/lib/mjpg-streamer/input_uvc.so -d /dev/video5" -o "/usr/lib/mjpg-streamer/output_http.so -p 8080"

Bonus: If you're annoyed the the built in camera isn't the "first" camera anymore like I was, flip the device numbers on the last line... It would go from:

mjpg_streamer -b -i "/usr/lib/mjpg-streamer/input_uvc.so -d /dev/video4" -i "/usr/lib/mjpg-streamer/input_uvc.so -d /dev/video5" -o "/usr/lib/mjpg-streamer/output_http.so -p 8080"

To:

mjpg_streamer -b -i "/usr/lib/mjpg-streamer/input_uvc.so -d /dev/video5" -i "/usr/lib/mjpg-streamer/input_uvc.so -d /dev/video4" -o "/usr/lib/mjpg-streamer/output_http.so -p 8080"

Big thanks to the dev @trevos3d, your scripts got me going on this one and I'm thrilled to finally have a second camera on my Creality K1 Max so now I can get a top down few and have a better idea of what the F is going on in my printer when I'm not near it. Now that I have way too deep of an understanding of this thing and cameras, I might have to try adding another 1 or two since I have a bunch laying around!

Diags, attempts, ramblings over and over-caffeinated manic:

I'm was the same issue... with the usb camera plugged in, that starts working, but it kills the builtin camera. In addition, the
v4l2-ctl --list-devices command now gives a segmentation fault:

~ # v4l2-ctl --list-devices
Segmentation fault

It was working previous to the reboot and my second camera was detected on /dev/video5... at least I think that's the new one... when I ran it before doing anything with nothing plugged into the USB port, I got:

~ # v4l2-ctl --list-devices
jz-rot ():
    /dev/video0

CCX2F3299 (1.3):
    /dev/media0

Dummy video device (0x0000) (platform:v4l2loopback-000):
    /dev/video3

CCX2F3299 (usb-13500000.otg_new-1.3):
    /dev/video4

vpu-felix (vpu-felix):
    /dev/video2

vpu-helix (vpu-helix):
    /dev/video1

Which seems to show that the original camera is plugged into /dev/video4, then after plugging in the camera I didn't see anything different. I installed everything and got:

~/K1-USB-Cam # v4l2-ctl --list-devices
jz-rot ():
    /dev/video0

UVC Camera (046d:09a1) (1.2):
    /dev/media1

CCX2F3299 (1.3):
    /dev/media0

Dummy video device (0x0000) (platform:v4l2loopback-000):
    /dev/video3

UVC Camera (046d:09a1) (usb-13500000.otg_new-1.2):
    /dev/video5

CCX2F3299 (usb-13500000.otg_new-1.3):
    /dev/video4

vpu-felix (vpu-felix):
    /dev/video2

vpu-helix (vpu-helix):
    /dev/video1

So it would seem my USB camera is on /dev/video5 now. However with everything installed and the camera added in the fluidd interface. I'm only seeing the USB camera. I tried the folowing process:

From here things get a bit fuzzy... I attempted to take apart the scripts and see how they tick... and I found the lines that start the mjpg-streamer process... but somewhere along the way, my camera jumped from /dev/video5 to /dev/video6, but if I run the line that started the mjpg-streamer manually (mjpg_streamer -b -i "/usr/lib/mjpg-streamer/input_uvc.so -d /dev/video4" -i "/usr/lib/mjpg-streamer/input_uvc.so -d /dev/video5" -o "/usr/lib/mjpg-streamer/output_http.so -p 8080") it does start working again. But after reboot, I'm back in the same spot where I can't list devices anymore, but in /dev/v4l/by-id/ I do see both cameras. Oddly I'm stuck to video6 again:

~ # ls -laph /dev/v4l/by-id/
total 0
drwxr-xr-x    2 root     root          80 Mar  4  2020 ./
drwxr-xr-x    4 root     root          80 Mar  4  2020 ../
lrwxrwxrwx    1 root     root          12 Mar  4  2020 main-video-4 -> ../../video4
lrwxrwxrwx    1 root     root          12 Mar  4  2020 main-video-6 -> ../../video6

However the running mjpg-streamer process is on the wrong device again:

mjpg_streamer -b -i /usr/lib/mjpg-streamer/input_uvc.so -d /dev/video4 -i /usr/lib/mjpg-streamer/input_uvc.so -d /dev/video5 -o /usr/lib/mjpg-streamer/output_http.so -p 8080

So if I kill that by it's PID number, and rerun it with video6 in there it does seem to work... so following that test, I edited the start script at /usr/data/startup_usb_camera.sh and changed video5 to video6 and rebooted... then, OF COURSE, my USB camera seems to have switched back to video5 again. Put the script back to original and rebooted YET again... this time, it seem like klipper took a while to start again... and again we're back to only the USB camera working, not the K1 camera. Listing devices seg faults again... so it looks like something in the startup script is causing the problem!

With this new info, I moved the S99usb_camera script out of `/etc/init.d' over to my home directory. Now when I reboot the original camera works, the USB camera obviously does not, and the mjpg-stream process is running as expected again.

Basically did a bunch more testing... and it seems like no matter what I do... if I have the S99_usb_camera script in /etc/init.d it doesn't work.

So that is all the diagnostic data I can think of that might be useful for changing the code to get it working...

THAT BEING SAID... I got it working on my Creality K1 Max on the latest firmware availible on today's date, August 17th, 2024 which is rooted and has Moonraker, Fluidd and a few other things installed. What it seems to come down to is a delay... I started a significant rewrite of these scripts and in the process figured out that if the service starts right away, before other things are running... maybe klipper itself... that's when it fails. So, in the end the only meaning change I made in addition to adding a lot more logging, diagnostic options, etc... is to add a 2 minute delay in the startup script to everything else is up and running before it kills the camera processes and reruns mjpg_streamer!


@bmharon, @ahappysloth, @dbg17891, give my solution at the top of this comment a try... its fully working across reboots and everything now.

@slakouz, did your second camera just work right away with the video5/6 switch? Or did you have to do anything else?

dbg17891 commented 2 months ago

@stevenmcastano That didn't work for me I'm using the k1 with moonraker installed. Screenshot 2024-08-18 192951 Screenshot 2024-08-18 191618

trevos3d commented 1 week ago

I'm trying to replicate this scenario here, but even making a factory reset and a fresh install of the script, both webcams are working.

@stevenmcastano I added your suggestion on main branch to be easy for others to test.