Android奇怪的AudioTrack崩溃

时间:2017-09-25 10:54:37

标签: android audio opensl

我是一名Android App开发人员,我遇到以下崩溃报告(我每天有5/6),我遇到了麻烦:

  native: pc 00000000000418e0  /system/lib/libc.so (tgkill+12)
  native: pc 0000000000040d59  /system/lib/libc.so (pthread_kill+32)
  native: pc 000000000001c7eb  /system/lib/libc.so (raise+10)
  native: pc 000000000001999d  /system/lib/libc.so (__libc_android_abort+34)
  native: pc 0000000000017550  /system/lib/libc.so (abort+4)
  native: pc 0000000000008d53  /system/lib/libcutils.so (__android_log_assert+86)
  native: pc 000000000006e2c3  /system/lib/libmedia.so (_ZN7android11ClientProxy13releaseBufferEPNS_5Proxy6BufferE+94)
  native: pc 000000000006c11d  /system/lib/libmedia.so (_ZN7android10AudioTrack13releaseBufferEPKNS0_6BufferE+112)
  native: pc 000000000006c987  /system/lib/libmedia.so (_ZN7android10AudioTrack18processAudioBufferEv+1350)
  native: pc 000000000006d7f3  /system/lib/libmedia.so (_ZN7android10AudioTrack16AudioTrackThread10threadLoopEv+194)
  native: pc 0000000000010079  /system/lib/libutils.so (_ZN7android6Thread11_threadLoopEPv+112)
  native: pc 000000000004065b  /system/lib/libc.so (_ZL15__pthread_startPv+30)
  native: pc 000000000001a021  /system/lib/libc.so (__start_thread+6)

OpenSL世界中调用JNI个函数。

这些是存储在堆中的所有变量:

/* OpenSL ES audio stuff */
SLObjectItf engineObject = NULL;
SLEngineItf engineEngine = NULL;
SLObjectItf outputMixObject = NULL;
SLObjectItf playerObject = NULL;
SLPlayItf   playerPlay = NULL;
SLVolumeItf playerVolume = NULL;
SLAndroidSimpleBufferQueueItf playerBufferQueue = NULL;
char        openSLinited = 0;

int16_t     audioBuffer1[48000];
int16_t     audioBuffer2[48000];
int16_t    *currentAudioBuffer;

这就是我初始化所有机器的方式:

void Java_com_myapp_myappname_MyActivity_jniOpenSLInit(JNIEnv *env,
                                                       jobject thiz,
                                                       jint freq)
{
    SLresult result;

    // create engine
    result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
    assert(SL_RESULT_SUCCESS == result);

    // realize the engine
    result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
    assert(SL_RESULT_SUCCESS == result);

    // get the engine interface, which is needed to create other objects
    result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE,
                                           &engineEngine);
    assert(SL_RESULT_SUCCESS == result);

    // create output mix
    result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject,
                                              0, NULL, NULL);
    assert(SL_RESULT_SUCCESS == result);

    // realize the output mix
    result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
    assert(SL_RESULT_SUCCESS == result);

    SLuint32 SLfreq;
    if (freq == 44100)
        SLfreq = SL_SAMPLINGRATE_44_1;
    else
        SLfreq = SL_SAMPLINGRATE_48;

    SLDataLocator_AndroidSimpleBufferQueue loc_bufq =
        {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
    SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM, 2, SLfreq,
                                   SL_PCMSAMPLEFORMAT_FIXED_16,
                                   SL_PCMSAMPLEFORMAT_FIXED_16,
                                   SL_SPEAKER_FRONT_LEFT |
                                   SL_SPEAKER_FRONT_RIGHT,
                                   SL_BYTEORDER_LITTLEENDIAN};

    SLDataSource audioSrc = {&loc_bufq, &format_pcm};

    /* configure audio sink */
    SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX,
                                          outputMixObject};
    SLDataSink audioSnk = {&loc_outmix, NULL};

    const SLInterfaceID idsAudioPlayer[2] = {SL_IID_BUFFERQUEUE,
                                             SL_IID_VOLUME };

    const SLboolean reqAudioPlayer[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };

    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject,
                                                &audioSrc,
                                                &audioSnk, 2, idsAudioPlayer,
                                                reqAudioPlayer);
    assert(SL_RESULT_SUCCESS == result);

    // realize the player
    result = (*playerObject)->Realize(playerObject, SL_BOOLEAN_FALSE);
    assert(SL_RESULT_SUCCESS == result);

    // get the play interface
    result = (*playerObject)->GetInterface(playerObject, SL_IID_PLAY,
                                           &playerPlay);
    assert(SL_RESULT_SUCCESS == result);

    // get the volume interface
    result = (*playerObject)->GetInterface(playerObject, SL_IID_VOLUME,
                                           &playerVolume);
    assert(SL_RESULT_SUCCESS == result);

    // get the buffer queue interface
    result = (*playerObject)->GetInterface(playerObject, SL_IID_BUFFERQUEUE,
                                           &playerBufferQueue);
    assert(SL_RESULT_SUCCESS == result);

    // register callback on the buffer queue
    result = (*playerBufferQueue)->RegisterCallback(playerBufferQueue,
                                                    audio_player_cb, NULL);
    assert(SL_RESULT_SUCCESS == result);

    // done!
    openSLinited = 1;
}

这被称为启动机器。

void openSLStart()
{
    bzero(audioBuffer1, 96000);
    bzero(audioBuffer2, 96000);

    if (!openSLinited)
        return;

    (*playerBufferQueue)->Enqueue(playerBufferQueue, audioBuffer1,
                                  4096 * 4);
    (*playerBufferQueue)->Enqueue(playerBufferQueue, audioBuffer2,
                                  4096 * 4);

    currentAudioBuffer = audioBuffer1;

    // set the player's state to playing
    (*playerPlay)->SetPlayState(playerPlay, SL_PLAYSTATE_PLAYING);
}

