#ifndef PUSHSTREAM_H
#define PUSHSTREAM_H

#include <QDebug>
#include <QMutex>
#include <QObject>
#include <QQueue>
#include <QThread>

#include "avpacketqueuemanager.h"
#include "ffmpeginclude.h"
class PushStream : public QObject {
    Q_OBJECT
public:
    explicit PushStream(QObject *parent = nullptr);
    /**
     * @brief 设置推流地址
     * @param url 远端推流地址
     */
    void setRemoteIP(QString url);
    bool init(AVFormatContext *inputFormatCtx,
              AVPacketQueueManager *queueManager);
    void close();
public slots:
    void pushStream(int64_t startTime);
signals:
    void startPushStreamSignal(int64_t startTime);
    void sendErrorMessageSignal(QString message, int type);

private:
    bool openNetworkStream(AVFormatContext *inputFormatCtx);
    int reconnect(int ret);
    void free();

private:
    // QQueue<AVPacket *> *m_pusherQueue = nullptr;
    AVFormatContext *m_inputFormatCtx = nullptr;  //
    AVFormatContext *m_outputFormatCtx = NULL;    //
    AVStream *m_istream = nullptr;
    AVStream *m_ostream = nullptr;
    bool m_bwriteHeader = false;
    int m_videoIndex = -1;
    QString m_pushStreamIP;  // 推流地址
    bool m_start = false;
    bool m_end = false;
    int64_t m_startTime;
    int64_t m_firstPts;
    int m_frm_cnt = 0;
    int MAXCONNECT = 12;
    int MAXDELAY = 40000;  // 最大休眠时间40ms
    AVPacketQueueManager *m_queueManager = nullptr;
    // QMutex m_mutex;

    AVRational m_inputTimeBase;
    AVRational m_inputFrameRate;
    AVRational m_outputTimeBase;
};

#endif  // PUSHSTREAM_H
