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.55k stars 773 forks source link

Audio clicking noise on Android 12 and 12L API 31/32 #1044

Open rashid56156 opened 2 years ago

rashid56156 commented 2 years ago

Hello @pedroSG94 ,

I am having an audio clicking noise issue on Android 12 (API 32) only. It works fine on Android 11 and below. Tested it on multiple devices with Android 12. For example, Samsung S10 was working fine until it was on Android 11.

I am using RtmpCamera1 with OpenGlView. I am streaming to Youtube so here is a sample video. Focus on the first 12 seconds of the video.

https://youtu.be/gYWdV5gSYL8

I am using the latest version of the library. I can see the audio-video packets. It's just the clicking/popcorn sort of noise. I also tried

rtmpCamera1.setMicrophoneMode(MicrophoneMode.SYNC);

but the issue is still here. Any sort of help would be welcomed.

pedroSG94 commented 2 years ago

Can you tell me device used in video? It seems an emulator and if you indicate me emulator configuration and youtube stream configuration I can reproduce the problem

pedroSG94 commented 2 years ago

For now, I did a commit for it: https://github.com/pedroSG94/rtmp-rtsp-stream-client-java/commit/3b1f26b3d65a03074a5d6a2828ec37885ce8d568

Please, compile my app example using OpenGl RTMP and add microphone mode in this line BUFFER:

rtmpCamera1.setMicrophoneMode(MicrophoneMode.BUFFER);

Let me know if it is working for you.

rashid56156 commented 2 years ago

The device in the video is indeed an emulator running on API 32 but the issue can be reproduced on any physical device running on API 32.

Emulator Config: Pixel XL (5.5 1440 X 2560 560DPI) Android API 32 x86_64

Audio Config: Audio Bitrate: 128 Sample Rate: 44100 Channel: Stereo Echo Canceller: True Noise Suppress: True

Youtube Config:

   public static YouTube.LiveBroadcasts.Bind createLiveEvent(YouTube youtube, String location) throws IOException {   

   @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat(
            "MMM dd, h:mm a");
    dateFormat.setTimeZone(TimeZone.getDefault());
    long futureDateMillis = System.currentTimeMillis()
            + FUTURE_DATE_OFFSET_MILLIS;
    Date futureDate = new Date();
    futureDate.setTime(futureDateMillis);
    String date = dateFormat.format(futureDate);
    YouTube.LiveBroadcasts.Bind liveBroadcastBind = null;

    String name = "Test Name";
    String description = "Test Description";

    LiveBroadcastSnippet broadcastSnippet = new LiveBroadcastSnippet();
    broadcastSnippet.setTitle(name);
    broadcastSnippet.setScheduledStartTime(new DateTime(futureDate));

    LiveBroadcastContentDetails contentDetails = new LiveBroadcastContentDetails();
    MonitorStreamInfo monitorStream = new MonitorStreamInfo();
    monitorStream.setEnableMonitorStream(false);
    contentDetails.setMonitorStream(monitorStream);

    // Create LiveBroadcastStatus with privacy status.
    LiveBroadcastStatus status = new LiveBroadcastStatus();
    status.setPrivacyStatus("public");

    LiveBroadcast broadcast = new LiveBroadcast();
    broadcast.setKind("youtube#liveBroadcast");
    broadcast.setSnippet(broadcastSnippet);
    broadcast.setStatus(status);
    broadcast.setContentDetails(contentDetails);

    // Create the insert request
    YouTube.LiveBroadcasts.Insert liveBroadcastInsert = youtube
            .liveBroadcasts().insert("snippet,status,contentDetails",
                    broadcast);

    // Request is executed and inserted broadcast is returned
    LiveBroadcast returnedBroadcast = liveBroadcastInsert.execute();

    // Create a snippet with title.
    LiveStreamSnippet streamSnippet = new LiveStreamSnippet();
    streamSnippet.setTitle(name);
    streamSnippet.setDescription(description);
    streamSnippet.setPublishedAt(new DateTime(futureDate));

    // Create content distribution network with format and ingestion
    // type.
    CdnSettings cdn = new CdnSettings();
    cdn.set("resolution", "variable");
    cdn.set("frameRate", "variable");
    cdn.setIngestionType("rtmp");

    LiveStream stream = new LiveStream();
    stream.setKind("youtube#liveStream");
    stream.setSnippet(streamSnippet);
    stream.setCdn(cdn);

    // Create the insert request
    YouTube.LiveStreams.Insert liveStreamInsert = youtube.liveStreams()
            .insert("snippet,cdn", stream);

    // Request is executed and inserted stream is returned
    LiveStream returnedStream = liveStreamInsert.execute();

    // Create the bind request
    liveBroadcastBind = youtube
            .liveBroadcasts().bind(returnedBroadcast.getId(), "id,contentDetails");

    // Set stream id to bind
    liveBroadcastBind.setStreamId(returnedStream.getId());

    // Request is executed and bound broadcast is returned
    liveBroadcastBind.execute();

    return liveBroadcastBind;
}

