/usr/lib/python2.7/dist-packages/framework/subsystems/odeviced/audio.py is in fso-frameworkd 0.10.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 | #!/usr/bin/env python
"""
Open Device Daemon - A plugin for audio device peripherals
(C) 2008-2009 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
(C) 2008 Openmoko, Inc.
GPLv2 or later
Package: odeviced
Module: audio
"""
MODULE_NAME = "odeviced.audio"
__version__ = "0.5.9.11"
from framework.config import config
from framework.patterns import asyncworker, processguard
from helpers import DBUS_INTERFACE_PREFIX, DBUS_PATH_PREFIX, readFromFile, writeToFile, cleanObjectName
APLAY_COMMAND = "/usr/bin/aplay"
import gobject
import dbus.service
import sys, os, time, types, subprocess
import logging
logger = logging.getLogger( "odeviced.audio" )
#----------------------------------------------------------------------------#
class UnknownFormat( dbus.DBusException ):
#----------------------------------------------------------------------------#
_dbus_error_name = "org.freesmartphone.Device.Audio.UnknownFormat"
#----------------------------------------------------------------------------#
class UnsupportedFormat( dbus.DBusException ):
#----------------------------------------------------------------------------#
_dbus_error_name = "org.freesmartphone.Device.Audio.UnsupportedFormat"
#----------------------------------------------------------------------------#
class PlayerError( dbus.DBusException ):
#----------------------------------------------------------------------------#
_dbus_error_name = "org.freesmartphone.Device.Audio.PlayerError"
#----------------------------------------------------------------------------#
class NotPlaying( dbus.DBusException ):
#----------------------------------------------------------------------------#
_dbus_error_name = "org.freesmartphone.Device.Audio.NotPlaying"
#----------------------------------------------------------------------------#
class AlreadyPlaying( dbus.DBusException ):
#----------------------------------------------------------------------------#
_dbus_error_name = "org.freesmartphone.Device.Audio.AlreadyPlaying"
#----------------------------------------------------------------------------#
class ScenarioInvalid( dbus.DBusException ):
#----------------------------------------------------------------------------#
_dbus_error_name = "org.freesmartphone.Device.Audio.ScenarioInvalid"
#----------------------------------------------------------------------------#
class ScenarioStackUnderflow( dbus.DBusException ):
#----------------------------------------------------------------------------#
_dbus_error_name = "org.freesmartphone.Device.Audio.ScenarioStackUnderflow"
#----------------------------------------------------------------------------#
class DeviceFailed( dbus.DBusException ):
#----------------------------------------------------------------------------#
_dbus_error_name = "org.freesmartphone.Device.Audio.DeviceFailed"
#----------------------------------------------------------------------------#
class Player( asyncworker.AsyncWorker ):
#----------------------------------------------------------------------------#
"""
Base class implementing common logic for all Players.
"""
def __init__( self, dbus_object ):
asyncworker.AsyncWorker.__init__( self )
self._object = dbus_object
def enqueueTask( self, ok_cb, error_cb, task, *args ):
self.enqueue( ok_cb, error_cb, task, args )
def onProcessElement( self, element ):
logger.debug( "getting task from queue..." )
ok_cb, error_cb, task, args = element
logger.debug( "got task: %s %s" % ( task, args ) )
try:
method = getattr( self, "task_%s" % task )
except AttributeError:
logger.debug( "unhandled task: %s %s" % ( task, args ) )
else:
method( ok_cb, error_cb, *args )
return True
def task_play( self, ok_cb, error_cb, name, loop, length ):
ok_cb()
def task_stop( self, ok_cb, error_cb, name ):
ok_cb()
def task_panic( self, ok_cb, error_cb ):
ok_cb()
@classmethod
def supportedFormats( cls ):
return []
#----------------------------------------------------------------------------#
class NullPlayer( Player ):
#----------------------------------------------------------------------------#
"""
A dummy player, useful e.g. if no audio subsystem is available.
"""
def task_play( self, ok_cb, error_cb, name, loop, length ):
logger.info( "NullPlayer [not] playing sound %s" % name )
ok_cb()
def task_stop( self, ok_cb, error_cb, name ):
logger.info( "NullPlayer [not] stopping sound %s" % name )
ok_cb()
def task_panic( self, ok_cb, error_cb ):
logger.info( "NullPlayer [not] stopping all sounds" )
ok_cb()
#----------------------------------------------------------------------------#
class GStreamerPlayer( Player ):
#----------------------------------------------------------------------------#
"""
A Gstreamer based Player.
"""
decoderMap = {}
@classmethod
def supportedFormats( cls ):
try:
global gst
import gst as gst
except ImportError:
logger.warning( "Could not setup gstreamer player (python-gst not installed?)" )
return []
# set up decoder map
if cls.decoderMap == {}:
cls._trySetupDecoder( "mod", "modplug" )
cls._trySetupDecoder( "mp3", "mad" )
cls._trySetupDecoder( "sid", "siddec" )
cls._trySetupDecoder( "wav", "wavparse" )
# ogg w/ integer vorbis decoder, found on embedded systems
haveit = cls._trySetupDecoder( "ogg", "oggdemux ! ivorbisdec ! audioconvert" )
if not haveit:
# ogg w/ floating point vorbis decoder, found on desktop systems
cls._trySetupDecoder( "ogg", "oggdemux ! vorbisdec ! audioconvert" )
return cls.decoderMap.keys()
@classmethod
def _trySetupDecoder( cls, ext, dec ):
# FIXME might even save the bin's already, not just the description
try:
gst.parse_bin_from_description( dec, 0 )
except gobject.GError, e:
logger.warning( "GST can't parse %s; Not adding %s to decoderMap" % ( dec, ext ) )
return False
else:
cls.decoderMap[ext] = dec
return True
def __init__( self, *args, **kwargs ):
Player.__init__( self, *args, **kwargs )
self.pipelines = {}
def _onMessage( self, bus, message, name ):
pipeline, status, loop, length, ok_cb, error_cb = self.pipelines[name]
logger.debug( "GST message received while file status = %s" % status )
t = message.type
if t == gst.MESSAGE_EOS:
# shall we restart?
if loop:
logger.debug( "G: EOS -- restarting stream" )
pipeline.seek_simple( gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH, 0 )
else:
logger.debug( "G: EOS" )
self._updateSoundStatus( name, "stopped" )
pipeline.set_state( gst.STATE_NULL )
del self.pipelines[name]
elif t == gst.MESSAGE_ERROR:
pipeline.set_state(gst.STATE_NULL)
del self.pipelines[name]
err, debug = message.parse_error()
logger.debug( "G: ERROR: %s %s" % ( err, debug ) )
error_cb( PlayerError( err.message ) )
elif t == gst.MESSAGE_STATE_CHANGED:
previous, current, pending = message.parse_state_changed()
logger.debug( "G: STATE NOW: (%s) -> %s -> (%s)" % ( previous, current, pending ) )
if ( previous, current, pending ) == ( gst.STATE_READY, gst.STATE_PAUSED, gst.STATE_PLAYING ):
self._updateSoundStatus( name, "playing" )
ok_cb()
if length:
logger.debug( "adding timeout for %s of %d seconds" % ( name, length ) )
gobject.timeout_add_seconds( length, self._playTimeoutReached, name )
elif ( previous, current, pending ) == ( gst.STATE_PLAYING, gst.STATE_PAUSED, gst.STATE_READY ):
self._updateSoundStatus( name, "stopped" )
pipeline.set_state( gst.STATE_NULL )
del self.pipelines[name]
# ok_cb()
else: # uninteresting state change
pass
else:
logger.debug( "G: UNHANDLED: %s" % t )
def _playTimeoutReached( self, name ):
try:
pipeline, status, loop, length, ok_cb, error_cb = self.pipelines[name]
except KeyError: # might have vanished in the meantime?
logger.warning( "audio pipeline for %s has vanished before timer could fire" % name )
return False
previous, current, next = pipeline.get_state()
logger.debug( "custom player timeout for %s reached, state is %s" % ( name, current ) )
if loop:
pipeline.set_state( gst.STATE_NULL )
del self.pipelines[name]
self.task_play( lambda: None, lambda foo: None, name, loop, length )
#pipeline.seek_simple( gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH, 0 )
else:
self.task_stop( lambda: None, lambda foo: None, name )
return False # don't call us again, mainloop
def _updateSoundStatus( self, name, newstatus ):
pipeline, status, loop, length, ok_cb, error_cb = self.pipelines[name]
if newstatus != status:
self.pipelines[name] = pipeline, newstatus, loop, length, ok_cb, error_cb
self._object.SoundStatus( name, newstatus, {} )
def task_play( self, ok_cb, error_cb, name, loop, length ):
if name in self.pipelines:
error_cb( AlreadyPlaying( name ) )
else:
# Split options from filename, these may be useful for advanced
# settings on MOD and SID files.
try:
base, ext = name.rsplit( '.', 1 )
except ValueError: # no extension provided
return error_cb( UnknownFormat( "Can't guess format from extension" ) )
options = ext.split( ';' )
ext = options.pop( 0 )
file = ".".join( [ base, ext ] ).replace( ' ', r'\ ' )
try:
decoder = GStreamerPlayer.decoderMap[ ext ]
except KeyError:
return error_cb( UnknownFormat( "Known formats are %s" % self.decoderMap.keys() ) )
else:
if len(options) > 0:
decoder = decoder + " " + " ".join( options )
# parse_launch may burn a few cycles compared to element_factory_make,
# however it should still be faster than creating the pipeline from
# individual elements in python, since it's all happening in compiled code
try:
pipeline = gst.parse_launch( 'filesrc location="%s" ! %s ! alsasink' % ( file, decoder ) )
except gobject.GError, e:
logger.exception( "could not instanciate pipeline: %s" % e )
return error_cb( PlayerError( "Could not instanciate pipeline due to an internal error." ) )
else:
# everything ok, go play
bus = pipeline.get_bus()
bus.add_signal_watch()
bus.connect( "message", self._onMessage, name )
self.pipelines[name] = ( pipeline, "unknown", loop, length, ok_cb, error_cb )
pipeline.set_state( gst.STATE_PLAYING )
def task_stop( self, ok_cb, error_cb, name ):
try:
pipeline = self.pipelines[name][0]
except KeyError:
error_cb( NotPlaying( name ) )
else:
pipeline.set_state( gst.STATE_READY )
ok_cb()
def task_panic( self, ok_cb, error_cb ):
for name in self.pipelines:
self.pipelines[name][0].set_state( gst.STATE_READY )
ok_cb()
#----------------------------------------------------------------------------#
class AlsaPlayer( Player ):
#----------------------------------------------------------------------------#
"""
An alsa player, useful for wav format, when the latency of the GStreamerPlayer
is too heavy.
"""
@classmethod
def supportedFormats( cls ):
if os.path.exists( APLAY_COMMAND ):
return [ "wav" ]
else:
return []
sounds = {}
def task_play( self, ok_cb, error_cb, name, loop, length ):
if name in self.sounds:
error_cb( AlreadyPlaying() )
else:
p = processguard.ProcessGuard( [ "/usr/bin/aplay", str(name) ] )
p.execute( onExit = self._onPlayingFinished )
self.sounds[name] = p, loop, length
ok_cb()
logger.info( "AlsaPlayer playing sound %s" % name )
self._object.SoundStatus( name, "playing", {} )
def task_stop( self, ok_cb, error_cb, name ):
if name not in self.sounds:
error_cb( NotPlaying() )
else:
p, loop, length = self.sounds[name]
p.shutdown()
del self.sounds[name]
ok_cb()
logger.info( "AlsaPlayer stopped sound %s" % name )
self._object.SoundStatus( name, "stopped", {} )
def task_panic( self, ok_cb, error_cb ):
logger.info( "AlsaPlayer stopping all sounds" )
for key, value in self.sounds.items():
p, loop, length = value
p.shutdown()
del self.sounds[key]
self._object.SoundStatus( key, "stopped", {} )
ok_cb()
def _onPlayingFinished( self, pid, exitcode, exitsignal ):
logger.info( "AlsaPlayer %d exited with exitcode %d (signal %d)" % ( pid, exitcode, exitsignal ) )
normalShutdown = ( exitcode == 0 )
for key, value in self.sounds.items():
p, loop, length = value
if p.hadpid == pid or p.pid == pid:
if normalShutdown and loop:
logger.debug( "AlsaPlayer restarting sound %s due to loop value" % key )
p.execute( onExit = self._onPlayingFinished )
else:
del self.sounds[key]
self._object.SoundStatus( key, "stopped", {} )
#----------------------------------------------------------------------------#
class AlsaScenarios( object ):
#----------------------------------------------------------------------------#
"""
Controls alsa audio scenarios.
"""
def __init__( self, dbus_object, statedir, defaultscene ):
self._object = dbus_object
self._statedir = statedir
self._default = defaultscene
self._statenames = None
# FIXME set default profile (from configuration)
# FIXME should be set when this audio object initializes
self._current = "unknown"
self._stack = []
gobject.idle_add( self._initScenario )
logger.info( " ::: using alsa scenarios in %s, default = %s" % ( statedir, defaultscene ) )
def _initScenario( self ):
# gather default profile from preferences
if os.path.exists( "%s/%s.state" % ( self._statedir, self._default ) ):
self.setScenario( self._default )
logger.info( "default alsa scenario restored" )
else:
logger.warning( "default alsa scenario '%s' not found in '%s'. device may start uninitialized" % ( self._default, self._statedir ) )
return False
def pushScenario( self, scenario ):
current = self._current
if self.setScenario( scenario ):
self._stack.append( current )
return True
else:
return False
def pullScenario( self ):
previous = self._stack.pop()
result = self.setScenario( previous )
if result is False:
return result
else:
return previous
def getScenario( self ):
return self._current
def storeScenario( self, scenario ):
statename = "%s/%s.state" % ( self._statedir, scenario )
result = subprocess.call( [ "alsactl", "-f", statename, "store" ] )
if result != 0:
logger.error( "can't store alsa scenario to %s" % statename )
return False
else:
# reload scenarios next time
self._statenames = None
return True
def getAvailableScenarios( self ):
# FIXME might check timestamp or use inotify
if self._statenames is None:
try:
files = os.listdir( self._statedir )
except OSError:
logger.warning( "no state files in %s found" % self._statedir )
self._statenames = []
else:
self._statenames = [ state[:-6] for state in files if state.endswith( ".state" ) ]
return self._statenames
def setScenario( self, scenario ):
if not scenario in self.getAvailableScenarios():
return False
statename = "%s/%s.state" % ( self._statedir, scenario )
result = subprocess.call( [ "alsactl", "-f", statename, "restore" ] )
if result == 0:
# work around ASoC DAPM problem
if scenario == "gsmbluetooth":
result += subprocess.call( [ "amixer", "sset", "Capture Left Mixer", "Analogue Mix Right" ] )
result += subprocess.call( [ "amixer", "sset", "Capture Left Mixer", "Analogue Mix Left" ] )
if result == 0:
self._current = scenario
self._object.Scenario( scenario, "user" )
return True
else:
logger.error( "can't set alsa scenario from %s" % statename )
return False
def hasScenario( self, scenario ):
return scenario in self.getAvailableScenarios()
#----------------------------------------------------------------------------#
class Audio( dbus.service.Object ):
#----------------------------------------------------------------------------#
"""
A Dbus Object implementing org.freesmartphone.Device.Audio
"""
DBUS_INTERFACE = DBUS_INTERFACE_PREFIX + ".Audio"
players = {}
def __init__( self, bus, index, node ):
self.interface = self.DBUS_INTERFACE
self.path = DBUS_PATH_PREFIX + "/Audio"
dbus.service.Object.__init__( self, bus, self.path )
for player in ( AlsaPlayer, GStreamerPlayer, ):
supportedFormats = player.supportedFormats()
instance = player( self )
for format in supportedFormats:
if format not in self.players:
self.players[format] = instance
scenario_dir = config.getValue( MODULE_NAME, "scenario_dir", "/etc/alsa/scenario" )
default_scenario = config.getValue( MODULE_NAME, "default_scenario", "default" )
self.scenario = AlsaScenarios( self, scenario_dir, default_scenario )
logger.info( "%s %s initialized. Serving %s at %s" % ( self.__class__.__name__, __version__, self.interface, self.path ) )
logger.debug( "^^^ found players for following formats: '%s'" % self.players.keys() )
def playerForFile( self, name ):
try:
base, ext = name.rsplit( '.', 1 )
except ValueError: # no extension provided
raise UnknownFormat( "Can't guess format from extension" )
options = ext.split( ';' )
ext = options.pop( 0 )
try:
player = self.players[ext]
except KeyError:
raise UnsupportedFormat( "No player registered for format '%s'" % ext )
return player
#
# dbus info methods
#
@dbus.service.method( DBUS_INTERFACE, "", "a{sv}",
async_callbacks=( "dbus_ok", "dbus_error" ) )
def GetInfo( self, dbus_ok, dbus_error ):
info = {}
formats = []
for player in self.players.values():
formats += player.supportedFormats()
info["name"] = "Default Audio Device"
info["formats"] = list( set( formats ) )
info["scenario"] = self.scenario.getScenario()
info["scenarios"] = dbus.Array( self.scenario.getAvailableScenarios(), "as" )
dbus_ok( info )
#
# dbus sound methods
#
@dbus.service.method( DBUS_INTERFACE, "sii", "",
async_callbacks=( "dbus_ok", "dbus_error" ) )
def PlaySound( self, name, loop, length, dbus_ok, dbus_error ):
self.playerForFile( name ).enqueueTask( dbus_ok, dbus_error, "play", name, loop, length )
@dbus.service.method( DBUS_INTERFACE, "s", "",
async_callbacks=( "dbus_ok", "dbus_error" ) )
def StopSound( self, name, dbus_ok, dbus_error ):
self.playerForFile( name ).enqueueTask( dbus_ok, dbus_error, "stop", name )
@dbus.service.method( DBUS_INTERFACE, "", "",
async_callbacks=( "dbus_ok", "dbus_error" ) )
def StopAllSounds( self, dbus_ok, dbus_error ):
for player in self.players.values():
player.enqueueTask( dbus_ok, dbus_error, "panic" )
#
# dbus scenario methods
#
# FIXME ugly. error handling should be done by the scenario itself
@dbus.service.method( DBUS_INTERFACE, "", "as",
async_callbacks=( "dbus_ok", "dbus_error" ) )
def GetAvailableScenarios( self, dbus_ok, dbus_error ):
dbus_ok( self.scenario.getAvailableScenarios() )
@dbus.service.method( DBUS_INTERFACE, "", "s",
async_callbacks=( "dbus_ok", "dbus_error" ) )
def GetScenario( self, dbus_ok, dbus_error ):
dbus_ok( self.scenario.getScenario() )
@dbus.service.method( DBUS_INTERFACE, "s", "",
async_callbacks=( "dbus_ok", "dbus_error" ) )
def SetScenario( self, name, dbus_ok, dbus_error ):
if not self.scenario.hasScenario( name ):
dbus_error( ScenarioInvalid( "available scenarios are: %s" % self.scenario.getAvailableScenarios() ) )
else:
if self.scenario.setScenario( name ):
dbus_ok()
else:
dbus_error( DeviceFailed( "unknown error while setting scenario" ) )
@dbus.service.method( DBUS_INTERFACE, "s", "",
async_callbacks=( "dbus_ok", "dbus_error" ) )
def PushScenario( self, name, dbus_ok, dbus_error ):
if not self.scenario.hasScenario( name ):
dbus_error( ScenarioInvalid( "available scenarios are: %s" % self.scenario.getAvailableScenarios() ) )
else:
if self.scenario.pushScenario( name ):
dbus_ok()
else:
dbus_error( DeviceFailed( "unknown error while pushing scenario" ) )
@dbus.service.method( DBUS_INTERFACE, "", "s",
async_callbacks=( "dbus_ok", "dbus_error" ) )
def PullScenario( self, dbus_ok, dbus_error ):
try:
previousScenario = self.scenario.pullScenario()
except IndexError:
dbus_error( ScenarioStackUnderflow( "forgot to push a scenario?" ) )
else:
if previousScenario is False:
dbus_error( DeviceFailed( "unknown error while pulling scenario" ) )
else:
dbus_ok( previousScenario )
@dbus.service.method( DBUS_INTERFACE, "s", "",
async_callbacks=( "dbus_ok", "dbus_error" ) )
def StoreScenario( self, name, dbus_ok, dbus_error ):
if self.scenario.storeScenario( name ):
dbus_ok()
else:
dbus_error( DeviceFailed( "unknown error while storing scenario" ) )
#
# dbus signals
#
@dbus.service.signal( DBUS_INTERFACE, "ssa{sv}" )
def SoundStatus( self, name, status, properties ):
logger.info( "sound status %s %s %s" % ( name, status, properties ) )
@dbus.service.signal( DBUS_INTERFACE, "ss" )
def Scenario( self, scenario, reason ):
logger.info( "sound scenario %s %s" % ( scenario, reason ) )
#----------------------------------------------------------------------------#
def factory( prefix, controller ):
#----------------------------------------------------------------------------#
"""Instanciate plugins"""
return [ Audio( controller.bus, 0, "" ) ]
if __name__ == "__main__":
import dbus
bus = dbus.SystemBus()
|