AudioTrack的内存泄漏

时间:2019-03-15 07:39:20

标签: android mediacodec audiotrack

当一起使用AudioTrack和MediaSync时,我遇到了一个严重的内存泄漏问题。在我看来,问题在于AudioTrack不会释放某些本机资源。结果,该应用只能运行几次,此后,由于不再有可用的曲目,因此无法创建AudioTrack。

下面是一个导致内存泄漏的简短示例。完整项目可以在GitHub上here下载。 APK文件可以下载here

final MediaSync mediaSync = new MediaSync();
mediaSync.setSurface(mSurface);
final Surface inputSurface = mediaSync.createInputSurface(); // There is no the memory leak if I don't create this input surface.

final AudioTrack audioTrack = new AudioTrack.Builder()
        .setAudioAttributes(new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MOVIE)
                .build())
        .setAudioFormat(new AudioFormat.Builder()
                .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
                .setSampleRate(48000)
                .setChannelMask(12)
                .build())
        .build();
mediaSync.setAudioTrack(audioTrack); // There is no the memory leak if I don't set AudioTrack.

mediaSync.release();
inputSurface.release();
audioTrack.release();

我通过以下方式重现该问题:

  1. 运行应用。
  2. 按“主页”按钮,再次运行。重复大约14次,然后得到错误消息。

Logcat:

2019-03-15 09:19:57.313 239-15387/? E/AudioFlinger: no more track names available
2019-03-15 09:19:57.313 239-15387/? E/AudioFlinger: createTrack_l() initCheck failed -12; no control block?
2019-03-15 09:19:57.313 3413-3413/com.audiotrackmemoryleak E/AudioTrack: AudioFlinger could not create track, status: -12
2019-03-15 09:19:57.313 3413-3413/com.audiotrackmemoryleak E/AudioTrack-JNI: Error -12 initializing AudioTrack
2019-03-15 09:19:57.313 3413-3413/com.audiotrackmemoryleak E/android.media.AudioTrack: Error code -20 when initializing AudioTrack.
2019-03-15 09:19:57.315 3413-3413/com.audiotrackmemoryleak D/AndroidRuntime: Shutting down VM
2019-03-15 09:19:57.316 3413-3413/com.audiotrackmemoryleak E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.audiotrackmemoryleak, PID: 3413
    java.lang.UnsupportedOperationException: Cannot create AudioTrack
        at android.media.AudioTrack$Builder.build(AudioTrack.java:776)
        at com.audiotrackmemoryleak.MainActivity.createMediaSync(MainActivity.java:68)
        at com.audiotrackmemoryleak.MainActivity.access$100(MainActivity.java:18)
        at com.audiotrackmemoryleak.MainActivity$1.surfaceCreated(MainActivity.java:32)
        at android.view.SurfaceView.updateWindow(SurfaceView.java:618)
        at ...

命令adb shell dumpsys media.audio_flinger演示了该问题:

...
Clients:             
  pid: 3413          
Notification Clients:
  pid: 239           
  pid: 841           
  pid: 3413          
  pid: 28651         
Global session refs: 
  session   pid count
     3193  3413     1
     3201  3413     1
     3209  3413     1
     3217  3413     1
     3225  3413     1
     3233  3413     1
     3241  3413     1
     3249  3413     1
     3257  3413     1
     3265  3413     1
     3273  3413     1
     3281  3413     1
     3289  3413     1
     3297  3413     1
