1
function PlayMoviesDemoOSX(moviename, backgroundMaskOut, tolerance)
3
% PlayMoviesDemoOSX(moviename [, backgroundMaskOut][, tolerance])
5
% This demo accepts a pattern for a valid moviename, e.g.,
6
% moviename='*.mpg', then it plays all movies in the current working
7
% directory whose names match the provided pattern, e.g., the '*.mpg'
8
% pattern would play all MPEG files in the current directory.
10
% If you don't specify a moviename, the demo will ask you if it should play
11
% our standard DualDiscs.mov demo movie, or rather play through a set of
12
% videos in a playlist which are streamed from the internet. These 'c'ool
13
% videos may provide you with useful information for your daily work.
15
% This demo uses automatic asynchronous playback for synchronized playback
16
% of video and sound. Each movie plays until end, then rewinds and plays
17
% again from the start. Pressing the Cursor-Up/Down key pauses/unpauses the
18
% movie and increases/decreases playback rate.
19
% The left- right arrow keys jump in 1 seconds steps. SPACE jumps to the
20
% next movie in the list. ESC ends the demo.
22
% If the optional RGB color vector backgroundMaskOut is provided, then
23
% color pixels in the video which are equal or close to backgroundMaskOut will be
24
% discarded during drawing. E.g., backgroundMaskOut = [255 255 255] would
25
% discard all white pixels, backgroundMaskOut = [0 0 0] would discard all
26
% black pixels etc. The optional tolerance parameter allows for some
27
% lenience, e.g., tolerance = 10 would discard all pixels whose euclidean
28
% distance in RGB color space is less than 10 units to the backgroundMaskOut
29
% color. Background color masking requires a graphics card with fragment
30
% shader support and will fail otherwise.
34
% 10/30/05 mk Wrote it.
35
% 07/17/11 mk Add support for background pixel color removal via shaders.
36
% Code cleanup, dead code removal.
37
% 04/14/12 mk Code cleanup, refinements for network video streams.
38
% Add useful videos to playlist which are less pathetic than
39
% the fairy-tales of the iPhone company.
43
if (nargin < 1) || isempty(moviename)
45
theanswer = input('Serious or cool? Type s or c [s/c]? ', 's');
52
% Switch KbName into unified mode: It will use the names of the OS-X
53
% platform on all platforms in order to make this script portable:
54
KbName('UnifyKeyNames');
56
space=KbName('SPACE');
58
right=KbName('RightArrow');
59
left=KbName('LeftArrow');
61
down=KbName('DownArrow');
62
shift=KbName('RightShift');
67
background=[128, 128, 128];
69
% Open onscreen window:
70
screen=max(Screen('Screens'));
71
win = Screen('OpenWindow', screen, 0);
74
if (nargin > 1) && ~isempty(backgroundMaskOut)
78
shader = CreateSinglePassImageProcessingShader(win, 'BackgroundMaskOut', backgroundMaskOut, tolerance);
81
% Clear screen to background color:
82
Screen('FillRect', win, background);
84
% Initial display and sync to timestamp:
89
% Use blocking wait for new frames by default:
92
% Default preload setting:
95
if isempty(strfind(moviename, 'http'))
96
% Return full list of movie files from directory+pattern:
97
moviefiles=dir(moviename);
99
if isempty(moviefiles)
100
moviefiles(1).name = [ PsychtoolboxRoot 'PsychDemos/QuicktimeDemos/DualDiscs.mov' ];
102
for i=1:size(moviefiles,1)
103
moviefiles(i).name = [ pwd filesep moviefiles(i).name ];
107
moviecount = size(moviefiles,1);
109
moviefiles(1).name = moviename;
113
if strcmpi(theanswer, 'c')
114
% Cool stuff, streaming from the web ;-)
117
% On GStreamer setups we add a few more movies to the playlist, before the pathetic Apple
118
% commercials. Can't use them on OS/X yet, as Apple's QT engine does not handle them
119
% with the default codec set:
120
if IsLinux || (Screen('Preference', 'OverrideMultimediaEngine') == 1)
121
% Promotional videos for the best OS for cognitive science and technical/educational
122
% videos some users may find of practical use:
124
% ELC 2012 talk: Gstreamer-1.0 No-longer-compromise-flexibility-for-performance:
125
moviefiles(end+1).name = 'http://d17mmld7179ppq.cloudfront.net/gstreamer-10-no-longer-compromise-flexibility-for-performance_52ca47/hd_ready.webm';
127
% FOSDEM 2012 talk about Linux's next generation graphics display server "Wayland":
128
moviefiles(end+1).name = 'http://video.fosdem.org/2012/maintracks/k.1.105/Wayland.webm';
131
moviefiles(end+1).name = 'http://movies.apple.com/movies/us/apple/getamac/apple_getamac_group_20080512_480x272.mov';
132
moviefiles(end+1).name = 'http://movies.apple.com/movies/us/apple/getamac/apple_getamac_sadsong_extended_20080519_480x272.mov';
133
moviefiles(end+1).name = 'http://movies.apple.com/movies/us/apple/getamac/apple_getamac_breakthrough_20080401_480x272.mov';
134
moviefiles(end+1).name = 'http://movies.apple.com/movies/us/apple/getamac/apple-getamac-fat_480x376.mov';
135
moviefiles(end+1).name = 'http://movies.apple.com/movies/us/apple/getamac_ads4/prlady_480x272.mov';
137
% Count all movies in our playlist:
138
moviecount = size(moviefiles,2);
140
% Use polling to wait for new frames when playing movies from the
141
% internet. This to make sure we don't time out too early or block
142
% for too long if the network connection is slow / high-latency / bad.
145
% For network playback we use a higher than default caching time:
149
% Playbackrate defaults to 1:
152
% Endless loop, runs until ESC key pressed:
154
iteration=iteration + 1;
155
fprintf('ITER=%i::', iteration);
156
moviename=moviefiles(mod(iteration, moviecount)+1).name;
158
% Open movie file and retrieve basic info about movie:
159
[movie movieduration fps imgw imgh] = Screen('OpenMovie', win, moviename, [], preloadsecs);
160
fprintf('Movie: %s : %f seconds duration, %f fps, w x h = %i x %i...\n', moviename, movieduration, fps, imgw, imgh);
164
% Start playback of movie. This will start
165
% the realtime playback clock and playback of audio tracks, if any.
166
% Play 'movie', at a playbackrate = 1, with endless loop=1 and
167
% 1.0 == 100% audio volume.
168
Screen('PlayMovie', movie, rate, 1, 1.0);
172
% Infinite playback loop: Fetch video frames and display them...
174
% Check for abortion:
176
[keyIsDown,secs,keyCode]=KbCheck;
177
if (keyIsDown==1 && keyCode(esc))
178
% Set the abort-demo flag.
183
% Check for skip to next movie:
184
if (keyIsDown==1 && keyCode(space))
185
% Exit while-loop: This will load the next movie...
189
% Only perform video image fetch/drawing if playback is active
190
% and the movie actually has a video track (imgw and imgh > 0):
191
if ((abs(rate)>0) && (imgw>0) && (imgh>0))
192
% Return next frame in movie, in sync with current playback
194
% tex is either the positive texture handle or zero if no
195
% new frame is ready yet in non-blocking mode (blocking == 0).
196
% It is -1 if something went wrong and playback needs to be stopped:
197
tex = Screen('GetMovieImage', win, movie, blocking);
199
% Valid texture returned?
201
% No, and there won't be any in the future, due to some
202
% error. Abort playback loop:
207
% No new frame in polling wait (blocking == 0). Just sleep
208
% a bit and then retry.
209
WaitSecs('YieldSecs', 0.005);
213
% Draw the new texture immediately to screen:
214
Screen('DrawTexture', win, tex, [], [], [], [], [], [], shader);
220
Screen('Close', tex);
226
% Further keyboard checks...
228
if (keyIsDown==1 && keyCode(right))
229
% Advance movietime by one second:
230
Screen('SetMovieTimeIndex', movie, Screen('GetMovieTimeIndex', movie) + 1);
233
if (keyIsDown==1 && keyCode(left))
234
% Rewind movietime by one second:
235
Screen('SetMovieTimeIndex', movie, Screen('GetMovieTimeIndex', movie) - 1);
238
if (keyIsDown==1 && keyCode(up))
239
% Increase playback rate by 1 unit.
246
Screen('PlayMovie', movie, rate, 1, 1.0);
249
if (keyIsDown==1 && keyCode(down))
250
% Decrease playback rate by 1 unit.
254
while KbCheck; WaitSecs(0.01); end;
257
Screen('PlayMovie', movie, rate, 1, 1.0);
261
telapsed = GetSecs - t1;
262
fprintf('Elapsed time %f seconds, for %i frames.\n', telapsed, i);
267
% Done. Stop playback:
268
Screen('PlayMovie', movie, 0);
270
% Close movie object:
271
Screen('CloseMovie', movie);
280
% Error handling: Close all windows and movies, release all ressources.