wangrong
2025-01-18 8b0e31121a9a29b92a41fc575138a2e9b4c12d8e
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
 
#include "vp_image_record_task.h"
 
namespace vp_nodes {
    vp_image_record_task::vp_image_record_task(int channel_index,
                        std::string file_name_without_ext,
                        std::string save_dir,
                        bool auto_sub_dir,
                        bool osd,
                        vp_objects::vp_size resolution_w_h,
                        std::string host_node_name,
                        bool auto_start):
                        vp_record_task(channel_index, file_name_without_ext, save_dir, auto_sub_dir, resolution_w_h, osd, host_node_name) {
        // start automatically when initializing
        if (auto_start) {
            start();
        }   
    }
 
    vp_image_record_task::~vp_image_record_task() {
        stop_task();
    }
 
    void vp_image_record_task::record_task_run() {
        /* Below Code Run In A Separate Thread! */
        // get valid path
        auto full_record_path = get_full_record_path();
        
        while (status == vp_record_task_status::STARTED) {
             // it is a consumer
            cache_semaphore.wait();
 
            auto frame_to_record = frames_to_record.front();
            if (frame_to_record == nullptr) {
                //dead flag
                continue;
            }
 
            cv::Mat frame_data;
            // preprocess, vp_frame_meta -> cv::Mat
            preprocess(frame_to_record, frame_data);
            frames_to_record.pop_front();
            
            // write to disk
            cv::imwrite(full_record_path, frame_data);
            VP_DEBUG(vp_utils::string_format("[%s] [record] already written frame for `%s`", host_node_name.c_str(), get_full_record_path().c_str()));
            /* for image recording, just saving only 1 frame and then complete
             * refer to vp_video_record_task
             */
 
            vp_record_info record_info;
            record_info.record_type = vp_record_type::IMAGE;
            notify_task_complete(record_info);
            // loop end
        }
    }
 
    std::string vp_image_record_task::get_file_ext() {
        // save as jpg
        return ".jpg";
    }
}