FFRecoderTaskManager.cpp 14.1 KB
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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
#include "FFRecoderTaskManager.h"
#include <chrono>

struct RecodeThreadParam {
    FFRecoderTaskManager* _this;
    RecodeParam param;
};

static long get_cur_time() {

	chrono::time_point<chrono::system_clock, chrono::milliseconds> tpMicro
		= chrono::time_point_cast<chrono::milliseconds>(chrono::system_clock::now());

	return tpMicro.time_since_epoch().count();
}

static string get_cur_datetime()
{
	using namespace std;
	auto t = chrono::system_clock::to_time_t(chrono::system_clock::now());

	char buffer[40];
	strftime(buffer, 80, "%Y_%m_%d_%H_%M_%S", std::localtime(&t));
	buffer[20] = '\0';
	return buffer;
}

static bool is_key_frame(AVPacket *pkt) {  
    return (pkt->flags & AV_PKT_FLAG_KEY) != 0;  
}

FFRecoderTaskManager::FFRecoderTaskManager(){
    m_recoder_thread = nullptr;
    m_cache_thread = nullptr;

    m_bExit = false;
    m_bExitRecoderThread = false;
}

FFRecoderTaskManager::~FFRecoderTaskManager(){

    close();

    LOG_DEBUG("~FFRecoderTaskManager()");
}

bool FFRecoderTaskManager::init(AVStream* stream, AVCodecContext* avctx){
    if(stream == nullptr || avctx == nullptr) {
        return false;
    }

    m_time_base = stream->time_base;
    m_avctx = avctx;
    m_inStream = stream;

    m_recoder_thread = new std::thread(
        [](void* arg) {
            FFRecoderTaskManager* _this=(FFRecoderTaskManager*)arg;
            if(_this != nullptr) {
                _this->recode_thread();
            }else{
                LOG_ERROR("recode 线程启动失败 !");
            }
            return (void*)0;
        }, this);

    return true;
}

bool FFRecoderTaskManager::init2(int w, int h, int fps, int bit_rate) {
    m_width = w;
    m_height = h;
    m_bit_rate = bit_rate;

    if (fps > 1) {
        m_fps = fps;
    } else {
        m_fps = 25;
    }

    m_cache_thread = new std::thread(
        [](void* arg) {
            FFRecoderTaskManager* _this=(FFRecoderTaskManager*)arg;
            if(_this != nullptr) {
                _this->pkt_cache_thread();
            }else{
                LOG_ERROR("recode 线程启动失败 !");
            }
            return (void*)0;
        }, this);

    return true;
}

static AVPacket* packet_clone(AVPacket* pkt) {
    AVPacket *new_pkt = av_packet_alloc();
	av_init_packet( new_pkt );
    av_new_packet(new_pkt, pkt->size);
    memcpy(new_pkt->data, pkt->data, pkt->size);
    new_pkt->size = pkt->size;
    // new_pkt->pts = pkt->pts;
    // new_pkt->dts = pkt->dts;
    // new_pkt->stream_index = pkt->stream_index;
    // new_pkt->duration = pkt->duration;
    // new_pkt->pos = pkt->pos;
    // new_pkt->flags = pkt->flags;
    // av_copy_packet_side_data(new_pkt, pkt);
    return new_pkt;
}

void FFRecoderTaskManager::cache_pkt(AVPacket* pkt, long long frame_nb){
    if(m_bExit) {
        // 任务退出了就不再缓存数据了
        return;
    }

    std::lock_guard<std::mutex> l_pkt(m_pkt_list_mtx);

    // 考虑到一个AVPacket中的数据并不很大,为减少与解码模块的耦合度,方便管理,这里做一个clone
    AVPacket *new_pkt = packet_clone(pkt);

    DataPacket* newDataPkt = new DataPacket();
    newDataPkt->pkt = new_pkt;
    newDataPkt->frame_nb = frame_nb;

    if(is_key_frame(pkt)){
        // 越来越大的值
        newDataPkt->isKeyFrame = true;
        LOG_INFO("key frame_nb: {}", frame_nb);
    } else {
        newDataPkt->isKeyFrame = false;
    }
    
    std::lock_guard<std::mutex> l_info(m_pkt_list_short_mtx);
    m_pkt_list_short.push_back(newDataPkt);
}

