123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "XnsCommon.h"
- #include "XnsDeviceInterface.h"
- #include "FrmSamsungPlayer1F.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma link "XNSSDKWINDOWLib_OCX"
- #pragma link "XNSSDKDEVICELib_OCX"
- #pragma link "cxContainer"
- #pragma link "cxControls"
- #pragma link "cxEdit"
- #pragma link "cxGraphics"
- #pragma link "cxLabel"
- #pragma link "cxLookAndFeelPainters"
- #pragma link "cxLookAndFeels"
- #pragma link "dxSkinBlack"
- #pragma link "dxSkinMcSkin"
- #pragma link "dxSkinsCore"
- #pragma link "dxSkinBlue"
- #pragma link "dxSkinCaramel"
- #pragma link "dxSkinCoffee"
- #pragma link "dxSkinDarkRoom"
- #pragma link "dxSkinDarkSide"
- #pragma link "dxSkinFoggy"
- #pragma link "dxSkinGlassOceans"
- #pragma link "dxSkiniMaginary"
- #pragma link "dxSkinLilian"
- #pragma link "dxSkinLiquidSky"
- #pragma link "dxSkinLondonLiquidSky"
- #pragma link "dxSkinMoneyTwins"
- #pragma link "dxSkinOffice2007Black"
- #pragma link "dxSkinOffice2007Blue"
- #pragma link "dxSkinOffice2007Green"
- #pragma link "dxSkinOffice2007Pink"
- #pragma link "dxSkinOffice2007Silver"
- #pragma link "dxSkinOffice2010Black"
- #pragma link "dxSkinOffice2010Blue"
- #pragma link "dxSkinOffice2010Silver"
- #pragma link "dxSkinSeven"
- #pragma link "dxSkinSharp"
- #pragma link "dxSkinSilver"
- #pragma link "dxSkinStardust"
- #pragma resource "*.dfm"
- //TFrmSamsungPlayer1 *FrmSamsungPlayer1 = NULL;
- //---------------------------------------------------------------------------
- __fastcall TFrmSamsungPlayer1::TFrmSamsungPlayer1(TComponent* Owner, TWinControl* AParent)
- : TForm(Owner)
- {
- //Parent = AParent;
- Align = alClient;
- Application->ProcessMessages();
- //::CoInitialize(NULL);
- XnsSdkDevice1->Left = -500;
- XnsSdkWindow1->Parent = PnlClient;
- XnsSdkWindow1->Align = alClient;
- m_hDevice = 0; // Device handle
- m_hMediaSource = 0; // Media stream ID
- m_nControlId = 0; // Control ID
- m_bIsMediaPlay = false;
- FInitialize = false;
- }
- //---------------------------------------------------------------------------
- /*
- * form을 보여줄때 호출되는 event 메서드이다.
- * arguments
- * Sender : event handler 객체
- * return
- * void
- */
- void __fastcall TFrmSamsungPlayer1::FormShow(TObject *Sender)
- {
- SetupWebCamera();
- }
- //---------------------------------------------------------------------------
- void __fastcall TFrmSamsungPlayer1::FormClose(TObject *Sender, TCloseAction &Action)
- {
- CommClose();
- Action = caFree;
- }
- //---------------------------------------------------------------------------
- void __fastcall TFrmSamsungPlayer1::FormDestroy(TObject *Sender)
- {
- CommClose();
- }
- //---------------------------------------------------------------------------
- void __fastcall TFrmSamsungPlayer1::CommClose()
- {
- try {
- WebCamStop();
- } catch(...) {}
- }
- //---------------------------------------------------------------------------
- void __fastcall TFrmSamsungPlayer1::WebCamPlay()
- {
- try
- {
- Caption = FId + ": " + FLoc;
- DisconnectWebCamera();
- ConnectWebCamera();
- }
- catch(...)
- {
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TFrmSamsungPlayer1::WebCamStop()
- {
- try {
- DisconnectWebCamera();
- } catch(...) {}
- }
- //---------------------------------------------------------------------------
- bool __fastcall TFrmSamsungPlayer1::SetupWebCamera()
- {
- String sErrMsg;
- long nRet = XnsSdkDevice1->Initialize();
- if (nRet != ERR_SUCCESS && nRet != ERR_ALREADY_INITIALIZE)
- {
- sErrMsg.printf(L"Samsung XnsSdkDevice1 Initialize() failed.\r\nreturn=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
- ErrorMsg(sErrMsg);
- return false;
- }
- nRet = XnsSdkWindow1->Initialize(NULL, NULL);
- if (nRet != ERR_SUCCESS && nRet != ERR_ALREADY_INITIALIZE)
- {
- sErrMsg.printf(L"Samsung XnsSdkWindow1 Initialize() failed.\r\nreturn=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
- ErrorMsg(sErrMsg);
- return false;
- }
- m_lDeviceId = XnsSdkDevice1->CreateDeviceEx();
- m_hDevice = XnsSdkDevice1->GetDeviceHandle(m_lDeviceId);
- if (m_hDevice == 0)
- {
- sErrMsg = "CreateDevice() failed.";
- sErrMsg.printf(L"Samsung XnsSdkDevice1 GetDeviceHandle() failed.\r\nreturn=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
- ErrorMsg(sErrMsg);
- return false;
- }
- FInitialize = true;
- return FInitialize;
- }
- //---------------------------------------------------------------------------
- void __fastcall TFrmSamsungPlayer1::ReleaseWebCamera()
- {
- if (m_hDevice != 0)
- {
- if (m_hMediaSource)
- {
- XnsSdkWindow1->Stop();
- XnsSdkDevice1->CloseMedia(m_hDevice, m_hMediaSource);
- }
- if (XnsSdkDevice1->GetDeviceStatus(m_hDevice) == XDEVICE_STATUS_CONNECTED)
- {
- XnsSdkDevice1->Disconnect(m_hDevice);
- }
- XnsSdkDevice1->ReleaseDevice(m_hDevice);
- m_hDevice = 0;
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TFrmSamsungPlayer1::ConnectWebCamera()
- {
- bool bConnect = true;
- XnsSdkDevice1->SetConnectionInfo(
- m_hDevice, // [in] Device handle
- FDefModel.Detach(), // [in] Fixed as 'Samsung'
- FModelName.Detach(), // [in] Name of model to connect to. The maximum length allowed is 126-byte.
- XADDRESS_IP, FIpAddress.Detach(), // [in] Address type, actual address according to addresstype.
- FPort, 0, // [in] Port number, port number for web access
- FConnId.Detach(), FConnPass.Detach() // [in] Login ID, password
- );
- long nRet = XnsSdkDevice1->ConnectNonBlock(
- m_hDevice, // [in] Device handle
- false, // [in] Flag to decide where to forcibly log in or not.
- false // [in] If this value is 1, try to connect again.
- );
- if (nRet != ERR_SUCCESS)
- {
- String sErrMsg;
- sErrMsg.printf(L"XnsSdkDevice1 ConnectNonBlock() failed.\r\nerrno=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
- ErrorMsg(sErrMsg);
- bConnect = false;
- }
- return bConnect;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TFrmSamsungPlayer1::OpenWebCamera()
- {
- XnsSdkDevice1->OpenMedia(m_hDevice, 3, MEDIA_TYPE_LIVE, 0, 0, &m_hMediaSource);
- XnsSdkWindow1->Start(m_hMediaSource);
- m_nControlId = 3;
- m_bIsMediaPlay = true;
- return true;
- }
- //---------------------------------------------------------------------------
- void __fastcall TFrmSamsungPlayer1::DisconnectWebCamera()
- {
- if (m_hDevice == 0) return;
- CloseWebCamera();
- // Disconnects from the device.
- if (XnsSdkDevice1->GetDeviceStatus(m_hDevice) == XDEVICE_STATUS_CONNECTED)
- {
- XnsSdkDevice1->Disconnect(m_hDevice);
- }
- m_bIsMediaPlay = false;
- m_hMediaSource = 0;
- }
- //---------------------------------------------------------------------------
- void __fastcall TFrmSamsungPlayer1::CloseWebCamera()
- {
- if (!m_hMediaSource)
- {
- return;
- }
- if (m_bIsMediaPlay)
- {
- XnsSdkWindow1->Stop();
- m_bIsMediaPlay = false;
- }
- XnsSdkDevice1->CloseMedia(m_hDevice, m_hMediaSource);
- m_hMediaSource = 0;
- m_nControlId = 0;
- }
- //---------------------------------------------------------------------------
- void __fastcall TFrmSamsungPlayer1::XnsSdkDevice1ConnectFailed(TObject *Sender, long nDeviceID, long nControlID)
- {
- Caption = Caption + " - Connect Failed.";
- ErrorMsg(Caption);
- }
- //---------------------------------------------------------------------------
- void __fastcall TFrmSamsungPlayer1::XnsSdkDevice1DeviceStatusChanged(TObject *Sender, long nDeviceID, long nErrorCode, long nDeviceStatus,
- long nHddCondition)
- {
- if (nErrorCode != ERR_SUCCESS || nDeviceStatus != 1)
- {
- //ErrorMsg("Device Status Change");
- return;
- }
- //if (nErrorCode == ERR_SUCCESS && nDeviceStatus == 1)
- {
- Caption = FId + ": " + FLoc;
- String sId = FId;
- String sName = FLoc;
- #if 1
- XnsSdkWindow1->SetOSDOnOff(OSD_ALL, false);
- XnsSdkWindow1->SetOSDOnOff(OSD_TIME1|OSD_IPS, true);
- XnsSdkWindow1->SetCustomOSDOnOff(false);
- WideString sOsdText = sName;
- XnsSdkWindow1->SetCustomOSD(sOsdText.Detach(), 1, 1, 255,255,255);
- XnsSdkWindow1->SetCustomOSDOnOff(true);
- XnsSdkWindow1->SetOSDOnOff(OSD_TIME1|OSD_IPS, true);
- #endif
- OpenWebCamera();
- }
- }
- //---------------------------------------------------------------------------
- int __fastcall TFrmSamsungPlayer1::CameraControl(int ACommand, int ASpeed)
- {
- long ret = -1;
- if (m_hDevice == 0 || m_nControlId == 0) return ret;
- int nPtzSpeed = ASpeed;
- // if( nCommand != XPTZ_STOP)
- // m_ctrlXnsDevice.ControlPtz(m_hDevice, m_nControlId, XPTZ_STOP, 30);
- // Returns the capabilities of the control module.
- long bIsCap = XnsSdkDevice1->GetControlCapability(m_hDevice, m_nControlId, XCTL_CAP_PTZ_SPEED);
- // [ XNS ACTIVEX HELP ]
- // -----------------------------------------------------------------------
- // Moves the camera to the predefined PTZ position. This function is
- // valid as long as the application is receiving media stream from the
- // camera.
- // -----------------------------------------------------------------------
- // Command Value Description
- // -----------------------------------------------------------------------
- // XPTZ_UP 1 Tilt up
- // XPTZ_DOWN 2 Tilt down
- // XPTZ_LEFT 3 Pan left
- // XPTZ_RIGHT 4 Pan right
- // XPTZ_UPLEFT 5 Tilt up and pan left
- // XPTZ_UPRIGHT 6 Tilt up and pan right
- // XPTZ_DOWNLEFT 7 Tilt down and pan left
- // XPTZ_DOWNRIGHT 8 Tilt down and pan right
- // XPTZ_ZOOMIN 9 Zoom in
- // XPTZ_ZOOMOUT 10 Zoom out
- // XPTZ_STOP 11 Stop the PTZ moving
- // XPTZ_FOCUS_NEAR 12 Focus near
- // XPTZ_FOCUS_FAR 13 Focus far
- // XPTZ_FOCUS_STOP 14 Stop focus moving
- // XPTZ_IRIS_OPEN 15 Open iris
- // XPTZ_IRIS_CLOSE 16 Close iris
- // -----------------------------------------------------------------------
- if (bIsCap)
- {
- ret = XnsSdkDevice1->ControlPtz(
- m_hDevice, // [in] Device handle. This value is returned from CreateDevice().
- m_nControlId, // [in] Control ID.
- ACommand, // [in] PTZ command
- nPtzSpeed // [in] If nPtzCommand is a speed-related command,
- // this value indicates the PTZ operation speed.
- // (1~100) This value is valid only if the camera
- // supports the XCTL_CAP_PTZ_SPEED capability.
- );
- }
- else
- {
- ret = XnsSdkDevice1->ControlPtz(m_hDevice, m_nControlId, ACommand, 30);
- }
- if (ret != ERR_SUCCESS)
- {
- String sErr;
- sErr.printf(L"ControlPtz() fail:: error=[%d](%s)", ret, XnsSdkDevice1->GetErrorString(ret));
- ErrorMsg(sErr);
- }
- return ret;
- }
- //---------------------------------------------------------------------------
- void __fastcall TFrmSamsungPlayer1::ErrorMsg(String AErrMsg)
- {
- LblErrMsg->Caption = AErrMsg;
- LblErrMsg->BringToFront();
- LblErrMsg->Transparent = true;
- LblErrMsg->Style->Font->Color = clWhite;
- LblErrMsg->Style->TextColor = clWhite;
- LblErrMsg->Visible = true;
- LblErrMsg->Refresh();
- LblErrMsg->BringToFront();
- //Application->MessageBox(AErrMsg.c_str(), Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
- }
- //---------------------------------------------------------------------------
- int __fastcall TFrmSamsungPlayer1::Play(String AId,
- String ALoc,
- String AIpAddress,
- int APort,
- String AConnId,
- String AConnPass,
- String AComModel,
- String ADevModel)
- {
- FId = AId;
- FLoc = ALoc;
- FIpAddress = WideString(AIpAddress);
- FPort = APort;
- FConnId = WideString(AConnId);
- FConnPass = WideString(AConnPass);
- FDefModel = WideString(AComModel);
- FModelName = WideString(ADevModel);
- LblErrMsg->Visible = false;
- Caption = FId + ": " + FLoc;
- WebCamPlay();
- return 0;
- }
- //---------------------------------------------------------------------------
|