apivideo / api.video-flutter-live-stream

Flutter RTMP live stream client. Made with ♥ by api.video
MIT License
62 stars 37 forks source link

[Bug]: RTMP send error #39

Closed dengpan-edgejumps closed 5 months ago

dengpan-edgejumps commented 10 months ago

Version

v1.1.1

Which operating systems have you used?

Environment that reproduces the issue

Google Pixel 6 (Android 12)

Is it reproducible in the example application?

Yes

RTMP Server

rtmp://broadcast.api.video/s

Reproduction steps

  1. configure video with VideoConfig.defaultBitrate()
  2. configure audio with AudioConfig()
  3. startPreview()
  4. startStreaming(streamKey)

Expected result

The RTMP server could receive the video stream correctly.

Actual result

startPreview() works correctly because I could see the video on the preview widget, while the video stream seems not sent to the RTMP server correctly.

The same error also applies to the example application, not just my application.

It would first report connected event, and then this error was thrown. As a result, a disconnect event was reported after seconds.

I/        (21871): Format changed : {max-bitrate=2000000, crop-right=719, level=512, mime=video/avc, profile=8, bitrate=2000000, priority=0, intra-refresh-period=0, color-standard=1, csd-1=java.nio.HeapByteBuffer[pos=0 lim=8 cap=8], color-transfer=3, crop-bottom=1279, prepend-sps-pps-to-idr-frames=0, crop-left=0, width=720, bitrate-mode=1, color-range=2, crop-top=0, frame-rate=30, height=1280, csd-0=java.nio.HeapByteBuffer[pos=0 lim=21 cap=21]}
E/rtmpdroid(21871): WriteN, RTMP send error 104 (12 bytes)
E/rtmpdroid(21871): WriteN, RTMP send error 32 (71 bytes)
E/rtmpdroid(21871): WriteN, RTMP send error 9 (42 bytes)
E/RtmpProducer(21871): Error while writing packet to socket
E/RtmpProducer(21871): java.net.SocketException: Connection error
E/RtmpProducer(21871):  at video.api.rtmpdroid.Rtmp.write(Rtmp.kt:139)
E/RtmpProducer(21871):  at io.github.thibaultbee.streampack.ext.rtmp.internal.endpoints.RtmpProducer.write(RtmpProducer.kt:94)
E/RtmpProducer(21871):  at io.github.thibaultbee.streampack.streamers.bases.BaseStreamer$muxListener$1.onOutputFrame(BaseStreamer.kt:132)
E/RtmpProducer(21871):  at io.github.thibaultbee.streampack.internal.muxers.flv.FlvMuxer.encode(FlvMuxer.kt:77)
E/RtmpProducer(21871):  at io.github.thibaultbee.streampack.streamers.bases.BaseStreamer$videoEncoderListener$1.onOutputFrame(BaseStreamer.kt:120)
E/RtmpProducer(21871):  at io.github.thibaultbee.streampack.internal.encoders.MediaCodecEncoder$encoderCallback$1.onOutputBufferAvailable(MediaCodecEncoder.kt:109)
E/RtmpProducer(21871):  at android.media.MediaCodec$EventHandler.handleCallback(MediaCodec.java:1845)
E/RtmpProducer(21871):  at android.media.MediaCodec$EventHandler.handleMessage(MediaCodec.java:1743)
E/RtmpProducer(21871):  at android.os.Handler.dispatchMessage(Handler.java:106)
E/RtmpProducer(21871):  at android.os.Looper.loopOnce(Looper.java:201)
E/RtmpProducer(21871):  at android.os.Looper.loop(Looper.java:288)
E/RtmpProducer(21871):  at android.os.HandlerThread.run(HandlerThread.java:67)

Additional context

[✓] Flutter (Channel stable, 3.10.5, on macOS 13.4.1 22F770820d darwin-arm64, locale en-CA)
    • Flutter version 3.10.5 on channel stable at /Users/simonwang/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 796c8ef792 (3 months ago), 2023-06-13 15:51:02 -0700
    • Engine revision 45f6e00911
    • Dart version 3.0.5
    • DevTools version 2.23.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at /Users/simonwang/Library/Android/sdk
    • Platform android-33, build-tools 33.0.1
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E300c
    • CocoaPods version 1.12.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] IntelliJ IDEA Community Edition (version 2022.3)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.81.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.72.0

[✓] Connected device (3 available)
    • Pixel 6 (mobile) • 21121FDF6006BK • android-arm64  • Android 12 (API 32)
    • macOS (desktop)  • macos          • darwin-arm64   • macOS 13.4.1 22F770820d darwin-arm64
    • Chrome (web)     • chrome         • web-javascript • Google Chrome 116.0.5845.179

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Relevant logs output

No response

dengpan-edgejumps commented 10 months ago

The RTMP connection was established correctly, and my local RTMP server could receive some data and indicate living.

However, the RTMP connection would be disconnected when writing buffer after seconds, and then my local RTMP server became idle and indicates not living.

The same error is also applied when I used api.video's RTMP server.

dengpan-edgejumps commented 10 months ago

my guess is that 1) the buffer is overflowing the RTMP server 2) or the encoded data is malformed or contains errors

However, I have little knowledge about Kotlin and no idea about how to diagnose and fix it.

