Skip to content

Commit 0959cce

Browse files
author
bijunda
committed
sim: stabilize camera enumeration ordering for macOS and Linux V4L2
Add macOS AVFoundation camera enumeration. Filter Linux V4L2 nodes that do not support CAP_VIDEO_CAPTURE to fix Linux/macOS camera device node mount ordering and exclude Linux metadata nodes. Signed-off-by: Peter Bee <bijunda@bytedance.com>
1 parent 6cb117b commit 0959cce

3 files changed

Lines changed: 50 additions & 67 deletions

File tree

arch/sim/src/sim/macos/sim_host_avfoundation_backend.m

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ static int avf_report_open_error(int err, const char *message)
151151
{
152152
AVCaptureDeviceDiscoverySession *discovery;
153153
NSArray<AVCaptureDevice *> *devices;
154+
NSMutableArray<AVCaptureDevice *> *ordered;
155+
AVCaptureDevice *device;
154156

155157
discovery = [AVCaptureDeviceDiscoverySession
156158
discoverySessionWithDeviceTypes:@[
@@ -171,7 +173,36 @@ static int avf_report_open_error(int err, const char *message)
171173
devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
172174
}
173175

174-
return devices;
176+
if (devices == nil || [devices count] == 0)
177+
{
178+
return devices;
179+
}
180+
181+
ordered = [NSMutableArray arrayWithCapacity:[devices count]];
182+
if (ordered == nil)
183+
{
184+
return devices;
185+
}
186+
187+
for (device in devices)
188+
{
189+
if ([device.deviceType isEqualToString:
190+
AVCaptureDeviceTypeBuiltInWideAngleCamera])
191+
{
192+
[ordered addObject:device];
193+
}
194+
}
195+
196+
for (device in devices)
197+
{
198+
if (![device.deviceType isEqualToString:
199+
AVCaptureDeviceTypeBuiltInWideAngleCamera])
200+
{
201+
[ordered addObject:device];
202+
}
203+
}
204+
205+
return ordered;
175206
}
176207

177208
static bool avf_parse_device_index(const char *dev_path, unsigned long *index_out)

arch/sim/src/sim/posix/sim_host_v4l2.c

Lines changed: 16 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include <stdlib.h>
3232
#include <stdint.h>
3333
#include <string.h>
34-
#include <dirent.h>
3534
#include <limits.h>
3635
#include <sys/stat.h>
3736
#include <syslog.h>
@@ -47,6 +46,7 @@
4746
****************************************************************************/
4847

4948
#define MAX_REQBUFS 3
49+
#define HOST_MAX_VIDEO_DEVICES 64
5050

5151
#define WARN(fmt, ...) \
5252
syslog(LOG_WARNING, "sim_host_video: " fmt "\n", ##__VA_ARGS__)
@@ -120,37 +120,24 @@ static bool host_video_is_capture_device(const char *host_video_dev_path)
120120
return available;
121121
}
122122

123-
static int host_video_get_device_path_by_index(const char *dirpath,
124-
int index,
123+
static int host_video_get_device_path_by_index(int index,
125124
char *devpath,
126125
size_t devpathlen)
127126
{
128-
DIR *dir;
129-
struct dirent *entry;
130127
int count = 0;
128+
int i;
131129

132-
dir = opendir(dirpath);
133-
if (dir == NULL)
134-
{
135-
return -errno;
136-
}
137-
138-
while ((entry = readdir(dir)) != NULL)
130+
for (i = 0; i < HOST_MAX_VIDEO_DEVICES; i++)
139131
{
140-
char path[PATH_MAX];
141-
142-
if (strncmp(entry->d_name, "video", 5) != 0)
143-
{
144-
continue;
145-
}
132+
char path[32];
146133

147-
if (snprintf(path, sizeof(path), "/dev/%s", entry->d_name) >=
134+
if (snprintf(path, sizeof(path), "/dev/video%d", i) >=
148135
(int)sizeof(path))
149136
{
150137
continue;
151138
}
152139

153-
if (!host_video_is_capture_device(path))
140+
if (access(path, F_OK) != 0 || !host_video_is_capture_device(path))
154141
{
155142
continue;
156143
}
@@ -159,55 +146,40 @@ static int host_video_get_device_path_by_index(const char *dirpath,
159146
{
160147
if (snprintf(devpath, devpathlen, "%s", path) >= devpathlen)
161148
{
162-
closedir(dir);
163149
return -ENAMETOOLONG;
164150
}
165151

166-
closedir(dir);
167152
return 0;
168153
}
169154

170155
count++;
171156
}
172157

173-
closedir(dir);
174158
return -ENODEV;
175159
}
176160

177-
static int host_video_count_devices_in_dir(const char *dirpath)
161+
static int host_video_count_devices(void)
178162
{
179-
DIR *dir;
180-
struct dirent *entry;
181163
int count = 0;
164+
int i;
182165

183-
dir = opendir(dirpath);
184-
if (dir == NULL)
185-
{
186-
return -errno;
187-
}
188-
189-
while ((entry = readdir(dir)) != NULL)
166+
for (i = 0; i < HOST_MAX_VIDEO_DEVICES; i++)
190167
{
191-
char devpath[PATH_MAX];
168+
char devpath[32];
192169

193-
if (strncmp(entry->d_name, "video", 5) != 0)
194-
{
195-
continue;
196-
}
197-
198-
if (snprintf(devpath, sizeof(devpath), "/dev/%s", entry->d_name) >=
170+
if (snprintf(devpath, sizeof(devpath), "/dev/video%d", i) >=
199171
(int)sizeof(devpath))
200172
{
201173
continue;
202174
}
203175

204-
if (host_video_is_capture_device(devpath))
176+
if (access(devpath, F_OK) == 0 &&
177+
host_video_is_capture_device(devpath))
205178
{
206179
count++;
207180
}
208181
}
209182

210-
closedir(dir);
211183
return count;
212184
}
213185

@@ -241,15 +213,8 @@ static int host_video_resolve_device_path(const char *host_video_dev_path,
241213
return -EINVAL;
242214
}
243215

244-
ret = host_video_get_device_path_by_index("/sys/class/video4linux", index,
245-
resolved_path,
216+
ret = host_video_get_device_path_by_index(index, resolved_path,
246217
resolved_path_len);
247-
if (ret == -ENOENT || ret == -ENODEV || ret == -EINVAL)
248-
{
249-
ret = host_video_get_device_path_by_index("/dev", index,
250-
resolved_path,
251-
resolved_path_len);
252-
}
253218

254219
if (ret < 0)
255220
{
@@ -266,21 +231,7 @@ static int host_video_resolve_device_path(const char *host_video_dev_path,
266231

267232
int host_video_get_device_count(void)
268233
{
269-
int count;
270-
271-
count = host_video_count_devices_in_dir("/sys/class/video4linux");
272-
if (count >= 0)
273-
{
274-
return count;
275-
}
276-
277-
count = host_video_count_devices_in_dir("/dev");
278-
if (count >= 0)
279-
{
280-
return count;
281-
}
282-
283-
return 0;
234+
return host_video_count_devices();
284235
}
285236

286237
bool host_video_is_available(const char *host_video_dev_path)

tools/mkallsyms.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ if [ -f "${1}" ];then
6464
${nm} -n ${1} | grep -E " [T|t] " | uniq | \
6565
while read addr type name
6666
do
67-
echo " { \"$(${filt} $name | sed -e "s/(.*)$//")\", (FAR ${CONST} void *)0x$addr },"
67+
demangled=$(printf '%s\n' "$name" | ${filt} | sed -e "s/(.*)$//")
68+
echo " { \"${demangled}\", (FAR ${CONST} void *)0x$addr },"
6869
done
6970
fi
7071

0 commit comments

Comments
 (0)