/usr/share/psychtoolbox-3/PsychDemos/VideoDVCamCaptureDemo.m is in psychtoolbox-3-common 3.0.12.20160126.dfsg1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | function VideoDVCamCaptureDemo(fullscreen, fullsize, roi, depth, deviceId, moviename)
% Demonstrate simple use of built-in video capture engine with DV consumer cameras.
%
% VideoDVCamCaptureDemo([fullscreen=0][, fullsize=0][, roi][, depth][,deviceId=0][, moviename])
%
% NOTE: As of October 2014, DV video camera capture has not been tested at
% all on MS-Windows, as GStreamer-1 currently doesn't ship with video
% capture plugins for MS-Windows. On Apple OSX 10.9.5 Mavericks with our
% new GStreamer-1 video capture engine, video capture from DV did not work,
% neither with Psychtoolbox, nor with GStreamer command line tools or other
% independent open source video capture applications. Only Apples Facetime
% app managed to get a marginally useable video stream from the DV camera.
% Testing on Linux with this specific demo showed mixed results. However on
% Linux there is a slightly hacky way that makes it work somewhat reliably
% with the new backend by exposing the camera as a regular video source, so
% all regular video capture/recording/processing demos can be used for DV
% capture, without any need for special treatment like in this demo. Read
% the section below for further instructions for a reliable DV setup on
% Linux.
%
% VideoDVCamCaptureDemo initializes the first attached and supported DV
% firewire consumer camera, then shows its video image in a Psychtoolbox
% window. A press of the ESCape key ends the demo.
%
% Optional parameters:
%
% 'fullscreen' If set to non-zero value, the image is displayed in a
% fullscreen window, as usual, otherwise a normal GUI window is used.
%
% 'fullsize' If set to 1, the cameras image is scaled up to full screen
% resolution, ie. so it fills the maximum amount of display area, but
% preserving the original aspect ratio.
%
% 'roi' Set to [0 0 720 576] for a PAL-DV camera and [0 0 720 480] for a
% NTSC-DV camera. Default is PAL if omitted.
%
% 'deviceId' Device index of video capture device. Defaults to system
% default. You can also specify a gst-launch style string to define a
% videosource here. Or you can set the special string deviceId = 'X' so
% builtin spec strings suitable for each operating system will be used.
%
% 'moviename' Name string for selection of filename of a target movie file
% to which video should be recorded. Defaults to none,ie., no video
% recording.
%
% VideoDVCamCaptureDemo also allows you to test out video capture from
% special video sources other than Consumer-DV cameras, ie. sources which
% require use of a custom built GStreamer video source bin. You test such
% setups by specifying the bin spec-string and other parameters as parameters
% for the demo. As an example, the following call would try to capture a video
% stream that is encoded as H264 video and that is transmitted over the network
% via TCP-IP protocol, ie., a H264 video stream encapsulated in TCP:
%
% VideoDVCamCaptureDemo([], [], [0 0 320 240], 6, 'tcpclientsrc port=8554 host=localhost ! h264parse ! avdec_h264 name=ptbdvsource');
%
% This would receive the TCP stream from port 8554 on the machine with
% the name 'localhost' (the local machine). The stream would decode from
% H264 to color format 6 - YUV-I420 - with video frames of 320 x 240 pixels
% size. For testing purpose you could enter the following GStreamer command
% line into a terminal window to generate a test video stream and send it from
% the local machine 'localhost' on port 8554, as H264 encoded TCP-IP stream,
% then receive it via VideoDVCamCaptureDemo as shown above:
%
% gst-launch-1.0 videotestsrc horizontal-speed=5 ! x264enc tune="zerolatency" threads=1 ! video/x-h264,stream-format=byte-stream ! tcpserversink port=8554
%
% One application of such a custom setup can be seen in the discussion thread
% containing message #18807 on the Psychtoolbox forum. There the video source
% is an IP camera attached to a robot, streaming H264 video over the network for
% consumption by a machine running Psychtoolbox.
%
%
%
%
% Loopback setup on Linux for use with new GStreamer-1 video backend:
%
% This specific configuration was shown to work at least on Ubuntu 14.04
% LTS with a Sony PAL-DV firewire camera. After following the setup steps,
% demos like our standard VideoCaptureDemo, VideoRecordingDemo, ... worked
% without any special configuration or treatment of DV cameras.
%
% Here you need to install a Video4Linux2 loopback kernel module. It will
% allow to create virtual video sources, from which Psychtoolbox can
% read/capture/process record live video. Then some external application
% can feed video into those virtual sources. You then attach an external
% command line DV capture session as video source.
%
% 1. Install the package "v4l2loopback-dkms" to get the kernel module installed and
% loaded. A "sudo apt-get install v4l2loopback-dkms" on Ubuntu 14.04-LTS
% and later distributions should do the trick. The package is probably
% also available on Debian, other Debian/Ubuntu derived distros etc. Or
% you get the most recent version to compile and install from source
% code from the homepage of the project:
% https://github.com/umlaeute/v4l2loopback
%
% 2. You may or may not need to "sudo modprobe v4l2loopback" on first use.
%
% 3. Then you use a GStreamer video capture pipeline launched from a terminal
% window to connect to your DV camera, capture live video and feed it
% into the virtual video loopback device. An example launch line can
% look like this:
%
% gst-launch dv1394src ! dvdemux ! dvdec ! v4l2sink device=/dev/video0
%
% This would make live video from the first connected DV camera
% available on /dev/video0. See
% https://github.com/umlaeute/v4l2loopback/wiki for more detailed
% instructions.
%
% If this doesn't work for you with GStreamer-1 you may need to install
% good old GStreamer-0.10 in addition to the already installed
% GStreamer-1 and instead use the gst-launch-0.10 command instead of the
% gst-launch command to select for the old implementation.
%
% 4. Psychtoolbox video capture functions should now report and be able to
% use a new virtual video capture device with a name like "Dummy video
% device 0000" or some name defined by you. Psychtoolbox should be able
% to video capture or record video from that device aka your DV video
% camera.
%
% The Wiki of v4l2loopback describes more elaborate setups, e.g., for
% capturing from multiple video DV cameras.
%
% History:
% 27-Dec-2013 mk Written.
% 26-Aug-2014 mk Adapted to GStreamer-1.4.0+ backend.
% 19-Sep-2014 mk Update instructions for GStreamer-1.x, drop dead code for GStreamer-1.
% 05-Oct-2014 mk Clarify OSX doesn't work at all. Deuglify help text formatting a bit.
% 02-Mar-2015 mk Add example use case "video streaming over TCP network" to help text.
PsychDefaultSetup(1);
if nargin < 1
fullscreen=[];
end
if isempty(fullscreen)
fullscreen=0;
end
if nargin < 2
fullsize=[];
end
if isempty(fullsize)
fullsize=0;
end
if nargin < 3 || isempty(roi)
% Must set explicit roi with video resolution. Only two options,
% 720 x 576 for PAL DV cameras, and 720 x 480 for NTSC DV cameras.
% We choose PAL:
roi = [0 0 720 576];
end
if nargin < 4
depth = [];
end
if nargin < 5
deviceId = [];
end
if nargin < 6
moviename = [];
end
recordingflags = 0;
screenid=max(Screen('Screens'));
try
if fullscreen<1
win=Screen('OpenWindow', screenid, 0, [0 0 800 600]);
else
win=Screen('OpenWindow', screenid, 0);
end;
% Initial flip to a blank screen:
Screen('Flip',win);
% Set text size for info text to 24 pixels.
Screen('TextSize', win, 24);
% Use of a gst-launch style spec for a video source requested? A deviceId which
% is actually a character string would signal this:
if ischar(deviceId)
% Yes. Meaningful spec string, or only a single dummy character?
if length(deviceId) < 2
% Dummy: Replace by hard-coded "known to work" example string:
if IsLinux
% Any of these work on Linux:
capturebinspec = 'dv1394src ! dvdemux ! dvdec name=ptbdvsource';
%capturebinspec = 'dv1394src ! video/x-dv ! dvdemux ! dvdec name=ptbdvsource';
%capturebinspec = 'dv1394src ! dvdemux ! avdec_dvvideo name=ptbdvsource';
end
if IsWin
% This should work on MS-Windows, as tested with a Sony PAL-DV camera:
capturebinspec = 'dshowvideosrc ! dvdemux ! ffdec_dvvideo name=ptbdvsource';
if Is64Bit
warning('To our knowledge (as of August 2014, GStreamer-1.4.0) video capture is not yet\nsupported on MS-Windows, so this will likely fail.\n');
end
end
if IsOSX
% This should work on OSX, if at all. If this doesn't work then game-over (Untested):
warning('To our knowledge (as of October 2014, GStreamer-1.4.0) video capture from DV cameras on OSX does not work correctly, so this will likely fail.\n');
capturebinspec = 'avfvideosrc name=ptbdvsource';
end
else
% Real string: Use it.
capturebinspec = deviceId;
end
if isempty(roi)
error('You *must* specify a roi of [0 0 720 576] for PAL-DV or [0 0 720 480] for NTSC-DV if you define your own video capture source via deviceId as a string!');
end
% Assign capturebinspec as gst-launch style capture bin spec for use as video source:
Screen('SetVideoCaptureParameter', -1, sprintf('SetNextCaptureBinSpec=%s', capturebinspec));
% Signal to Screen() that spec string should be used. This via special deviceId -9:
deviceId = -9;
end
% Open DV camera: We always use engineId 3 for GStreamer, as only GStreamer supports DV cameras atm.:
grabber = Screen('OpenVideoCapture', win, deviceId, roi, depth, [], [], moviename, recordingflags, 3);
% We refrain from setting target fps here, as DV cameras do have a fixed fps. Instead
% we use the "do what you think is right" 'realmax' joker.
Screen('StartVideoCapture', grabber, realmax, 1);
dstRect = [];
oldpts = 0;
count = 0;
t=GetSecs;
% Run for at most 600 seconds or until keypress:
while (GetSecs - t) < 600
if KbCheck(-1)
break;
end
[tex pts nrdropped,intensity]=Screen('GetCapturedImage', win, grabber, 1); %#ok<ASGLU,NASGU>
% fprintf('tex = %i pts = %f nrdropped = %i\n', tex, pts, nrdropped);
if tex > 0
% Perform first-time setup of transformations, if needed:
if fullsize && (count == 0)
texrect = Screen('Rect', tex);
winrect = Screen('Rect', win);
sf = min([RectWidth(winrect) / RectWidth(texrect) , RectHeight(winrect) / RectHeight(texrect)]);
dstRect = CenterRect(ScaleRect(texrect, sf, sf) , winrect);
end
% texinfo = Screen('Getwindowinfo', tex)
% outintens = intensity
% Draw new texture from framegrabber.
Screen('DrawTexture', win, tex, [], dstRect);
% Print pts:
Screen('DrawText', win, sprintf('%.4f', pts), 0, 0, 255);
if count > 0
% Compute delta:
delta = (pts - oldpts) * 1000;
oldpts = pts;
Screen('DrawText', win, sprintf('%.4f', delta), 0, 20, 255);
end
% Show it.
Screen('Flip', win);
Screen('Close', tex);
end
count = count + 1;
end
telapsed = GetSecs - t %#ok<NOPRT>
Screen('StopVideoCapture', grabber);
Screen('CloseVideoCapture', grabber);
Screen('CloseAll');
avgfps = count / telapsed %#ok<NOPRT,NASGU>
catch %#ok<CTCH>
Screen('CloseAll');
end
|