void FFRecoderTaskManager::save_intask_recoderinfo(RecoderInfo info) {
    std::lock_guard<std::mutex> l(m_recoderinfo_list_mtx);
    if(m_recoderinfo_list.size() <= 0) {
        m_recoderinfo_list.push_back(info);
         return;
    }

    for(auto it = m_recoderinfo_list.begin(); it != m_recoderinfo_list.end(); it++) {
        if(it->frame_nb > info.frame_nb) {
            m_recoderinfo_list.insert(it, info);
            return;
        }
    }
    
    // 新 frame_nb 比所有的都大
    m_recoderinfo_list.push_back(info);
}

list<DataPacket*>::iterator FFRecoderTaskManager::getStartIterator(unsigned long long frame_nb){
    std::lock_guard<std::mutex> l(m_pkt_list_mtx);

    auto it_first = m_pkt_list.begin();

    long long start_frame_nb = (long long)(frame_nb - 375);
    if(start_frame_nb <= 0) {
        return it_first;
    }

    auto it_second = m_pkt_list.begin();
    for(;it_second != m_pkt_list.end(); it_second++) {
        DataPacket* dataPkt = *it_second;
        if(dataPkt->isKeyFrame) {
            it_first = it_second;
        }
        if(start_frame_nb >= (*it_first)->frame_nb && start_frame_nb <= (*it_second)->frame_nb) {
            return it_first;
        }
    }

    return m_pkt_list.begin();
}

void FFRecoderTaskManager::create_recode_task(RecoderInfo& recoderInfo) {
    if(m_bExit) {
        // 任务退出了就不再接收录制任务
        return;
    }
    save_intask_recoderinfo(recoderInfo);
}

void FFRecoderTaskManager::recode_thread() {
    LOG_INFO("recode_thread start...");
    while(true) {
        if(m_bExit) {
            break;
        }

        m_recoderinfo_list_mtx.lock();
        if(m_recoderinfo_list.size() <= 0){
            m_recoderinfo_list_mtx.unlock();
            std::this_thread::sleep_for(std::chrono::milliseconds(3));
            continue;
        }

        auto it_param = m_recoderinfo_list.begin();
        RecoderInfo recoderinfo = *it_param;
        m_recoderinfo_list_mtx.unlock();

        do
        {
            auto it_data = getStartIterator(recoderinfo.frame_nb);
            if(it_data == m_pkt_list.end()) {
                std::this_thread::sleep_for(std::chrono::milliseconds(3));
                break;
            }

            LOG_INFO("start frame_nb: {}", (*it_data)->frame_nb);

            m_pkt_list_mtx.lock();
            auto it = m_pkt_list.begin();
            while (it != it_data) {
                DataPacket* dataPkt = m_pkt_list.front();
                delete dataPkt;
                dataPkt = nullptr;
                m_pkt_list.pop_front();
                it = m_pkt_list.begin();
            }
            m_pkt_list_mtx.unlock();

            string file_name = recoderinfo.recoderPath;
            
            FFRecoder ffrecoder;
            bool bInit = ffrecoder.init(m_inStream, m_avctx, file_name.c_str());
            if (!bInit) {
                LOG_ERROR("ffrecoder init error : {} {} {}", recoderinfo.task_id, recoderinfo.object_id, recoderinfo.frame_nb);
                ffrecoder.uninit();
                break;
            }
            LOG_DEBUG("record start, pkt_list size: {} task_id: {}  object_id:{}  frame_nb: {}", m_pkt_list.size(), recoderinfo.task_id, recoderinfo.object_id, recoderinfo.frame_nb);

            int count = 0;
            auto it_save = it_data;
            unsigned long long start_frame_nb = (*it_data)->frame_nb;
            unsigned long long end_frame_nb = (*it_data)->frame_nb;
            for (; it_save != m_pkt_list.end() && count < 500; ++it_save) {
                DataPacket* dataPkt = *it_save;
                if(dataPkt->frame_nb > recoderinfo.frame_nb) {
                    break;
                }
                AVPacket* pkt = dataPkt->pkt;
                if(pkt == nullptr) {
                    LOG_ERROR("{} pkt is nullptr", recoderinfo.task_id);
                    continue;
                } else if (pkt->data == nullptr || pkt->size <= 0){
                    LOG_ERROR("{} pkt data is nullptr", recoderinfo.task_id);
                    continue;
                }
                
                ffrecoder.write_pkt(pkt);
                count++;
                end_frame_nb = (*it_save)->frame_nb;
            }

            ffrecoder.flush_pkt();
            ffrecoder.uninit();

            // 发送mq消息
            if(mq_publish_func && recoderinfo.mq_info.length() > 0) {
                mq_publish_func(recoderinfo.mq_info.c_str());
                // LOG_INFO("record save: {}", recoderinfo.mq_info.c_str());
            }

            LOG_INFO("record end, total save: {} start_frame_nb: {} end_frame_nb: {}  file_path: {}", count, start_frame_nb, end_frame_nb, file_name);
        } while (0);
        
        // m_recoderinfo_list 为空会触发 m_pkt_list size 大于1000时的删除操作,
        // 因此应当在本次m_pkt_list操作都完成之后再pop,避免这边在用m_pkt_list, 另一边在删除,从而导致崩溃
        m_recoderinfo_list_mtx.lock();
        m_recoderinfo_list.pop_front();
        m_recoderinfo_list_mtx.unlock();  
    }

    LOG_INFO("recode_thread end.");
}

