From ee2b0cd0c886da3657fabcf64f5b1894e3e8d433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=B9=BF?= Date: Fri, 7 Apr 2023 11:31:09 +0800 Subject: [PATCH] add exit handler to clean resoure --- CMakeLists.txt.user | 404 +++++++++++++++++++++++++++++++++------- hkstreamer.creator.user | 12 +- hkstreamer.files | 2 + hkstreamer.includes | 1 + include/HkStreamer.h | 1 + src/HkStreamer.cpp | 96 ++++++++-- 6 files changed, 431 insertions(+), 85 deletions(-) diff --git a/CMakeLists.txt.user b/CMakeLists.txt.user index d6ee190..591f04a 100644 --- a/CMakeLists.txt.user +++ b/CMakeLists.txt.user @@ -1,10 +1,10 @@ - + EnvironmentId - {e8c938e2-c8fa-4354-a6d6-5cb042d4a566} + {b1d6c31c-e354-4f9b-a89f-0f02a67ed6d1} ProjectExplorer.Project.ActiveTarget @@ -87,26 +87,25 @@ ProjectExplorer.Project.Target.0 Desktop - 桌面 - 桌面 - {976a26f7-9753-41c3-a002-83e83b921ad8} + Desktop (x86-darwin-generic-mach_o-32bit) + Desktop (x86-darwin-generic-mach_o-32bit) + {ea69aa4c-9f65-4091-af6f-c8dca7313cfe} 0 0 - 0 + -1 Debug 2 false - -DCMAKE_GENERATOR:STRING=Unix Makefiles --DCMAKE_BUILD_TYPE:STRING=Debug + -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{buildDir}/.qtc/package-manager/auto-setup.cmake --DQT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable} --DCMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX} --DCMAKE_C_COMPILER:STRING=%{Compiler:Executable:C} --DCMAKE_CXX_COMPILER:STRING=%{Compiler:Executable:Cxx} --DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} - /home/kingecg/code/build-hkstreamer-unknown-Debug +-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} +-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} +-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} +-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} +%{CMAKE_OSX_ARCHITECTURES:DefaultFlag} + /Users/chengguang/code/build-hkstreamer-Desktop_x86_darwin_generic_mach_o_32bit-Debug @@ -154,15 +153,14 @@ 2 false - -DCMAKE_GENERATOR:STRING=Unix Makefiles --DCMAKE_BUILD_TYPE:STRING=Release + -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{buildDir}/.qtc/package-manager/auto-setup.cmake --DQT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable} --DCMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX} --DCMAKE_C_COMPILER:STRING=%{Compiler:Executable:C} --DCMAKE_CXX_COMPILER:STRING=%{Compiler:Executable:Cxx} --DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} - /home/kingecg/code/build-hkstreamer-unknown-Release +-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} +-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} +-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} +-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} +%{CMAKE_OSX_ARCHITECTURES:DefaultFlag} + /Users/chengguang/code/build-hkstreamer-Desktop_x86_darwin_generic_mach_o_32bit-Release @@ -172,7 +170,6 @@ false true - 构建 CMakeProjectManager.MakeStep 1 @@ -189,7 +186,6 @@ false true - 构建 CMakeProjectManager.MakeStep 1 @@ -210,15 +206,14 @@ 2 false - -DCMAKE_GENERATOR:STRING=Unix Makefiles --DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo + -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo -DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{buildDir}/.qtc/package-manager/auto-setup.cmake --DQT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable} --DCMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX} --DCMAKE_C_COMPILER:STRING=%{Compiler:Executable:C} --DCMAKE_CXX_COMPILER:STRING=%{Compiler:Executable:Cxx} --DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} - /home/kingecg/code/build-hkstreamer-unknown-RelWithDebInfo +-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} +-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} +-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} +-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} +%{CMAKE_OSX_ARCHITECTURES:DefaultFlag} + /Users/chengguang/code/build-hkstreamer-Desktop_x86_darwin_generic_mach_o_32bit-RelWithDebInfo @@ -264,16 +259,15 @@ 2 false - -DCMAKE_GENERATOR:STRING=Unix Makefiles --DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo + -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo -DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{buildDir}/.qtc/package-manager/auto-setup.cmake --DQT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable} --DCMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX} --DCMAKE_C_COMPILER:STRING=%{Compiler:Executable:C} --DCMAKE_CXX_COMPILER:STRING=%{Compiler:Executable:Cxx} --DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} +-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} +-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} +-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} +-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} +%{CMAKE_OSX_ARCHITECTURES:DefaultFlag} 0 - /home/kingecg/code/build-hkstreamer-unknown-Profile + /Users/chengguang/code/build-hkstreamer-Desktop_x86_darwin_generic_mach_o_32bit-Profile @@ -319,15 +313,14 @@ 2 false - -DCMAKE_GENERATOR:STRING=Unix Makefiles --DCMAKE_BUILD_TYPE:STRING=MinSizeRel + -DCMAKE_BUILD_TYPE:STRING=MinSizeRel -DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{buildDir}/.qtc/package-manager/auto-setup.cmake --DQT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable} --DCMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX} --DCMAKE_C_COMPILER:STRING=%{Compiler:Executable:C} --DCMAKE_CXX_COMPILER:STRING=%{Compiler:Executable:Cxx} --DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} - /home/kingecg/code/build-hkstreamer-unknown-MinSizeRel +-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} +-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} +-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} +-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} +%{CMAKE_OSX_ARCHITECTURES:DefaultFlag} + /Users/chengguang/code/build-hkstreamer-Desktop_x86_darwin_generic_mach_o_32bit-MinSizeRel @@ -382,30 +375,307 @@ ProjectExplorer.DefaultDeployConfiguration 1 - - true - true - true - - 2 - - hkstreamer - CMakeProjectManager.CMakeRunConfiguration.hkstreamer - hkstreamer - --cmd test -U user -P passwd --channel 1 - false - true - true - false - true - /home/kingecg/code/build-hkstreamer-unknown-Debug + 0 + + + + ProjectExplorer.Project.Target.1 + + Desktop + Desktop (x86-darwin-generic-mach_o-64bit) + Desktop (x86-darwin-generic-mach_o-64bit) + {c56fdce9-f42d-46e6-a039-74b4e52116a9} + 0 + 0 + -1 + + Debug + 2 + false + + -DCMAKE_BUILD_TYPE:STRING=Debug +-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{buildDir}/.qtc/package-manager/auto-setup.cmake +-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} +-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} +-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} +-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} +%{CMAKE_OSX_ARCHITECTURES:DefaultFlag} + /Users/chengguang/code/build-hkstreamer-Desktop_x86_darwin_generic_mach_o_64bit-Debug + + + + + all + + false + + true + 构建 + CMakeProjectManager.MakeStep + + 1 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + + + clean + + false + + true + 构建 + CMakeProjectManager.MakeStep + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Debug + CMakeProjectManager.CMakeBuildConfiguration - 1 + + Release + 2 + false + + -DCMAKE_BUILD_TYPE:STRING=Release +-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{buildDir}/.qtc/package-manager/auto-setup.cmake +-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} +-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} +-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} +-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} +%{CMAKE_OSX_ARCHITECTURES:DefaultFlag} + /Users/chengguang/code/build-hkstreamer-Desktop_x86_darwin_generic_mach_o_64bit-Release + + + + + all + + false + + true + CMakeProjectManager.MakeStep + + 1 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + + + clean + + false + + true + CMakeProjectManager.MakeStep + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Release + CMakeProjectManager.CMakeBuildConfiguration + + + RelWithDebInfo + 2 + false + + -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo +-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{buildDir}/.qtc/package-manager/auto-setup.cmake +-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} +-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} +-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} +-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} +%{CMAKE_OSX_ARCHITECTURES:DefaultFlag} + /Users/chengguang/code/build-hkstreamer-Desktop_x86_darwin_generic_mach_o_64bit-RelWithDebInfo + + + + + all + + false + + true + CMakeProjectManager.MakeStep + + 1 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + + + clean + + false + + true + CMakeProjectManager.MakeStep + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Release with Debug Information + CMakeProjectManager.CMakeBuildConfiguration + + + RelWithDebInfo + 2 + false + + -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo +-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{buildDir}/.qtc/package-manager/auto-setup.cmake +-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} +-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} +-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} +-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} +%{CMAKE_OSX_ARCHITECTURES:DefaultFlag} + 0 + /Users/chengguang/code/build-hkstreamer-Desktop_x86_darwin_generic_mach_o_64bit-Profile + + + + + all + + false + + true + CMakeProjectManager.MakeStep + + 1 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + + + clean + + false + + true + CMakeProjectManager.MakeStep + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Profile + CMakeProjectManager.CMakeBuildConfiguration + + + MinSizeRel + 2 + false + + -DCMAKE_BUILD_TYPE:STRING=MinSizeRel +-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{buildDir}/.qtc/package-manager/auto-setup.cmake +-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} +-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} +-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} +-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} +%{CMAKE_OSX_ARCHITECTURES:DefaultFlag} + /Users/chengguang/code/build-hkstreamer-Desktop_x86_darwin_generic_mach_o_64bit-MinSizeRel + + + + + all + + false + + true + CMakeProjectManager.MakeStep + + 1 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + + + clean + + false + + true + CMakeProjectManager.MakeStep + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Minimum Size Release + CMakeProjectManager.CMakeBuildConfiguration + + 5 + + + 0 + 部署 + 部署 + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + 0 ProjectExplorer.Project.TargetCount - 1 + 2 ProjectExplorer.Project.Updater.FileVersion diff --git a/hkstreamer.creator.user b/hkstreamer.creator.user index 106ac4b..03b315f 100644 --- a/hkstreamer.creator.user +++ b/hkstreamer.creator.user @@ -1,10 +1,10 @@ - + EnvironmentId - {e8c938e2-c8fa-4354-a6d6-5cb042d4a566} + {b1d6c31c-e354-4f9b-a89f-0f02a67ed6d1} ProjectExplorer.Project.ActiveTarget @@ -87,14 +87,14 @@ ProjectExplorer.Project.Target.0 Desktop - 桌面 - 桌面 - {976a26f7-9753-41c3-a002-83e83b921ad8} + Desktop (x86-darwin-generic-mach_o-32bit) + Desktop (x86-darwin-generic-mach_o-32bit) + {ea69aa4c-9f65-4091-af6f-c8dca7313cfe} 0 0 0 - /home/kingecg/code/hkstreamer + /Users/chengguang/code/hkstreamer diff --git a/hkstreamer.files b/hkstreamer.files index 5be5eea..ceae3fb 100644 --- a/hkstreamer.files +++ b/hkstreamer.files @@ -32,6 +32,8 @@ lib/libhpr.so lib/libopenal.so.1 lib/libssl.so.1.1 lib/libz.so +nlohmann/json.hpp +nlohmann/json_fwd.hpp src/HkStreamer.cpp src/cmdlineparser.cpp src/convert.cpp diff --git a/hkstreamer.includes b/hkstreamer.includes index d5c3200..710fd07 100644 --- a/hkstreamer.includes +++ b/hkstreamer.includes @@ -1,2 +1,3 @@ include src +nlohmann diff --git a/include/HkStreamer.h b/include/HkStreamer.h index 5fe300b..658c6cb 100644 --- a/include/HkStreamer.h +++ b/include/HkStreamer.h @@ -34,6 +34,7 @@ extern "C" #define HPR_OK 0 #define HPR_ERROR -1 int playback(LPLoginInfo loginInfo, LPStream stream); + int play(LPLoginInfo loginInfo, LPStream stream); int getCfg(LPLoginInfo loginInfo); #ifdef __cplusplus } diff --git a/src/HkStreamer.cpp b/src/HkStreamer.cpp index d073ae1..6031c3a 100644 --- a/src/HkStreamer.cpp +++ b/src/HkStreamer.cpp @@ -3,12 +3,38 @@ #include #include #include +#include +#include #include "../include/HCNetSDK.h" #include "../include/HkStreamer.h" #include "../include/convert.h" + using namespace std; +long playHandle = -1; +bool is_playback = false; +long lUserID; const int outcache_size=1024; -void CALLBACK fPlayDataCallBack(LONG lPlayHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, void* dwUser){ + +void signalexit(int signal){ + if(playHandle!= -1){ + if(is_playback){ + NET_DVR_StopGetFile(playHandle); + } else { + NET_DVR_StopRealPlay(playHandle); + } + NET_DVR_Logout(lUserID); + NET_DVR_Cleanup(); + } + exit(signal); +} + +void exitHandler(){ + signalexit(0); +} + + +template +void CALLBACK fPlayDataCallBack(LONG lPlayHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, T dwUser){ DWORD remained = dwBufSize; char cache[outcache_size]={0}; BYTE *cp; // cp head point @@ -59,7 +85,7 @@ int login(LPLoginInfo loginInfo) int getCfg(LPLoginInfo loginInfo) { - long lUserID = login(loginInfo); + lUserID = login(loginInfo); if (lUserID < 0) { std::cerr << "pyd1---Login error" << NET_DVR_GetLastError() << std::endl; @@ -102,31 +128,33 @@ int _playback(long lUserID, int timezone,LPStream stream) { convert_isotimestr_time(stream->start,timezone, &(struVodPara.struBeginTime)); convert_isotimestr_time(stream->start,timezone, &(struVodPara.struEndTime)); struVodPara.struIDInfo.dwChannel = stream->lChannel; - int hPlayback = NET_DVR_PlayBackByTime_V40(lUserID, &struVodPara); - if(checkResult(hPlayback<0,"play back error")){ + playHandle = NET_DVR_PlayBackByTime_V40(lUserID, &struVodPara); + if(checkResult(playHandle<0,"play back error")){ return HPR_ERROR; } + is_playback = true; char usrInfo[200]; - if(checkResult(!NET_DVR_SetPlayDataCallBack_V40(hPlayback, fPlayDataCallBack, usrInfo), "setup callback error")){ + if(checkResult(!NET_DVR_SetPlayDataCallBack_V40(playHandle, fPlayDataCallBack, usrInfo), "setup callback error")){ return HPR_ERROR; } - if(checkResult(!NET_DVR_PlayBackControl_V40(hPlayback, NET_DVR_PLAYSTART,NULL, 0, NULL, NULL),"start playback error")){ + if(checkResult(!NET_DVR_PlayBackControl_V40(playHandle, NET_DVR_PLAYSTART,NULL, 0, NULL, NULL),"start playback error")){ return HPR_ERROR; } - if(checkResult(!NET_DVR_PlayBackControl_V40(hPlayback, NET_DVR_SETSPEED,&(stream->bitrate), 0, NULL, NULL),"set plackback speed error")){ + if(checkResult(!NET_DVR_PlayBackControl_V40(playHandle, NET_DVR_SETSPEED,&(stream->bitrate), 0, NULL, NULL),"set plackback speed error")){ return HPR_ERROR; } int pos = 0; - for (pos = 0; pos < 100 && pos >= 0; pos = NET_DVR_GetDownloadPos(hPlayback)) + for (pos = 0; pos < 100 && pos >= 0; pos = NET_DVR_GetDownloadPos(playHandle)) { // output progress in cerr std::cerr<<"Pos:"<lChannel = stream->lChannel + 33/*channel start*/ - 1; + int fChannelInfo = _readChannelConfig(lUserID, stream); + if(fChannelInfo == HPR_ERROR){ + return fChannelInfo; + } +// stream->lChannel = stream->lChannel + 33/*channel start*/ - 1; int playbackResult = _playback(lUserID, timezone, stream); NET_DVR_Logout(lUserID); NET_DVR_Cleanup(); return playbackResult; } + +int _getStream(long lUserID,LPStream stream){ + long lRealPlayHandle; + NET_DVR_PREVIEWINFO struPlayInfo = {0}; + struPlayInfo.hPlayWnd = 0; + struPlayInfo.lChannel = stream->lChannel; //channel NO + struPlayInfo.dwLinkMode = 1; // 采用子码流 + struPlayInfo.bBlocked = 1; + struPlayInfo.dwDisplayBufNum = 1; + playHandle = NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, NULL, NULL); + if(checkResult(lRealPlayHandle<0,"start play error")){ + return HPR_ERROR; + } + int scRet; + scRet = NET_DVR_SetStandardDataCallBack(playHandle,fPlayDataCallBack,0); + if(checkResult(scRet<0,"set play callback error")){ + return HPR_ERROR; + } + return 0; +} + + +int play(LPLoginInfo loginInfo, LPStream stream) +{ + lUserID = login(loginInfo); + if(checkResult(lUserID<1, "device login error")){ + return HPR_ERROR; + } + int fChannelInfo = _readChannelConfig(lUserID, stream); + if(fChannelInfo == HPR_ERROR){ + return fChannelInfo; + } + + return _getStream(lUserID, stream); +}