Skip to content

Commit f35bb9b

Browse files
authored
Sort devices by name, ensure FaceTime camera is listed first. closes #8377 (#8392)
1 parent 2375f41 commit f35bb9b

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

libs/openFrameworks/video/ofAVFoundationGrabber.mm

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@ - (BOOL)initCapture:(int)framerate capWidth:(int)w capHeight:(int)h{
5858
devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
5959
#pragma clang diagnostic pop
6060
}
61+
62+
if([devices count] > 1) {
63+
// Sort devices: "FaceTime" devices first, then alphabetically
64+
devices = [devices sortedArrayUsingComparator:^NSComparisonResult(AVCaptureDevice *d1, AVCaptureDevice *d2) {
65+
NSString *name1 = d1.localizedName;
66+
NSString *name2 = d2.localizedName;
67+
68+
BOOL isFaceTime1 = [name1 hasPrefix:@"FaceTime"];
69+
BOOL isFaceTime2 = [name2 hasPrefix:@"FaceTime"];
70+
71+
if (isFaceTime1 && !isFaceTime2) {
72+
return NSOrderedAscending; // FaceTime first
73+
} else if (!isFaceTime1 && isFaceTime2) {
74+
return NSOrderedDescending; // FaceTime first
75+
} else {
76+
// Otherwise alphabetical
77+
return [name1 compare:name2];
78+
}
79+
}];
80+
}
81+
6182
if([devices count] > 0) {
6283
if(deviceID>[devices count]-1)
6384
deviceID = [devices count]-1;
@@ -284,6 +305,26 @@ -(CGImageRef)getCurrentFrame{
284305
#pragma clang diagnostic pop
285306
}
286307

308+
if([devices count] > 1) {
309+
// Sort devices: "FaceTime" devices first, then alphabetically
310+
devices = [devices sortedArrayUsingComparator:^NSComparisonResult(AVCaptureDevice *d1, AVCaptureDevice *d2) {
311+
NSString *name1 = d1.localizedName;
312+
NSString *name2 = d2.localizedName;
313+
314+
BOOL isFaceTime1 = [name1 hasPrefix:@"FaceTime"];
315+
BOOL isFaceTime2 = [name2 hasPrefix:@"FaceTime"];
316+
317+
if (isFaceTime1 && !isFaceTime2) {
318+
return NSOrderedAscending; // FaceTime first
319+
} else if (!isFaceTime1 && isFaceTime2) {
320+
return NSOrderedDescending; // FaceTime first
321+
} else {
322+
// Otherwise alphabetical
323+
return [name1 compare:name2];
324+
}
325+
}];
326+
}
327+
287328
int i=0;
288329
for (AVCaptureDevice * captureDevice in devices){
289330
deviceNames.push_back([captureDevice.localizedName UTF8String]);

0 commit comments

Comments
 (0)