Eittipat / pyrtmp

PyRTMP: Pure Python RTMP server
MIT License
90 stars 20 forks source link

handshake failing in tests #18

Closed nkarpov closed 6 months ago

nkarpov commented 7 months ago

Was trying to get this working on a local env (mac m1, 14.4.1) and running into a handshake issue with the readme examples. Pulled and ran the tests directly in master and getting the same...

=================================================================================================== FAILURES ===================================================================================================
_______________________________________________________________________________________ TestFFMPEG.test_multiple_ffmpeg ________________________________________________________________________________________

self = <tests.test_demo_ffmpeg.TestFFMPEG testMethod=test_multiple_ffmpeg>

    async def test_multiple_ffmpeg(self):
        with tempfile.TemporaryDirectory() as tempdir:
            # given
            server = SimpleServer(tempdir)
            await server.create(host="127.0.0.1", port=1935)
            await server.start()

            tasks = []
            for i in range(3):
                stream_name = f"test_ffmpeg_{i}"
                command = "ffmpeg -i SampleVideo_1280x720_5mb.flv -c:v copy -c:a copy -f flv {}"
                target = os.path.join(tempdir, stream_name + ".flv")
                remove_if_exist(target)
                tasks.append(invoke_command(command.format(f"rtmp://127.0.0.1:1935/test/{stream_name}")))

            # when
>           await asyncio.gather(*tasks)

test_demo_ffmpeg.py:59:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

command = 'ffmpeg -i SampleVideo_1280x720_5mb.flv -c:v copy -c:a copy -f flv rtmp://127.0.0.1:1935/test/test_ffmpeg_1'

    async def invoke_command(command: str):
        proc = None
        try:
            proc = await asyncio.subprocess.create_subprocess_shell(
                command,
                stdout=asyncio.subprocess.PIPE,
                stderr=asyncio.subprocess.PIPE,
            )
            stdout, stderr = await proc.communicate()
            if proc.returncode > 0:
                logging.exception(stderr.decode())
>               raise Exception("Non-zero returned")
E               Exception: Non-zero returned

__init__.py:24: Exception
---------------------------------------------------------------------------------------------- Captured log call -----------------------------------------------------------------------------------------------
INFO     pyrtmp.rtmp:rtmp.py:208 Serving on ('127.0.0.1', 1935)
DEBUG    pyrtmp.rtmp:rtmp.py:75 Client connected 127.0.0.1:58575
DEBUG    pyrtmp.rtmp:rtmp.py:75 Client connected 127.0.0.1:58576
DEBUG    pyrtmp.rtmp:rtmp.py:75 Client connected 127.0.0.1:58574
ERROR    pyrtmp.rtmp:rtmp.py:113 Reading off end of bitstring with fmt 'bytes:1528'. Only 10632 bits available.

Is this an environment thing? Here's the ffmpeg version/config in case it's relevant

ffmpeg version 7.0 Copyright (c) 2000-2024 the FFmpeg developers
built with Apple clang version 15.0.0 (clang-1500.3.9.4)
configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/7.0 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags='-Wl,-ld_classic' --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libaribb24 --enable-libbluray --enable-libdav1d --enable-libharfbuzz --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopenvino --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox --enable-neon
libavutil      59.  8.100 / 59.  8.100
libavcodec     61.  3.100 / 61.  3.100
libavformat    61.  1.100 / 61.  1.100
libavdevice    61.  1.100 / 61.  1.100
libavfilter    10.  1.100 / 10.  1.100
libswscale      8.  1.100 /  8.  1.100
libswresample   5.  1.100 /  5.  1.100
libpostproc    58.  1.100 / 58.  1.100

Appreciate any advice - thanks!

Akuma555555555555 commented 7 months ago

bro,I met the same problem,have you solved it? I guess it is because of the “bitstring" version,I will try other version later

Eittipat commented 7 months ago

Hi, all

I merged this pull request https://github.com/Eittipat/pyrtmp/pull/13 let see if it help

PedroSumur commented 6 months ago

@Eittipat I have the same error with the pip version and your github code, this is my stacktrace:

DEBUG:asyncio:Using selector: KqueueSelector
INFO:pyrtmp.rtmp:Serving on ('0.0.0.0', 1935)
DEBUG:pyrtmp.rtmp:Client connected 192.168.0.37:42904
ERROR:pyrtmp.rtmp:Needed a length of at least 12224 bits, but only 10632 bits were available.
Traceback (most recent call last):
  File "/Users/pedro/Desktop/piTests/venv/lib/python3.12/site-packages/pyrtmp/rtmp.py", line 79, in client_callback
    await self.on_handshake(session)
  File "/Users/pedro/Desktop/piTests/venv/lib/python3.12/site-packages/pyrtmp/rtmp.py", line 120, in on_handshake
    await session.handshake()
  File "/Users/pedro/Desktop/piTests/venv/lib/python3.12/site-packages/pyrtmp/session_manager.py", line 56, in handshake
    await C2.from_stream(self.fifo_reader)
  File "/Users/pedro/Desktop/piTests/venv/lib/python3.12/site-packages/pyrtmp/messages/handshake.py", line 57, in from_stream
    rand = await stream.read("bytes:1528")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pedro/Desktop/piTests/venv/lib/python3.12/site-packages/pyrtmp/__init__.py", line 42, in read
    value = self.buffer.read(fmt)
            ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pedro/Desktop/piTests/venv/lib/python3.12/site-packages/bitstring/bitstream.py", line 349, in read
    val = dtype.read_fn(self, self._pos)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pedro/Desktop/piTests/venv/lib/python3.12/site-packages/bitstring/dtypes.py", line 300, in read_fn
    raise bitstring.ReadError(f"Needed a length of at least {length} bits, but only {len(bs) - start} bits were available.")
bitstring.exceptions.ReadError: Needed a length of at least 12224 bits, but only 10632 bits were available.
DEBUG:pyrtmp.rtmp:Clean up 192.168.0.37:42904

Regards.

Eittipat commented 6 months ago

I will check it this weekend, can you give me a your test file @PedroSumur

PedroSumur commented 6 months ago

I will check it this weekend, can you give me a your test file @PedroSumur

@Eittipat the test file is from your examples, don't worry about that.

Btw, I have the same error with my laptop with fedora 40 and my macbook pro m1 with mac os Sonoma, both laptops with python 3.12.

Regards and thanks for your work.

Joesv commented 6 months ago

@Eittipat first of all, thank you for this lib.

I installed pyrtmp with the following command to enforce using the latest version, but I still run into the problem mentioned above.

pip install --no-cache-dir --upgrade pyrtmp@git+https://github.com/Eittipat/pyrtmp.git@ee8bf199d7dec0a599cda1224e50fe40f44f46b9

I'm using the example mentioned in the readme, do you have any suggestions?

Eittipat commented 6 months ago

Hi, bitstring needs to be version >=4.1<=4.2. I fixed in https://github.com/Eittipat/pyrtmp/pull/20 and released new version v0.3.1

Thank for all feedback

Joesv commented 6 months ago

Perfect, I'll give it a shot. Thanks!