#include "qffwidget.h"
#include "ui_qffwidget.h"

QFfWidget::QFfWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::QFfWidget)
{
    ui->setupUi(this);
    VideoDecThread = new QThread(this);
    OVideoDec = new VideoDec;
    readTimer = new QTimer(this);
    BUFFINDEX = 0;
    OVideoDec->moveToThread(VideoDecThread);
    VideoDecThread->start();
    connect(this,SIGNAL(init_s()),OVideoDec,SLOT(init()));
    connect(this,SIGNAL(play_s()),OVideoDec,SLOT(play()));
    //connect(OVideoDec,SIGNAL(SendImage(QImage)),this,SLOT(showVideo(QImage)));
    //connect(OVideoDec,SIGNAL(SendImage(QImage)),this,SLOT(showVideo(QImage)));
    connect(readTimer,SIGNAL(timeout()),this,SLOT(showVideo()));
    FileName =QFileDialog::getOpenFileName(this,"Open Video","","video file(*.mp4 *.rmvb)");
    QByteArray byte = FileName.toLocal8Bit();
    OVideoDec->FileName = byte.data();
    emit init_s();
    emit play_s();
    readTimer->start(35);
}

QFfWidget::~QFfWidget()
{

    if(OVideoDec!=nullptr)
    {
        OVideoDec->stop();
        delete OVideoDec;
    }
    if(VideoDecThread!=nullptr)
    {
        VideoDecThread->quit();
        delete VideoDecThread;
    }
    delete ui;
}
void QFfWidget::on_VideoPlay_clicked()
{
    emit init_s();
    readTimer->start(35);
}
void QFfWidget::showVideo()
{

    QPixmap pix;
    if(OVideoDec->VideoImg.isEmpty()) return;
    pix = pix.fromImage(OVideoDec->VideoImg.at(BUFFINDEX));//.scaledToWidth(ui->VideoShow->width());
    ui->VideoShow->setPixmap(pix);
    if(BUFFINDEX>50){
        BUFFINDEX = 0;
        OVideoDec->BUFFSIZE = 0;
        OVideoDec->VideoImg.clear();
        return;
    }
    BUFFINDEX++;
}

void QFfWidget::on_btnOpenFile_clicked()
{
    FileName = QFileDialog::getOpenFileName(this,"Open Video","","video file(*.mp4 *.rmvb)");
    QByteArray byte = FileName.toLocal8Bit();
    OVideoDec->FileName = byte.data();
}
