safe_ptr<core::basic_frame> get_frame(int hints) { if(exception_ != nullptr) std::rethrow_exception(exception_); hints_ = hints; safe_ptr<core::basic_frame> frame = core::basic_frame::late(); if(!frame_buffer_.try_pop(frame)) graph_->set_tag("late-frame"); graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity())); return frame; }
virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame* video, IDeckLinkAudioInputPacket* audio) { if(!video) return S_OK; try { graph_->set_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5); tick_timer_.restart(); frame_timer_.restart(); // PUSH void* bytes = nullptr; if(FAILED(video->GetBytes(&bytes)) || !bytes) return S_OK; safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free); avcodec_get_frame_defaults(av_frame.get()); av_frame->data[0] = reinterpret_cast<uint8_t*>(bytes); av_frame->linesize[0] = video->GetRowBytes(); av_frame->format = PIX_FMT_UYVY422; av_frame->width = video->GetWidth(); av_frame->height = video->GetHeight(); av_frame->interlaced_frame = format_desc_.field_mode != core::field_mode::progressive; av_frame->top_field_first = format_desc_.field_mode == core::field_mode::upper ? 1 : 0; std::shared_ptr<core::audio_buffer> audio_buffer; // It is assumed that audio is always equal or ahead of video. if(audio && SUCCEEDED(audio->GetBytes(&bytes)) && bytes) { auto sample_frame_count = audio->GetSampleFrameCount(); auto audio_data = reinterpret_cast<int32_t*>(bytes); audio_buffer = std::make_shared<core::audio_buffer>(audio_data, audio_data + sample_frame_count*format_desc_.audio_channels); } else audio_buffer = std::make_shared<core::audio_buffer>(audio_cadence_.front(), 0); // Note: Uses 1 step rotated cadence for 1001 modes (1602, 1602, 1601, 1602, 1601) // This cadence fills the audio mixer most optimally. sync_buffer_.push_back(audio_buffer->size()); if(!boost::range::equal(sync_buffer_, audio_cadence_)) { CASPAR_LOG(trace) << print() << L" Syncing audio."; return S_OK; } muxer_.push(audio_buffer); muxer_.push(av_frame, hints_); boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1); // POLL for(auto frame = muxer_.poll(); frame; frame = muxer_.poll()) { if(!frame_buffer_.try_push(make_safe_ptr(frame))) { auto dummy = core::basic_frame::empty(); frame_buffer_.try_pop(dummy); frame_buffer_.try_push(make_safe_ptr(frame)); graph_->set_tag("dropped-frame"); } } graph_->set_value("frame-time", frame_timer_.elapsed()*format_desc_.fps*0.5); graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity())); } catch(...) { exception_ = std::current_exception(); return E_FAIL; } return S_OK; }