📄 yauap-engine.cpp
字号:
const Engine::Scope &yauapEngine::scope(){ int len = 0; dbus_int16_t* data = 0; DBusMessage* msg = con->send_with_reply("get_scopedata", DBUS_TYPE_INVALID); if (msg) { DBusMessageIter args; if (dbus_message_iter_init(msg, &args) && DBUS_TYPE_ARRAY == dbus_message_iter_get_arg_type(&args)) { DBusMessageIter sub; dbus_message_iter_recurse(&args, &sub); dbus_message_iter_next(&args); dbus_message_iter_get_fixed_array(&sub,&data,&len); } dbus_message_unref(msg); } /* 2 channel 16 bit samples */ if(len == SCOPESIZE * 2) { for( int i=0; i < SCOPESIZE ; i++) m_scope[i] = data[i]; }else debug() << "get_scopedata returned the wrong amount of data " << len << endl; return m_scope;}voidyauapEngine::yauapProcessExited(){ debug() << "yauapProcessExited!!!!!" << endl; closeDbusConnection(); initDbusConnection();}voidyauapEngine::closeDbusConnection(){ /* destroy Qt DBus connection */ delete con; con = 0; /* kill yauap */#ifndef MANUAL_YAUAP_START helper.kill();#endif}boolyauapEngine::initDbusConnection(){#ifndef MANUAL_YAUAP_START /* start yauap in slave mode */ helper.clearArguments(); helper << "yauap" << "-noexit"; if( !helper.start(KProcess::NotifyOnExit, KProcess::All)) { debug() << "could not start yauap " << endl; emit statusText( i18n( "could not start yauap" ) ); return false; }#endif /* create and open qt DBus connection so that we are able to receive signals */ con = new DBusConnection( this ); if (!con->open()) { debug() << "could not connect to dbus" << endl; emit statusText( i18n( "Error: could not connect to dbus" ) ); return false; } /* makes sure the player is stopped: retry the call a few times because it takes some time until yauap registered its dbus service */ for( int i=0; i < 2; ++i ) { if( con->send("stop", DBUS_TYPE_INVALID) >= 0 ) return true; usleep(20 * 1000); } return false;}#if 0void yauapEngine::handleDbusError(const char* method){ debug() << method << " failed " << endl; closeDbusConnection(); initDbusConnection();}#endifvoidyauapEngine::customEvent(QCustomEvent *e){ QString* message = static_cast<QString*>(e->data()); switch (e->type()) { case 3000: m_state = Engine::Idle; emit trackEnded(); break; case 3002: emit statusText(*message); delete message; break; case 3003: { Engine::SimpleMetaBundle* bundle = static_cast<Engine::SimpleMetaBundle*>(e->data()); emit metaData(*bundle); delete bundle; break; } case 3004: update_metadata(); break; default: ; }}/* init engine */bool yauapEngine::init(){ debug() << "In init" << endl; m_state = Engine::Idle; connect(&helper, SIGNAL(processExited(KProcess*)), SLOT(yauapProcessExited())); if (initDbusConnection()) return true; emit statusText( i18n( "Error: timed out waiting for yauap" ) ); return false;}/* check if the given url can be decoded */boolyauapEngine::canDecode( const KURL &kurl ) const{ QCString qurl = kurl.url().utf8(); const char* url = qurl.data(); return con->call("can_decode", DBUS_TYPE_STRING, &url, DBUS_TYPE_INVALID) > 0;}/* load a new track FIXME: implement cross fading */bool yauapEngine::load( const KURL &url, bool isStream ){ QString qurl = url.url(); const char* curl = qurl.ascii(); m_isStream = isStream; Engine::Base::load( url, isStream || url.protocol() == "http" ); change_state(Engine::Idle); if (!curl || !con->call("load", DBUS_TYPE_STRING, &curl, DBUS_TYPE_INVALID)) return false; loaded_url = url; return true;}/* set volume */void yauapEngine::setVolumeSW( uint volume ){ dbus_uint32_t dbus_volume = volume; int ret; debug() << "In setVolumeSW " << volume << endl; ret = con->send("set_volume", DBUS_TYPE_UINT32, &dbus_volume, DBUS_TYPE_INVALID); debug() << "=> " << ret << endl;}/* start playback */bool yauapEngine::play( uint offset ){ dbus_uint32_t dbus_offset = offset; debug() << "In play" << endl; if (con->send("start", DBUS_TYPE_UINT32, &dbus_offset, DBUS_TYPE_INVALID) > 0) { change_state( Engine::Playing ); return true; } change_state( Engine::Empty ); return false;}/* stop playback */void yauapEngine::stop(){ change_state( Engine::Empty ); if (con->send("stop", DBUS_TYPE_INVALID) <= 0) { debug() << "stop failed " << endl; return; } change_state(Engine::Empty);}/* pause playback */void yauapEngine::pause(){ debug() << "In pause " << endl; if (!con->call("pause", DBUS_TYPE_INVALID)) return; if( state() == Engine::Playing ) change_state( Engine::Paused ); else change_state( Engine::Playing );}/* unpause playback */voidyauapEngine::unpause(){ pause();}/* get track length in ms */uintyauapEngine::length() const{ debug() << "In length " << endl; int length = con->call("get_length", DBUS_TYPE_INVALID); if (length < 0) return 0; debug() << "length is => " << length << endl; return (uint) length;}/* get current position */uintyauapEngine::position() const{ int position = 0; position = con->call("get_position", DBUS_TYPE_INVALID); if (position < 0) position = 0; return (uint) position;}/* seek to offset in ms */voidyauapEngine::seek( uint offset ){ dbus_uint32_t dbus_offset = offset; if (!con->send("seek", DBUS_TYPE_UINT32, &dbus_offset, DBUS_TYPE_INVALID)) debug() << "seek failed " << endl;}boolyauapEngine::getAudioCDContents(const QString &device, KURL::List &urls){ debug() << "Getting AudioCD contents..." << endl; QCString cdevice = device.latin1(); const char* cdevice_ptr = cdevice.data(); DBusMessage* msg = con->send_with_reply("get_audio_cd_contents", DBUS_TYPE_STRING, &cdevice_ptr, DBUS_TYPE_INVALID); if (msg) { DBusMessageIter args; int i = 0; if (dbus_message_iter_init(msg, &args) && DBUS_TYPE_ARRAY == dbus_message_iter_get_arg_type(&args)) { DBusMessageIter sub; dbus_message_iter_recurse(&args, &sub); dbus_message_iter_next(&args); while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING) { char* reply_ptr = 0; dbus_message_iter_get_basic(&sub, &reply_ptr); dbus_message_iter_next(&sub); debug() << "reply_ptr: " << reply_ptr << endl; Engine::SimpleMetaBundle b; char* saveptr; KURL url = QString("cdda://").append( strtok_r(reply_ptr,"=",&saveptr)); urls << url; debug() << url << endl; b.title = QString( i18n( "Track %1" ) ).arg( i+1 ); b.length = strtok_r(NULL,"=",&saveptr); b.album = "AudioCD"; b.tracknr = i+1; b.samplerate = "44100"; b.bitrate = "1411"; cd_tracks.push_back(b); ++i; } } dbus_message_unref(msg); } return true;}bool yauapEngine::metaDataForUrl(const KURL &url, Engine::SimpleMetaBundle &b){ if ( url.protocol() == "cdda" ) { b = cd_tracks[url.host().toUInt()-1]; return true; } return false;}#include "yauap-engine.moc"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -