summarylogtreecommitdiffstats
path: root/v4l2_by-path.patch
blob: 4fb112f13b8b76d4480b4be37309a3a6aab160c4 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
From 57cc18a73a05e00338f2ece4adb88ef3c3b87db8 Mon Sep 17 00:00:00 2001
From: Grzegorz Godlewski <gg@gitgis.com>
Date: Mon, 16 May 2022 18:29:16 +0200
Subject: [PATCH] linux-v4l2: Save device by id or path

---
 plugins/linux-v4l2/v4l2-input.c | 127 ++++++++++++++++++++++----------
 1 file changed, 90 insertions(+), 37 deletions(-)

diff --git a/plugins/linux-v4l2/v4l2-input.c b/plugins/linux-v4l2/v4l2-input.c
index e9d6f1712bbb..95387ff0c11d 100644
--- a/plugins/linux-v4l2/v4l2-input.c
+++ b/plugins/linux-v4l2/v4l2-input.c
@@ -341,32 +341,27 @@ static void v4l2_props_set_enabled(obs_properties_t *props,
 	}
 }
 
-/*
- * List available devices
- */
-static void v4l2_device_list(obs_property_t *prop, obs_data_t *settings)
+static void v4l2_device_list_directory(obs_property_t *prop,
+				       const char *basedir,
+				       bool allow_duplicates)
 {
 	DIR *dirp;
 	struct dirent *dp;
 	struct dstr device;
-	bool cur_device_found;
-	size_t cur_device_index;
-	const char *cur_device_name;
 
 #ifdef __FreeBSD__
-	dirp = opendir("/dev");
+	dirp = opendir(basedir);
 #else
-	dirp = opendir("/sys/class/video4linux");
+	if (0 == strcmp("/dev/", basedir)) {
+		dirp = opendir("/sys/class/video4linux");
+	} else {
+		dirp = opendir(basedir);
+	}
 #endif
 	if (!dirp)
 		return;
 
-	cur_device_found = false;
-	cur_device_name = obs_data_get_string(settings, "device_id");
-
-	obs_property_list_clear(prop);
-
-	dstr_init_copy(&device, "/dev/");
+	dstr_init(&device);
 
 	while ((dp = readdir(dirp)) != NULL) {
 		int fd;
@@ -381,20 +376,30 @@ static void v4l2_device_list(obs_property_t *prop, obs_data_t *settings)
 		if (dp->d_type == DT_DIR)
 			continue;
 
-		dstr_resize(&device, 5);
-		dstr_cat(&device, dp->d_name);
+		char *dev_path = dp->d_name;
+		char buf[1024];
+		ssize_t len;
+		if ((len = readlink(dp->d_name, buf, sizeof(buf) - 1)) != -1) {
+			buf[len] = '\0';
+			dev_path = &buf[0];
+		}
+		dstr_copy(&device, basedir);
+		dstr_cat(&device, dev_path);
 
 		if ((fd = v4l2_open(device.array, O_RDWR | O_NONBLOCK)) == -1) {
-			blog(LOG_INFO, "Unable to open %s", device.array);
+			const char *errstr = strerror(errno);
+			blog(LOG_WARNING, "Unable to open %s: %s", device.array,
+			     errstr);
 			continue;
 		}
 
 		if (v4l2_ioctl(fd, VIDIOC_QUERYCAP, &video_cap) == -1) {
-			blog(LOG_INFO, "Failed to query capabilities for %s",
+			blog(LOG_WARNING, "Failed to query capabilities for %s",
 			     device.array);
 			v4l2_close(fd);
 			continue;
 		}
+		v4l2_close(fd);
 
 #ifndef V4L2_CAP_DEVICE_CAPS
 		caps = video_cap.capabilities;
@@ -406,26 +411,77 @@ static void v4l2_device_list(obs_property_t *prop, obs_data_t *settings)
 #endif
 
 		if (!(caps & V4L2_CAP_VIDEO_CAPTURE)) {
-			blog(LOG_INFO, "%s seems to not support video capture",
+			blog(LOG_WARNING,
+			     "%s seems to not support video capture",
 			     device.array);
-			v4l2_close(fd);
 			continue;
 		}
 
-		/* make sure device names are unique */
-		char unique_device_name[68];
-		sprintf(unique_device_name, "%s (%s)", video_cap.card,
-			video_cap.bus_info);
-		obs_property_list_add_string(prop, unique_device_name,
-					     device.array);
-		blog(LOG_INFO, "Found device '%s' at %s", video_cap.card,
-		     device.array);
-
-		/* check if this is the currently used device */
-		if (cur_device_name && !strcmp(cur_device_name, device.array))
-			cur_device_found = true;
+		bool device_already_added = false;
+
+		if (!allow_duplicates) {
+			size_t listidx = 0;
+			const char *item_name;
+			while (NULL != (item_name = obs_property_list_item_name(
+						prop, listidx++))) {
+				if (NULL !=
+				    strstr(item_name, video_cap.bus_info)) {
+					device_already_added = true;
+					break;
+				}
+			}
+		}
 
-		v4l2_close(fd);
+		if (!device_already_added) {
+			char unique_device_name[68];
+			if (0 == strcmp("/dev/v4l/by-path/", basedir)) {
+				sprintf(unique_device_name, "%s (%s)",
+					video_cap.bus_info, video_cap.card);
+			} else {
+				sprintf(unique_device_name, "%s (%s)",
+					video_cap.card, video_cap.bus_info);
+			}
+			obs_property_list_add_string(prop, unique_device_name,
+						     device.array);
+			blog(LOG_INFO, "Found device '%s' at %s",
+			     video_cap.card, device.array);
+		}
+	}
+
+	closedir(dirp);
+	dstr_free(&device);
+}
+
+/*
+ * List available devices
+ */
+static void v4l2_device_list(obs_property_t *prop, obs_data_t *settings)
+{
+	bool cur_device_found;
+	size_t cur_device_index;
+	const char *cur_device_name;
+
+	obs_property_list_clear(prop);
+
+#ifdef __FreeBSD__
+	v4l2_device_list_directory(prop, "/dev/", false);
+#else
+	v4l2_device_list_directory(prop, "/dev/v4l/by-id/", true);
+	v4l2_device_list_directory(prop, "/dev/v4l/by-path/", true);
+	v4l2_device_list_directory(prop, "/dev/", false);
+#endif
+
+	cur_device_found = false;
+	cur_device_name = obs_data_get_string(settings, "device_id");
+
+	size_t listidx = 0;
+	const char *item_name;
+	while (NULL !=
+	       (item_name = obs_property_list_item_string(prop, listidx++))) {
+		if (0 == strcmp(item_name, cur_device_name)) {
+			cur_device_found = true;
+			break;
+		}
 	}
 
 	/* add currently selected device if not present, but disable it ... */
@@ -434,9 +490,6 @@ static void v4l2_device_list(obs_property_t *prop, obs_data_t *settings)
 			prop, cur_device_name, cur_device_name);
 		obs_property_list_item_disable(prop, cur_device_index, true);
 	}
-
-	closedir(dirp);
-	dstr_free(&device);
 }
 
 /*