系统相关
首页 > 系统相关> > android – AudioTrack的内存泄漏

android – 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();

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

>运行应用程序.
>按主页按钮,再次运行.重复约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?

解决方法:

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

要解决这个问题,你应该通过例如添加.setFlags(AudioAttributes.FLAG_LOW_LATENCY)调用你的AudioAttributes来禁用这个标志,但它需要min SDK 24.否则你可以完全放弃使用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并使其不以任何其他方式通过.

标签:android,mediacodec,audiotrack
来源: https://codeday.me/bug/20190622/1261101.html