From 727feda935952e118519f193fc79d7f9aa12d632 Mon Sep 17 00:00:00 2001 From: Vadim Levin Date: Mon, 12 Dec 2022 10:41:39 +0300 Subject: [PATCH] fix: AVFoundation inconsistent camera indices --- modules/videoio/src/cap_avfoundation_mac.mm | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/modules/videoio/src/cap_avfoundation_mac.mm b/modules/videoio/src/cap_avfoundation_mac.mm index 3049b4ba6e..504bdb9ce9 100644 --- a/modules/videoio/src/cap_avfoundation_mac.mm +++ b/modules/videoio/src/cap_avfoundation_mac.mm @@ -376,6 +376,15 @@ int CvCaptureCAM::startCaptureDevice(int cameraNum) { return 0; } + // Preserve devices ordering on the system + // see AVCaptureDevice::uniqueID property documentation for more info + devices = [devices + sortedArrayUsingComparator:^NSComparisonResult(AVCaptureDevice *d1, + AVCaptureDevice *d2) { + return [d1.uniqueID compare:d2.uniqueID]; + } + ]; + mCaptureDevice = devices[cameraNum]; if ( ! mCaptureDevice ) {