google / ExoPlayer

This project is deprecated and stale. The latest ExoPlayer code is available in https://github.com/androidx/media
https://developer.android.com/media/media3/exoplayer
Apache License 2.0
21.75k stars 6.03k forks source link

SimpleExoPlayer audio fails after repeated use #9541

Open mike-targetr opened 3 years ago

mike-targetr commented 3 years ago

If SimpleExoPlayer is created, used and released many times, eventually audio stops working.

Audio in all apps then fails. Other apps that use ExoPlayer (e.g. YouTube) usually crash. The device must be rebooted to fix.

This appears to only affect Android 5+ devices, and may take thousands of repeats.

Affected devices: Nvidia Shield Some Minix devices

Usually the following exception is thrown:

Caused by: com.google.android.exoplayer2.ExoPlaybackException: MediaCodecAudioRenderer error, index=1, format=Format(null, null, null, audio/raw, null, -1, null, [-1, -1, -1.0], [1, 44100]), format_supported=YES
    at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:558)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    ... 2 more
Caused by: com.google.android.exoplayer2.audio.AudioSink$InitializationException: AudioTrack init failed 0 Config(44100, 4, 22050)
    at com.google.android.exoplayer2.audio.DefaultAudioSink$Configuration.buildAudioTrack(DefaultAudioSink.java:2041)
    at com.google.android.exoplayer2.audio.DefaultAudioSink.buildAudioTrack(DefaultAudioSink.java:854)
    at com.google.android.exoplayer2.audio.DefaultAudioSink.initializeAudioTrack(DefaultAudioSink.java:660)
    at com.google.android.exoplayer2.audio.DefaultAudioSink.handleBuffer(DefaultAudioSink.java:736)
    at com.google.android.exoplayer2.audio.MediaCodecAudioRenderer.processOutputBuffer(MediaCodecAudioRenderer.java:639)
    at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.drainOutputBuffer(MediaCodecRenderer.java:1898)
    at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:823)
    at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:978)
    at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:482)
    ... 3 more
    Suppressed: com.google.android.exoplayer2.audio.AudioSink$InitializationException: AudioTrack init failed 0 Config(44100, 4, 22050)
        ... 12 more

I believe the root cause is allocation of new audio session IDs in the SimpleExoPlayer constructor. This occurs before setAudioSessionId() can be used and is therefore unavoidable.

The best workaround is to reuse SimpleExoPlayer. However, this can pose problems when operating on different threads and recovering from errors.

mike-targetr commented 3 years ago

Unfortunately even reusing the same exoplayer instances ultimately fails after about 15,000 plays with audio related problems:

The error on Nvidia Shield is:

10-07 17:22:13.537 E/IAudioFlinger(27780): createTrack returned error -12
10-07 17:22:13.537 E/AudioTrack(27780): AudioFlinger could not create track, status: -12 output 1280723605
10-07 17:22:13.537 E/AudioTrack-JNI(27780): Error -12 initializing AudioTrack
10-07 17:22:13.537 D/AudioTrack(27780): no metrics gathered, track status=-12
10-07 17:22:13.537 E/android.media.AudioTrack(27780): Error code -20 when initializing AudioTrack.
10-07 17:22:13.538 E/MediaCodecAudioRenderer(27780): Audio sink error
10-07 17:22:13.538 E/MediaCodecAudioRenderer(27780):   com.google.android.exoplayer2.audio.AudioSink$InitializationException: AudioTrack init failed 0 Config(48000, 12, 48000)
10-07 17:22:13.538 E/MediaCodecAudioRenderer(27780):       at com.google.android.exoplayer2.audio.DefaultAudioSink$Configuration.buildAudioTrack(DefaultAudioSink.java:2041)
10-07 17:22:13.538 E/MediaCodecAudioRenderer(27780):       at com.google.android.exoplayer2.audio.DefaultAudioSink.buildAudioTrack(DefaultAudioSink.java:854)
10-07 17:22:13.538 E/MediaCodecAudioRenderer(27780):       at com.google.android.exoplayer2.audio.DefaultAudioSink.initializeAudioTrack(DefaultAudioSink.java:660)
10-07 17:22:13.538 E/MediaCodecAudioRenderer(27780):       at com.google.android.exoplayer2.audio.DefaultAudioSink.handleBuffer(DefaultAudioSink.java:736)
10-07 17:22:13.538 E/MediaCodecAudioRenderer(27780):       at com.google.android.exoplayer2.audio.MediaCodecAudioRenderer.processOutputBuffer(MediaCodecAudioRenderer.java:639)
10-07 17:22:13.538 E/MediaCodecAudioRenderer(27780):       at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.drainOutputBuffer(MediaCodecRenderer.java:1899)
10-07 17:22:13.538 E/MediaCodecAudioRenderer(27780):       at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:823)
10-07 17:22:13.538 E/MediaCodecAudioRenderer(27780):       at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:978)
10-07 17:22:13.538 E/MediaCodecAudioRenderer(27780):       at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:482)
10-07 17:22:13.538 E/MediaCodecAudioRenderer(27780):       at android.os.Handler.dispatchMessage(Handler.java:102)
10-07 17:22:13.538 E/MediaCodecAudioRenderer(27780):       at android.os.Looper.loop(Looper.java:193)
10-07 17:22:13.538 E/MediaCodecAudioRenderer(27780):       at android.os.HandlerThread.run(HandlerThread.java:65)