ThibaultBee commented 9 months ago

As far as I understand, you don't use rtmp://broadcast.api.video/s but a local server. What local server do you use?

The error looks like an issue with the connection. Could you try to reduce video bitrate?

ndahlquist commented 5 months ago

I'm seeing this error regularly with the rtmp://broadcast.api.video/s endpoint, and the default settings in the demo app.

E/RtmpProducer(23985): java.net.SocketException: Connection error
E/RtmpProducer(23985):  at video.api.rtmpdroid.Rtmp.write(Rtmp.kt:139)
E/RtmpProducer(23985):  at io.github.thibaultbee.streampack.ext.rtmp.internal.endpoints.RtmpProducer.write(RtmpProducer.kt:94)
E/RtmpProducer(23985):  at io.github.thibaultbee.streampack.streamers.bases.BaseStreamer$muxListener$1.onOutputFrame(BaseStreamer.kt:132)
E/RtmpProducer(23985):  at io.github.thibaultbee.streampack.internal.muxers.flv.FlvMuxer.encode(FlvMuxer.kt:77)
E/RtmpProducer(23985):  at io.github.thibaultbee.streampack.streamers.bases.BaseStreamer$videoEncoderListener$1.onOutputFrame(BaseStreamer.kt:120)
E/RtmpProducer(23985):  at io.github.thibaultbee.streampack.internal.encoders.MediaCodecEncoder$encoderCallback$1.onOutputBufferAvailable(MediaCodecEncoder.kt:109)
E/RtmpProducer(23985):  at android.media.MediaCodec$EventHandler.handleCallback(MediaCodec.java:1943)
E/RtmpProducer(23985):  at android.media.MediaCodec$EventHandler.handleMessage(MediaCodec.java:1841)
E/RtmpProducer(23985):  at android.os.Handler.dispatchMessage(Handler.java:106)
E/RtmpProducer(23985):  at android.os.Looper.loopOnce(Looper.java:230)
E/RtmpProducer(23985):  at android.os.Looper.loop(Looper.java:319)
E/RtmpProducer(23985):  at android.os.HandlerThread.run(HandlerThread.java:67)

It seems that lowering the bitrate does help. I suggest that the demo app should set the demo bitrates to something lower, because it's a frustrating experience that the app doesn't work out-of-the-box.

ThibaultBee commented 5 months ago

@ndahlquist Thanks for the feedback. It is never easy to set such default parameter because one could expect a low bitrate and one could expect a better quality. Anyway, I lower the default resolution and (bitrate at the same time): https://github.com/apivideo/api.video-flutter-live-stream/commit/a6313cd8d5de510955218f2c87771ed2f5ea5721

ThibaultBee commented 5 months ago

So this issue seems to be related to the default video bitrate. I am closing it. Please reopen an issue if you still face it.

WallyHale commented 1 month ago

I too am getting this error, and have tried lowering the bitrate to RESOLUTION_240 in code, as well as trying all bitrate options in Settings of example app. The app runs fine and streams on Pixel 7a, so it's not a problem with app / network.

The device I'm testing on is a Chinese Android tablet device, so perhaps it's related to that, but is there a way we can troubleshoot?

I presume from the following error it's some encoding issue? E/RtmpProducer(16034): at io.github.thibaultbee.streampack.internal.muxers.flv.FlvMuxer.encode(FlvMuxer.kt:77)

Full error: E/rtmpdroid(16034): WriteN, RTMP send error 32 (111 bytes) E/rtmpdroid(16034): WriteN, RTMP send error 32 (71 bytes) E/rtmpdroid(16034): WriteN, RTMP send error 9 (42 bytes) E/RtmpProducer(16034): Error while writing packet to socket E/RtmpProducer(16034): java.net.SocketException: Connection error E/RtmpProducer(16034): at video.api.rtmpdroid.Rtmp.write(Rtmp.kt:139) E/RtmpProducer(16034): at io.github.thibaultbee.streampack.ext.rtmp.internal.endpoints.RtmpProducer.write(RtmpProducer.kt:94) E/RtmpProducer(16034): at io.github.thibaultbee.streampack.streamers.bases.BaseStreamer$muxListener$1.onOutputFrame(BaseStreamer.kt:132) E/RtmpProducer(16034): at io.github.thibaultbee.streampack.internal.muxers.flv.FlvMuxer.encode(FlvMuxer.kt:77) E/RtmpProducer(16034): at io.github.thibaultbee.streampack.streamers.bases.BaseStreamer$videoEncoderListener$1.onOutputFrame(BaseStreamer.kt:120) E/RtmpProducer(16034): at io.github.thibaultbee.streampack.internal.encoders.MediaCodecEncoder$encoderCallback$1.onOutputBufferAvailable(MediaCo decEncoder.kt:109) E/RtmpProducer(16034): at android.media.MediaCodec$EventHandler.handleCallback(MediaCodec.java:1664) E/RtmpProducer(16034): at android.media.MediaCodec$EventHandler.handleMessage(MediaCodec.java:1611) E/RtmpProducer(16034): at android.os.Handler.dispatchMessage(Handler.java:102) E/RtmpProducer(16034): at android.os.Looper.loop(Looper.java:154) E/RtmpProducer(16034): at android.os.HandlerThread.run(HandlerThread.java:61)