基于生产者-消费者模型的视频播放器 - 续2

昨天《基于生产者-消费者模型的播放器(基于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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值