Let me know if you need anything else.

rashid56156 commented 2 years ago

For now, I did a commit for it: 3b1f26b

Please, compile my app example using OpenGl RTMP and add microphone mode in this line BUFFER:

rtmpCamera1.setMicrophoneMode(MicrophoneMode.BUFFER);

Let me know if it is working for you.

@pedroSG94 Unfortunately, the issue is still there. I did as you said, took the updated code of your example app, used OpenGl RTMP, and added Buffer as Microphone mode.

Here is the video that I streamed for this test.

https://www.youtube.com/watch?v=7GbqPHoSQJ4

pedroSG94 commented 2 years ago

I can't reproduce the problem in Android emulator. I only have silence... Also, I haven't a device with API 32 (Android 12 v2) only API 31 (Android 12)

rashid56156 commented 2 years ago

@pedroSG94 By silence you mean no unwanted noise? Or do you mean the sound is muted? Make sure your mic is set up on emulator as "Virtual microphone use host audio input".

pedroSG94 commented 2 years ago

I did that but I can't input any sound to emulator so I think that my microphone is not detected. API 32 is android 12L right? It is still in beta. I will try add my Pixel 4a to the beta and test it.

rashid56156 commented 2 years ago

Yes, that is correct, 12L. I am going to do a test on API 31 as well and see if it works. You have to enable the microphone in emulator settings if your machine has a mic attached then it should work.

rashid56156 commented 2 years ago

@pedroSG94 It is reproducible on Android 12 API 31 as well.

pedroSG94 commented 2 years ago

I can't reproduce it on Android 12L APi 32 or Android 12 Api 31 using Pixel 4a. Maybe it isn't relevant but I'm using youtube studio to create stream and I use the endpoint provided in the web. I will try to input audio to emulator using Android studio on windows (I'm using Linux) and report back if I can reproduce the problem.

rashid56156 commented 2 years ago

Yes, I am doing the same to test when using your example app.

  1. Create RTMP based youtube stream from youtube studio
  2. Use it in the example app OpenGl RTMP
pedroSG94 commented 2 years ago

After install Android studio on windows and test it with emulators I can reproduce the audio problem but after a lot of test I can tell you that at least using emulator it isn't a Android version problem (I tested APIs 32, 31 and 30 with the same problem).

After all test, I can't reproduce the error in other devices with same APIs so I need a few info about this.

rashid56156 commented 2 years ago

Samsung S10 - Android 12 (Produces the audio problem) Samsung S10 - Android 11 (No audio problem) OnePlus 6 - Android 11 (No audio Problem)

Video recording: Let me do it and get back to you.

rashid56156 commented 2 years ago

@pedroSG94 I have tried asking a couple of people to do the local recording for me as I personally don't have an android 12 phone, They all tell me that they can't access the data folder where the example app is saving the video. One user on Samsung S10 and Other on Google Pixel, both reported that when they go to Internal Storage > Android > data

It's totally empty. While on my android 11 phone I can see the contents of the data folder as well as the local recording that the app is saving.

pedroSG94 commented 2 years ago

It is really weird. I have a device with android 12 and other device with andorid 12L and I can record a local video without problems so it isn't an android version problem. Maybe the library is not updating the gallery properly. Try to record more than 1min and reboot device to make sure that gallery can find the file.

