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