昨天《基于生产者-消费者模型的播放器(基于FFMPEG 4.0和SDL2.0.8,在Ubuntu 14.04下开发验证)》中的程序,存在两个遗留问题,今天都修改好了。之所以新开一篇而不是在原来的基础上编辑,是为了保留这个修改的过程。通过前后对比,可以清楚看到做了哪些修改。
MyPlayerV2.c:
/*
* A simple player with FFMPEG4.0 and SDL2.0.8.
* Created by LiuWei@20180530
* 1. Only support video decoder, not support audio and subtitle.
* 2. Based on Producer-Consumer thread model.
*
* 20180531 Modifications:
* 1. Add key event process
* 2. Producer thread produce frames every 40ms, using the same frequence with Consumer thread.
*/
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <sys/stat.h>
#include <pthread.h>
#include <SDL2/SDL.h>
#include "video_frame_queue.h"
static AVFormatContext *fmt_ctx = NULL;
static AVStream *video_stream = NULL;
static AVCodec *dec = NULL;
static AVCodecContext *video_dec_ctx = NULL;
static int video_stream_index = -1;
static int end_of_video = 0;
static int play_stop = 0;
static int play_pause = 0;
/* The queue which stores uncompressed frame data */
avframe_q frame_q;
#define FRAME_REFRESH_EVENT (SDL_USEREVENT+1)
static int frame_refresh_thread(void *arg)
{
while(!end_of_video && !play_stop) {
if(!play_pause) {
SDL_Event event;
event.type = FRAME_REFRESH_EVENT;
SDL_PushEvent(&event);
}
SDL_Delay(40);
}
return 0;
}
/* Output uncompressed frame, then push into queue */
static void *producer_thread(void *arg)
{
int ret = 0;
AVPacket pkt;
AVFrame *video_frame = av_frame_alloc();
if(!video_frame) {
printf("producer_thread(): Could not allocate frame\n");
return;
}
/* Initialize packet, send data to NULL, let the demuxer fill it */
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
/* Read frames */
while(!play_stop && av_read_frame(fmt_ctx, &pkt) >= 0) { /* The packet must be freed with av_packet_unref() */
if(pkt.stream_index != video_stream_index) {
av_packet_unref(&pkt);
continue;
}
ret = avcodec_send_packet(video_dec_ctx, &pkt);
if(ret < 0) {
av_packet_unref(&pkt);
continue;
}
do {
ret = avcodec_receive_frame(video_dec_ctx, video_frame);
if(ret < 0)
break;
else if(ret == 0) { /* Got a frame successfully */
/* It will be blocked if queue is full. */
enqueue(&frame_q, video_frame);
} else if(ret == AVERROR_EOF) {
avcodec_flush_buffers(video_dec_ctx);
break;
}
} while(ret != AVERROR(EAGAIN));
av_p