前言
上篇最簡(jiǎn)iOS播放器(一)利用ffmpeg和SDL構(gòu)建了最簡(jiǎn)iOS播放器,但視頻數(shù)據(jù)是渲染到了SDLWindow上,這個(gè)結(jié)果肯定不是我們想要的,渲染到UIView上我們才能更好的操作控制視頻的播放,本篇的目的便是把數(shù)據(jù)渲染到UIView上。
正文
一、思路分析
以上篇博客中構(gòu)建好的項(xiàng)目為基礎(chǔ)來改造項(xiàng)目,用UIView代替SDLWindow,用openGL來渲染代替SDLRender??傮w架構(gòu)依然是ffplay的架構(gòu),解碼線程read_thread基本不變,AVPacket、AVFrame依然用原來的隊(duì)列管理。由于不再使用SDLEvent,我們創(chuàng)建一個(gè)線程video_refresh_thread來刷新一幀畫面(ijkplayer便是這么做的)。原來負(fù)責(zé)刷新、音視頻同步等函數(shù)基本不變,只修改video_image_display函數(shù),在這個(gè)函數(shù)里把解碼得到的AVFrame數(shù)據(jù)傳到UIView,并在UIView里編寫openGL代碼渲染視頻數(shù)據(jù)。
二、關(guān)于音頻
音頻的播放可以使用蘋果的<AudioToolbox/AudioToolbox.h>庫里的AudioQueue或者AudioUnit,也可以直接使用SDL的SDL_audio.h來處理播放音頻,為了省事,我這里還是和ffplay一樣使用SDL_audio,使用時(shí)必須調(diào)用的一個(gè)函數(shù)SDL_SetMainReady();,不然會(huì)導(dǎo)致無法播放出聲音。
static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params){
SDL_SetMainReady();
SDL_AudioSpec wanted_spec, spec;
const char *env;
……
}
這里遇到一個(gè)問題,SDL_OpenAudioDevice這個(gè)函數(shù)刪除SDLWindow等代碼后,竟然無法打開音頻,提示錯(cuò)誤
SDL_OpenAudio (2 channels, 44100 Hz): Audio subsystem is not initialized
但是使用SDL_OpenAudio卻可以,不知道什么原因,只能先使用SDL_OpenAudio,找到方法后再來解決。
while (!(audio_dev = SDL_OpenAudioDevice(NULL, 0, &wanted_spec, &spec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_CHANNELS_CHANGE))) {
……
}
SDL_CloseAudioDevice(audio_dev);
SDL_PauseAudioDevice(audio_dev, 0);
while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
……
}
SDL_CloseAudio();
SDL_PauseAudio(0);
三、關(guān)于視頻
修改refresh_loop_wait_event函數(shù)
static int video_refresh_thread(void *arg) {
VideoState *is = arg;
double remaining_time = 0.0;
//每remaining_time運(yùn)行一次循環(huán)(刷新一次屏幕)
while (!is->abort_request) {
if (remaining_time > 0.0) {
av_usleep((int64_t)(remaining_time * 1000000.0));
}
remaining_time = REFRESH_RATE;
if (is->show_mode != SHOW_MODE_NONE && (!is->paused || is->force_refresh)) {
video_refresh(is, &remaining_time);
}
}
return 0;
}
修改video_image_display函數(shù)
static void video_image_display(VideoState *is){
Frame *vp = frame_queue_peek_last(&is->pictq);
if (vp->frame) {
enum AVPixelFormat sw_pix_fmt = is->viddec.avctx->sw_pix_fmt;
if (sw_pix_fmt == AV_PIX_FMT_YUV420P || sw_pix_fmt == AV_PIX_FMT_YUVJ420P){
VideoFrame *videoFrame = (VideoFrame *)malloc(sizeof(VideoFrame));
videoFrame->width = vp->width;
videoFrame->height = vp->height;
videoFrame->format = AV_PIX_FMT_YUV420P;
videoFrame->planar = 3;
videoFrame->pixels[0] = malloc(vp->width * vp->height);
videoFrame->pixels[1] = malloc(vp->width * vp->height);
videoFrame->pixels[2] = malloc(vp->width * vp->height);
copyYUVFrameData(vp->frame->data[0], videoFrame->pixels[0], vp->frame->linesize[0], vp->width, vp->height);
copyYUVFrameData(vp->frame->data[1], videoFrame->pixels[1], vp->frame->linesize[1], vp->width / 2, vp->height / 2);
copyYUVFrameData(vp->frame->data[2], videoFrame->pixels[2], vp->frame->linesize[2], vp->width / 2, vp->height / 2);
display_frame(glkView, videoFrame);
free(videoFrame->pixels[0]);
free(videoFrame->pixels[1]);
free(videoFrame->pixels[2]);
free(videoFrame);
}
}
}
四、關(guān)于線程
SDL里自帶線程模塊,我們可以直接使用。刪除掉SDLEvent后,使用SDLThread創(chuàng)建一個(gè)畫面刷新的線程,用于刷新每一幀數(shù)據(jù)。
is->video_refresh_tid = SDL_CreateThread(video_refresh_thread, "video_refresh_thread",is);
五、其他
- main函數(shù)改成對(duì)外公布的
preparePlayerWindow函數(shù),傳遞三個(gè)參數(shù):UIView、videoStare和播放地址url,解碼得到的數(shù)據(jù)就要渲染到這個(gè)UIView上。 - ffplay.c文件里定義的結(jié)構(gòu)體對(duì)外公布,移動(dòng)到ffplay.h頭文件里。
- 注釋或者刪除SDLWindow、SDLRender相關(guān)代碼(要?jiǎng)h的很多,參考ffplay.c里注釋掉的代碼)
- UIView對(duì)外提供
display_frame方法,接收一個(gè)視頻數(shù)據(jù)VideoFrame參數(shù),不斷調(diào)用來刷新畫面 - 創(chuàng)建
VideoFrame結(jié)構(gòu)體用于存儲(chǔ)傳遞給UIView的數(shù)據(jù)
typedef struct VideoFrame VideoFrame;
struct VideoFrame {
int width;
int height;
UInt8 *pixels[AV_NUM_DATA_POINTERS];
int pitches[AV_NUM_DATA_POINTERS];
int planar;
enum AVPixelFormat format;
};
六、shader編寫
關(guān)于openGl的知識(shí),本人也在學(xué)習(xí)當(dāng)中,并非精通,只是略知一二,可以到這里OpenGL ES文集學(xué)習(xí)
- vertexShader
precision highp float;
varying highp vec2 vv2_texcoord;
attribute highp vec4 av4_position;
attribute highp vec2 av2_texcoord;
uniform mat4 um4_matrix;
void main(){
gl_Position = um4_matrix * av4_position;
vv2_texcoord = av2_texcoord;
}
- FragmentShader
precision highp float;
uniform highp sampler2D samplerY;
uniform highp sampler2D samplerU;
uniform highp sampler2D samplerV;
varying highp vec2 vv2_texcoord;
void main()
{
highp float y = texture2D(samplerY, vv2_texcoord).r;
highp float u = texture2D(samplerU, vv2_texcoord).r - 0.5;
highp float v = texture2D(samplerV, vv2_texcoord).r - 0.5;
highp float r = y + 1.402 * v;
highp float g = y - 0.344 * u - 0.714 * v;
highp float b = y + 1.772 * u;
gl_FragColor = vec4(r, g, b, 1.0);
}
七、openGl
openGl渲染AV_PIX_FMT_YUV420P的核心代碼
int widths[frame->planar];
int heights[frame->planar];
if (frame->format == AV_PIX_FMT_YUV420P){
widths[0] = frame->width;
widths[1] = frame->width / 2;
widths[2] = frame->width / 2;
heights[0] = frame->height;
heights[1] = frame->height / 2;
heights[2] = frame->height / 2;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for (int i = 0; i < frame->planar; ++i) {
glActiveTexture(GL_TEXTURE0 + i);//設(shè)置當(dāng)前活動(dòng)的紋理單元
glBindTexture(GL_TEXTURE_2D, plane_textures[i]);//texture綁定
if (frame->format == AV_PIX_FMT_YUV420P){
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, widths[i], heights[i], 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, frame->pixels[i]);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glUniform1i(us2_sampler[i], i);
}
總結(jié)
相比渲染到SDLWindow,渲染到UIView增加了很多工作量,特別是openGL這塊兒的知識(shí),建議先把最簡(jiǎn)iOS播放器(一)解碼流程、openGL等知識(shí)了解一下,再來看本篇。本篇里的項(xiàng)目使用軟解碼解碼視頻數(shù)據(jù),OpenGLES/ES2渲染YUV420,SDL_audio.h播放音頻數(shù)據(jù),SDL_threa.h管理線程,本來想把硬解碼也寫到項(xiàng)目里,不過本篇內(nèi)容已經(jīng)夠多,下篇再專門寫一下硬解碼,最后再把播放時(shí)間、總時(shí)長(zhǎng)、暫停等功能函數(shù)補(bǔ)全,做成一個(gè)完整的項(xiàng)目,可能做不怎么好,但是如果不去學(xué)習(xí)、練習(xí),怎么進(jìn)步。
播放器參考:
openGl的學(xué)習(xí)參考:
OpenGL Shading Language(GLSL)學(xué)習(xí)參考: