gpt4 book ai didi

android - AudioTrack 的内存泄漏

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:40:23 30 4
gpt4 key购买 nike

我在AudioTrack和MediaSync一起使用时遇到严重的内存泄漏问题。在我看来,问题是 AudioTrack 没有释放一些原生资源。因此,该应用程序只能运行几次,之后,由于不再有可用的轨道,因此无法创建 AudioTrack。

下面是一个导致内存泄漏的简短示例。可以下载完整的项目 here在 GitHub 上。 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 次,然后得到错误。

日志:

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 并且 switch case 导致 PERFORMANCE_MODE_POWER_SAVING 并启用 FLAG_DEEP_BUFFER

要解决此问题,您应该禁用此标志,例如将 .setFlags(AudioAttributes.FLAG_LOW_LATENCY) 调用添加到您的 AudioAttributes,但它需要最低 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并使其不以任何其他方式通过。

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

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

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) {
}
});

祝你好运!

关于android - AudioTrack 的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55177737/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com