Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/gplay.py
blob: 52d34cfad894462d3d31a0100790d95a443ce96b (plain)
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
#Copyright (c) 2008, Media Modifications Ltd.

#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:

#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.

import gi
gi.require_version('Gst', '1.0')

from gi.repository import GObject
from gi.repository import Gst
from gi.repository import GstVideo

import logging
logger = logging.getLogger('record:gplay.py')

class Gplay(GObject.GObject):
    
    __gsignals__ = {
    'playback-status-changed': (GObject.SIGNAL_RUN_LAST,
        GObject.TYPE_NONE, (GObject.TYPE_INT, GObject.TYPE_FLOAT))}

    def __init__(self, activity_obj):
        
        super(Gplay, self).__init__()
        
        self.activity = activity_obj
        self._playback_monitor_handler = None
        self._player = Gst.ElementFactory.make("playbin", "player")

        bus = self._player.get_bus()
        bus.add_signal_watch()
        bus.connect('message::error', self._bus_error)
        bus.connect('message::eos', self._bus_eos)

    def _bus_error(self, bus, message):
        
        err, debug = message.parse_error()
        logger.error('bus error=%s debug=%s' % (err, debug))

    def _bus_eos(self, bus, message):
        
        self.stop()

    def set_location(self, location):
        
        if self._player.get_property('uri') == location:
            self.seek(0)
            return

        self._player.set_state(Gst.State.READY)
        self._player.set_property('uri', location)

    def seek(self, position):
        
        if position == 0:
            location = 0
            
        else:
            duration = self._player.query_duration(Gst.Format.TIME, None)[0]
            location = duration * (position / 100)

        event = Gst.Event.new_seek(
            1.0, Gst.Format.TIME,
            Gst.SeekFlags.FLUSH |
            Gst.SeekFlags.ACCURATE,
            Gst.SeekType.SET, location,
            Gst.SeekType.NONE, 0)
            
        res = self._player.send_event(event)
        
        if res:
            # FIXME: 'Pipeline' object has no attribute 'new_stream_time'
            #self._player.set_new_stream_time(0L)
            pass

    def pause(self):
        
        self._player.set_state(Gst.State.PAUSED)

    def play(self):
        
        if self.get_state() == Gst.State.PLAYING:
            return

        if not self._player.props.video_sink:
            sink = Gst.ElementFactory.make('xvimagesink', 'xvimagesink')
            sink.props.force_aspect_ratio = True
            self._player.props.video_sink = sink

        self.activity.set_gplay_sink(self._player.props.video_sink)
        self._player.set_state(Gst.State.PLAYING)
        self._emit_playback_status(0)

        self._playback_monitor_handler = GObject.timeout_add(500, self._playback_monitor)

    def _playback_monitor(self):
        
        try:
            position = self._player.query_position(Gst.Format.TIME)[0]
            duration = self._player.query_duration(Gst.Format.TIME)[0]
            
        except:
            return True

        value = (float(position) / float(duration)) * 100.0
        self._emit_playback_status(value)
        
        
        return True

    def _emit_playback_status(self, position):
        
        state = self._player.get_state(0)[1]
        self.emit('playback-status-changed', state, position)

    def get_state(self):
        
        return self._player.get_state(0)[1]

    def stop(self):
        
        if self._playback_monitor_handler:
            GObject.source_remove(self._playback_monitor_handler)
            self._playback_monitor_handler = None
            
        self._player.set_state(Gst.State.NULL)
        self._emit_playback_status(0)