FrmSamsungPlayer1F.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "XnsCommon.h"
  5. #include "XnsDeviceInterface.h"
  6. #include "FrmSamsungPlayer1F.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma link "XNSSDKWINDOWLib_OCX"
  10. #pragma link "XNSSDKDEVICELib_OCX"
  11. #pragma link "cxContainer"
  12. #pragma link "cxControls"
  13. #pragma link "cxEdit"
  14. #pragma link "cxGraphics"
  15. #pragma link "cxLabel"
  16. #pragma link "cxLookAndFeelPainters"
  17. #pragma link "cxLookAndFeels"
  18. #pragma link "dxSkinBlack"
  19. #pragma link "dxSkinMcSkin"
  20. #pragma link "dxSkinsCore"
  21. #pragma resource "*.dfm"
  22. //TFrmSamsungPlayer1 *FrmSamsungPlayer1 = NULL;
  23. //---------------------------------------------------------------------------
  24. __fastcall TFrmSamsungPlayer1::TFrmSamsungPlayer1(TComponent* Owner, TWinControl* AParent)
  25. : TForm(Owner)
  26. {
  27. Parent = AParent;
  28. Align = alClient;
  29. Application->ProcessMessages();
  30. //::CoInitialize(NULL);
  31. XnsSdkDevice1->Left = -500;
  32. XnsSdkWindow1->Parent = PnlClient;
  33. XnsSdkWindow1->Align = alClient;
  34. m_hDevice = 0; // Device handle
  35. m_hMediaSource = 0; // Media stream ID
  36. m_nControlId = 0; // Control ID
  37. m_bIsMediaPlay = false;
  38. FInitialize = false;
  39. }
  40. //---------------------------------------------------------------------------
  41. /*
  42. * form을 보여줄때 호출되는 event 메서드이다.
  43. * arguments
  44. * Sender : event handler 객체
  45. * return
  46. * void
  47. */
  48. void __fastcall TFrmSamsungPlayer1::FormShow(TObject *Sender)
  49. {
  50. TmrShow->Enabled = false;
  51. SetupWebCamera();
  52. }
  53. //---------------------------------------------------------------------------
  54. void __fastcall TFrmSamsungPlayer1::TmrShowTimer(TObject *Sender)
  55. {
  56. TmrShow->Enabled = false;
  57. #if 0
  58. Caption = FId + ": " + FLoc;
  59. WebCamPlay();
  60. SetFocus();
  61. #endif
  62. }
  63. //---------------------------------------------------------------------------
  64. void __fastcall TFrmSamsungPlayer1::FormClose(TObject *Sender, TCloseAction &Action)
  65. {
  66. CommClose();
  67. Action = caFree;
  68. }
  69. //---------------------------------------------------------------------------
  70. void __fastcall TFrmSamsungPlayer1::FormDestroy(TObject *Sender)
  71. {
  72. CommClose();
  73. }
  74. //---------------------------------------------------------------------------
  75. void __fastcall TFrmSamsungPlayer1::CommClose()
  76. {
  77. try {
  78. WebCamStop();
  79. } catch(...) {}
  80. }
  81. //---------------------------------------------------------------------------
  82. void __fastcall TFrmSamsungPlayer1::WebCamPlay()
  83. {
  84. try
  85. {
  86. //LblErrMsg->Visible = false;
  87. Caption = FId + ": " + FLoc;
  88. //if (!FInitialize)
  89. {
  90. //SetupWebCamera();
  91. }
  92. //WebCamStop();
  93. // 신규 영상 연결
  94. ConnectWebCamera();
  95. }
  96. catch(...)
  97. {
  98. }
  99. }
  100. //---------------------------------------------------------------------------
  101. void __fastcall TFrmSamsungPlayer1::WebCamStop()
  102. {
  103. try {
  104. DisconnectWebCamera();
  105. } catch(...) {}
  106. }
  107. //---------------------------------------------------------------------------
  108. bool __fastcall TFrmSamsungPlayer1::SetupWebCamera()
  109. {
  110. String sErrMsg;
  111. long nRet = XnsSdkDevice1->Initialize();
  112. if (nRet != ERR_SUCCESS && nRet != ERR_ALREADY_INITIALIZE)
  113. {
  114. sErrMsg.printf(L"Samsung XnsSdkDevice Initialize() failed.\r\nreturn=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
  115. //Application->MessageBox(sErrMsg.c_str(), Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
  116. ErrorMsg(sErrMsg);
  117. return false;
  118. }
  119. //nRet = XnsSdkWindow1->Initialize((long)PnlClient->Handle, NULL);
  120. nRet = XnsSdkWindow1->Initialize(NULL, NULL);
  121. if (nRet != ERR_SUCCESS && nRet != ERR_ALREADY_INITIALIZE)
  122. {
  123. sErrMsg.printf(L"Samsung XnsSdkWindow Initialize() failed.\r\nreturn=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
  124. //Application->MessageBox(sErrMsg.c_str(), Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
  125. ErrorMsg(sErrMsg);
  126. return false;
  127. }
  128. m_lDeviceId = XnsSdkDevice1->CreateDeviceEx();
  129. //m_lDeviceId = XnsSdkDevice1->CreateDevice(1);
  130. m_hDevice = XnsSdkDevice1->GetDeviceHandle(m_lDeviceId);
  131. //m_hDevice = XnsSdkDevice1->CreateDevice(1);
  132. if (m_hDevice == 0)
  133. {
  134. sErrMsg = "CreateDevice() failed.";
  135. sErrMsg.printf(L"Samsung XnsSdkWindow Initialize() failed.\r\nreturn=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
  136. //Application->MessageBox(sErrMsg.c_str(), Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
  137. ErrorMsg(sErrMsg);
  138. return false;
  139. }
  140. FInitialize = true;
  141. return true;
  142. }
  143. //---------------------------------------------------------------------------
  144. void __fastcall TFrmSamsungPlayer1::ReleaseWebCamera()
  145. {
  146. if (m_hDevice != 0)
  147. {
  148. if (m_hMediaSource)
  149. {
  150. XnsSdkWindow1->Stop();
  151. XnsSdkDevice1->CloseMedia(m_hDevice, m_hMediaSource);
  152. }
  153. if (XnsSdkDevice1->GetDeviceStatus(m_hDevice) == XDEVICE_STATUS_CONNECTED)
  154. {
  155. XnsSdkDevice1->Disconnect(m_hDevice);
  156. }
  157. //XnsSdkDevice1->ReleaseDevice(m_hDevice);
  158. }
  159. }
  160. //---------------------------------------------------------------------------
  161. bool __fastcall TFrmSamsungPlayer1::ConnectWebCamera()
  162. {
  163. bool bConnect = true;
  164. XnsSdkDevice1->SetConnectionInfo(
  165. m_hDevice, // [in] Device handle
  166. FDefModel.Detach(), // [in] Fixed as 'Samsung'
  167. FModelName.Detach(), // [in] Name of model to connect to. The maximum length allowed is 126-byte.
  168. XADDRESS_IP, FIpAddress.Detach(), // [in] Address type, actual address according to addresstype.
  169. FPort, 0, // [in] Port number, port number for web access
  170. FConnId.Detach(), FConnPass.Detach() // [in] Login ID, password
  171. );
  172. long nRet = XnsSdkDevice1->ConnectNonBlock(
  173. m_hDevice, // [in] Device handle
  174. false, // [in] Flag to decide where to forcibly log in or not.
  175. false // [in] If this value is 1, try to connect again.
  176. );
  177. //if (nRet != ERR_SUCCESS)
  178. {
  179. String sErrMsg;
  180. sErrMsg.printf(L"ConnectNonBlock() failed.\r\nerrno=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
  181. //Application->MessageBox(sErrMsg.c_str(), Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
  182. ErrorMsg(sErrMsg);
  183. bConnect = false;
  184. }
  185. return bConnect;
  186. }
  187. //---------------------------------------------------------------------------
  188. bool __fastcall TFrmSamsungPlayer1::OpenWebCamera()
  189. {
  190. XnsSdkDevice1->OpenMedia(m_hDevice, 3, MEDIA_TYPE_LIVE, 0, 0, &m_hMediaSource);
  191. XnsSdkWindow1->Start(m_hMediaSource);
  192. m_nControlId = 3;
  193. m_bIsMediaPlay = true;
  194. //XnsSdkWindow1->Visible = true;
  195. return true;
  196. }
  197. //---------------------------------------------------------------------------
  198. void __fastcall TFrmSamsungPlayer1::DisconnectWebCamera()
  199. {
  200. if (m_hDevice == 0) return;
  201. CloseWebCamera();
  202. // Disconnects from the device.
  203. if (XnsSdkDevice1->GetDeviceStatus(m_hDevice) == XDEVICE_STATUS_CONNECTED)
  204. {
  205. XnsSdkDevice1->Disconnect(m_hDevice);
  206. }
  207. XnsSdkDevice1->ReleaseDevice(m_hDevice);
  208. m_bIsMediaPlay = false;
  209. m_hMediaSource = 0;
  210. }
  211. //---------------------------------------------------------------------------
  212. void __fastcall TFrmSamsungPlayer1::CloseWebCamera()
  213. {
  214. if (!m_hMediaSource)
  215. {
  216. return;
  217. }
  218. if (m_bIsMediaPlay)
  219. {
  220. XnsSdkWindow1->Stop();
  221. m_bIsMediaPlay = false;
  222. }
  223. XnsSdkDevice1->CloseMedia(m_hDevice, m_hMediaSource);
  224. m_hMediaSource = 0;
  225. m_nControlId = 0;
  226. }
  227. //---------------------------------------------------------------------------
  228. void __fastcall TFrmSamsungPlayer1::XnsSdkDevice1ConnectFailed(TObject *Sender, long nDeviceID, long nControlID)
  229. {
  230. Caption = Caption + " - Connect Failed.";
  231. ErrorMsg(Caption);
  232. }
  233. //---------------------------------------------------------------------------
  234. void __fastcall TFrmSamsungPlayer1::XnsSdkDevice1DeviceStatusChanged(TObject *Sender, long nDeviceID, long nErrorCode, long nDeviceStatus,
  235. long nHddCondition)
  236. {
  237. if (nErrorCode != ERR_SUCCESS || nDeviceStatus != 1)
  238. {
  239. ErrorMsg("Device Status Change");
  240. return;
  241. }
  242. //if (nErrorCode == ERR_SUCCESS && nDeviceStatus == 1)
  243. {
  244. Caption = FId + ": " + FLoc;
  245. String sId = FId;
  246. String sName = FLoc;
  247. #if 0
  248. XnsSdkWindow1->SetOSDOnOff(OSD_ALL, false);
  249. XnsSdkWindow1->SetOSDOnOff(OSD_TIME1|OSD_IPS, true);
  250. XnsSdkWindow1->SetCustomOSDOnOff(false);
  251. WideString sOsdText = sName;
  252. XnsSdkWindow1->SetCustomOSD(sOsdText.Detach(), 1, 1, 255,255,255);
  253. XnsSdkWindow1->SetCustomOSDOnOff(true);
  254. XnsSdkWindow1->SetOSDOnOff(OSD_TIME1|OSD_IPS, true);
  255. #endif
  256. OpenWebCamera();
  257. }
  258. }
  259. //---------------------------------------------------------------------------
  260. int __fastcall TFrmSamsungPlayer1::CameraControl(int ACommand, int ASpeed)
  261. {
  262. long ret = -1;
  263. if (m_hDevice == 0 || m_nControlId == 0) return ret;
  264. int nPtzSpeed = ASpeed;
  265. // if( nCommand != XPTZ_STOP)
  266. // m_ctrlXnsDevice.ControlPtz(m_hDevice, m_nControlId, XPTZ_STOP, 30);
  267. // Returns the capabilities of the control module.
  268. long bIsCap = XnsSdkDevice1->GetControlCapability(m_hDevice, m_nControlId, XCTL_CAP_PTZ_SPEED);
  269. // [ XNS ACTIVEX HELP ]
  270. // -----------------------------------------------------------------------
  271. // Moves the camera to the predefined PTZ position. This function is
  272. // valid as long as the application is receiving media stream from the
  273. // camera.
  274. // -----------------------------------------------------------------------
  275. // Command Value Description
  276. // -----------------------------------------------------------------------
  277. // XPTZ_UP 1 Tilt up
  278. // XPTZ_DOWN 2 Tilt down
  279. // XPTZ_LEFT 3 Pan left
  280. // XPTZ_RIGHT 4 Pan right
  281. // XPTZ_UPLEFT 5 Tilt up and pan left
  282. // XPTZ_UPRIGHT 6 Tilt up and pan right
  283. // XPTZ_DOWNLEFT 7 Tilt down and pan left
  284. // XPTZ_DOWNRIGHT 8 Tilt down and pan right
  285. // XPTZ_ZOOMIN 9 Zoom in
  286. // XPTZ_ZOOMOUT 10 Zoom out
  287. // XPTZ_STOP 11 Stop the PTZ moving
  288. // XPTZ_FOCUS_NEAR 12 Focus near
  289. // XPTZ_FOCUS_FAR 13 Focus far
  290. // XPTZ_FOCUS_STOP 14 Stop focus moving
  291. // XPTZ_IRIS_OPEN 15 Open iris
  292. // XPTZ_IRIS_CLOSE 16 Close iris
  293. // -----------------------------------------------------------------------
  294. if (bIsCap)
  295. {
  296. ret = XnsSdkDevice1->ControlPtz(
  297. m_hDevice, // [in] Device handle. This value is returned from CreateDevice().
  298. m_nControlId, // [in] Control ID.
  299. ACommand, // [in] PTZ command
  300. nPtzSpeed // [in] If nPtzCommand is a speed-related command,
  301. // this value indicates the PTZ operation speed.
  302. // (1~100) This value is valid only if the camera
  303. // supports the XCTL_CAP_PTZ_SPEED capability.
  304. );
  305. }
  306. else
  307. {
  308. ret = XnsSdkDevice1->ControlPtz(m_hDevice, m_nControlId, ACommand, 30);
  309. }
  310. if (ret != ERR_SUCCESS)
  311. {
  312. String sErr;
  313. sErr.printf(L"ControlPtz() fail:: error=[%d](%s)", ret, XnsSdkDevice1->GetErrorString(ret));
  314. ErrorMsg(sErr);
  315. }
  316. }
  317. //---------------------------------------------------------------------------
  318. void __fastcall TFrmSamsungPlayer1::ErrorMsg(String AErrMsg)
  319. {
  320. LblErrMsg->Caption = AErrMsg;
  321. LblErrMsg->BringToFront();
  322. LblErrMsg->Transparent = true;
  323. LblErrMsg->Style->Font->Color = clWhite;
  324. LblErrMsg->Style->TextColor = clWhite;
  325. LblErrMsg->Visible = true;
  326. //Application->MessageBox(AErrMsg.c_str(), Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
  327. }
  328. //---------------------------------------------------------------------------