FrmSamsungPlayer1F.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 link "AcesTechXPlayer2Lib_OCX"
  46. #pragma resource "*.dfm"
  47. //TFrmSamsungPlayer1 *FrmSamsungPlayer1 = NULL;
  48. //---------------------------------------------------------------------------
  49. __fastcall TFrmSamsungPlayer1::TFrmSamsungPlayer1(TComponent* Owner, TXCctv *ACctv, bool AAutoPlay)
  50. : TForm(Owner)
  51. {
  52. Align = alCustom;
  53. Application->ProcessMessages();
  54. //::CoInitialize(NULL);
  55. XnsSdkDevice1->Left = -500;
  56. XnsSdkWindow1->Parent = PnlCamera;
  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. FPlay = false;
  64. FAutoPlay = AAutoPlay;
  65. FInstalled = false;
  66. InitCamera(ACctv, FAutoPlay);
  67. }
  68. //---------------------------------------------------------------------------
  69. /*
  70. * form을 보여줄때 호출되는 event 메서드이다.
  71. * arguments
  72. * Sender : event handler 객체
  73. * return
  74. * void
  75. */
  76. void __fastcall TFrmSamsungPlayer1::FormShow(TObject *Sender)
  77. {
  78. SetupWebCamera();
  79. }
  80. //---------------------------------------------------------------------------
  81. void __fastcall TFrmSamsungPlayer1::FormClose(TObject *Sender, TCloseAction &Action)
  82. {
  83. CommClose();
  84. Action = caFree;
  85. }
  86. //---------------------------------------------------------------------------
  87. void __fastcall TFrmSamsungPlayer1::FormDestroy(TObject *Sender)
  88. {
  89. CommClose();
  90. }
  91. //---------------------------------------------------------------------------
  92. void __fastcall TFrmSamsungPlayer1::CommClose()
  93. {
  94. try {
  95. WebCamStop();
  96. } catch(...) {}
  97. }
  98. //---------------------------------------------------------------------------
  99. void __fastcall TFrmSamsungPlayer1::WebCamPlay()
  100. {
  101. try
  102. {
  103. Caption = FId + ": " + FLoc;
  104. DisconnectWebCamera();
  105. ConnectWebCamera();
  106. }
  107. catch(...)
  108. {
  109. }
  110. }
  111. //---------------------------------------------------------------------------
  112. void __fastcall TFrmSamsungPlayer1::WebCamStop()
  113. {
  114. try {
  115. DisconnectWebCamera();
  116. } catch(...) {}
  117. }
  118. //---------------------------------------------------------------------------
  119. bool __fastcall TFrmSamsungPlayer1::SetupWebCamera()
  120. {
  121. if (FInitialize)
  122. {
  123. // ShowMessage("Already SetupWebCamera");
  124. return true;
  125. }
  126. String sErrMsg;
  127. long nRet = XnsSdkDevice1->Initialize();
  128. if (nRet != ERR_SUCCESS && nRet != ERR_ALREADY_INITIALIZE)
  129. {
  130. sErrMsg.printf(L"Samsung XnsSdkDevice1 Initialize() failed.\r\nreturn=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
  131. ErrorMsg(sErrMsg);
  132. return false;
  133. }
  134. nRet = XnsSdkWindow1->Initialize(NULL, NULL);
  135. if (nRet != ERR_SUCCESS && nRet != ERR_ALREADY_INITIALIZE)
  136. {
  137. sErrMsg.printf(L"Samsung XnsSdkWindow1 Initialize() failed.\r\nreturn=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
  138. ErrorMsg(sErrMsg);
  139. return false;
  140. }
  141. m_lDeviceId = XnsSdkDevice1->CreateDeviceEx();
  142. m_hDevice = XnsSdkDevice1->GetDeviceHandle(m_lDeviceId);
  143. if (m_hDevice == 0)
  144. {
  145. sErrMsg = "CreateDevice() failed.";
  146. sErrMsg.printf(L"Samsung XnsSdkDevice1 GetDeviceHandle() failed.\r\nreturn=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
  147. ErrorMsg(sErrMsg);
  148. return false;
  149. }
  150. // ShowMessage("SetupWebCamera");
  151. FInitialize = true;
  152. return FInitialize;
  153. }
  154. //---------------------------------------------------------------------------
  155. void __fastcall TFrmSamsungPlayer1::ReleaseWebCamera()
  156. {
  157. if (m_hDevice != 0)
  158. {
  159. if (m_hMediaSource)
  160. {
  161. XnsSdkWindow1->Stop();
  162. XnsSdkDevice1->CloseMedia(m_hDevice, m_hMediaSource);
  163. }
  164. if (XnsSdkDevice1->GetDeviceStatus(m_hDevice) == XDEVICE_STATUS_CONNECTED)
  165. {
  166. XnsSdkDevice1->Disconnect(m_hDevice);
  167. }
  168. XnsSdkDevice1->ReleaseDevice(m_hDevice);
  169. m_hDevice = 0;
  170. }
  171. }
  172. //---------------------------------------------------------------------------
  173. bool __fastcall TFrmSamsungPlayer1::ConnectWebCamera()
  174. {
  175. bool bConnect = true;
  176. XnsSdkDevice1->SetConnectionInfo(
  177. m_hDevice, // [in] Device handle
  178. FDefModel.Detach(), // [in] Fixed as 'Samsung'
  179. FModelName.Detach(), // [in] Name of model to connect to. The maximum length allowed is 126-byte.
  180. XADDRESS_IP, FIpAddress.Detach(), // [in] Address type, actual address according to addresstype.
  181. FPort, 0, // [in] Port number, port number for web access
  182. FConnId.Detach(), FConnPass.Detach() // [in] Login ID, password
  183. );
  184. long nRet = XnsSdkDevice1->ConnectNonBlock(
  185. m_hDevice, // [in] Device handle
  186. false, // [in] Flag to decide where to forcibly log in or not.
  187. false // [in] If this value is 1, try to connect again.
  188. );
  189. if (nRet != ERR_SUCCESS)
  190. {
  191. String sErrMsg;
  192. sErrMsg.printf(L"XnsSdkDevice1 ConnectNonBlock() failed.\r\nerrno=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
  193. ErrorMsg(sErrMsg);
  194. bConnect = false;
  195. }
  196. return bConnect;
  197. }
  198. //---------------------------------------------------------------------------
  199. bool __fastcall TFrmSamsungPlayer1::OpenWebCamera()
  200. {
  201. XnsSdkDevice1->OpenMedia(m_hDevice, 3, MEDIA_TYPE_LIVE, 0, 0, &m_hMediaSource);
  202. XnsSdkWindow1->Start(m_hMediaSource);
  203. m_nControlId = 3;
  204. m_bIsMediaPlay = true;
  205. return true;
  206. }
  207. //---------------------------------------------------------------------------
  208. void __fastcall TFrmSamsungPlayer1::DisconnectWebCamera()
  209. {
  210. if (m_hDevice == 0) return;
  211. CloseWebCamera();
  212. // Disconnects from the device.
  213. if (XnsSdkDevice1->GetDeviceStatus(m_hDevice) == XDEVICE_STATUS_CONNECTED)
  214. {
  215. XnsSdkDevice1->Disconnect(m_hDevice);
  216. }
  217. m_bIsMediaPlay = false;
  218. m_hMediaSource = 0;
  219. }
  220. //---------------------------------------------------------------------------
  221. void __fastcall TFrmSamsungPlayer1::CloseWebCamera()
  222. {
  223. if (!m_hMediaSource)
  224. {
  225. return;
  226. }
  227. if (m_bIsMediaPlay)
  228. {
  229. XnsSdkWindow1->Stop();
  230. m_bIsMediaPlay = false;
  231. }
  232. XnsSdkDevice1->CloseMedia(m_hDevice, m_hMediaSource);
  233. m_hMediaSource = 0;
  234. m_nControlId = 0;
  235. }
  236. //---------------------------------------------------------------------------
  237. void __fastcall TFrmSamsungPlayer1::XnsSdkDevice1ConnectFailed(TObject *Sender, long nDeviceID, long nControlID)
  238. {
  239. Caption = Caption + " - Connect Failed.";
  240. ErrorMsg(Caption);
  241. }
  242. //---------------------------------------------------------------------------
  243. void __fastcall TFrmSamsungPlayer1::XnsSdkDevice1DeviceStatusChanged(TObject *Sender, long nDeviceID, long nErrorCode, long nDeviceStatus,
  244. long nHddCondition)
  245. {
  246. if (nErrorCode != ERR_SUCCESS || nDeviceStatus != 1)
  247. {
  248. //ErrorMsg("Device Status Change");
  249. return;
  250. }
  251. //if (nErrorCode == ERR_SUCCESS && nDeviceStatus == 1)
  252. {
  253. Caption = FId + ": " + FLoc;
  254. String sId = FId;
  255. String sName = FLoc;
  256. #if 1
  257. XnsSdkWindow1->SetOSDOnOff(OSD_ALL, false);
  258. XnsSdkWindow1->SetOSDOnOff(OSD_TIME1|OSD_IPS, true);
  259. XnsSdkWindow1->SetCustomOSDOnOff(false);
  260. WideString sOsdText = sName;
  261. XnsSdkWindow1->SetCustomOSD(sOsdText.Detach(), 1, 1, 255,255,255);
  262. XnsSdkWindow1->SetCustomOSDOnOff(true);
  263. XnsSdkWindow1->SetOSDOnOff(OSD_TIME1|OSD_IPS, true);
  264. #endif
  265. OpenWebCamera();
  266. if (!XnsSdkWindow1->Visible) XnsSdkWindow1->Visible = true;
  267. }
  268. }
  269. //---------------------------------------------------------------------------
  270. int __fastcall TFrmSamsungPlayer1::CameraControl(int ACommand, int ASpeed)
  271. {
  272. long ret = -1;
  273. if (m_hDevice == 0 || m_nControlId == 0) return ret;
  274. int nPtzSpeed = ASpeed;
  275. // if( nCommand != XPTZ_STOP)
  276. // m_ctrlXnsDevice.ControlPtz(m_hDevice, m_nControlId, XPTZ_STOP, 30);
  277. // Returns the capabilities of the control module.
  278. long bIsCap = XnsSdkDevice1->GetControlCapability(m_hDevice, m_nControlId, XCTL_CAP_PTZ_SPEED);
  279. // [ XNS ACTIVEX HELP ]
  280. // -----------------------------------------------------------------------
  281. // Moves the camera to the predefined PTZ position. This function is
  282. // valid as long as the application is receiving media stream from the
  283. // camera.
  284. // -----------------------------------------------------------------------
  285. // Command Value Description
  286. // -----------------------------------------------------------------------
  287. // XPTZ_UP 1 Tilt up
  288. // XPTZ_DOWN 2 Tilt down
  289. // XPTZ_LEFT 3 Pan left
  290. // XPTZ_RIGHT 4 Pan right
  291. // XPTZ_UPLEFT 5 Tilt up and pan left
  292. // XPTZ_UPRIGHT 6 Tilt up and pan right
  293. // XPTZ_DOWNLEFT 7 Tilt down and pan left
  294. // XPTZ_DOWNRIGHT 8 Tilt down and pan right
  295. // XPTZ_ZOOMIN 9 Zoom in
  296. // XPTZ_ZOOMOUT 10 Zoom out
  297. // XPTZ_STOP 11 Stop the PTZ moving
  298. // XPTZ_FOCUS_NEAR 12 Focus near
  299. // XPTZ_FOCUS_FAR 13 Focus far
  300. // XPTZ_FOCUS_STOP 14 Stop focus moving
  301. // XPTZ_IRIS_OPEN 15 Open iris
  302. // XPTZ_IRIS_CLOSE 16 Close iris
  303. // -----------------------------------------------------------------------
  304. if (bIsCap)
  305. {
  306. ret = XnsSdkDevice1->ControlPtz(
  307. m_hDevice, // [in] Device handle. This value is returned from CreateDevice().
  308. m_nControlId, // [in] Control ID.
  309. ACommand, // [in] PTZ command
  310. nPtzSpeed // [in] If nPtzCommand is a speed-related command,
  311. // this value indicates the PTZ operation speed.
  312. // (1~100) This value is valid only if the camera
  313. // supports the XCTL_CAP_PTZ_SPEED capability.
  314. );
  315. }
  316. else
  317. {
  318. ret = XnsSdkDevice1->ControlPtz(m_hDevice, m_nControlId, ACommand, 30);
  319. }
  320. if (ret != ERR_SUCCESS)
  321. {
  322. String sErr;
  323. sErr.printf(L"ControlPtz() fail:: error=[%d](%s)", ret, XnsSdkDevice1->GetErrorString(ret));
  324. ErrorMsg(sErr);
  325. }
  326. return ret;
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TFrmSamsungPlayer1::ErrorMsg(String AErrMsg)
  330. {
  331. LblErrMsg->Caption = AErrMsg;
  332. LblErrMsg->BringToFront();
  333. LblErrMsg->Transparent = true;
  334. LblErrMsg->Style->Font->Color = clWhite;
  335. LblErrMsg->Style->TextColor = clWhite;
  336. LblErrMsg->Visible = true;
  337. LblErrMsg->Refresh();
  338. LblErrMsg->BringToFront();
  339. //Application->MessageBox(AErrMsg.c_str(), Caption.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
  340. }
  341. //---------------------------------------------------------------------------
  342. int __fastcall TFrmSamsungPlayer1::Play(String AId,
  343. String ALoc,
  344. String AIpAddress,
  345. int APort,
  346. String AConnId,
  347. String AConnPass,
  348. String AComModel,
  349. String ADevModel)
  350. {
  351. FId = AId;
  352. FLoc = ALoc;
  353. FIpAddress = WideString(AIpAddress);
  354. FPort = APort;
  355. FConnId = WideString(AConnId);
  356. FConnPass = WideString(AConnPass);
  357. FDefModel = WideString(AComModel);
  358. FModelName = WideString(ADevModel);
  359. LblErrMsg->Visible = false;
  360. WebCamPlay();
  361. return 0;
  362. }
  363. //---------------------------------------------------------------------------
  364. void __fastcall TFrmSamsungPlayer1::MnuConnectClick(TObject *Sender)
  365. {
  366. FAutoPlay = true;
  367. Connect();
  368. }
  369. //---------------------------------------------------------------------------
  370. void __fastcall TFrmSamsungPlayer1::MnuDisconnectClick(TObject *Sender)
  371. {
  372. FAutoPlay = false;
  373. Disconnect();
  374. }
  375. //---------------------------------------------------------------------------
  376. void __fastcall TFrmSamsungPlayer1::InitCamera(TXCctv *ACctv, bool AAutoPlay/*=true*/)
  377. {
  378. FAutoPlay = AAutoPlay;
  379. PopupMenu = NULL;
  380. if (!ACctv)
  381. {
  382. FId = "";
  383. FGroup = "";
  384. FName = "";
  385. FStreamingType = "";
  386. FViewAddress = "";
  387. FFullAddress = "";
  388. PnlCamera->Caption = "-Empty-";
  389. DisplayName(" ");
  390. FInstalled = false;
  391. XnsSdkWindow1->Visible = false;
  392. try
  393. {
  394. String sImageFile = ExtractFilePath(Application->ExeName) + "\\image\\vmscam.bmp";
  395. ImgNoData->Picture->LoadFromFile(sImageFile);
  396. ImgNoData->Align = alClient;
  397. ImgNoData->Visible = true;
  398. PnlName->Visible = false;
  399. }
  400. catch(Exception &e)
  401. {
  402. ImgNoData->Visible = false;
  403. }
  404. }
  405. else
  406. {
  407. FId = ACctv->Id;
  408. FGroup = ACctv->Group;
  409. FName = "[" + ACctv->CAMID + "] " + ACctv->Name;
  410. FStreamingType = ACctv->StreamingType;
  411. FViewAddress = ACctv->ViewAddress;
  412. FFullAddress = ACctv->FullAddress;
  413. FId = ACctv->CAMID;
  414. FLoc = ACctv->Name;
  415. FIpAddress = WideString(ACctv->WebCamIp);
  416. FPort = ACctv->WebCamPort;
  417. FConnId = WideString(ACctv->WebCamUser);
  418. FConnPass = WideString(ACctv->WebCamPswd);
  419. FDefModel = WideString(ACctv->WebCamCompany);
  420. FModelName = WideString(ACctv->WebCamModel);
  421. PnlCamera->Caption = "Disconnected";
  422. DisplayName(FName);
  423. FInstalled = true;
  424. if (ACctv->WebCamIp.IsEmpty())
  425. {
  426. XnsSdkWindow1->Visible = false;
  427. PnlCamera->Caption = "Streamming Address Error";
  428. }
  429. else
  430. {
  431. PopupMenu = PopupMenu1;
  432. }
  433. }
  434. }
  435. //---------------------------------------------------------------------------
  436. void __fastcall TFrmSamsungPlayer1::DisplayName(String AName)
  437. {
  438. PnlName->Caption = " " + FName;
  439. PnlName->Visible = true;
  440. PnlName->Refresh();
  441. }
  442. //---------------------------------------------------------------------------
  443. void __fastcall TFrmSamsungPlayer1::Connect()
  444. {
  445. Disconnect();
  446. #if 0
  447. if (FId.IsEmpty()) return;
  448. if (FIpAddress.IsEmpty()) return;
  449. #endif
  450. PnlCamera->Caption = "Connect...";
  451. XnsSdkWindow1->Visible = true;
  452. WebCamPlay();
  453. FPlay = true;
  454. }
  455. //---------------------------------------------------------------------------
  456. void __fastcall TFrmSamsungPlayer1::Disconnect()
  457. {
  458. if (!FPlay) return;
  459. try
  460. {
  461. DisconnectWebCamera();
  462. }
  463. catch(...)
  464. {
  465. }
  466. PnlCamera->Caption = "Disconnected";
  467. XnsSdkWindow1->Visible = false;
  468. FPlay = false;
  469. }
  470. //---------------------------------------------------------------------------
  471. void __fastcall TFrmSamsungPlayer1::CameraRelease()
  472. {
  473. Disconnect();
  474. PnlCamera->Caption = "";
  475. XnsSdkWindow1->Visible = false;
  476. }
  477. //---------------------------------------------------------------------------
  478. void __fastcall TFrmSamsungPlayer1::TmrPlayTimer(TObject *Sender)
  479. {
  480. TmrStop->Enabled = false;
  481. TmrPlay->Enabled = false;
  482. Connect();
  483. }
  484. //---------------------------------------------------------------------------
  485. void __fastcall TFrmSamsungPlayer1::TmrStopTimer(TObject *Sender)
  486. {
  487. TmrPlay->Enabled = false;
  488. TmrStop->Enabled = false;
  489. Disconnect();
  490. }
  491. //---------------------------------------------------------------------------