AlexxIT / go2rtc

Ultimate camera streaming application with support RTSP, RTMP, HTTP-FLV, WebRTC, MSE, HLS, MP4, MJPEG, HomeKit, FFmpeg, etc.
https://github.com/AlexxIT/Blog
MIT License
5.16k stars 409 forks source link

Kasa KC200 no video using config.yaml and No Audio with workaround script. #1006

Closed mechanysm closed 6 months ago

mechanysm commented 7 months ago

Been trying to get this going for a couple of days, I can see the video stream in Fiddler but can't seem to get it working in go2rtc, i'm running a container with 1.8.5 so looks to be supported.

Getting this error: undefined error=streams: Get "https://myemail@gmail.com:***@192.168.1.19:19443/https/stream/mixed?video=H264&audio=G711": net/http: HTTP/1.x transport connection broken: bad Content-Length "-1" caller=github.com/AlexxIT/go2rtc/internal/mjpeg/init.go:166

my config is basic and as follows

streams:
  kasa: kasa://myemail@gmail.com:password@192.168.1.19:19443/https/stream/mixed?video=H264&audio=G711

reponse header contains the following

Content-Type: multipart/x-mixed-replace;boundary=data-boundary--

This seems to correspond to the body which contains --data-boundary-- prior to the av content.

Any help would be much appreciated as feel like I'm so close

mechanysm commented 7 months ago

Thought I would add some extra information, hopefully its helpful, this is scrapped from fiddler.

Request Header

GET https://192.168.1.19:19443/https/stream/mixed?video=H264&audio=G711 HTTP/1.1
Authorization: Basic <Base64 Token>
Connection: keep-alive
User-Agent: Dalvik/2.1.0 (Linux; U; Android 14; SM-S901E Build/UP1A.231005.007)
Host: 192.168.1.19:19443
Accept-Encoding: gzip

Response Header + data boundary

HTTP/1.0 200 OK
Server: Streamd,
Date: Tue, 19 Mar 2024 18:19:18 UTC
Content-Type: multipart/x-mixed-replace;boundary=data-boundary--
Pragma: no-cache
Cache-Control: no-cache
Connection: keep-alive
Content-Encoding: gzip

‹       A@¾¿--data-boundary--
Content-Type: video/x-h264
Content-Length: 140257
X-UtcTime:1710872358
X-Timestamp: 7420219.405023
X-Audio: 1
X-FrameType: 0
X-FrameRate: 12.5
X-Video-Detection: 0

   gM *¸ ‰ùfà   @   hî<€   åK€   e¸  BOìR1‚&Ÿ&ê0áM× }.9®Äm¾ñÞ¬¡ÎÒáÇ囋o?¹uU"$Nс‚¬ÿõ'¾÷}—ûRdýZžË%=À=„²rù5ÚÀ'ö/ÕâüÈU|ÛÿFß
mechanysm commented 7 months ago

Was able to successfully get a video output with curl, still no luck with go2rtc :(

curl -k -u 'myemail@gmail.com:<password>' --ignore-content-length "https://192.168.1.19:19443/https/stream/mixed?video=H264&audio=G711" --output - | ffmpeg -hide_banner -y -i - -vcodec copy ./ffmpeg/test.mp4

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  929k    0  929k    0     0  81562      0 --:--:--  0:00:11 --:--:-- 88529Input #0, h264, from 'pipe:':
  Duration: N/A, bitrate: N/A
  Stream #0:0: Video: h264 (Main), yuvj420p(pc, bt709, progressive), 1920x1080, 25 fps, 25 tbr, 1200k tbn, 50 tbc
Output #0, mp4, to './ffmpeg/test.mp4':
  Metadata:
    encoder         : Lavf58.76.100
  Stream #0:0: Video: h264 (Main) (avc1 / 0x31637661), yuvj420p(pc, bt709, progressive), 1920x1080, q=2-31, 25 fps, 25 tbr, 1200k tbn, 1200k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
[mp4 @ 0x55891c575480] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly
100 1012k    0 1012k    0     0  81737      0 --:--:--  0:00:12 --:--:-- 89509frame=  142 fps=135 q=-1.0 size=     768kB tim100 1086k    0 1086k    0     0  81395      0 --:--:--  0:00:13 --:--:-- 88729frame=  156 fps= 72 q=-1.0 size=    1024kB tim100 1167k    0 1167k    0     0  81362      0 --:--:--  0:00:14 --:--:-- 89980frame=  170 fps= 51 q=-1.0 size=    1024kB tim

Hopefully this is helpful in some way :)

