//---------------------------------------------------------------------------

#include <vcl.h>
#include "AppGlobalF.h"
#include "ITSLangTransF.h"
#pragma hdrstop

#include "FrmCrsCmraF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "AcesTechXPlayer2Lib_OCX"
#pragma link "FFBaseComponent"
#pragma link "FFBasePlay"
#pragma link "FFPlay"
#pragma resource "*.dfm"


#define LICENSE_KEY "FSXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX"

//TFrmCrsCamra *FrmCrsCamra;
//---------------------------------------------------------------------------
__fastcall TFrmCrsCamra::TFrmCrsCamra(TComponent* Owner, TCrossCam *ACmra)
    : TForm(Owner)
{
    FCmra = ACmra;
    FPlay = false;
    if (FCmra == NULL) {
        PnlTitle->Visible = false;
    }

#if FFPLAY
    AcesTechXPlayer2->Visible = false;

    FFPlayer->SetLicenseKey(LICENSE_KEY);
    FFPlayer->DisableFPUExceptions();
#endif

#if ACES
    AcesTechXPlayer2->Align = alClient;
    AcesTechXPlayer2->Visible = true;
#endif
}
//---------------------------------------------------------------------------
void __fastcall TFrmCrsCamra::FormClose(TObject *Sender, TCloseAction &Action)
{
    Action = caFree;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsCamra::FormDestroy(TObject *Sender)
{
    Disconnect();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsCamra::FFPlayerState(TObject *Sender, TPlayState APlayState)
{
enum TPlayState { psPlay, psPause, psResume, psStep, psStop, psEnd };

    if (APlayState == Ffbasecomponent::psStop || APlayState == Ffbasecomponent::psEnd)
    {
        PnlTitle->Caption = FCmra->CAM_NM + " : Disconnected";
        Timer1->Enabled = true;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsCamra::FormShow(TObject *Sender)
{
    Refresh();

    if (FCmra == NULL)
    {
        return;
    }

    PnlTitle->Caption = FCmra->CAM_NM;

    if (FCmra->RTSP_URL == "")
    {
        return;
    }

#if FFPLAY
    String sAVILibDir = g_sAppDir + "LibAV";
    if (!FFPlayer->AVLibLoaded())
    {
        if (!FFPlayer->LoadAVLib(sAVILibDir))
        {
            PnlTitle->Caption = FCmra->CAM_NM + ": 영상표출 라이브러리를 로드 실패!!";
            return;
        }
    }
#endif

    Timer1->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TFrmCrsCamra::Timer1Timer(TObject *Sender)
{
    Timer1->Enabled = false;
    Connect();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsCamra::Connect()
{
    Disconnect();

    try
    {
        PnlStream->Caption = "Connecting...";
        PnlTitle->Caption = FCmra->CAM_NM;

#if FFPLAY
        FFPlayer->AspectRatio = -1;
        FFPlayer->TryOpen(FCmra->RTSP_URL, PnlStream->Handle);
        FConHandle = (long)FFPlayer->ScreenHandle;
#endif
#if ACES
        AcesTechXPlayer2->URL = FCmra->RTSP_URL;
        FConHandle = AcesTechXPlayer2->ConnectAsync();
        if (FConHandle > 0)
        {
        }
#endif

        FPlay = true;
        Application->ProcessMessages();
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsCamra::Disconnect()
{
    if (!FPlay) return;

    FPlay = false;

    try
    {
#if FFPLAY
        if (FFPlayer->ScreenHandle != NULL)
        {
            FFPlayer->Stop(true);
            PostMessage(FFPlayer->ScreenHandle, CM_INVALIDATE, 0, 0);
            UpdateWindow(FFPlayer->ScreenHandle);
            FFPlayer->ScreenHandle = NULL;
        }
#endif
#if ACES
        if (FConHandle > 0)
        {
            AcesTechXPlayer2->Close();
        }
#endif
        FConHandle = 0;
        PnlStream->Caption = "Disconnected";

        Application->ProcessMessages();
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

