~ubuntu-branches/ubuntu/trusty/psychtoolbox-3/trusty-proposed

« back to all changes in this revision

Viewing changes to Psychtoolbox/PsychDemos/QuicktimeDemos/PlayMoviesDemoOSX.m

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2013-11-19 23:34:50 UTC
  • mfrom: (3.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20131119233450-f7nf92vb8qavjmk8
Tags: 3.0.11.20131017.dfsg1-3
Upload to unsable since fresh glew has arrived to sid!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
function PlayMoviesDemoOSX(moviename, backgroundMaskOut, tolerance)
2
 
%
3
 
% PlayMoviesDemoOSX(moviename [, backgroundMaskOut][, tolerance])
4
 
%
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.
9
 
%
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.
14
 
%
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.
21
 
%
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.
31
 
%
32
 
 
33
 
% History:
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.
40
 
 
41
 
theanswer = [];
42
 
 
43
 
if (nargin < 1) || isempty(moviename) 
44
 
    moviename = [];
45
 
    theanswer = input('Serious or cool? Type s or c [s/c]? ', 's');
46
 
end;
47
 
 
48
 
if isempty(moviename)
49
 
    moviename = '*.mov';
50
 
end
51
 
 
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');
55
 
 
56
 
space=KbName('SPACE');
57
 
esc=KbName('ESCAPE');
58
 
right=KbName('RightArrow');
59
 
left=KbName('LeftArrow');
60
 
up=KbName('UpArrow');
61
 
down=KbName('DownArrow');
62
 
shift=KbName('RightShift');
63
 
 
64
 
try
65
 
    % Child protection
66
 
    AssertOpenGL;
67
 
    background=[128, 128, 128];
68
 
 
69
 
    % Open onscreen window:
70
 
    screen=max(Screen('Screens'));
71
 
    win = Screen('OpenWindow', screen, 0);
72
 
 
73
 
    shader = [];
74
 
    if (nargin > 1) && ~isempty(backgroundMaskOut)
75
 
        if nargin < 3
76
 
            tolerance = [];
77
 
        end
78
 
        shader = CreateSinglePassImageProcessingShader(win, 'BackgroundMaskOut', backgroundMaskOut, tolerance);
79
 
    end
80
 
    
81
 
    % Clear screen to background color:
82
 
    Screen('FillRect', win, background);
83
 
    
84
 
    % Initial display and sync to timestamp:
85
 
    Screen('Flip',win);
86
 
    iteration = 0;    
87
 
    abortit = 0;
88
 
 
89
 
    % Use blocking wait for new frames by default:
90
 
    blocking = 1;
91
 
 
92
 
    % Default preload setting:
93
 
    preloadsecs = [];
94
 
 
95
 
    if isempty(strfind(moviename, 'http'))
96
 
        % Return full list of movie files from directory+pattern:
97
 
        moviefiles=dir(moviename);
98
 
        
99
 
        if isempty(moviefiles)
100
 
            moviefiles(1).name = [ PsychtoolboxRoot 'PsychDemos/QuicktimeDemos/DualDiscs.mov' ];
101
 
        else
102
 
            for i=1:size(moviefiles,1)
103
 
                moviefiles(i).name = [ pwd filesep moviefiles(i).name ];
104
 
            end
105
 
        end
106
 
        
107
 
        moviecount = size(moviefiles,1);
108
 
    else
109
 
        moviefiles(1).name = moviename;
110
 
        moviecount = 1;
111
 
    end
112
 
 
113
 
    if strcmpi(theanswer, 'c')
114
 
        % Cool stuff, streaming from the web ;-)
115
 
        moviefiles = [];
116
 
        
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:
123
 
 
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';
126
 
 
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';
129
 
        end
130
 
 
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';
136
 
 
137
 
        % Count all movies in our playlist:
138
 
        moviecount = size(moviefiles,2);
139
 
 
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.
143
 
        blocking = 0;
144
 
 
145
 
        % For network playback we use a higher than default caching time:
146
 
        preloadsecs = 10;
147
 
    end
148
 
 
149
 
    % Playbackrate defaults to 1:
150
 
    rate=1;
151
 
    
152
 
    % Endless loop, runs until ESC key pressed:
153
 
    while (abortit<2)
