author | zecke <zecke> | 2003-10-07 12:53:39 (UTC) |
---|---|---|
committer | zecke <zecke> | 2003-10-07 12:53:39 (UTC) |
commit | e65d5c86b5fff521dfb94282a96606546bad9585 (patch) (side-by-side diff) | |
tree | 7aee07687deec9dec91031a0a7f70fc802d47c06 | |
parent | 877f982ab6996b85870befffcacdef84a6700746 (diff) | |
download | opie-e65d5c86b5fff521dfb94282a96606546bad9585.zip opie-e65d5c86b5fff521dfb94282a96606546bad9585.tar.gz opie-e65d5c86b5fff521dfb94282a96606546bad9585.tar.bz2 |
-Kill stupid QCOP check
-introduce OPIE_SOUND_FRAGMENT_SHIFT
-USE_REALTIME_AUDIO_THREAD is now defined in custom.h
-rw-r--r-- | core/multimedia/opieplayer/audiodevice.cpp | 9 | ||||
-rw-r--r-- | core/multimedia/opieplayer/loopcontrol_threaded.cpp | 7 |
2 files changed, 8 insertions, 8 deletions
diff --git a/core/multimedia/opieplayer/audiodevice.cpp b/core/multimedia/opieplayer/audiodevice.cpp index 6a38fc9..d296d27 100644 --- a/core/multimedia/opieplayer/audiodevice.cpp +++ b/core/multimedia/opieplayer/audiodevice.cpp @@ -1,305 +1,306 @@ /********************************************************************** ** Copyright (C) 2000 Trolltech AS. All rights reserved. ** ** This file is part of Qtopia Environment. ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ** See http://www.trolltech.com/gpl/ for GPL licensing information. ** ** Contact info@trolltech.com if any conditions of this licensing are ** not clear to you. ** **********************************************************************/ // L.J.Potter added better error code Fri 02-15-2002 14:37:47 #include <stdlib.h> #include <stdio.h> #include <qpe/qpeapplication.h> #include <qpe/config.h> +#include <qpe/custom.h> #include <qmessagebox.h> #include "audiodevice.h" #include <errno.h> -#if ( defined Q_WS_QWS || defined(_WS_QWS_) ) && !defined(QT_NO_COP) -#include "qpe/qcopenvelope_qws.h" +#if !defined(QT_NO_COP) +#include <qpe/qcopenvelope_qws.h> #endif #if defined(Q_WS_X11) || defined(Q_WS_QWS) #include <fcntl.h> #include <sys/ioctl.h> #include <sys/soundcard.h> #include <sys/stat.h> #include <sys/time.h> #include <sys/types.h> #include <unistd.h> #endif -# if defined(QT_QWS_IPAQ) -static const int sound_fragment_shift = 14; +#ifdef OPIE_SOUND_FRAGMENT_SHIFT +static const int sound_fragment_shift = OPIE_SOUND_FRAGMENT_SHIFT; # else static const int sound_fragment_shift = 16; # endif static const int sound_fragment_bytes = (1<<sound_fragment_shift); //#endif class AudioDevicePrivate { public: int handle; unsigned int frequency; unsigned int channels; unsigned int bytesPerSample; unsigned int bufferSize; //#ifndef Q_OS_WIN32 bool can_GETOSPACE; char* unwrittenBuffer; unsigned int unwritten; //#endif static int dspFd; static bool muted; static unsigned int leftVolume; static unsigned int rightVolume; }; #ifdef Q_WS_QWS // This is for keeping the device open in-between playing files when // the device makes clicks and it starts to drive you insane! :) // Best to have the device not open when not using it though //#define KEEP_DEVICE_OPEN #endif int AudioDevicePrivate::dspFd = 0; bool AudioDevicePrivate::muted = FALSE; unsigned int AudioDevicePrivate::leftVolume = 0; unsigned int AudioDevicePrivate::rightVolume = 0; void AudioDevice::getVolume( unsigned int& leftVolume, unsigned int& rightVolume, bool &muted ) { muted = AudioDevicePrivate::muted; unsigned int volume; #ifdef QT_QWS_DEVFS int mixerHandle = open( "/dev/sound/mixer", O_RDWR ); #else int mixerHandle = open( "/dev/mixer", O_RDWR ); #endif if ( mixerHandle >= 0 ) { if(ioctl( mixerHandle, MIXER_READ(0), &volume )==-1) perror("ioctl(\"MIXER_READ\")"); close( mixerHandle ); } else perror("open(\"/dev/mixer\")"); leftVolume = ((volume & 0x00FF) << 16) / 101; rightVolume = ((volume & 0xFF00) << 8) / 101; } void AudioDevice::setVolume( unsigned int leftVolume, unsigned int rightVolume, bool muted ) { AudioDevicePrivate::muted = muted; if ( muted ) { AudioDevicePrivate::leftVolume = leftVolume; AudioDevicePrivate::rightVolume = rightVolume; leftVolume = 0; rightVolume = 0; } else { leftVolume = ( (int) leftVolume < 0 ) ? 0 : (( leftVolume > 0xFFFF ) ? 0xFFFF : leftVolume ); rightVolume = ( (int)rightVolume < 0 ) ? 0 : (( rightVolume > 0xFFFF ) ? 0xFFFF : rightVolume ); } // Volume can be from 0 to 100 which is 101 distinct values unsigned int rV = (rightVolume * 101) >> 16; # if 0 unsigned int lV = (leftVolume * 101) >> 16; unsigned int volume = ((rV << 8) & 0xFF00) | (lV & 0x00FF); int mixerHandle = 0; #ifdef QT_QWS_DEVFS if ( ( mixerHandle = open( "/dev/sound/mixer", O_RDWR ) ) >= 0 ) { #else if ( ( mixerHandle = open( "/dev/mixer", O_RDWR ) ) >= 0 ) { #endif if(ioctl( mixerHandle, MIXER_WRITE(0), &volume ) ==-1) perror("ioctl(\"MIXER_WRITE\")"); close( mixerHandle ); } else perror("open(\"/dev/mixer\")"); # else // This is the way this has to be done now I guess, doesn't allow for // independant right and left channel setting, or setting for different outputs Config cfg("qpe"); // qtopia is "Sound" cfg.setGroup("Volume"); // qtopia is "Settings" cfg.writeEntry("VolumePercent",(int)rV); //qtopia is Volume # endif //#endif // qDebug( "setting volume to: 0x%x", volume ); #if ( defined Q_WS_QWS || defined(_WS_QWS_) ) && !defined(QT_NO_COP) // Send notification that the volume has changed QCopEnvelope( "QPE/System", "volumeChange(bool)" ) << muted; #endif } AudioDevice::AudioDevice( unsigned int f, unsigned int chs, unsigned int bps ) { // qDebug("creating new audio device"); // QCopEnvelope( "QPE/System", "volumeChange(bool)" ) << TRUE; d = new AudioDevicePrivate; d->frequency = f; d->channels = chs; d->bytesPerSample = bps; // qDebug("%d",bps); int format=0; if( bps == 8) format = AFMT_U8; else if( bps <= 0) format = AFMT_S16_LE; else format = AFMT_S16_LE; // qDebug("AD- freq %d, channels %d, b/sample %d, bitrate %d",f,chs,bps,format); connect( qApp, SIGNAL( volumeChanged(bool) ), this, SLOT( volumeChanged(bool) ) ); int fragments = 0x10000 * 8 + sound_fragment_shift; int capabilities = 0; #ifdef KEEP_DEVICE_OPEN if ( AudioDevicePrivate::dspFd == 0 ) { #endif #ifdef QT_QWS_DEVFS if ( ( d->handle = ::open( "/dev/sound/dsp", O_WRONLY ) ) < 0 ) { #else if ( ( d->handle = ::open( "/dev/dsp", O_WRONLY ) ) < 0 ) { #endif perror("open(\"/dev/dsp\")"); QString errorMsg=tr("Somethin's wrong with\nyour sound device.\nopen(\"/dev/dsp\")\n")+(QString)strerror(errno)+tr("\n\nClosing player now."); QMessageBox::critical(0, "Vmemo", errorMsg, tr("Abort")); exit(-1); //harsh? } #ifdef KEEP_DEVICE_OPEN AudioDevicePrivate::dspFd = d->handle; } else { d->handle = AudioDevicePrivate::dspFd; } #endif if(ioctl( d->handle, SNDCTL_DSP_GETCAPS, &capabilities )==-1) perror("ioctl(\"SNDCTL_DSP_GETCAPS\")"); if(ioctl( d->handle, SNDCTL_DSP_SETFRAGMENT, &fragments )==-1) perror("ioctl(\"SNDCTL_DSP_SETFRAGMENT\")"); if(ioctl( d->handle, SNDCTL_DSP_SETFMT, & format )==-1) perror("ioctl(\"SNDCTL_DSP_SETFMT\")"); // qDebug("freq %d", d->frequency); if(ioctl( d->handle, SNDCTL_DSP_SPEED, &d->frequency )==-1) perror("ioctl(\"SNDCTL_DSP_SPEED\")"); // qDebug("channels %d",d->channels); if ( ioctl( d->handle, SNDCTL_DSP_CHANNELS, &d->channels ) == -1 ) { d->channels = ( d->channels == 1 ) ? 2 : d->channels; if(ioctl( d->handle, SNDCTL_DSP_CHANNELS, &d->channels )==-1) perror("ioctl(\"SNDCTL_DSP_CHANNELS\")"); } // QCopEnvelope( "QPE/System", "volumeChange(bool)" ) << FALSE; d->bufferSize = sound_fragment_bytes; d->unwrittenBuffer = new char[d->bufferSize]; d->unwritten = 0; d->can_GETOSPACE = TRUE; // until we find otherwise //if ( chs != d->channels ) qDebug( "Wanted %d, got %d channels", chs, d->channels ); //if ( f != d->frequency ) qDebug( "wanted %dHz, got %dHz", f, d->frequency ); //if ( capabilities & DSP_CAP_BATCH ) qDebug( "Sound card has local buffer" ); //if ( capabilities & DSP_CAP_REALTIME )qDebug( "Sound card has realtime sync" ); //if ( capabilities & DSP_CAP_TRIGGER ) qDebug( "Sound card has precise trigger" ); //if ( capabilities & DSP_CAP_MMAP ) qDebug( "Sound card can mmap" ); } AudioDevice::~AudioDevice() { // qDebug("destryo audiodevice"); // QCopEnvelope( "QPE/System", "volumeChange(bool)" ) << TRUE; # ifndef KEEP_DEVICE_OPEN close( d->handle ); // Now it should be safe to shut the handle # endif delete d->unwrittenBuffer; delete d; // QCopEnvelope( "QPE/System", "volumeChange(bool)" ) << FALSE; } void AudioDevice::volumeChanged( bool muted ) { AudioDevicePrivate::muted = muted; } void AudioDevice::write( char *buffer, unsigned int length ) { int t = ::write( d->handle, buffer, length ); if ( t<0 ) t = 0; if ( t != (int)length) { // qDebug("Ahhh!! memcpys 1"); memcpy(d->unwrittenBuffer,buffer+t,length-t); d->unwritten = length-t; } //#endif } unsigned int AudioDevice::channels() const { return d->channels; } unsigned int AudioDevice::frequency() const { return d->frequency; } unsigned int AudioDevice::bytesPerSample() const { return d->bytesPerSample; } unsigned int AudioDevice::bufferSize() const { return d->bufferSize; } unsigned int AudioDevice::canWrite() const { audio_buf_info info; if ( d->can_GETOSPACE && ioctl(d->handle,SNDCTL_DSP_GETOSPACE,&info) ) { d->can_GETOSPACE = FALSE; fcntl( d->handle, F_SETFL, O_NONBLOCK ); } if ( d->can_GETOSPACE ) { int t = info.fragments * sound_fragment_bytes; return QMIN(t,(int)bufferSize()); } else { if ( d->unwritten ) { int t = ::write( d->handle, d->unwrittenBuffer, d->unwritten ); if ( t<0 ) t = 0; if ( (unsigned)t!=d->unwritten ) { memcpy(d->unwrittenBuffer,d->unwrittenBuffer+t,d->unwritten-t); d->unwritten -= t; } else { d->unwritten = 0; } } diff --git a/core/multimedia/opieplayer/loopcontrol_threaded.cpp b/core/multimedia/opieplayer/loopcontrol_threaded.cpp index 364e77b..3796549 100644 --- a/core/multimedia/opieplayer/loopcontrol_threaded.cpp +++ b/core/multimedia/opieplayer/loopcontrol_threaded.cpp @@ -1,300 +1,299 @@ /********************************************************************** ** Copyright (C) 2000 Trolltech AS. All rights reserved. ** ** This file is part of Qtopia Environment. ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ** See http://www.trolltech.com/gpl/ for GPL licensing information. ** ** Contact info@trolltech.com if any conditions of this licensing are ** not clear to you. ** **********************************************************************/ #define _REENTRANT #include <qpe/qpeapplication.h> +#include <qpe/custom.h> #include <qimage.h> #include <qpainter.h> -#ifdef Q_WS_QWS +#if !defined(QT_NO_COP) #include <qpe/qcopenvelope_qws.h> #endif #include "mediaplayerplugininterface.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> #include <pthread.h> #include "loopcontrol.h" #include "audiodevice.h" #include "videowidget.h" #include "audiowidget.h" #include "mediaplayerstate.h" -#if defined(QT_QWS_SL5XXX) || defined(QT_QWS_IPAQ) || defined(QT_QWS_RAMSES) -#define USE_REALTIME_AUDIO_THREAD -#endif + extern VideoWidget *videoUI; // now only needed to tell it to play a frame extern MediaPlayerState *mediaPlayerState; #define DecodeLoopDebug(x) qDebug x //#define DecodeLoopDebug(x) static char *audioBuffer = NULL; static AudioDevice *audioDevice = NULL; static bool disabledSuspendScreenSaver = FALSE; pthread_t video_tid; pthread_attr_t video_attr; pthread_t audio_tid; pthread_attr_t audio_attr; bool emitPlayFinished = FALSE; bool emitChangePos = FALSE; class Mutex { public: Mutex() { pthread_mutexattr_t attr; pthread_mutexattr_init( &attr ); pthread_mutex_init( &mutex, &attr ); pthread_mutexattr_destroy( &attr ); } ~Mutex() { pthread_mutex_destroy( &mutex ); } void lock() { pthread_mutex_lock( &mutex ); } void unlock() { pthread_mutex_unlock( &mutex ); } /* bool locked() { switch ( pthread_mutex_trylock( &mutex ) ) { case EBUSY: return TRUE; case 0: pthread_mutex_unlock( &mutex ); default: return FALSE; } } */ private: pthread_mutex_t mutex; }; class currentFrameObj { public: currentFrameObj() : value( 0 ) { } void set( long f ) { mutex.lock(); value = f; mediaPlayerState->curDecoder()->videoSetFrame( f, 0 ); mutex.unlock(); } long get() { return value; } private: long value; Mutex mutex; }; Mutex *videoMutex; Mutex *audioMutex; Mutex *globalMutex; clock_t begin; LoopControl::LoopControl( QObject *parent, const char *name ) : QObject( parent, name ) { isMuted = FALSE; connect( qApp, SIGNAL( volumeChanged(bool) ), this, SLOT( setMute(bool) ) ); timerid = startTimer( 200 ); videoMutex = new Mutex; audioMutex = new Mutex; globalMutex = new Mutex; //begin = clock(); } LoopControl::~LoopControl() { stop(); killTimer( timerid ); } static bool sendingNewPos = FALSE; static long prev_frame = 0; static int currentSample = 0; void LoopControl::timerEvent( QTimerEvent* ) { // We need to emit playFinished from the main thread, not one of the // decoding threads else we'll have all kinds of yucky things happen (reentrance). // playFinished will eventually call stop() which stops these threads. if ( emitPlayFinished ) { emitPlayFinished = FALSE; mediaPlayerState->setPlaying( FALSE ); } if ( emitChangePos ) { emitChangePos = FALSE; if ( hasVideoChannel && hasAudioChannel ) { sendingNewPos = TRUE; mediaPlayerState->setPosition( current_frame ); } else if ( hasVideoChannel ) { sendingNewPos = TRUE; mediaPlayerState->setPosition( current_frame ); } else if ( hasAudioChannel ) { sendingNewPos = TRUE; mediaPlayerState->setPosition( audioSampleCounter ); } } } void LoopControl::setPosition( long pos ) { if ( sendingNewPos ) { sendingNewPos = FALSE; return; } if ( hasVideoChannel && hasAudioChannel ) { videoMutex->lock(); audioMutex->lock(); //qDebug("setting position"); playtime.restart(); playtime = playtime.addMSecs( -pos * 1000 / framerate ); //begin = clock() - (double)pos * CLOCKS_PER_SEC / framerate; current_frame = pos + 1; mediaPlayerState->curDecoder()->videoSetFrame( current_frame, stream ); prev_frame = current_frame - 1; currentSample = (int)( current_frame * freq / framerate ); mediaPlayerState->curDecoder()->audioSetSample( currentSample, stream ); audioSampleCounter = currentSample - 1; audioMutex->unlock(); videoMutex->unlock(); } else if ( hasVideoChannel ) { videoMutex->lock(); playtime.restart(); playtime = playtime.addMSecs( -pos * 1000 / framerate ); //begin = clock() - (double)pos * CLOCKS_PER_SEC / framerate; current_frame = pos + 1; mediaPlayerState->curDecoder()->videoSetFrame( current_frame, stream ); videoMutex->unlock(); prev_frame = current_frame - 1; } else if ( hasAudioChannel ) { audioMutex->lock(); playtime.restart(); playtime = playtime.addMSecs( -pos * 1000 / freq ); //begin = clock() - (double)pos * CLOCKS_PER_SEC / freq; currentSample = pos + 1; // (int)( current_frame * freq / framerate ); mediaPlayerState->curDecoder()->audioSetSample( currentSample, stream ); audioSampleCounter = currentSample - 1; audioMutex->unlock(); } } void *startVideoThread( void *ptr ) { LoopControl *mpegView = (LoopControl *)ptr; mpegView->startVideo(); return 0; } void *startAudioThread( void *ptr ) { LoopControl *mpegView = (LoopControl *)ptr; mpegView->startAudio(); return 0; } void LoopControl::startVideo() { moreVideo = TRUE; while ( moreVideo ) { if ( mediaPlayerState->curDecoder() && hasVideoChannel ) { if ( hasAudioChannel && !isMuted ) { bool done = FALSE; do { /* videoMutex->lock(); current_frame = int( (double)playtime.elapsed() * (double)framerate / 1000.0 ); //current_frame = ( clock() - begin ) * (double)framerate / CLOCKS_PER_SEC; // Sync to Audio // current_frame = (long)((double)(audioSampleCounter - 1000) * framerate / (double)freq); long mSecsToNextFrame = 0; if ( current_frame == prev_frame ) { int nf = current_frame + 1; if ( nf > 0 && nf != total_video_frames ) // mSecsToNextFrame = long(double(nf * CLOCKS_PER_SEC) / framerate) - ( clock() - begin ); mSecsToNextFrame = long(double(nf * 1000) / framerate) - ( playtime.elapsed() ); } videoMutex->unlock(); if ( mSecsToNextFrame ) { usleep( mSecsToNextFrame ); // wait a bit videoMutex->lock(); // This should now be the next frame current_frame = int( (double)playtime.elapsed() * (double)framerate / 1000.0 ); //current_frame = ( clock() - begin ) * (double)framerate / CLOCKS_PER_SEC; videoMutex->unlock(); } videoMutex->lock(); done = current_frame >= prev_frame; videoMutex->unlock(); */ videoMutex->lock(); current_frame = int( (double)playtime.elapsed() * (double)framerate / 1000.0 ); done = current_frame >= prev_frame; videoMutex->unlock(); if ( !done ) usleep( 1000 ); // wait a bit } while ( !done ); // qDebug("elapsed: %i %i (%f)", int( playtime.elapsed() ), current_frame, framerate ); } else { videoMutex->lock(); current_frame++; |