int RollPollingWorker::FilterFrame(FilterContext* filter, AVFrame* frame) {
int ret = av_buffersrc_add_frame_flags(filter->buffersrc_ctx_, frame, 0);
if (ret < 0) {
ErrorL << "Error while feeding the filtergraph \n";
return ret;
}
code
while (1) {
ret = av_buffersink_get_frame(filter->buffersink_ctx_, filter->filter_frame_);
if (ret < 0) {
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
ret = 0;
break;
}
filter->filter_frame_->pict_type = AV_PICTURE_TYPE_NONE;
if (need_idr_) {
filter->filter_frame_->pict_type = AV_PICTURE_TYPE_I;
need_idr_ = false;
}
ret = EncodeFrame(filter->filter_frame_);
av_frame_unref(filter->filter_frame_);
if (ret < 0)
break;
}
return ret;
}
int RollPollingWorker::EncodeFrame(AVFrame* frame) {
if (avcodec_send_frame(ocodec_ctx_, frame) < 0)
return -1;
code
AVPacket* enc_pkt = av_packet_alloc();
int ret = 0;
while (ret >= 0) {
ret = avcodec_receive_packet(ocodec_ctx_, enc_pkt);
if (ret < 0) {
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
ret = 0;
break;
}
enc_pkt->stream_index = 0;
enc_pkt->pts = enc_pkt->dts = (frame_cnt_++) * 40;
enc_pkt->duration = 0;
enc_pkt->pos = -1;
//std::cerr << "muxing frame: pts " << enc_pkt->pts << " duration " << enc_pkt->duration << "\n";
ret = av_interleaved_write_frame(ofmt_ctx_, enc_pkt);
av_packet_unref(enc_pkt);
if (ret < 0) {
ErrorL << Prefix() << " write packet to output file failed \n";
break;
}
}
av_packet_free(&enc_pkt);
return ret;
}
int RollPollingWorkerPool::StartWorker(const std::string input, const std::string output) {
std::shared_ptr worker = std::make_shared();
if (!worker->Start(input, output)) {
return -1;
}
code
std::weak_ptr<RollPollingWorker> weak_worker = worker;
auto task = [weak_worker](int64_t unused){
auto worker = weak_worker.lock();
if (worker)
worker->Runnable();
};
std::lock_guard<std::mutex> lock(mutex_);
worker_list_[output] = worker;
return 0;
}
int RollPollingWorkerPool::StopWorker(const std::string url) {
std::lock_guardstd::mutex lock(mutex_);
auto it = worker_list_.find(url);
return it != worker_list_.end() ? it->second->Stop() : -1;
}
int RollPollingWorkerPool::PollingStart(const std::vectorstd::string inputsurl, const std::string outputurl) {
std::vectorstd::string inputs = inputsurl ;
// for (int i = 1; i < (argc - 1); i++)
// inputs.push_back(argv[i]);
// std::string output = argv[argc - 1];
std::string output = outputurl;
code
mediakit::RollPollingWorker worker;
if (worker.Start(inputs[0], output) < 0)
return -1;
auto f = [&](){
worker.Runnable();
};
std::thread task(f);
task.detach();
size_t idx = 1;
while (worker.IsRunning()) {
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
if (idx >= 4)
break;
if (inputs.size() > 1) {
std::string tmp = inputs[idx % inputs.size()];
std::cerr << "change input file to [" << tmp << "\n";
worker.UpdateInput(tmp);
idx++;
}
}
worker.Stop();
//task.join();
return 0;