home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
71.35k stars 29.88k forks source link

Stream - Malformed AAC bitstream detected #47084

Closed JaHaa88 closed 2 years ago

JaHaa88 commented 3 years ago

The problem

Unable to get a Reolink camera's to work in HA. There are multiple errors in the log which boil down too: Malformed AAC bitstream

The error looks very similar to the following issue: Stream - Malformed AAC bitstream detected #39848 The proposed solution is not working for me: Disable audio for HLS or mpegts input #39906

I have tried the ONVIF integration as well as the generic and ffmpeg components. All results in the same errors. Both the sub (H264) and main (H265) streams have this issue.

This issue is also mentioned by other people on the forum: https://community.home-assistant.io/t/rtsp-camera-stream-not-working-reolink-rlc-810a/279976

What is version of Home Assistant Core has the issue?

2021.2.3

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

ONVIF, camera

Link to integration documentation on our website

https://www.home-assistant.io/integrations/onvif/

Example YAML snippet

in the camera.yaml:

- platform: ffmpeg
  name: Camera meterkast main
  input: !secret Camera_meterkast_main
  extra_arguments: '-bsf:a aac_adtstoasc'
- platform: ffmpeg
  name: Camera meterkast sub
  input: !secret Camera_meterkast_sub

- platform: generic
  name: Streaming Enabled
  still_image_url: !secret Camera_meterkast_mainstill
  stream_source: !secret Camera_meterkast_main

The URL in secret: rtsp://user:password@192.168.x.x:554/h264Preview_01_sub

Anything in the logs that might be useful for us?

Logger: homeassistant.components.stream.worker
Source: components/stream/worker.py:213
Integration: Stream (documentation, issues)
First occurred: 8:09:37 AM (3 occurrences)
Last logged: 8:34:13 AM

Stream connection failed: rtsp://user:password@192.168.x.x:554/
Stream connection failed: rtsp://user:password@192.168.x.x:554/h264Preview_01_sub
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/stream/worker.py", line 56, in stream_worker
    _stream_worker_internal(hass, stream, quit_event)
  File "/usr/src/homeassistant/homeassistant/components/stream/worker.py", line 293, in _stream_worker_internal
    mux_video_packet(packet)  # mutates packet timestamps
  File "/usr/src/homeassistant/homeassistant/components/stream/worker.py", line 213, in mux_video_packet
    buffer.output.mux(packet)
  File "av/container/output.pyx", line 204, in av.container.output.OutputContainer.mux
  File "av/container/output.pyx", line 227, in av.container.output.OutputContainer.mux_one
  File "av/container/core.pyx", line 257, in av.container.core.Container.err_check
  File "av/error.pyx", line 336, in av.error.err_check
av.error.PermissionError: [Errno 1] Operation not permitted: '<none>'; last error log: [mp4] Malformed AAC bitstream detected: use the audio bitstream filter 'aac_adtstoasc' to fix it ('-bsf:a aac_adtstoasc' option with ffmpeg)

Error 2:
Logger: libav.mp4
Source: components/stream/worker.py:213
First occurred: 8:09:37 AM (3 occurrences)
Last logged: 8:34:13 AM

Malformed AAC bitstream detected: use the audio bitstream filter 'aac_adtstoasc' to fix it ('-bsf:a aac_adtstoasc' option with ffmpeg)

error 3:
Logger: homeassistant.components.stream.worker
Source: components/stream/worker.py:183
Integration: Stream (documentation, issues)
First occurred: 8:09:48 AM (5 occurrences)
Last logged: 8:35:19 AM

Error demuxing stream while finding first packet: Invalid data - got 7 packets with missing DTS while initializing
Error demuxing stream while finding first packet:

error 4:
Logger: root
Source: /usr/src/homeassistant/homeassistant/bootstrap.py:307
First occurred: 9:06:12 AM (5 occurrences)
Last logged: 9:09:40 AM

Uncaught exception
Traceback (most recent call last):
  File "av/container/output.pyx", line 23, in av.container.output.close_output
  File "av/container/core.pyx", line 257, in av.container.core.Container.err_check
  File "av/error.pyx", line 323, in av.error.err_check
  File "av/container/pyio.pyx", line 39, in av.container.pyio.pyio_write_gil
ValueError: I/O operation on closed file.

Installation is running in Docker on a Ubuntu 18.04 LTS. IP cam is in another VLAN but is reachable and working with VLC.

Stream, ffmpeg and camera integrations are added to the configuration yaml.

