ijkplayer 讀線(xiàn)程

ijkplayer初始化流程中的結(jié)尾提到,stream_open()會(huì)創(chuàng)建讀線(xiàn)程和視頻渲染線(xiàn)程,下面是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()函數(shù)主要干了四件事:

  1. 調(diào)用frame_queue_init()創(chuàng)建兩個(gè)隊(duì)列pictq、sampq (其實(shí)是三個(gè),還有subq,但是ijk好像沒(méi)有啟用字幕所以以后我們都只考慮沒(méi)有字幕的情況),這兩個(gè)隊(duì)列分別用于存儲(chǔ)解碼后的視音頻包

  2. 調(diào)用packet_queue_ini()和上一步類(lèi)似,創(chuàng)建兩個(gè)隊(duì)列videoq、audioq,分別用于存儲(chǔ)解封裝但為解碼的視音頻包

  3. SDL_CreateThreadEx(..., video_refresh_thread, ...)創(chuàng)建視頻渲染線(xiàn)程

4. SDL_CreateThreadEx(..., read_thread, ...)創(chuàng)建讀線(xiàn)程

下面主要將讀線(xiàn)程里的操作,讀線(xiàn)程的主要代碼如下

/* 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);
        }
        ...
    }
    ...
}

讀線(xiàn)程又做了如下六件事:
1). avformat_alloc_context(),創(chuàng)建AVFormatContext結(jié)構(gòu)體,用來(lái)保存輸入輸出格式等信息
2). avformat_open_input(),打開(kāi)文件,主要是探測(cè)協(xié)議類(lèi)型,如果是網(wǎng)絡(luò)文件則創(chuàng)建網(wǎng)絡(luò)鏈接等
3). avformat_find_stream_info(),探測(cè)媒體類(lèi)型,可得到當(dāng)前文件的封裝格式,音視頻編碼參數(shù)等信息
4). stream_component_open(),這步做的事情比較多稍后重點(diǎn)描述
5). av_read_frame(),從網(wǎng)絡(luò)或者硬盤(pán)讀包
6). packet_queue_put(),將音視頻數(shù)據(jù)分別送入相應(yīng)的queue中(也就是前面說(shuō)的videoq、audioq)

接下來(lái)著重描述一下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;
}

該函數(shù)主要有以下步驟:
a. 調(diào)用avcodec_find_decoder()函數(shù)查找ffmepg里的解碼器,解碼器的注冊(cè)在ijkplayer初始化流程中的avcodec_register_all()完成
b. 調(diào)用avcodec_open2()函數(shù)用于初始化一個(gè)視音頻編解碼器的AVCodecContext
c. 進(jìn)入switch,分視頻流和音頻流的情況

  • case AVMEDIA_TYPE_AUDIO:
    a). 調(diào)用audio_open()函數(shù),該函數(shù)里面的調(diào)用路徑為SDL_AoutOpenAudio() ->> aout->open_audio(),這個(gè)open_audio()在初始化的時(shí)候賦值了,地方就在ff_ffpipeline_android.c/ func_open_audio_output(),這個(gè)函數(shù)選好輸出音頻的工具后就會(huì)給open_audio賦值,最后調(diào)用ijksdl_aout_android_audiotrack.c/aout_open_audio_n()(如果用的是audiotrack),aout_open_audio_n()函數(shù)里又會(huì)調(diào)用SDL_CreateThreadEx(..., aout_thread, aout, ...)創(chuàng)建音頻輸出線(xiàn)程
    b). 接著調(diào)用了decoder_init()函數(shù),里面有d->queue = queue這一步是將ffplayer的audioq賦值給解碼器中的queue隊(duì)列,用于解碼使用
    c). 最后調(diào)用decode_start()函數(shù),里面執(zhí)行SDL_CreateThreadEx()創(chuàng)建音頻解碼線(xiàn)程
  • case AVMEDIA_TYPE_VIDEO:
    不同于音頻,這里沒(méi)有類(lèi)似audio_open()函數(shù),因?yàn)橐曨l輸出線(xiàn)程在本節(jié)前面提到的stream_open()里創(chuàng)建
    a). 調(diào)用decoder_init()函數(shù),和音頻一樣,d->queue = queue,將ffplayer的videoq賦值給解碼器中的queue隊(duì)列,用于解碼使用
    b). 調(diào)用ffpipeline_open_video_decoder()函數(shù),里面會(huì)調(diào)用pipeline->func_open_video_decoder(),這里的func_open_video_decoder()在ijk初始化時(shí)賦值,賦值地方和前面講的ff_ffpipeline_android.c/ func_open_audio_output()的賦值地方一樣,都在ijkplayer_android.c/ffpipeline_create_from_android()函數(shù)里,pipeline->func_open_video_decoder()類(lèi)似于func_open_audio_output()是用來(lái)選擇使用硬解還是軟解
    c). 和音頻一樣,調(diào)用decode_start()函數(shù),里面執(zhí)行SDL_CreateThreadEx()創(chuàng)建視頻解碼線(xiàn)程

到這里讀線(xiàn)程的邏輯就結(jié)束了,里面的一些seek(滑動(dòng)操作),pause等操作沒(méi)有細(xì)說(shuō),可以自己更深入的看

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容