pedroSG94 / RootEncoder

RootEncoder for Android (rtmp-rtsp-stream-client-java) is a stream encoder to push video/audio to media servers using protocols RTMP, RTSP, SRT and UDP with all code written in Java/Kotlin
Apache License 2.0
2.57k stars 776 forks source link

ClosedWriteChannelException: The channel was closed #1601

Open noho501 opened 1 month ago

noho501 commented 1 month ago

Hi @pedroSG94 ,

We are encountering an issue during screen broadcasting on Facebook, where an error appears after about a minute. This problem only occurs on Facebook; streaming to YouTube works fine. This error didn’t appear before, but it has started showing up recently, possibly after a recent update.

Branch: Master Screen broadcast settings: 720x1280, 30fps, bitrate: 500*1000 Platform: Facebook

I’ve attached the full log. Could you please provide any suggestions to help resolve this issue? OPPO-CPH2473-Android-14_2024-10-12_093032.txt.txt

pedroSG94 commented 1 month ago

Hello,

According with the logs, the videoinfo is setted multiple times doing that video packet is reset and maybe produce this error. To avoid it I did a fix for it: https://github.com/pedroSG94/RootEncoder/pull/1602

  implementation 'com.github.pedroSG94.RootEncoder:library:c62693873d'
pedroSG94 commented 1 month ago

I was able to reproduce the error. I'm working on it

noho501 commented 1 month ago

Thank you so much. I am looking forward to your good news.

pedroSG94 commented 1 month ago

For now, I detected that this is a bug in Ktor library because using java.io socket it is working fine.

pedroSG94 commented 1 month ago

Fixed here: https://github.com/pedroSG94/RootEncoder/commit/546ef083a9f704a563e9ec789a7176237db40c29

The problem was related with the soTimeout using a tls socket. I think it is a bug in the ktor library but I was able to solve it using withtimeout in read/write methods manually

noho501 commented 1 month ago

Thank you so much, I will test it and let you know

noho501 commented 1 month ago

Thank you for the fix. However, another issue still seems unresolved. I am trying to stream using the local file I attached, but after a second, it stops. Please check the log.

Log: OPPO-CPH2473-Android-14_2024-10-14_151002.txt

The video used for streaming: https://drive.google.com/file/d/1ncjueHqJ8mIUdl7vSlsQ_7moYSP79mw8/view?usp=sharing

Thanks.

pedroSG94 commented 1 month ago

Hello,

I don't understand that logs. It is a json About the error, are you able to reproduce it using other device?

noho501 commented 1 month ago

Hi,

Please see the new attached log. Yes, I can reproduce the issue on other devices (Android 12 & 14). It's the same error with this video (some videos still work): Video link.

OPPO-CPH2473-Android-14_2024-10-14_1545.txt

pedroSG94 commented 1 month ago

Ok, I was able to reproduce the error.

The audio is working but the video decoder stopped

pedroSG94 commented 1 month ago

Hello,

I was debugging the error with that file and seems that MediaExtractor class of Android is not working properly with that file. Only can extract 3 video frames producing that the video freeze.

For now, I'm looking an alternative to MediaExtractor or a way to fix it.

noho501 commented 1 month ago

Thank you for your confirmation. I am looking forward to your good news.

pedroSG94 commented 1 month ago

After multiple tests I can confirm that it is not a bug in the library code, it is a bug in the MediaExtractor class. MediaExtractor is not able to extract video frames from that video for some reason (audio is working). If you re encode the video using ffmpeg it is wofking fine so it is not a problem in the video config.

The only solution will be implement a new extractor without MediaExtractor class but the only viable alternative is use the extractor of ExoPlayer project. The problem is that ExoPlayer need min API version 19 but this project have min API version 16 so I have plan to do a full refactor to decoders that will allow change the extractor allowing set a extractor based on ExoPlayer and add that extractor to extra-sources module which use min API 21.

I will do this new feature for version 2.5.3 because this is a big change and I want avoid change more code in this version.

pedroSG94 commented 1 month ago

Other alternative is use MediaPlayer class to get video frames and MediaExtractor for audio but I'm not sure if this solution is a good idea.

What do you think about it?. I tested this but it produce others limitations

noho501 commented 1 month ago

Thank you for confirming. I also believe that using ExoPlayer is the best solution. I will wait for version 2.5.3 and test it once it's available.

noho501 commented 3 weeks ago

Hi @pedroSG94 ,

After updating to version 2.5.4, the problem persists, and there are additional issues. The audio is distorted and plays slowly.

Here is the link to the video I streamed: YouTube Live Video.

Here is the original video: Original Video.

Additionally, this video cannot be streamed; it says it is not supported:

Best regards,

pedroSG94 commented 3 weeks ago

Hello,

Thank you for the report. I will check it.

I'm developing an Extractor using Exoplayer extractor instead of MediaExtractor from Android but it is not ready yet so all should be working like in the version 2.5.2 (the video that freeze is not working yet video2)

I will check the new issues maybe related with the new decoder implementation and fix it. For now, use version 2.5.2

pedroSG94 commented 3 weeks ago

Hello,

I checked the first file but the error related with slowly audio is present in previous versions before the change so It is not a problem only in the last version but maybe it is fixeable (still checking it).

About the not supported error, it is because I added an additional check in the decoder that force the video used to have duration that I think is the problem because the video is freeze. If the duration can't be extracted, the video extraction fail in prepareVideo before start decoding to avoid reproduce video that will be invalid and freeze.

I will check the first video but the second and third video should be fixed using the new extractor instead the current extractor. I think that report a fail is better than let the user reproduce it and produce a freeze.

pedroSG94 commented 3 weeks ago

I discovered the reason about the first video fail.

When you extract audio info from the first video using MediaExtractor or even with MediaMetadataRetriever the samplerate is 24000 but the real samplerate is 48000. This produce the slowly audio effect because the encoder is configured based on the decoder extracted value. If you hardcode the samplerate value all is working fine.

This seem that it is not a problem in the decoder code but more a problem in MediaExtractor class of Android. I'm looking if I can find a way to fix it

pedroSG94 commented 3 weeks ago

I added a commit that implement a Media3Extractor using the exoplayer project dependency that improve a bit this problem but still produce problems: https://github.com/pedroSG94/RootEncoder/commit/dcf669a9873651c3822caedf95dd685508be5171

noho501 commented 3 weeks ago

Thank you! I’ll test it and report back to you.

pedroSG94 commented 3 weeks ago

This new Extractor solve the problem with the first file but still have few problems using the second and third file (mostly with the second file). Remember that you need change the extractor for video or audio using:

    genericFromFile.setAudioExtractor(Media3Extractor(this))
    genericFromFile.setVideoExtractor(Media3Extractor(this))
noho501 commented 2 weeks ago

Hi @pedroSG94,

I am using the following dependencies:

implementation 'com.github.pedroSG94.RootEncoder:library:2.5.4'
implementation ('com.github.pedroSG94.RootEncoder:extra-sources:2.5.4') {
    exclude module: 'UVCAndroid'
}

When I implement version 2.5.4 and try to import import com.pedro.extrasources.extractor.Media3Extractor, I receive the error: Unresolved reference: extractor.

Do you know how to resolve this issue?

Thank you!

pedroSG94 commented 2 weeks ago

Hello,

It isn't in the last release. Use this for now:

  implementation 'com.github.pedroSG94.RootEncoder:library:73b7c9d43e'
  implementation 'com.github.pedroSG94.RootEncoder:extra-sources:73b7c9d43e'