#include "VideoCut.h"
CVideoCut::CVideoCut(void):m_nDownLoad(0),m_strLinkInfo(""){
}
CVideoCut::~CVideoCut(void){
}
bool CVideoCut::download(string strIp,string userID,string password,WORD port,string startTime,string endTime){
//cout<<"strIp:"<<strIp<<" userID:"<<userID<<" password:"<<password<<" port:"<<port<<" startTIme:"<<startTime<<" endTime:"<<endTime<<endl;
NET_DVR_TIME struStartTime, struStopTime;
if(startTime.length() > 0){
vector<string> sTime =splitString(startTime,"-");
if(sTime.size() ==6){
struStartTime.dwYear = atol(sTime[0].c_str());
struStartTime.dwMonth = atol(sTime[1].c_str());
struStartTime.dwDay = atol(sTime[2].c_str());
struStartTime.dwHour = atol(sTime[3].c_str());
struStartTime.dwMinute = atol(sTime[4].c_str());
struStartTime.dwSecond = atol(sTime[5].c_str());
}
else{
cerr<<"解析开始时间失败"<<endl;
return false;
}
}
if(endTime.length() > 0){
vector<string> eTime = splitString(endTime,"-");
if(eTime.size() ==6){
struStopTime.dwYear = atol(eTime[0].c_str());
struStopTime.dwMonth = atol(eTime[1].c_str());
struStopTime.dwDay = atol(eTime[2].c_str());
struStopTime.dwHour = atol(eTime[3].c_str());
struStopTime.dwMinute = atol(eTime[4].c_str());
struStopTime.dwSecond = atol(eTime[5].c_str());
}
else{
cerr<<"解析结束时间失败"<<endl;
return false;
}
}
if((struStartTime.dwYear > struStopTime.dwYear) ||
((struStartTime.dwYear - struStopTime.dwYear) == 0 && struStartTime.dwMonth > struStopTime.dwMonth)||
((struStartTime.dwYear - struStopTime.dwYear) == 0 && (struStartTime.dwMonth - struStopTime.dwMonth) == 0 && struStartTime.dwDay > struStopTime.dwDay)||
((struStartTime.dwYear - struStopTime.dwYear) == 0 && (struStartTime.dwMonth - struStopTime.dwMonth) == 0 && (struStartTime.dwDay - struStopTime.dwDay) == 0 && struStartTime.dwHour > struStopTime.dwHour) ||
((struStartTime.dwYear - struStopTime.dwYear) == 0 && (struStartTime.dwMonth - struStopTime.dwMonth) == 0 && (struStartTime.dwDay - struStopTime.dwDay) == 0 && (struStartTime.dwHour - struStopTime.dwHour) == 0 && struStartTime.dwMinute > struStopTime.dwMinute) ||
((struStartTime.dwYear - struStopTime.dwYear) == 0 && (struStartTime.dwMonth - struStopTime.dwMonth) == 0 && (struStartTime.dwDay - struStopTime.dwDay) == 0 && (struStartTime.dwHour - struStopTime.dwHour) == 0 && (struStartTime.dwMinute - struStopTime.dwMinute) == 0 && struStartTime.dwSecond >= struStopTime.dwSecond)){
cerr<<"输入的开始时间在结束时间之后,请检查"<<endl;
return false;
}
//匹配文件路径
int hPlayback;
string FileName = ReadVideoPathFromConfigFile();
FileName.append((char*)startTime.c_str());
FileName.append((char*)"_");
FileName.append((char*)endTime.c_str());
FileName.append((char*)".MP4");
//cout<<"FileName "<<FileName<<endl;
//判断文件是否已经存在
ifstream File(FileName);
if(File){
cerr << "要创建的文件已经存在"<<endl;
return false;
}
// 初始化
NET_DVR_Init();
//设置连接时间与重连时间
NET_DVR_SetConnectTime(2000, 1);
NET_DVR_SetReconnect(10000, true);
// 注册设备
LONG lUserID;
NET_DVR_DEVICEINFO_V30 struDeviceInfo;
lUserID = NET_DVR_Login_V30((char*)strIp.c_str(),port,(char*)userID.c_str(),(char*)password.c_str(), &struDeviceInfo);
if (lUserID < 0)
{
printf("Login error, %d\n", NET_DVR_GetLastError());
NET_DVR_Cleanup();
return false;
}
//下载文件
hPlayback = NET_DVR_GetFileByTime(lUserID,2,&struStartTime,&struStopTime,(char*)FileName.c_str());
if(hPlayback < 0){
printf("NET_DVR_GetFileByTime fail,last error %d\n",NET_DVR_GetLastError());
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return false;
}
//开始下载,控制录像下载的状态
if(!NET_DVR_PlayBackControl(hPlayback,NET_DVR_PLAYSTART,0,NULL)){
printf("play back control failed [%d],[%d]\n",NET_DVR_GetLastError(),m_nDownLoad);
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return false;
}
int nPos = 0;
for(nPos = 0; nPos < 100&&nPos>=0; nPos = NET_DVR_GetDownloadPos(hPlayback)){
cout<<nPos<<endl;
Sleep(1000); //millisecond
}
cout<<nPos<<endl;
if(!NET_DVR_StopGetFile(hPlayback)) {
printf("failed to stop get file [%d]\n",NET_DVR_GetLastError());
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return false;
}
if(nPos<0||nPos>100){
printf("download err [%d]\n",NET_DVR_GetLastError());
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return false;
}
//注销用户
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
cout<<"downLoad "<<FileName<<" Finished"<<endl;
return true;
}
void CVideoCut::CutVideoThread(string strIp,string userID,string password,WORD port,string startTime,string endTime){
boost::thread FireAlarmThrd = boost::thread(&CVideoCut::CutVideo,this,strIp,userID,password,port,startTime,endTime);
}
void CVideoCut::CutVideo(string strIp,string userID,string password,WORD port,string startTime,string endTime){
try {
m_strLinkInfo = "db=owl user=root password=123456 host=192.168.3.105 port=3306";
// 连接数据库
session sql(mysql,m_strLinkInfo);
string FileName =ReadVideoPathFromConfigFile();
FileName.append((char*)startTime.c_str());
FileName.append((char*)"_");
FileName.append((char*)endTime.c_str());
FileName.append((char*)".MP4");
//写数据库,标识文件下载开始
string sqlState="update owl.file set status=1 where filename ='";
sqlState.append(FileName);
sqlState.append("'");
sql <<sqlState;
bool bFinished=false;
//bool bFinished=download(strIp,userID,password,port,startTime,endTime);
while(m_nDownLoad<3){
if(download(strIp,userID,password,port,startTime,endTime)){
bFinished=true;
break;
}
else{
removeFile(startTime,endTime);
}
m_nDownLoad++;
Sleep(10);
}
//写数据库,失败
if(!bFinished){
sqlState="update owl.file set status='2' where filename ='";
sqlState.append(FileName);
sqlState.append("'");
sql <<sqlState;
}
else{
//写数据库,成功
sqlState="update owl.file set status='0' where filename ='";
sqlState.append(FileName);
sqlState.append("'");
sql <<sqlState;
}
}
catch (exception const &e){
cerr << "Error: " << e.what() << '\n';
}
}
bool CVideoCut::removeFile(string startTime,string endTime){
bool retVal=false;
string FileName = ReadVideoPathFromConfigFile();
FileName.append((char*)startTime.c_str());
FileName.append((char*)"_");
FileName.append((char*)endTime.c_str());
FileName.append((char*)".MP4");
ifstream iFile(FileName);
if(iFile){
iFile.close();
if(DeleteFile(FileName.c_str()) == TRUE)
retVal=true;
}
else{
retVal=true;
}
return retVal;
}
////////////////////////////////////////////////////////////////
//Function: 解析ICE传入的信息,获取相应的调用函数及参数数据
//s: ICE传入的参数,即要解析的对象
//splitedToken:分割传入数据的标识
/////////////////////////////////////////////////////////////////
vector<string> CVideoCut::splitString(const string& s,const string& splitedToken) {
vector<string> ret;
size_t oldPos,newPos;
oldPos=0;
newPos=0;
string tempData;
while(true){
newPos=s.find(splitedToken,oldPos);
if(newPos!=string::npos) {
tempData = s.substr(oldPos,newPos-oldPos);
ret.push_back(tempData);
oldPos=newPos+splitedToken.size();
}
else if(oldPos<=s.size()) {
tempData= s.substr(oldPos);
ret.push_back(tempData);
break;
}
else{
break;
}
}//while
return ret;
}
void CVideoCut::Test(string paramIn){
paramIn = Trim(paramIn);
vector<string> sTime =splitString(paramIn," ");
vector<string> ymd=splitString(sTime[0],"-");
for (vector<string>::iterator tempit =ymd.begin();tempit < ymd.end();tempit++){
cout<<*tempit<<" "<<endl;
}
vector<string> hms=splitString(sTime[1],":");
f