#ifndef VIDEOWIDGETGL_H
#define VIDEOWIDGETGL_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLBuffer>
#include <QTimer>
#include "kylincamera_global.h"

//OpenGL类的申明
QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram)
QT_FORWARD_DECLARE_CLASS(QOpenGLTexture)

#define FPS_MAX_FRAMES (200)

class VideoCapture;
class VideoDecoder;
class VideoEncoder;
class CCycleBuffer;
class AudioCapture;
class VideoDisplayGL : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT
public:
    explicit VideoDisplayGL(QWidget *parent, KylinCameraInfo *device);
    ~VideoDisplayGL();

    //void paintEvent(QPaintEvent *);

    CameraMode m_camera_mode;
    VideoCapture *videoCapture;
    VideoDecoder *videoDecoder;
    VideoEncoder *videoEncoder;
    AudioCapture *audioCapture;
    char *m_rgb_buf;

    FILE *rgb_fp;

    void set_src_ratio(unsigned int width, unsigned int height);
    void set_background_color(QColor color);
    void set_cover(float cover);

    void fps_init(void);
    void fps_handle(int time);
    int get_fps(void);
public Q_SLOTS:
    void slotShowYuv(uchar *ptr,uint width,uint height); //显示一帧Yuv图像

protected:
    void resizeGL(int w, int h);
    void initializeGL() Q_DECL_OVERRIDE;
    void paintGL()  Q_DECL_OVERRIDE;


    void setVboVertices(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top);

private:
    double des_w_h_ratio;
    double src_w_h_ratio;
    int scale_w;
    int scale_h;
    QColor background_color;

    char m_start_status;

    CCycleBuffer *m_capture_buf;
    CCycleBuffer *m_encoder_buf;
    CCycleBuffer *m_audio_buf;

    QOpenGLShaderProgram *program;
    QOpenGLBuffer vbo;
    GLuint textureUniformY,textureUniformU,textureUniformV,textureUniformC; 	//opengl中y、u、v分量位置
    QOpenGLTexture *textureY = {nullptr},*textureU = {nullptr},*textureV = {nullptr};
    GLuint idY,idU,idV; 			//自己创建的纹理对象ID，创建错误返回0
    uint videoW,videoH;
    uchar *yuvPtr = nullptr;
    float m_cover;

    int fps_frames[FPS_MAX_FRAMES];
    int fps_frames_pos;
    int fps;
Q_SIGNALS:
    void readout();
};

#endif // VIDEOWIDGETGL_H
