File: DecklinkInput.cpp

package info (click to toggle)
obs-studio 30.2.3%2Bdfsg-3.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,928 kB
  • sloc: ansic: 202,137; cpp: 112,403; makefile: 868; python: 599; sh: 275; javascript: 19
file content (155 lines) | stat: -rw-r--r-- 4,087 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "DecklinkInput.hpp"

#include <util/threading.h>

DeckLinkInput::DeckLinkInput(obs_source_t *source,
			     DeckLinkDeviceDiscovery *discovery_)
	: DecklinkBase(discovery_),
	  source(source)
{
	discovery->AddCallback(DeckLinkInput::DevicesChanged, this);
}

DeckLinkInput::~DeckLinkInput(void)
{
	discovery->RemoveCallback(DeckLinkInput::DevicesChanged, this);
	Deactivate();
}

void DeckLinkInput::DevicesChanged(void *param, DeckLinkDevice *device,
				   bool added)
{
	DeckLinkInput *decklink = reinterpret_cast<DeckLinkInput *>(param);
	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;
		BMDVideoConnection videoConnection;
		BMDAudioConnection audioConnection;

		settings = obs_source_get_settings(decklink->source);
		hash = obs_data_get_string(settings, "device_hash");
		videoConnection = (BMDVideoConnection)obs_data_get_int(
			settings, "video_connection");
		audioConnection = (BMDAudioConnection)obs_data_get_int(
			settings, "audio_connection");
		mode = obs_data_get_int(settings, "mode_id");
		obs_data_release(settings);

		if (device->GetHash().compare(hash) == 0) {
			if (!decklink->activateRefs)
				return;
			if (decklink->Activate(device, mode, videoConnection,
					       audioConnection))
				os_atomic_dec_long(&decklink->activateRefs);
		}

	} else if (!added && decklink->instance) {
		if (decklink->instance->GetDevice() == device) {
			os_atomic_inc_long(&decklink->activateRefs);
			decklink->Deactivate();
		}
	}
}

bool DeckLinkInput::Activate(DeckLinkDevice *device, long long modeId,
			     BMDVideoConnection bmdVideoConnection,
			     BMDAudioConnection bmdAudioConnection)
{
	std::lock_guard<std::recursive_mutex> lock(deviceMutex);
	DeckLinkDevice *curDevice = GetDevice();
	const bool same = device == curDevice;
	const bool isActive = instance != nullptr;

	if (same) {
		if (!isActive)
			return false;
		if (instance->GetActiveModeId() == modeId &&
		    instance->GetVideoConnection() == bmdVideoConnection &&
		    instance->GetAudioConnection() == bmdAudioConnection &&
		    instance->GetActivePixelFormat() == pixelFormat &&
		    instance->GetActiveColorSpace() == colorSpace &&
		    instance->GetActiveColorRange() == colorRange &&
		    instance->GetActiveChannelFormat() == channelFormat &&
		    instance->GetActiveSwapState() == swap)
			return false;
	}

	if (isActive)
		instance->StopCapture();

	isCapturing = false;
	if (!same)
		instance.Set(new DeckLinkDeviceInstance(this, device));

	if (instance == nullptr)
		return false;

	if (GetDevice() == nullptr) {
		LOG(LOG_ERROR,
		    "Tried to activate an input with nullptr device.");
		return false;
	}

	DeckLinkDeviceMode *mode = GetDevice()->FindInputMode(modeId);
	if (mode == nullptr) {
		instance = nullptr;
		return false;
	}

	if (!instance->StartCapture(mode, allow10Bit, bmdVideoConnection,
				    bmdAudioConnection)) {
		instance = nullptr;
		return false;
	}

	os_atomic_inc_long(&activateRefs);
	SaveSettings();
	id = modeId;
	isCapturing = true;
	return true;
}

void DeckLinkInput::Deactivate(void)
{
	std::lock_guard<std::recursive_mutex> lock(deviceMutex);
	if (instance)
		instance->StopCapture();
	isCapturing = false;
	instance = nullptr;

	os_atomic_dec_long(&activateRefs);
}

bool DeckLinkInput::Capturing(void)
{
	return isCapturing;
}

void DeckLinkInput::SaveSettings()
{
	if (!instance)
		return;

	DeckLinkDevice *device = instance->GetDevice();
	DeckLinkDeviceMode *mode = instance->GetMode();

	obs_data_t *settings = obs_source_get_settings(source);

	obs_data_set_string(settings, "device_hash", device->GetHash().c_str());
	obs_data_set_string(settings, "device_name",
			    device->GetDisplayName().c_str());
	obs_data_set_int(settings, "mode_id", instance->GetActiveModeId());
	obs_data_set_string(settings, "mode_name", mode->GetName().c_str());

	obs_data_release(settings);
}

obs_source_t *DeckLinkInput::GetSource(void) const
{
	return source;
}