| 1 |
# Copyright (C) 2008-2010 Adam Olsen |
| 2 |
# |
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation; either version 2, or (at your option) |
| 6 |
# any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program; if not, write to the Free Software |
| 15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 |
# |
| 17 |
# |
| 18 |
# The developers of the Exaile media player hereby grant permission |
| 19 |
# for non-GPL compatible GStreamer and Exaile plugins to be used and |
| 20 |
# distributed together with GStreamer and Exaile. This permission is |
| 21 |
# above and beyond the permissions granted by the GPL license by which |
| 22 |
# Exaile is covered. If you modify this code, you may extend this |
| 23 |
# exception to your version of the code, but you are not obligated to |
| 24 |
# do so. If you do not wish to do so, delete this exception statement |
| 25 |
# from your version. |
| 26 |
|
| 27 |
|
| 28 |
from gi.repository import Gst |
| 29 |
|
| 30 |
from xl.providers import ProviderHandler |
| 31 |
|
| 32 |
import logging |
| 33 |
|
| 34 |
logger = logging.getLogger(__name__) |
| 35 |
|
| 36 |
|
| 37 |
def element_link_many(*elems): |
| 38 |
for i in range(len(elems) - 1): |
| 39 |
e = elems[i] |
| 40 |
n = elems[i + 1] |
| 41 |
e.link(n) |
| 42 |
|
| 43 |
|
| 44 |
def disable_video_text(bin): |
| 45 |
# From quod libet: disable all video/text decoding in playbin |
| 46 |
GST_PLAY_FLAG_VIDEO = 1 << 0 |
| 47 |
GST_PLAY_FLAG_TEXT = 1 << 2 |
| 48 |
flags = bin.get_property("flags") |
| 49 |
flags &= ~(GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_TEXT) |
| 50 |
bin.set_property("flags", flags) |
| 51 |
|
| 52 |
|
| 53 |
class ElementBin(Gst.Bin): |
| 54 |
""" |
| 55 |
A bin for easily containing elements |
| 56 |
|
| 57 |
elements are added to the elements dictionary in the form of |
| 58 |
elements[position] = element |
| 59 |
where position is a value from 0-100 indicating its position |
| 60 |
in the resulting bin, and element is the Gst.Element itself. |
| 61 |
|
| 62 |
changes made to elements do not apply until setup_elements() |
| 63 |
is called |
| 64 |
""" |
| 65 |
|
| 66 |
def __init__(self, name=None): |
| 67 |
if name: |
| 68 |
Gst.Bin.__init__(self, name=name) |
| 69 |
else: |
| 70 |
Gst.Bin.__init__(self) |
| 71 |
|
| 72 |
self.elements = {} |
| 73 |
self.added_elems = [] |
| 74 |
self.srcpad = None |
| 75 |
self.sinkpad = None |
| 76 |
self.src = None |
| 77 |
self.sink = None |
| 78 |
|
| 79 |
def setup_elements(self): |
| 80 |
""" |
| 81 |
This function should only be called when state is NULL |
| 82 |
|
| 83 |
:returns: True if there are elements, False otherwise |
| 84 |
""" |
| 85 |
name = self.get_name() |
| 86 |
logger.debug("%s: Removing all elements", name) |
| 87 |
|
| 88 |
if len(self.added_elems) > 0: |
| 89 |
for elem in self.added_elems: |
| 90 |
elem.set_state(Gst.State.NULL) |
| 91 |
self.remove(elem) |
| 92 |
|
| 93 |
all_elems = sorted(self.elements.items()) |
| 94 |
|
| 95 |
elems = [] |
| 96 |
for _unused, e in all_elems: |
| 97 |
# Don't add empty elements! |
| 98 |
if hasattr(e, 'setup_elements'): |
| 99 |
if not e.setup_elements(): |
| 100 |
continue |
| 101 |
|
| 102 |
elems.append(e) |
| 103 |
|
| 104 |
self.added_elems = elems |
| 105 |
|
| 106 |
if len(elems) == 0: |
| 107 |
return False |
| 108 |
|
| 109 |
for e in elems: |
| 110 |
self.add(e) |
| 111 |
e.sync_state_with_parent() |
| 112 |
logger.debug("%s: Adding %s", name, e.get_name()) |
| 113 |
|
| 114 |
element_link_many(*elems) |
| 115 |
|
| 116 |
self.srcpad = elems[-1].get_static_pad("src") |
| 117 |
if self.src is not None: |
| 118 |
self.src.set_target(self.srcpad) |
| 119 |
else: |
| 120 |
self.src = Gst.GhostPad.new('src', self.srcpad) |
| 121 |
self.add_pad(self.src) |
| 122 |
|
| 123 |
self.sinkpad = elems[0].get_static_pad("sink") |
| 124 |
if self.sink is not None: |
| 125 |
self.sink.set_target(self.sinkpad) |
| 126 |
else: |
| 127 |
self.sink = Gst.GhostPad.new('sink', self.sinkpad) |
| 128 |
self.add_pad(self.sink) |
| 129 |
|
| 130 |
return True |
| 131 |
|
| 132 |
|
| 133 |
class ProviderBin(ElementBin, ProviderHandler): |
| 134 |
""" |
| 135 |
A ProviderBin is a Gst.Bin that adds and removes elements from itself |
| 136 |
using the providers system. Providers should be a subclass of |
| 137 |
Gst.Element and provide the following attributes: |
| 138 |
name - name to use for this element |
| 139 |
index - priority within the pipeline. range [0-100] integer. |
| 140 |
lower numbers are higher priority. elements must |
| 141 |
choose a unique number. |
| 142 |
""" |
| 143 |
|
| 144 |
def __init__(self, servicename, name=None): |
| 145 |
""" |
| 146 |
:param servicename: the Provider name to listen for |
| 147 |
""" |
| 148 |
if name is None: |
| 149 |
name = servicename |
| 150 |
ElementBin.__init__(self, name=name) |
| 151 |
ProviderHandler.__init__(self, servicename) |
| 152 |
|
| 153 |
self.reset_providers() |
| 154 |
|
| 155 |
def reset_providers(self): |
| 156 |
self.elements = {} |
| 157 |
dups = {} |
| 158 |
for provider in self.get_providers(): |
| 159 |
idx = provider.index |
| 160 |
if idx in self.elements: |
| 161 |
dup = dups.setdefault(idx, [self.elements[idx].name]) |
| 162 |
dup.append(provider.name) |
| 163 |
while idx in self.elements: |
| 164 |
idx += 1 |
| 165 |
try: |
| 166 |
self.elements[idx] = provider() |
| 167 |
except Exception: |
| 168 |
logger.exception( |
| 169 |
"Could not create %s element for %s.", provider, self.get_name() |
| 170 |
) |
| 171 |
|
| 172 |
for k, v in dups.items(): |
| 173 |
logger.warning( |
| 174 |
"Audio plugins %s are sharing index %s (may have unpredictable output!)", |
| 175 |
v, |
| 176 |
k, |
| 177 |
) |
| 178 |
|
| 179 |
def on_provider_added(self, provider): |
| 180 |
self.reset_providers() |
| 181 |
|
| 182 |
def on_provider_removed(self, provider): |
| 183 |
self.reset_providers() |
| 184 |
|
| 185 |
|
| 186 |
def parse_stream_tags(track, tag_list): |
| 187 |
""" |
| 188 |
Called when a tag is found in a stream. |
| 189 |
|
| 190 |
:type track: xl.trax.Track |
| 191 |
:type tag_list: Gst.TagList |
| 192 |
|
| 193 |
Gst.TagList guarantees that all strings are either ASCII or UTF8 |
| 194 |
""" |
| 195 |
|
| 196 |
newsong = False |
| 197 |
|
| 198 |
keep = [ |
| 199 |
'bitrate', |
| 200 |
'duration', |
| 201 |
'track-number', |
| 202 |
'track-count', |
| 203 |
'album-disc-number', |
| 204 |
'album-disc-count', |
| 205 |
'album', |
| 206 |
'artist', |
| 207 |
'genre', |
| 208 |
'comment', |
| 209 |
'title', |
| 210 |
'datetime', |
| 211 |
] |
| 212 |
|
| 213 |
# Build a dictionary first |
| 214 |
tags = {} |
| 215 |
for i in range(tag_list.n_tags()): |
| 216 |
k = tag_list.nth_tag_name(i) |
| 217 |
if k not in keep: |
| 218 |
continue |
| 219 |
|
| 220 |
values = [ |
| 221 |
tag_list.get_value_index(k, vi) for vi in range(tag_list.get_tag_size(k)) |
| 222 |
] |
| 223 |
|
| 224 |
tags[k] = values |
| 225 |
|
| 226 |
etags = {} |
| 227 |
|
| 228 |
v = tags.get('bitrate') |
| 229 |
if v: |
| 230 |
etags['__bitrate'] = int(v[0]) |
| 231 |
|
| 232 |
v = tags.get('duration') |
| 233 |
if v: |
| 234 |
etags['__length'] = float(v[0]) / Gst.SECOND |
| 235 |
|
| 236 |
v = tags.get('track-number') |
| 237 |
if v: |
| 238 |
c = tags.get('track-count') |
| 239 |
if c: |
| 240 |
etags['tracknumber'] = '%d/%d' % (v[0], c[0]) |
| 241 |
else: |
| 242 |
etags['tracknumber'] = '%d' % (v[0]) |
| 243 |
|
| 244 |
v = tags.get('album-disc-number') |
| 245 |
if v: |
| 246 |
c = tags.get('album-disc-count') |
| 247 |
if c: |
| 248 |
etags['discnumber'] = '%d/%d' % (v[0], c[0]) |
| 249 |
else: |
| 250 |
etags['discnumber'] = '%d' % (v[0]) |
| 251 |
|
| 252 |
v = tags.get('album') |
| 253 |
if v: |
| 254 |
etags['album'] = v |
| 255 |
|
| 256 |
v = tags.get('artist') |
| 257 |
if v: |
| 258 |
etags['artist'] = v |
| 259 |
|
| 260 |
v = tags.get('genre') |
| 261 |
if v: |
| 262 |
etags['genre'] = v |
| 263 |
|
| 264 |
v = tags.get('datetime') |
| 265 |
if v: |
| 266 |
# v[0] is a Gst.DateTime object |
| 267 |
etags['date'] = v[0].to_iso8601_string() |
| 268 |
|
| 269 |
# if there's a comment, but no album, set album to the comment |
| 270 |
v = tags.get('comment') |
| 271 |
if v and not track.get_tag_raw('album'): |
| 272 |
etags['album'] = v |
| 273 |
|
| 274 |
v = tags.get('title') |
| 275 |
if v: |
| 276 |
try: |
| 277 |
if track.get_tag_raw('__rawtitle') != v: |
| 278 |
etags['__rawtitle'] = v |
| 279 |
newsong = True |
| 280 |
except AttributeError: |
| 281 |
etags['__rawtitle'] = v |
| 282 |
newsong = True |
| 283 |
|
| 284 |
if not etags.get('artist'): |
| 285 |
title_array = v[0].split(' - ', 1) |
| 286 |
if len(title_array) == 1 or track.get_loc_for_io().lower().endswith(".mp3"): |
| 287 |
etags['title'] = v |
| 288 |
else: |
| 289 |
etags['artist'] = [title_array[0]] |
| 290 |
etags['title'] = [title_array[1]] |
| 291 |
else: |
| 292 |
etags['title'] = v |
| 293 |
|
| 294 |
track.set_tags(**etags) |
| 295 |
return newsong |
| 296 |
|
| 297 |
|
| 298 |
# vim: et sts=4 sw=4 |