mechanysm commented 7 months ago

Have created a ngrok tunnel for the camera if that helps.

mechanysm commented 7 months ago

Well I bodged my way around some of my issues, I now have some video, not perfect so would still be keen to have this issue resolved if that's possible?

It feels like the current go2rtc implementation is very close but I get the feeling the issue is with the --data-boundary-- and the weird issues with audio being mixed into the same stream as its very intermittent, I assume its only sending audio through when there is something over a certain db level.

I tried to create some rather elaborate bash scripts to split out the audio and video into separate FIFO's but I think along with the overhead its just to slow and ffmpeg didn't seem to like it when I was trying to recombine it and the FIFO's were flaky at best.

I have settled on the following script which loads the video relatively quickly compared to previous attempts, I have the codec for audio in it but it doesn't work :(

#!/bin/bash

# make fifo if needed.
[ -p stream.fifo ] || mkfifo stream.fifo

# Pipe video and audio to fifo.
curl -s -k -u 'myemail@gmail.com:<encoded password>' --ignore-content-length "https://192.168.1.19:19443/https/stream/mixed?video=H264&audio=G711" --output stream.fifo &

# ffmpeg convert to something les broken.
ffmpeg -hide_banner -i stream.fifo -c:v copy -c:a pcm_mulaw -f flv - &

# wait for ffmpeg to end before closing.
wait

#done
exit

Using the docker version of go2rtc so created a script folder in config and mapped the path across, dumped the script in there and ran chmod +x ./scriptname.sh on it to give it execute.

Audio data boundary below, there are some odd things going on, its almost like it finishes the audio then starts the video again without a new --data-boundary-- though this may be my noob knowledge of audio\video streams.

--data-boundary--
Content-Length: 480
X-Timestamp: 7420238.689837
Content-Type: audio/g711u

Some additional information I found in my travels

charlieamer commented 7 months ago

I believe go2rtc doesn't perfectly handle streams for kasa. There is homeassistant addon which I believe handles it much better, but it has its own flaws. https://github.com/joshgetter/hassio-addons/tree/master/kasa-camera . This one is splitting and recombining streams properly though. I am considering to sell the KC200 camera and buy a different one with better support, one that's less propriatory 😅

mechanysm commented 7 months ago

Thanks @charlieamer its funny you say that as I have just ordered two new (less known brand) onvif based cameras, hopefully I dont regret this purchase, guess I will find out soon, I don't have a lot of money though so my options were slim.

charlieamer commented 6 months ago

Thanks @charlieamer its funny you say that as I have just ordered two new (less known brand) onvif based cameras, hopefully I dont regret this purchase, guess I will find out soon, I don't have a lot of money though so my options were slim.

I found a way to connect go2rtc/frigate with kasa! It requires a custom python script. I'll send details soon. I am certain the solution could be integrated in golang as well, as part of native go2rtc ecosystem.

AlexxIT commented 6 months ago

You can send the accesses to your camera to me in PM.

saintyuk commented 6 months ago

Any updates? I can potentially help if useful :)

AlexxIT commented 6 months ago

I'm waiting PMs

saintyuk commented 6 months ago

I've added you as a friend on Discord. I have a spare KC200 we can play about with 👍

Edit: KC420WS works perfectly with go2rtc including audio!

AlexxIT commented 6 months ago

This is duplicate https://github.com/AlexxIT/go2rtc/issues/725 Fixed in latest master version