| | |
| | | using std::string; |
| | | |
| | | RtspAnalysManager::RtspAnalysManager(LDBTool *_dbTool) : m_lDBTool(nullptr), m_maxCount(50), m_currentCount(0) { |
| | | m_lDBTool=new LDBTool; |
| | | INFO("MYH DEBUG HERE"); |
| | | m_lDBTool = new LDBTool; |
| | | init(); |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | static std::string rtspAddrBuild(std::string ip, const int port, std::string username, |
| | |
| | | return rtsp_url; |
| | | } |
| | | |
| | | //初始化函数 |
| | | void RtspAnalysManager::init() { |
| | | INFO("MYH DEBUG HERE") |
| | | |
| | | auto lst = m_lDBTool->searchCamDevTableAll(); |
| | | Record_Config lst_dev = m_lDBTool->searchConfigTableWithinServerInfo(); |
| | | m_GB28181_Enable = appPref.getIntData("GB28181_Enable"); |
| | | //#todo GB28181 |
| | | if (m_GB28181_Enable) { |
| | | auto lst = m_lDBTool->searchCamDevTableByType(1); |
| | | Record_Config lst_dev = m_lDBTool->searchConfigTableWithinServerInfo(); |
| | | |
| | | //设置视频的最长和最短时间间隔 |
| | | appPref.setIntData("n_cut_max_duration", lst_dev.n_cut_max_duration); |
| | | appPref.setIntData("n_cut_min_duration", lst_dev.n_cut_min_duration); |
| | | if (lst.size() > 0) { |
| | | |
| | | for (auto item : lst) |
| | | { |
| | | std::string t_camIdex = item.str_cam_dev_id.toStdString(); |
| | | std::string rtsp_url = rtspAddrBuild(item.str_ip.toStdString(), 554, item.str_username.toStdString(), |
| | | item.str_password.toStdString(), item.str_brand.toStdString()); |
| | | INFO("cam add is " << item.str_addr.toStdString()); |
| | | |
| | | addCamera(t_camIdex, rtsp_url); |
| | | |
| | | //设置视频的最长和最短时间间隔 |
| | | appPref.setIntData("n_cut_max_duration", lst_dev.n_cut_max_duration); |
| | | appPref.setIntData("n_cut_min_duration", lst_dev.n_cut_min_duration); |
| | | if (lst.size() > 0) { |
| | | for (auto item : lst) { |
| | | std::string t_camIdex = item.str_cam_dev_id.toStdString(); |
| | | std::string rtsp_url = "GB28181"; |
| | | // rtspAddrBuild(item.str_ip.toStdString(), 554, item.str_username.toStdString(), |
| | | // item.str_password.toStdString(), item.str_brand.toStdString()); |
| | | INFO("cam add is " << item.str_addr.toStdString()); |
| | | addCamera(t_camIdex, rtsp_url); |
| | | } |
| | | } else { |
| | | ERR("searchCamDevTableAll size is 0"); |
| | | } |
| | | } else { |
| | | ERR("searchCamDevTableAll size is 0"); |
| | | } |
| | | |
| | | } else { |
| | | auto lst = m_lDBTool->searchCamDevTableAll(); |
| | | Record_Config lst_dev = m_lDBTool->searchConfigTableWithinServerInfo(); |
| | | |
| | | //设置视频的最长和最短时间间隔 |
| | | appPref.setIntData("n_cut_max_duration", lst_dev.n_cut_max_duration); |
| | | appPref.setIntData("n_cut_min_duration", lst_dev.n_cut_min_duration); |
| | | if (lst.size() > 0) { |
| | | int startCamNO = appPref.getIntData("CamStart") * appPref.getIntData("CamStep"); |
| | | int CamCount = appPref.getIntData("CamStep"); |
| | | INFO("StartCamNO: " << startCamNO << " CamStep: " << CamCount); |
| | | auto itor = lst.begin(); |
| | | |
| | | if (startCamNO >= lst.size()) { |
| | | ERR("startCamNO > lst.size()"); |
| | | return; |
| | | } |
| | | |
| | | for (int i = 0; i < startCamNO; i++) { |
| | | |
| | | std::string t_camIdex = itor->str_cam_dev_id.toStdString(); |
| | | std::string rtsp_url = rtspAddrBuild(itor->str_ip.toStdString(), 554, itor->str_username.toStdString(), |
| | | itor->str_password.toStdString(), itor->str_brand.toStdString()); |
| | | INFO("JumpCam: " << t_camIdex << " URL: " << rtsp_url); |
| | | itor++; |
| | | } |
| | | |
| | | for (int i = 0; i < CamCount; i++) { |
| | | if (itor == lst.end()) { |
| | | ERR("itor == lst.end()"); |
| | | return; |
| | | } |
| | | std::string t_camIdex = itor->str_cam_dev_id.toStdString(); |
| | | std::string rtsp_url = rtspAddrBuild(itor->str_ip.toStdString(), 554, itor->str_username.toStdString(), |
| | | itor->str_password.toStdString(), itor->str_brand.toStdString()); |
| | | INFO("cam add is " << itor->str_addr.toStdString()); |
| | | |
| | | addCamera(t_camIdex, rtsp_url); |
| | | itor++; |
| | | } |
| | | |
| | | // for (auto item : lst) |
| | | // { |
| | | // std::string t_camIdex = item.str_cam_dev_id.toStdString(); |
| | | // std::string rtsp_url = rtspAddrBuild(item.str_ip.toStdString(), 554, item.str_username.toStdString(), |
| | | // item.str_password.toStdString(), item.str_brand.toStdString()); |
| | | // INFO("cam add is " << item.str_addr.toStdString()); |
| | | // |
| | | // addCamera(t_camIdex, rtsp_url); |
| | | // |
| | | // } |
| | | } else { |
| | | ERR("searchCamDevTableAll size is 0"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | RtspAnalysManager::~RtspAnalysManager() { |
| | | INFO("Program Exit"); |
| | | for (auto controller: m_controllers) { |
| | | INFO("Delete Controller: " << controller.first); |
| | | delete controller.second; |
| | | } |
| | | m_controllers.clear(); |
| | | } |
| | | |
| | | int RtspAnalysManager::addCamera(const std::string &index, const std::string& rtsp) { |
| | | auto lst = m_lDBTool->searchCamDevTableAll(); |
| | | Record_Config lst_dev = m_lDBTool->searchConfigTableWithinServerInfo(); |
| | | |
| | | if (m_controllers.find(index) == m_controllers.end()) { |
| | | if (m_currentCount >= m_maxCount) { |
| | | ERR("addCamera faild, camera's num is full!") |
| | | return -1; |
| | | } |
| | | DBG("RTSP: "<<rtsp<<" INDEX:"<<index); |
| | | m_imgRedisControllers[index] = new RtspImageRedisElement(index); |
| | | m_imgRedisControllers[index]->start(); |
| | | |
| | | m_controllers[index] = new RtspCaptureElement(rtsp,index, 25,3000,0,this); |
| | | m_controllers[index]->start(); |
| | | m_controllers[index]->SetVideoMinMaxSeconds(lst_dev.n_cut_min_duration,lst_dev.n_cut_max_duration); |
| | | m_currentCount++; |
| | | return 0; |
| | | |
| | | } else { |
| | | removeCamera(index); |
| | | DBG("removeCamera " << index) |
| | | return addCamera(rtsp, index); |
| | | for (auto controller: m_controllers_videoCapElem) { |
| | | INFO("Delete Controller: " << controller.first); |
| | | delete controller.second; |
| | | } |
| | | m_controllers_videoCapElem.clear(); |
| | | } |
| | | |
| | | int RtspAnalysManager::removeCamera(const std::string &index) { |
| | | if (m_controllers.find(index) == m_controllers.end())return -1; |
| | | auto controller = m_controllers[index]; |
| | | controller->stop(); |
| | | controller->wait(); |
| | | delete controller; |
| | | m_controllers.erase(index); |
| | | m_currentCount--; |
| | | /** |
| | | * 根据摄像机ID和RTSP路径,增加摄像机 |
| | | * 对于每个摄像机有一个 RtspImageRedisElement 对象用于保存图片到Redis |
| | | * 有一个 RtspCaptureElement 用户录取和保存视频 |
| | | * @param index 摄像机id |
| | | * @param rtsp 上面的摄像机对应的视频路径 |
| | | * @return -1 添加失败,0 添加成功 |
| | | */ |
| | | int RtspAnalysManager::addCamera(const std::string &index, const std::string &rtsp) { |
| | | |
| | | if (m_GB28181_Enable) { |
| | | //#todo |
| | | // why search lst ? |
| | | // auto lst = m_lDBTool->searchCamDevTableAll(); |
| | | Record_Config lst_dev = m_lDBTool->searchConfigTableWithinServerInfo(); |
| | | |
| | | //#todo end |
| | | if (m_controllers_videoCapElem.find(index) == m_controllers_videoCapElem.end()) { |
| | | INFO("MYH DEBUG HERE"); |
| | | if (m_currentCount >= m_maxCount) { |
| | | ERR("addCamera faild, camera's num is full!") |
| | | return -1; |
| | | } |
| | | INFO("RTSP: " << rtsp << " INDEX:" << index); |
| | | |
| | | m_imgRedisCRwLock.wrlock(); |
| | | m_imgRedisControllers[index] = new RtspImageRedisElement(index); |
| | | m_imgRedisControllers[index]->start(); |
| | | m_imgRedisCRwLock.unlock(); |
| | | |
| | | //VideoCaptureElementWithRtp(std::string &chanPubID, int fps, int streamTransType, int gpuIdx = -1) |
| | | // m_controllers_videoCapElem[index] = new BASICGB28181::VideoCaptureElementWithRtp( |
| | | // const_cast<string &>(index), |
| | | // 25, 0, 0, this); |
| | | |
| | | m_controllers_videoCapElem[index] = new PushStreamAppPipeController(const_cast<string &>(index), |
| | | 25, 0, 0, this); |
| | | m_controllers_videoCapElem[index]->SetVideoMinMaxSeconds(lst_dev.n_cut_min_duration, |
| | | lst_dev.n_cut_max_duration); |
| | | m_controllers_videoCapElem[index]->start(); |
| | | m_currentCount++; |
| | | return 0; |
| | | |
| | | } else { |
| | | removeCamera(index); |
| | | INFO("removeCamera " << index); |
| | | //DBG("removeCamera " << index); |
| | | return addCamera(index, rtsp); |
| | | } |
| | | } else { |
| | | // why search lst ? |
| | | // auto lst = m_lDBTool->searchCamDevTableAll(); |
| | | Record_Config lst_dev = m_lDBTool->searchConfigTableWithinServerInfo(); |
| | | if (m_controllers.find(index) == m_controllers.end()) { |
| | | INFO("MYH DEBUG HERE"); |
| | | if (m_currentCount >= m_maxCount) { |
| | | ERR("addCamera faild, camera's num is full!") |
| | | return -1; |
| | | } |
| | | INFO("RTSP: " << rtsp << " INDEX:" << index); |
| | | m_imgRedisControllers[index] = new RtspImageRedisElement(index); |
| | | m_imgRedisControllers[index]->start(); |
| | | |
| | | m_controllers[index] = new RtspCaptureElement(rtsp, index, 25, 3000, 0, this); |
| | | m_controllers[index]->SetVideoMinMaxSeconds(lst_dev.n_cut_min_duration, lst_dev.n_cut_max_duration); |
| | | m_controllers[index]->start(); |
| | | m_currentCount++; |
| | | return 0; |
| | | |
| | | } else { |
| | | removeCamera(index); |
| | | INFO("removeCamera " << index); |
| | | //DBG("removeCamera " << index); |
| | | return addCamera(index, rtsp); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 根据CamID移除摄像机,同时停止视频的抓取和保存图片到Redis |
| | | * @param index 摄像机ID |
| | | * @return 总是0 |
| | | */ |
| | | int RtspAnalysManager::removeCamera(const std::string &index) { |
| | | INFO("MYH DEBUG HERE"); |
| | | if (m_GB28181_Enable) { |
| | | if (m_controllers_videoCapElem.find(index) == m_controllers_videoCapElem.end())return -1; |
| | | auto controller = m_controllers_videoCapElem[index]; |
| | | controller->stop(); |
| | | controller->wait(); |
| | | delete controller; |
| | | m_controllers_videoCapElem.erase(index); |
| | | m_currentCount--; |
| | | } else { |
| | | if (m_controllers.find(index) == m_controllers.end())return -1; |
| | | auto controller = m_controllers[index]; |
| | | controller->stop(); |
| | | controller->wait(); |
| | | delete controller; |
| | | m_controllers.erase(index); |
| | | m_currentCount--; |
| | | } |
| | | |
| | | m_imgRedisCRwLock.wrlock(); |
| | | auto imgRedis = m_imgRedisControllers[index]; |
| | | imgRedis->stop(); |
| | | imgRedis->wait(); |
| | | delete imgRedis; |
| | | m_imgRedisControllers.erase(index); |
| | | m_imgRedisCRwLock.unlock(); |
| | | INFO("MYH DEBUG HERE"); |
| | | return 0; |
| | | } |
| | | |
| | | /** |
| | | * 移除所有的摄像机 |
| | | * @return |
| | | */ |
| | | int RtspAnalysManager::removeAll() { |
| | | for (auto controller: m_controllers) { |
| | | controller.second->stop(); |
| | | } |
| | | for (auto controller: m_controllers) { |
| | | controller.second->wait(); |
| | | delete controller.second; |
| | | } |
| | | m_controllers.clear(); |
| | | INFO("MYH DEBUG HERE"); |
| | | |
| | | if (m_GB28181_Enable) { |
| | | for (auto controller: m_controllers_videoCapElem) { |
| | | controller.second->stop(); |
| | | } |
| | | for (auto controller: m_controllers_videoCapElem) { |
| | | controller.second->wait(); |
| | | delete controller.second; |
| | | } |
| | | m_controllers_videoCapElem.clear(); |
| | | } else { |
| | | for (auto controller: m_controllers) { |
| | | controller.second->stop(); |
| | | } |
| | | for (auto controller: m_controllers) { |
| | | controller.second->wait(); |
| | | delete controller.second; |
| | | } |
| | | m_controllers.clear(); |
| | | } |
| | | INFO("MYH DEBUG HERE"); |
| | | |
| | | m_imgRedisCRwLock.wrlock(); |
| | | for (auto controller: m_imgRedisControllers) { |
| | | controller.second->stop(); |
| | | } |
| | |
| | | controller.second->wait(); |
| | | delete controller.second; |
| | | } |
| | | m_controllers.clear(); |
| | | m_imgRedisControllers.clear(); |
| | | m_imgRedisCRwLock.unlock(); |
| | | |
| | | |
| | | m_currentCount = 0; |
| | | INFO("MYH DEBUG HERE"); |
| | | return 0; |
| | | } |
| | | |
| | | //获取最大的摄像机数量 |
| | | int RtspAnalysManager::getMaxCamCount() { |
| | | return m_maxCount; |
| | | } |
| | | |
| | | //获取当前的摄像机数量 |
| | | int RtspAnalysManager::getCurrentCamCount() { |
| | | return m_currentCount; |
| | | } |
| | | ::std::string RtspAnalysManager::recordVideo(const ::std::string& name, const ::Ice::Current&) |
| | | { |
| | | INFO("Record Video For: "<<name); |
| | | ImageName_s_t nameSt=ImageName_s_t::fromString(name); |
| | | if(nameSt.Valid()) |
| | | { |
| | | auto pCaptureElem = m_controllers.find(nameSt.m_camId); |
| | | if(pCaptureElem!= m_controllers.end()) |
| | | { |
| | | pCaptureElem->second->SaveVideo(name); |
| | | } else{ |
| | | ERR("Can not Find CamId "<<nameSt.m_camId); |
| | | |
| | | //录取视频的RPC的接口函数 |
| | | ::std::string RtspAnalysManager::recordVideo(const ::std::string &name, const ::Ice::Current &) { |
| | | INFO("Record Video For: " << name); |
| | | ImageName_s_t nameSt = ImageName_s_t::fromString(name); |
| | | if (nameSt.Valid()) { |
| | | if (m_GB28181_Enable) { |
| | | auto pCaptureElem = m_controllers_videoCapElem.find(nameSt.m_camId); |
| | | if (pCaptureElem != m_controllers_videoCapElem.end()) { |
| | | //#todo ~~~~~~!!!!!!! 这里有bug 需要放开调试 |
| | | pCaptureElem->second->SaveVideo(name); |
| | | } else { |
| | | ERR("Can not Find CamId " << nameSt.m_camId); |
| | | } |
| | | } else { |
| | | auto pCaptureElem = m_controllers.find(nameSt.m_camId); |
| | | if (pCaptureElem != m_controllers.end()) { |
| | | pCaptureElem->second->SaveVideo(name); |
| | | } else { |
| | | ERR("Can not Find CamId " << nameSt.m_camId); |
| | | } |
| | | } |
| | | } |
| | | else{ |
| | | ERR("Record Video Failed:Name Not Valid Name: "<<name); |
| | | } else { |
| | | ERR("Record Video Failed:Name Not Valid Name: " << name); |
| | | } |
| | | return name; |
| | | } |
| | | |
| | | //保存视频到RtspImageRedis的队列,由RtspCaptureElement调用 |
| | | bool RtspAnalysManager::SaveImageToRedis(const std::string &camId, const std::string &imageName, const cv::Mat &img) { |
| | | INFO("MYH DEBUG HERE " << camId); |
| | | |
| | | bool RtspAnalysManager::SaveImageToRedis(const std::string& camId,const std::string& imageName,const cv::Mat& img) |
| | | { |
| | | // m_imgRedisCRwLock.rdlock(); |
| | | auto item = m_imgRedisControllers.find(camId); |
| | | if(item!=m_imgRedisControllers.end()) |
| | | { |
| | | item->second->SaveImage(imageName,img); |
| | | if (item != m_imgRedisControllers.end()) { |
| | | INFO("Save Succeed Cam: " << camId << " ImageKey: " << imageName); |
| | | item->second->SaveImage(imageName, img); |
| | | } else { |
| | | ERR("Save Failed Cam: " << camId << " ImageKey: " << imageName); |
| | | } |
| | | // m_imgRedisCRwLock.unlock(); |
| | | INFO("MYH DEBUG HERE"); |
| | | return true; |
| | | } |
| | | |