这是将新样本排入队列的回调

void audio_player_cb(SLAndroidSimpleBufferQueueItf bq, void *context)
{
    if (!openSLinited)
        return;

    assert(bq == playerBufferQueue);
    assert(NULL == context);

    // switch between audio buffer 1 and 2
    if (currentAudioBuffer == audioBuffer1)
        currentAudioBuffer = audioBuffer2;
    else
        currentAudioBuffer = audioBuffer1;

    // this function read samples (4096 16 bit samples) from an internal buffer
    sound_read_samples(4096, currentAudioBuffer);

    // feed openSL machine
    (*playerBufferQueue)->Enqueue(playerBufferQueue, currentAudioBuffer,
                                  4096 * 2);
}

最后,这就是OpenSL被终止的方式

void Java_com_myfirm_myappname_MyActivity_jniOpenSLTerm(JNIEnv *env,
                                                        jobject thiz)
{
    // shutdown every created object
    if (playerObject)
    {
        // stop the player
        SLresult result = (*playerPlay)->SetPlayState(playerPlay,
                                                      SL_PLAYSTATE_STOPPED);

        if (SL_RESULT_SUCCESS == result)
            utils_log("Player succesfully stopped");

        (*playerObject)->Destroy(playerObject);
        playerObject = NULL;
        playerPlay = NULL;
    }

    if (outputMixObject)
    {
        (*outputMixObject)->Destroy(outputMixObject);
        outputMixObject = NULL;
    }

    if (engineObject)
    {
        (*engineObject)->Destroy(engineObject);
        engineObject = NULL;
    }

    openSLinited = 0;

    utils_log("OpenSLTerm complete");
}

我无法在手机和模拟器上重现......它永远不会以这种方式崩溃。

我没有关于如何解决这个问题的想法。有人可以帮助我摆脱这次崩溃吗?

10月8日更新

我尝试按照建议删除日志。崩溃仍然存在。

问题影响Android 6.0,7.0和7.1(至少,我没有针对不同版本的报告)

10月9日更新

按照Amjad Khan的要求:

这是生成库

的Android.mk
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := libmyapp-jni
LOCAL_SRC_FILES := src.c src2.c src3.c 
LOCAL_LDLIBS := -llog -landroid -ljnigraphics -lGLESv2 -lOpenSLES
LOCAL_CFLAGS += -O3 -DNDEBUG

include $(BUILD_SHARED_LIBRARY)

这是运行以构建库的命令(生成所有ABI)

/cygdrive/c/Android/ndk/ndk-build.cmd NDK_DEBUG=0 APP_BUILD_SCRIPT=./Android.mk NDK_PROJECT_PATH=. 

提前谢谢。

2 个答案:

答案 0 :(得分:1)

我已经看到崩溃与我的崩溃相同,这是由于android日志生成的,最近我上传了apk并找到了相同的跟踪。

  native: pc 0000000000048793  /system/lib/libc.so (pthread_kill+34)
  native: pc 000000000001d5d5  /system/lib/libc.so (raise+10)
  native: pc 0000000000019111  /system/lib/libc.so (__libc_android_abort+34)
  native: pc 0000000000017174  /system/lib/libc.so (abort+4)
  native: pc 000000000000c481  /system/lib/libcutils.so (__android_log_assert+112)
  native: pc 0000000000025595  /system/lib/libhwui.so
  native: pc 00000000000270d1  /system/lib/libhwui.so
  native: pc 000000000002b959  /system/lib/libhwui.so (_ZN7android10uirenderer12renderthread12RenderThread10threadLoopEv+80)
  native: pc 000000000000e35d  /system/lib/libutils.so (_ZN7android6Thread11_threadLoopEPv+140)
  native: pc 000000000006830b  /system/lib/libandroid_runtime.so (_ZN7android14AndroidRuntime15javaThreadShellEPv+102)
  native: pc 0000000000048263  /system/lib/libc.so (_ZL15__pthread_startPv+22)
  native: pc 0000000000019b5d  /system/lib/libc.so (__start_thread+6)

这个回溯是我最近的更新,所以我在我的原生文件中进行了android日志打印。

__android_log_print(ANDROID_LOG_ERROR, "TRACKERS", "%s", Str);

我使用日志打印我的数据,以检查我上传的时间是否有回溯,所以我删除了本机和java函数的所有日志,这些日志是从本机调用的。

然后,在我完成了干净的构建和上传之后,所以在最新版本中,直到现在还没有生成超过5个构建上传

  

特定于Android的日志支持

     

包含应用程序可用于从本机代码向内核发送日志消息的各种定义。有关这些定义的更多信息,请参阅中的注释。

     

您可以编写自己的包装器宏来访问此功能。如果要执行日志记录,则本机模块应链接到/system/lib/liblog.so。通过在Android.mk文件中包含以下行来实现此链接:

     

LOCAL_LDLIBS:= -llog

在这里,您将找到轻松找到原生错误的方法

答案 1 :(得分:0)

这看起来像OpenSL ES中的已知错误。 插入耳机后,可以在releaseBuffer()中获得一个断言。 如果回调具有很高的CPU负载,则竞争条件会变得更糟。

回调通过在AudioTrack中调用gainBuffer开始。 回调结束时,它将调用releaseBuffer。但是如果 设备将在回调的中间进行更改,然后它将缓冲区释放到原始源。因此断言。

如果您从回调中调用playInterface-> getPosition(),则很可能发生此崩溃,因为这会触发任何待处理的设备切换。

Oboe网站上的技术说明中有完整的分析。 https://github.com/google/oboe/blob/master/docs/notes/rlsbuffer.md