File: PsychGetCamIdForSpec.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (111 lines) | stat: -rw-r--r-- 3,180 bytes parent folder | download | duplicates (6)
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
function [devid, dev] = PsychGetCamIdForSpec(className, inputNameOrPort, instance, engineId)
% Return deviceIndex of a specified camera, one that matches given criteria.
% 
% [deviceIndex, dev] = PsychGetCamIdForSpec([className][, inputNameOrPort][, instance][, engineId]);
%
% Searches for video sources which match given criteria. A handle to the
% first source that satisfies the criteria is returned in argument
% 'deviceIndex' - You can open a connection to the source via
% Screen('OpenVideoCapture', windowPtr, deviceIndex, ...);
%
% The 2nd optional return argument contains the complete 'dev'ice
% description struct, as returned by Screen().
%
% Returns empty variables if no match can be made.
%
% Optional criteria:
%
% 'className' Index or name of video input device class: Default is to
% accept any class. This matches against the 'ClassName' property of the
% list returned by Screen('VideoCaptureDevices').
%
% 'inputNameOrPort' selects the i'th input device of a matching class if an
% index is given (zero-based), or a specific named device, e.g., 'iSight'
% for the builtin iSight camera of Apple hardware.
%
% 'instance' If multiple devices match, take the i'th device where i ==
% instance. By default, the first device (instance == 0) is assigned.
%
% 'engineId' Enumerate for video capture engine 'engineId'. By default, the
% default videocapture engine is used.
%

%
% History:
% 9.5.2009  mk Written.

if nargin < 1
    className = [];
end

if nargin < 2
    inputNameOrPort = [];
end

if nargin < 3
    instance = [];
end

if isempty(instance)
    instance = 0;
end

if nargin < 4
    engineId = [];
end

cams = Screen('VideoCaptureDevices', engineId);
devid = [];
dev = [];

curinst = -1;

for i=1:length(cams)
    if ~isempty(className)
        if isnumeric(className)
            if cams(i).ClassIndex ~= className
                % ClassIndex doesn't match: Reject.
                continue;
            end
        else
            if isempty(strfind(cams(i).ClassName, className))
                % Classname doesn't match: Reject.
                continue;
            end
        end
    end
    
    if ~isempty(inputNameOrPort)
        if isnumeric(inputNameOrPort)
            if cams(i).InputIndex ~= inputNameOrPort
                % InputIndex doesn't match: Reject.
                continue;
            end
        else
            if isfield(cams(i), 'InputName') && isempty(strfind(cams(i).InputName, inputNameOrPort))
                % InputName doesn't match: Reject.
                continue;
            end

            if isfield(cams(i), 'DeviceName') && isempty(strfind(cams(i).DeviceName, inputNameOrPort))
                % InputName doesn't match: Reject.
                continue;
            end
        end
    end
    
    % Matching criteria satisfied. Matching instance?
    curinst = curinst + 1;
    
    if curinst == instance
        % Assign deviceIndex for matched device:
        devid = cams(i).DeviceIndex;
        dev = cams(i);
        break;
    end
end

% Ok, either devid is empty if no matching device could be found, or we
% have a unique deviceIndex for use with Screen('OpenVideoCapture').

return;