FrmSamsungPlayer1F.cpp 12 KB

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