#ifndef SAVESTREAM_H
#define SAVESTREAM_H

#include <QDir>
#include <QMutex>
#include <QMutexLocker>
#include <QObject>
#include <QQueue>
#include <QThread>

#include "avpacketqueuemanager.h"
#include "ffmpeginclude.h"

class SaveStream : public QObject {
    Q_OBJECT
public:
    explicit SaveStream(QObject *parent = nullptr);
    bool init(AVFormatContext *formatContext,
              AVPacketQueueManager *queueManager, int videoIndex);
    /**
     * @brief 设置拉流保存文件夹路径
     * @param fileDirPath 文件夹路径
     */
    void setSaveFileDirPath(QString fileDirPath);
    void close();
public slots:
    void startSaveStream();
signals:
    void startSaveStreamSignal();
    void sendErrorMessageSignal(QString message, int type);

private:
    bool openFile();
    void free();
    bool isValidAVPacket(AVPacket *pkt);

private:
    AVFormatContext *m_formatContextSave = nullptr;  // 封装上下文
    AVFormatContext *m_inputFormatContext = nullptr;
    // AVCodecContext *m_inputCodecContex = nullptr;  // 解码器上下文
    QString m_strCodecName;                // 编解码器名称
    AVStream *m_videoStreamOut = nullptr;  // 输出视频流
    int m_videoIndex = 0;
    bool m_writeHeader = false;  // 是否写入文件头
    QString m_outputDirPath;
    bool m_start;

    // QQueue<AVPacket *> *m_saverQueue;
    AVPacketQueueManager *m_queueManager = nullptr;
    // QMutex m_mutex;
};

#endif  // SAVESTREAM_H
