ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

grim/movie/movie.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "graphics/surface.h"
00024 
00025 #include "common/system.h"
00026 #include "common/timer.h"
00027 
00028 #include "engines/grim/movie/movie.h"
00029 #include "engines/grim/grim.h"
00030 #include "engines/grim/debug.h"
00031 #include "engines/grim/savegame.h"
00032 
00033 namespace Grim {
00034 
00035 MoviePlayer *g_movie;
00036 
00037 MoviePlayer::MoviePlayer() {
00038     _channels = -1;
00039     _freq = 22050;
00040     _videoFinished = false;
00041     _videoLooping = false;
00042     _videoPause = true;
00043     _updateNeeded = false;
00044     _showSubtitles = true;
00045     _movieTime = 0;
00046     _frame = -1;
00047     _x = 0;
00048     _y = 0;
00049     _videoDecoder = nullptr;
00050     _internalSurface = nullptr;
00051     _externalSurface = new Graphics::Surface();
00052     _timerStarted = false;
00053 }
00054 
00055 MoviePlayer::~MoviePlayer() {
00056     // Remove the callback immediately, so we're sure timerCallback() doesn't get called
00057     // after the deinit() or the deletes.
00058     if (_timerStarted)
00059         g_system->getTimerManager()->removeTimerProc(&timerCallback);
00060 
00061     deinit();
00062     delete _videoDecoder;
00063     delete _externalSurface;
00064 }
00065 
00066 void MoviePlayer::pause(bool p) {
00067     Common::StackLock lock(_frameMutex);
00068     _videoPause = p;
00069     _videoDecoder->pauseVideo(p);
00070 }
00071 
00072 void MoviePlayer::stop() {
00073     Common::StackLock lock(_frameMutex);
00074     deinit();
00075     g_grim->setMode(GrimEngine::NormalMode);
00076 }
00077 
00078 void MoviePlayer::timerCallback(void *instance) {
00079     MoviePlayer *movie = static_cast<MoviePlayer *>(instance);
00080     Common::StackLock lock(movie->_frameMutex);
00081     if (movie->prepareFrame())
00082         movie->postHandleFrame();
00083 }
00084 
00085 bool MoviePlayer::prepareFrame() {
00086     if (!_videoLooping && _videoDecoder->endOfVideo()) {
00087         _videoFinished = true;
00088     }
00089 
00090     if (_videoPause) {
00091         return false;
00092     }
00093 
00094     if (_videoFinished) {
00095         if (g_grim->getMode() == GrimEngine::SmushMode) {
00096             g_grim->setMode(GrimEngine::NormalMode);
00097         }
00098         _videoPause = true;
00099         return false;
00100     }
00101 
00102     if (_videoDecoder->getTimeToNextFrame() > 0)
00103         return false;
00104 
00105     handleFrame();
00106     _internalSurface = _videoDecoder->decodeNextFrame();
00107     if (_frame != _videoDecoder->getCurFrame()) {
00108         _updateNeeded = true;
00109     }
00110 
00111     _movieTime = _videoDecoder->getTime();
00112     _frame = _videoDecoder->getCurFrame();
00113 
00114     return true;
00115 }
00116 
00117 Graphics::Surface *MoviePlayer::getDstSurface() {
00118     Common::StackLock lock(_frameMutex);
00119     if (_updateNeeded && _internalSurface) {
00120         _externalSurface->copyFrom(*_internalSurface);
00121     }
00122 
00123     return _externalSurface;
00124 }
00125 
00126 void MoviePlayer::init() {
00127     if (!_timerStarted) {
00128         g_system->getTimerManager()->installTimerProc(&timerCallback, 10000, this, "movieLoop");
00129         _timerStarted = true;
00130     }
00131 
00132     _frame = -1;
00133     _movieTime = 0;
00134     _updateNeeded = false;
00135     _videoFinished = false;
00136 }
00137 
00138 void MoviePlayer::deinit() {
00139     Debug::debug(Debug::Movie, "Deinitting video '%s'.\n", _fname.c_str());
00140 
00141     if (_videoDecoder)
00142         _videoDecoder->close();
00143 
00144     _internalSurface = nullptr;
00145 
00146     if (_externalSurface)
00147         _externalSurface->free();
00148 
00149     _videoPause = false;
00150     _videoFinished = true;
00151 }
00152 
00153 bool MoviePlayer::play(const Common::String &filename, bool looping, int x, int y, bool start, bool showSubtitles) {
00154     Common::StackLock lock(_frameMutex);
00155     deinit();
00156     _x = x;
00157     _y = y;
00158     _fname = filename;
00159     _videoLooping = looping;
00160     _showSubtitles = showSubtitles;
00161 
00162     if (!loadFile(_fname))
00163         return false;
00164 
00165     Debug::debug(Debug::Movie, "Playing video '%s'.\n", filename.c_str());
00166 
00167     init();
00168     _internalSurface = nullptr;
00169 
00170     if (start) {
00171         _videoDecoder->start();
00172 
00173         // Get the first frame immediately
00174         timerCallback(this);
00175     }
00176 
00177     return true;
00178 }
00179 
00180 bool MoviePlayer::loadFile(const Common::String &filename) {
00181     return _videoDecoder->loadFile(filename);
00182 }
00183 
00184 void MoviePlayer::saveState(SaveGame *state) {
00185     Common::StackLock lock(_frameMutex);
00186     state->beginSection('SMUS');
00187 
00188     state->writeString(_fname);
00189 
00190     state->writeLESint32(_frame);
00191     state->writeFloat(_movieTime);
00192     state->writeBool(_videoFinished);
00193     state->writeBool(_videoLooping);
00194 
00195     state->writeLESint32(_x);
00196     state->writeLESint32(_y);
00197 
00198     save(state);
00199 
00200     state->endSection();
00201 }
00202 
00203 void MoviePlayer::restoreState(SaveGame *state) {
00204     Common::StackLock lock(_frameMutex);
00205     state->beginSection('SMUS');
00206 
00207     _fname = state->readString();
00208 
00209     int32 frame = state->readLESint32();
00210     float movieTime = state->readFloat();
00211     bool videoFinished = state->readBool();
00212     bool videoLooping = state->readBool();
00213 
00214     int x = state->readLESint32();
00215     int y = state->readLESint32();
00216 
00217     if (!videoFinished && !_fname.empty()) {
00218         play(_fname.c_str(), videoLooping, x, y, false);
00219     }
00220     _frame = frame;
00221     _movieTime = movieTime;
00222 
00223     restore(state);
00224 
00225     state->endSection();
00226 }
00227 
00228 #if !defined(USE_MPEG2) || !defined(USE_BINK)
00229 #define NEED_NULLPLAYER
00230 #endif
00231 
00232 // Temporary fix while reworking codecs:
00233 #ifndef NEED_NULLPLAYER
00234 #define NEED_NULLPLAYER
00235 #endif
00236 
00237 // Fallback for when USE_MPEG2 / USE_BINK isnt defined
00238 
00239 #ifdef NEED_NULLPLAYER
00240 class NullPlayer : public MoviePlayer {
00241 public:
00242     NullPlayer(const char *codecID) {
00243         warning("%s-playback not compiled in, but needed", codecID);
00244         _videoFinished = true; // Rigs all movies to be completed.
00245     }
00246     ~NullPlayer() {}
00247     bool play(const Common::String &filename, bool looping, int x, int y, bool start = true, bool showSubtitles = false) override { return true; }
00248     bool loadFile(const Common::String &filename) override { return true; }
00249     void stop() override {}
00250     void pause(bool p) override {}
00251     void saveState(SaveGame *state) {}
00252     void restoreState(SaveGame *state) {}
00253 private:
00254     static void timerCallback(void *ptr) {}
00255     void handleFrame() override {}
00256     bool prepareFrame() override { return false; }
00257     void init() override {}
00258     void deinit() override {}
00259 };
00260 #endif
00261 
00262 #ifndef USE_MPEG2
00263 MoviePlayer *CreateMpegPlayer() {
00264     return new NullPlayer("MPEG2");
00265 }
00266 #endif
00267 
00268 #ifndef USE_BINK
00269 MoviePlayer *CreateBinkPlayer(bool demo) {
00270     return new NullPlayer("BINK");
00271 }
00272 #endif
00273 
00274 } // end of namespace Grim


Generated on Sat Mar 23 2019 05:01:51 for ResidualVM by doxygen 1.7.1
curved edge   curved edge