summaryrefslogtreecommitdiff
path: root/core/multimedia/opieplayer/loopcontrol.cpp
Unidiff
Diffstat (limited to 'core/multimedia/opieplayer/loopcontrol.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--core/multimedia/opieplayer/loopcontrol.cpp328
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
@@ -19,3 +19,6 @@
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
@@ -44,9 +47,9 @@ extern MediaPlayerState *mediaPlayerState;
44 47
45 static char *audioBuffer = NULL; 48static char *audioBuffer = NULL;
46static AudioDevice *audioDevice = NULL; 49static AudioDevice *audioDevice = NULL;
47 static bool disabledSuspendScreenSaver = FALSE; 50static bool disabledSuspendScreenSaver = FALSE;
48 static bool previousSuspendMode = FALSE; 51static bool previousSuspendMode = FALSE;
49 52
50 53
51 pthread_taudio_tid; 54pthread_t audio_tid;
52pthread_attr_t audio_attr; 55pthread_attr_t audio_attr;
@@ -58,6 +61,6 @@ 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 }
@@ -65,3 +68,3 @@ public:
65 ~Mutex() { 68 ~Mutex() {
66 pthread_mutex_destroy( &mutex ); 69 pthread_mutex_destroy( &mutex );
67 } 70 }
@@ -69,3 +72,3 @@ public:
69 void lock() { 72 void lock() {
70 pthread_mutex_lock( &mutex ); 73 pthread_mutex_lock( &mutex );
71 } 74 }
@@ -73,3 +76,3 @@ public:
73 void unlock() { 76 void unlock() {
74 pthread_mutex_unlock( &mutex ); 77 pthread_mutex_unlock( &mutex );
75 } 78 }
@@ -83,6 +86,6 @@ void *startAudioThread( void *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 }
@@ -107,9 +110,9 @@ LoopControl::LoopControl( QObject *parent, const char *name )
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,&params); 113 pthread_attr_setschedparam(&audio_attr,&params);
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 }
@@ -132,10 +135,10 @@ void LoopControl::timerEvent( QTimerEvent *te ) {
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 }
@@ -153,22 +156,22 @@ void LoopControl::setPosition( long pos ) {
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 }
@@ -185,28 +188,28 @@ void LoopControl::startVideo() {
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
@@ -222,32 +225,33 @@ void LoopControl::startAudio() {
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
@@ -264,3 +268,3 @@ void LoopControl::killTimers() {
264 if ( hasVideoChannel ) 268 if ( hasVideoChannel )
265 killTimer( videoId ); 269 killTimer( videoId );
266 killTimer( sliderId ); 270 killTimer( sliderId );
@@ -280,5 +284,5 @@ void LoopControl::startTimers() {
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 }
@@ -286,4 +290,4 @@ void LoopControl::startTimers() {
286 if ( hasAudioChannel ) { 290 if ( hasAudioChannel ) {
287 moreAudio = TRUE; 291 moreAudio = TRUE;
288 threadOkToGo = TRUE; 292 threadOkToGo = TRUE;
289 } 293 }
@@ -299,12 +303,12 @@ void LoopControl::setPaused( bool pause ) {
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 }
@@ -317,5 +321,5 @@ void LoopControl::stop( bool willPlayAgainShortly ) {
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 }
@@ -325,16 +329,16 @@ void LoopControl::stop( bool willPlayAgainShortly ) {
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
@@ -357,11 +361,11 @@ bool LoopControl::init( const QString& filename ) {
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 }
@@ -372,30 +376,30 @@ bool LoopControl::init( const QString& filename ) {
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 }
@@ -403,17 +407,17 @@ bool LoopControl::init( const QString& filename ) {
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
@@ -437,7 +441,7 @@ void LoopControl::play() {
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 }
@@ -452,10 +456,10 @@ 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 }