2021-09-03 11:34:01 -04:00
|
|
|
# Copyright 2021 (c) Anna Schumaker.
|
|
|
|
from gi.repository import GObject
|
2021-09-03 18:34:56 -04:00
|
|
|
from gi.repository import GLib
|
2021-09-03 11:34:01 -04:00
|
|
|
from gi.repository import Gst
|
|
|
|
|
|
|
|
class BassPlayer(GObject.GObject):
|
|
|
|
def __init__(self):
|
|
|
|
GObject.GObject.__init__(self)
|
|
|
|
self.video = Gst.ElementFactory.make("fakesink")
|
|
|
|
self.playbin = Gst.ElementFactory.make("playbin")
|
|
|
|
self.playbin.set_property("video-sink", self.video)
|
|
|
|
self.playbin.set_state(Gst.State.READY)
|
2021-09-04 07:44:51 -04:00
|
|
|
|
|
|
|
self.bus.add_signal_watch()
|
2021-09-03 12:10:40 -04:00
|
|
|
self.bus.connect("message::state-changed", self.state_changed)
|
2021-09-03 17:50:24 -04:00
|
|
|
self.bus.connect("message::stream-start", self.stream_start)
|
2021-09-03 18:34:56 -04:00
|
|
|
self.bus.connect("message::state-changed", self.state_changed)
|
|
|
|
|
|
|
|
self.timeout = None
|
2021-09-04 07:44:51 -04:00
|
|
|
|
|
|
|
@GObject.Property
|
|
|
|
def bus(self):
|
|
|
|
return self.playbin.get_bus()
|
2021-09-03 11:58:48 -04:00
|
|
|
|
2021-09-03 17:50:24 -04:00
|
|
|
@GObject.Property
|
|
|
|
def duration(self):
|
|
|
|
(res, dur) = self.playbin.query_duration(Gst.Format.TIME)
|
|
|
|
return dur if res == True else 0
|
|
|
|
|
2021-09-03 12:10:40 -04:00
|
|
|
@GObject.Property
|
|
|
|
def playing(self):
|
|
|
|
(ret, state, pending) = self.playbin.get_state(Gst.CLOCK_TIME_NONE)
|
|
|
|
return state == Gst.State.PLAYING
|
|
|
|
|
|
|
|
@playing.setter
|
|
|
|
def playing(self, playing):
|
|
|
|
state = Gst.State.PLAYING if playing else Gst.State.PAUSED
|
|
|
|
self.playbin.set_state(state)
|
|
|
|
|
2021-09-03 18:34:56 -04:00
|
|
|
@GObject.Property
|
|
|
|
def position(self):
|
|
|
|
(res, pos) = self.playbin.query_position(Gst.Format.TIME)
|
|
|
|
return pos if res == True else 0
|
|
|
|
|
|
|
|
@position.setter
|
|
|
|
def position(self, pos):
|
|
|
|
self.playbin.seek_simple(Gst.Format.TIME,
|
|
|
|
Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT,
|
|
|
|
pos)
|
|
|
|
|
2021-09-03 11:58:48 -04:00
|
|
|
@GObject.Property
|
|
|
|
def uri(self):
|
|
|
|
return self.playbin.get_property("uri")
|
|
|
|
|
|
|
|
@uri.setter
|
|
|
|
def uri(self, uri):
|
|
|
|
if uri:
|
|
|
|
self.playbin.set_property("uri", uri)
|
|
|
|
else:
|
|
|
|
self.playbin.set_state(Gst.State.READY)
|
2021-09-03 12:10:40 -04:00
|
|
|
|
|
|
|
def state_changed(self, bus, message):
|
|
|
|
if message.src == self.playbin:
|
|
|
|
(old, new, pending) = message.parse_state_changed()
|
|
|
|
if new == Gst.State.PLAYING:
|
|
|
|
self.emit("playback-start")
|
|
|
|
else:
|
|
|
|
self.emit("playback-paused")
|
|
|
|
|
2021-09-03 17:50:24 -04:00
|
|
|
def stream_start(self, bus, message):
|
|
|
|
self.emit("duration-changed")
|
|
|
|
|
2021-09-03 18:34:56 -04:00
|
|
|
def timeout_function(self):
|
|
|
|
self.emit("position-changed")
|
|
|
|
return GLib.SOURCE_CONTINUE
|
|
|
|
|
2021-09-03 17:50:24 -04:00
|
|
|
@GObject.Signal
|
|
|
|
def duration_changed(self):
|
|
|
|
pass
|
|
|
|
|
2021-09-03 12:10:40 -04:00
|
|
|
@GObject.Signal
|
|
|
|
def playback_start(self):
|
2021-09-03 18:34:56 -04:00
|
|
|
if not self.timeout:
|
|
|
|
self.timeout = GLib.timeout_add(200, self.timeout_function)
|
2021-09-03 12:10:40 -04:00
|
|
|
|
|
|
|
@GObject.Signal
|
|
|
|
def playback_paused(self):
|
2021-09-03 18:34:56 -04:00
|
|
|
if self.timeout:
|
|
|
|
GLib.source_remove(self.timeout)
|
|
|
|
self.timeout = None
|
|
|
|
|
|
|
|
@GObject.Signal
|
|
|
|
def position_changed(self):
|
2021-09-03 12:10:40 -04:00
|
|
|
pass
|