在ijkplayer初始化流程中的結(jié)尾提到亩钟,stream_open()會(huì)創(chuàng)建讀線程和視頻渲染線程,下面是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ù)主要干了四件事:
調(diào)用frame_queue_init()創(chuàng)建兩個(gè)隊(duì)列pictq袜爪、sampq (其實(shí)是三個(gè),還有subq长酗,但是ijk好像沒(méi)有啟用字幕所以以后我們都只考慮沒(méi)有字幕的情況)纽谒,這兩個(gè)隊(duì)列分別用于存儲(chǔ)解碼后的視音頻包
調(diào)用packet_queue_ini()和上一步類似,創(chuàng)建兩個(gè)隊(duì)列videoq萨西、audioq有鹿,分別用于存儲(chǔ)解封裝但為解碼的視音頻包
SDL_CreateThreadEx(..., video_refresh_thread, ...)創(chuàng)建視頻渲染線程
4. SDL_CreateThreadEx(..., read_thread, ...)創(chuàng)建讀線程
下面主要將讀線程里的操作,讀線程的主要代碼如下
/* 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()谎脯,創(chuàng)建AVFormatContext結(jié)構(gòu)體印颤,用來(lái)保存輸入輸出格式等信息
2). avformat_open_input(),打開文件穿肄,主要是探測(cè)協(xié)議類型,如果是網(wǎng)絡(luò)文件則創(chuàng)建網(wǎng)絡(luò)鏈接等
3). avformat_find_stream_info()际看,探測(cè)媒體類型咸产,可得到當(dāng)前文件的封裝格式,音視頻編碼參數(shù)等信息
4). stream_component_open()仲闽,這步做的事情比較多稍后重點(diǎn)描述
5). av_read_frame()脑溢,從網(wǎng)絡(luò)或者硬盤讀包
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)建音頻輸出線程
b). 接著調(diào)用了decoder_init()函數(shù),里面有d->queue = queue這一步是將ffplayer的audioq賦值給解碼器中的queue隊(duì)列熟空,用于解碼使用
c). 最后調(diào)用decode_start()函數(shù)藤巢,里面執(zhí)行SDL_CreateThreadEx()創(chuàng)建音頻解碼線程 - case AVMEDIA_TYPE_VIDEO:
不同于音頻,這里沒(méi)有類似audio_open()函數(shù)息罗,因?yàn)橐曨l輸出線程在本節(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()類似于func_open_audio_output()是用來(lái)選擇使用硬解還是軟解
c). 和音頻一樣嫉戚,調(diào)用decode_start()函數(shù),里面執(zhí)行SDL_CreateThreadEx()創(chuàng)建視頻解碼線程
到這里讀線程的邏輯就結(jié)束了澈圈,里面的一些seek(滑動(dòng)操作)彬檀,pause等操作沒(méi)有細(xì)說(shuō),可以自己更深入的看