ijkplayer 读线程

在ijkplayer初始化流程中的结尾提到,stream_open()会创建读线程和视频渲染线程,下面是stream_open()的主要代码

static VideoState *stream_open(FFPlayer *ffp, const char *filename, AVInputFormat *iformat)
{
    ...
    /* start video display */
    if (frame_queue_init(&is->pictq, &is->videoq, ffp->pictq_size, 1) < 0)
        goto fail;
    if (frame_queue_init(&is->subpq, &is->subtitleq, SUBPICTURE_QUEUE_SIZE, 0) < 0)
        goto fail;
    if (frame_queue_init(&is->sampq, &is->audioq, SAMPLE_QUEUE_SIZE, 1) < 0)
        goto fail;

    if (packet_queue_init(&is->videoq) < 0 ||
        packet_queue_init(&is->audioq) < 0 ||
        packet_queue_init(&is->subtitleq) < 0)
        goto fail;
    ...
    is->video_refresh_tid = SDL_CreateThreadEx(&is->_video_refresh_tid, video_refresh_thread, ffp, "ff_vout");
     ...
    is->read_tid = SDL_CreateThreadEx(&is->_read_tid, read_thread, ffp, "ff_read");
    ...
}

可以看出ff_ffplayer.c/stream_open()函数主要干了四件事:

  1. 调用frame_queue_init()创建两个队列pictq、sampq (其实是三个,还有subq,但是ijk好像没有启用字幕所以以后我们都只考虑没有字幕的情况),这两个队列分别用于存储解码后的视音频包

  2. 调用packet_queue_ini()和上一步类似,创建两个队列videoq、audioq,分别用于存储解封装但为解码的视音频包

  3. SDL_CreateThreadEx(..., video_refresh_thread, ...)创建视频渲染线程

4. SDL_CreateThreadEx(..., read_thread, ...)创建读线程

下面主要将读线程里的操作,读线程的主要代码如下

/* this thread gets the stream from the disk or the network */
static int read_thread(void *arg)
{
    ...
    ic = avformat_alloc_context();
    ...
    err = avformat_open_input(&ic, is->filename, is->iformat, &ffp->format_opts);
   ...
    err = avformat_find_stream_info(ic, opts);
    ...
    /* open the streams */
    if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
        stream_component_open(ffp, st_index[AVMEDIA_TYPE_AUDIO]);
    }

    ret = -1;
    if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
        ret = stream_component_open(ffp, st_index[AVMEDIA_TYPE_VIDEO]);
    }
    if (is->show_mode == SHOW_MODE_NONE)
        is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;

    if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
        stream_component_open(ffp, st_index[AVMEDIA_TYPE_SUBTITLE]);
    }
    ...
    ffp->prepared = true;
    ffp_notify_msg1(ffp, FFP_MSG_PREPARED);
    ...
    for (;;) {
        ...
        ret = av_read_frame(ic, pkt);
        ...
        if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
            packet_queue_put(&is->audioq, pkt);
        } else if (pkt->stream_index == is->video_stream && pkt_in_play_range
                   && !(is->video_st && (is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC))) {
            packet_queue_put(&is->videoq, pkt);
        } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
            packet_queue_put(&is->subtitleq, pkt);
        } else {
            av_packet_unref(pkt);
        }
        ...
    }
    ...
}

读线程又做了如下六件事:
1). avformat_alloc_context(),创建AVFormatContext结构体,用来保存输入输出格式等信息
2). avformat_open_input(),打开文件,主要是探测协议类型,如果是网络文件则创建网络链接等
3). avformat_find_stream_info(),探测媒体类型,可得到当前文件的封装格式,音视频编码参数等信息
4). stream_component_open(),这步做的事情比较多稍后重点描述
5). av_read_frame(),从网络或者硬盘读包
6). packet_queue_put(),将音视频数据分别送入相应的queue中(也就是前面说的videoq、audioq)

接下来着重描述一下stream_component_open()

