/***************************************************************************************
 *
 *  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
 *
 *  By downloading, copying, installing or using the software you agree to this license.
 *  If you do not agree to this license, do not download, install, 
 *  copy or use the software.
 *
 *  Copyright (C) 2014-2024, Happytimesoft Corporation, all rights reserved.
 *
 *  Redistribution and use in binary forms, with or without modification, are permitted.
 *
 *  Unless required by applicable law or agreed to in writing, software distributed 
 *  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
 *  language governing permissions and limitations under the License.
 *
****************************************************************************************/

#ifndef FILE_PLAYER_H
#define FILE_PLAYER_H

#include "video_player.h"

#define FILE_EVE_STOPPED    80          // stopped
#define FILE_EVE_CONNECTING 81          // connecting
#define FILE_EVE_CONNFAIL   82          // connect failed
#define FILE_EVE_CONNSUCC   83          // connect success
#define FILE_EVE_NOSIGNAL   84          // no signal
#define FILE_EVE_RESUME     85          // resume 
#define FILE_EVE_AUTHFAILED 86          // authentication failed
#define FILE_EVE_NODATA   	87          // No data received within the timeout period

typedef struct
{
    uint8 *     data;
    int         size;
    int64		ts;
    int         waitnext;
} HTPACKET;

class CFilePlayer : public CVideoPlayer
{    
public:
    CFilePlayer();
    virtual ~CFilePlayer();

    BOOL    open(std::string fileName);
    void    close();
    BOOL    play();
    void    stop();
    BOOL    pause();
    BOOL    seek(int pos);
    int64   getElapse() {return m_nCurPos;};
    int64   getDuration() {return m_nDuration;}
    int     getVideoCodec();
    int     getAudioCodec();
    BOOL    onRecord();
    void    setBbox(cv::Rect bbox);
    void    setCrop(bool crop);;
    void    readThread();
    void    videoThread();
    void    audioThread();
    
private:
    BOOL    openFile(const char * filename);
    BOOL    readFrame();
    void    videoData(uint8 * data, int size, int64 pts, int waitnext);
    void    audioData(uint8 * data, int size, int64 pts);
    void    clearQueue(HQUEUE * queue);
    BOOL    seekStream(double pos);
    double  getFramerate();
    
private:
    int                 m_nAudioIndex;      // audio stream index
    int                 m_nVideoIndex;      // video stream index
    int64               m_nDuration;        // the stream duration, unit is millisecond
    int64               m_nCurPos;          // the current play position, unit is millisecond
    int                 m_bSeek;            // seek request
    double              m_dSeekPos;         // seek position, nit is millisecond
    int                 m_nNalLength;
    
    AVFormatContext   * m_pFormatContext;   // format context

    pthread_t           m_hReadThread;      // read thread
    pthread_t           m_hVideoThread;     // video thread
	pthread_t           m_hAudioThread;     // audio thread

    HQUEUE            * m_pVideoQueue;      // video queue
    HQUEUE            * m_pAudioQueue;      // audio queue
};

#endif


