如何在Mac OS上获取视频捕获设备(网络摄像头)列表?(C++)

5

所需内容很简单 - 当前可用的视频捕获设备列表(网络摄像头)。我需要一个简单的或C++控制台应用程序。列表的意思是类似于这样的控制台输出:

1) Asus Web Camera
2) Sony Web Camera

似乎很简单,但是我有一个要求——尽可能使用本机操作系统API,不使用外部库。毕竟,我们只想打印出一个列表,而不是飞到月球!请不要使用Objective-C,只使用纯C/C++。

如何实现这样的事情呢?


同系列文章:


1
你不允许使用glibc让生活变得非常困难。直接调用系统调用(也就是操作系统API)并不好玩。 - Ben Voigt
@Ben Voigt:您说只使用glibc就可以实现吗? - Rella
从理论上讲,可以不使用任何库来实现。理论上可以使用纯汇编重写OS-X。然而,这将是一场浪费时间的巨大行动,因为你会花费大量时间在程序中复制库代码,而当你可以直接链接它时。我想说的是,说“我想尽量减少依赖性”是可以的,但说“完全不使用外部库”有点荒谬。 - Ben Voigt
1
@Ben:我觉得他只是想尽可能简单直接地完成,而不使用第三方库,对吗? - Ken Aspeslagh
2个回答

4
你需要使用SGGetChannelDeviceList函数,它是QuickTime C API的一部分。每个设备可以有多个输入。正确的解析方式如下:
    // first get a video channel from the sequence grabber

   ComponentDescription    theDesc;
   Component               sgCompID;
   ComponentResult         result;
   theDesc.componentType           = SeqGrabComponentType;
   theDesc.componentSubType        = 0L;
   theDesc.componentManufacturer   = 'appl';
   theDesc.componentFlags          = 0L;
   theDesc.componentFlagsMask      = 0L;   
   sgCompID = FindNextComponent (NULL, &theDesc);
   seqGrabber = OpenComponent (sgCompID);
   result = SGInitialize (seqGrabber);
   result = SGNewChannel (seqGrabber, VideoMediaType, &videoChannel);
   SGDeviceList  theDevices;
   SGGetChannelDeviceList(videoChannel, sgDeviceListDontCheckAvailability | sgDeviceListIncludeInputs, &theDevices);

    if (theDevices)
    {
        int theDeviceIndex;
        for (theDeviceIndex = 0; theDeviceIndex != (*theDevices)->count; ++theDeviceIndex)
        {
            SGDeviceName theDeviceEntry = (*theDevices)->entry[theDeviceIndex];
            // name of device is a pstring in theDeviceEntry.name

        SGDeviceInputList theInputs = theDeviceEntry.inputs;
            if (theInputs != NULL)
            {
                int theInputIndex;
                for ( theInputIndex = 0; theInputIndex != (*theInputs)->count; ++theInputIndex)
                {
                    SGDeviceInputName theInput = (*theInputs)->entry[theInputIndex];
                    // name of input is a pstring in theInput.name
                }
            }
        }       
    }

需要哪些头文件? - Rella
QuickTime.h 你知道苹果网站上有很多示例代码可以帮助你入门。 - Ken Aspeslagh
请注意,C API已被弃用。您应该使用Objective-C来处理此类事情。 - Ken Aspeslagh
你尝试过编译这段代码吗?因为它似乎无法编译=( 我该怎么办?你能提供可编译的C++代码吗? - Rella

0

显然你可以在终端中使用system_profiler SPUSBDataType,这就是答案:

USB:

    USB 3.1 Bus:

      Host Controller Driver: AppleT8103USBXHCI

        USB Camera-OV580:

          Product ID: 0x058a
          Vendor ID: 0x05a9  (OmniVision Technologies, Inc.)
          Version: 1.00
          Speed: Up to 5 Gb/s
          Manufacturer: Omnivision Technologies, Inc.
          Location ID: 0x01200000 / 1
          Current Available (mA): 900
          Current Required (mA): 512
          Extra Operating Current (mA): 0

    USB 3.1 Bus:

      Host Controller Driver: AppleT8103USBXHCI

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接