我正在使用AAudio从USB连接的设备上读取Android上的立体声音频数据。打开流(调用AAudioStreamBuilder_openStream)需要10到15秒。我可以减少吗?
我修改了AAudio示例代码以成功录制设备中的音频。但是,在设备上打开记录流时的启动时间在Samsung S9上花费15秒,在Samsung S8上花费10秒。由于用户可以打开该应用并立即尝试进行记录,因此这太长了。
s
(顺便说一句,采样率48000适用于此设备。)
时间码确认我等待logcat输出继续的时间。
在S8上,我看到以下输出:
StreamBuilder AudioEngine::makeStreamBuilder()
{
AAudioStreamBuilder *builder = nullptr;
aaudio_result_t result = AAudio_createStreamBuilder(&builder);
if (result != AAUDIO_OK) {
__android_log_print(ANDROID_LOG_ERROR, __func__, "Failed to create stream builder %s (%d)",
AAudio_convertResultToText(result), result);
return StreamBuilder(nullptr, &AAudioStreamBuilder_delete);
}
return StreamBuilder(builder, &AAudioStreamBuilder_delete);
}
void AudioEngine::openRecordingDevice( int deviceId )
{
StreamBuilder recordingBuilder = makeStreamBuilder();
AAudioStreamBuilder_setDeviceId(recordingBuilder.get(), deviceId );
AAudioStreamBuilder_setDirection(recordingBuilder.get(), AAUDIO_DIRECTION_INPUT);
AAudioStreamBuilder_setPerformanceMode(recordingBuilder.get(), AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
AAudioStreamBuilder_setSharingMode(recordingBuilder.get(), AAUDIO_SHARING_MODE_EXCLUSIVE);
//AAudioStreamBuilder_setSharingMode(recordingBuilder.get(), AAUDIO_SHARING_MODE_SHARED);
AAudioStreamBuilder_setFormat(recordingBuilder.get(), AAUDIO_FORMAT_PCM_FLOAT);
AAudioStreamBuilder_setSampleRate(recordingBuilder.get(), 48000);
AAudioStreamBuilder_setChannelCount(recordingBuilder.get(), kChannelCountStereo);
AAudioStreamBuilder_setDataCallback(recordingBuilder.get(), ::recordingDataCallback, this);
AAudioStreamBuilder_setErrorCallback(recordingBuilder.get(), ::errorCallback, this);
struct timeval before, after, diff;
gettimeofday(&before, NULL);
result = AAudioStreamBuilder_openStream(recordingBuilder.get(), &mRecordingStream);
gettimeofday(&after, NULL);
timeval_subtract( &diff, &after, &before );
__android_log_print(ANDROID_LOG_DEBUG, "M2MAudioEngine", "open record stream took %ld", diff.tv_sec);
...
}
在S9(大约需要5秒钟的时间)上,中间还有其他输出:
2019-05-29 10:40:27.982 24011-24011/com.example.myapp D/AAudio: AudioStreamBuilder(): mmapPolicy = 1, mapExclusivePolicy = 1
2019-05-29 10:40:27.982 24011-24011/com.example.myapp W/AAudio: AudioStreamBuilder(): EXCLUSIVE sharing mode not supported. Use SHARED.
2019-05-29 10:40:27.982 24011-24011/com.example.myapp I/AAudio: AudioStream::open() rate = 48000, channels = 2, format = 2, sharing = 1, dir = INPUT
2019-05-29 10:40:27.982 24011-24011/com.example.myapp I/AAudio: AudioStream::open() device = 11, perfMode = 12, callbackFrames = 0
2019-05-29 10:40:27.982 24011-24011/com.example.myapp D/AAudio: AudioStreamRecord::open(), request notificationFrames = 0, frameCount = 0
2019-05-29 10:40:33.299 24011-24011/com.example.myapp W/AudioRecord: AUDIO_INPUT_FLAG_FAST denied by server; frameCount 0 -> 2880
2019-05-29 10:40:38.603 24011-24011/com.example.myapp W/AAudio: AudioStreamRecord::open() flags changed from 0x00000005 to 0x00000000
2019-05-29 10:40:38.603 24011-24011/com.example.myapp W/AAudio: AudioStreamRecord::open() perfMode changed from 12 to 10
2019-05-29 10:40:38.605 24011-24011/com.example.myapp D/AAudio: AAudioStreamBuilder_openStream() returns 0 = AAUDIO_OK for (0x73f1b2a480) ----
------------
2019-05-29 10:40:38.605 24011-24011/com.example.myapp D/M2MAudioEngine: open record stream took 10
我从Oboe(包装AAudio)https://github.com/google/oboe/issues/155的这个问题中了解到,有人试图将设备打开为“ MMAP”(内存映射),但超时。我不知道为什么这会在S9而不是S8上发生。我正在使用完全相同的外部设备。
我的问题是:可以整体降低流打开延迟吗?如何防止额外的MMAP延迟(我知道这不适用)?