wangrong
2025-03-19 291afb9c81aae1d15d9e3edcea618d0a2e1fc5a6
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
 
#include "vp_node.h"
 
namespace vp_nodes {
    
    vp_node::vp_node(std::string node_name): node_name(node_name) {
    }
    
    vp_node::~vp_node() {
 
    }
 
    // there is only one thread poping data from the in_queue, we don't use lock here when poping.
    // there is only one thread pushing data to the out_queue, we don't use lock here when pushing.
    void vp_node::handle_run() {
        // cache for batch handling if need
        std::vector<std::shared_ptr<vp_objects::vp_frame_meta>> frame_meta_batch_cache;
        while (alive) {
            // wait for producer, make sure in_queue is not empty.
            this->in_queue_semaphore.wait();
 
            VP_DEBUG(vp_utils::string_format("[%s] before handling meta, in_queue.size()==>%d", node_name.c_str(), in_queue.size()));
            auto in_meta = this->in_queue.front();
            
            // dead flag
            if (in_meta == nullptr) {
                continue;
            }
            
            // handling hooker activated if need
            invoke_meta_handling_hooker(node_name, in_queue.size(), in_meta);
 
            std::shared_ptr<vp_objects::vp_meta> out_meta;
            auto batch_complete = false;
 
            // call handlers
            if (in_meta->meta_type == vp_objects::vp_meta_type::CONTROL) {
                auto meta_2_handle = std::dynamic_pointer_cast<vp_objects::vp_control_meta>(in_meta);
                out_meta = this->handle_control_meta(meta_2_handle);
            }
            else if (in_meta->meta_type == vp_objects::vp_meta_type::FRAME) {    
                auto meta_2_handle = std::dynamic_pointer_cast<vp_objects::vp_frame_meta>(in_meta);
                // one by one
                if (frame_meta_handle_batch == 1) {                    
                    out_meta = this->handle_frame_meta(meta_2_handle);
                } 
                else {
                    // batch by batch
                    frame_meta_batch_cache.push_back(meta_2_handle);
                    if (frame_meta_batch_cache.size() >= frame_meta_handle_batch) {
                        // cache complete
                        this->handle_frame_meta(frame_meta_batch_cache);
                        batch_complete = true;
                    } 
                    else {
                        // cache not complete, do nothing
                        VP_DEBUG(vp_utils::string_format("[%s] handle meta with batch, frame_meta_batch_cache.size()==>%d", node_name.c_str(), frame_meta_batch_cache.size()));
                    }
                }
            }
            else {
                throw "invalid meta type!";
            }
            this->in_queue.pop();
            VP_DEBUG(vp_utils::string_format("[%s] after handling meta, in_queue.size()==>%d", node_name.c_str(), in_queue.size()));
 
            // one by one mode
            // return nullptr means do not push it to next nodes(such as in des nodes).
            if (out_meta != nullptr && node_type() != vp_node_type::DES) {
                VP_DEBUG(vp_utils::string_format("[%s] before handling meta, out_queue.size()==>%d", node_name.c_str(), out_queue.size()));
                this->out_queue.push(out_meta);
 
                // handled hooker activated if need
                invoke_meta_handled_hooker(node_name, out_queue.size(), out_meta);
 
                // notify consumer of out_queue
                this->out_queue_semaphore.signal();
                VP_DEBUG(vp_utils::string_format("[%s] after handling meta, out_queue.size()==>%d", node_name.c_str(), out_queue.size()));
            }
 
            // batch by batch mode
            if (batch_complete && node_type() != vp_node_type::DES) {
                // push to out_queue one by one
                for (auto& i: frame_meta_batch_cache) {
                    VP_DEBUG(vp_utils::string_format("[%s] before handling meta, out_queue.size()==>%d", node_name.c_str(), out_queue.size()));
                    this->out_queue.push(i);
 
                    // handled hooker activated if need
                    invoke_meta_handled_hooker(node_name, out_queue.size(), i);
 
                    // notify consumer of out_queue
                    this->out_queue_semaphore.signal();
                    VP_DEBUG(vp_utils::string_format("[%s] after handling meta, out_queue.size()==>%d", node_name.c_str(), out_queue.size()));
                }
                // clean cache for the next batch
                frame_meta_batch_cache.clear();
            }
        }
        // send dead flag for dispatch_thread
        this->out_queue.push(nullptr);
        this->out_queue_semaphore.signal();
    }
 
    // there is only one thread poping from the out_queue, we don't use lock here when poping.
    void vp_node::dispatch_run() {
        while (alive) {
            // wait for producer, make sure out_queue is not empty.
            this->out_queue_semaphore.wait();
 
            VP_DEBUG(vp_utils::string_format("[%s] before dispatching meta, out_queue.size()==>%d", node_name.c_str(), out_queue.size()));
            auto out_meta = this->out_queue.front();
            // dead flag
            if (out_meta == nullptr) {
                continue;
            }
 
            // leaving hooker activated if need
            invoke_meta_leaving_hooker(node_name, out_queue.size(), out_meta);
 
            // do something..
            this->push_meta(out_meta);
            this->out_queue.pop();
            VP_DEBUG(vp_utils::string_format("[%s] after dispatching meta, out_queue.size()==>%d", node_name.c_str(), out_queue.size()));
        }
    }
 