i have tried to add the suggested '-bsf:a aac_adtstoasc' to the ONVIF options arguments and to the extra_arguments in the ffmpeg component with no luck.

probot-home-assistant[bot] commented 3 years ago

Hey there @hunterjm, mind taking a look at this issue as its been labeled with an integration (onvif) you are listed as a codeowner for? Thanks! (message by CodeOwnersMention)

dangel666 commented 3 years ago

I'm having the same issue - hassio on a RPI4 in my case with two 810A cameras. I saw errors using the Reolink integration and added some lovelace cards which do display the static periodic images fine. Whilst hunting down another problem I saw errors in the log opening the RTMP stream and switched to RTSP with no luck - the live previews don't work still but the errors in my log have now changed:

2021-02-27 07:59:41 ERROR (Recorder) [libav.mp4] Malformed AAC bitstream detected: use the audio bitstream filter 'aac_adtstoasc' to fix it ('-bsf:a aac_adtstoasc' option with ffmpeg) 2021-02-27 07:59:41 ERROR (Recorder) [root] Uncaught exception Traceback (most recent call last): File "av/container/output.pyx", line 23, in av.container.output.close_output File "av/container/core.pyx", line 257, in av.container.core.Container.err_check File "av/error.pyx", line 323, in av.error.err_check File "av/container/pyio.pyx", line 39, in av.container.pyio.pyio_write_gil ValueError: I/O operation on closed file. 2021-02-27 07:59:49 ERROR (stream_worker) [libav.mp4] Malformed AAC bitstream detected: use the audio bitstream filter 'aac_adtstoasc' to fix it ('-bsf:a aac_adtstoasc' option with ffmpeg) 2021-02-27 07:59:49 ERROR (stream_worker) [homeassistant.components.stream.worker] Stream connection failed: rtsp://homeassistant:password@192.168.1.41:554/h264Preview_01_sub Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/stream/worker.py", line 56, in stream_worker _stream_worker_internal(hass, stream, quit_event) File "/usr/src/homeassistant/homeassistant/components/stream/worker.py", line 293, in _stream_worker_internal mux_video_packet(packet) # mutates packet timestamps File "/usr/src/homeassistant/homeassistant/components/stream/worker.py", line 213, in mux_video_packet buffer.output.mux(packet) File "av/container/output.pyx", line 204, in av.container.output.OutputContainer.mux File "av/container/output.pyx", line 227, in av.container.output.OutputContainer.mux_one File "av/container/core.pyx", line 257, in av.container.core.Container.err_check File "av/error.pyx", line 336, in av.error.err_check av.error.PermissionError: [Errno 1] Operation not permitted: ''; last error log: [mp4] Malformed AAC bitstream detected: use the audio bitstream filter 'aac_adtstoasc' to fix it ('-bsf:a aac_adtstoasc' option with ffmpeg)

I then switched back to RTMP with the sub (and not main) stream and now have working previews. As I understand it the main stream is H265 and the sub is H264.

I'm still seeing errors in the log - but they're referring to RTSP. I can see both cameras live.. Weird. Gonna reboot the server..

Core 2021.2.3, HA OS 5,12, Integration: https://github.com/fwestenberg/reolink_dev

dangel666 commented 3 years ago

Rebooted, still got live streams in lovelace. No errors in the log this time - will keep an eye on it.

So for now RTMP + stream 'sub' - works!

JaHaa88 commented 3 years ago

No luck for me. I tried the RTMP stream with the generic component with no success.

JaHaa88 commented 3 years ago

After tinkering around i found that disabling the stream component (commenting out in config) all camera components started to work. I now can see the live stream (main and sub) in the Onvif integration, generic and ffmpeg component.

So the stream component is clear causing the above mentioned errors.

Only the generic component does show the main stream in 4k fluent the other components bring the entire server to a stand still. But this however is unrelated i think and probable due the the ffmpeg handling of H265 streams.

uvjustin commented 3 years ago

Just saw this - it's essentially the same issue as #41932. The problem is coming from the audio stream - due to the options we use, the bitstream filter to correct the audio track is not automatically applied, and we currently have no way to apply it using the current library. We were able to work around it with other cameras by disabling the audio stream either by disabling it outright or selecting an unusable codec, but admittedly this is kind of a hack. I will see if there is a way we can detect these problematic audio streams and disable them automatically so we don't have to resort to adding a separate option to disable the audio streams manually. Can someone run this code snippet on a problematic camera feed and tell me what it prints out?

