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

« back to all changes in this revision

Viewing changes to Psychtoolbox/PsychDemos/QuicktimeDemos/PlayMoviesWithoutGapDemoOSX.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 PlayMoviesWithoutGapDemoOSX(moviename)
2
 
%
3
 
% PlayMoviesWithoutGapDemoOSX(moviename)
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
 
% Pressing ESC ends the demo.
11
 
%
12
 
% Movies are played one after each other. We try to minimize perceptible
13
 
% gaps between end of movie i and start of movie i+1 by asynchronous
14
 
% loading: While movie i is played back, we ask Psychtoolbox to load the
15
 
% next movie i+1 in the background, so startup time for movie i+1 will be
16
 
% minimized. This feature is currently only supported on MacOS-X and it
17
 
% doesn't work well with movies that have sound tracks. The reason for this
18
 
% is currently unknown, but it leads to frequent lockups of the playback
19
 
% engine. How well this feature works depends strongly on the computational
20
 
% horsepower of your machine. Slow machines will probably stutter...
21
 
%
22
 
 
23
 
% History:
24
 
% 7/5/06  mk  Wrote it.
25
 
 
26
 
AssertOpenGL;
27
 
 
28
 
if nargin < 1
29
 
    moviename = '*.mov' %#ok<NOPRT>
30
 
end;
31
 
 
32
 
% Switch KbName into unified mode: It will use the names of the OS-X
33
 
% platform on all platforms in order to make this script portable:
34
 
KbName('UnifyKeyNames');
35
 
 
36
 
esc=KbName('ESCAPE');
37
 
 
38
 
try
39
 
    % Child protection
40
 
    AssertOpenGL;
41
 
    background=[128, 128, 128];
42
 
 
43
 
    % Open onscreen window:
44
 
    screen=max(Screen('Screens'));
45
 
    win = Screen('OpenWindow', screen, background);
46
 
    
47
 
    % Initial display and sync to timestamp:
48
 
    Screen('Flip',win);
49
 
    abortit = 0;
50
 
        
51
 
    % Return full list of movie files from directory+pattern:
52
 
    moviefiles=dir(moviename);
53
 
    for i=1:size(moviefiles,1)
54
 
        moviefiles(i).name = [ pwd filesep moviefiles(i).name ];
55
 
    end
56
 
    
57
 
    % Playbackrate defaults to 1:
58
 
    rate=1;
59
 
    
60
 
    % Load first movie. This is a synchronous (blocking) load:
61
 
    iteration = 1;
62
 
    moviename=moviefiles(mod(iteration, size(moviefiles,1))+1).name;
63
 
    [movie movieduration fps imgw imgh] = Screen('OpenMovie', win, moviename);
64
 
 
65
 
    % Start playback of movie. This will start
66
 
    % the realtime playback clock and playback of audio tracks, if any.
67
 
    % Play 'movie', at a playbackrate = 1, with 1.0 == 100% audio volume.
68
 
    Screen('PlayMovie', movie, rate, 0, 1.0);
69
 
 
70
 
    prefetched=0;
71
 
    newmovie = -1;
72
 
 
73
 
    % Endless loop, runs until ESC key pressed:
74
 
    while (abortit<2)
75
 
        % Show basic info about movie:
76
 
        fprintf('ITER=%i::', iteration);
77
 
        fprintf('Movie: %s  : %f seconds duration, %f fps, w x h = %i x %i...\n', moviename, movieduration, fps, imgw, imgh);
78
 
 
79
 
        i=0;
80
 
 
81
 
        % Get moviename of next file:
82
 
        iteration=iteration + 1;
83
 
        moviename=moviefiles(mod(iteration, size(moviefiles,1))+1).name;
84
 
 
85
 
        t1 = GetSecs;
86
 
 
87
 
        % Playback loop: Fetch video frames and display them...
88
 
        while(1)
89
 
            i=i+1;
90
 
            if (abs(rate)>0)
91
 
                % Return next frame in movie, in sync with current playback
92
 
                % time and sound.
93
 
                % tex either the texture handle or zero if no new frame is
94
 
                % ready yet. pts = Presentation timestamp in seconds.
95
 
                [tex pts] = Screen('GetMovieImage', win, movie, 1);
