#include "AppController.h"
|
#include <basic/util/file/FileUtil.h>
|
|
#define VECSIZE 1
|
|
AppController::AppController(std::string path, int interval) : TimerElement(interval), m_path(path),
|
func_cond_mutex(PTHREAD_MUTEX_INITIALIZER),
|
func_cond(PTHREAD_COND_INITIALIZER) {
|
file_filter_type filter = [](const char *dirName, const char *dirent) {
|
return (strstr(dirent, ".mp4") != nullptr);// add format
|
};
|
|
std::string tmp = path;
|
m_pathOut = tmp.replace(tmp.begin() + tmp.find("/load"), tmp.begin() + tmp.find("/load") + 5, "/cut");
|
// DBG("m_pathOut is " << m_pathOut);
|
|
std::vector<std::string> vec = for_each_file(m_path, filter);
|
|
INFO(path << " vec size : " << vec.size());
|
|
for (int i = 0; i < vec.size(); ++i) {
|
m_que.push(vec[i]);
|
}
|
|
if (vec.size() >= VECSIZE) {
|
m_vec.resize(VECSIZE);
|
} else {
|
m_vec.resize(vec.size());
|
}
|
|
for (int i = 0; i < m_vec.size(); ++i) {
|
m_vec[i] = nullptr;
|
}
|
}
|
|
RapidAppPipeController *AppController::addController() {
|
RapidAppPipeController *p = nullptr;
|
std::lock_guard<std::mutex> lg(mtx);
|
std::string strPath;
|
Json::Value value;
|
if (!m_que.empty()) {
|
strPath = m_que.front();
|
value["rtsp"] = strPath;
|
m_que.pop();
|
p = new RapidAppPipeController(m_nIndex, value, m_pathOut);
|
p->start();
|
m_nIndex++;
|
}
|
return p;
|
}
|
|
std::string AppController::startController() {
|
start();
|
pthread_mutex_lock(&func_cond_mutex);
|
// INFO("before wait" << m_path);
|
pthread_cond_wait(&func_cond, &func_cond_mutex);
|
int m_t_size = m_vec.size();
|
for (int i = 0; i < m_vec.size(); ++i) {
|
if (m_vec[i] == nullptr) {
|
continue;
|
}
|
while (m_vec[i]->getIsRunning()) {
|
usleep(40000);
|
}
|
m_vec[i]->stop();
|
m_vec[i]->wait();
|
delete m_vec[i];
|
m_vec[i] = nullptr;
|
m_t_size--;
|
}
|
// INFO("after wait" << m_path);
|
pthread_mutex_unlock(&func_cond_mutex);
|
wait();
|
// DBG("cut ok ;m_pathOut is " << m_pathOut);
|
return m_pathOut;
|
}
|
|
void AppController::timerFunc() {
|
if (m_que.empty()) {
|
// INFO("before signal " << m_path);
|
stop();
|
pthread_cond_signal(&func_cond);
|
// INFO("after signal" << m_path);
|
return;
|
}
|
|
for (int i = 0; i < m_vec.size(); ++i) {
|
if (m_vec[i] == nullptr) {
|
m_vec[i] = addController();
|
continue;
|
}
|
|
if (!m_vec[i]->getIsRunning()) {
|
|
m_vec[i]->stop();
|
m_vec[i]->wait();
|
|
delete m_vec[i];
|
// std::string cmd = "rm -rf \"";
|
// cmd.append(m_vec[i]->getPath()).append("\" & ");
|
// system(cmd.c_str());
|
m_vec[i] = nullptr;
|
|
m_vec[i] = addController();
|
continue;
|
}
|
}
|
}
|
|
//if (!m_que.empty())
|
//{
|
// strPath = m_que.pop();
|
//}else
|
//{
|
// return;
|
//}
|
//value["rtsp"] = strPath;
|
|
//std::string strPath;
|
//Json::Value value;
|
|
//m_vec[i] = new RapidAppPipeController(m_nIndex,strPath);
|
//m_vec[i]->start();
|
|
//m_vec[i]->stop();
|
//m_vec[i]->wait();
|
|
//delete m_vec[i];
|
//m_vec[i] = nullptr;
|
|
//m_vec[i] = new RapidAppPipeController(m_nIndex,strPath);
|
//m_vec[i]->start();
|