*
* Author: Robert Nagy, ronag89@gmail.com
*/
-
+
#include "../StdAfx.h"
#include "bluefish_consumer.h"
#include <memory>
#include <array>
-namespace caspar { namespace bluefish {
-
+namespace caspar { namespace bluefish {
+
+#define BLUEFISH_HW_BUFFER_DEPTH 1
+#define BLUEFISH_SOFTWARE_BUFFERS 4
+
enum class hardware_downstream_keyer_mode
{
disable = 0,
struct bluefish_consumer : boost::noncopyable
{
- spl::shared_ptr<bvc_wrapper> blue_;
- const unsigned int device_index_;
- const core::video_format_desc format_desc_;
- const core::audio_channel_layout channel_layout_;
- core::audio_channel_remapper channel_remapper_;
- const int channel_index_;
-
- const std::wstring model_name_;
-
- spl::shared_ptr<diagnostics::graph> graph_;
- boost::timer frame_timer_;
- boost::timer tick_timer_;
- boost::timer sync_timer_;
-
- unsigned int vid_fmt_;
-
- std::array<blue_dma_buffer_ptr, 4> all_frames_;
- std::queue<blue_dma_buffer_ptr> reserved_frames_;
- std::mutex reserved_frames_lock_;
- std::queue<blue_dma_buffer_ptr> live_frames_;
- std::mutex live_frames_lock_;
- std::shared_ptr<std::thread> dma_present_thread_;
- bool end_dma_thread_;
-
- tbb::concurrent_bounded_queue<core::const_frame> frame_buffer_;
- tbb::atomic<int64_t> presentation_delay_millis_;
- core::const_frame previous_frame_ = core::const_frame::empty();
-
- const bool embedded_audio_;
- const bool key_only_;
-
- executor executor_;
- hardware_downstream_keyer_mode hardware_keyer_;
- hardware_downstream_keyer_audio_source keyer_audio_source_;
- bluefish_hardware_output_channel device_output_channel_;
+ spl::shared_ptr<bvc_wrapper> blue_;
+ const unsigned int device_index_;
+ const core::video_format_desc format_desc_;
+ const core::audio_channel_layout channel_layout_;
+ core::audio_channel_remapper channel_remapper_;
+ const int channel_index_;
+
+ const std::wstring model_name_;
+
+ spl::shared_ptr<diagnostics::graph> graph_;
+ boost::timer frame_timer_;
+ boost::timer tick_timer_;
+ boost::timer sync_timer_;
+
+ unsigned int vid_fmt_;
+
+ std::array<blue_dma_buffer_ptr, BLUEFISH_SOFTWARE_BUFFERS> all_frames_;
+ tbb::concurrent_bounded_queue<blue_dma_buffer_ptr> reserved_frames_;
+ tbb::concurrent_bounded_queue<blue_dma_buffer_ptr> live_frames_;
+ std::shared_ptr<std::thread> dma_present_thread_;
+ tbb::atomic<bool> end_dma_thread_;
+
+ tbb::concurrent_bounded_queue<core::const_frame> frame_buffer_;
+ tbb::atomic<int64_t> presentation_delay_millis_;
+ core::const_frame previous_frame_ = core::const_frame::empty();
+
+ const bool embedded_audio_;
+ const bool key_only_;
+
+ executor executor_;
+ hardware_downstream_keyer_mode hardware_keyer_;
+ hardware_downstream_keyer_audio_source keyer_audio_source_;
+ bluefish_hardware_output_channel device_output_channel_;
public:
bluefish_consumer(
const core::video_format_desc& format_desc,
executor_.set_capacity(1);
presentation_delay_millis_ = 0;
- graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
+ reserved_frames_.set_capacity(BLUEFISH_SOFTWARE_BUFFERS);
+ live_frames_.set_capacity(BLUEFISH_SOFTWARE_BUFFERS);
+
+ graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
graph_->set_color("sync-time", diagnostics::color(1.0f, 0.0f, 0.0f));
graph_->set_color("frame-time", diagnostics::color(0.5f, 1.0f, 0.2f));
graph_->set_text(print());
// Select Update Mode for output
if(BLUE_FAIL(blue_->set_card_property32(VIDEO_UPDATE_TYPE, UPD_FMT_FRAME)))
CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set update type."));
-
+
disable_video_output();
setup_hardware_output_channel_routing();
-
+
//Select output memory format
if(BLUE_FAIL(blue_->set_card_property32(VIDEO_MEMORY_FORMAT, MEM_FMT_ARGB_PC)))
CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set memory format."));
-
+
//Select image orientation
if(BLUE_FAIL(blue_->set_card_property32(VIDEO_IMAGE_ORIENTATION, ImageOrientation_Normal)))
- CASPAR_LOG(warning) << print() << L" Failed to set image orientation to normal.";
+ CASPAR_LOG(warning) << print() << L" Failed to set image orientation to normal.";
// Select data range
if(BLUE_FAIL(blue_->set_card_property32(VIDEO_RGB_DATA_RANGE, CGR_RANGE)))
- CASPAR_LOG(warning) << print() << L" Failed to set RGB data range to CGR.";
-
+ CASPAR_LOG(warning) << print() << L" Failed to set RGB data range to CGR.";
+
if(!embedded_audio_ || (hardware_keyer_ == hardware_downstream_keyer_mode::internal && keyer_audio_source_ == hardware_downstream_keyer_audio_source::SDIVideoInput) )
{
if(BLUE_FAIL(blue_->set_card_property32(EMBEDEDDED_AUDIO_OUTPUT, 0)))
- CASPAR_LOG(warning) << TEXT("BLUECARD ERROR: Failed to disable embedded audio.");
+ CASPAR_LOG(warning) << TEXT("BLUECARD ERROR: Failed to disable embedded audio.");
CASPAR_LOG(info) << print() << TEXT(" Disabled embedded-audio.");
}
else
audio_value |= blue_emb_audio_group4_enable;
if(BLUE_FAIL(blue_->set_card_property32(EMBEDEDDED_AUDIO_OUTPUT, audio_value)))
- CASPAR_LOG(warning) << print() << TEXT(" Failed to enable embedded audio.");
+ CASPAR_LOG(warning) << print() << TEXT(" Failed to enable embedded audio.");
CASPAR_LOG(info) << print() << TEXT(" Enabled embedded-audio.");
}
setup_hardware_downstream_keyer(hardware_keyer_, keyer_audio_source_);
enable_video_output();
-
+
int n = 0;
boost::range::generate(all_frames_, [&]{return std::make_shared<blue_dma_buffer>(static_cast<int>(format_desc_.size), n++);});
-
+
for (size_t i = 0; i < all_frames_.size(); i++)
reserved_frames_.push(all_frames_[i]);
}
{
end_dma_thread_ = true;
disable_video_output();
- blue_->detach();
+ blue_->detach();
if (dma_present_thread_)
dma_present_thread_->join();
bool duallink_4224_enabled = false;
if ((device_output_channel_ == bluefish_hardware_output_channel::channel_a || device_output_channel_ == bluefish_hardware_output_channel::channel_c) &&
- (hardware_keyer_ == hardware_downstream_keyer_mode::external))
+ (hardware_keyer_ == hardware_downstream_keyer_mode::external) || (hardware_keyer_ == hardware_downstream_keyer_mode::internal) )
{
duallink_4224_enabled = true;
}
CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to MR 2 routing."));
// If single link 422, but on second channel AND on Neutron we need to set Genlock to Aux.
- if (is_epoch_neutron_1i2o_card((*blue_)))
+ if (is_epoch_neutron_1i2o_card((*blue_)))
{
if (blueVideoOutputChannel == BLUE_VIDEO_OUTPUT_CHANNEL_B)
{
if (BLUE_FAIL(blue_->get_card_property32(INVALID_VIDEO_MODE_FLAG, invalidVideoModeFlag)))
CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to get invalid video mode flag"));
+ // The bluefish HW keyer is NOT going to pre-multiply the RGB with the A.
+ keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_DATA_IS_PREMULTIPLIED(keyer_control_value);
+
keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_ENABLED(keyer_control_value);
if (BLUE_FAIL(blue_->get_card_property32(VIDEO_INPUT_SIGNAL_VIDEO_MODE, inputVideoSignal)))
CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to get video input signal mode"));
keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_ENABLE_OVER_BLACK(keyer_control_value);
else
keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_DISABLE_OVER_BLACK(keyer_control_value);
-
+
// lock to input
if (BLUE_FAIL(blue_->set_card_property32(VIDEO_GENLOCK_SIGNAL, BlueSDI_A_BNC)))
CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to set the genlock to the input for the HW keyer"));
void enable_video_output()
{
if(BLUE_FAIL(blue_->set_card_property32(VIDEO_BLACKGENERATOR, 0)))
- CASPAR_LOG(error) << print() << TEXT(" Failed to disable video output.");
+ CASPAR_LOG(error) << print() << TEXT(" Failed to disable video output.");
}
void disable_video_output()
{
blue_->video_playback_stop(0,0);
+ blue_->set_card_property32(VIDEO_DUAL_LINK_OUTPUT, 0);
+ ULONG routingValue = EPOCH_SET_ROUTING(EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHA, EPOCH_DEST_SDI_OUTPUT_B, BLUE_CONNECTOR_PROP_SINGLE_LINK);
+ blue_->set_card_property32(MR2_ROUTING, routingValue);
+
if(BLUE_FAIL(blue_->set_card_property32(VIDEO_BLACKGENERATOR, 1)))
- CASPAR_LOG(error)<< print() << TEXT(" Failed to disable video output.");
+ CASPAR_LOG(error)<< print() << TEXT(" Failed to disable video output.");
if (BLUE_FAIL(blue_->set_card_property32(EMBEDEDDED_AUDIO_OUTPUT, 0)))
CASPAR_LOG(error) << print() << TEXT(" Failed to disable audio output.");
}
-
+
std::future<bool> send(core::const_frame& frame)
- {
+ {
return executor_.begin_invoke([=]() -> bool
{
try
- {
- display_frame(frame);
+ {
+ display_frame(frame);
graph_->set_value("tick-time", static_cast<float>(tick_timer_.elapsed()*format_desc_.fps*0.5));
tick_timer_.restart();
}
});
}
- static void dma_present_thread_actual(void* arg)
+ void dma_present_thread_actual()
{
- bluefish_consumer* blue = (bluefish_consumer*)arg;
+ ensure_gpf_handler_installed_for_thread("bluefish consumer DMA thread");
bvc_wrapper wait_b;
- wait_b.attach(blue->device_index_);
- EBlueVideoChannel out_vid_channel = get_bluesdk_videochannel_from_streamid(blue->device_output_channel_);
+ wait_b.attach(device_index_);
+ EBlueVideoChannel out_vid_channel = get_bluesdk_videochannel_from_streamid(device_output_channel_);
wait_b.set_card_property32(DEFAULT_VIDEO_OUTPUT_CHANNEL, out_vid_channel);
- int frames_to_buffer = 3;
+ int frames_to_buffer = BLUEFISH_HW_BUFFER_DEPTH;
unsigned long buffer_id = 0;
unsigned long underrun = 0;
- while (!blue->end_dma_thread_)
+ while (!end_dma_thread_)
{
- if (blue->live_frames_.size() && BLUE_OK(blue->blue_->video_playback_allocate(buffer_id, underrun)))
+ blue_dma_buffer_ptr buf = nullptr;
+ if (live_frames_.try_pop(buf) && BLUE_OK(blue_->video_playback_allocate(buffer_id, underrun)))
{
- blue->live_frames_lock_.lock();
- blue_dma_buffer_ptr buf = blue->live_frames_.front();
- blue->live_frames_.pop();
- blue->live_frames_lock_.unlock();
-
// Send and display
- if (blue->embedded_audio_)
+ if (embedded_audio_)
{
- // Do video first, then encode hanc, then do hanc DMA...
- blue->blue_->system_buffer_write(const_cast<uint8_t*>(buf->image_data()),
+ // Do video first, then do hanc DMA...
+ blue_->system_buffer_write(const_cast<uint8_t*>(buf->image_data()),
static_cast<unsigned long>(buf->image_size()),
BlueImage_HANC_DMABuffer(buffer_id, BLUE_DATA_IMAGE),
0);
- blue->blue_->system_buffer_write(buf->hanc_data(),
+ blue_->system_buffer_write(buf->hanc_data(),
static_cast<unsigned long>(buf->hanc_size()),
BlueImage_HANC_DMABuffer(buffer_id, BLUE_DATA_HANC),
0);
- if (BLUE_FAIL(blue->blue_->video_playback_present(BlueBuffer_Image_HANC(buffer_id), 1, 0, 0)))
+ if (BLUE_FAIL(blue_->video_playback_present(BlueBuffer_Image_HANC(buffer_id), 1, 0, 0)))
{
- CASPAR_LOG(warning) << blue->print() << TEXT(" video_playback_present failed.");
+ CASPAR_LOG(warning) << print() << TEXT(" video_playback_present failed.");
}
}
else
{
- blue->blue_->system_buffer_write(const_cast<uint8_t*>(buf->image_data()),
+ blue_->system_buffer_write(const_cast<uint8_t*>(buf->image_data()),
static_cast<unsigned long>(buf->image_size()),
BlueImage_DMABuffer(buffer_id, BLUE_DATA_IMAGE),
0);
- if (BLUE_FAIL(blue->blue_->video_playback_present(BlueBuffer_Image(buffer_id), 1, 0, 0)))
- CASPAR_LOG(warning) << blue->print() << TEXT(" video_playback_present failed.");
+ if (BLUE_FAIL(blue_->video_playback_present(BlueBuffer_Image(buffer_id), 1, 0, 0)))
+ CASPAR_LOG(warning) << print() << TEXT(" video_playback_present failed.");
}
- // blue->graph_->set_value("frame-time", static_cast<float>(blue->frame_timer_.elapsed()*blue->format_desc_.fps*0.5));
-
- blue->reserved_frames_lock_.lock();
- blue->reserved_frames_.push(buf);
- blue->reserved_frames_lock_.unlock();
+ reserved_frames_.push(buf);
}
else
{
- // do WFS
+ // do WFS
unsigned long n_field = 0;
wait_b.wait_video_output_sync(UPD_FMT_FRAME, n_field);
}
frames_to_buffer--;
if (frames_to_buffer == 0)
{
- if (BLUE_FAIL(blue->blue_->video_playback_start(0, 0)))
- CASPAR_LOG(warning) << blue->print() << TEXT("Error video playback start failed");
+ if (BLUE_FAIL(blue_->video_playback_start(0, 0)))
+ CASPAR_LOG(warning) << print() << TEXT("Error video playback start failed");
}
}
}
void display_frame(core::const_frame frame)
{
- frame_timer_.restart();
+ frame_timer_.restart();
if (previous_frame_ != core::const_frame::empty())
presentation_delay_millis_ = previous_frame_.get_age_millis();
previous_frame_ = frame;
+ blue_dma_buffer_ptr buf = nullptr;
// Copy to local buffers
- if (reserved_frames_.size())
+ if (reserved_frames_.try_pop(buf))
{
- reserved_frames_lock_.lock();
- blue_dma_buffer_ptr buf = reserved_frames_.front();
- reserved_frames_.pop();
- reserved_frames_lock_.unlock();
void* dest = buf->image_data();
-
if (!frame.image_data().empty())
{
if (key_only_)
A_memcpy(dest, frame.image_data().begin(), frame.image_data().size());
}
else
- A_memset(dest, 0, reserved_frames_.front()->image_size());
+ A_memset(dest, 0, buf->image_size());
frame_timer_.restart();
static_cast<int>(frame.audio_data().size() / channel_layout_.num_channels),
static_cast<int>(channel_layout_.num_channels));
}
-
- live_frames_lock_.lock();
live_frames_.push(buf);
- live_frames_lock_.unlock();
// start the thread if required.
if (dma_present_thread_ == 0)
{
end_dma_thread_ = false;
- dma_present_thread_ = spl::make_shared<std::thread>(&dma_present_thread_actual, this);
+ dma_present_thread_ = std::make_shared<std::thread>([this] {dma_present_thread_actual(); });
#if defined(_WIN32)
HANDLE handle = (HANDLE)dma_present_thread_->native_handle();
SetThreadPriority(handle, THREAD_PRIORITY_HIGHEST);
}
void encode_hanc(BLUE_UINT32* hanc_data, void* audio_data, int audio_samples, int audio_nchannels)
- {
+ {
const auto sample_type = AUDIO_CHANNEL_24BIT | AUDIO_CHANNEL_LITTLEENDIAN;
auto emb_audio_flag = blue_emb_audio_enable | blue_emb_audio_group1_enable;
if (audio_nchannels > 12)
emb_audio_flag |= blue_emb_audio_group4_enable;
-
+
hanc_stream_info_struct hanc_stream_info;
memset(&hanc_stream_info, 0, sizeof(hanc_stream_info));
-
+
hanc_stream_info.AudioDBNArray[0] = -1;
hanc_stream_info.AudioDBNArray[1] = -1;
hanc_stream_info.AudioDBNArray[2] = -1;
hanc_stream_info.AudioDBNArray[3] = -1;
hanc_stream_info.hanc_data_ptr = hanc_data;
- hanc_stream_info.video_mode = vid_fmt_;
-
+ hanc_stream_info.video_mode = vid_fmt_;
+
int cardType = CRD_INVALID;
blue_->query_card_type(cardType, device_index_);
blue_->encode_hanc_frame(cardType, &hanc_stream_info, audio_data, audio_nchannels, audio_samples, sample_type, emb_audio_flag);
}
-
+
std::wstring print() const
{
- return model_name_ + L" [" + boost::lexical_cast<std::wstring>(channel_index_) + L"-" +
+ return model_name_ + L" [" + boost::lexical_cast<std::wstring>(channel_index_) + L"-" +
boost::lexical_cast<std::wstring>(device_index_) + L"|" + format_desc_.name + L"]";
}
public:
- bluefish_consumer_proxy(int device_index,
- bool embedded_audio,
- bool key_only,
+ bluefish_consumer_proxy(int device_index,
+ bool embedded_audio,
+ bool key_only,
hardware_downstream_keyer_mode keyer,
hardware_downstream_keyer_audio_source keyer_audio_source,
const core::audio_channel_layout& out_channel_layout,
, hardware_output_channel_(hardware_output_channel)
{
}
-
+
// frame_consumer
-
+
void initialize(const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout, int channel_index) override
{
format_desc_ = format_desc;
out_channel_layout_ = in_channel_layout_;
consumer_.reset();
- consumer_.reset(new bluefish_consumer( format_desc,
- in_channel_layout_,
- out_channel_layout_,
- device_index_,
- embedded_audio_,
- key_only_,
+ consumer_.reset(new bluefish_consumer( format_desc,
+ in_channel_layout_,
+ out_channel_layout_,
+ device_index_,
+ embedded_audio_,
+ key_only_,
hardware_keyer_,
- hardware_keyer_audio_source_,
+ hardware_keyer_audio_source_,
channel_index,
hardware_output_channel_));
}
-
+
std::future<bool> send(core::const_frame frame) override
{
CASPAR_VERIFY(audio_cadence_.front() * in_channel_layout_.num_channels == static_cast<size_t>(frame.audio_data().size()));
boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
return consumer_->send(frame);
}
-
+
std::wstring print() const override
{
return consumer_ ? consumer_->print() : L"[bluefish_consumer]";
int buffer_depth() const override
{
- return 1;
+ return BLUEFISH_HW_BUFFER_DEPTH;
}
-
+
int index() const override
{
return 400 + device_index_;
{
return monitor_subject_;
}
-};
+};
void describe_consumer(core::help_sink& sink, const core::help_repository& repo)
spl::shared_ptr<core::frame_consumer> create_preconfigured_consumer(
const boost::property_tree::wptree& ptree, core::interaction_sink*,
std::vector<spl::shared_ptr<core::video_channel>> channels)
-{
+{
const auto device_index = ptree.get( L"device", 1);
const auto device_stream = ptree.get( L"sdi-stream", L"a");
const auto embedded_audio = ptree.get( L"embedded-audio", false);
const auto channel_layout = ptree.get_optional<std::wstring>( L"channel-layout");
const auto hardware_keyer_value = ptree.get( L"keyer", L"disabled");
const auto keyer_audio_source_value = ptree.get( L"internal-keyer-audio-source", L"videooutputchannel");
-
+
auto layout = core::audio_channel_layout::invalid();
if (channel_layout)
return spl::make_shared<bluefish_consumer_proxy>(device_index, embedded_audio, key_only, keyer_mode, keyer_audio_source, layout, device_output_channel);
}
-}}
\ No newline at end of file
+}}