void FFRecoderTaskManager::pkt_cache_thread() {
    LOG_INFO("pkt_cache_thread start...");

     m_recoder_thread = new std::thread(
        [](void* arg) {
            FFRecoderTaskManager* _this=(FFRecoderTaskManager*)arg;
            if(_this != nullptr) {
                _this->recode_thread2();
            }else{
                LOG_ERROR("recode 线程启动失败 !");
            }
            return (void*)0;
        }, this);

    // 开始缓存
    while(true) {
        if(m_bExit) {
            break;
        }

        std::this_thread::sleep_for(std::chrono::milliseconds(500));

        m_pkt_list_short_mtx.lock();
        m_pkt_list_mtx.lock();
        for (size_t i = 0; i < m_pkt_list_short.size(); i++) {
            auto item = m_pkt_list_short.front();
            m_pkt_list.push_back(item);
            m_pkt_list_short.pop_front();
        }
        m_pkt_list_mtx.unlock();
        m_pkt_list_short_mtx.unlock();
        

        std::lock_guard<std::mutex> l_info(m_recoderinfo_list_mtx);
        if(m_recoderinfo_list.size() <= 0){
            // 没有任务的时候,维持500的长度
            m_pkt_list_mtx.lock();
            while(m_pkt_list.size() > 1000) {
                DataPacket* dataPkt = m_pkt_list.front();
                delete dataPkt;
                dataPkt = nullptr;
                m_pkt_list.pop_front();
            }
            m_pkt_list_mtx.unlock();
        }
    }

    m_bExitRecoderThread = true;

    if (m_recoder_thread) {
        m_recoder_thread->join();
        delete m_recoder_thread;
        m_recoder_thread = nullptr;
    }

    // 清空数据
    while(!m_pkt_list.empty()) {
        DataPacket* dataPkt = m_pkt_list.front();
        delete dataPkt;
        dataPkt = nullptr;
        m_pkt_list.pop_front();
    }

    LOG_INFO("pkt_cache_thread end.");
}

