PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PlatformMovie.cpp
Go to the documentation of this file.
1
2//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
4// Name: src/platforms/sdl/PlatformMovie.cpp
5// Purpose: Contains SDL-specific interface for images
6// Author: Shane T. Mueller, Ph.D.
7// Copyright: (c) 2003-2026 Shane T. Mueller <smueller@obereed.net>
8// License: GPL 2
9//
10//
11//
12// This file is part of the PEBL project.
13//
14// PEBL is free software; you can redistribute it and/or modify
15// it under the terms of the GNU General Public License as published by
16// the Free Software Foundation; either version 2 of the License, or
17// (at your option) any later version.
18//
19// PEBL is distributed in the hope that it will be useful,
20// but WITHOUT ANY WARRANTY; without even the implied warranty of
21// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22// GNU General Public License for more details.
23//
24// You should have received a copy of the GNU General Public License
25// along with PEBL; if not, write to the Free Software
26// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28#include "PlatformMovie.h"
29#include "../../utility/PEBLPath.h"
30#include "../../utility/PError.h"
31#include "../../utility/PEBLUtility.h"
32
33#ifdef PEBL_EMSCRIPTEN
34#include "../../base/Evaluator2.h"
35#else
36#include "../../base/Evaluator.h"
37#endif
38
39
40#include "../../base/PEBLObject.h"
41
42//#include "SDL/SDL.h"
43//#include "SDL/SDL_image.h"
44//#include "WAAVE.h"
45
46#include <string>
47#include <stdio.h>
48
49// cout removed - use cerr for debug output
50using std::cerr;
51using std::endl;
52using std::flush;
53using std::list;
54using std::ostream;
55using std::string;
56
57#ifdef PEBL_MOVIES
58
59
61PlatformMovie::PlatformMovie():
62 PMovie()
63
64{
65 mCDT=CDT_MOVIE;
66 mSurface = NULL; //this isn't needed
67 mRenderer = NULL;
68 mStream = NULL;
69
70}
71
72
73
74PlatformMovie::PlatformMovie(PlatformMovie & pm):
76{
77
78 mStream = pm.GetStream();
79 mStreamObj= pm.GetStreamingObj();
80
81 mCDT=CDT_MOVIE;
82 mSurface = NULL;
83}
84
86PlatformMovie::~PlatformMovie()
87{
88
89
90
91 /* close the opened stream. not mandatory */
92 if(mStream)
93 WV_closeStream(mStream);
94
95
96
97
98 /* close the streaming object */
99 if(mStreamObj)
100 WV_freeStreamRendererObj (mStreamObj);
101
102 /* close the waave engine */
103 //This might not be warranted if we have multiple videos in a single experiment.
104 //It probably should only be closed on SDL_QUIT or something;
105 //maybe in ~PlatformEnvironment???
106
107 // WV_waaveClose();
108
109
110}
111
112//Overloaded setposition
113void PlatformMovie::SetPosition(int x, int y)
114{
116
117 SDL_Rect location;
118 location.h = mHeight;
119 location.w = mWidth;
120 location.x = mX;
121 location.y = mY;
122
123 if(mStreamObj)
124 WV_resetStreamRendererOutput(mStreamObj,mRenderer, &location);
125
126}
127
128void PlatformMovie::SetWidth(int w)
129{
131 SDL_Rect location;
132 location.h = mHeight;
133 location.w = mWidth;
134 location.x = mX;
135 location.y = mY;
136
137 if(mStreamObj)
138 WV_resetStreamRendererOutput(mStreamObj,mRenderer, &location);
139
140}
141
142void PlatformMovie::SetHeight(int h)
143{
145 SDL_Rect location;
146 location.h = mHeight;
147 location.w = mWidth;
148 location.x = mX;
149 location.y = mY;
150
151 if(mStreamObj)
152 WV_resetStreamRendererOutput(mStreamObj,mRenderer, &location);
153
154}
155
156
157
158void PlatformMovie::SetPlaybackPosition(unsigned long int x)
159{
161 uint32_t streamDuration = WV_getStreamDuration(mStream);
162 if(x<=streamDuration)
163 {
164 WV_seekStream(mStream, x);
165 }
166
167}
168
169
170
171void PlatformMovie::SetVolume(long double vol)
172{
173
175 WV_setVolume(mStream, vol);
176}
177
178
179// Inheritable function that is called by friend method << operator of PComplexData
180
181ostream & PlatformMovie::SendToStream(ostream& out) const
182{
183
184 out << "<SDL PlatformMovie>" << flush;
185 return out;
186}
187
188
189
190
191
192bool PlatformMovie::LoadMovie(const std::string & moviefilename,
193 PlatformWindow * window,
194 int width, int height)
195{
196
197#if defined(PEBL_MOVIES)
198 //mParent = window; movie really doesn't have a parent.
199 mWindow = window;
200 //Check to see if we can find the movie file; if not, call everything off.
201 string filename = Evaluator::gPath.FindFile(moviefilename);
202
203 if(filename == "")
204 PError::SignalFatalError(string("Unable to find movie file [") + moviefilename + string("]."));
205
206 //This uses the waave library to load a movie
207 //Initialize the library. We may initialize it multiple times,
208 //and I don't know what that will dho.
209 WV_waaveInit(WAAVE_INIT_AUDIO|WAAVE_INIT_VIDEO);
210 char* fname = (char*)(filename.c_str());
211 mStream = WV_getStream(fname); //shouldn't this be const???
212
213 mRenderer = window->GetRenderer();
214 SDL_RenderClear(mRenderer);
215
216
217
218
219 int streamType = WV_getStreamType(mStream);
220
221
222 if(streamType == WV_STREAM_TYPE_VIDEO || streamType ==WV_STREAM_TYPE_AUDIOVIDEO)
223 {
224 SDL_Rect location;
225 location.h = height;
226 location.w = width;
227 location.x = 0;
228 location.y = 0;
229
230 mStreamObj =WV_getStreamRendererObj(mRenderer,&location,1);
231 WV_setStreamingMethod(mStream,mStreamObj);
232 WV_loadStream(mStream);
233
234 mLength = WV_getStreamDuration(mStream);
235
236 }else if(streamType == WV_STREAM_TYPE_AUDIO )
237 {
238 //just load the audio.
239 mStreamObj = NULL;
240 WV_loadStream(mStream);
241
242 }else{
243 PError::SignalFatalError(string("file is not a movie file."));
244 }
245
246
247 mWidth = width;
248 mHeight = height;
249 PMovie::SetProperty("WIDTH", Variant(mWidth));
250 PMovie::SetProperty("HEIGHT", Variant(mHeight));
251 PMovie::SetProperty("DURATION",
252 Variant((long unsigned int)
253 WV_getStreamDuration(mStream)));
254
255 PMovie::SetProperty("FILENAME",Variant(filename));
256
257
258
259#else
260 PError::SignalFatalError("PEBL Not compiled with movie playing support");
261#endif
262 return false;
263}
264
265// This is for loading .mp3 and the like
266//
267//
268bool PlatformMovie::LoadAudioFile(const std::string & audiofilename)
269{
270
271#if defined(PEBL_MOVIES)
272 //mParent = window; movie really doesn't have a parent.
273
274 //Check to see if we can find the movie file; if not, call everything off.
275 string filename = Evaluator::gPath.FindFile(audiofilename);
276
277 if(filename == "")
278 PError::SignalFatalError(string("Unable to find audio file [") + audiofilename + string("]."));
279
280 //This uses the waave library to load a movie
281 //Initialize the library. We may initialize it multiple times,
282 //and I don't know what that will dho.
283 WV_waaveInit(WAAVE_INIT_AUDIO);
284 char* fname = (char*)(filename.c_str());
285 mStream = WV_getStream(fname); //shouldn't this be const???
286
287
288 int streamType = WV_getStreamType(mStream);
289
290 if(streamType == WV_STREAM_TYPE_VIDEO || streamType ==WV_STREAM_TYPE_AUDIOVIDEO)
291 {
292 std::cerr << "Warning: trying to load video file using LoadAudioFile\n";
293
294 }else if(streamType == WV_STREAM_TYPE_AUDIO )
295 {
296 //just load the audio.
297 mStreamObj = NULL;
298 WV_loadStream(mStream);
299
300 }else{
301 PError::SignalFatalError(string("file is not a known media file."));
302 }
303
304
305 PMovie::SetProperty("WIDTH", Variant(0));
306 PMovie::SetProperty("HEIGHT", Variant(0));
307 PMovie::SetProperty("DURATION",
308 Variant((long unsigned int)
309 WV_getStreamDuration(mStream)));
310
311 PMovie::SetProperty("FILENAME",Variant(filename));
312
313
314
315#else
316 PError::SignalFatalError("PEBL Not compiled with movie playing support");
317#endif
318 return false;
319}
320
321
322
323
324void PlatformMovie::StartPlayback()
325{
326
327 if(mStream)
328 {
329 WV_playStream(mStream);
330 }
331
332}
333
334void PlatformMovie::PausePlayback()
335{
336
337 if(mStream)
338 {
339 WV_pauseStream(mStream);
340 }
341
342}
343
344
345
346
347//This should be called when the eventloop gets the WV_REFRESH_EVENT
348void PlatformMovie::RefreshVideo(SDL_Event &event)
349{
350
351 cerr << "refreshingvideo\n";
352#if PEBL_MOVIES
353 WV_refreshVideoFrame(&event);
354#endif
355}
356
357
358
359int PlatformMovie::GetState(int interface) const
360{
361 //cerr << "Unable to get state of movie\n";
362 return 1;
363}
364
365
366
367Variant PlatformMovie::GetProperty(std::string name)const
368{
369 if(PEBLUtility::ToUpper(name) == "PLAYBACKPOSITION")
370 {
371 long unsigned int pos = WV_getStreamClock(mStream);
372 //SetProperty("PLAYBACKPOSITION",pos)
373 return Variant(pos);
374 }
375 return PEBLObjectBase::GetProperty(name);
376}
377
378#endif
#define NULL
Definition BinReloc.cpp:317
@ CDT_MOVIE
Definition PEBLObject.h:67
static PEBLPath gPath
Variant GetProperty(std::string) const
std::string FindFile(const string &filename)
Definition PEBLPath.cpp:368
virtual bool SetProperty(std::string, Variant v)
Definition PMovie.cpp:85
virtual void SetPlaybackPosition(unsigned long int x)
Definition PMovie.cpp:70
virtual void SetVolume(long double vol)
Definition PMovie.cpp:78
virtual void SetPosition(pInt x, pInt y)
This sets the widget's position on its parent widget.
Definition PWidget.cpp:220
virtual void SetWidth(pInt w)
Definition PWidget.cpp:265
virtual void SetHeight(pInt h)
Definition PWidget.cpp:258
SDL_Renderer * GetRenderer()
std::string ToUpper(const std::string &text)
void SignalFatalError(const std::string &message)