#ifndef VIDEOENCODER_H
#define VIDEOENCODER_H

#include <QMutex>
#include <QThread>
typedef struct _X264Context X264Context;
typedef struct _VAAPIContext VAAPIContext;
typedef struct _HWEncContext HWEncContext;
typedef struct _YUV_Surf YUV_Surf;

class CCycleBuffer;
class VideoEncoder :  public QThread
{
    Q_OBJECT
public:
    VideoEncoder(CCycleBuffer *video_buf, CCycleBuffer *audio_buf, int width, int height, int fps);
    ~VideoEncoder();

    int encoder_init(int width, int height, int fps);
    int encoder_deinit();

    int encoder_audio_init(unsigned long rate, unsigned int channel, unsigned int size);
    bool encoder_hwenc_init();

    int recordStart(char *videoname, bool hwenc, int nsamplerate, int nchannels, int nsamplesize, int enclevel);
    void recordStop();

    X264Context *m_ctx;

    int m_buf_size;
    uint8_t *m_buf;
    int m_width;
    int m_height;
    int m_fps;
    bool m_stopped;
    bool m_started;
    QMutex m_mutex;

    double m_real_time;
    long long m_pause_time;
    long long m_time;

    long long m_record_time;      //编码时间
    long long m_start_time;       //开始时间

private:
    void run();
    void encode_last_data();

    FILE *m_fp;
    VAAPIContext *m_va_ctx;
    HWEncContext *m_enc_ctx;
    YUV_Surf *m_nv12_surf;

    bool m_hwenc_support;
    CCycleBuffer *m_encoder_buf;
    CCycleBuffer *m_audio_buf;
    unsigned long m_mp4_file;

    /*AACparam*/
    unsigned long m_rate;           //sample rate
    unsigned int m_channels;	       //audio channels
    unsigned long m_bit_size;           //sample bit size
    unsigned long m_input_size;         //一次送入编码的样本大小
    unsigned long m_output_size;       //一次最大AAC数据输出大小
    void *audio_encoder;              //编码器句柄
    void *audio_config;    //编码器设置句柄
    char *m_pcm_buf;           //用于存储输入的PCM数据
    unsigned char *m_aac_buf;           //用于存储输出的AAC数据
    uint32_t m_pcm_size;
    int m_enc_level;
public Q_SLOTS:
    void encode_fix(int width, int height);

};

#endif // VIDEOENCODER_H
