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

Fix panic for broken RTP with AAC #697

Closed
felipecrs opened this issue Oct 15, 2023 · 9 comments
Closed

Fix panic for broken RTP with AAC #697

felipecrs opened this issue Oct 15, 2023 · 9 comments
Labels
bug Something isn't working panic
Milestone

Comments

@felipecrs
Copy link
Contributor

felipecrs commented Oct 15, 2023

I realized that one of my Tuya cameras simply cannot be streamed through RTSP, but they can through HLS (m3u8).

I built an echo script to help me get it:

#!/bin/bash

# /config/scripts/get_tuya_stream.sh

set -euo pipefail

shopt -s inherit_errexit

readonly device_id="${1}"

readonly client_id="${2}"
readonly client_secret="${3}"
readonly tuya_base_url="${4}"

readonly encoded_empty_body="e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

t=$(date +%s%N | sed "s/......$//g")
readonly t

path="/v1.0/token?grant_type=1"
sign=$(
  echo -en "${client_id}${t}GET\n${encoded_empty_body}\n\n${path}" |
    openssl dgst -sha256 -hmac "${client_secret}" |
    tr '[:lower:]' '[:upper:]' |
    sed "s/.* //g"
)

access_token=$(
  curl -fsSL --request GET "${tuya_base_url}${path}" \
    --header "sign_method: HMAC-SHA256" \
    --header "client_id: ${client_id}" \
    --header "t: ${t}" \
    --header "mode: cors" \
    --header "Content-Type: application/json" \
    --header "sign: ${sign}" \
    --header "access_token: " |
    sed "s/.*\"access_token\":\"//g" | sed "s/\".*//g"
)

path="/v1.0/devices/${device_id}/stream/actions/allocate"
body=$(
  jq -c <<EOF
{
  "type": "HLS"
}
EOF
)
encoded_body=$(echo -n "${body}" | openssl dgst -sha256 | sed "s/.*[ ]//g")
method="POST"
sign=$(
  echo -en "${client_id}${access_token}${t}${method}\n${encoded_body}\n\n${path}" |
    openssl dgst -sha256 -hmac "${client_secret}" |
    tr '[:lower:]' '[:upper:]' |
    sed "s/.* //g"
)

url=$(
  curl -fsSL --request "${method}" "${tuya_base_url}${path}" \
    --header "sign_method: HMAC-SHA256" \
    --header "client_id: ${client_id}" \
    --header "t: ${t}" \
    --header "mode: cors" \
    --header "Content-Type: application/json" \
    --header "sign: ${sign}" \
    --header "access_token: ${access_token}" \
    --data "${body}" |
    jq -er .result.url
)

echo -n "${url}"

And this outputs something like:

https://aws-tractor2.tuyaus.com:8033/hls/ea47e8aa1a4a468aaa74792ca112f1b99cf84809a528fe44.m3u8

So, I integrated in go2rtc this way:

go2rtc:
  stream:
    tuya_hls_cam:
      - echo:/config/scripts/get_tuya_stream.sh <device id> <client id> <access secret> https://openapi.tuyaus.com

But, when I start the stream through go2rtc, it does not survive long enough to be able to view it.

However, in VLC it works fine.

Here is what I see in go2rtc logs:

