1 Star 0 Fork 0

zobzob/videoplayer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
vpdecodevideothread.cpp 2.53 KB
一键复制 编辑 原始数据 按行查看 历史
zobzob 提交于 2023-10-06 23:44 . 暂停,停止,快进快退完全实现
#include "vpdecodevideothread.h"
#include <QDebug>
#include "vpentrance.h"
VpDecodeVideoThread::VpDecodeVideoThread()
{
m_stop = false;
}
bool VpDecodeVideoThread::start(AVCodecContext *codec_ctx, VpPacketQueue *video_pkt_queue, VpFrameQueue *video_fme_queue)
{
m_stop = false;
bool res = true;
if(!codec_ctx)
res = false;
thread decode_video_th(work, this, codec_ctx, video_pkt_queue, video_fme_queue);
decode_video_th.detach();
return res;
}
void VpDecodeVideoThread::stop()
{
m_stop = true;
}
void VpDecodeVideoThread::work(VpDecodeVideoThread* vp_decode_video_th, AVCodecContext *codec_ctx, VpPacketQueue *video_pkt_queue, VpFrameQueue *video_fme_queue)
{
qDebug()<<"decode video thread start";
VpEntrance::s_RUNNING_THREAD_NUM += 1;
int ret = -1;
while(!vp_decode_video_th->m_stop)
{
{
unique_lock<mutex> lock(video_fme_queue->m_mutex);
while(video_fme_queue->get_size() > MAX_VIDEO_FRAME_NUM && !vp_decode_video_th->m_stop)
video_fme_queue->m_cond.wait_for(lock, chrono::milliseconds(20));
}
VpPacket pkt;
VpFrame srcframe ;
do
{
ret = avcodec_receive_frame(codec_ctx, srcframe.m_frame);
if(ret == 0)
{
srcframe.setSerial(video_fme_queue->serial());
srcframe.m_pts = (srcframe.m_frame->pts == AV_NOPTS_VALUE) ? NAN : srcframe.m_frame->pts * av_q2d(video_fme_queue->m_tb);
srcframe.m_duration = (srcframe.m_frame->duration == AV_NOPTS_VALUE) ? NAN : srcframe.m_frame->duration * av_q2d(video_fme_queue->m_tb);
video_fme_queue->push(srcframe);
}
else if(ret == AVERROR_EOF)
{
avcodec_flush_buffers(codec_ctx);
break;
}
}while(ret != AVERROR(EAGAIN));
pkt = video_pkt_queue->pop();
if(pkt.serial() != video_fme_queue->serial())
{
video_fme_queue->flush_queue();
video_fme_queue->setSerial(pkt.serial());
}
ret = avcodec_send_packet(codec_ctx, pkt.m_packet);
if(ret == AVERROR_EOF) {
avcodec_flush_buffers(codec_ctx);
}else if(ret == AVERROR(EAGAIN)){
continue;
}
if(ret < 0) {
qDebug()<<"error"<<ret;
break;
}
}
{
lock_guard<mutex> lock(vp_decode_video_th->m_mutex);
VpEntrance::s_RUNNING_THREAD_NUM -= 1;
qDebug()<<"decode video thread end";
}
return;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/zobzob/videoplayer.git
git@gitee.com:zobzob/videoplayer.git
zobzob
videoplayer
videoplayer
master

搜索帮助