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

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

#include "FRAME_CameraF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "AcesTechXPlayer2Lib_OCX"
#pragma link "FFBaseComponent"
#pragma link "FFBasePlay"
#pragma link "FFPlay"
#pragma link "dxGDIPlusClasses"
#pragma resource "*.dfm"
TFRAMECamera *FRAMECamera;
#define LICENSE_KEY "FSXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX"
#define FFPLAY  0
#define ACES    1

//---------------------------------------------------------------------------
__fastcall TFRAMECamera::TFRAMECamera(TComponent* Owner)
    : TFrame(Owner)
{
}
//---------------------------------------------------------------------------
bool __fastcall TFRAMECamera::Init(String AMngrNmbr, String ACtlrId, String ACtlrNm, String AStreamUrl)
{
#if FFPLAY
    AcesTechXPlayer2->Visible = false;

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

#if ACES
    AcesTechXPlayer2->Align = alClient;
    AcesTechXPlayer2->Visible = true;
#endif

    Disconnect();

    PnlTitle->Visible = true;

    FMngrNmbr = AMngrNmbr;
    FCtlrId = ACtlrId;
    FCtlrNm = ACtlrNm;
    FStreamUrl = AStreamUrl;
    FPlay = false;
    FConHandle = 0;

    PnlTitle->Caption = FCtlrNm;
    PnlStream->Font->Color = clWhite;

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


    if (FStreamUrl == "") {
        PnlTitle->Caption = FCtlrNm + ", Stream URL Empty!";
        PnlStream->Caption = "Invalid Stream Address!!!";//영상주소가 설정되지 않았습니다.";
        PnlStream->Font->Color = clWhite;
    }
    ImgDefault->Visible = false;

    Timer1->Enabled = true;

    return true;
}
//---------------------------------------------------------------------------

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

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

    try
    {
        if (FStreamUrl != "") {
            PnlStream->Caption = FStreamUrl + ", Connecting...";
            PnlTitle->Caption = FCtlrNm;

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

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

void __fastcall TFRAMECamera::Disconnect()
{
    Timer1->Enabled = false;
    if (!FPlay) return;

    FPlay = false;

    try
    {
        if (FStreamUrl != "") {
#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
            PnlStream->Caption = "Disconnected";
        }
        FConHandle = 0;

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

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

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

