2019-03-01 00:59:58 -08:00
|
|
|
#include "DecklinkInput.hpp"
|
2015-02-18 15:15:38 -08:00
|
|
|
|
|
|
|
#include <util/threading.h>
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
DeckLinkInput::DeckLinkInput(obs_source_t *source,
|
|
|
|
DeckLinkDeviceDiscovery *discovery_)
|
|
|
|
: DecklinkBase(discovery_), source(source)
|
2015-02-18 15:15:38 -08:00
|
|
|
{
|
2018-09-25 15:51:32 -07:00
|
|
|
discovery->AddCallback(DeckLinkInput::DevicesChanged, this);
|
2015-02-18 15:15:38 -08:00
|
|
|
}
|
|
|
|
|
2018-09-25 15:51:32 -07:00
|
|
|
DeckLinkInput::~DeckLinkInput(void)
|
2015-02-18 15:15:38 -08:00
|
|
|
{
|
2018-09-25 15:51:32 -07:00
|
|
|
discovery->RemoveCallback(DeckLinkInput::DevicesChanged, this);
|
2015-02-18 15:15:38 -08:00
|
|
|
Deactivate();
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
void DeckLinkInput::DevicesChanged(void *param, DeckLinkDevice *device,
|
|
|
|
bool added)
|
2015-02-18 15:15:38 -08:00
|
|
|
{
|
2019-06-22 22:13:45 -07:00
|
|
|
DeckLinkInput *decklink = reinterpret_cast<DeckLinkInput *>(param);
|
2015-02-18 15:15:38 -08:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(decklink->deviceMutex);
|
|
|
|
|
|
|
|
obs_source_update_properties(decklink->source);
|
|
|
|
|
|
|
|
if (added && !decklink->instance) {
|
|
|
|
const char *hash;
|
|
|
|
long long mode;
|
|
|
|
obs_data_t *settings;
|
2019-02-25 21:19:35 -08:00
|
|
|
BMDVideoConnection videoConnection;
|
|
|
|
BMDAudioConnection audioConnection;
|
2015-02-18 15:15:38 -08:00
|
|
|
|
|
|
|
settings = obs_source_get_settings(decklink->source);
|
|
|
|
hash = obs_data_get_string(settings, "device_hash");
|
2019-06-22 22:13:45 -07:00
|
|
|
videoConnection = (BMDVideoConnection)obs_data_get_int(
|
|
|
|
settings, "video_connection");
|
|
|
|
audioConnection = (BMDAudioConnection)obs_data_get_int(
|
|
|
|
settings, "audio_connection");
|
2015-02-18 15:15:38 -08:00
|
|
|
mode = obs_data_get_int(settings, "mode_id");
|
|
|
|
obs_data_release(settings);
|
|
|
|
|
|
|
|
if (device->GetHash().compare(hash) == 0) {
|
|
|
|
if (!decklink->activateRefs)
|
|
|
|
return;
|
2019-06-22 22:13:45 -07:00
|
|
|
if (decklink->Activate(device, mode, videoConnection,
|
|
|
|
audioConnection))
|
2015-02-18 15:15:38 -08:00
|
|
|
os_atomic_dec_long(&decklink->activateRefs);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (!added && decklink->instance) {
|
|
|
|
if (decklink->instance->GetDevice() == device) {
|
|
|
|
os_atomic_inc_long(&decklink->activateRefs);
|
|
|
|
decklink->Deactivate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 21:19:35 -08:00
|
|
|
bool DeckLinkInput::Activate(DeckLinkDevice *device, long long modeId,
|
2019-06-22 22:13:45 -07:00
|
|
|
BMDVideoConnection bmdVideoConnection,
|
|
|
|
BMDAudioConnection bmdAudioConnection)
|
2015-02-18 15:15:38 -08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(deviceMutex);
|
|
|
|
DeckLinkDevice *curDevice = GetDevice();
|
|
|
|
const bool same = device == curDevice;
|
|
|
|
const bool isActive = instance != nullptr;
|
|
|
|
|
2015-10-08 10:42:33 -07:00
|
|
|
if (same) {
|
|
|
|
if (!isActive)
|
|
|
|
return false;
|
|
|
|
if (instance->GetActiveModeId() == modeId &&
|
2019-02-25 21:19:35 -08:00
|
|
|
instance->GetVideoConnection() == bmdVideoConnection &&
|
|
|
|
instance->GetAudioConnection() == bmdAudioConnection &&
|
2017-03-30 21:03:38 -07:00
|
|
|
instance->GetActivePixelFormat() == pixelFormat &&
|
2017-05-05 05:55:06 -07:00
|
|
|
instance->GetActiveColorSpace() == colorSpace &&
|
|
|
|
instance->GetActiveColorRange() == colorRange &&
|
2019-03-01 00:59:58 -08:00
|
|
|
instance->GetActiveChannelFormat() == channelFormat &&
|
|
|
|
instance->GetActiveSwapState() == swap)
|
2015-10-08 10:42:33 -07:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-18 15:15:38 -08:00
|
|
|
|
|
|
|
if (isActive)
|
|
|
|
instance->StopCapture();
|
|
|
|
|
2018-07-30 18:31:53 -07:00
|
|
|
isCapturing = false;
|
2015-02-18 15:15:38 -08:00
|
|
|
if (!same)
|
|
|
|
instance.Set(new DeckLinkDeviceInstance(this, device));
|
|
|
|
|
|
|
|
if (instance == nullptr)
|
|
|
|
return false;
|
|
|
|
|
2018-09-25 15:51:32 -07:00
|
|
|
if (GetDevice() == nullptr) {
|
2019-06-22 22:13:45 -07:00
|
|
|
LOG(LOG_ERROR,
|
|
|
|
"Tried to activate an input with nullptr device.");
|
2018-09-25 15:51:32 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeckLinkDeviceMode *mode = GetDevice()->FindInputMode(modeId);
|
2015-02-18 15:15:38 -08:00
|
|
|
if (mode == nullptr) {
|
|
|
|
instance = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
if (!instance->StartCapture(mode, bmdVideoConnection,
|
|
|
|
bmdAudioConnection)) {
|
2015-02-18 15:15:38 -08:00
|
|
|
instance = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
os_atomic_inc_long(&activateRefs);
|
|
|
|
SaveSettings();
|
2018-07-30 18:31:53 -07:00
|
|
|
id = modeId;
|
|
|
|
isCapturing = true;
|
2015-02-18 15:15:38 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-25 15:51:32 -07:00
|
|
|
void DeckLinkInput::Deactivate(void)
|
2015-02-18 15:15:38 -08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(deviceMutex);
|
|
|
|
if (instance)
|
|
|
|
instance->StopCapture();
|
2018-07-30 18:31:53 -07:00
|
|
|
isCapturing = false;
|
2015-02-18 15:15:38 -08:00
|
|
|
instance = nullptr;
|
|
|
|
|
|
|
|
os_atomic_dec_long(&activateRefs);
|
|
|
|
}
|
|
|
|
|
2018-09-25 15:51:32 -07:00
|
|
|
bool DeckLinkInput::Capturing(void)
|
2018-07-30 18:31:53 -07:00
|
|
|
{
|
|
|
|
return isCapturing;
|
|
|
|
}
|
|
|
|
|
2018-09-25 15:51:32 -07:00
|
|
|
void DeckLinkInput::SaveSettings()
|
2015-02-18 15:15:38 -08:00
|
|
|
{
|
|
|
|
if (!instance)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DeckLinkDevice *device = instance->GetDevice();
|
|
|
|
DeckLinkDeviceMode *mode = instance->GetMode();
|
|
|
|
|
|
|
|
obs_data_t *settings = obs_source_get_settings(source);
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_data_set_string(settings, "device_hash", device->GetHash().c_str());
|
2015-02-18 15:15:38 -08:00
|
|
|
obs_data_set_string(settings, "device_name",
|
2019-06-22 22:13:45 -07:00
|
|
|
device->GetDisplayName().c_str());
|
2015-02-18 15:15:38 -08:00
|
|
|
obs_data_set_int(settings, "mode_id", instance->GetActiveModeId());
|
|
|
|
obs_data_set_string(settings, "mode_name", mode->GetName().c_str());
|
|
|
|
|
|
|
|
obs_data_release(settings);
|
|
|
|
}
|
|
|
|
|
2018-09-25 15:51:32 -07:00
|
|
|
obs_source_t *DeckLinkInput::GetSource(void) const
|
2015-02-18 15:15:38 -08:00
|
|
|
{
|
|
|
|
return source;
|
|
|
|
}
|