    std::shared_ptr<vp_objects::vp_meta> vp_node::handle_frame_meta(std::shared_ptr<vp_objects::vp_frame_meta> meta) {
        return meta;
    }
 
    std::shared_ptr<vp_objects::vp_meta> vp_node::handle_control_meta(std::shared_ptr<vp_objects::vp_control_meta> meta) {
        return meta;
    }
 
    void vp_node::handle_frame_meta(const std::vector<std::shared_ptr<vp_objects::vp_frame_meta>>& meta_with_batch) {
        
    }
 
    void vp_node::meta_flow(std::shared_ptr<vp_objects::vp_meta> meta) {
        if (meta == nullptr) {
            return;
        }
 
        std::lock_guard<std::mutex> guard(this->in_queue_lock);
        VP_DEBUG(vp_utils::string_format("[%s] before meta flow, in_queue.size()==>%d", node_name.c_str(), in_queue.size()));
        this->in_queue.push(meta);
 
        // arriving hooker activated if need
        invoke_meta_arriving_hooker(node_name, in_queue.size(), meta);
        
        // notify consumer of in_queue
        this->in_queue_semaphore.signal();
        VP_DEBUG(vp_utils::string_format("[%s] after meta flow, in_queue.size()==>%d", node_name.c_str(), in_queue.size()));
    }
 
    void vp_node::detach() {
        for(auto i : this->pre_nodes) {
            i->remove_subscriber(shared_from_this());
        }
        this->pre_nodes.clear();
    }
 
    void vp_node::detach_from(std::vector<std::string> pre_node_names) {
        for (auto i = this->pre_nodes.begin(); i != this->pre_nodes.end();) {
            if (std::find(pre_node_names.begin(), pre_node_names.end(), (*i)->node_name) != pre_node_names.end()) {
                (*i)->remove_subscriber(shared_from_this());
                i = this->pre_nodes.erase(i);
            }
            else {
                i++;
            }
        }
    }
 
    void vp_node::detach_recursively() {
        detach();
        auto nodes = next_nodes();
        for (auto& n: nodes) {
            n->detach_recursively();
        }
    }
 
    void vp_node::attach_to(std::vector<std::shared_ptr<vp_node>> pre_nodes) {
        // can not attach src node to any previous nodes
        if (this->node_type() == vp_node_type::SRC) {
            throw vp_excepts::vp_invalid_calling_error("SRC nodes must not have any previous nodes!");
        }
        // can not attach any nodes to des node
        for(auto i : pre_nodes) {
            if (i->node_type() == vp_node_type::DES) {
                throw vp_excepts::vp_invalid_calling_error("DES nodes must not have any next nodes!");
            }
            i->add_subscriber(shared_from_this());
            this->pre_nodes.push_back(i);
        }
    }
 
    void vp_node::initialized() {
        // start threads since all resources have been initialized
        this->handle_thread = std::thread(&vp_node::handle_run, this);
        this->dispatch_thread = std::thread(&vp_node::dispatch_run, this);
    }
 
    void vp_node::deinitialized() {
        // send dead flag
        alive = false;
        {
            std::lock_guard<std::mutex> guard(this->in_queue_lock);
            this->in_queue.push(nullptr);
            this->in_queue_semaphore.signal();
        }
        // wait for threads exits in vp_node
        if (handle_thread.joinable()) {
            handle_thread.join();
        }
        if (dispatch_thread.joinable()) {
            dispatch_thread.join();
        }
    }
 
    vp_node_type vp_node::node_type() {
        // return vp_node_type::MID by default
        // need override in child class
        return vp_node_type::MID;
    }
 
    std::vector<std::shared_ptr<vp_node>> vp_node::next_nodes() {
        std::vector<std::shared_ptr<vp_node>> next_nodes;
        std::lock_guard<std::mutex> guard(this->subscribers_lock);
        for(auto & i: this->subscribers) {
            next_nodes.push_back(std::dynamic_pointer_cast<vp_node>(i));
        }
        return next_nodes;
    }
 
    std::string vp_node::to_string() {
        // return node_name by default
        return node_name;
    }
 
    void vp_node::pendding_meta(std::shared_ptr<vp_objects::vp_meta> meta) {
        this->out_queue.push(meta);
        // handled hooker activated if need
        invoke_meta_handled_hooker(node_name, out_queue.size(), meta);        
        // notify consumer of out_queue
        this->out_queue_semaphore.signal();
    }
}