2
# -*- coding: utf-8 -*-
6
__authors__ = ["Jan Jokela <janjokela@gmail.com>"]
7
__licenses__ = ["LICENSE.LGPL"]
8
__description__ = "Frame widget"
19
from container import Container
21
class Video(Container):
23
A video widget offers easy and simple video playback backed by GStreamer.
24
Features include seamless playback, support for all GStreamer compatible
25
video streams, basic playback control and support for native, widescreen
26
(16:9), TV (4:3) and "smart" aspect ratios.
30
def __init__(self, source=None, aspect_ratio="native"):
31
""" Initialize video widget """
33
super(Video, self).__init__()
36
self._aspect_ratio = aspect_ratio
40
self._update_style(self.style)
42
def _update_style(self, props=None):
45
super(Video, self)._update_style(props)
49
def _update_layout(self):
50
""" Updates layout """
52
super(Video, self)._update_layout()
57
widthu = self.get_widthu()
58
heightu = self.get_heightu()
60
# Account for 0 width or height
62
if widthu == 0 or heightu == 0:
65
width_delta = math.fabs(widthu - widthu * 0.5625)
66
height_delta = math.fabs(heightu - heightu * 1.7777)
69
if self.aspect_ratio == "native":
70
self._texture.set_xu(0)
71
self._texture.set_yu(0)
72
self._texture.set_widthu(widthu)
73
self._texture.set_heightu(heightu)
75
# 16:9 widescreen (720p & 1080p)
76
elif self.aspect_ratio == "widescreen":
77
if width_delta < height_delta:
78
video_height = int(widthu * 0.5625)
79
self._texture.set_widthu(widthu)
80
self._texture.set_heightu(video_height)
81
self._texture.set_xu(0)
82
self._texture.set_yu(int((heightu - video_height) / 2.0))
84
video_width = int(heightu * 1.7777)
85
self._texture.set_widthu(video_width)
86
self._texture.set_heightu(heightu)
87
self._texture.set_xu(int((widthu - video_width) / 2.0))
88
self._texture.set_yu(0)
91
elif self.aspect_ratio == "tv":
92
if width_delta < height_delta:
93
video_height = int(width * 0.75)
94
self._texture.set_width(width)
95
self._texture.set_height(video_height)
96
self._texture.set_x(0)
97
self._texture.set_y(int((height - video_height) / 2.0))
99
video_width = int(height * 1.3333)
100
self._texture.set_width(video_width)
101
self._texture.set_height(height)
102
self._texture.set_x(int((width - video_width) / 2.0))
103
self._texture.set_y(0)
105
# smart: maximum viewing area without cutting too much
106
elif self.aspect_ratio == "smart":
107
if width_delta < height_delta:
108
video_height = int(width * 0.5625)
109
self._texture.set_width(width)
110
self._texture.set_height(video_height)
111
self._texture.set_x(0)
112
self._texture.set_y(int((height - video_height) / 2.0))
114
video_width = int(height * 1.7777)
115
self._texture.set_width(video_width)
116
self._texture.set_height(height)
117
self._texture.set_x(int((width - video_width) / 2.0))
118
self._texture.set_y(0)
121
def get_source(self):
122
""" Retrieve video source (uri) """
126
def set_source(self, value):
127
""" Sets video source
129
value -- (str) source file uri
134
source = property(get_source, set_source)
136
def get_aspect_ratio(self):
137
""" Retireve aspect ratio """
139
return self._aspect_ratio
141
def set_aspect_ratio(self, value):
142
""" Sets aspect ratio
144
value -- (str) 'native', 'widescreen', 'tv' or 'smart'
147
self._aspect_ratio = value
149
aspect_ratio = property(get_aspect_ratio, set_aspect_ratio)
154
""" Play video. Resumes or starts video playback """
156
if not self.is_playing():
157
self._texture.set_playing(True)
160
""" Pause video playback """
162
if self.is_playing():
163
self._texture.set_playing(False)
166
""" Stop video playback """
169
self.remove(self._texture)
170
self._texture.set_playing(False)
171
self._texture.set_property("position", 0)
173
def is_playing(self):
174
""" Retrieve wether video is playing """
177
if self._texture.get_playing():
181
def _on_gst_message(self, bus, message):
182
""" Messages from our gstreamer bus """
184
if message.type == gst.MESSAGE_EOS:
186
elif message.type == gst.MESSAGE_ERROR:
187
print "Gstreamer exception -- bus: " + str(bus) + ", message: " + \
190
def _set_source(self):
191
""" Set texture based on given source """
193
width = self.get_width()
194
height = self.get_height()
196
# Account for 0 width or height
197
if width == 0 or height == 0:
200
# Account for no size change
201
if self._texture and \
202
width == self._texture.get_width() and \
203
height == self._texture.get_height():
206
if os.path.exists(self.source):
207
self._texture = cluttergst.VideoTexture()
208
self.playbin = self._texture.get_playbin()
209
self.bus = self.playbin.get_bus()
210
self.bus.add_signal_watch()
211
self.bus.connect('message', self._on_gst_message)
213
self._texture.set_filename(self.source)
214
self.add(self._texture)
217
raise AttributeError, "source doesn't exist: %s" % self.source