From 87e1c6f69c0fe27af09b23809d05d7967ff6762a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=B9=BF?= Date: Fri, 31 Mar 2023 18:32:46 +0800 Subject: [PATCH] refactor --- CMakeLists.txt | 8 +- hkstreamer.creator.user | 12 +- hkstreamer.creator.user.e8c938e | 175 ++++++++++++++++++++ hkstreamer.files | 7 +- hkstreamer.includes | 1 + include/HkStreamer.h | 26 ++- include/cmdlineparser.h | 6 +- include/convert.h | 9 ++ HkStreamer.cpp => src/HkStreamer.cpp | 179 +++++++++++---------- cmdlineparser.cpp => src/cmdlineparser.cpp | 47 +++++- src/convert.cpp | 35 ++++ main.cpp => src/main.cpp | 0 12 files changed, 393 insertions(+), 112 deletions(-) create mode 100644 hkstreamer.creator.user.e8c938e create mode 100644 include/convert.h rename HkStreamer.cpp => src/HkStreamer.cpp (57%) rename cmdlineparser.cpp => src/cmdlineparser.cpp (56%) create mode 100644 src/convert.cpp rename main.cpp => src/main.cpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 47ea4e4..39ee08e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,11 +26,13 @@ set (SRCS include/HCNetSDK.h include/HkStreamer.h include/plaympeg4.h - HkStreamer.cpp - main.cpp + include/cmdlineparser.h + src/HkStreamer.cpp + src/main.cpp + src/cmdlineparser.cpp ) add_executable(${CMAKE_PROJECT_NAME} ${SRCS}) target_link_libraries(${CMAKE_PROJECT_NAME} libhcnetsdk.so) -target_link_libraries(${CMAKE_PROJECT_NAME} libhpr.so) \ No newline at end of file +target_link_libraries(${CMAKE_PROJECT_NAME} libhpr.so) diff --git a/hkstreamer.creator.user b/hkstreamer.creator.user index 256b2fd..dc4e89b 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.creator.user.e8c938e b/hkstreamer.creator.user.e8c938e new file mode 100644 index 0000000..256b2fd --- /dev/null +++ b/hkstreamer.creator.user.e8c938e @@ -0,0 +1,175 @@ + + + + + + EnvironmentId + {e8c938e2-c8fa-4354-a6d6-5cb042d4a566} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + false + true + false + 0 + true + true + 0 + 8 + true + false + 1 + true + true + true + *.md, *.MD, Makefile + false + true + true + + + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + + 0 + true + + true + true + Builtin.DefaultTidyAndClazy + 4 + + + + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + 桌面 + 桌面 + {976a26f7-9753-41c3-a002-83e83b921ad8} + 0 + 0 + 0 + + /home/kingecg/code/hkstreamer + + + + all + + true + GenericProjectManager.GenericMakeStep + + 1 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + + clean + + true + GenericProjectManager.GenericMakeStep + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + 默认 + GenericProjectManager.GenericBuildConfiguration + + 1 + + + 0 + 部署 + 部署 + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + true + true + true + + 2 + + ProjectExplorer.CustomExecutableRunConfiguration + + false + true + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/hkstreamer.files b/hkstreamer.files index 96a92f5..5be5eea 100644 --- a/hkstreamer.files +++ b/hkstreamer.files @@ -4,6 +4,8 @@ include/DataType.h include/DecodeCardSdk.h include/HCNetSDK.h include/HkStreamer.h +include/cmdlineparser.h +include/convert.h include/plaympeg4.h lib/HCNetSDKCom/libAudioIntercom.so lib/HCNetSDKCom/libHCAlarm.so @@ -30,4 +32,7 @@ lib/libhpr.so lib/libopenal.so.1 lib/libssl.so.1.1 lib/libz.so -main.cpp +src/HkStreamer.cpp +src/cmdlineparser.cpp +src/convert.cpp +src/main.cpp diff --git a/hkstreamer.includes b/hkstreamer.includes index a1a869c..d5c3200 100644 --- a/hkstreamer.includes +++ b/hkstreamer.includes @@ -1 +1,2 @@ include +src diff --git a/include/HkStreamer.h b/include/HkStreamer.h index 9c0edca..5fe300b 100644 --- a/include/HkStreamer.h +++ b/include/HkStreamer.h @@ -1,6 +1,7 @@ #ifndef _HK_STREAMER_ #define _HK_STREAMER_ - +#include +using namespace std; // // #ifdef __LINUX__ // typedef unsigned char UCHAR; // typedef unsigned char* PBYTE; @@ -16,25 +17,18 @@ extern "C" #endif typedef struct { - char sDeviceAddress[129]; - char sUserName[64]; - char sPassword[64]; + string sDeviceAddress; + string sUserName; + string sPassword; } LoginInfo, *LPLoginInfo; - typedef struct - { - unsigned long dwYear; // 年 - unsigned long dwMonth; // 月 - unsigned long dwDay; // 日 - unsigned long dwHour; // 时 - unsigned long dwMinute; // 分 - unsigned long dwSecond; // 秒 - } StreamDate, *LStreamDate; + typedef struct { unsigned long lChannel; - StreamDate start; - StreamDate end; + unsigned long bitrate; + string start; + string end; } StreamCon, *LPStream; #define HPR_OK 0 @@ -45,4 +39,4 @@ extern "C" } #endif -#endif \ No newline at end of file +#endif diff --git a/include/cmdlineparser.h b/include/cmdlineparser.h index cfec78d..7f454b0 100644 --- a/include/cmdlineparser.h +++ b/include/cmdlineparser.h @@ -15,14 +15,18 @@ typedef struct { class CmdLineParser { public: - CmdLineParser(); + CmdLineParser(string name,string desc); void regOpt(OptDefine optdef); + void regOpt(string name,int shorName, int needParam, string desc); string getOpt(string key); void parseArgs(int argc, char *argv[]); private: + string appName; + string appDesc; map optMap; map paraMap; bool hasOptSet(string key); + void help(); }; #endif // CMDLINEPARSER_H diff --git a/include/convert.h b/include/convert.h new file mode 100644 index 0000000..c02f9e2 --- /dev/null +++ b/include/convert.h @@ -0,0 +1,9 @@ +#ifndef CONVERT_H +#define CONVERT_H +#include +#include +using namespace std; +void convert_str_chararr(string src, char *dst, int dstlen); +void convert_isotimestr_time(string timestr, int timezone, LPNET_DVR_TIME time); + +#endif // CONVERT_H diff --git a/HkStreamer.cpp b/src/HkStreamer.cpp similarity index 57% rename from HkStreamer.cpp rename to src/HkStreamer.cpp index 8871197..2a2094b 100644 --- a/HkStreamer.cpp +++ b/src/HkStreamer.cpp @@ -5,22 +5,37 @@ #include #include "HCNetSDK.h" #include "HkStreamer.h" - +#include "convert.h" +using namespace std; +const int outcache_size=1024; void CALLBACK fPlayDataCallBack(LONG lPlayHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, void* dwUser) { - if (dwBufSize > 0) - { - char pb[dwBufSize] = {0}; - memcpy(pb, pBuffer, dwBufSize); - - std::cout.write(pb, dwBufSize); - std::cout.flush(); - } - int pos = NET_DVR_GetDownloadPos(lPlayHandle); - if (pos > 98) - { - fprintf(stderr, "pyd---Current Time:%d, data size:%d\n", time(NULL), dwBufSize); + DWORD remained = dwBufSize; + char cache[outcache_size]={0}; + BYTE *cp; // cp head point + cp = pBuffer; + while(remained>0){ + int cplen = outcache_size; + if(remaineddwYear = date.dwYear; nvrTime->dwMonth = date.dwMonth; @@ -49,74 +64,7 @@ void cpTime(NET_DVR_TIME *nvrTime, StreamDate date) nvrTime->dwMinute = date.dwMinute; nvrTime->dwSecond = date.dwSecond; } -int playback(LPLoginInfo loginInfo, LPStream stream) -{ - long lUserID = login(loginInfo); - if (lUserID < 0) - { - std::cerr << "pyd1---Login error" << NET_DVR_GetLastError() << std::endl; - return HPR_ERROR; - } - NET_DVR_VOD_PARA struVodPara = {0}; - StreamDate start = stream->start; - struVodPara.struIDInfo.dwChannel = stream->lChannel; - cpTime(&struVodPara.struBeginTime, start); - cpTime(&struVodPara.struEndTime, stream->end); - int hPlayback = NET_DVR_PlayBackByTime_V40(lUserID, &struVodPara); - if (hPlayback < 0) - { - std::cerr << "pyd1---PlayBack error: " << NET_DVR_GetLastError() << std::endl; - return HPR_ERROR; - } - char usrInfo[200]; - if (!NET_DVR_SetPlayDataCallBack_V40(hPlayback, fPlayDataCallBack, usrInfo)) - { - std::cerr << "NET_DVR_SetPlayDataCallBack fail!\n" - << std::endl; - } - time_t lstart = time(NULL); - if (!NET_DVR_PlayBackControl_V40(hPlayback, NET_DVR_PLAYSTART,NULL, 0, NULL, NULL)) - { - std::cerr << "play back control failed : " << NET_DVR_GetLastError() << std::endl; - return HPR_ERROR; - } - - int pos = 0; - for (pos = 0; pos < 100 && pos >= 0; pos = NET_DVR_GetDownloadPos(hPlayback)) - { - std::cerr<<"Pos:"< 100) - { - std::cerr << "download err:" << NET_DVR_GetLastError() << std::endl; - return HPR_ERROR; - } - else - { - return 0; - } - - // sleep(180); //˯3���� - NET_DVR_Logout_V30(lUserID); - // cleanup - NET_DVR_Cleanup(); - - return HPR_OK; -} int getCfg(LPLoginInfo loginInfo) { long lUserID = login(loginInfo); @@ -155,4 +103,71 @@ int getCfg(LPLoginInfo loginInfo) NET_DVR_Logout(lUserID); NET_DVR_Cleanup(); return 0; -} \ No newline at end of file +} + +int _playback(long lUserID, int timezone,LPStream stream) { + NET_DVR_VOD_PARA struVodPara = {0}; + 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")){ + return HPR_ERROR; + } + char usrInfo[200]; + if(checkResult(!NET_DVR_SetPlayDataCallBack_V40(hPlayback, 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")){ + return HPR_ERROR; + } + if(checkResult(!NET_DVR_PlayBackControl_V40(hPlayback, 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)) + { + // output progress in cerr + std::cerr<<"Pos:"<lChannel > ipcfg.dwDChanNum, "channel not correct")){ + return HPR_ERROR; + } + + stream->lChannel = ipcfg.dwStartDChan + stream->lChannel -1; + stream->bitrate = 2048; // controll speed for download, TODO: get bitrate from device; +} + +int playback(LPLoginInfo loginInfo, LPStream stream) +{ + long lUserID = login(loginInfo); + if(checkResult(lUserID<1, "device login error")){ + return HPR_ERROR; + } + // TODO: find timezone cfg + int timezone = 8; + // TODO: find actually channel + stream->lChannel = stream->lChannel + 33/*channel start*/ - 1; + int playbackResult = _playback(lUserID, timezone, stream); + NET_DVR_Logout(lUserID); + NET_DVR_Cleanup(); + return playbackResult; +} diff --git a/cmdlineparser.cpp b/src/cmdlineparser.cpp similarity index 56% rename from cmdlineparser.cpp rename to src/cmdlineparser.cpp index ad2fb59..727bb7a 100644 --- a/cmdlineparser.cpp +++ b/src/cmdlineparser.cpp @@ -1,8 +1,19 @@ #include "cmdlineparser.h" - -CmdLineParser::CmdLineParser() +#include +using namespace std; +template +bool mapHasKey(map *pMap, int key){ + try { + pMap->at(key); + return true; + }catch(exception e) { + return false; + } +} +CmdLineParser::CmdLineParser(string name,string desc) { - + appDesc = desc; + appName = name; } void CmdLineParser::regOpt(OptDefine optdef) @@ -10,6 +21,12 @@ void CmdLineParser::regOpt(OptDefine optdef) optMap[optdef.shortName] = optdef; } +void CmdLineParser::regOpt(string name, int shorName, int needParam, string desc) +{ + OptDefine opt = {name,shorName, needParam, desc}; + regOpt(opt); +} + string CmdLineParser::getOpt(string key) { if(!hasOptSet(key)){ @@ -43,13 +60,21 @@ void CmdLineParser::parseArgs(int argc, char *argv[]) int optIndex=0; int opt; while ((opt=getopt_long(argc, argv, shortArgs.c_str(), longOption, &optIndex))!=-1) { + if(!mapHasKey(&optMap, opt)){ + help(); + exit(0); + } OptDefine d = optMap.at(opt); + if(d.needParam == no_argument){ paraMap[d.name] = "true"; } else { paraMap[d.name] = optarg; } } + //释放动态构造的option数组 + + delete[] longOption; } bool CmdLineParser::hasOptSet(string key) @@ -60,3 +85,19 @@ bool CmdLineParser::hasOptSet(string key) } return true; } + +void CmdLineParser::help() +{ + cout<< appName <<":" << appDesc << endl; + cout<< "Usage:" << appName << "[opt] optvalue" << endl; + for(auto i = optMap.begin();i!=optMap.end(); ++i) { + OptDefine od = i->second; + cout<<"--"<=65&&od.shortName<=90){ + cout<<",-"< +#include +#include "time.h" + +using namespace std; + +void convert_str_chararr(string src, char *dst, int dstlen) +{ + int srcSize = src.size(); + int cplen = 0; + if(dstlen>=srcSize){ + cplen = srcSize; + } else { + cplen = dstlen; + } + strlcpy(dst, src.c_str(), cplen); +} + + +void convert_isotimestr_time(string timestr, int timezone, LPNET_DVR_TIME time) +{ + struct tm t; + strptime(timestr.c_str(), "%Y-%m-%dT%H:%M:%SZ", &t); + time_t ts = mktime(&t); + ts+=timezone*3600; + struct tm ltime; + localtime_r(&ts, <ime); + time->dwYear = ltime.tm_year + 1900; + time->dwMonth = ltime.tm_mon + 1; + time->dwDay = ltime.tm_mday; + time->dwHour = ltime.tm_hour; + time->dwMinute = ltime.tm_min; + time->dwSecond = ltime.tm_sec; +} diff --git a/main.cpp b/src/main.cpp similarity index 100% rename from main.cpp rename to src/main.cpp