root/OpenSceneGraph/trunk/src/osgPlugins/quicktime/QuicktimeImageStream.cpp @ 13041

Revision 13041, 6.3 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// -*-c++-*-
2
3/*
4* Copyright (C) 2004 Stephan Huber http://digitalmind.de
5*
6*
7* The Open Scene Graph (OSG) is a cross platform C++/OpenGL library for
8* real-time rendering of large 3D photo-realistic models.
9* The OSG homepage is http://www.openscenegraph.org/
10*
11* This software is free software; you can redistribute it and/or modify
12* it under the terms of the GNU General Public License as published by
13* the Free Software Foundation; either version 2 of the License, or
14* (at your option) any later version.
15*
16* This software is distributed in the hope that it will be useful,
17* but WITHOUT ANY WARRANTY; without even the implied warranty of
18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19* GNU General Public License for more details.
20*
21* You should have received a copy of the GNU General Public License
22* along with this program; if not, write to the Free Software
23* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24*/
25
26#include "QuicktimeImageStream.h"
27#include <osg/Notify>
28#include <osg/Timer>
29#include <osg/Math>
30
31#include <OpenThreads/ScopedLock>
32#include <OpenThreads/Thread>
33
34#include "QTUtils.h"
35#include "MovieData.h"
36
37
38
39#define IDLE_TIMEOUT 150000L
40
41int QuicktimeImageStream::_qtInstanceCount = 0;
42
43// Constructor: setup and start thread
44QuicktimeImageStream::QuicktimeImageStream(std::string fileName) : ImageStream()
45{
46    setOrigin(osg::Image::TOP_LEFT);
47
48    _len = 0;
49    _movieData = new MovieData();
50
51    for (int i = 0; i < NUM_CMD_INDEX; i++)
52      _cmd[i] = THREAD_IDLE;
53    _wrIndex = _rdIndex = 0;
54
55    load(fileName);
56
57    if (!fileName.empty())
58      setFileName(fileName);
59
60    // ricky
61    _status = ImageStream::PAUSED;
62}
63
64
65// Deconstructor: stop and terminate thread
66QuicktimeImageStream::~QuicktimeImageStream()
67{
68   if( isRunning() )
69   {
70      quit(true);
71   }
72
73
74   // clean up quicktime movies.
75   delete _movieData;
76
77}
78
79
80// Set command
81void QuicktimeImageStream::setCmd(ThreadCommand cmd, double rate)
82{
83   OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
84
85   _cmd[_wrIndex] = cmd;
86   _rates[_wrIndex] = rate;
87   _wrIndex = (_wrIndex + 1) % NUM_CMD_INDEX;
88}
89
90
91// Get command
92QuicktimeImageStream::ThreadCommand QuicktimeImageStream::getCmd()
93{
94   ThreadCommand cmd = THREAD_IDLE;
95
96   OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
97
98   if (_rdIndex != _wrIndex) {
99      cmd = _cmd[_rdIndex];
100      _currentRate = _rates[_rdIndex];
101      _rdIndex = (_rdIndex + 1) % NUM_CMD_INDEX;
102   }
103
104   return cmd;
105}
106
107
108void QuicktimeImageStream::load(std::string fileName)
109{
110   OSG_DEBUG << "QT-ImageStream: loading quicktime movie from " << fileName << std::endl;
111
112   _movieData->load(this, fileName);
113
114   _len = _movieData->getMovieDuration();
115   _current = 0.0;
116}
117
118void QuicktimeImageStream::quit(bool wiatForThreadToExit)
119{
120   OSG_DEBUG<<"Sending quit"<<this<<std::endl;
121   setCmd(THREAD_QUIT);
122
123   if (wiatForThreadToExit)
124   {
125      while(isRunning())
126      {
127         OSG_DEBUG<<"Waiting for QuicktimeImageStream to quit"<<this<<std::endl;
128         OpenThreads::Thread::YieldCurrentThread();
129      }
130      OSG_DEBUG<<"QuicktimeImageStream has quit"<<this<<std::endl;
131   }
132}
133
134void QuicktimeImageStream::setVolume(float volume)
135{
136    _movieData->setVolume(osg::minimum(osg::maximum(volume,0.0f),1.0f));
137}
138
139// Get and Set the playback volume of the stream.
140float QuicktimeImageStream::getVolume() const
141{
142    return _movieData->getVolume();
143}
144
145void QuicktimeImageStream::run()
146{
147
148   bool playing = false;
149   bool done = false;
150
151   while (!done) {
152
153
154      ThreadCommand cmd = getCmd();
155      OSG_DEBUG << "movietime: " << _movieData->getMovieTime() << " rate: " << _movieData->getMovieRate() << " state " << cmd << " playing: " << playing << " done " << done << "  " << _wrIndex << "/" << _rdIndex << std::endl;
156      // Handle commands
157      {
158         if (cmd != THREAD_IDLE) {
159            OSG_DEBUG << "new cmd: " << cmd << std::endl;
160            switch (cmd) {
161                    case THREAD_START: // Start or continue stream
162                       applyLoopingMode();
163                       _movieData->setMovieRate(1.0);
164
165                       playing = true;
166                       break;
167
168                    case THREAD_STOP:
169                       _movieData->setMovieRate(0);
170                       OSG_INFO << "QT-ImageStream: stop at "<< std::endl;
171                       playing = false;
172                       break;
173
174                    case THREAD_REWIND:
175                       SetMovieRate(_movieData->getMovie(),0.0);
176                       GoToBeginningOfMovie(_movieData->getMovie());
177                       break;
178
179                    case THREAD_FORWARD:
180                       SetMovieRate(_movieData->getMovie(),0.0);
181                       GoToEndOfMovie(_movieData->getMovie());
182                       break;
183
184                    case THREAD_SEEK:
185                       _movieData->setMovieTime(_currentRate);
186                       playing = true;
187                       break;
188
189                    case THREAD_SETRATE:
190                       _movieData->setMovieRate(_currentRate);
191                       playing = (_currentRate != 0.0);
192                       break;
193
194                    case THREAD_CLOSE:
195                       _movieData->setMovieRate(0.0);
196                       break;
197
198                    case THREAD_QUIT: // TODO
199                       _movieData->setMovieRate(0.0);
200                       OSG_INFO << "QT-ImageStream: quit" << std::endl;
201                       //playing = false;
202                       done = true;
203                       break;
204                    default:
205                       OSG_WARN << "QT-ImageStream: Unknown command " << cmd << std::endl;
206                       break;
207            }
208         }
209
210         MoviesTask(_movieData->getMovie(),0);
211         _current = _movieData->getMovieTime();
212      }
213
214
215      if (_lastUpdate!= _current)
216      {
217         // force the texture to update the image
218         dirty();
219         // update internal time and take care of looping
220         _lastUpdate = _current;
221      }
222
223      if (playing)
224      {
225         OpenThreads::Thread::microSleep(16000);
226      }
227      else if (!done)
228      {
229         OpenThreads::Thread::microSleep(IDLE_TIMEOUT);
230      }
231   }
232
233
234}
235
236
237void QuicktimeImageStream::applyLoopingMode()
238{
239    OSG_INFO << "applying loop mode " << getLoopingMode() << std::endl;
240    _movieData->setLooping(getLoopingMode() == osg::ImageStream::LOOPING);
241}
Note: See TracBrowser for help on using the browser.