前言
上篇最簡iOS播放器(一)利用ffmpeg和SDL構(gòu)建了最簡iOS播放器贺辰,但視頻數(shù)據(jù)是渲染到了SDLWindow上佳魔,這個(gè)結(jié)果肯定不是我們想要的鸿市,渲染到UIView上我們才能更好的操作控制視頻的播放嘁酿,本篇的目的便是把數(shù)據(jù)渲染到UIView上廊镜。
正文
一牙肝、思路分析
以上篇博客中構(gòu)建好的項(xiàng)目為基礎(chǔ)來改造項(xiàng)目,用UIView代替SDLWindow嗤朴,用openGL來渲染代替SDLRender配椭。總體架構(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ù)改成對外公布的
preparePlayerWindow
函數(shù),傳遞三個(gè)參數(shù):UIView角骤、videoStare和播放地址url隅忿,解碼得到的數(shù)據(jù)就要渲染到這個(gè)UIView上。 - ffplay.c文件里定義的結(jié)構(gòu)體對外公布邦尊,移動(dòng)到ffplay.h頭文件里背桐。
- 注釋或者刪除SDLWindow、SDLRender相關(guān)代碼(要?jiǎng)h的很多蝉揍,參考ffplay.c里注釋掉的代碼)
- UIView對外提供
display_frame
方法链峭,接收一個(gè)視頻數(shù)據(jù)VideoFrame
參數(shù),不斷調(diào)用來刷新畫面 - 創(chuàng)建
VideoFrame
結(jié)構(gòu)體用于存儲傳遞給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的知識弊仪,本人也在學(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這塊兒的知識,建議先把最簡iOS播放器(一)解碼流程表窘、openGL等知識了解一下典予,再來看本篇。本篇里的項(xiàng)目使用軟解碼解碼視頻數(shù)據(jù)乐严,OpenGLES/ES2
渲染YUV420瘤袖,SDL_audio.h
播放音頻數(shù)據(jù),SDL_threa.h
管理線程昂验,本來想把硬解碼也寫到項(xiàng)目里捂敌,不過本篇內(nèi)容已經(jīng)夠多昭娩,下篇再專門寫一下硬解碼,最后再把播放時(shí)間黍匾、總時(shí)長、暫停等功能函數(shù)補(bǔ)全呛梆,做成一個(gè)完整的項(xiàng)目锐涯,可能做不怎么好,但是如果不去學(xué)習(xí)填物、練習(xí)纹腌,怎么進(jìn)步。
播放器參考:
openGl的學(xué)習(xí)參考:
OpenGL Shading Language(GLSL)學(xué)習(xí)參考: