派生自 development/c++

pansen
2019-03-07 d3b7bbe7102cd089680a828f5d8f6402c8cf6342
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#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();