summarylogtreecommitdiffstats
path: root/vaapi_set_dri_devices.patch
blob: b5bb71405971ef470f10a32dea565d212ebbfe8b (plain)
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
diff --git a/plugins/obs-ffmpeg/CMakeLists.txt b/plugins/obs-ffmpeg/CMakeLists.txt
index 9df656b2d..20b084184 100644
--- a/plugins/obs-ffmpeg/CMakeLists.txt
+++ b/plugins/obs-ffmpeg/CMakeLists.txt
@@ -11,6 +11,11 @@ find_package(FFmpeg REQUIRED
 	COMPONENTS avcodec avfilter avdevice avutil swscale avformat swresample)
 include_directories(${FFMPEG_INCLUDE_DIRS})
 
+if(UNIX AND NOT APPLE)
+        find_package(Libpci REQUIRED)
+        include_directories(${LIBPCI_INCLUDE_DIRS})
+endif()
+
 configure_file(
 	"${CMAKE_CURRENT_SOURCE_DIR}/obs-ffmpeg-config.h.in"
 	"${CMAKE_CURRENT_BINARY_DIR}/obs-ffmpeg-config.h")
@@ -37,7 +42,8 @@ if(UNIX AND NOT APPLE)
 	list(APPEND obs-ffmpeg_SOURCES
 		obs-ffmpeg-vaapi.c)
 	LIST(APPEND obs-ffmpeg_PLATFORM_DEPS
-		${LIBVA_LBRARIES})
+		${LIBVA_LBRARIES}
+		${LIBPCI_LIBRARIES})
 endif()
 
 if(ENABLE_FFMPEG_LOGGING)
diff --git a/plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c b/plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c
index 445c5a792..1598d39db 100644
--- a/plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c
+++ b/plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c
@@ -22,6 +22,7 @@
 #include <util/darray.h>
 #include <util/dstr.h>
 #include <util/base.h>
+#include <util/platform.h>
 #include <media-io/video-io.h>
 #include <obs-module.h>
 #include <obs-avc.h>
@@ -35,6 +36,8 @@
 #include <libavformat/avformat.h>
 #include <libavfilter/avfilter.h>
 
+#include <pci/pci.h>
+
 #include "obs-ffmpeg-formats.h"
 
 #define do_log(level, format, ...)                          \
@@ -538,6 +541,31 @@ static bool rate_control_modified(obs_properties_t *ppts, obs_property_t *p,
 	return true;
 }
 
+static bool get_device_name_from_pci(struct pci_access *pacc, char *pci_slot,
+				     char *buf, int size)
+{
+	struct pci_filter filter;
+	struct pci_dev *dev;
+	char *name;
+
+	pci_filter_init(pacc, &filter);
+	if (pci_filter_parse_slot(&filter, pci_slot))
+		return false;
+
+	pci_scan_bus(pacc);
+	for (dev = pacc->devices; dev; dev = dev->next) {
+		if (pci_filter_match(&filter, dev)) {
+			pci_fill_info(dev, PCI_FILL_IDENT);
+			name = pci_lookup_name(pacc, buf, size,
+					       PCI_LOOKUP_DEVICE,
+					       dev->vendor_id, dev->device_id);
+			strcpy(buf, name);
+			return true;
+		}
+	}
+	return false;
+}
+
 static obs_properties_t *vaapi_properties(void *unused)
 {
 	UNUSED_PARAMETER(unused);
@@ -549,15 +577,58 @@ static obs_properties_t *vaapi_properties(void *unused)
 				       obs_module_text("VAAPI.Device"),
 				       OBS_COMBO_TYPE_LIST,
 				       OBS_COMBO_FORMAT_STRING);
-	char path[32] = "/dev/dri/renderD1";
-	for (int i = 28;; i++) {
-		sprintf(path, "/dev/dri/renderD1%d", i);
-		if (access(path, F_OK) == 0) {
-			char card[128] = "Card: ";
-			sprintf(card, "Card%d: %s", i - 28, path);
-			obs_property_list_add_string(list, card, path);
-		} else {
-			break;
+	if (os_file_exists("/dev/dri/by-path")) {
+		os_dir_t *by_path_dir = os_opendir("/dev/dri/by-path");
+		struct pci_access *pacc = pci_alloc();
+		struct os_dirent *file;
+		char namebuf[1024];
+		char pci_slot[13];
+		char *type;
+
+		pci_init(pacc);
+		while ((file = os_readdir(by_path_dir)) != NULL) {
+			// file_name pattern: pci-<pci_slot::12>-<type::{"card","render"}>
+			char *file_name = file->d_name;
+			if (strcmp(file_name, ".") == 0 ||
+			    strcmp(file_name, "..") == 0)
+				continue;
+
+			char path[64] = "\0";
+			sprintf(path, "/dev/dri/by-path/%s", file_name);
+			type = strrchr(file_name, '-');
+			if (type == NULL)
+				continue;
+			else
+				type++;
+
+			if (strcmp(type, "render") == 0) {
+				strncpy(pci_slot, file_name + 4, 12);
+				pci_slot[12] = 0;
+				bool name_found = get_device_name_from_pci(
+					pacc, pci_slot, namebuf,
+					sizeof(namebuf));
+				if (!name_found)
+					obs_property_list_add_string(list, path,
+								     path);
+				else
+					obs_property_list_add_string(
+						list, namebuf, path);
+			}
+		}
+		pci_cleanup(pacc);
+		os_closedir(by_path_dir);
+	}
+	if (obs_property_list_item_count(list) == 0) {
+		char path[32] = "/dev/dri/renderD1";
+		for (int i = 28;; i++) {
+			sprintf(path, "/dev/dri/renderD1%d", i);
+			if (access(path, F_OK) == 0) {
+				char card[128] = "Card: ";
+				sprintf(card, "Card%d: %s", i - 28, path);
+				obs_property_list_add_string(list, card, path);
+			} else {
+				break;
+			}
 		}
 	}