FrameSamsungXnsActiveXF.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "FrameSamsungXnsActiveXF.h"
  5. #include "XnsCommon.h"
  6. #include "XnsDeviceInterface.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma link "cxButtons"
  10. #pragma link "cxGraphics"
  11. #pragma link "cxLookAndFeelPainters"
  12. #pragma link "cxLookAndFeels"
  13. #pragma link "dxSkinBlack"
  14. #pragma link "dxSkinMcSkin"
  15. #pragma link "dxSkinsCore"
  16. #pragma link "ImageBtn"
  17. #pragma link "cxContainer"
  18. #pragma link "cxControls"
  19. #pragma link "cxEdit"
  20. #pragma link "cxLabel"
  21. #pragma link "cxTextEdit"
  22. #pragma link "XNSSDKDEVICELib_OCX"
  23. #pragma link "XNSSDKWINDOWLib_OCX"
  24. #pragma link "cxDropDownEdit"
  25. #pragma link "cxMaskEdit"
  26. #pragma resource "*.dfm"
  27. TFrameSamsungXnsActiveX *FrameSamsungXnsActiveX;
  28. //---------------------------------------------------------------------------
  29. __fastcall TFrameSamsungXnsActiveX::TFrameSamsungXnsActiveX(TComponent* Owner)
  30. : TFrame(Owner)
  31. {
  32. //::CoInitialize(NULL);
  33. //::CoUninitialize();
  34. FStrmPlay = false;
  35. reMsg->Lines->Clear();
  36. FStrmHandle = 0;
  37. PnlCamera->Color = clBlack;
  38. XnsSdkWindow1->Parent = PnlCamera;
  39. XnsSdkWindow1->Align = alClient;
  40. m_hDevice = 0; // Device handle
  41. m_hMediaSource = 0; // Media stream ID
  42. m_nControlId = 0; // Control ID
  43. m_bIsMediaPlay = false;
  44. SetupWebCamera();
  45. }
  46. //---------------------------------------------------------------------------
  47. bool __fastcall TFrameSamsungXnsActiveX::SetupWebCamera()
  48. {
  49. //// Samsung ip camera activex initialize start
  50. String sLog;
  51. long nRet = XnsSdkDevice1->Initialize();
  52. if (nRet != ERR_SUCCESS && nRet != ERR_ALREADY_INITIALIZE)
  53. {
  54. //sLog.printf(L"Samsung XnsSdkDevice Initialize() fail: errno=[%d]", nRet);
  55. sLog.printf(L"Samsung XnsSdkDevice Initialize() fail: return=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
  56. ShowMessage(sLog);
  57. return false;
  58. }
  59. nRet = XnsSdkWindow1->Initialize(NULL, NULL);
  60. if (nRet != ERR_SUCCESS && nRet != ERR_ALREADY_INITIALIZE)
  61. {
  62. //sLog.printf(L"Samsung XnsSdkDevice Initialize() fail: errno=[%d]", nRet);
  63. sLog.printf(L"Samsung XnsSdkWindow Initialize() fail: return=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
  64. ShowMessage(sLog);
  65. return false;
  66. }
  67. long device_id = XnsSdkDevice1->CreateDeviceEx();
  68. m_hDevice = XnsSdkDevice1->GetDeviceHandle(device_id);
  69. if (m_hDevice == 0)
  70. {
  71. sLog = "CreateDevice() fail";
  72. ShowMessage(sLog);
  73. return false;
  74. }
  75. //EdDeviceId->Text = String(device_id);
  76. #if 1
  77. WideString sFindModel = "samsung";
  78. long nFind = XnsSdkDevice1->FindModel(sFindModel.c_bstr());
  79. long nCount = XnsSdkDevice1->GetFindSize(nFind);
  80. String sModel;
  81. CboModel->Properties->Items->Clear();
  82. for (int ii = 0 ; ii < nCount ; ii++)
  83. {
  84. sModel = XnsSdkDevice1->GetFindString(nFind, ii);
  85. CboModel->Properties->Items->Add(sModel);
  86. }
  87. XnsSdkDevice1->CloseFind(nFind);
  88. CboModel->ItemIndex = 0;
  89. CboModel->Text = "SNP-3120VH";
  90. #endif
  91. return true;
  92. //// Samsung ip camera activex initialize end
  93. }
  94. //---------------------------------------------------------------------------
  95. void __fastcall TFrameSamsungXnsActiveX::ReleaseWebCamera()
  96. {
  97. if (m_hDevice != 0)
  98. {
  99. if (m_hMediaSource)
  100. {
  101. XnsSdkWindow1->Stop();
  102. XnsSdkDevice1->CloseMedia(m_hDevice, m_hMediaSource);
  103. }
  104. if (XnsSdkDevice1->GetDeviceStatus(m_hDevice) == XDEVICE_STATUS_CONNECTED)
  105. {
  106. XnsSdkDevice1->Disconnect(m_hDevice);
  107. }
  108. XnsSdkDevice1->ReleaseDevice(m_hDevice);
  109. }
  110. }
  111. //---------------------------------------------------------------------------
  112. void __fastcall TFrameSamsungXnsActiveX::BtnWebCamControlMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
  113. {
  114. TImageBtn *pBtn = (TImageBtn*)Sender;
  115. //PTZ Control
  116. // XPTZ_UP 1 Tilt up
  117. // XPTZ_DOWN 2 Tilt down
  118. // XPTZ_LEFT 3 Pan left
  119. // XPTZ_RIGHT 4 Pan right
  120. // XPTZ_UPLEFT 5 Tilt up and pan left
  121. // XPTZ_UPRIGHT 6 Tilt up and pan right
  122. // XPTZ_DOWNLEFT 7 Tilt down and pan left
  123. // XPTZ_DOWNRIGHT 8 Tilt down and pan right
  124. // XPTZ_ZOOMIN 9 Zoom in
  125. // XPTZ_ZOOMOUT 10 Zoom out
  126. // XPTZ_STOP 11 Stop the PTZ moving
  127. // XPTZ_FOCUS_NEAR 12 Focus near
  128. // XPTZ_FOCUS_FAR 13 Focus far
  129. // XPTZ_FOCUS_STOP 14 Stop focus moving
  130. // XPTZ_IRIS_OPEN 15 Open iris
  131. // XPTZ_IRIS_CLOSE 16 Close iris
  132. try
  133. {
  134. TImageBtn *pBtn = (TImageBtn*)Sender;
  135. // PTZ_FOCUS_NEAR 12 Focus near
  136. // PTZ_FOCUS_FAR 13 Focus far
  137. // PTZ_FOCUS_STOP 14 Stop focus moving
  138. if (pBtn->Tag == 0)
  139. {
  140. }
  141. else
  142. {
  143. ControlCamera((int)pBtn->Tag);
  144. }
  145. }
  146. catch(...) {}
  147. }
  148. //---------------------------------------------------------------------------
  149. void __fastcall TFrameSamsungXnsActiveX::BtnWebCamControlMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
  150. {
  151. TImageBtn *pBtn = (TImageBtn*)Sender;
  152. //PTZ Control Stop
  153. try
  154. {
  155. TImageBtn *pBtn = (TImageBtn*)Sender;
  156. // PTZ_FOCUS_NEAR 12 Focus near
  157. // PTZ_FOCUS_FAR 13 Focus far
  158. // PTZ_FOCUS_STOP 14 Stop focus moving
  159. if (pBtn->Tag == PTZ_FOCUS_NEAR || pBtn->Tag == PTZ_FOCUS_FAR)
  160. {
  161. ControlCamera(PTZ_FOCUS_STOP);
  162. }
  163. else if (pBtn->Tag == 0)
  164. {
  165. }
  166. else
  167. {
  168. ControlCamera(PTZ_STOP);
  169. }
  170. }
  171. catch(...) {}
  172. }
  173. //---------------------------------------------------------------------------
  174. void __fastcall TFrameSamsungXnsActiveX::ControlCamera(int nCommand)
  175. {
  176. long ret;
  177. if (m_hDevice == 0 || m_nControlId == 0) return;
  178. int nPtzSpeed = PTZSpeed->Position;
  179. //WLOGD(_T("MoveCamera, command=[%d], speed=%d\n"), nCommand, m_nPtzSpeed);
  180. // if( nCommand != XPTZ_STOP)
  181. // m_ctrlXnsDevice.ControlPtz(m_hDevice, m_nControlId, XPTZ_STOP, 30);
  182. // Returns the capabilities of the control module.
  183. long bIsCap = XnsSdkDevice1->GetControlCapability(m_hDevice, m_nControlId, XCTL_CAP_PTZ_SPEED);
  184. // [ XNS ACTIVEX HELP ]
  185. // -----------------------------------------------------------------------
  186. // Moves the camera to the predefined PTZ position. This function is
  187. // valid as long as the application is receiving media stream from the
  188. // camera.
  189. // -----------------------------------------------------------------------
  190. // Command Value Description
  191. // -----------------------------------------------------------------------
  192. // XPTZ_UP 1 Tilt up
  193. // XPTZ_DOWN 2 Tilt down
  194. // XPTZ_LEFT 3 Pan left
  195. // XPTZ_RIGHT 4 Pan right
  196. // XPTZ_UPLEFT 5 Tilt up and pan left
  197. // XPTZ_UPRIGHT 6 Tilt up and pan right
  198. // XPTZ_DOWNLEFT 7 Tilt down and pan left
  199. // XPTZ_DOWNRIGHT 8 Tilt down and pan right
  200. // XPTZ_ZOOMIN 9 Zoom in
  201. // XPTZ_ZOOMOUT 10 Zoom out
  202. // XPTZ_STOP 11 Stop the PTZ moving
  203. // XPTZ_FOCUS_NEAR 12 Focus near
  204. // XPTZ_FOCUS_FAR 13 Focus far
  205. // XPTZ_FOCUS_STOP 14 Stop focus moving
  206. // XPTZ_IRIS_OPEN 15 Open iris
  207. // XPTZ_IRIS_CLOSE 16 Close iris
  208. // -----------------------------------------------------------------------
  209. if (bIsCap)
  210. {
  211. ret = XnsSdkDevice1->ControlPtz(
  212. m_hDevice, // [in] Device handle. This value is returned from CreateDevice().
  213. m_nControlId, // [in] Control ID.
  214. nCommand, // [in] PTZ command
  215. nPtzSpeed // [in] If nPtzCommand is a speed-related command,
  216. // this value indicates the PTZ operation speed.
  217. // (1~100) This value is valid only if the camera
  218. // supports the XCTL_CAP_PTZ_SPEED capability.
  219. );
  220. }
  221. else
  222. {
  223. ret = XnsSdkDevice1->ControlPtz(m_hDevice, m_nControlId, nCommand, 30);
  224. }
  225. if (ret != ERR_SUCCESS)
  226. {
  227. String sErr;
  228. sErr.printf(L"ControlPtz() fail:: error=[%d](%s)", ret, XnsSdkDevice1->GetErrorString(ret));
  229. ShowMessage(sErr);
  230. }
  231. }
  232. //---------------------------------------------------------------------------
  233. void __fastcall TFrameSamsungXnsActiveX::OpenStream()
  234. {
  235. }
  236. //---------------------------------------------------------------------------
  237. void __fastcall TFrameSamsungXnsActiveX::CloseStream()
  238. {
  239. DisconnectWebCamera();
  240. ReleaseWebCamera();
  241. }
  242. //---------------------------------------------------------------------------
  243. void __fastcall TFrameSamsungXnsActiveX::BtnConnectClick(TObject *Sender)
  244. {
  245. ConnectWebCamera();
  246. }
  247. //---------------------------------------------------------------------------
  248. bool __fastcall TFrameSamsungXnsActiveX::ConnectWebCamera()
  249. {
  250. bool bConnect = true;
  251. WideString sDefModel = "Samsung";
  252. WideString sModelName = CboModel->Text.Trim();
  253. WideString sIpAddress = EdIpAddress->Text.Trim();
  254. int nPort = StrToInt(EdPort->Text.Trim());
  255. WideString sID = EdStreamId->Text.Trim();
  256. WideString sPass = EdStreamPswd->Text.Trim();
  257. nPort = 4520;
  258. XnsSdkDevice1->SetConnectionInfo(
  259. m_hDevice, // [in] Device handle
  260. sDefModel.Copy(), // [in] Fixed as 'Samsung'
  261. sModelName.Copy(), // [in] Name of model to connect to. The maximum length allowed is 126-byte.
  262. XADDRESS_IP, sIpAddress.Copy(), // [in] Address type, actual address according to addresstype.
  263. nPort, 0, // [in] Port number, port number for web access
  264. sID.Copy(), sPass.Copy() // [in] Login ID, password
  265. );
  266. long nRet = XnsSdkDevice1->ConnectNonBlock(
  267. m_hDevice, // [in] Device handle
  268. true, // [in] Flag to decide where to forcibly log in or not.
  269. false // [in] If this value is 1, try to connect again.
  270. );
  271. if (nRet != ERR_SUCCESS)
  272. {
  273. String sLog;
  274. sLog.printf(L"ConnectNonBlock() fail: errno=[%d](%s)\n", nRet, XnsSdkDevice1->GetErrorString(nRet));
  275. ShowMessage(sLog);
  276. bConnect = false;
  277. }
  278. return bConnect;
  279. }
  280. //---------------------------------------------------------------------------
  281. bool __fastcall TFrameSamsungXnsActiveX::OpenWebCamera()
  282. {
  283. if (m_hMediaSource)
  284. {
  285. reMsg->Lines->Add("Media stream was already opend\n");
  286. return true;
  287. }
  288. int xret = XnsSdkDevice1->GetControlType(m_hDevice, 1);
  289. //WLOGD(_T("Open media stream:%d\n"), xret);
  290. // [ XNS ACTIVEX HELP ]
  291. // -----------------------------------------------------------------------
  292. // Returns the type of control module corresponding to the control ID.
  293. //
  294. // < DVR control module structure >
  295. // +------------------------------------------------------------
  296. // Control Module(Device, 1) ----------------- Control ID : 1
  297. // |---- Control Module(Camera, 1) ----------- Control ID : 2
  298. // |---- Control Module(Camera, 2) ----------- Control ID : 3
  299. // |---- Control Module(Camera, 3) ----------- Control ID : 4
  300. // |---- ....
  301. // |---- Control Module(Camera,16) ----------- Control ID : 17
  302. // |---- Control Module(Sensor-in, 1) -------- Control ID : 18
  303. // |---- ...
  304. // |---- Control Module(Sensor-in, 16) ------- Control ID : 34
  305. // |---- Control Module(Digital-out, 1) ------ Control ID : 35
  306. // |---- ...
  307. // |---- Control Module(Digital-out, 4) ------ Control ID : 38
  308. // +------------------------------------------------------------
  309. //
  310. // < Network Camera control module structure >
  311. // +------------------------------------------------------------
  312. // Control Module(Device, 1) ----------------- Control ID : 1
  313. // |---- Control Module(Camera, 1) ----------- Control ID : 2
  314. // |--- Control Module(Video, 1) ------- Control ID : 3
  315. // |--- Control Module(Video, 2) ------- Control ID : 4
  316. // |--- ...
  317. // |--- Control Module(Video, 10) ------ Control ID : 12
  318. // |--- Control Module(Sensor-in, 1) --- Control ID : 13
  319. // |--- Control Module(Digital-out, 1) - Control ID : 14
  320. // +------------------------------------------------------------
  321. // -----------------------------------------------------------------------
  322. if (XnsSdkDevice1->GetControlType(m_hDevice, 1) & XCTL_DVR)
  323. {
  324. // Returns the number of control modules. The application can get
  325. // the number of a specific type of control modules, and can get
  326. // also the whole number of video recorders or cameras.
  327. // DVR control id is start with 2
  328. int nCount = XnsSdkDevice1->GetControlCount(m_hDevice, XCTL_CAMERA);
  329. for(int i=0; i<nCount ; i++)
  330. {
  331. // Returns the capabilities of the control module.
  332. if (XnsSdkDevice1->GetControlCapability(m_hDevice, i+2, XCTL_CAP_LIVE) &&
  333. // Gets the status of the control module.
  334. // DVR control id is start with 2
  335. XnsSdkDevice1->GetControlStatus(m_hDevice, i+2, 1))
  336. {
  337. // When called, it will start getting media streams from the
  338. // device. The receiving media streams will, then, be
  339. // forwarded to the XnsSdkWindow component that will play
  340. // the streams after decoding. phMediaSource is needed to
  341. // link the stream data with XnsSdkWindow.
  342. // The value can be obtained from a parameter (out-parameter)
  343. // of OpenMedia(). When XnsSdkWindow receives this value,
  344. // it can get stream data from the device.
  345. // phMediaSource is also used for controlling playback of
  346. // multimedia files. As a result, the application should keep this value at all times.
  347. XnsSdkDevice1->OpenMedia(
  348. m_hDevice, // [in] Device handle. This value is returned from CreateDevice().
  349. i+2, // [in] Control ID of video control module.
  350. MEDIA_TYPE_LIVE, // [in] Media type.
  351. 0, // [in] Play start time. Format: 4-byte time_t.
  352. 0, // [in] Play end time. Format: 4-byte time_t.
  353. &m_hMediaSource // [out] Media stream ID. This is needed for controlling
  354. // the media stream and also used as a parameter
  355. // of XnsSdkWindow::Start().
  356. );
  357. // Adds the media source handle to XnsSdkWindow.
  358. // The media source handle is created by XnsSdkDevice.
  359. // If the application calls XnsSdkDevice::OpenMedia(),
  360. // it will receive media stream from the device and return the MediaSource
  361. // handle. The application uses uses Start() to forward the mediasource
  362. // handle to XnsSdkWindow so that XnsSdkWindow can obtain stream data.
  363. XnsSdkWindow1->Start(m_hMediaSource);
  364. m_nControlId = i+2;
  365. m_bIsMediaPlay = true;
  366. //WLOGD(_T("DVR TYPE, control id = %d\n"), m_nControlId);
  367. return true;
  368. }
  369. }
  370. }
  371. else if (XnsSdkDevice1->GetControlType(m_hDevice, 1) & (XCTL_NETCAM |XCTL_ENCODER))
  372. {
  373. int nCount = XnsSdkDevice1->GetControlCount(m_hDevice, XCTL_VIDEO);
  374. if (nCount == 0)
  375. {
  376. nCount = XnsSdkDevice1->GetControlCount(m_hDevice, XCTL_CAMERA);
  377. }
  378. for(int i=0; i<nCount ; i++)
  379. {
  380. // Network camera and encoder control id is start with 3
  381. if (XnsSdkDevice1->GetControlCapability(m_hDevice, i+3, XCTL_CAP_LIVE) &&
  382. XnsSdkDevice1->GetControlStatus(m_hDevice, i+3, 1))
  383. {
  384. XnsSdkDevice1->OpenMedia(m_hDevice, i+3, MEDIA_TYPE_LIVE, 0, 0, &m_hMediaSource);
  385. XnsSdkWindow1->Start(m_hMediaSource);
  386. m_nControlId = i+3;
  387. m_bIsMediaPlay = true;
  388. //WLOGD(_T("NETCAM or ENCODER TYPE, control id = %d\n"), m_nControlId);
  389. return true;
  390. }
  391. }
  392. }
  393. //WLOGD(_T("No valid device.."));
  394. return false;
  395. }
  396. //---------------------------------------------------------------------------
  397. void __fastcall TFrameSamsungXnsActiveX::BtnDisconnectClick(TObject *Sender)
  398. {
  399. DisconnectWebCamera();
  400. BtnConnect->Enabled = true;
  401. BtnDisconnect->Enabled = false;
  402. }
  403. //---------------------------------------------------------------------------
  404. void __fastcall TFrameSamsungXnsActiveX::DisconnectWebCamera()
  405. {
  406. if (m_hDevice == 0) return;
  407. CloseWebCamera();
  408. // Disconnects from the device.
  409. XnsSdkDevice1->Disconnect(m_hDevice);
  410. m_bIsMediaPlay = false;
  411. m_hMediaSource = 0;
  412. }
  413. //---------------------------------------------------------------------------
  414. void __fastcall TFrameSamsungXnsActiveX::CloseWebCamera()
  415. {
  416. if (!m_hMediaSource)
  417. {
  418. return;
  419. }
  420. if (m_bIsMediaPlay)
  421. {
  422. XnsSdkWindow1->Stop();
  423. m_bIsMediaPlay = false;
  424. }
  425. XnsSdkDevice1->CloseMedia(m_hDevice, m_hMediaSource);
  426. m_hMediaSource = 0;
  427. m_nControlId = 0;
  428. }
  429. //---------------------------------------------------------------------------
  430. void __fastcall TFrameSamsungXnsActiveX::XnsSdkDevice1ConnectFailed(TObject *Sender, long nDeviceID, long nControlID)
  431. {
  432. String sDeviceID = String(nDeviceID);
  433. String sControlID = String(nControlID);
  434. String sLog = "Connect Failed: " + sDeviceID + " " + sControlID;
  435. reMsg->Lines->Add(sLog);
  436. BtnConnect->Enabled = true;
  437. BtnDisconnect->Enabled = false;
  438. }
  439. //---------------------------------------------------------------------------
  440. void __fastcall TFrameSamsungXnsActiveX::XnsSdkDevice1DeviceStatusChanged(TObject *Sender, long nDeviceID, long nErrorCode,
  441. long nDeviceStatus, long nHddCondition)
  442. {
  443. String sDeviceID = String(nDeviceID);
  444. String sDeviceStatus = String(nDeviceStatus);
  445. String sErrorCode = String(nErrorCode);
  446. String sErrorDesc;
  447. sErrorDesc.printf(L"%s", XnsSdkDevice1->GetErrorString(nErrorCode));
  448. reMsg->Lines->Add("Event: " + sDeviceID + " " + sDeviceStatus + " " + sErrorDesc);
  449. #if 0
  450. if (nErrorCode != ERR_SUCCESS || nDeviceStatus != 1)
  451. {
  452. return;
  453. }
  454. #endif
  455. if (nErrorCode == ERR_SUCCESS && nDeviceStatus == 1)
  456. {
  457. reMsg->Lines->Add("Connected...");
  458. bool bIsPtzSupport = XnsSdkDevice1->GetControlCapability(m_hDevice, 2, XCTL_CAP_PTZ_PAN);
  459. OpenWebCamera();
  460. BtnConnect->Enabled = false;
  461. BtnDisconnect->Enabled = true;
  462. }
  463. else if (nErrorCode == ERR_SUCCESS && nDeviceStatus == 0)
  464. {
  465. reMsg->Lines->Add("DisConnected...");
  466. }
  467. }
  468. //---------------------------------------------------------------------------
  469. void __fastcall TFrameSamsungXnsActiveX::reMsgDblClick(TObject *Sender)
  470. {
  471. reMsg->Lines->Clear();
  472. }
  473. //---------------------------------------------------------------------------