import av
input_container=av.open("rtsp://yourcamerasrtspurl")
audio_stream=input_container.streams.audio[0]
i=0
for packet in input_container.demux(audio_stream):
  if packet.size > 2:
    packet_view=memoryview(packet)
    print(packet_view[0], packet_view[1])
    if i > 5:
      break
    i+=1
input_container.close()
JaHaa88 commented 3 years ago

Hi uvjustin,

Thanks for the feedback. I have tried to disable de audio on the camera itself with no luck.

I can surely run the code. But i have no idea how >.<.

probot-home-assistant[bot] commented 3 years ago

stream documentation stream source (message by IssueLinks)

uvjustin commented 3 years ago

From the bash shell in your container, type "python3". That should bring up the python interpreter. Then run the code snippet I pasted above, filling in your rtsp url in place of the yourcamerasrtspurl. To exit the interpreter, just type exit()

JaHaa88 commented 3 years ago

i get this syntax error:

`

audio_stream=input_container.streams.audio[0]

i=0 for packet in input_container.demux(audio_stream): ... if packet.size > 2: ... packet_view=memoryview(packet) ... print(packet_view[0], packet_view[1]) ... if i > 5: ... break ... i+=1 ... input_container.close() File "", line 8 input_container.close() ^ SyntaxError: invalid syntax `

i have done this is the 'execute bash' in portainer.

uvjustin commented 3 years ago

The indentations/spaces are important in python - the body of the for statement and the if statement have to be indented consistently. Can you try again?

JaHaa88 commented 3 years ago

tempsnip

Sorry, i can't get it to work.

uvjustin commented 3 years ago

Thanks for the screenshot. Add one extra blank line before the input_container.close() line.

JaHaa88 commented 3 years ago

Than i get this:

Capture

uvjustin commented 3 years ago

Great, I think that might be enough for me to work on a fix. Thanks

JaHaa88 commented 3 years ago

okee great! thank you for the support.

simon-beep commented 3 years ago

I am seeing the same behaviour with a Reolink RLC-510WA on 2021.4.3. Is this fixed in 2021.4? And if not, when is it likely to arrive? Apologies if this is a stupid question, I'm not sure how to tell from Github.

2021-04-10 16:04:33 INFO (MainThread) [homeassistant.components.stream] Started stream: rtsp://****:****@192.168.1.22:554/h264Preview_01_sub
2021-04-10 16:04:39 ERROR (stream_worker) [libav.mp4] Malformed AAC bitstream detected: use the audio bitstream filter 'aac_adtstoasc' to fix it ('-bsf:a aac_adtstoasc' option with ffmpeg)
2021-04-10 16:04:39 ERROR (stream_worker) [root] Uncaught thread exception
Traceback (most recent call last):
File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/usr/src/homeassistant/homeassistant/components/stream/__init__.py", line 203, in _run_worker
stream_worker(self.source, self.options, segment_buffer, self._thread_quit)
File "/usr/src/homeassistant/homeassistant/components/stream/worker.py", line 288, in stream_worker
segment_buffer.mux_packet(packet)
File "/usr/src/homeassistant/homeassistant/components/stream/worker.py", line 99, in mux_packet
self._stream_buffer.output.mux(packet)
File "av/container/output.pyx", line 204, in av.container.output.OutputContainer.mux
File "av/container/output.pyx", line 227, in av.container.output.OutputContainer.mux_one
File "av/container/core.pyx", line 258, in av.container.core.Container.err_check
File "av/error.pyx", line 336, in av.error.err_check
av.error.PermissionError: [Errno 1] Operation not permitted: '<none>'; last error log: [mp4] Malformed AAC bitstream detected: use the audio bitstream filter 'aac_adtstoasc' to fix it ('-bsf:a aac_adtstoasc' option with ffmpeg)
roadbikemike commented 3 years ago

I am having the same issue with RLC-810A. Cannot get the stream working at all, even with audio disabled. Would be great to see some movement on this.

I was able to get this working with the Reolink HAC component as noted above: https://github.com/fwestenberg/reolink_dev

uvjustin commented 3 years ago

A google search shows there seems to be some kind of problem with Reolink's RTSP implementation and ffmpeg. There are documented problems on several video projects. A common workaround is to use the RTMP feed. Try using the RTMP feed in stream_source and see if you still have the same issue. The urls for main and sub stream look something like:

rtmp://192.168.1.100/bcs/channel0_main.bcs?channel=0&stream=0&user=admin&password=password

rtmp://192.168.1.100/bcs/channel0_sub.bcs?channel=0&stream=0&user=admin&password=password

roadbikemike commented 3 years ago

I tried all sorts of combinations (different streams) between RTSP and RTMP without luck. The only thing I could get working was https://github.com/fwestenberg/reolink_dev. Glad to see this is re-opened and going to get another look and potential fix.

uvjustin commented 3 years ago

What version of HA are you on? Make sure you are on 2021.4 or later. Can you post the errors in your log when using the RTMP stream?

roadbikemike commented 3 years ago

I am on the latest, 2021.5.1. Yeah, I can get you the logs later on today.

elad-bar commented 3 years ago

In 2021.6.3 most of the audio stream are not working, in VLC the same audio streams are working

FosCam R2 via Shinobi Video (ceoverted to AAC) - not working: image

Dahua via Shinobi Video (converted to AAC) - working: image

uvjustin commented 3 years ago

In 2021.6.3 most of the audio stream are not working, in VLC the same audio streams are working

FosCam R2 via Shinobi Video (ceoverted to AAC) - not working: image

Dahua via Shinobi Video (converted to AAC) - working: image

Audio support in the stream component is limited. Some of this is codec related (only mp3 and aac are supported), and some of it is pyav (the library we use to access ffmpeg) related (some aac streams require bitstream filters which are not enabled in pyav).

uvjustin commented 3 years ago

@krushmike Did you get a chance to capture your logs?

ankerstal commented 3 years ago

Hi! I also have a similar problem. I run the docker image: homeassistant/raspberrypi4-homeassistant:latest (pulled today) on a rpi4.

When trying to stream video from my Reolink RLC-810A i get the following error. (I get alot if different errors but this seems to be one oft he first to appear.

I have tried all combinations of rtsp/rtmp, mainstream substream i could think of. Still not been able to stream från the camera. (works just fine in VLC).

2021-07-30 16:10:46 INFO (MainThread) [homeassistant.components.stream] Started stream: rtsp://****:****@172.25.0.102:554/
2021-07-30 16:10:47 WARNING (stream_worker) [homeassistant.components.stream.worker] ADTS AAC detected - disabling audio stream
2021-07-30 16:10:47 ERROR (stream_worker) [libav.mp4] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 90179 >= 90179
2021-07-30 16:10:47 ERROR (stream_worker) [root] Uncaught thread exception
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/threading.py", line 973, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.9/threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/src/homeassistant/homeassistant/components/stream/__init__.py", line 213, in _run_worker
    stream_worker(self.source, self.options, segment_buffer, self._thread_quit)
  File "/usr/src/homeassistant/homeassistant/components/stream/worker.py", line 384, in stream_worker
    segment_buffer.mux_packet(packet)
  File "/usr/src/homeassistant/homeassistant/components/stream/worker.py", line 132, in mux_packet
    self._av_output.mux(packet)
  File "av/container/output.pyx", line 204, in av.container.output.OutputContainer.mux
  File "av/container/output.pyx", line 227, in av.container.output.OutputContainer.mux_one
  File "av/container/core.pyx", line 258, in av.container.core.Container.err_check
  File "av/error.pyx", line 336, in av.error.err_check
av.error.ValueError: [Errno 22] Invalid argument: '<none>'; last error log: [mp4] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 90179 >= 90179
PRebs64 commented 3 years ago

Same issue with Reolink 822A - connected via the Reolink NVR. Updated still-picture in Lovelace, but not working on either RTSP or RTMP. I've tried both the HACS Reolink_dev and the OVNIF. One of the integrations seem to work with Live stream. Firmware updated, running on latest 2021.8.4 version of HA. Please suggest or help. Thanks in advance :)

ankerstal commented 3 years ago

@PRebs64 when I upgraded to the beta 2021.8.0b5 it resolved all my problems. Im not sure if the changes have been merged into the stable releases. See #53761

PRebs64 commented 3 years ago

Thanks AnkerStål - I've now installed core version 2021.8.0b5 and tried to add the camera's one by one again. Sadly enough it is still the same issue. Pre-view (still picture) is updated, but streaming does not start up. NVR firmware is up to date. As info, under Supervisor it says, that latest version is 2021.8.4 (which I was on b4). Any other suggestions or info I can provide to move closer to a resolution/work around?

github-actions[bot] commented 2 years ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.