The video and audio work fine for first 15K+ plays.

Could anyone offer any advice on achieving stability for repetitive use?

andrewlewis commented 3 years ago

If you comment out the code in the SimpleExoPlayer constructor that generates an audio session ID (and remove usages of it) does the problem go away? I'm just trying to eliminate the possibility that the root cause is something different.

The best workaround is to reuse SimpleExoPlayer. However, this can pose problems when operating on different threads and recovering from errors.

Can you share any specifics? It seems like the best way to avoid this problem.

Do the lifetimes of the player instances overlap?

mike-targetr commented 3 years ago

As above, I also had audio problems when reusing players and so it is not the allocation of audio session IDs (since I only allocated 3 in total). - This seemed to be the most logical assumption that could affect other apps that play audio (such as YouTube), but I was wrong, its deeper.

The lifetime of the instances do overlap. Basically I prepare the next exoplayer just before the current exoplayer finishes. The exoplayers video output to independent textures and opengl fragment shaders are used to transition between videos, images and web pages.

It is really hard triggering this problem as it takes over 24 hours of continuously playing 5 second videos. If we disable the audio renderer everything works flawlessly.

So there seems to be a "leak" regarding audio decoding somewhere. - Eventually audio fails everywhere and a reboot is necessary.

My latest attempt to resolve this is to set setRendererDisabled(C.TRACK_TYPE_AUDIO, true) at the end of playback and enable again when I reuse the exoplayer. Fingers crossed this helps.

andrewlewis commented 3 years ago

You might want to monitor adb shell dumpsys media.audio_flinger during this playback and look whether the number of allocated AudioTracks is exceeding some threshold when the error happens. I think each track consumes a piece of some (relatively small) shared memory buffer so one possible explanation is that this is not a small leak but instead it's a race condition that just takes a long time to trigger.

mike-targetr commented 3 years ago

This output looks really useful. Amongst the reams of info I see: 2 Tracks of which 1 are active which sounds spot on given I only recently started the app. I'll keep an eye on this.

Is there anyway of forcefully destroying inactive Audio Tracks? I have been rebooting, but I believe entering standby and returning works too.

What is particularly nasty about this issue is it prevents audio working everywhere. Not just 1 app. I'm fairly confident I'm reusing components properly, seems strange it is only audio related.

mike-targetr commented 3 years ago

After almost exactly 24 hours exoplayer started failing again. I now see.

40 Tracks of which 40 are active

I checked multiple times over last 24 hours and there were never more than 2 tracks. Very strange that this would jump to 40.

A way of destroying audio tracks could be a possible work around. - disabling and enabling audio renderers had no effect.

andrewlewis commented 3 years ago

Very strange that this would jump to 40.

Agreed.

A way of destroying audio tracks could be a possible work around. - disabling and enabling audio renderers had no effect.

