#ifndef LIVESTREAMPUSHCOMPONENT_H
#define LIVESTREAMPUSHCOMPONENT_H

#include "ImageIO.h"
#include <liveMedia.hh>
#include <BasicUsageEnvironment.hh>
#include <BasicUsageEnvironment.hh>
#include "LiveStreamMediaSubSession.h"
#include <thread>

struct CLiveStreamHandler;

struct LiveStreamParam
{
    uint        nFrameRate;
    uint        nWidth;
    uint        nHeight;
    std::string strStreamName;
    std::string strStreamInfo;
};

class CLiveStreamPushModule
{
public:
    CLiveStreamPushModule();
    ~CLiveStreamPushModule();

    //开启服务：打开一个线程运行live555推流框架
    //关闭服务：关闭线程
    //是否在服务中
    void StartLiveStreamService(int nPort);
    void StopLiveStreamService();
    bool IsInService();

    //创建一个实时流，推流，停止流
    //实时流是以LiveStreamParam中的strStreamName为ID的，即不能有重名的实时流
    //推流接口中的cv::Mat的参数必须和创建时传入的LiveStreamParam中的参数相同
    CLiveStreamHandler *AddOneLiveStream(LiveStreamParam sParam);
    bool PushImage(CLiveStreamHandler *pHandler,const cv::Mat &iMat);
    bool StopLiveStream(CLiveStreamHandler *pHandler);

    //将当前实时流保存成文件
    //开始保存，停止保存
    //strFileName为全路径，格式flv
    bool StartSaveToFile(CLiveStreamHandler *pHandler,std::string strFileName);
    bool StopSaveToFile(CLiveStreamHandler *pHandler);

private:
    int HandleUnicast();
    void WorkThread();
    bool CheckInService();

    CLiveStreamHandler *AlreadyHasStreamName(const std::string &strStreamName);
    bool ContainsStreamHandler(CLiveStreamHandler *pHandler);
    bool RemoveStreamHandler(CLiveStreamHandler *pHandler);

private:
    RTSPServer* m_pRTSPServer;
    UsageEnvironment* m_pEnv;
    volatile char m_szQuit;
    bool m_bInService;

    int m_nPort;

    std::vector<CLiveStreamHandler *> m_vLiveStreamHandlers;
};

#endif // LIVESTREAMPUSHCOMPONENT_H
