File: decklink.cpp

package info (click to toggle)
obs-studio 0.15.4%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,628 kB
  • ctags: 35,365
  • sloc: ansic: 123,403; cpp: 28,026; objc: 1,036; makefile: 908; sh: 375; python: 49
file content (130 lines) | stat: -rw-r--r-- 3,137 bytes parent folder | download
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
#include "decklink.hpp"
#include "decklink-device-discovery.hpp"
#include "decklink-device-instance.hpp"
#include "decklink-device-mode.hpp"

#include <util/threading.h>

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

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

DeckLinkDevice *DeckLink::GetDevice() const
{
	return instance ? instance->GetDevice() : nullptr;
}

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

		settings = obs_source_get_settings(decklink->source);
		hash = obs_data_get_string(settings, "device_hash");
		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))
				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 DeckLink::Activate(DeckLinkDevice *device, long long modeId)
{
	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->GetActivePixelFormat() == pixelFormat)
			return false;
	}

	if (isActive)
		instance->StopCapture();

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

	if (instance == nullptr)
		return false;

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

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

	os_atomic_inc_long(&activateRefs);
	SaveSettings();
	return true;
}

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

	os_atomic_dec_long(&activateRefs);
}

void DeckLink::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 *DeckLink::GetSource(void) const
{
	return source;
}