[AudioTrack.release](https://developer.android.com/reference/android/media/AudioTrack#release()) should be sufficient. You could add logging in DefaultAudioSink where an AudioTrack is instantiated and where it's released (before and after the call to release), to verify that the number of reported instances matches between the dumpsys and the logging.

mike-targetr commented 3 years ago

Failed again after 24 hours. I used completely different videos with different durations.

Something I noticed:

When everything is fine:

      2 Tracks of which 1 are active
T Name Active Client Session S  Flags   Format Chn mask  SRate ST  L dB  R dB  VS dB   Server FrmCnt  FrmRdy F Underruns  Flushed   Latency
     0    yes  15507    5745 A  0x000 00000001 00000003  44100  3     0     0     0  001263AC  11025   11025 A         0        0  351.68 t
     1     no  15507    5745 I  0x000 00000001 00000003  44100  3     0     0     0  00000000  11025   11025 I         0        0       new

After the AudioTrack: AudioFlinger could not create track, status: -12 output 1334733319 errors:

      40 Tracks of which 40 are active
T Name Active Client Session S  Flags   Format Chn mask  SRate ST  L dB  R dB  VS dB   Server FrmCnt  FrmRdy F Underruns  Flushed   Latency
     1    yes  15507    5745 T  0x200 00000001 00000003  44100  3     0     0     0  00077400  11025   11025 A         0        0 -1323027.41 t
    21    yes  15507    5745 T  0x200 00000001 00000003  44100  3     0     0     0  00077400  11025       0 A         0        0 -717414.15 t
     4    yes  15507    5745 T  0x200 00000001 00000003  44100  3     0     0     0  000F7800  11025   11025 A         0        0 -1221899.45 t
    29    yes  15507    5745 T  0x200 00000001 00000003  44100  3     0     0     0  00143400  11025   11025 A         0        0 -477960.95 t
    30    yes  15507    5745 T  0x200 00000001 00000003  44100  3     0     0     0  00226C00  11025   11025 A         0        0 -425417.21 t
     0    yes  15507    5745 T  0x200 00000001 00000003  44100  3     0     0     0  00226C00  11025   11025 A         0        0 -1334220.11 t
     9    yes  15507    5745 T  0x200 00000001 00000003  44100  3     0     0     0  00143400  11025   11025 A         0        0 -1082950.07 t
    10    yes  15507    5745 T  0x200 00000001 00000003  44100  3     0     0     0  00226C00  11025   11025 A         0        0 -1031264.46 t
...

The large negative values in latency column look very wrong. Maybe some weird 24 hour wrap around problem?

This is not happening on other test devices. Only Nvidia Shield.

Sorry, I have not added extra logging yet since I am not building exoplayer from scratch and making changes to it is not trivial.

fenjuly commented 2 years ago

I had faced the same issue. Have you solved the issue? @mike-targetr

mike-targetr commented 2 years ago

Unfortunately not. The only workarounds we found were:

  1. Put the device into standby and wake up at least once every 24 hours
  2. Disable the audio renderer completely

Thankfully most of our customers use our player for muted digital signage. But we no longer recommend Nvidia Shield due to this audio problem.

Sternbach-Software commented 1 year ago

Same issue.

fenjuly commented 1 year ago

My solution for the AudioTrack init error is that when I encounter this type of error, I try to use OpenSL instead of AudioTrack for audio playback. Therefore, I re-implemented an AudioSink interface using OpenSL

wisdomtl commented 1 year ago

Very strange that this would jump to 40.

Agreed.

A way of destroying audio tracks could be a possible work around. - disabling and enabling audio renderers had no effect.

[AudioTrack.release](https://developer.android.com/reference/android/media/AudioTrack#release()) should be sufficient. You could add logging in DefaultAudioSink where an AudioTrack is instantiated and where it's released (before and after the call to release), to verify that the number of reported instances matches between the dumpsys and the logging.

how to get instance of AudioTrack in ExoPlayer 2.18.5

adham-n4a7 commented 1 year ago

Hi, I'm experiencing the same issue.

My ExoPlayer core VERSION_SLASHY is ExoPlayerLib/2.18.1 and ExoPlayer core version is 2.18.1.

Is there any update on this issue? Has it been fixed in any of the ExoPlayer releases?

Below are the logs:

 E AudioTrack-JNI: Error -32 during AudioTrack native read
 E android.media.AudioTrack: AudioTrack.write() could not write audio data!
 E MediaCodecAudioRenderer: Audio sink error
 E MediaCodecAudioRenderer:   com.google.android.exoplayer2.audio.AudioSink$WriteException: AudioTrack write failed: -6
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink.writeBuffer(DefaultAudioSink.java:1174)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink.processBuffers(DefaultAudioSink.java:1098)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink.handleBuffer(DefaultAudioSink.java:1030)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.MediaCodecAudioRenderer.processOutputBuffer(MediaCodecAudioRenderer.java:703)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.drainOutputBuffer(MediaCodecRenderer.java:1894)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:792)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:999)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:500)
 E MediaCodecAudioRenderer:       at android.os.Handler.dispatchMessage(Handler.java:102)
 E MediaCodecAudioRenderer:       at android.os.Looper.loop(Looper.java:223)
 E MediaCodecAudioRenderer:       at android.os.HandlerThread.run(HandlerThread.java:67)
 W ExoPlayerImplInternal: Recoverable renderer error
 W ExoPlayerImplInternal:   com.google.android.exoplayer2.ExoPlaybackException: MediaCodecAudioRenderer error, index=1, format=Format(dxADIeIBnw.., null, null, audio/mp4a-latm, mp4a.40.2, 576000, en, [-1, -1, -1.0], [2, 48000]), format_supported=YES
 W ExoPlayerImplInternal:       at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:576)
 W ExoPlayerImplInternal:       at android.os.Handler.dispatchMessage(Handler.java:102)
 W ExoPlayerImplInternal:       at android.os.Looper.loop(Looper.java:223)
 W ExoPlayerImplInternal:       at android.os.HandlerThread.run(HandlerThread.java:67)
 W ExoPlayerImplInternal:   Caused by: com.google.android.exoplayer2.audio.AudioSink$WriteException: AudioTrack write failed: -6
 W ExoPlayerImplInternal:       at com.google.android.exoplayer2.audio.DefaultAudioSink.writeBuffer(DefaultAudioSink.java:1174)
 W ExoPlayerImplInternal:       at com.google.android.exoplayer2.audio.DefaultAudioSink.processBuffers(DefaultAudioSink.java:1098)
 W ExoPlayerImplInternal:       at com.google.android.exoplayer2.audio.DefaultAudioSink.handleBuffer(DefaultAudioSink.java:1030)
 W ExoPlayerImplInternal:       at com.google.android.exoplayer2.audio.MediaCodecAudioRenderer.processOutputBuffer(MediaCodecAudioRenderer.java:703)
 W ExoPlayerImplInternal:       at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.drainOutputBuffer(MediaCodecRenderer.java:1894)
 W ExoPlayerImplInternal:       at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:792)
 W ExoPlayerImplInternal:       at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:999)
 W ExoPlayerImplInternal:       at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:500)
 W ExoPlayerImplInternal:       ... 3 more
 E IAudioFlinger: createTrack returned error -38
 E AudioTrack: createTrack_l(165): AudioFlinger could not create track, status: -38 output 0
 E AudioTrack-JNI: Error -38 initializing AudioTrack
 D AudioTrack: gather(): no metrics gathered, track status=-38
 E android.media.AudioTrack: Error code -20 when initializing AudioTrack.
 E MediaCodecAudioRenderer: Audio sink error
 E MediaCodecAudioRenderer:   com.google.android.exoplayer2.audio.AudioSink$InitializationException: AudioTrack init failed 0 Config(48000, 12, 61568)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink$Configuration.buildAudioTrack(DefaultAudioSink.java:2092)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink.buildAudioTrack(DefaultAudioSink.java:1070)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink.buildAudioTrackWithRetry(DefaultAudioSink.java:1049)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink.initializeAudioTrack(DefaultAudioSink.java:846)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink.handleBuffer(DefaultAudioSink.java:929)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.MediaCodecAudioRenderer.processOutputBuffer(MediaCodecAudioRenderer.java:703)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.drainOutputBuffer(MediaCodecRenderer.java:1894)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:792)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:999)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:500)
 E MediaCodecAudioRenderer:       at android.os.Handler.dispatchMessage(Handler.java:102)
 E MediaCodecAudioRenderer:       at android.os.Looper.loop(Looper.java:223)
 E MediaCodecAudioRenderer:       at android.os.HandlerThread.run(HandlerThread.java:67)
 E MediaCodecAudioRenderer:   Caused by: java.lang.UnsupportedOperationException: Cannot create AudioTrack
 E MediaCodecAudioRenderer:       at android.media.AudioTrack$Builder.build(AudioTrack.java:1277)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink$Configuration.createAudioTrackV29(DefaultAudioSink.java:2141)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink$Configuration.createAudioTrack(DefaultAudioSink.java:2119)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink$Configuration.buildAudioTrack(DefaultAudioSink.java:2084)
 E MediaCodecAudioRenderer:       ... 12 more
 E IAudioFlinger: createTrack returned error -38
 E AudioTrack: createTrack_l(165): AudioFlinger could not create track, status: -38 output 0
 E AudioTrack-JNI: Error -38 initializing AudioTrack
 D AudioTrack: gather(): no metrics gathered, track status=-38
 E android.media.AudioTrack: Error code -20 when initializing AudioTrack.
 E MediaCodecAudioRenderer: Audio sink error
 E MediaCodecAudioRenderer:   com.google.android.exoplayer2.audio.AudioSink$InitializationException: AudioTrack init failed 0 Config(48000, 12, 61568)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink$Configuration.buildAudioTrack(DefaultAudioSink.java:2092)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink.buildAudioTrack(DefaultAudioSink.java:1070)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink.buildAudioTrackWithRetry(DefaultAudioSink.java:1049)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink.initializeAudioTrack(DefaultAudioSink.java:846)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink.handleBuffer(DefaultAudioSink.java:929)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.MediaCodecAudioRenderer.processOutputBuffer(MediaCodecAudioRenderer.java:703)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.drainOutputBuffer(MediaCodecRenderer.java:1894)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:792)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:999)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:500)
 E MediaCodecAudioRenderer:       at android.os.Handler.dispatchMessage(Handler.java:102)
 E MediaCodecAudioRenderer:       at android.os.Looper.loop(Looper.java:223)
 E MediaCodecAudioRenderer:       at android.os.HandlerThread.run(HandlerThread.java:67)
 E MediaCodecAudioRenderer:   Caused by: java.lang.UnsupportedOperationException: Cannot create AudioTrack
 E MediaCodecAudioRenderer:       at android.media.AudioTrack$Builder.build(AudioTrack.java:1277)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink$Configuration.createAudioTrackV29(DefaultAudioSink.java:2141)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink$Configuration.createAudioTrack(DefaultAudioSink.java:2119)
 E MediaCodecAudioRenderer:       at com.google.android.exoplayer2.audio.DefaultAudioSink$Configuration.buildAudioTrack(DefaultAudioSink.java:2084)
 E MediaCodecAudioRenderer:       ... 12 more
 E IAudioFlinger: createTrack returned error -38