void FFRecoderTaskManager::recode_thread2() {
    LOG_INFO("recode_thread2 start...");
    while(true) {
        if(m_bExitRecoderThread) {
            break;
        }

        m_recoderinfo_list_mtx.lock();
        if(m_recoderinfo_list.size() <= 0){
            m_recoderinfo_list_mtx.unlock();
            std::this_thread::sleep_for(std::chrono::milliseconds(3));
            continue;
        }

        auto it_param = m_recoderinfo_list.begin();
        RecoderInfo recoderinfo = *it_param;
        m_recoderinfo_list_mtx.unlock();

        do
        {
            auto it_data = getStartIterator(recoderinfo.frame_nb);
            if(it_data == m_pkt_list.end()) {
                std::this_thread::sleep_for(std::chrono::milliseconds(3));
                break;
            }

            LOG_INFO("start frame_nb: {}", (*it_data)->frame_nb);

            std::lock_guard<std::mutex> l_long(m_pkt_list_mtx);

            string file_name = recoderinfo.recoderPath;
            
            FFRecoder2 ffrecoder;
            bool bInit = ffrecoder.init(m_width, m_height, m_fps, m_bit_rate, file_name.c_str());
            if (!bInit) {
                LOG_ERROR("ffrecoder init error : {} {} {}", recoderinfo.task_id, recoderinfo.object_id, recoderinfo.frame_nb);
                ffrecoder.uninit();
                break;
            }
            LOG_DEBUG("record start, pkt_list size: {} task_id: {}  object_id:{}  frame_nb: {}", m_pkt_list.size(), recoderinfo.task_id, recoderinfo.object_id, recoderinfo.frame_nb);

            int count = 0;
            auto it_save = it_data;
            unsigned long long start_frame_nb = (*it_data)->frame_nb;
            unsigned long long end_frame_nb = (*it_data)->frame_nb;
            for (; it_save != m_pkt_list.end() && count < 500; ++it_save) {
                DataPacket* dataPkt = *it_save;
                if(dataPkt->frame_nb > recoderinfo.frame_nb) {
                    break;
                }
                AVPacket* pkt = dataPkt->pkt;
                if(pkt == nullptr) {
                    LOG_ERROR("{} pkt is nullptr", recoderinfo.task_id);
                    continue;
                } else if (pkt->data == nullptr || pkt->size <= 0){
                    LOG_ERROR("{} pkt data is nullptr", recoderinfo.task_id);
                    continue;
                }
                
                ffrecoder.write_pkt(pkt);
                count++;
                end_frame_nb = (*it_save)->frame_nb;
            }

            ffrecoder.flush();
            ffrecoder.uninit();

            // 发送mq消息
            if(mq_publish_func && recoderinfo.mq_info.length() > 0) {
                mq_publish_func(recoderinfo.mq_info.c_str());
                // LOG_INFO("record save: {}", recoderinfo.mq_info.c_str());
            }

            LOG_INFO("record end, total save: {} start_frame_nb: {} end_frame_nb: {}  file_path: {}", count, start_frame_nb, end_frame_nb, file_name);
        } while (0);
        
        // m_recoderinfo_list 为空会触发 m_pkt_list size 大于1000时的删除操作,
        // 因此应当在本次m_pkt_list操作都完成之后再pop,避免这边在用m_pkt_list, 另一边在删除,从而导致崩溃
        m_recoderinfo_list_mtx.lock();
        m_recoderinfo_list.pop_front();
        m_recoderinfo_list_mtx.unlock();  
    }

    LOG_INFO("recode_thread2 end.");
}

void FFRecoderTaskManager::close() {
    m_bExit = true;

    if (m_cache_thread) {
        m_cache_thread->join();
        delete m_cache_thread;
        m_cache_thread = nullptr;
    }

    // 清空数据
    while(!m_pkt_list_short.empty()) {
        DataPacket* dataPkt = m_pkt_list_short.front();
        delete dataPkt;
        dataPkt = nullptr;
        m_pkt_list_short.pop_front();
    }
}

void FFRecoderTaskManager::set_mq_callback(mq_callback_t cb) {
    mq_publish_func = cb;
}