MediaCodec 异步模式,NDK 不触发回调函数

问题描述 投票:0回答:1

我正在开发一个使用 Android NDK MediaCodec 解码 AVC 流的演示。从 Kotlin 调用以下 jni 函数,不会触发 MediaCodec 回调函数“notifyCallback”。它们没有按预期工作。

extern "C"
JNIEXPORT void JNICALL
Java_com_initiald_avcdemo_MainActivityKt_configCodec(JNIEnv *env, jclass clazz,
                                                     jobject surface, jobject frames) {
    __android_log_print(ANDROID_LOG_INFO, "Native_Gate", "configCodec>>>>");

    AMediaCodec* decoder = AMediaCodec_createCodecByName("OMX.google.h264.decoder");
    AMediaFormat* format = AMediaFormat_new();
    AMediaFormat_setString(format, "mime", "video/avc");
    AMediaFormat_setInt32(format, "width", 832);
    AMediaFormat_setInt32(format, "height", 480);

    ANativeWindow* window = ANativeWindow_fromSurface(env, surface);
    if(NULL == window){
        __android_log_print(ANDROID_LOG_ERROR, "Native_Gate", "native window is null");
        return;
    }

    AMediaCodecOnAsyncNotifyCallback* notifyCallback = new  AMediaCodecOnAsyncNotifyCallback{
            []( AMediaCodec *codec,void *userdata,int32_t index)
            {
                __android_log_print(ANDROID_LOG_ERROR, "Native_Gate", "onAsyncInputAvailable");
            },
            []( AMediaCodec *codec, void *userdata, int32_t index, AMediaCodecBufferInfo *bufferInfo)
            {
                __android_log_print(ANDROID_LOG_ERROR, "Native_Gate", "onAsyncOutputAvailable");
            },
            [](AMediaCodec *codec, void *userdata, AMediaFormat *format)
            {
                __android_log_print(ANDROID_LOG_ERROR, "Native_Gate", "onAsyncFormatChanged");
            },
            [](AMediaCodec *codec, void *userdata, media_status_t error,int32_t actionCode,
               const char *detail)
            {
                __android_log_print(ANDROID_LOG_ERROR, "Native_Gate", "onAsyncError");
            }
    };


    media_status_t status = AMediaCodec_configure(decoder, format, window, NULL, 0);
    if(status != 0){
        __android_log_print(ANDROID_LOG_ERROR, "Native_Gate", "config codec failed [%d]", status);
        return;
    }

    status = AMediaCodec_setAsyncNotifyCallback(decoder, *notifyCallback, nullptr);
    if(status != 0){
        __android_log_print(ANDROID_LOG_ERROR, "Native_Gate", "set callback failed [%d]", status);
        return;
    }


    status = AMediaCodec_start(decoder);
    if(status != 0){
        __android_log_print(ANDROID_LOG_ERROR, "Native_Gate", "start codec failed");
        return;
    }

 
    // ANativeWindow_release(window);
    __android_log_print(ANDROID_LOG_INFO, "Native_Gate", "configCodec<<<<<");
}

任何人都可以帮我找出此问题的根本原因吗?或者分享相关文件给我。 谢谢!

android h.264 android-mediacodec
1个回答
0
投票

根据文档

如果客户端打算以异步模式使用该组件,则 应在配置(MediaFormat, Surface、MediaCrypto、int) 被调用。

就您的情况而言,应在致电

AMediaCodec_setAsyncNotifyCallback
之前先致电
AMediaCodec_configure

© www.soinside.com 2019 - 2024. All rights reserved.