Thanks!

adham-n4a7 commented 1 year ago

Hi @andrewlewis @mike-targetr

Any solution for this issue ?

Whenever we disconnect and reconnect any HDMI or Bluetooth device 'Audio sink error' is encountered. As mentioned above our ExoPlayer version is 2.18.1

andrewlewis commented 1 year ago

I think it's expected that the audio output path gets invalidated and audio track throws when the output device is disconnected, but I think there's code already in that version to take care of retrying creating the audio track after a delay.

@tianyif Please could you take a look? Thanks.

tianyif commented 1 year ago

@adham-n4a7,

We have the fix for this and I think the first available release is 2.19.0, could you please upgrade your ExoPlayer version and try?

As @andrewlewis already mentioned, the AudioTrack got invalidated while the output device is disconnected / connected, and the output device change is also very likely to introduce the audio capabilities change. For example, the Bluetooth headphone just supports stereo PCM, if the output was initially from the TV speaker and then you connect the Bluetooth headphone and the audio tries to output from it, the audio capabilities may change from a large set of supported encodings (audio capabilities of your TV) to stereo PCM only. In the version of 2.18.1 that your are using, we already treat it as a recoverable error and retry the AudioTrack creation (your first piece of log), but at that time, while the AudioTrack is re-created, the DefaultAudioSink is still using the stale audio capabilities, and the AudioTrack creation will fail because of it. In the latest fix, we will use the updated audio capabilities.

