ocarina/src/core/ct/gstreamer.py

161 lines
3.1 KiB
Python

#! /usr/bin/python
# To change this template, choose Tools | Templates
# and open the template in the editor.
__author__="bjschuma"
__date__ ="$Feb 5, 2010 7:53:19 PM$"
import gst
import settings
from bt.file import *
from bt import signal
from bt.message import write
import manager
import cline
global pipeline
global time
global bin
global bus
pipeline = gst.Pipeline("player")
bin = gst.element_factory_make("playbin",None)
bin.set_state(gst.STATE_NULL)
time = gst.Format(gst.FORMAT_TIME)
# Volume range goes from 0 to 1.0 (before sounding bad)
volume = gst.element_factory_make("volume","vol")
pipeline.add(bin,volume)
bus = pipeline.get_bus()
def load(path):
path = expandPath(path)
if checkPath(path) == False:
return
write("loading file: "+path, True)
global bin
bin.set_property("uri", "file://"+path)
bin.set_state(gst.STATE_PAUSED)
def play():
global pipeline
#if not pipeline == None:
pipeline.set_state(gst.STATE_PLAYING)
#else:
# manager.run("next",settings.get("cursong"))
def pause():
global pipeline
if not pipeline == None:
pipeline.set_state(gst.STATE_PAUSED)
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)