Diffstat (limited to 'core/multimedia/opieplayer/loopcontrol.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/multimedia/opieplayer/loopcontrol.cpp | 328 |
1 files changed, 166 insertions, 162 deletions
diff --git a/core/multimedia/opieplayer/loopcontrol.cpp b/core/multimedia/opieplayer/loopcontrol.cpp index 6dfd057..859a67a 100644 --- a/core/multimedia/opieplayer/loopcontrol.cpp +++ b/core/multimedia/opieplayer/loopcontrol.cpp | |||
@@ -1,464 +1,468 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | // L.J.Potter added changes Fri 02-15-2002 | ||
21 | |||
20 | #include <qpe/qpeapplication.h> | 22 | #include <qpe/qpeapplication.h> |
23 | |||
21 | #ifdef Q_WS_QWS | 24 | #ifdef Q_WS_QWS |
22 | #include <qpe/qcopenvelope_qws.h> | 25 | #include <qpe/qcopenvelope_qws.h> |
23 | #endif | 26 | #endif |
24 | #include <stdio.h> | 27 | #include <stdio.h> |
25 | #include <stdlib.h> | 28 | #include <stdlib.h> |
26 | #include <string.h> | 29 | #include <string.h> |
27 | #include <pthread.h> | 30 | #include <pthread.h> |
28 | #include <errno.h> | 31 | #include <errno.h> |
29 | #include <unistd.h> | 32 | #include <unistd.h> |
30 | #include "loopcontrol.h" | 33 | #include "loopcontrol.h" |
31 | #include "videowidget.h" | 34 | #include "videowidget.h" |
32 | #include "audiodevice.h" | 35 | #include "audiodevice.h" |
33 | #include "mediaplayerplugininterface.h" | 36 | #include "mediaplayerplugininterface.h" |
34 | #include "mediaplayerstate.h" | 37 | #include "mediaplayerstate.h" |
35 | 38 | ||
36 | 39 | ||
37 | extern VideoWidget *videoUI; // now only needed to tell it to play a frame | 40 | extern VideoWidget *videoUI; // now only needed to tell it to play a frame |
38 | extern MediaPlayerState *mediaPlayerState; | 41 | extern MediaPlayerState *mediaPlayerState; |
39 | 42 | ||
40 | 43 | ||
41 | //#define DecodeLoopDebug(x) qDebug x | 44 | //#define DecodeLoopDebug(x) qDebug x |
42 | #define DecodeLoopDebug(x) | 45 | #define DecodeLoopDebug(x) |
43 | 46 | ||
44 | 47 | ||
45 | static char *audioBuffer = NULL; | 48 | static char *audioBuffer = NULL; |
46 | static AudioDevice *audioDevice = NULL; | 49 | static AudioDevice *audioDevice = NULL; |
47 | static bool disabledSuspendScreenSaver = FALSE; | 50 | static bool disabledSuspendScreenSaver = FALSE; |
48 | static bool previousSuspendMode = FALSE; | 51 | static bool previousSuspendMode = FALSE; |
49 | 52 | ||
50 | 53 | ||
51 | pthread_taudio_tid; | 54 | pthread_t audio_tid; |
52 | pthread_attr_t audio_attr; | 55 | pthread_attr_t audio_attr; |
53 | bool threadOkToGo = FALSE; | 56 | bool threadOkToGo = FALSE; |
54 | 57 | ||
55 | 58 | ||
56 | class Mutex { | 59 | class Mutex { |
57 | public: | 60 | public: |
58 | Mutex() { | 61 | Mutex() { |
59 | pthread_mutexattr_t attr; | 62 | pthread_mutexattr_t attr; |
60 | pthread_mutexattr_init( &attr ); | 63 | pthread_mutexattr_init( &attr ); |
61 | pthread_mutex_init( &mutex, &attr ); | 64 | pthread_mutex_init( &mutex, &attr ); |
62 | pthread_mutexattr_destroy( &attr ); | 65 | pthread_mutexattr_destroy( &attr ); |
63 | } | 66 | } |
64 | 67 | ||
65 | ~Mutex() { | 68 | ~Mutex() { |
66 | pthread_mutex_destroy( &mutex ); | 69 | pthread_mutex_destroy( &mutex ); |
67 | } | 70 | } |
68 | 71 | ||
69 | void lock() { | 72 | void lock() { |
70 | pthread_mutex_lock( &mutex ); | 73 | pthread_mutex_lock( &mutex ); |
71 | } | 74 | } |
72 | 75 | ||
73 | void unlock() { | 76 | void unlock() { |
74 | pthread_mutex_unlock( &mutex ); | 77 | pthread_mutex_unlock( &mutex ); |
75 | } | 78 | } |
76 | private: | 79 | private: |
77 | pthread_mutex_t mutex; | 80 | pthread_mutex_t mutex; |
78 | }; | 81 | }; |
79 | 82 | ||
80 | 83 | ||
81 | void *startAudioThread( void *ptr ) { | 84 | void *startAudioThread( void *ptr ) { |
82 | LoopControl *mpegView = (LoopControl *)ptr; | 85 | LoopControl *mpegView = (LoopControl *)ptr; |
83 | while ( TRUE ) { | 86 | while ( TRUE ) { |
84 | if ( threadOkToGo && mpegView->moreAudio ) | 87 | if ( threadOkToGo && mpegView->moreAudio ) |
85 | mpegView->startAudio(); | 88 | mpegView->startAudio(); |
86 | else | 89 | else |
87 | usleep( 10000 ); // Semi-buzy-wait till we are playing again | 90 | usleep( 10000 ); // Semi-buzy-wait till we are playing again |
88 | } | 91 | } |
89 | return 0; | 92 | return 0; |
90 | } | 93 | } |
91 | 94 | ||
92 | 95 | ||
93 | Mutex *audioMutex; | 96 | Mutex *audioMutex; |
94 | 97 | ||
95 | 98 | ||
96 | LoopControl::LoopControl( QObject *parent, const char *name ) | 99 | LoopControl::LoopControl( QObject *parent, const char *name ) |
97 | : QObject( parent, name ) { | 100 | : QObject( parent, name ) { |
98 | isMuted = FALSE; | 101 | isMuted = FALSE; |
99 | connect( qApp, SIGNAL( volumeChanged(bool) ), this, SLOT( setMute(bool) ) ); | 102 | connect( qApp, SIGNAL( volumeChanged(bool) ), this, SLOT( setMute(bool) ) ); |
100 | 103 | ||
101 | audioMutex = new Mutex; | 104 | audioMutex = new Mutex; |
102 | 105 | ||
103 | pthread_attr_init(&audio_attr); | 106 | pthread_attr_init(&audio_attr); |
104 | #define USE_REALTIME_AUDIO_THREAD | 107 | #define USE_REALTIME_AUDIO_THREAD |
105 | #ifdef USE_REALTIME_AUDIO_THREAD | 108 | #ifdef USE_REALTIME_AUDIO_THREAD |
106 | // Attempt to set it to real-time round robin | 109 | // Attempt to set it to real-time round robin |
107 | if ( pthread_attr_setschedpolicy( &audio_attr, SCHED_RR ) == 0 ) { | 110 | if ( pthread_attr_setschedpolicy( &audio_attr, SCHED_RR ) == 0 ) { |
108 | sched_param params; | 111 | sched_param params; |
109 | params.sched_priority = 50; | 112 | params.sched_priority = 50; |
110 | pthread_attr_setschedparam(&audio_attr,¶ms); | 113 | pthread_attr_setschedparam(&audio_attr,¶ms); |
111 | } else { | 114 | } else { |
112 | qDebug( "Error setting up a realtime thread, reverting to using a normal thread." ); | 115 | qDebug( "Error setting up a realtime thread, reverting to using a normal thread." ); |
113 | pthread_attr_destroy(&audio_attr); | 116 | pthread_attr_destroy(&audio_attr); |
114 | pthread_attr_init(&audio_attr); | 117 | pthread_attr_init(&audio_attr); |
115 | } | 118 | } |
116 | #endif | 119 | #endif |
117 | pthread_create(&audio_tid, &audio_attr, (void * (*)(void *))startAudioThread, this); | 120 | pthread_create(&audio_tid, &audio_attr, (void * (*)(void *))startAudioThread, this); |
118 | } | 121 | } |
119 | 122 | ||
120 | 123 | ||
121 | LoopControl::~LoopControl() { | 124 | LoopControl::~LoopControl() { |
122 | stop(); | 125 | stop(); |
123 | } | 126 | } |
124 | 127 | ||
125 | 128 | ||
126 | static long prev_frame = 0; | 129 | static long prev_frame = 0; |
127 | static int currentSample = 0; | 130 | static int currentSample = 0; |
128 | 131 | ||
129 | 132 | ||
130 | void LoopControl::timerEvent( QTimerEvent *te ) { | 133 | void LoopControl::timerEvent( QTimerEvent *te ) { |
131 | 134 | ||
132 | if ( te->timerId() == videoId ) | 135 | if ( te->timerId() == videoId ) |
133 | startVideo(); | 136 | startVideo(); |
134 | 137 | ||
135 | if ( te->timerId() == sliderId ) { | 138 | if ( te->timerId() == sliderId ) { |
136 | if ( hasAudioChannel && !hasVideoChannel && moreAudio ) { | 139 | if ( hasAudioChannel && !hasVideoChannel && moreAudio ) { |
137 | mediaPlayerState->updatePosition( audioSampleCounter ); | 140 | mediaPlayerState->updatePosition( audioSampleCounter ); |
138 | } else if ( hasVideoChannel && moreVideo ) { | 141 | } else if ( hasVideoChannel && moreVideo ) { |
139 | mediaPlayerState->updatePosition( current_frame ); | 142 | mediaPlayerState->updatePosition( current_frame ); |
140 | } | 143 | } |
141 | } | 144 | } |
142 | 145 | ||
143 | if ( !moreVideo && !moreAudio ) { | 146 | if ( !moreVideo && !moreAudio ) { |
144 | mediaPlayerState->setPlaying( FALSE ); | 147 | mediaPlayerState->setPlaying( FALSE ); |
145 | mediaPlayerState->setNext(); | 148 | mediaPlayerState->setNext(); |
146 | } | 149 | } |
147 | } | 150 | } |
148 | 151 | ||
149 | 152 | ||
150 | void LoopControl::setPosition( long pos ) { | 153 | void LoopControl::setPosition( long pos ) { |
151 | audioMutex->lock(); | 154 | audioMutex->lock(); |
152 | 155 | ||
153 | if ( hasVideoChannel && hasAudioChannel ) { | 156 | if ( hasVideoChannel && hasAudioChannel ) { |
154 | playtime.restart(); | 157 | playtime.restart(); |
155 | playtime = playtime.addMSecs( long((double)-pos * 1000.0 / framerate) ); | 158 | playtime = playtime.addMSecs( long((double)-pos * 1000.0 / framerate) ); |
156 | current_frame = pos + 1; | 159 | current_frame = pos + 1; |
157 | mediaPlayerState->curDecoder()->videoSetFrame( current_frame, stream ); | 160 | mediaPlayerState->curDecoder()->videoSetFrame( current_frame, stream ); |
158 | prev_frame = current_frame - 1; | 161 | prev_frame = current_frame - 1; |
159 | currentSample = (int)( (double)current_frame * freq / framerate ); | 162 | currentSample = (int)( (double)current_frame * freq / framerate ); |
160 | mediaPlayerState->curDecoder()->audioSetSample( currentSample, stream ); | 163 | mediaPlayerState->curDecoder()->audioSetSample( currentSample, stream ); |
161 | audioSampleCounter = currentSample - 1; | 164 | audioSampleCounter = currentSample - 1; |
162 | } else if ( hasVideoChannel ) { | 165 | } else if ( hasVideoChannel ) { |
163 | playtime.restart(); | 166 | playtime.restart(); |
164 | playtime = playtime.addMSecs( long((double)-pos * 1000.0 / framerate) ); | 167 | playtime = playtime.addMSecs( long((double)-pos * 1000.0 / framerate) ); |
165 | current_frame = pos + 1; | 168 | current_frame = pos + 1; |
166 | mediaPlayerState->curDecoder()->videoSetFrame( current_frame, stream ); | 169 | mediaPlayerState->curDecoder()->videoSetFrame( current_frame, stream ); |
167 | prev_frame = current_frame - 1; | 170 | prev_frame = current_frame - 1; |
168 | } else if ( hasAudioChannel ) { | 171 | } else if ( hasAudioChannel ) { |
169 | playtime.restart(); | 172 | playtime.restart(); |
170 | playtime = playtime.addMSecs( long((double)-pos * 1000.0 / freq) ); | 173 | playtime = playtime.addMSecs( long((double)-pos * 1000.0 / freq) ); |
171 | currentSample = pos + 1; | 174 | currentSample = pos + 1; |
172 | mediaPlayerState->curDecoder()->audioSetSample( currentSample, stream ); | 175 | mediaPlayerState->curDecoder()->audioSetSample( currentSample, stream ); |
173 | audioSampleCounter = currentSample - 1; | 176 | audioSampleCounter = currentSample - 1; |
174 | } | 177 | } |
175 | 178 | ||
176 | audioMutex->unlock(); | 179 | audioMutex->unlock(); |
177 | } | 180 | } |
178 | 181 | ||
179 | 182 | ||
180 | void LoopControl::startVideo() { | 183 | void LoopControl::startVideo() { |
181 | 184 | ||
182 | if ( moreVideo ) { | 185 | if ( moreVideo ) { |
183 | 186 | ||
184 | if ( mediaPlayerState->curDecoder() ) { | 187 | if ( mediaPlayerState->curDecoder() ) { |
185 | 188 | ||
186 | if ( hasAudioChannel && !isMuted ) { | 189 | if ( hasAudioChannel && !isMuted ) { |
187 | 190 | ||
188 | current_frame = long( playtime.elapsed() * framerate / 1000 ); | 191 | current_frame = long( playtime.elapsed() * framerate / 1000 ); |
189 | 192 | ||
190 | if ( prev_frame != -1 && current_frame <= prev_frame ) | 193 | if ( prev_frame != -1 && current_frame <= prev_frame ) |
191 | return; | 194 | return; |
192 | 195 | ||
193 | } else { | 196 | } else { |
194 | // Don't skip | 197 | // Don't skip |
195 | current_frame++; | 198 | current_frame++; |
196 | } | 199 | } |
197 | 200 | ||
198 | if ( prev_frame == -1 || current_frame > prev_frame ) { | 201 | if ( prev_frame == -1 || current_frame > prev_frame ) { |
199 | if ( current_frame > prev_frame + 1 ) { | 202 | if ( current_frame > prev_frame + 1 ) { |
200 | mediaPlayerState->curDecoder()->videoSetFrame( current_frame, stream ); | 203 | mediaPlayerState->curDecoder()->videoSetFrame( current_frame, stream ); |
201 | } | 204 | } |
202 | moreVideo = videoUI->playVideo(); | 205 | moreVideo = videoUI->playVideo(); |
203 | prev_frame = current_frame; | 206 | prev_frame = current_frame; |
204 | } | 207 | } |
205 | 208 | ||
206 | } else { | 209 | } else { |
207 | 210 | ||
208 | moreVideo = FALSE; | 211 | moreVideo = FALSE; |
209 | killTimer( videoId ); | 212 | killTimer( videoId ); |
210 | 213 | ||
211 | } | 214 | } |
212 | 215 | ||
213 | } | 216 | } |
214 | } | 217 | } |
215 | 218 | ||
216 | 219 | ||
217 | void LoopControl::startAudio() { | 220 | void LoopControl::startAudio() { |
218 | 221 | ||
219 | audioMutex->lock(); | 222 | audioMutex->lock(); |
220 | 223 | ||
221 | if ( moreAudio ) { | 224 | if ( moreAudio ) { |
222 | 225 | ||
223 | if ( !isMuted && mediaPlayerState->curDecoder() ) { | 226 | if ( !isMuted && mediaPlayerState->curDecoder() ) { |
224 | 227 | ||
225 | currentSample = audioSampleCounter + 1; | 228 | currentSample = audioSampleCounter + 1; |
226 | 229 | ||
227 | if ( currentSample != audioSampleCounter + 1 ) | 230 | if ( currentSample != audioSampleCounter + 1 ) |
228 | qDebug("out of sync with decoder %i %i", currentSample, audioSampleCounter); | 231 | qDebug("out of sync with decoder %i %i", currentSample, audioSampleCounter); |
229 | 232 | ||
230 | long samplesRead = 0; | 233 | long samplesRead = 0; |
231 | bool readOk=mediaPlayerState->curDecoder()->audioReadSamples( (short*)audioBuffer, channels, 1024, samplesRead, stream ); | 234 | bool readOk=mediaPlayerState->curDecoder()->audioReadSamples( (short*)audioBuffer, channels, 1024, samplesRead, stream ); |
232 | long sampleWeShouldBeAt = long( playtime.elapsed() ) * freq / 1000; | 235 | long sampleWeShouldBeAt = long( playtime.elapsed() ) * freq / 1000; |
233 | long sampleWaitTime = currentSample - sampleWeShouldBeAt; | 236 | long sampleWaitTime = currentSample - sampleWeShouldBeAt; |
234 | 237 | ||
235 | if ( ( sampleWaitTime > 2000 ) && ( sampleWaitTime < 20000 ) ) { | 238 | // if ( ( sampleWaitTime > 2000 ) && ( sampleWaitTime < 5000 ) ) { |
236 | usleep( (long)((double)sampleWaitTime * 1000000.0 / freq) ); | 239 | // usleep( (long)((double)sampleWaitTime * 1000000.0 / freq) ); |
237 | } else if ( sampleWaitTime <= -5000 ) { | 240 | // } |
238 | qDebug("need to catch up by: %li (%i,%li)", -sampleWaitTime, currentSample, sampleWeShouldBeAt ); | 241 | // else if ( sampleWaitTime <= -5000 ) { |
239 | //mediaPlayerState->curDecoder()->audioSetSample( sampleWeShouldBeAt, stream ); | 242 | // qDebug("need to catch up by: %li (%i,%li)", -sampleWaitTime, currentSample, sampleWeShouldBeAt ); |
240 | currentSample = sampleWeShouldBeAt; | 243 | // //mediaPlayerState->curDecoder()->audioSetSample( sampleWeShouldBeAt, stream ); |
241 | } | 244 | // currentSample = sampleWeShouldBeAt; |
245 | // } | ||
242 | 246 | ||
243 | audioDevice->write( audioBuffer, samplesRead * 2 * channels ); | 247 | audioDevice->write( audioBuffer, samplesRead * 2 * channels ); |
244 | audioSampleCounter = currentSample + samplesRead - 1; | 248 | audioSampleCounter = currentSample + samplesRead - 1; |
245 | 249 | ||
246 | moreAudio = readOk && (audioSampleCounter <= total_audio_samples); | 250 | moreAudio = readOk && (audioSampleCounter <= total_audio_samples); |
247 | 251 | ||
248 | } else { | 252 | } else { |
249 | 253 | ||
250 | moreAudio = FALSE; | 254 | moreAudio = FALSE; |
251 | 255 | ||
252 | } | 256 | } |
253 | 257 | ||
254 | } | 258 | } |
255 | 259 | ||
256 | audioMutex->unlock(); | 260 | audioMutex->unlock(); |
257 | } | 261 | } |
258 | 262 | ||
259 | 263 | ||
260 | void LoopControl::killTimers() { | 264 | void LoopControl::killTimers() { |
261 | 265 | ||
262 | audioMutex->lock(); | 266 | audioMutex->lock(); |
263 | 267 | ||
264 | if ( hasVideoChannel ) | 268 | if ( hasVideoChannel ) |
265 | killTimer( videoId ); | 269 | killTimer( videoId ); |
266 | killTimer( sliderId ); | 270 | killTimer( sliderId ); |
267 | threadOkToGo = FALSE; | 271 | threadOkToGo = FALSE; |
268 | 272 | ||
269 | audioMutex->unlock(); | 273 | audioMutex->unlock(); |
270 | } | 274 | } |
271 | 275 | ||
272 | 276 | ||
273 | void LoopControl::startTimers() { | 277 | void LoopControl::startTimers() { |
274 | 278 | ||
275 | audioMutex->lock(); | 279 | audioMutex->lock(); |
276 | 280 | ||
277 | moreVideo = FALSE; | 281 | moreVideo = FALSE; |
278 | moreAudio = FALSE; | 282 | moreAudio = FALSE; |
279 | 283 | ||
280 | if ( hasVideoChannel ) { | 284 | if ( hasVideoChannel ) { |
281 | moreVideo = TRUE; | 285 | moreVideo = TRUE; |
282 | int mSecsBetweenFrames = (int)(100 / framerate); // 10% of the real value | 286 | int mSecsBetweenFrames = (int)(100 / framerate); // 10% of the real value |
283 | videoId = startTimer( mSecsBetweenFrames ); | 287 | videoId = startTimer( mSecsBetweenFrames ); |
284 | } | 288 | } |
285 | 289 | ||
286 | if ( hasAudioChannel ) { | 290 | if ( hasAudioChannel ) { |
287 | moreAudio = TRUE; | 291 | moreAudio = TRUE; |
288 | threadOkToGo = TRUE; | 292 | threadOkToGo = TRUE; |
289 | } | 293 | } |
290 | 294 | ||
291 | sliderId = startTimer( 300 ); // update slider every 1/3 second | 295 | sliderId = startTimer( 300 ); // update slider every 1/3 second |
292 | 296 | ||
293 | audioMutex->unlock(); | 297 | audioMutex->unlock(); |
294 | } | 298 | } |
295 | 299 | ||
296 | 300 | ||
297 | void LoopControl::setPaused( bool pause ) { | 301 | void LoopControl::setPaused( bool pause ) { |
298 | 302 | ||
299 | if ( !mediaPlayerState->curDecoder() || !mediaPlayerState->curDecoder()->isOpen() ) | 303 | if ( !mediaPlayerState->curDecoder() || !mediaPlayerState->curDecoder()->isOpen() ) |
300 | return; | 304 | return; |
301 | 305 | ||
302 | if ( pause ) { | 306 | if ( pause ) { |
303 | killTimers(); | 307 | killTimers(); |
304 | } else { | 308 | } else { |
305 | // Force an update of the position | 309 | // Force an update of the position |
306 | mediaPlayerState->setPosition( mediaPlayerState->position() + 1 ); | 310 | mediaPlayerState->setPosition( mediaPlayerState->position() + 1 ); |
307 | mediaPlayerState->setPosition( mediaPlayerState->position() - 1 ); | 311 | mediaPlayerState->setPosition( mediaPlayerState->position() - 1 ); |
308 | // Just like we never stopped | 312 | // Just like we never stopped |
309 | startTimers(); | 313 | startTimers(); |
310 | } | 314 | } |
311 | } | 315 | } |
312 | 316 | ||
313 | 317 | ||
314 | void LoopControl::stop( bool willPlayAgainShortly ) { | 318 | void LoopControl::stop( bool willPlayAgainShortly ) { |
315 | 319 | ||
316 | #if defined(Q_WS_QWS) && !defined(QT_NO_COP) | 320 | #if defined(Q_WS_QWS) && !defined(QT_NO_COP) |
317 | if ( !willPlayAgainShortly && disabledSuspendScreenSaver ) { | 321 | if ( !willPlayAgainShortly && disabledSuspendScreenSaver ) { |
318 | disabledSuspendScreenSaver = FALSE; | 322 | disabledSuspendScreenSaver = FALSE; |
319 | // Re-enable the suspend mode | 323 | // Re-enable the suspend mode |
320 | QCopEnvelope("QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::Enable; | 324 | QCopEnvelope("QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::Enable; |
321 | } | 325 | } |
322 | #endif | 326 | #endif |
323 | 327 | ||
324 | if ( mediaPlayerState->curDecoder() && mediaPlayerState->curDecoder()->isOpen() ) { | 328 | if ( mediaPlayerState->curDecoder() && mediaPlayerState->curDecoder()->isOpen() ) { |
325 | 329 | ||
326 | killTimers(); | 330 | killTimers(); |
327 | 331 | ||
328 | audioMutex->lock(); | 332 | audioMutex->lock(); |
329 | 333 | ||
330 | mediaPlayerState->curDecoder()->close(); | 334 | mediaPlayerState->curDecoder()->close(); |
331 | 335 | ||
332 | if ( audioDevice ) { | 336 | if ( audioDevice ) { |
333 | delete audioDevice; | 337 | delete audioDevice; |
334 | delete audioBuffer; | 338 | delete audioBuffer; |
335 | audioDevice = 0; | 339 | audioDevice = 0; |
336 | audioBuffer = 0; | 340 | audioBuffer = 0; |
337 | } | 341 | } |
338 | 342 | ||
339 | audioMutex->unlock(); | 343 | audioMutex->unlock(); |
340 | 344 | ||
341 | } | 345 | } |
342 | } | 346 | } |
343 | 347 | ||
344 | 348 | ||
345 | bool LoopControl::init( const QString& filename ) { | 349 | bool LoopControl::init( const QString& filename ) { |
346 | stop(); | 350 | stop(); |
347 | 351 | ||
348 | audioMutex->lock(); | 352 | audioMutex->lock(); |
349 | 353 | ||
350 | fileName = filename; | 354 | fileName = filename; |
351 | stream = 0; // only play stream 0 for now | 355 | stream = 0; // only play stream 0 for now |
352 | current_frame = total_video_frames = total_audio_samples = 0; | 356 | current_frame = total_video_frames = total_audio_samples = 0; |
353 | 357 | ||
354 | qDebug( "Using the %s decoder", mediaPlayerState->curDecoder()->pluginName() ); | 358 | qDebug( "Using the %s decoder", mediaPlayerState->curDecoder()->pluginName() ); |
355 | 359 | ||
356 | // ### Hack to use libmpeg3plugin to get the number of audio samples if we are using the libmad plugin | 360 | // ### Hack to use libmpeg3plugin to get the number of audio samples if we are using the libmad plugin |
357 | if ( mediaPlayerState->curDecoder()->pluginName() == QString("LibMadPlugin") ) { | 361 | if ( mediaPlayerState->curDecoder()->pluginName() == QString("LibMadPlugin") ) { |
358 | if ( mediaPlayerState->libMpeg3Decoder() && mediaPlayerState->libMpeg3Decoder()->open( filename ) ) { | 362 | if ( mediaPlayerState->libMpeg3Decoder() && mediaPlayerState->libMpeg3Decoder()->open( filename ) ) { |
359 | total_audio_samples = mediaPlayerState->libMpeg3Decoder()->audioSamples( 0 ); | 363 | total_audio_samples = mediaPlayerState->libMpeg3Decoder()->audioSamples( 0 ); |
360 | mediaPlayerState->libMpeg3Decoder()->close(); | 364 | mediaPlayerState->libMpeg3Decoder()->close(); |
361 | } | 365 | } |
362 | } | 366 | } |
363 | 367 | ||
364 | if ( !mediaPlayerState->curDecoder()|| !mediaPlayerState->curDecoder()->open( filename ) ) { | 368 | if ( !mediaPlayerState->curDecoder()|| !mediaPlayerState->curDecoder()->open( filename ) ) { |
365 | audioMutex->unlock(); | 369 | audioMutex->unlock(); |
366 | return FALSE; | 370 | return FALSE; |
367 | } | 371 | } |
368 | 372 | ||
369 | hasAudioChannel = mediaPlayerState->curDecoder()->audioStreams() > 0; | 373 | hasAudioChannel = mediaPlayerState->curDecoder()->audioStreams() > 0; |
370 | hasVideoChannel = mediaPlayerState->curDecoder()->videoStreams() > 0; | 374 | hasVideoChannel = mediaPlayerState->curDecoder()->videoStreams() > 0; |
371 | 375 | ||
372 | if ( hasAudioChannel ) { | 376 | if ( hasAudioChannel ) { |
373 | int astream = 0; | 377 | int astream = 0; |
374 | 378 | ||
375 | channels = mediaPlayerState->curDecoder()->audioChannels( astream ); | 379 | channels = mediaPlayerState->curDecoder()->audioChannels( astream ); |
376 | DecodeLoopDebug(( "channels = %d\n", channels )); | 380 | qDebug( "LC- channels = %d", channels ); |
377 | 381 | ||
378 | if ( !total_audio_samples ) | 382 | if ( !total_audio_samples ) |
379 | total_audio_samples = mediaPlayerState->curDecoder()->audioSamples( astream ); | 383 | total_audio_samples = mediaPlayerState->curDecoder()->audioSamples( astream ); |
380 | 384 | ||
381 | total_audio_samples += 1000; | 385 | // total_audio_samples += 1000; |
382 | 386 | ||
383 | mediaPlayerState->setLength( total_audio_samples ); | 387 | mediaPlayerState->setLength( total_audio_samples ); |
384 | 388 | ||
385 | freq = mediaPlayerState->curDecoder()->audioFrequency( astream ); | 389 | freq = mediaPlayerState->curDecoder()->audioFrequency( astream ); |
386 | DecodeLoopDebug(( "frequency = %d\n", freq )); | 390 | qDebug( "LC- frequency = %d", freq ); |
387 | 391 | ||
388 | audioSampleCounter = 0; | 392 | audioSampleCounter = 0; |
389 | 393 | ||
390 | static const int bytes_per_sample = 2; //16 bit | 394 | int bits_per_sample = mediaPlayerState->curDecoder()->audioBitsPerSample( astream); |
391 | 395 | ||
392 | audioDevice = new AudioDevice( freq, channels, bytes_per_sample ); | 396 | audioDevice = new AudioDevice( freq, channels, bits_per_sample); |
393 | audioBuffer = new char[ audioDevice->bufferSize() ]; | 397 | audioBuffer = new char[ audioDevice->bufferSize() ]; |
394 | channels = audioDevice->channels(); | 398 | channels = audioDevice->channels(); |
395 | 399 | ||
396 | //### must check which frequency is actually used. | 400 | //### must check which frequency is actually used. |
397 | static const int size = 1; | 401 | static const int size = 1; |
398 | short int buf[size]; | 402 | short int buf[size]; |
399 | long samplesRead = 0; | 403 | long samplesRead = 0; |
400 | mediaPlayerState->curDecoder()->audioReadSamples( buf, channels, size, samplesRead, stream ); | 404 | mediaPlayerState->curDecoder()->audioReadSamples( buf, channels, size, samplesRead, stream ); |
401 | } | 405 | } |
402 | 406 | ||
403 | if ( hasVideoChannel ) { | 407 | if ( hasVideoChannel ) { |
404 | total_video_frames = mediaPlayerState->curDecoder()->videoFrames( stream ); | 408 | total_video_frames = mediaPlayerState->curDecoder()->videoFrames( stream ); |
405 | 409 | ||
406 | mediaPlayerState->setLength( total_video_frames ); | 410 | mediaPlayerState->setLength( total_video_frames ); |
407 | 411 | ||
408 | framerate = mediaPlayerState->curDecoder()->videoFrameRate( stream ); | 412 | framerate = mediaPlayerState->curDecoder()->videoFrameRate( stream ); |
409 | DecodeLoopDebug(( "Frame rate %g total %ld", framerate, total_video_frames )); | 413 | DecodeLoopDebug(( "Frame rate %g total %ld", framerate, total_video_frames )); |
410 | 414 | ||
411 | if ( framerate <= 1.0 ) { | 415 | if ( framerate <= 1.0 ) { |
412 | DecodeLoopDebug(( "Crazy frame rate, resetting to sensible" )); | 416 | DecodeLoopDebug(( "Crazy frame rate, resetting to sensible" )); |
413 | framerate = 25; | 417 | framerate = 25; |
414 | } | 418 | } |
415 | 419 | ||
416 | if ( total_video_frames == 1 ) { | 420 | if ( total_video_frames == 1 ) { |
417 | DecodeLoopDebug(( "Cannot seek to frame" )); | 421 | DecodeLoopDebug(( "Cannot seek to frame" )); |
418 | } | 422 | } |
419 | 423 | ||
420 | } | 424 | } |
421 | 425 | ||
422 | current_frame = 0; | 426 | current_frame = 0; |
423 | prev_frame = -1; | 427 | prev_frame = -1; |
424 | 428 | ||
425 | connect( mediaPlayerState, SIGNAL( positionChanged( long ) ), this, SLOT( setPosition( long ) ) ); | 429 | connect( mediaPlayerState, SIGNAL( positionChanged( long ) ), this, SLOT( setPosition( long ) ) ); |
426 | connect( mediaPlayerState, SIGNAL( pausedToggled( bool ) ), this, SLOT( setPaused( bool ) ) ); | 430 | connect( mediaPlayerState, SIGNAL( pausedToggled( bool ) ), this, SLOT( setPaused( bool ) ) ); |
427 | 431 | ||
428 | audioMutex->unlock(); | 432 | audioMutex->unlock(); |
429 | 433 | ||
430 | return TRUE; | 434 | return TRUE; |
431 | } | 435 | } |
432 | 436 | ||
433 | 437 | ||
434 | void LoopControl::play() { | 438 | void LoopControl::play() { |
435 | 439 | ||
436 | #if defined(Q_WS_QWS) && !defined(QT_NO_COP) | 440 | #if defined(Q_WS_QWS) && !defined(QT_NO_COP) |
437 | if ( !disabledSuspendScreenSaver || previousSuspendMode != hasVideoChannel ) { | 441 | if ( !disabledSuspendScreenSaver || previousSuspendMode != hasVideoChannel ) { |
438 | disabledSuspendScreenSaver = TRUE; | 442 | disabledSuspendScreenSaver = TRUE; |
439 | previousSuspendMode = hasVideoChannel; | 443 | previousSuspendMode = hasVideoChannel; |
440 | // Stop the screen from blanking and power saving state | 444 | // Stop the screen from blanking and power saving state |
441 | QCopEnvelope("QPE/System", "setScreenSaverMode(int)" ) | 445 | QCopEnvelope("QPE/System", "setScreenSaverMode(int)" ) |
442 | << ( hasVideoChannel ? QPEApplication::Disable : QPEApplication::DisableSuspend ); | 446 | << ( hasVideoChannel ? QPEApplication::Disable : QPEApplication::DisableSuspend ); |
443 | } | 447 | } |
444 | #endif | 448 | #endif |
445 | 449 | ||
446 | playtime.start(); | 450 | playtime.start(); |
447 | startTimers(); | 451 | startTimers(); |
448 | } | 452 | } |
449 | 453 | ||
450 | 454 | ||
451 | void LoopControl::setMute( bool on ) { | 455 | void LoopControl::setMute( bool on ) { |
452 | if ( on != isMuted ) { | 456 | if ( on != isMuted ) { |
453 | isMuted = on; | 457 | isMuted = on; |
454 | if ( !on ) { | 458 | if ( !on ) { |
455 | // Force an update of the position | 459 | // Force an update of the position |
456 | mediaPlayerState->setPosition( mediaPlayerState->position() + 1 ); | 460 | mediaPlayerState->setPosition( mediaPlayerState->position() + 1 ); |
457 | mediaPlayerState->setPosition( mediaPlayerState->position() - 1 ); | 461 | mediaPlayerState->setPosition( mediaPlayerState->position() - 1 ); |
458 | // Resume playing audio | 462 | // Resume playing audio |
459 | moreAudio = TRUE; | 463 | moreAudio = TRUE; |
460 | } | 464 | } |
461 | } | 465 | } |
462 | } | 466 | } |
463 | 467 | ||
464 | 468 | ||