adham-n4a7 commented 1 year ago

Thanks @tianyif for your inputs.

We are also facing another issue, where audio is stopping and failing video playback scenario 1 : when in long run multiple audio's are played , scenario 2 : when the single audio is in long run

we are unable to conclude why this audio track sink error is occurring ,

Logs :

IAudioFlinger           E  createTrack returned error -38
AudioTrack              E  createTrack_l(0): AudioFlinger could not create track, status: -38 output 0
AudioTrack-JNI          E  Error -38 initializing AudioTrack
AudioTrack              D  gather(): no metrics gathered, track status=-38
android.me...AudioTrack E  Error code -20 when initializing AudioTrack.
MediaCodecAudioRenderer E  Audio sink error
                                                                                                      com.google.android.exoplayer2.audio.AudioSink$InitializationException: AudioTrack init failed 0 Config(48000, 252, 191998)
                                                                                                          at com.google.android.exoplayer2.audio.DefaultAudioSink$f.a(DefaultAudioSink.java:7)
                                                                                                          at com.google.android.exoplayer2.audio.DefaultAudioSink.q(DefaultAudioSink.java:1)
                                                                                                          at com.google.android.exoplayer2.audio.DefaultAudioSink.y(DefaultAudioSink.java:4)
                                                                                                          at com.google.android.exoplayer2.audio.DefaultAudioSink.j(DefaultAudioSink.java:18)
                                                                                                          at com.google.android.exoplayer2.audio.h.processOutputBuffer(MediaCodecAudioRenderer.java:8)
                                                                                                          at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.drainOutputBuffer(MediaCodecRenderer.java:32)
                                                                                                          at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:16)
                                                                                                          at com.google.android.exoplayer2.m.e(ExoPlayerImplInternal.java:144)
                                                                                                          at com.google.android.exoplayer2.m.handleMessage(ExoPlayerImplInternal.java:29)
                                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                          at android.os.Looper.loop(Looper.java:223)
                                                                                                          at android.os.HandlerThread.run(HandlerThread.java:67)
                                                                                                      Caused by: java.lang.UnsupportedOperationException: Cannot create AudioTrack
                                                                                                          at android.media.AudioTrack$Builder.build(AudioTrack.java:1277)
                                                                                                          at com.google.android.exoplayer2.audio.DefaultAudioSink$f.b(DefaultAudioSink.java:12)
                                                                                                          at com.google.android.exoplayer2.audio.DefaultAudioSink$f.a(DefaultAudioSink.java:1)
                                                                                                          at com.google.android.exoplayer2.audio.DefaultAudioSink.q(DefaultAudioSink.java:1) 
                                                                                                          at com.google.android.exoplayer2.audio.DefaultAudioSink.y(DefaultAudioSink.java:4) 
                                                                                                          at com.google.android.exoplayer2.audio.DefaultAudioSink.j(DefaultAudioSink.java:18) 
                                                                                                          at com.google.android.exoplayer2.audio.h.processOutputBuffer(MediaCodecAudioRenderer.java:8) 
                                                                                                          at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.drainOutputBuffer(MediaCodecRenderer.java:32) 
                                                                                                          at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:16) 
                                                                                                          at com.google.android.exoplayer2.m.e(ExoPlayerImplInternal.java:144) 
                                                                                                          at com.google.android.exoplayer2.m.handleMessage(ExoPlayerImplInternal.java:29) 
                                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                                          at android.os.Looper.loop(Looper.java:223) 
                                                                                                          at android.os.HandlerThread.run(HandlerThread.java:67) 
IAudioFlinger            E  createTrack returned error -38
tianyif commented 1 year ago

Hi @adham-n4a7,

For the above issue, do you see this when using 2.19.0?

adham-n4a7 commented 1 year ago

@tianyif , actually no, with same 2.18.1 version of exoplayer, this error occurred in different scenario

tianyif commented 1 year ago

@adham-n4a7, thanks for confirmation! Yes I understand that the last issue above is in a different scenario than the output invalidation, but I would still recommend you to try on the latest ExoPlayer version as it could be fixed along with the iteration. Please let me know if the issue still persist, then we can have an investigation on this un-addressed problem.