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

在《基于生产者-消费者模型的视频播放器 - 续2》中还存在两个问题:1. 视频播放结束后,窗口不能自动退出,且无法响应鼠标、键盘事件;2. 有时按 空格  暂停不生效,或延时生效。主要原因是在Consumer Thread中,有两个阻塞等待的地方:一个是SDL_WaitEvent(&event),等待按键或REFRESH event;另外一个是dequeue(&frame_q),从...
摘要由CSDN通过智能技术生成

在《基于生产者-消费者模型的视频播放器 - 续2》中还存在两个问题:

1. 视频播放结束后,窗口不能自动退出,且无法响应鼠标、键盘事件;

2. 有时按 空格  暂停不生效,或延时生效。

主要原因是在Consumer Thread中,有两个阻塞等待的地方:一个是SDL_WaitEvent(&event),等待按键或REFRESH event;另外一个是dequeue(&frame_q),从队列中获取视频帧。如果队列为空,则进入阻塞等待。如果在这个地方阻塞了,此时按键事件就无法响应了。

因此,对程序做了小改进,去掉了dequeue()里面的阻塞等待。这个地方确实用得不对,因为在同一个循环里面,已经有了一处阻塞等待的地方。另外,加了一个快进功能。每按一次方向右键,快进30秒(通过快速播放30*25帧画面实现)。

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.
 *
 * 20180601 Modifications:
 * 1. Fix bug which Player Window can't quit when end of video.
 * 2. Add fast forward function.
 */
 
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <sys/stat.h>
#include <pthread.h>
#include <SDL2/SDL.h>
#include "video_frame_queue.h"

#define FRAMES_NUM_PER_SEC           25   /* how many frames each second */
#define FRAMES_SKIP_ON_FAST_FORWARD (30*FRAMES_NUM_PER_SEC)   /* 10 seconds per key event(Right Key) */
#define FRAME_REFRESH_EVENT         (SDL_USEREVENT+1)
#define FRAME_REFRESH_TIMER          40  /* 40ms */

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;

typedef struct {
	int end_of_video;
	int play_stop;
	int play_pause;
	int fastforward_skip_frames;
	int frame_refresh_timer; 
} VideoState;

/* The queue which stores uncompressed frame data */
avframe_q frame_q;
/* Video play state */
VideoState av_state = {0, 0, 0, 0, FRAME_REFRESH_TIMER};

/* Send a refresh signal to Consumer Thread every VideoState.frame_refresh_timer(it's 40ms by default). */
static int frame_refresh_thread(void *arg)
{
	while(!av_state.end_of_video && !av_state.play_stop) {
		if(!av_state.play_pause) {
			SDL_Event event;
			event.type = FRAME_REFRESH_EVENT;
			SDL_PushEvent(&event);
		}
		SDL_Delay(av_state.frame_refresh_timer);
	}
	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: The packet must be freed with av_packet_unref(). 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值