/* open a given stream. Return 0 if OK */
static int stream_component_open(FFPlayer *ffp, int stream_index)
{
    ...
    codec = avcodec_find_decoder(avctx->codec_id);
    switch (avctx->codec_type) {
        case AVMEDIA_TYPE_AUDIO   : is->last_audio_stream    = stream_index; forced_codec_name = ffp->audio_codec_name; break;
        case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = ffp->subtitle_codec_name; break;
        case AVMEDIA_TYPE_VIDEO   : is->last_video_stream    = stream_index; forced_codec_name = ffp->video_codec_name; break;
        default: break;
    }
    ...
    if ((ret = avcodec_open2(avctx, codec, &opts)) < 0) {
        goto fail;
    }
    ...
    switch (avctx->codec_type) {
    case AVMEDIA_TYPE_AUDIO:
        ...
        /* prepare audio output */
        if ((ret = audio_open(ffp, channel_layout, nb_channels, sample_rate, &is->audio_tgt)) < 0)
            goto fail;
        ...
        decoder_init(&is->auddec, avctx, &is->audioq, is->continue_read_thread);
        ...
        if ((ret = decoder_start(&is->auddec, audio_thread, ffp, "ff_audio_dec")) < 0)
            goto out;
        SDL_AoutPauseAudio(ffp->aout, 0);
        break;
    case AVMEDIA_TYPE_VIDEO:
        ...
        decoder_init(&is->viddec, avctx, &is->videoq, is->continue_read_thread);
        ffp->node_vdec = ffpipeline_open_video_decoder(ffp->pipeline, ffp);
        ...
        if ((ret = decoder_start(&is->viddec, video_thread, ffp, "ff_video_dec")) < 0)
            goto out;
        ...
        break;
    ...
    default:
        break;
    }
    goto out;
fail:
    avcodec_free_context(&avctx);
out:
    av_dict_free(&opts);

    return ret;
}

该函数主要有以下步骤:
a. 调用avcodec_find_decoder()函数查找ffmepg里的解码器,解码器的注册在ijkplayer初始化流程中的avcodec_register_all()完成
b. 调用avcodec_open2()函数用于初始化一个视音频编解码器的AVCodecContext
c. 进入switch,分视频流和音频流的情况

  • case AVMEDIA_TYPE_AUDIO:
    a). 调用audio_open()函数,该函数里面的调用路径为SDL_AoutOpenAudio() ->> aout->open_audio(),这个open_audio()在初始化的时候赋值了,地方就在ff_ffpipeline_android.c/ func_open_audio_output(),这个函数选好输出音频的工具后就会给open_audio赋值,最后调用ijksdl_aout_android_audiotrack.c/aout_open_audio_n()(如果用的是audiotrack),aout_open_audio_n()函数里又会调用SDL_CreateThreadEx(..., aout_thread, aout, ...)创建音频输出线程
    b). 接着调用了decoder_init()函数,里面有d->queue = queue这一步是将ffplayer的audioq赋值给解码器中的queue队列,用于解码使用
    c). 最后调用decode_start()函数,里面执行SDL_CreateThreadEx()创建音频解码线程
  • case AVMEDIA_TYPE_VIDEO:
    不同于音频,这里没有类似audio_open()函数,因为视频输出线程在本节前面提到的stream_open()里创建
    a). 调用decoder_init()函数,和音频一样,d->queue = queue,将ffplayer的videoq赋值给解码器中的queue队列,用于解码使用
    b). 调用ffpipeline_open_video_decoder()函数,里面会调用pipeline->func_open_video_decoder(),这里的func_open_video_decoder()在ijk初始化时赋值,赋值地方和前面讲的ff_ffpipeline_android.c/ func_open_audio_output()的赋值地方一样,都在ijkplayer_android.c/ffpipeline_create_from_android()函数里,pipeline->func_open_video_decoder()类似于func_open_audio_output()是用来选择使用硬解还是软解
    c). 和音频一样,调用decode_start()函数,里面执行SDL_CreateThreadEx()创建视频解码线程

到这里读线程的逻辑就结束了,里面的一些seek(滑动操作),pause等操作没有细说,可以自己更深入的看

你可能感兴趣的:(ijkplayer 读线程)