FrmCameraPlayerF.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include "AppGlobalF.h"
  4. #include "FrmCameraPlayerF.h"
  5. #include "FrmCameraFullScreenF.h"
  6. #include "FrmCameraInfoF.h"
  7. #include "FrmCameraScreenF.h"
  8. #pragma hdrstop
  9. //---------------------------------------------------------------------------
  10. #pragma package(smart_init)
  11. #pragma link "dxGDIPlusClasses"
  12. #pragma link "PasLibVlcPlayerUnit"
  13. #pragma resource "*.dfm"
  14. TFrmCameraPlayer *FrmCameraPlayer = NULL;
  15. //---------------------------------------------------------------------------
  16. __fastcall TFrmCameraPlayer::TFrmCameraPlayer(TComponent* Owner)
  17. : TForm(Owner)
  18. {
  19. IsTerm = false;
  20. FPlay = false;
  21. FUserStop = false;
  22. FConHandle = 0;
  23. FFullScreen = false;
  24. FParent = this->Parent;
  25. PnlCamera->Tag = (int)this;
  26. FTimeChangeTick = 0;
  27. TmrReset->Enabled = false;
  28. TmrTick->Enabled = false;
  29. SetResetTimer();
  30. FIsResetVideo = false;
  31. //mmLog->Lines->Clear();
  32. }
  33. //---------------------------------------------------------------------------
  34. void __fastcall TFrmCameraPlayer::SetResetTimer()
  35. {
  36. if (g_AppCfg.cmraViewerResetMin > 0) {
  37. TmrReset->Interval = g_AppCfg.cmraViewerResetMin * 60 * 1000;
  38. }
  39. }
  40. //---------------------------------------------------------------------------
  41. void __fastcall TFrmCameraPlayer::TmrShowTimer(TObject *Sender)
  42. {
  43. TmrShow->Enabled = false;
  44. if (IsTerm) {
  45. return;
  46. }
  47. if (Installed)
  48. {
  49. PopupMenu = PopupMenu1;
  50. ImgNoData->Visible = false;
  51. //PnlName->Caption = CTLR_NMBR + ": " + NAME;
  52. PnlName->Caption = " " + NAME;
  53. Play();
  54. }
  55. else
  56. {
  57. PasLibVlcPlayer1->Visible = false;
  58. PopupMenu = NULL;
  59. PnlCamera->Caption = " " + NAME;
  60. LoadEmptyImage();
  61. }
  62. }
  63. //---------------------------------------------------------------------------
  64. void __fastcall TFrmCameraPlayer::LoadEmptyImage()
  65. {
  66. try
  67. {
  68. #if 0
  69. String sImageFile = g_AppCfg.sEmptyImg;
  70. if (sImageFile != "")
  71. {
  72. ImgNoData->Picture->LoadFromFile(sImageFile);
  73. ImgNoData->Align = alClient;
  74. }
  75. #endif
  76. ImgNoData->Visible = true;
  77. }
  78. catch(Exception &e)
  79. {
  80. ImgNoData->Visible = true;
  81. }
  82. }
  83. //---------------------------------------------------------------------------
  84. void __fastcall TFrmCameraPlayer::FormShow(TObject *Sender)
  85. {
  86. Refresh();
  87. this->FLastBounds = this->BoundsRect;
  88. // TmrShow->Enabled = true;
  89. }
  90. //---------------------------------------------------------------------------
  91. void __fastcall TFrmCameraPlayer::FormClose(TObject *Sender, TCloseAction &Action)
  92. {
  93. try {
  94. FUserStop = false;
  95. TmrTick->Enabled = false;
  96. Stop();
  97. } catch(...) {}
  98. Action = caFree;
  99. }
  100. //---------------------------------------------------------------------------
  101. void __fastcall TFrmCameraPlayer::FormDestroy(TObject *Sender)
  102. {
  103. try {
  104. FUserStop = false;
  105. TmrTick->Enabled = false;
  106. Stop();
  107. } catch(...) {}
  108. }
  109. //---------------------------------------------------------------------------
  110. void __fastcall TFrmCameraPlayer::MnuConnectClick(TObject *Sender)
  111. {
  112. FPlay = false;
  113. FUserStop = false;
  114. Application->ProcessMessages();
  115. Play();
  116. }
  117. //---------------------------------------------------------------------------
  118. void __fastcall TFrmCameraPlayer::MnuDisconnectClick(TObject *Sender)
  119. {
  120. PasLibVlcPlayer1->Visible = false;
  121. PasLibVlcPlayer1->Refresh();
  122. PnlCamera->Refresh();
  123. FUserStop = true;
  124. Application->ProcessMessages();
  125. Stop();
  126. }
  127. //---------------------------------------------------------------------------
  128. void __fastcall TFrmCameraPlayer::AutoPlay()
  129. {
  130. TmrShow->Enabled = true;
  131. }
  132. //---------------------------------------------------------------------------
  133. void __fastcall TFrmCameraPlayer::CameraLog(String ALogMsg)
  134. {
  135. PnlCamera->Caption = ALogMsg;
  136. PnlCamera->Refresh();
  137. Application->ProcessMessages();
  138. }
  139. //---------------------------------------------------------------------------
  140. void __fastcall TFrmCameraPlayer::CameraInfo(String ALogInfo)
  141. {
  142. //lblRate->Caption = ALogInfo;
  143. //lblRate->Refresh();
  144. Application->ProcessMessages();
  145. }
  146. //---------------------------------------------------------------------------
  147. void __fastcall TFrmCameraPlayer::Play()
  148. {
  149. if (FPlay) return;
  150. if (PasLibVlcPlayer1->Visible) {
  151. PasLibVlcPlayer1->Visible = false;
  152. }
  153. Stop();
  154. if (STRM_ADDR == "") {
  155. CameraLog("### 영상정보가 없습니다 ###");
  156. return;
  157. }
  158. CameraLog("--- 영상 연결 중 ---");
  159. try
  160. {
  161. ///--udp-caching=500 --tcp-caching=500 --realrtsp-caching=500
  162. //ffplay.exe rtsp://224.1.1.1:8086 -fflags nobuffer -flags low_delay -avioflags direct -fflags discardcorrupt
  163. //PasLibVlcPlayer1->VLC->Path = "plugins";
  164. //PasLibVlcPlayer1->VLC->AddOption("http-caching=300");
  165. PasLibVlcPlayer1->VLC->AddOption("live-capture-caching=300");
  166. PasLibVlcPlayer1->VLC->AddOption("file-caching=300");
  167. PasLibVlcPlayer1->VLC->AddOption("disk-caching=300");
  168. PasLibVlcPlayer1->VLC->AddOption("network-caching=300");
  169. //PasLibVlcPlayer1->VLC->AddOption("avcodec-hw=none");
  170. //PasLibVlcPlayer1->VLC->AddOption("avcodec-hw={any,d3d11va,dxva2,none}");
  171. //PasLibVlcPlayer1->VLC->AddOption("avcodec-threads=0");
  172. //PasLibVlcPlayer1->VLC->AddOption("no-sout-all");
  173. //PasLibVlcPlayer1->VLC->AddOption("sout-keep");
  174. //PasLibVlcPlayer1->VLC->AddOption(":file-caching=300");
  175. //PasLibVlcPlayer1->VLC->AddOption(":network-caching=300");
  176. //PasLibVlcPlayer1->VLC->AddOption(":avcodec-hw=none");
  177. //PasLibVlcPlayer1->VLC->AddOption("--avcodec-threads=0");
  178. //PasLibVlcPlayer1->VLC->AddOption(":no-sout-all");
  179. //PasLibVlcPlayer1->VLC->AddOption(":sout-keep");
  180. PasLibVlcPlayer1->Play(STRM_ADDR);
  181. FConHandle = 1;
  182. Application->ProcessMessages();
  183. AdjustVideoFit();
  184. if (FConHandle > 0)
  185. {
  186. if (ImgNoData->Visible == true) {
  187. ImgNoData->Visible = false;
  188. }
  189. }
  190. //PnlName->Caption = libvlc_get_install_path();
  191. Application->ProcessMessages();
  192. }
  193. catch(Exception &e)
  194. {
  195. }
  196. FPlay = true;
  197. }
  198. //---------------------------------------------------------------------------
  199. void __fastcall TFrmCameraPlayer::Stop()
  200. {
  201. TmrReset->Enabled = false;
  202. TmrTick->Enabled = false;
  203. if (!FPlay) return;
  204. FPlay = false;
  205. if (STRM_ADDR == "") {
  206. return;
  207. }
  208. CameraLog("--- 영상 연결 해제 ---");
  209. try
  210. {
  211. if (FConHandle > 0)
  212. {
  213. PasLibVlcPlayer1->Stop();
  214. }
  215. Application->ProcessMessages();
  216. FConHandle = 0;
  217. }
  218. catch(Exception &e)
  219. {
  220. }
  221. CameraLog("*** 영상 연결 종료 ***");
  222. FConHandle = 0;
  223. FPlay = false;
  224. }
  225. //---------------------------------------------------------------------------
  226. void __fastcall TFrmCameraPlayer::AdjustVideoFit()
  227. {
  228. try {
  229. if (FConHandle > 0 && PasLibVlcPlayer1->IsPlay()) {
  230. String ratioData = IntToStr(PasLibVlcPlayer1->Width) + ":" + IntToStr(PasLibVlcPlayer1->Height);
  231. PasLibVlcPlayer1->SetVideoAspectRatio(ratioData);
  232. }
  233. }
  234. catch(Exception &e) {
  235. }
  236. }
  237. //---------------------------------------------------------------------------
  238. void __fastcall TFrmCameraPlayer::Term()
  239. {
  240. IsTerm = true;
  241. Stop();
  242. }
  243. //---------------------------------------------------------------------------
  244. void __fastcall TFrmCameraPlayer::InitCamera(bool AInstalled, String ACtlrNmbr, String AName, String AStrmAddr, String AFullStrmAddr, int AViewMode)
  245. {
  246. Installed = AInstalled;
  247. CTLR_NMBR = ACtlrNmbr;
  248. NAME = AName;
  249. STRM_ADDR = AStrmAddr;
  250. FULL_STRM_ADDR = AFullStrmAddr;
  251. VIEW_MODE = AViewMode;
  252. Caption = NAME;
  253. PnlName->Caption = NAME;
  254. PnlCamera->Caption = AStrmAddr;
  255. FUserStop = false;
  256. }
  257. //---------------------------------------------------------------------------
  258. void __fastcall TFrmCameraPlayer::PasLibVlcPlayer1DblClick(TObject *Sender)
  259. {
  260. FrmCameraScreen->TmrFullScreen->Enabled = false;
  261. FrmCameraScreen->FFullScreenIdx = Tag;
  262. FrmCameraScreen->TmrFullScreen->Enabled = true;
  263. }
  264. //---------------------------------------------------------------------------
  265. void __fastcall TFrmCameraPlayer::MnuInfoClick(TObject *Sender)
  266. {
  267. FrmCameraInfo = new TFrmCameraInfo(this);
  268. FrmCameraInfo->Edit1->Text = CTLR_NMBR;
  269. FrmCameraInfo->Edit2->Text = NAME;
  270. FrmCameraInfo->Edit3->Text = STRM_ADDR;
  271. FrmCameraInfo->Edit4->Text = FULL_STRM_ADDR;
  272. FrmCameraInfo->ShowModal();
  273. delete FrmCameraInfo;
  274. FrmCameraInfo = NULL;
  275. }
  276. //---------------------------------------------------------------------------
  277. void __fastcall TFrmCameraPlayer::PasLibVlcPlayer1MediaPlayerOpening(TObject *Sender)
  278. {
  279. // 1. Opening
  280. if (PasLibVlcPlayer1->Visible == false) {
  281. PasLibVlcPlayer1->Visible = true;
  282. PnlCamera->Refresh();
  283. AdjustVideoFit();
  284. }
  285. if (ImgNoData->Visible == true) {
  286. ImgNoData->Visible = false;
  287. }
  288. FTimeChangeTick = 0;
  289. //CameraInfo("PlayerOpening");
  290. // 영상이 연결됨
  291. //AdjustVideoFit();
  292. }
  293. //---------------------------------------------------------------------------
  294. void __fastcall TFrmCameraPlayer::PasLibVlcPlayer1MediaPlayerPlaying(TObject *Sender)
  295. {
  296. // 2. Playing
  297. //CameraInfo("PlayerPlaying");
  298. FTimeChangeTick = 0;
  299. FUserStop = false;
  300. TmrTick->Enabled = false;
  301. AdjustVideoFit();
  302. TmrReset->Enabled = false;
  303. FIsResetVideo = false;
  304. if (g_AppCfg.cmraViewerDelaySec > 0) {
  305. TmrTick->Enabled = true;
  306. }
  307. if (g_AppCfg.cmraViewerResetMin > 0) {
  308. SetResetTimer();
  309. TmrReset->Enabled = true;
  310. }
  311. }
  312. //---------------------------------------------------------------------------
  313. void __fastcall TFrmCameraPlayer::PasLibVlcPlayer1MediaPlayerTimeChanged(TObject *Sender, __int64 time)
  314. {
  315. // 3. TimeChanged
  316. FTimeChangeTick = GetTickCount();
  317. }
  318. //---------------------------------------------------------------------------
  319. void __fastcall TFrmCameraPlayer::PasLibVlcPlayer1MediaPlayerMediaChanged(TObject *Sender, UnicodeString mrl)
  320. {
  321. // 스트리밍 변경됨
  322. CameraLog("@@@ 영상 연결 중.. @@@");
  323. //AdjustVideoFit();
  324. }
  325. //---------------------------------------------------------------------------
  326. void __fastcall TFrmCameraPlayer::PasLibVlcPlayer1MediaPlayerEncounteredError(TObject *Sender)
  327. {
  328. // 에러가 발생하고 PlayerStopped 이벤트가 발생됨
  329. if (PasLibVlcPlayer1->Visible) {
  330. PasLibVlcPlayer1->Visible = false;
  331. }
  332. CameraLog("@@@ 영상 표출 오류 @@@");
  333. }
  334. //---------------------------------------------------------------------------
  335. void __fastcall TFrmCameraPlayer::PasLibVlcPlayer1MediaPlayerStopped(TObject *Sender)
  336. {
  337. if (FIsResetVideo == false) {
  338. CameraLog("@@@ 영상 표출이 종료됨 @@@");
  339. }
  340. if (FUserStop) {
  341. if (PasLibVlcPlayer1->Visible) {
  342. PasLibVlcPlayer1->Visible = false;
  343. }
  344. }
  345. FPlay = false;
  346. if (FIsResetVideo == false && FUserStop == false) {
  347. TmrShow->Enabled = false;
  348. TmrShow->Interval = 1000;
  349. TmrShow->Enabled = true;
  350. }
  351. }
  352. //---------------------------------------------------------------------------
  353. void __fastcall TFrmCameraPlayer::TmrResetTimer(TObject *Sender)
  354. {
  355. TmrReset->Enabled = false;
  356. //CameraInfo("TmrReset");
  357. if (FUserStop == false) {
  358. ResetVideoCamera();
  359. }
  360. }
  361. //---------------------------------------------------------------------------
  362. void __fastcall TFrmCameraPlayer::ResetVideoCamera()
  363. {
  364. TmrReset->Enabled = false;
  365. FIsResetVideo = true;
  366. try
  367. {
  368. PasLibVlcPlayer1->Stop();
  369. Application->ProcessMessages();
  370. }
  371. catch(Exception &e) {
  372. }
  373. Application->ProcessMessages();
  374. Application->ProcessMessages();
  375. Application->ProcessMessages();
  376. Application->ProcessMessages();
  377. Application->ProcessMessages();
  378. Application->ProcessMessages();
  379. FTimeChangeTick = 0;
  380. try
  381. {
  382. PasLibVlcPlayer1->Play(STRM_ADDR);
  383. FConHandle = 1;
  384. }
  385. catch(Exception &e) {
  386. }
  387. Application->ProcessMessages();
  388. Application->ProcessMessages();
  389. }
  390. //---------------------------------------------------------------------------
  391. void __fastcall TFrmCameraPlayer::TmrTickTimer(TObject *Sender)
  392. {
  393. TmrTick->Enabled = false;
  394. if (FUserStop == false && g_AppCfg.cmraViewerDelaySec > 0 && FTimeChangeTick > 0) {
  395. //CameraInfo("TmrTickTimer: " + String(GetTickCount() - FTimeChangeTick));
  396. if ((GetTickCount() - FTimeChangeTick) > (g_AppCfg.cmraViewerDelaySec * 1000)) {
  397. ResetVideoCamera();
  398. return;
  399. }
  400. }
  401. if (FUserStop == false && g_AppCfg.cmraViewerDelaySec > 0 && FTimeChangeTick > 0) {
  402. TmrTick->Enabled = true;
  403. }
  404. }
  405. //---------------------------------------------------------------------------
  406. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  407. //void __fastcall TFrmCameraPlayer::PasLibVlcPlayer1MediaPlayerPositionChanged(TObject *Sender, float position)
  408. #if 0
  409. if (PasLibVlcPlayer1->Visible == false) {
  410. PasLibVlcPlayer1->Visible = true;
  411. PnlCamera->Refresh();
  412. AdjustVideoFit();
  413. }
  414. if (ImgNoData->Visible == true) {
  415. ImgNoData->Visible = false;
  416. }
  417. CameraInfo("Changed: " + String(position) + ", " + String(PasLibVlcPlayer1->GetState()));
  418. #endif
  419. #if 0
  420. case PasLibVlcPlayer1.GetState() of
  421. plvPlayer_NothingSpecial: stateName := 'Idle';
  422. plvPlayer_Opening: stateName := 'Opening';
  423. plvPlayer_Buffering: stateName := 'Buffering';
  424. plvPlayer_Playing: stateName := 'Playing';
  425. plvPlayer_Paused: stateName := 'Paused';
  426. plvPlayer_Stopped: stateName := 'Stopped';
  427. plvPlayer_Ended: stateName := 'Ended';
  428. plvPlayer_Error: stateName := 'Error';
  429. else stateName := 'Unknown';
  430. end;
  431. var
  432. info : string;
  433. sar_num, sar_den : LongWord;
  434. begin
  435. info := 'Aspect ratio = ' + PasLibVlcPlayer1.GetVideoAspectRatio();
  436. if (PasLibVlcPlayer1.GetVideoSampleAspectRatio(sar_num, sar_den)) then
  437. begin
  438. info := info + ', SampleAspectRatio = ' + IntToStr(sar_num) + ':' + IntToStr(sar_den);
  439. end;
  440. MessageDlg(
  441. info,
  442. mtInformation, [mbOK], 0);
  443. end;
  444. main libvlc debug: configured with /builds/videolan/vlc/extras/package/win32/../../../configure '--enable-update-check' '--enable-lua' '--enable-faad' '--enable-flac' '--enable-theora' '--enable-avcodec' '--enable-merge-ffmpeg' '--enable-dca' '--enable-mpc' '--enable-libass' '--enable-schroedinger' '--enable-realrtsp' '--enable-live555' '--enable-shout' '--enable-goom' '--enable-sse' '--enable-mmx' '--enable-libcddb' '--enable-zvbi' '--disable-telx' '--enable-nls' '--host=i686-w64-mingw32' '--with-contrib=../contrib/i686-w64-mingw32' '--with-breakpad=https://win.crashes.videolan.org' '--enable-qt' '--enable-skins2' '--enable-dvdread' '--enable-caca' 'host_alias=i686-w64-mingw32' 'CFLAGS= -D_WIN32_WINNT=0x0502 -DWINVER=0x502 -D__MSVCRT_VERSION__=0x700 ' 'CXXFLAGS= -D_WIN32_WINNT=0x0502 -DWINVER=0x502 -D__MSVCRT_VERSION__=0x700 ' 'PKG_CONFIG=pkg-config' 'PKG_CONFIG_LIBDIR=/usr/i686-w64-mingw32/lib/pkgconfig:/usr/lib/i686-w64-mingw32/pkgconfig'
  445. #endif