RenderHeads / UnityPlugin-AVProVideo

AVPro Video is a multi-platform Unity plugin for advanced video playback
https://www.renderheads.com/products/avpro-video/
233 stars 28 forks source link

No sound with bluetooth when Unity output on Android #1684

Closed dw8869 closed 9 months ago

dw8869 commented 10 months ago

No sound with bluetooth when Unity output on Android

I tested Demo_MediaPlayer scene. Set the AudioOutput component and changed the AudioOutput to Unity. When playing video on speakerphone, there is no sound when connected to the Bluetooth headset.

If you test it in the demo scene, you can reproduce it right away.

Ste-RH commented 10 months ago

Have you tried another device and/or other headphones? I have just tested here using some generic bluetooth headphones paired to a Xiaomi Mi10T and it works exactly as one would expect.

Do you know the samplerate that is set when using your headphones? Maybe this is a resampling bug (if they are not 48KHz - I have seen headphones that set the system to 22.5KHz in the past).

Edit: Send over a full, unfiltered log so we can have a look to see if anything is reported as odd. Just filtered my log here by 'samplerate' and 48000 is prevalent.

dw8869 commented 10 months ago

All I have is AirPods and Galaxy Buds 2. This devices sample rate is 44100. Can you solve this problem?

If you experience a problem during the Bluetooth connection process, the problem occurs even after you disable Bluetooth. Unless you run the app again, the problem cannot be resolved.

Chris-RH commented 10 months ago

It could be that. Have you tested using audio with the same sample rate? Also, have you tested other android devices? It does appear that Samsung 22+/Android 13 has bluetooth issues for some users

Could you send us a full, unfiltered logcat please :)

dw8869 commented 10 months ago

I don't have another Bluetooth device. Here is Samsung 22+/Android 13 log. logs.txt

I will test it on other devices and the OS and deliver additional logs.

dw8869 commented 10 months ago

Problems also occur with Galaxy S10 5G / Android 12. logs2.txt

Ste-RH commented 10 months ago

Nothing looks odd in those logs :/

I wonder, can you log out AudioConfiguration.sampleRate

...when you have the headphones connected, and also when you don't...and report back the values you get?

dw8869 commented 10 months ago

@Ste-RH Ok, I tried it, but the sample rate is not changing. You can find it by looking at the log Current AudioSettings.outputSampleRate

logs3.txt The first log is before Bluetooth connection. The second log is after a Bluetooth connection. The third log is after the Bluetooth disconnect.

Ste-RH commented 10 months ago

Ok, good to know.

Do you get the problem if you use sample video (RenderHeads-1080p30-H264.mp4) ?

Does audio come out the headphones from the OS, other applications?

dw8869 commented 10 months ago

@Ste-RH Yes, there is no problem with other applications.

The same problem is occurring with the sample video that you informed me. And I just found out that when there's no sound, Pause-Play comes out the sound.

Ste-RH commented 10 months ago

I am a little lost here. Are you saying you never get audio into your bluetooth headphones? Or only after an issue occurs pairing them?

dw8869 commented 10 months ago

@Ste-RH First, if you play a video without Bluetooth, the speakerphone will comes out the sound. And when you connect Bluetooth, there is no sound from Bluetooth.

Ste-RH commented 10 months ago

And if you re-run the app after bluetooth has been enabled and the headphones connected?

dw8869 commented 10 months ago

@Ste-RH If you're already connected to Bluetooth when running the app, there's no problem. comes out the sound.

dw8869 commented 10 months ago

@Ste-RH Can I get the solved issue version from the next version?

dw8869 commented 10 months ago

@Ste-RH any update?

dw8869 commented 10 months ago

@Chris-RH Do you have any updates on this issue?

Chris-RH commented 10 months ago

no update as of yet

dw8869 commented 10 months ago

@Chris-RH Is it in progress? I hope it will be fixed in the next version.

dw8869 commented 9 months ago

@Chris-RH

@Ste-RH has been 3 weeks, but there is no answer. Why?

This problem is adversely affecting our service, and we need to fix it. I'd like to know the progress.

Ste-RH commented 9 months ago

It has only been two weeks (edit: 11 days, so not even two weeks) since we last responded. As there is nothing to report yet, we have not. Would you rather we spend some of our time updating issues with 'nothing extra to report yet' instead of looking into then?

Ste-RH commented 9 months ago

So far, we have tracked it all the way back into Unity. In the AudioOutput class 'OnAudioFilterRead' stops being called when the bluetooth headset is connected mid video playback. This is what is used to feed audio samples to the Unity audio engine from the AVPro Video plugin. Unity is entirely responsible for the call of this function.

The only reference I can find as to why Unity might stop calling this function is: https://issuetracker.unity3d.com/issues/android-audioclip-stops-playing-when-connecting-the-bluetooth-earbuds-slash-earphones

So, unfortunately, this looks extremely likely that this is actually a Unity issue (due to hardware ultimately), and not anything related to AVPro Video. As such, we cannot offer a fix.

Side note. We found that if you close and re-open the video after a bluetooth connection, you get audio from the video in the bluetooth headphones (as desired). If you look at the callback AudioOutput -> void OnAudioConfigurationChanged(bool deviceChanged) ...then you could catch a change in device here (we have checked this callback fires on connecting a bluetooth headset whilst a video is playing) and close/open the video again.

Ste-RH commented 9 months ago

Additional:

Something like this appears to work:

    void OnAudioConfigurationChanged(bool deviceChanged)
    {
        if (_mediaPlayer == null || _mediaPlayer.Control == null)
            return;
        _mediaPlayer.Control.AudioConfigurationChanged(deviceChanged);

        double currentTime = _mediaPlayer.Control.GetCurrentTime();
        _mediaPlayer.Control.CloseMedia();
        _mediaPlayer.OpenMedia( _mediaPlayer.MediaReference, true );
        _mediaPlayer.Control.Seek( currentTime );
    }

You might want to check/set pause as well, depending on your use-case.

dw8869 commented 8 months ago
void OnAudioConfigurationChanged(bool deviceChanged)
{
    if (_mediaPlayer == null || _mediaPlayer.Control == null)
        return;
    _mediaPlayer.Control.AudioConfigurationChanged(deviceChanged);

    double currentTime = _mediaPlayer.Control.GetCurrentTime();
    _mediaPlayer.Control.CloseMedia();
    _mediaPlayer.OpenMedia( _mediaPlayer.MediaReference, true );
    _mediaPlayer.Control.Seek( currentTime );
}

@Ste-RH so.. I solved the issue with the code you told me, but there was a side effect issue. When the player plays again, the audio is output later than the video and is out of sync.

I think the early return of the RequestAudio function of the AudioOutputManager class has an impact.

public void RequestAudio(AudioOutput outputComponent, MediaPlayer mediaPlayer, float[] audioData, int audioChannelCount, int channelMask, AudioOutput.AudioOutputMode audioOutputMode, bool supportPositionalAudio)
{
    if (mediaPlayer == null || mediaPlayer.Control == null)
    {
        if (supportPositionalAudio)
        {
            ZeroAudio(audioData, 0);
        }
        return;
    }

I think we can just remove the buffered audio, is there a way?