| | |
| | | ,func_rec_info_(nullptr) |
| | | ,thrd_(nullptr) |
| | | ,error_occured_(false) |
| | | ,audio_(false) |
| | | ,cur_frame_a(0) |
| | | { |
| | | // logIt("RECODER ID: %s", id_.c_str()); |
| | | } |
| | |
| | | |
| | | } |
| | | |
| | | int Recorder::init_writer(){ |
| | | int Recorder::init_writer(const bool audio){ |
| | | if (out_) { |
| | | delete out_; |
| | | } |
| | |
| | | return -1; |
| | | } |
| | | |
| | | out_ = new FormatOut(in_->getStream(), "mp4"); |
| | | out_ = new FormatOut(in_->getFPS(), "mp4"); |
| | | |
| | | int pid = getpid(); |
| | | file_path_ = dir_ + "/" + sole::uuid4().base62() + "-" + std::to_string(pid) + ".mp4"; |
| | | |
| | | file_path_ = dir_ + "/" + sole::uuid4().base62() + ".mp4"; |
| | | auto ret = out_->JustWriter(in_->getStream(), file_path_.c_str()); |
| | | auto v = in_->getStream(AVMEDIA_TYPE_VIDEO); |
| | | if (!v){ |
| | | return -2; |
| | | } |
| | | AVStream *a = in_->getStream(AVMEDIA_TYPE_AUDIO); |
| | | if (!audio){ |
| | | a = NULL; |
| | | } |
| | | auto ret = out_->JustWriter(v, a, file_path_.c_str()); |
| | | if (ret){ |
| | | logIt("start record file: %s", file_path_.c_str()); |
| | | return 0; |
| | | } |
| | | |
| | | logIt("failed to start record: %s", file_path_.c_str()); |
| | | |
| | | return -1; |
| | | } |
| | |
| | | return 1; |
| | | } |
| | | |
| | | int64_t cur = cur_frame++; |
| | | AVPacket &op = pkt.data->getAVPacket(); |
| | | AVPacket np(op); |
| | | av_copy_packet(&np, &op); |
| | | auto ret = out_->writeFrame(np, cur); |
| | | av_packet_unref(&np); |
| | | if (!ret) return -1; |
| | | |
| | | if(pkt.id == id_frame_){ |
| | | id_frame_in_file_ = cur_frame-1; |
| | | int64_t cur = cur_frame; |
| | | if (in_->isVideoPkt(&np)){ |
| | | |
| | | if(pkt.id == id_frame_){ |
| | | id_frame_in_file_ = cur_frame; |
| | | } |
| | | cur_frame++; |
| | | }else if (in_->isAudioPkt(&np)) { |
| | | cur = cur_frame_a++; |
| | | } |
| | | |
| | | auto ret = out_->writeFrame(&np, cur); |
| | | av_packet_unref(&np); |
| | | |
| | | if (!ret) return -1; |
| | | |
| | | // logIt("WRITE FRAME ID: %d, RECORD ID: %d", pkt.id, id_frame_); |
| | | return 0; |
| | |
| | | end_writer(); |
| | | } |
| | | |
| | | int Recorder::Run(const char* output, const int mind, const int maxd){ |
| | | int Recorder::Run(const char* output, const int mind, const int maxd, const bool audio){ |
| | | |
| | | dir_ = output; |
| | | int ret = init_writer(); |
| | | int ret = init_writer(audio); |
| | | if(ret != 0){ |
| | | logIt("recorder init writer error"); |
| | | return -1; |
| | |
| | | minduration = fps * mind; |
| | | end_frame = minduration; |
| | | } |
| | | |
| | | audio_ = audio; |
| | | |
| | | // logIt("minduration %d maxduration %d curduration %d", minduration, maxduration, end_frame); |
| | | |
| | |
| | | |
| | | if(id_frame_ == -1){ |
| | | //wait I |
| | | if (list_pkt_.empty()) { |
| | | AVPacket &avpkt = pkt.data->getAVPacket(); |
| | | if (!(avpkt.flags & AV_PKT_FLAG_KEY)){ |
| | | return -1; |
| | | } |
| | | if (!audio_ && in_->isAudioPkt(&pkt.data->getAVPacket())){ |
| | | return 0; |
| | | } |
| | | |
| | | maybe_dump_gop(); |
| | |
| | | std::lock_guard<std::mutex> locker(mutex_pkt_); |
| | | bool i = false; |
| | | for (auto &p : lst){ |
| | | if (!i){ |
| | | AVPacket &avpkt = p.data->getAVPacket(); |
| | | if (!(avpkt.flags & AV_PKT_FLAG_KEY)){ |
| | | continue; |
| | | } |
| | | i = true; |
| | | if (!audio_ && in_->isAudioPkt(&p.data->getAVPacket())){ |
| | | continue; |
| | | } |
| | | |
| | | list_pkt_.push_back(p); |
| | |
| | | while (list_pkt_.size() > minduration) { |
| | | list_pkt_.pop_front(); |
| | | while(!list_pkt_.empty()){ |
| | | auto &cache = list_pkt_.front(); |
| | | AVPacket &avpkt = cache.data->getAVPacket(); |
| | | if (!(avpkt.flags & AV_PKT_FLAG_KEY)){ |
| | | auto &i = list_pkt_.front(); |
| | | if (!(i.data->getAVPacket().flags & AV_PKT_FLAG_KEY)){ |
| | | list_pkt_.pop_front(); |
| | | }else{ |
| | | break; |