2023-10-15 21:02:30.784950313  [INFO] Preparing go2rtc config...
2023-10-15 21:02:30.835215852  [INFO] Got IP address from supervisor: 192.168.1.10
2023-10-15 21:02:30.867775636  [INFO] Got WebRTC port from supervisor: 8555
2023-10-15 21:02:31.299206824  [WARN] Using go2rtc binary from '/config/go2rtc' instead of the embedded one
2023-10-15 21:02:31.299211093  [INFO] Starting go2rtc...
2023-10-15 21:02:31.375034717  18:02:31.372 INF go2rtc version 1.8.1 linux/amd64
2023-10-15 21:02:31.375038440  18:02:31.372 INF [api] listen addr=0.0.0.0:1984
2023-10-15 21:02:31.375039635  18:02:31.373 INF [rtsp] listen addr=0.0.0.0:8554
2023-10-15 21:02:31.375043955  18:02:31.373 INF [webrtc] listen addr=0.0.0.0:8555/tcp
2023-10-15 21:02:40.782784790  [INFO] Starting go2rtc healthcheck service...
2023-10-15 21:06:43.959376346  18:06:43.959 WRN [hls] can't get segment id=3DBak6Ck&n=3
2023-10-15 21:06:59.513105775  18:06:59.513 WRN [hls] can't get segment id=v8xnS9HG&n=3
2023-10-15 21:11:19.005465940  [INFO] The go2rtc service exited with code 256 (by signal 9)
2023-10-15 21:11:20.035853339  [WARN] Using go2rtc binary from '/config/go2rtc' instead of the embedded one
2023-10-15 21:11:20.035863164  [INFO] Starting go2rtc...
2023-10-15 21:11:20.110817290  18:11:20.109 INF go2rtc version 1.8.1 linux/amd64
2023-10-15 21:11:20.111133252  18:11:20.111 INF [api] listen addr=0.0.0.0:1984
2023-10-15 21:11:20.111669156  18:11:20.111 INF [rtsp] listen addr=0.0.0.0:8554
2023-10-15 21:11:20.111891316  18:11:20.111 INF [webrtc] listen addr=0.0.0.0:8555/tcp
2023-10-15 21:11:59.091371158  18:11:59.091 WRN github.com/AlexxIT/go2rtc/internal/streams/producer.go:171 > error=EOF url="echo:/config/scripts/get_tuya_stream.sh ebf031220b3de54904xgqs"
2023-10-15 21:12:10.946347443  18:12:10.946 WRN github.com/AlexxIT/go2rtc/internal/streams/producer.go:171 > error=EOF url="echo:/config/scripts/get_tuya_stream.sh <hidden>"
2023-10-15 21:12:35.354313149  panic: runtime error: slice bounds out of range [:5109] with capacity 872
2023-10-15 21:12:35.354321612  
2023-10-15 21:12:35.354325202  goroutine 610 [running]:
2023-10-15 21:12:35.354545601  github.com/AlexxIT/go2rtc/pkg/mp4.(*Consumer).AddTrack.RTPDepay.func5(0xc00183def0)
2023-10-15 21:12:35.354551231  	github.com/AlexxIT/go2rtc/pkg/aac/rtp.go:34 +0x325
2023-10-15 21:12:35.354555606  github.com/AlexxIT/go2rtc/pkg/core.(*Sender).HandleRTP.func1()
2023-10-15 21:12:35.354559572  	github.com/AlexxIT/go2rtc/pkg/core/track.go:146 +0x9b
2023-10-15 21:12:35.354566117  created by github.com/AlexxIT/go2rtc/pkg/core.(*Sender).HandleRTP in goroutine 592
2023-10-15 21:12:35.354612884  	github.com/AlexxIT/go2rtc/pkg/core/track.go:140 +0x359
2023-10-15 21:12:35.476791159  [INFO] The go2rtc service exited with code 2 (by signal 0)
2023-10-15 21:12:36.492441688  [WARN] Using go2rtc binary from '/config/go2rtc' instead of the embedded one
2023-10-15 21:12:36.492444880  [INFO] Starting go2rtc...
2023-10-15 21:12:36.558952629  18:12:36.558 INF go2rtc version 1.8.1 linux/amd64
2023-10-15 21:12:36.559276792  18:12:36.559 INF [api] listen addr=0.0.0.0:1984
2023-10-15 21:12:36.559766480  18:12:36.559 INF [rtsp] listen addr=0.0.0.0:8554
2023-10-15 21:12:36.559980911  18:12:36.559 INF [webrtc] listen addr=0.0.0.0:8555/tcp
2023-10-15 21:12:52.877437239  18:12:52.877 WRN github.com/AlexxIT/go2rtc/internal/streams/producer.go:171 > error=EOF url="echo:/config/scripts/get_tuya_stream.sh <hidden>"

Apparently, go2rtc even crashes and exits, so Frigate's s6-overlay automatically restarts it.

@felipecrs
Copy link
Contributor Author

@pergolafabio I suggest you try the method above with your famous wi-fi pet feeder. It should output a URL like the one I mentioned, then try to play it in VLC.

As mentioned however, it still cannot be played through go2rtc.

@AlexxIT
Copy link
Owner

AlexxIT commented Oct 16, 2023

HLS is very complicated source. Go2rtc has limited direct support. It's better to use ffmpeg source in case of problems.

@felipecrs
Copy link
Contributor Author

Yes, but unfortunately ffmpeg does not work either. I will get some logs.

I changed the last line of my script to:

echo -n "ffmpeg:${url}#video=copy#audio=copy"

Then I get only this in my logs:

2023-10-16 04:13:00.817574992  01:13:00.816 WRN [streams] json locked
2023-10-16 04:13:03.042972887  01:13:03.042 ERR github.com/AlexxIT/go2rtc/internal/mjpeg/init.go:166 > error="streams: exec: exit status 1"
2023-10-16 04:13:50.339977885  01:13:50.339 ERR github.com/AlexxIT/go2rtc/internal/mjpeg/init.go:166 > error="streams: exec: exit status 1"

Is it actually possible to output ffmpeg: with echo?

@felipecrs
Copy link
Contributor Author

Ok, I think it's possible. But the stream is actually very unstable right now. I will do more testing with ffmpeg first.

@pergolafabio
Copy link

@pergolafabio I suggest you try the method above with your famous wi-fi pet feeder. It should output a URL like the one I mentioned, then try to play it in VLC.

Aha, I try it later!! But before you were working on a script with mqtt , now it's completely different??

@felipecrs
Copy link
Contributor Author

Yeah, this is a different approach. Work on that should be tracked in #315, but I am not working on it anymore.

@pergolafabio
Copy link

quickly tested it , but it doesnt show me any output?

image

@felipecrs
Copy link
Contributor Author

Ok, NVM. It is not gonna work for you then.

@AlexxIT AlexxIT added bug Something isn't working panic labels Oct 24, 2023
@AlexxIT AlexxIT added this to the v1.8.3 milestone Nov 12, 2023
@AlexxIT AlexxIT changed the title Panic when using HLS as source Fix panic for broken RTP with AAC Nov 13, 2023
AlexxIT added a commit that referenced this issue Nov 13, 2023
@AlexxIT
Copy link
Owner

AlexxIT commented Nov 16, 2023

@AlexxIT AlexxIT closed this as completed Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working panic
Projects
None yet
Development

No branches or pull requests

3 participants