1# Copyright 2004-2011 Joe Wreschnig, Michael Urman, Steven Robertson,
2#           2011-2014 Christoph Reiter
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
8
9from gi.repository import Gtk
10
11from quodlibet import config
12from quodlibet import _
13from quodlibet.qltk.ccb import ConfigCheckButton
14from quodlibet.qltk.entry import UndoEntry
15from quodlibet.qltk.x import Button
16from quodlibet.qltk import Icons
17from quodlibet.util import connect_obj
18
19
20class GstPlayerPreferences(Gtk.VBox):
21    def __init__(self, player, debug=False):
22        super(GstPlayerPreferences, self).__init__(spacing=6)
23
24        e = UndoEntry()
25        e.set_tooltip_text(_("The GStreamer output pipeline used for "
26                "playback. Leave blank for the default pipeline. "
27                "In case the pipeline contains a sink, "
28                "it will be used instead of the default one."))
29
30        e.set_text(config.get('player', 'gst_pipeline'))
31
32        def changed(entry):
33            config.set('player', 'gst_pipeline', entry.get_text())
34        e.connect('changed', changed)
35
36        pipe_label = Gtk.Label(label=_('_Output pipeline:'))
37        pipe_label.set_use_underline(True)
38        pipe_label.set_mnemonic_widget(e)
39
40        apply_button = Button(_("_Apply"))
41
42        def format_buffer(scale, value):
43            return _("%.1f seconds") % value
44
45        def scale_changed(scale):
46            duration_msec = int(scale.get_value() * 1000)
47            player._set_buffer_duration(duration_msec)
48
49        duration = config.getfloat("player", "gst_buffer")
50        scale = Gtk.HScale.new(
51            Gtk.Adjustment(value=duration, lower=0.2, upper=10))
52        scale.set_value_pos(Gtk.PositionType.RIGHT)
53        scale.set_show_fill_level(True)
54        scale.connect('format-value', format_buffer)
55        scale.connect('value-changed', scale_changed)
56
57        buffer_label = Gtk.Label(label=_('_Buffer duration:'))
58        buffer_label.set_use_underline(True)
59        buffer_label.set_mnemonic_widget(scale)
60
61        def rebuild_pipeline(*args):
62            player._rebuild_pipeline()
63        apply_button.connect('clicked', rebuild_pipeline)
64
65        gapless_button = ConfigCheckButton(
66            _('Disable _gapless playback'),
67            "player", "gst_disable_gapless", populate=True)
68        gapless_button.set_alignment(0.0, 0.5)
69        gapless_button.set_tooltip_text(
70            _("Disabling gapless playback can avoid track changing problems "
71              "with some GStreamer versions"))
72
73        widgets = [(pipe_label, e, apply_button),
74                   (buffer_label, scale, None),
75        ]
76
77        table = Gtk.Table(n_rows=len(widgets), n_columns=3)
78        table.set_col_spacings(6)
79        table.set_row_spacings(6)
80        for i, (left, middle, right) in enumerate(widgets):
81            left.set_alignment(0.0, 0.5)
82            table.attach(left, 0, 1, i, i + 1,
83                         xoptions=Gtk.AttachOptions.FILL |
84                         Gtk.AttachOptions.SHRINK)
85            if right:
86                table.attach(middle, 1, 2, i, i + 1)
87                table.attach(right, 2, 3, i, i + 1,
88                             xoptions=Gtk.AttachOptions.FILL |
89                             Gtk.AttachOptions.SHRINK)
90            else:
91                table.attach(middle, 1, 3, i, i + 1)
92
93        table.attach(gapless_button, 0, 3, 2, 3)
94
95        self.pack_start(table, True, True, 0)
96
97        if debug:
98            def print_bin(player):
99                player._print_pipeline()
100
101            b = Button("Print Pipeline", Icons.DIALOG_INFORMATION)
102            connect_obj(b, 'clicked', print_bin, player)
103            self.pack_start(b, True, True, 0)
104