154
 
        iteration=iteration + 1;
155
 
        fprintf('ITER=%i::', iteration);
156
 
        moviename=moviefiles(mod(iteration, moviecount)+1).name;
157
 
        
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);
161
 
        
162
 
        i=0;
163
 
        
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);
169
 
    
170
 
        t1 = GetSecs;
171
 
        
172
 
        % Infinite playback loop: Fetch video frames and display them...
173
 
        while 1
174
 
            % Check for abortion:
175
 
            abortit=0;
176
 
            [keyIsDown,secs,keyCode]=KbCheck;
177
 
            if (keyIsDown==1 && keyCode(esc))
178
 
                % Set the abort-demo flag.
179
 
                abortit=2;
180
 
                break;
181
 
            end;
182
 
 
183
 
            % Check for skip to next movie:
184
 
            if (keyIsDown==1 && keyCode(space))
185
 
                % Exit while-loop: This will load the next movie...
186
 
                break;
187
 
            end;
188
 
 
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
193
 
                % time and sound.
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);
198
 
 
199
 
                % Valid texture returned?
200
 
                if tex < 0
201
 
                    % No, and there won't be any in the future, due to some
202
 
                    % error. Abort playback loop:
203
 
                    break;
204
 
                end
205
 
 
206
 
                if tex == 0
207
 
                    % No new frame in polling wait (blocking == 0). Just sleep
208
 
                    % a bit and then retry.
209
 
                    WaitSecs('YieldSecs', 0.005);
210
 
                    continue;
211
 
                end
212
 
 
213
 
                % Draw the new texture immediately to screen:
214
 
                Screen('DrawTexture', win, tex, [], [], [], [], [], [], shader);
215
 
 
216
 
                % Update display:
217
 
                Screen('Flip', win);
218
 
 
219
 
                % Release texture:
220
 
                Screen('Close', tex);
221
 
 
222
 
                % Framecounter:
223
 
                i=i+1;
224
 
            end;
225
 
 
226
 
            % Further keyboard checks...
227
 
 
228
 
            if (keyIsDown==1 && keyCode(right))
229
 
                % Advance movietime by one second:
230
 
                Screen('SetMovieTimeIndex', movie, Screen('GetMovieTimeIndex', movie) + 1);
231
 
            end;
232
 
 
233
 
            if (keyIsDown==1 && keyCode(left))
234
 
                % Rewind movietime by one second:
235
 
                Screen('SetMovieTimeIndex', movie, Screen('GetMovieTimeIndex', movie) - 1);
236
 
            end;
237
 
 
238
 
            if (keyIsDown==1 && keyCode(up))
239
 
                % Increase playback rate by 1 unit.
240
 
                if (keyCode(shift))
241
 
                    rate=rate+0.1;
242
 
                else
243
 
                    KbReleaseWait;
244
 
                    rate=round(rate+1);
245
 
                end;
246
 
                Screen('PlayMovie', movie, rate, 1, 1.0);
247
 
            end;
248
 
 
249
 
            if (keyIsDown==1 && keyCode(down))
250
 
                % Decrease playback rate by 1 unit.
251
 
                if (keyCode(shift))
252
 
                    rate=rate-0.1;
253
 
                else
254
 
                    while KbCheck; WaitSecs(0.01); end;
255
 
                    rate=round(rate-1);
256
 
                end;
257
 
                Screen('PlayMovie', movie, rate, 1, 1.0);
258
 
            end;
259
 
        end;
260
 
    
261
 
        telapsed = GetSecs - t1;
262
 
        fprintf('Elapsed time %f seconds, for %i frames.\n', telapsed, i);
263
 
 
264
 
        Screen('Flip', win);
265
 
        KbReleaseWait;
266
 
        
267
 
        % Done. Stop playback:
268
 
        Screen('PlayMovie', movie, 0);
269
 
 
270
 
        % Close movie object:
271
 
        Screen('CloseMovie', movie);
272
 
    end;
273
 
    
274
 
    % Close screens.
275
 
    Screen('CloseAll');
276
 
 
277
 
    % Done.
278
 
    return;
279
 
catch
280
 
    % Error handling: Close all windows and movies, release all ressources.
281
 
    Screen('CloseAll');
282
 
end;