96
 
                
97
 
                % Valid texture returned?
98
 
                if tex<0
99
 
                    % No. This means that the end of this movie is reached.
100
 
                    % We exit the loop and prepare next movie:
101
 
                    break;
102
 
                end;
103
 
 
104
 
                if (tex>0)
105
 
                    % Yes. Draw the new texture immediately to screen:
106
 
                    Screen('DrawTexture', win, tex);
107
 
 
108
 
                    % Update display:
109
 
                    Screen('Flip', win);
110
 
 
111
 
                    % Release texture:
112
 
                    Screen('Close', tex);
113
 
                end;
114
 
            end;
115
 
 
116
 
            % Check for abortion by user:
117
 
            abortit=0;
118
 
            [keyIsDown,secs,keyCode]=KbCheck;
119
 
            if (keyIsDown==1 && keyCode(esc))
120
 
                % Set the abort-demo flag.
121
 
                abortit=2;
122
 
                break;
123
 
            end;
124
 
 
125
 
            % We start background loading of the next movie 0.5 seconds
126
 
            % after start of playback of the current movie:
127
 
            if prefetched==0 && pts > 0.5
128
 
                % Initiate background async load operation:
129
 
                % We simply set the async flag to 1 and don't query any
130
 
                % return values:
131
 
                Screen('OpenMovie', win, moviename, 1);
132
 
                prefetched=1;
133
 
            end;
134
 
 
135
 
            % If asynchronous load of next movie has been started already
136
 
            % and we are less than 0.5 seconds from the end of the current
137
 
            % movie, then we try to finish the async load operation and
138
 
            % start playback of the new movie in order to give processing a
139
 
            % headstart:
140
 
            if prefetched==1 && movieduration - pts < 0.5
141
 
                % Less than 0.5 seconds until end of current movie. Try to
142
 
                % start playback for next movie:
143
 
                
144
 
                % Open the movie - this should take zero-time as the movie
145
 
                % should have been already prefetched during playback of
146
 
                % the current movie. In case loading did not finish, this
147
 
                % command will pause Matlabs execution until the movie is
148
 
                % really ready.
149
 
                [newmovie newmovieduration newfps newimgw newimgh] = Screen('OpenMovie', win, moviename);
150
 
                
151
 
                % Start it:
152
 
                Screen('PlayMovie', newmovie, rate, 0, 1.0);
153
 
                
154
 
                prefetched=2;
155
 
            end;
156
 
        end;
157
 
    
158
 
        telapsed = GetSecs - t1 %#ok<NOPRT,NASGU>
159
 
        finalcount=i %#ok<NOPRT,NASGU>
160
 
        
161
 
        % Done with old movie. Stop its playback:
162
 
        Screen('PlayMovie', movie, 0);
163
 
 
164
 
        % Close movie object:
165
 
        Screen('CloseMovie', movie);
166
 
        
167
 
        % Ok, now our 'newmovie' becomes the current 'movie':
168
 
        movie = newmovie;
169
 
        movieduration = newmovieduration;
170
 
        fps = newfps;
171
 
        imgw = newimgw;
172
 
        imgh = newimgh;
173
 
        prefetched = 0;
174
 
        % As playback of the new movie has been started already, we can
175
 
        % simply reenter the playback loop:
176
 
    end;
177
 
    
178
 
    % End of movie playback. Shut down and exit:
179
 
    if prefetched == 1
180
 
        % A prefetch operation for a movie is still in progress. We need to
181
 
        % finalize this cleanly by waiting for the movie to open and then
182
 
        % closing it.
183
 
        newmovie = Screen('OpenMovie', win, moviename);
184
 
        prefetched = 2;
185
 
    end
186
 
    
187
 
    if prefetched == 2
188
 
        % Playback of a new prefetched movie has been started. We need to stop and
189
 
        % close it:
190
 
        Screen('PlayMovie', newmovie, 0);
191
 
        Screen('CloseMovie', newmovie);
192
 
    end
193
 
    
194
 
    % Close screens.
195
 
    Screen('Close', win);
196
 
 
197
 
    % Done.
198
 
    return;
199
 
catch
200
 
    % Error handling: Close all windows and movies, release all ressources.
201
 
    Screen('CloseAll');
202
 
end;