...
          14 Tracks of which 0 are active
    Name Active Client Type      Fmt Chn mask Session fCount S F SRate  L dB  R dB    Server Main buf  Aux Buf Flags UndFrmCnt
       7     no   3413    3 00000001 00000003    3249   1924 I 0 48000     0     0  00000000 0xa77fe000 0x0 0x000         0
       5     no   3413    3 00000001 00000003    3233   1924 I 0 48000     0     0  00000000 0xa77fe000 0x0 0x000         0
      12     no   3413    3 00000001 00000003    3289   1924 I 0 48000     0     0  00000000 0xa77fe000 0x0 0x000         0
       3     no   3413    3 00000001 00000003    3217   1924 I 0 48000     0     0  00000000 0xa77fe000 0x0 0x000         0
       8     no   3413    3 00000001 00000003    3257   1924 I 0 48000     0     0  00000000 0xa77fe000 0x0 0x000         0
       9     no   3413    3 00000001 00000003    3265   1924 I 0 48000     0     0  00000000 0xa77fe000 0x0 0x000         0
       4     no   3413    3 00000001 00000003    3225   1924 I 0 48000     0     0  00000000 0xa77fe000 0x0 0x000         0
       1     no   3413    3 00000001 00000003    3201   1924 I 0 48000     0     0  00000000 0xa77fe000 0x0 0x000         0
      11     no   3413    3 00000001 00000003    3281   1924 I 0 48000     0     0  00000000 0xa77fe000 0x0 0x000         0
       0     no   3413    3 00000001 00000003    3193   1924 I 0 48000     0     0  00000000 0xa77fe000 0x0 0x000         0
       6     no   3413    3 00000001 00000003    3241   1924 I 0 48000     0     0  00000000 0xa77fe000 0x0 0x000         0
      13     no   3413    3 00000001 00000003    3297   1924 I 0 48000     0     0  00000000 0xa77fe000 0x0 0x000         0
       2     no   3413    3 00000001 00000003    3209   1924 I 0 48000     0     0  00000000 0xa77fe000 0x0 0x000         0
      10     no   3413    3 00000001 00000003    3273   1924 I 0 48000     0     0  00000000 0xa77fe000 0x0 0x000         0
  0 Effect Chains
...

我想知道这里是否有人可以解释发生了什么?如何正确释放AudioTrack?

1 个答案:

答案 0 :(得分:4)

我不确定这是否是SDK错误,但与AudioAttributes.FLAG_DEEP_BUFFER有关。通过构建器构造AudioTrack时,默认情况下会设置此标志。查看您的情况hereshouldEnablePowerSaving()的检查返回true,并且切换情况导致PERFORMANCE_MODE_POWER_SAVING启用FLAG_DEEP_BUFFER

要解决此问题,您应该禁用此标志,例如,将.setFlags(AudioAttributes.FLAG_LOW_LATENCY)调用添加到AudioAttributes,但是它需要至少24 SDK。否则,您可以完全放弃使用AudioTrack.Builder,并且像这样构造轨道:

    int audioSampleRate = 48000;
    int channelConfig = AudioFormat.CHANNEL_OUT_STEREO;
    int bufSize = AudioTrack.getMinBufferSize(audioSampleRate, channelConfig, AudioFormat.ENCODING_PCM_16BIT);

    AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, audioSampleRate,
            channelConfig, AudioFormat.ENCODING_PCM_16BIT, bufSize, AudioTrack.MODE_STREAM);

或者您可以检查the code of shouldEnablePowerSaving() check并使其不通过其他任何方式。

更新: 因此,上述解决方案只是将漏洞转移到另一个音频线程。我进行了进一步调查,发现surfaceCreated()将同一个Surface对象放在我的android 8设备上。实际上,this is the correct behaviour since android 7。我认为它以某种方式破坏了mediaSync表面逻辑:如果您删除对mediaSync.createInputSurface()的调用并在释放它之前添加mediaSync.setSurface(null)调用,则泄漏将消失。

我不知道如何解决此问题,因为Surface被系统重用,并且无法知道何时将其实际销毁。我建议切换到TextureView,它具有相似但更清晰的API,并且不会在活动暂停时破坏表面。您需要从createMediaSync()中删除onResume()调用,并像这样使用它:

setContentView(R.layout.activity_main);
final TextureView textureView = findViewById(R.id.texture_view);

textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        mSurface = new Surface(surface);
        createMediaSync();
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        //release resources here
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    }
});

祝你好运!