IjkPlayer For Android(2)-本地视频格式扩展

1、硬解码开启

创建好IjkMedaiPlayer,通过设置参数实现

player.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-all-videos", 1);
player.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-sync", 1);
player.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-auto-rotate", 1);
player.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-handle-resolution-change", 1);

2、开始播放时声音比视频晚出

由于开启了opensles导致的,关闭即可

player.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "opensles", 0);

3、部分不支持硬解码视频黑屏

但是在ijkmedia/ijkplayer/android/pipeline/ffpipenode_android_mediacodec_vdec.c的视频帧解析的时候有问题不能解析,但是返回的是未知错误

// 返回值是AMEDIACODEC__UNKNOWN_ERROR
output_buffer_index = SDL_AMediaCodecFake_dequeueOutputBuffer(opaque->acodec, &bufferInfo, timeUs);

而ijk未对未知错误做处理,我们可以往上层抛出一个错误,让上层收到错误消息切换到软解码(最好是下层自动切换,由于时间关系没有调查下层处理方式,有兴趣的朋友可以看看)

if (output_buffer_index == AMEDIACODEC__UNKNOWN_ERROR) {
    ALOGE("AMEDIACODEC__UNKNOWN_ERROR\n");
    ffp_notify_msg2(ffp, FFP_MSG_ERROR, AMEDIACODEC__UNKNOWN_ERROR); // 上层注册setOnErrorListener监听可收到消息
    return ACODEC_EXIT;
}

3、支持硬解码视频但切换到软解码

根据IkjMedaiPlayer设计,如果硬解码不支持会自动跳转到软解码
ijkmedia/ijkplayer/android/pipeline/ffpipeline_android.c

static IJKFF_Pipenode *func_open_video_decoder(IJKFF_Pipeline *pipeline, FFPlayer *ffp){
    IJKFF_Pipeline_Opaque *opaque = pipeline->opaque;
    IJKFF_Pipenode        *node = NULL;
    if (ffp->mediacodec_all_videos || ffp->mediacodec_avc || ffp->mediacodec_hevc || ffp->mediacodec_mpeg2)
        node = ffpipenode_create_video_decoder_from_android_mediacodec(ffp, pipeline, opaque->weak_vout); //如果开启硬解码,优先使用硬解码
    if (!node) {
        node = ffpipenode_create_video_decoder_from_ffplay(ffp);
    }
    return node;
}

我们就看看ffpipenode_create_video_decoder_from_android_mediacodec哪里出现问题了
通过添加log发现,视频是H264,HP的视频,但是ijk仅仅识别出了H264,没有识别出profile,导致硬解码识别出错
通过屏蔽profile的判断,发现是H264就用硬解码即可

