summaryrefslogtreecommitdiff
path: root/core/multimedia/opieplayer/loopcontrol.cpp
Unidiff
Diffstat (limited to 'core/multimedia/opieplayer/loopcontrol.cpp') (more/less context) (show whitespace changes)
-rw-r--r--core/multimedia/opieplayer/loopcontrol.cpp38
1 files changed, 20 insertions, 18 deletions
diff --git a/core/multimedia/opieplayer/loopcontrol.cpp b/core/multimedia/opieplayer/loopcontrol.cpp
index 1ae0059..dd466ed 100644
--- a/core/multimedia/opieplayer/loopcontrol.cpp
+++ b/core/multimedia/opieplayer/loopcontrol.cpp
@@ -1,48 +1,46 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved. 2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of Qtopia Environment. 4** This file is part of the 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
22#include <qpe/qpeapplication.h> 20#include <qpe/qpeapplication.h>
23
24#ifdef Q_WS_QWS 21#ifdef Q_WS_QWS
22
25#include <qpe/qcopenvelope_qws.h> 23#include <qpe/qcopenvelope_qws.h>
26#endif 24#endif
25#include <qpe/mediaplayerplugininterface.h>
27#include <stdio.h> 26#include <stdio.h>
28#include <stdlib.h> 27#include <stdlib.h>
29#include <string.h> 28#include <string.h>
30#include <pthread.h> 29#include <pthread.h>
31#include <errno.h> 30#include <errno.h>
32#include <unistd.h> 31#include <unistd.h>
33#include "loopcontrol.h" 32#include "loopcontrol.h"
34#include "videowidget.h" 33#include "videowidget.h"
35#include "audiodevice.h" 34#include "audiodevice.h"
36#include "mediaplayerplugininterface.h"
37#include "mediaplayerstate.h" 35#include "mediaplayerstate.h"
38 36
39 37
40extern VideoWidget *videoUI; // now only needed to tell it to play a frame 38extern VideoWidget *videoUI; // now only needed to tell it to play a frame
41extern MediaPlayerState *mediaPlayerState; 39extern MediaPlayerState *mediaPlayerState;
42 40
43 41
44//#define DecodeLoopDebug(x) qDebug x 42//#define DecodeLoopDebug(x) qDebug x
45#define DecodeLoopDebug(x) 43#define DecodeLoopDebug(x)
46 44
47 45
48static char *audioBuffer = NULL; 46static char *audioBuffer = NULL;
@@ -95,37 +93,42 @@ void *startAudioThread( void *ptr ) {
95 93
96Mutex *audioMutex; 94Mutex *audioMutex;
97 95
98 96
99LoopControl::LoopControl( QObject *parent, const char *name ) 97LoopControl::LoopControl( QObject *parent, const char *name )
100 : QObject( parent, name ) { 98 : QObject( parent, name ) {
101 isMuted = FALSE; 99 isMuted = FALSE;
102 connect( qApp, SIGNAL( volumeChanged(bool) ), this, SLOT( setMute(bool) ) ); 100 connect( qApp, SIGNAL( volumeChanged(bool) ), this, SLOT( setMute(bool) ) );
103 101
104 audioMutex = new Mutex; 102 audioMutex = new Mutex;
105 103
106 pthread_attr_init(&audio_attr); 104 pthread_attr_init(&audio_attr);
107#define USE_REALTIME_AUDIO_THREAD 105
108#ifdef USE_REALTIME_AUDIO_THREAD 106 if ( getuid() == 0 ) {
107 printf("true, guid = %i\n", getuid());
108
109 // Attempt to set it to real-time round robin 109 // Attempt to set it to real-time round robin
110 if ( pthread_attr_setschedpolicy( &audio_attr, SCHED_RR ) == 0 ) { 110 if ( pthread_attr_setschedpolicy( &audio_attr, SCHED_RR ) == 0 ) {
111 sched_param params; 111 sched_param params;
112 params.sched_priority = 50; 112 params.sched_priority = 50;
113 pthread_attr_setschedparam(&audio_attr,&params); 113 pthread_attr_setschedparam(&audio_attr,&params);
114 } else { 114 } else {
115 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." );
116 pthread_attr_destroy(&audio_attr); 116 pthread_attr_destroy(&audio_attr);
117 pthread_attr_init(&audio_attr); 117 pthread_attr_init(&audio_attr);
118 } 118 }
119#endif 119 }
120// printf("false, guid = %i\n", getuid());
121 usleep( 100 );
122
120 pthread_create(&audio_tid, &audio_attr, (void * (*)(void *))startAudioThread, this); 123 pthread_create(&audio_tid, &audio_attr, (void * (*)(void *))startAudioThread, this);
121} 124}
122 125
123 126
124LoopControl::~LoopControl() { 127LoopControl::~LoopControl() {
125 stop(); 128 stop();
126} 129}
127 130
128 131
129static long prev_frame = 0; 132static long prev_frame = 0;
130static int currentSample = 0; 133static int currentSample = 0;
131 134
@@ -222,41 +225,40 @@ void LoopControl::startAudio() {
222 audioMutex->lock(); 225 audioMutex->lock();
223 226
224 if ( moreAudio ) { 227 if ( moreAudio ) {
225 228
226 if ( !isMuted && mediaPlayerState->curDecoder() ) { 229 if ( !isMuted && mediaPlayerState->curDecoder() ) {
227 230
228 currentSample = audioSampleCounter + 1; 231 currentSample = audioSampleCounter + 1;
229 232
230 if ( currentSample != audioSampleCounter + 1 ) 233 if ( currentSample != audioSampleCounter + 1 )
231 qDebug("out of sync with decoder %i %i", currentSample, audioSampleCounter); 234 qDebug("out of sync with decoder %i %i", currentSample, audioSampleCounter);
232 235
233 long samplesRead = 0; 236 long samplesRead = 0;
234 bool readOk=mediaPlayerState->curDecoder()->audioReadSamples( (short*)audioBuffer, channels, 1024, samplesRead, stream ); 237 mediaPlayerState->curDecoder()->audioReadSamples( (short*)audioBuffer, channels, 1024, samplesRead, stream );
235 long sampleWeShouldBeAt = long( playtime.elapsed() ) * freq / 1000; 238 long sampleWeShouldBeAt = long( playtime.elapsed() ) * freq / 1000;
236 long sampleWaitTime = currentSample - sampleWeShouldBeAt; 239 long sampleWaitTime = currentSample - sampleWeShouldBeAt;
237 240
238 if ( ( sampleWaitTime > 2000 ) && ( sampleWaitTime < 20000 ) ) { 241 if ( ( sampleWaitTime > 2000 ) && ( sampleWaitTime < 20000 ) ) {
239 usleep( (long)((double)sampleWaitTime * 1000000.0 / freq) ); 242 usleep( (long)((double)sampleWaitTime * 1000000.0 / freq) );
240 } 243 } else if ( sampleWaitTime <= -5000 ) {
241 else if ( sampleWaitTime <= -5000 ) {
242 qDebug("need to catch up by: %li (%i,%li)", -sampleWaitTime, currentSample, sampleWeShouldBeAt ); 244 qDebug("need to catch up by: %li (%i,%li)", -sampleWaitTime, currentSample, sampleWeShouldBeAt );
243 //mediaPlayerState->curDecoder()->audioSetSample( sampleWeShouldBeAt, stream ); 245 //mediaPlayerState->curDecoder()->audioSetSample( sampleWeShouldBeAt, stream );
244 currentSample = sampleWeShouldBeAt; 246 currentSample = sampleWeShouldBeAt;
245 } 247 }
246 248
247 audioDevice->write( audioBuffer, samplesRead * 2 * channels ); 249 audioDevice->write( audioBuffer, samplesRead * 2 * channels );
248 audioSampleCounter = currentSample + samplesRead - 1; 250 audioSampleCounter = currentSample + samplesRead - 1;
249 251
250 moreAudio = readOk && (audioSampleCounter <= total_audio_samples); 252 moreAudio = audioSampleCounter <= total_audio_samples;
251 253
252 } else { 254 } else {
253 255
254 moreAudio = FALSE; 256 moreAudio = FALSE;
255 257
256 } 258 }
257 259
258 } 260 }
259 261
260 audioMutex->unlock(); 262 audioMutex->unlock();
261} 263}
262 264
@@ -368,41 +370,41 @@ bool LoopControl::init( const QString& filename ) {
368 if ( !mediaPlayerState->curDecoder()|| !mediaPlayerState->curDecoder()->open( filename ) ) { 370 if ( !mediaPlayerState->curDecoder()|| !mediaPlayerState->curDecoder()->open( filename ) ) {
369 audioMutex->unlock(); 371 audioMutex->unlock();
370 return FALSE; 372 return FALSE;
371 } 373 }
372 374
373 hasAudioChannel = mediaPlayerState->curDecoder()->audioStreams() > 0; 375 hasAudioChannel = mediaPlayerState->curDecoder()->audioStreams() > 0;
374 hasVideoChannel = mediaPlayerState->curDecoder()->videoStreams() > 0; 376 hasVideoChannel = mediaPlayerState->curDecoder()->videoStreams() > 0;
375 377
376 if ( hasAudioChannel ) { 378 if ( hasAudioChannel ) {
377 int astream = 0; 379 int astream = 0;
378 380
379 channels = mediaPlayerState->curDecoder()->audioChannels( astream ); 381 channels = mediaPlayerState->curDecoder()->audioChannels( astream );
380 qDebug( "LC- channels = %d", channels ); 382 DecodeLoopDebug(( "channels = %d\n", channels ));
381 383
382 if ( !total_audio_samples ) 384 if ( !total_audio_samples )
383 total_audio_samples = mediaPlayerState->curDecoder()->audioSamples( astream ); 385 total_audio_samples = mediaPlayerState->curDecoder()->audioSamples( astream );
384 386
385// total_audio_samples += 1000; 387 total_audio_samples += 1000;
386 388
387 mediaPlayerState->setLength( total_audio_samples ); 389 mediaPlayerState->setLength( total_audio_samples );
388 390
389 freq = mediaPlayerState->curDecoder()->audioFrequency( astream ); 391 freq = mediaPlayerState->curDecoder()->audioFrequency( astream );
390 qDebug( "LC- frequency = %d", freq ); 392 DecodeLoopDebug(( "frequency = %d\n", freq ));
391 393
392 audioSampleCounter = 0; 394 audioSampleCounter = 0;
393 395
394 int bits_per_sample = mediaPlayerState->curDecoder()->audioBitsPerSample( astream); 396 static const int bytes_per_sample = 2; //16 bit
395 397
396 audioDevice = new AudioDevice( freq, channels, bits_per_sample); 398 audioDevice = new AudioDevice( freq, channels, bytes_per_sample );
397 audioBuffer = new char[ audioDevice->bufferSize() ]; 399 audioBuffer = new char[ audioDevice->bufferSize() ];
398 channels = audioDevice->channels(); 400 channels = audioDevice->channels();
399 401
400 //### must check which frequency is actually used. 402 //### must check which frequency is actually used.
401 static const int size = 1; 403 static const int size = 1;
402 short int buf[size]; 404 short int buf[size];
403 long samplesRead = 0; 405 long samplesRead = 0;
404 mediaPlayerState->curDecoder()->audioReadSamples( buf, channels, size, samplesRead, stream ); 406 mediaPlayerState->curDecoder()->audioReadSamples( buf, channels, size, samplesRead, stream );
405 } 407 }
406 408
407 if ( hasVideoChannel ) { 409 if ( hasVideoChannel ) {
408 total_video_frames = mediaPlayerState->curDecoder()->videoFrames( stream ); 410 total_video_frames = mediaPlayerState->curDecoder()->videoFrames( stream );