Repository: aiortc/aiortc

Issue:
Invalid data found when processing input: 'avcodec_send_packet()' on VP8 track
Hello,

I encountered an issue when subscribing to a VP8 video track. When I call the `recv()` method on the `RemoteStreamTrack` within an `asyncio.Task`, it throws the following error:

```
Exception in thread video-decoder:
Traceback (most recent call last):
  File "/usr/lib/python3.12/threading.py", line 1073, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.12/threading.py", line 1010, in run
    self._target(*self._args, **self._kwargs)
  File "/home/<redacted>/venv/lib/python3.12/site-packages/aiortc/rtcrtpreceiver.py", line 68, in decoder_worker
    for frame in decoder.decode(encoded_frame):
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/<redacted>/venv/lib/python3.12/site-packages/aiortc/codecs/vpx.py", line 162, in decode
    return cast(list[Frame], self.codec.decode(packet))
                             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "av/codec/context.pyx", line 433, in av.codec.context.CodecContext.decode
  File "av/codec/context.pyx", line 448, in av.codec.context.CodecContext.decode
  File "av/codec/context.pyx", line 340, in av.codec.context.CodecContext._send_packet_and_recv
  File "av/error.pyx", line 424, in av.error.err_check
av.error.InvalidDataError: [Errno 1094995529] Invalid data found when processing input: 'avcodec_send_packet()'
```

After this error, the `video-decoder` thread stops completely. I was wondering what could cause this issue, and whether a `try...except` block could be added to the `decoder_worker` function in aiortc to handle this case gracefully?

Thanks in advance for your help.

