ocarina/src/core/gstreamer.py

268 lines
5.4 KiB
Python

#! /usr/bin/python
__author__ = "bjschuma"
__date__ = "$Feb 5, 2010 7:53:19 PM$"
from ct import path
from ct.call import write
from ct.opts import args
import gst
import ocarina
global player
global time
player = gst.element_factory_make("playbin2", "player")
time = gst.Format(gst.FORMAT_TIME)
bus = player.get_bus()
def getstate():
global player
state = player.get_state()[1]
write("Gstreamer state: "+str(state), 3)
return player.get_state()[1]
def play():
global player
player.set_state(gst.STATE_PLAYING)
if getstate() != gst.STATE_PLAYING:
ocarina.events.stop("ocarina-play")
else:
ocarina.vars["$playing"] = True
def pause():
global player
player.set_state(gst.STATE_PAUSED)
if getstate() != gst.STATE_PAUSED:
ocarina.events.stop("ocarina-pause")
else:
ocarina.vars["$playing"] = False
def load(song):
song = path.expand(song)
if path.exists(song) == False:
write("Path does not exist: " + song)
return
curstate = getstate()
pause()
write("Loading file: " + song, 1)
global player
player.set_state(gst.STATE_NULL)
player.set_property("uri", "file://" + song)
if ocarina.vars["$playonload"]==True or curstate==gst.STATE_PLAYING:
play()
else:
pause()
#player.set_state(gst.STATE_PAUSED)
def uninit():
global player
player.set_state(gst.STATE_NULL)
def onMessage(bus, message):
#print message.type
if message.type == gst.MESSAGE_TAG:
taglist = message.parse_tag()
for tag in taglist.keys():
write("Found tag: ("+tag+", "+str(taglist[tag])+")",1)
if tag == "title":
ocarina.vars["$title"] = taglist[tag]
elif tag == "artist":
ocarina.vars["$artist"]= taglist[tag]
elif tag == "album":
ocarina.vars["$album"] = taglist[tag]
ocarina.events.start("tags-changed")
def duration():
global player
global time
state = getstate()
if (state!=gst.STATE_PLAYING) and (state!=gst.STATE_PAUSED):
return 0
total = player.query_duration(time)[0]
return total
def currentpos():
global player
global time
state = getstate()
if (state!=gst.STATE_PLAYING) and (state!=gst.STATE_PAUSED):
return 0
position = player.query_position(time)[0]
return position
def getProgress():
global player
global time
state = getstate()
if (state!=gst.STATE_PLAYING) and (state!=gst.STATE_PAUSED):
return 0
total = duration()
current = currentpos()
fraction = float(current) / float(total)
if fraction < 0.0:
return 0.0
elif fraction > 1.0:
return 1.0
return fraction
# Seek to the desired percent of the song.
# Fraction is True if prcnt is already a fraction
def seek(prcnt):
global player
global time
if prcnt < 0:
prcnt = 0
elif prcnt > 100:
prcnt = 1
elif prcnt > 1:
prcnt = float(prcnt) / 100.0
newTime = duration() * prcnt
player.seek_simple(time,gst.SEEK_FLAG_FLUSH,newTime)
return 0
def setvol(volume):
global player
if volume > 1.0:
volume = 1.0
elif volume < 0.0:
volume = 0.0
player.set_property("volume",volume)
ocarina.vars["$volume"] = volume
def volup():
volume = ocarina.vars["$volume"]
volume += ocarina.vars["$volumeincr"]
setvol(volume)
def voldown():
volume = ocarina.vars["$volume"]
volume -= ocarina.vars["$volumeincr"]
setvol(volume)
bus.add_signal_watch()
bus.connect("message", onMessage)
ocarina.events.invite("ocarina-close", uninit)
ocarina.events.invite("ocarina-play", play,50)
ocarina.events.invite("ocarina-pause", pause,50)
setvol(ocarina.vars["$volume"])
#
#def setvol(value):
# global pipeline
# curvol = float( settings.get("volume") )
#
# vol = pipeline.get_by_name("vol")
# if value == "up":
# value = curvol + 0.05
# elif value == "down":
# value = curvol - 0.05
# else:
# # Prevent converting strings
# try:
# value = float(value)
# except:
# return
#
# if value > 1.0:
# value = 1.0
# if value < 0.0:
# value = 0.0
# settings.set("volume",value)
# vol.set_property("volume",value )
#
#
#def getProgress(tuple=False):
# global pipeline
# global time
#
# # Don't bother to go on if the pipeline isn't playing
# if not pipeline.get_state()[1] == gst.STATE_PLAYING:
# return -1
#
# position = pipeline.query_position(time)[0]
# total = pipeline.query_duration(time)[0]
# if tuple==False:
# return float(position) / float(total)
# else:
# return (position,total)
#
#
## Draw the progress bar on the command line
#def drawProgress():
# p = getProgress()
# if p == -1:
# return
# win = settings.get("maxyx")
# max = int(win[1] * p)
# if max > win[1]:
# max = win[1]
# if max == 0:
# cline.message.disp(" "*(win[1]-1) ,win[0]-1)
# else:
# cline.message.disp("="*max, win[0]-1)
#
#
## A callback when there are messages on the bus
#def onMessage(bus,message):
# if message.type == gst.MESSAGE_EOS:
# manager.run("next")
#
#
## Manually check the bus for messages
#def checkBus():
# global bus
# if bus.peek() == None:
# return
# onMessage(bus,bus.pop())
#
#
#
#def init():
# # Register signals
# signal.register("play",play)
# signal.register("pause",pause)
# signal.register("cliloop",drawProgress)
# signal.register("cliloop",checkBus,90)
#
# # Check for settings values
# if settings.has("args") == True:
# input = settings.get("args")
# if not input == []:
# join = ' '
# path = join.join(input)
# load(path)
# else:
# if settings.has("curtrk")==True:
# track = settings.get("curtrk")
# if track > 0:
# manager.run("next",[track,False])
# if settings.has("volume") == False:
# settings.set("volume",1.0)
# setvol(settings.get("volume"))
#
#
#def close():
# global pipeline
# global bin
# pause()
# pipeline.set_state(gst.STATE_NULL)
# bin.set_state(gst.STATE_NULL)