IJKFF_Pipenode *ffpipenode_create_video_decoder_from_android_mediacodec(FFPlayer *ffp, IJKFF_Pipeline *pipeline, SDL_Vout *vout){
    ...
    ALOGD("VR_ffpipenode_create_video_decoder_from_android_mediacodec codec_id=%d, profile=%d\n", opaque->codecpar->codec_id, opaque->codecpar->profile);
    switch (opaque->codecpar->codec_id) {
    case AV_CODEC_ID_H264:
        if (!ffp->mediacodec_avc && !ffp->mediacodec_all_videos) {
            ALOGE("%s: MediaCodec: AVC/H264 is disabled. codec_id:%d \n", __func__, opaque->codecpar->codec_id);
            goto fail;
        }
       // 注释掉profile识别的代码
        /*switch (opaque->codecpar->profile) {
            case FF_PROFILE_H264_BASELINE:
                ALOGI("%s: MediaCodec: H264_BASELINE: enabled\n", __func__);
                break;
            case FF_PROFILE_H264_CONSTRAINED_BASELINE:
                ALOGI("%s: MediaCodec: H264_CONSTRAINED_BASELINE: enabled\n", __func__);
                break;
            case FF_PROFILE_H264_MAIN:
                ALOGI("%s: MediaCodec: H264_MAIN: enabled\n", __func__);
                break;
            case FF_PROFILE_H264_EXTENDED:
                ALOGI("%s: MediaCodec: H264_EXTENDED: enabled\n", __func__);
                break;
            case FF_PROFILE_H264_HIGH:
                ALOGI("%s: MediaCodec: H264_HIGH: enabled\n", __func__);
                break;
            case FF_PROFILE_H264_HIGH_10:
                ALOGW("%s: MediaCodec: H264_HIGH_10: disabled\n", __func__);
                goto fail;
            case FF_PROFILE_H264_HIGH_10_INTRA:
                ALOGW("%s: MediaCodec: H264_HIGH_10_INTRA: disabled\n", __func__);
                goto fail;
            case FF_PROFILE_H264_HIGH_422:
                ALOGW("%s: MediaCodec: H264_HIGH_10_422: disabled\n", __func__);
                goto fail;
            case FF_PROFILE_H264_HIGH_422_INTRA:
                ALOGW("%s: MediaCodec: H264_HIGH_10_INTRA: disabled\n", __func__);
                goto fail;
            case FF_PROFILE_H264_HIGH_444:
                ALOGW("%s: MediaCodec: H264_HIGH_10_444: disabled\n", __func__);
                goto fail;
            case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
                ALOGW("%s: MediaCodec: H264_HIGH_444_PREDICTIVE: disabled\n", __func__);
                goto fail;
            case FF_PROFILE_H264_HIGH_444_INTRA:
                ALOGW("%s: MediaCodec: H264_HIGH_444_INTRA: disabled\n", __func__);
                goto fail;
            case FF_PROFILE_H264_CAVLC_444:
                ALOGW("%s: MediaCodec: H264_CAVLC_444: disabled\n", __func__);
                goto fail;
            default:
                ALOGW("%s: MediaCodec: (%d) unknown profile: disabled\n", __func__, opaque->codecpar->profile);
                goto fail;
        }*/
        // 注释掉profile识别的代码
        strcpy(opaque->mcc.mime_type, SDL_AMIME_VIDEO_AVC);
        opaque->mcc.profile = opaque->codecpar->profile;
        opaque->mcc.level   = opaque->codecpar->level;
        break;
    }
    ...
}

4、部分音频不能播放

ts视频格式,ac3音频格式,可以正常播放视频,但是没有声音(ts解码mpegts.c)


1.png

其实它是识别时间略长,导致不能播放声音,为了尽少修改代码,只是做了规避
首先我们直接找到音视频解码入口(ijkmedia/ijkplayer/ff_ffplay.c中的read_thread方法)

static int read_thread(void *arg){
...
err = avformat_open_input(&ic, is->filename, is->iformat, &ffp->format_opts);//读取视频头等信息
...
if (!ffp->audio_disable) {
        if(ic->streams[AVMEDIA_TYPE_AUDIO]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO
                && ic->streams[AVMEDIA_TYPE_AUDIO]->codecpar->codec_id >= AV_CODEC_ID_MP2) {
            if(ic->streams[AVMEDIA_TYPE_AUDIO]->codecpar->channels == 0) ic->streams[AVMEDIA_TYPE_AUDIO]->codecpar->channels = 2;
            if(ic->streams[AVMEDIA_TYPE_AUDIO]->codecpar->sample_rate == 0) ic->streams[AVMEDIA_TYPE_AUDIO]->codecpar->sample_rate = 48000;
        }
        // 获取最佳,但是必须要保证channels和sample_rate是数据有效(就是该处数据无效)
        st_index[AVMEDIA_TYPE_AUDIO] =
            av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
                                st_index[AVMEDIA_TYPE_AUDIO],
                                st_index[AVMEDIA_TYPE_VIDEO],
                                NULL, 0);
    }
...

然后再看看avformat_open_input干了啥

int avformat_open_input(AVFormatContext **ps, const char *filename,
                        AVInputFormat *fmt, AVDictionary **options){
...
if (!(s->flags&AVFMT_FLAG_PRIV_OPT)) {
        if (s->iformat->read_header2) {
            if (options)
                av_dict_copy(&tmp2, *options, 0);

            if ((ret = s->iformat->read_header2(s, &tmp2)) < 0)
                goto fail;
        } else if (s->iformat->read_header && (ret = s->iformat->read_header(s)) < 0)
            goto fail;
    }
...

这里将会跳转到具体视频解码文件(mpegts.c)的read_header方法中,读取头信息

你可能感兴趣的:(IjkPlayer For Android(2)-本地视频格式扩展)