Discussion:
@jlaine: Before we blindly ignore this error, could you try to narrow down what causes this to happen? At the very least I'd like to capture a realistic unit test for this.@apz-noan: For context, the subscribed stream come from Janus (SFU). I instrument a bit of the code to display further information about this exception:
```
processing track <aiortc.rtcrtpreceiver.RemoteStreamTrack object at 0x769b3a1d2450>
[packet] > 0 | False | False >> av.Packet of #0, dts=None, pts=0; 1861 bytes at 0x769b3a1c5f80>
0 | b'\xd1(\x00\xdf\xa7\xb7\xdeH0\xbc\x80\xbe\x81>\x81\x7f\xa0)\x00b+\x82\t\xcaz%\xeb\xb4!\xa5\xc2\xa6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\x03\xd5-Y\x05\xa9\xe9\xf3&V\\\x1e\xbf\x0c/\xa5]>im\x11\xf7\r\xea\'P>v\xa8\x06\xc2G\xdb\x02z\xed\xfe\xd3\xc0`\xfc\xb7\xf1\xdcD\xd89.\xa4\xf9\xde\xdc\xdb\n\x08\x92E\xb1>\xf9\x1b\xaa\xda\xcd\x1e\xa0/g\xf4g,b\xf3\x17\xdd\x1c6\xabG\xe1\xe8\xd8\xf5\x07\xfbJ_D\xff R\xaf\xde\x1a\xb4Da\x8a\x98\x9949)*\xd8M)\x8b\xc6=\xd9Z[\xbfA\xc0\x85\xe2\x14\xa7,\xfe-\\-,\xb0d\xe8\xec$K\x80&\xe1\xaa\xca\xcb5`\xaa\xf3]b\xe3\n\xb6\x8aGF\x08\xf8\x04]"\x9d)\x99kT\x8e\xc1\xec\x17\xca\x97\xe6@\xf3\x06\xc7\xe3x\xb9v\xd5\xbbs\x1d\xcf\xda\x1f\xea{\xcf\x02\xc3\x8cDS\xbb\xa3E\xdd)?m\x04\x0e\n\tl\xb5\x81\xda\xe54\xff\x84\xe8\xc3MAVL\xf7\x9a\xce\x878|\x187\xcdC#|7\xcd\xbb\x9fSe\x87\xd8\xc0/\x99\x9b\xc7\xed\xc7}|t\xd4\x1c:\xc5\x81\xf9\xf3\xac\x9c\xb5\xe5\xa2\\y\x17\xd4\xa2\x9cxF\xa3s\x8eS\xc9\xf7\xec-\x86(\xd98O]\xb0\xcb\x98\xdc\xccF\xc4\xe7(\xffcZ\x15\xd0'
Exception in thread video-decoder:
Traceback (most recent call last):
  File "/usr/lib/python3.12/threading.py", line 1073, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.12/threading.py", line 1010, in run
    self._target(*self._args, **self._kwargs)
  File "/home/<redacted>venv/lib/python3.12/site-packages/aiortc/rtcrtpreceiver.py", line 75, in decoder_worker
    raise err
  File "/home/<redacted>/venv/lib/python3.12/site-packages/aiortc/rtcrtpreceiver.py", line 70, in decoder_worker
    for frame in decoder.decode(encoded_frame):
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/<redacted>/venv/lib/python3.12/site-packages/aiortc/codecs/vpx.py", line 163, in decode
    return cast(list[Frame], self.codec.decode(packet))
                             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "av/codec/context.pyx", line 433, in av.codec.context.CodecContext.decode
  File "av/codec/context.pyx", line 448, in av.codec.context.CodecContext.decode
  File "av/codec/context.pyx", line 340, in av.codec.context.CodecContext._send_packet_and_recv
  File "av/error.pyx", line 424, in av.error.err_check
av.error.InvalidDataError: [Errno 1094995529] Invalid data found when processing input: 'avcodec_send_packet()'
```

Also the error seems to appear randomly (maybe a keyframe is not received as first packet?), sometimes it works flawlessly.@jlaine: Hm looking into it, our previous code did silently discard errors:

```python
        result = lib.vpx_codec_decode(
            self.codec,
            encoded_frame.data,
            len(encoded_frame.data),
            ffi.NULL,
            lib.VPX_DL_REALTIME,
        )
        if result == lib.VPX_CODEC_OK:
            ...
``

Solution patch:
diff --git a/src/aiortc/codecs/vpx.py b/src/aiortc/codecs/vpx.py
index 7232d47a..0e8c2a05 100644
--- a/src/aiortc/codecs/vpx.py
+++ b/src/aiortc/codecs/vpx.py
@@ -1,3 +1,4 @@
+import logging
 import multiprocessing
 import random
 from struct import pack, unpack_from
@@ -13,6 +14,8 @@
 from ..mediastreams import VIDEO_TIME_BASE, convert_timebase
 from .base import Decoder, Encoder
 
+logger = logging.getLogger(__name__)
+
 DEFAULT_BITRATE = 500000  # 500 kbps
 MIN_BITRATE = 250000  # 250 kbps
 MAX_BITRATE = 1500000  # 1.5 Mbps
@@ -168,10 +171,14 @@ def __init__(self) -> None:
         self.codec = CodecContext.create("libvpx", "r")
 
     def decode(self, encoded_frame: JitterFrame) -> list[Frame]:
-        packet = Packet(encoded_frame.data)
-        packet.pts = encoded_frame.timestamp
-        packet.time_base = VIDEO_TIME_BASE
-        return cast(list[Frame], self.codec.decode(packet))
+        try:
+            packet = Packet(encoded_frame.data)
+            packet.pts = encoded_frame.timestamp
+            packet.time_base = VIDEO_TIME_BASE
+            return cast(list[Frame], self.codec.decode(packet))
+        except av.FFmpegError as e:
+            logger.warning("Vp8Decoder() failed to decode, skipping package: " + str(e))
+            return []
 
 
 class Vp8Encoder(Encoder):


Test patch:
diff --git a/tests/test_vpx.py b/tests/test_vpx.py
index d80aff16..0da19734 100644
--- a/tests/test_vpx.py
+++ b/tests/test_vpx.py
@@ -1,4 +1,6 @@
 import fractions
+import io
+from contextlib import redirect_stderr
 from unittest import TestCase
 
 from aiortc.codecs import get_decoder, get_encoder
@@ -8,6 +10,7 @@
     VpxPayloadDescriptor,
     number_of_threads,
 )
+from aiortc.jitterbuffer import JitterFrame
 from aiortc.rtcrtpparameters import RTCRtpCodecParameters
 
 from .codecs import CodecTestCase
@@ -161,6 +164,11 @@ def test_decoder(self) -> None:
         decoder = get_decoder(VP8_CODEC)
         self.assertIsInstance(decoder, Vp8Decoder)
 
+        # decode junk
+        with redirect_stderr(io.StringIO()):
+            frames = decoder.decode(JitterFrame(data=b"123", timestamp=0))
+        self.assertEqual(frames, [])
+
     def test_encoder(self) -> None:
         encoder = self.ensureIsInstance(get_encoder(VP8_CODEC), Vp8Encoder)