Anyway, tomorrow I will have a Samsung s21 5G to test and I will try to reproduce this case. I'm not sure if I can reproduce the case since it is a different model but it has the same SO (Android 12 with samsung).

rashid56156 commented 2 years ago

@pedroSG94 Best of luck my friend. I am really hoping you reproduce the issue.

pedroSG94 commented 2 years ago

For few reasons. I'm not able to get the Samsung S21 5G so I decided buy the cheaper Samsung device to reproduce the problem. Maybe it is the Samsung S10 but I need comfirm this.

rashid56156 commented 2 years ago

I would say you can try it on any device that is on Android 12.

I was able to produce it on an Emulator as well. A friend of mine has a Pixel phone on Android 12 who tried to record the local video but could not find the file. Though from the save phone when he tried to stream to youtube, the issue was there in the stream.

In my country, most of the phones have not yet received the Android 12 update so I could not find many options to test.

pedroSG94 commented 2 years ago

Fixed problem recording a local video: https://github.com/pedroSG94/rtmp-rtsp-stream-client-java/commit/fcbf9ea82658d5ed11afa40cf81587d7d92ce60b

Previously the video was save correctly but you can't see it if you don't check it connecting device to PC. Maybe the privacy of that folder changed. Now, the videos are saved in Movies folder.

rashid56156 commented 2 years ago

ok let me take an update and share the update example app with my testers.

rashid56156 commented 2 years ago

@pedroSG94 So finally I am able to get hold of a Samsung S10 with android 12. Here are two local recording files. Plus I also did a stream to youtube using the example app. You know there is no audio captured at all. Not in the local recordings and not even in the stream. That is even weirder.

https://user-images.githubusercontent.com/7969092/152291234-453fc132-49bc-45dd-9042-66f52ef3c6de.mp4 https://user-images.githubusercontent.com/7969092/152291210-c9a90361-a6cc-4fb5-9c34-5d7058d79352.mp4

https://www.youtube.com/watch?v=U2tB1nqWG-Y

pedroSG94 commented 2 years ago

Check logcat to know if MicrophoneManager is working correctly and make sure that it is producing audio packets. I'm sure that is isn't a general problem on Android 12 because I have a Oneplus 7T (tested with android 12) and Google Pixel 4a (tested with android 12 and android 12L beta) but I can't reproduce the problem.

Also, I did a branch where the problem with android emulator is fixed: https://github.com/pedroSG94/rtmp-rtsp-stream-client-java/tree/a12audio

pedroSG94 commented 2 years ago

I have buy a samsung S10. It will take around 3 - 7 days to receive it and start tests.

rashid56156 commented 2 years ago

Check logcat to know if MicrophoneManager is working correctly and make sure that it is producing audio packets. I'm sure that is isn't a general problem on Android 12 because I have a Oneplus 7T (tested with android 12) and Google Pixel 4a (tested with android 12 and android 12L beta) but I can't reproduce the problem.

Also, I did a branch where the problem with android emulator is fixed: https://github.com/pedroSG94/rtmp-rtsp-stream-client-java/tree/a12audio

The issue is this phone is with a tester sort of person who doesn't have access to the code so i can't get the logs.

rashid56156 commented 2 years ago

I have buy a samsung S10. It will take around 3 - 7 days to receive it and start tests.

Thanks mate, we can wait a week no issue. I hope you get the phone soon.

pedroSG94 commented 2 years ago

Hello,

Yesterday, I received a Samsung S10 and I did test for it. After few test, I discovered that the problem is related with the codec selected. I did a branch with the fix: https://github.com/pedroSG94/rtmp-rtsp-stream-client-java/tree/samsungaudio Can you test it and comfirm me that it is working in your devices?

rashid56156 commented 2 years ago

Sure, let me test.

rashid56156 commented 2 years ago

@pedroSG94 Finally I heard back from my tester, he says the issue is fixed and there is no more clicking noise on his S10 with android 12. I had shared your example app with him.

The next step is to merge the fix with my existing code and test my app.

Thanks man, I confirm the fix is working :-)