2005-11-19 10:37:58 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- Mode: Python -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
|
2004-10-11 09:55:44 +00:00
|
|
|
import pygtk
|
|
|
|
pygtk.require('2.0')
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import gobject
|
2005-11-04 10:14:22 +00:00
|
|
|
|
|
|
|
import pygst
|
|
|
|
pygst.require('0.9')
|
2004-10-11 09:55:44 +00:00
|
|
|
import gst
|
|
|
|
import gst.interfaces
|
|
|
|
import gtk
|
|
|
|
|
|
|
|
class GstPlayer:
|
|
|
|
def __init__(self):
|
|
|
|
self.player = gst.element_factory_make("playbin", "player")
|
|
|
|
|
|
|
|
def set_video_sink(self, sink):
|
|
|
|
self.player.set_property('video-sink', sink)
|
2005-10-09 17:53:58 +00:00
|
|
|
gst.debug('using videosink %r' % self.player.get_property('video-sink'))
|
2004-10-11 09:55:44 +00:00
|
|
|
|
|
|
|
def set_location(self, location):
|
|
|
|
self.player.set_property('uri', location)
|
|
|
|
|
2005-10-09 17:53:58 +00:00
|
|
|
def query_position(self):
|
|
|
|
"Returns a (position, duration) tuple"
|
2005-11-04 10:14:22 +00:00
|
|
|
try:
|
|
|
|
ret = self.player.query_position(gst.FORMAT_TIME)
|
|
|
|
except:
|
|
|
|
position = gst.CLOCK_TIME_NONE
|
|
|
|
else:
|
|
|
|
position = ret[0]
|
|
|
|
|
|
|
|
try:
|
|
|
|
ret = self.player.query_duration(gst.FORMAT_TIME)
|
|
|
|
except:
|
|
|
|
duration = gst.CLOCK_TIME_NONE
|
|
|
|
else:
|
|
|
|
duration = ret[0]
|
2004-10-11 09:55:44 +00:00
|
|
|
|
2005-11-04 10:14:22 +00:00
|
|
|
return (position, duration, ret[1])
|
2004-10-11 09:55:44 +00:00
|
|
|
|
|
|
|
def seek(self, location):
|
2005-10-09 17:53:58 +00:00
|
|
|
"""
|
|
|
|
@param location: time to seek to, in nanoseconds
|
|
|
|
"""
|
|
|
|
gst.debug("seeking to %r" % location)
|
|
|
|
event = gst.event_new_seek(1.0, gst.FORMAT_TIME,
|
|
|
|
gst.SEEK_FLAG_FLUSH,
|
|
|
|
gst.SEEK_TYPE_SET, location,
|
|
|
|
gst.SEEK_TYPE_NONE, 0)
|
|
|
|
|
|
|
|
res = self.player.send_event(event)
|
|
|
|
if res:
|
|
|
|
gst.info("setting new stream time to 0")
|
|
|
|
self.player.set_new_stream_time(0L)
|
|
|
|
else:
|
|
|
|
gst.error("seek to %r failed" % location)
|
2004-10-11 09:55:44 +00:00
|
|
|
|
|
|
|
def pause(self):
|
2005-10-09 17:53:58 +00:00
|
|
|
gst.info("pausing player")
|
2004-10-11 09:55:44 +00:00
|
|
|
self.player.set_state(gst.STATE_PAUSED)
|
|
|
|
|
|
|
|
def play(self):
|
2005-10-09 17:53:58 +00:00
|
|
|
gst.info("playing player")
|
2004-10-11 09:55:44 +00:00
|
|
|
self.player.set_state(gst.STATE_PLAYING)
|
|
|
|
|
|
|
|
def stop(self):
|
2005-10-09 17:53:58 +00:00
|
|
|
gst.info("stopping player")
|
2004-10-11 09:55:44 +00:00
|
|
|
self.player.set_state(gst.STATE_READY)
|
2005-10-09 17:53:58 +00:00
|
|
|
gst.info("stopped player")
|
|
|
|
|
2005-11-04 10:14:22 +00:00
|
|
|
def get_state(self, timeout=1):
|
2005-10-09 17:53:58 +00:00
|
|
|
return self.player.get_state(timeout=timeout)
|
|
|
|
|
|
|
|
def is_in_state(self, state):
|
|
|
|
gst.debug("checking if player is in state %r" % state)
|
2005-11-04 10:14:22 +00:00
|
|
|
cur, pen, final = self.get_state(timeout=0)
|
2005-10-09 17:53:58 +00:00
|
|
|
gst.debug("checked if player is in state %r" % state)
|
|
|
|
if pen == gst.STATE_VOID_PENDING and cure == state:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
is_playing = lambda self: self.is_in_state(gst.STATE_PLAYING)
|
|
|
|
is_paused = lambda self: self.is_in_state(gst.STATE_PAUSED)
|
|
|
|
is_stopped = lambda self: self.is_in_state(gst.STATE_READY)
|
2004-10-11 09:55:44 +00:00
|
|
|
|
|
|
|
class VideoWidget(gtk.DrawingArea):
|
|
|
|
def __init__(self, player):
|
|
|
|
gtk.DrawingArea.__init__(self)
|
|
|
|
self.connect('destroy', self.destroy_cb)
|
|
|
|
self.connect_after('realize', self.after_realize_cb)
|
|
|
|
self.set_size_request(400, 400)
|
|
|
|
|
|
|
|
self.player = player
|
2004-11-23 10:16:58 +00:00
|
|
|
self.imagesink = gst.element_factory_make('xvimagesink')
|
2004-10-11 09:55:44 +00:00
|
|
|
self.player.set_video_sink(self.imagesink)
|
|
|
|
|
|
|
|
def destroy_cb(self, da):
|
|
|
|
self.set_window_id(0L)
|
|
|
|
|
|
|
|
# Sort of a hack, but it works for now.
|
|
|
|
def after_realize_cb(self, window):
|
2005-10-09 17:53:58 +00:00
|
|
|
gobject.idle_add(self.frame_video_sink)
|
2004-10-11 09:55:44 +00:00
|
|
|
|
2005-10-09 17:53:58 +00:00
|
|
|
def frame_video_sink(self):
|
2004-10-11 09:55:44 +00:00
|
|
|
self.set_window_id(self.window.xid)
|
|
|
|
|
|
|
|
def set_window_id(self, xid):
|
|
|
|
self.imagesink.set_xwindow_id(xid)
|
2005-10-09 17:53:58 +00:00
|
|
|
|
|
|
|
def unframe_video_sink(self):
|
|
|
|
self.set_window_id(0L)
|
2004-10-11 09:55:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PlayerWindow(gtk.Window):
|
|
|
|
UPDATE_INTERVAL = 500
|
|
|
|
def __init__(self):
|
|
|
|
gtk.Window.__init__(self)
|
|
|
|
self.connect('delete-event', gtk.main_quit)
|
|
|
|
self.set_default_size(96, 96)
|
|
|
|
|
|
|
|
self.player = GstPlayer()
|
|
|
|
|
|
|
|
self.create_ui()
|
|
|
|
|
|
|
|
self.update_id = -1
|
2005-10-09 17:53:58 +00:00
|
|
|
self.changed_id = -1
|
|
|
|
self.seek_timeout_id = -1
|
|
|
|
|
|
|
|
self.p_position = gst.CLOCK_TIME_NONE
|
|
|
|
self.p_duration = gst.CLOCK_TIME_NONE
|
2004-10-11 09:55:44 +00:00
|
|
|
|
|
|
|
def load_file(self, location):
|
|
|
|
self.player.set_location(location)
|
|
|
|
|
|
|
|
def create_ui(self):
|
|
|
|
vbox = gtk.VBox()
|
|
|
|
|
2005-10-09 17:53:58 +00:00
|
|
|
self.videowidget = VideoWidget(self.player)
|
|
|
|
vbox.pack_start(self.videowidget)
|
2004-10-11 09:55:44 +00:00
|
|
|
|
|
|
|
hbox = gtk.HBox()
|
2005-11-04 10:14:22 +00:00
|
|
|
vbox.pack_start(hbox, fill=False, expand=False)
|
2004-10-11 09:55:44 +00:00
|
|
|
|
|
|
|
button = gtk.Button('play')
|
|
|
|
button.connect('clicked', self.play_clicked_cb)
|
|
|
|
hbox.pack_start(button, False)
|
|
|
|
|
|
|
|
button = gtk.Button("pause");
|
|
|
|
button.connect('clicked', self.pause_clicked_cb)
|
|
|
|
hbox.pack_start(button, False)
|
|
|
|
|
|
|
|
button = gtk.Button("stop");
|
|
|
|
button.connect('clicked', self.stop_clicked_cb)
|
|
|
|
hbox.pack_start(button, False)
|
|
|
|
|
|
|
|
self.adjustment = gtk.Adjustment(0.0, 0.00, 100.0, 0.1, 1.0, 1.0)
|
|
|
|
hscale = gtk.HScale(self.adjustment)
|
|
|
|
hscale.set_digits(2)
|
|
|
|
hscale.set_update_policy(gtk.UPDATE_CONTINUOUS)
|
|
|
|
hscale.connect('button-press-event', self.scale_button_press_cb)
|
|
|
|
hscale.connect('button-release-event', self.scale_button_release_cb)
|
|
|
|
hscale.connect('format-value', self.scale_format_value_cb)
|
|
|
|
hbox.pack_start(hscale)
|
2005-10-09 17:53:58 +00:00
|
|
|
self.hscale = hscale
|
2004-10-11 09:55:44 +00:00
|
|
|
|
|
|
|
self.add(vbox)
|
|
|
|
|
|
|
|
def scale_format_value_cb(self, scale, value):
|
2005-10-09 17:53:58 +00:00
|
|
|
if self.p_duration == -1:
|
2004-10-11 09:55:44 +00:00
|
|
|
real = 0
|
|
|
|
else:
|
2005-10-09 17:53:58 +00:00
|
|
|
real = value * self.p_duration / 100
|
2004-10-11 09:55:44 +00:00
|
|
|
|
|
|
|
seconds = real / gst.SECOND
|
|
|
|
|
|
|
|
return "%02d:%02d" % (seconds / 60, seconds % 60)
|
|
|
|
|
|
|
|
def scale_button_press_cb(self, widget, event):
|
2005-10-09 17:53:58 +00:00
|
|
|
# see seek.c:start_seek
|
|
|
|
gst.debug('starting seek')
|
2004-10-11 09:55:44 +00:00
|
|
|
self.player.pause()
|
2005-10-09 17:53:58 +00:00
|
|
|
|
|
|
|
# don't timeout-update position during seek
|
2004-10-11 09:55:44 +00:00
|
|
|
if self.update_id != -1:
|
2005-10-09 17:53:58 +00:00
|
|
|
gobject.source_remove(self.update_id)
|
2004-10-11 09:55:44 +00:00
|
|
|
self.update_id = -1
|
2005-10-09 17:53:58 +00:00
|
|
|
|
|
|
|
# make sure we get changed notifies
|
|
|
|
if self.changed_id == -1:
|
|
|
|
self.changed_id = self.hscale.connect('value-changed',
|
|
|
|
self.scale_value_changed_cb)
|
2004-10-11 09:55:44 +00:00
|
|
|
|
2005-10-09 17:53:58 +00:00
|
|
|
def scale_value_changed_cb(self, scale):
|
|
|
|
# see seek.c:seek_cb
|
|
|
|
real = long(scale.get_value() * self.p_duration / 100) # in ns
|
|
|
|
gst.debug('value changed, perform seek to %r' % real)
|
2004-10-11 09:55:44 +00:00
|
|
|
self.player.seek(real)
|
2005-10-09 17:53:58 +00:00
|
|
|
# allow for a preroll
|
2005-11-04 10:14:22 +00:00
|
|
|
self.player.get_state(timeout=50) # 50 ms
|
2005-10-09 17:53:58 +00:00
|
|
|
|
|
|
|
def scale_button_release_cb(self, widget, event):
|
|
|
|
# see seek.cstop_seek
|
|
|
|
widget.disconnect(self.changed_id)
|
|
|
|
self.changed_id = -1
|
|
|
|
|
|
|
|
if self.seek_timeout_id != -1:
|
|
|
|
gobject.source_remove(self.seek_timeout_id)
|
|
|
|
self.seek_timeout_id = -1
|
|
|
|
else:
|
|
|
|
gst.debug('released slider, setting back to playing')
|
|
|
|
self.player.play()
|
|
|
|
|
|
|
|
if self.update_id != -1:
|
|
|
|
self.error('Had a previous update timeout id')
|
|
|
|
else:
|
|
|
|
self.update_id = gobject.timeout_add(self.UPDATE_INTERVAL,
|
|
|
|
self.update_scale_cb)
|
2004-10-11 09:55:44 +00:00
|
|
|
|
|
|
|
def update_scale_cb(self):
|
2005-10-09 17:53:58 +00:00
|
|
|
self.p_position, self.p_duration, format = self.player.query_position()
|
|
|
|
if self.p_position != gst.CLOCK_TIME_NONE:
|
|
|
|
value = self.p_position * 100.0 / self.p_duration
|
2004-10-11 09:55:44 +00:00
|
|
|
self.adjustment.set_value(value)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def play_clicked_cb(self, button):
|
|
|
|
if self.player.is_playing():
|
|
|
|
return
|
|
|
|
|
2005-10-09 17:53:58 +00:00
|
|
|
self.videowidget.frame_video_sink()
|
2004-10-11 09:55:44 +00:00
|
|
|
self.player.play()
|
2005-10-09 17:53:58 +00:00
|
|
|
# keep the time display updated
|
|
|
|
self.update_id = gobject.timeout_add(self.UPDATE_INTERVAL,
|
2004-10-11 09:55:44 +00:00
|
|
|
self.update_scale_cb)
|
|
|
|
|
|
|
|
def pause_clicked_cb(self, button):
|
|
|
|
if self.player.is_paused():
|
|
|
|
return
|
|
|
|
|
|
|
|
self.player.pause()
|
|
|
|
if self.update_id != -1:
|
2005-10-09 17:53:58 +00:00
|
|
|
gobject.source_remove(self.update_id)
|
2004-10-11 09:55:44 +00:00
|
|
|
self.update_id = -1
|
|
|
|
|
|
|
|
def stop_clicked_cb(self, button):
|
|
|
|
if self.player.is_stopped():
|
|
|
|
return
|
|
|
|
|
|
|
|
self.player.stop()
|
2005-10-09 17:53:58 +00:00
|
|
|
self.videowidget.unframe_video_sink()
|
2004-10-11 09:55:44 +00:00
|
|
|
if self.update_id != -1:
|
2005-10-09 17:53:58 +00:00
|
|
|
gobject.source_remove(self.update_id)
|
2004-10-11 09:55:44 +00:00
|
|
|
self.update_id = -1
|
|
|
|
self.adjustment.set_value(0.0)
|
|
|
|
|
|
|
|
def main(args):
|
|
|
|
w = PlayerWindow()
|
|
|
|
w.load_file(args[1])
|
|
|
|
w.show_all()
|
|
|
|
|
|
|
|
gtk.main()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main(sys.argv))
|