File: user_element.py

package info (click to toggle)
linux-show-player 0.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,896 kB
  • sloc: python: 12,408; sh: 154; makefile: 17; xml: 8
file content (91 lines) | stat: -rw-r--r-- 3,423 bytes parent folder | download | duplicates (4)
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
# -*- coding: utf-8 -*-
#
# This file is part of Linux Show Player
#
# Copyright 2012-2016 Francesco Ceruti <ceppofrancy@gmail.com>
#
# Linux Show Player is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Linux Show Player is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Linux Show Player.  If not, see <http://www.gnu.org/licenses/>.

from PyQt5.QtCore import QT_TRANSLATE_NOOP

from lisp.backend.media_element import ElementType, MediaType
from lisp.core.has_properties import Property
from lisp.modules.gst_backend.gi_repository import Gst
from lisp.modules.gst_backend.gst_element import GstMediaElement


class UserElement(GstMediaElement):
    ElementType = ElementType.Plugin
    MediaType = MediaType.Audio
    Name = QT_TRANSLATE_NOOP('MediaElementName', 'Custom Element')

    bin = Property(default='')

    def __init__(self, pipeline):
        super().__init__()

        self.pipeline = pipeline
        self.audio_convert_sink = Gst.ElementFactory.make("audioconvert", None)
        # A default assignment for the bin
        self.gst_bin = Gst.ElementFactory.make("identity", None)
        self.gst_bin.set_property("signal-handoffs", False)
        self.audio_convert_src = Gst.ElementFactory.make("audioconvert", None)

        pipeline.add(self.audio_convert_sink)
        pipeline.add(self.gst_bin)
        pipeline.add(self.audio_convert_src)

        self.audio_convert_sink.link(self.gst_bin)
        self.gst_bin.link(self.audio_convert_src)

        self._old_bin = self.gst_bin
        self.changed('bin').connect(self.__prepare_bin)

    def sink(self):
        return self.audio_convert_sink

    def src(self):
        return self.audio_convert_src

    def __prepare_bin(self, value):
        if value != '' and value != self._old_bin:
            self._old_bin = value

            # If in playing we need to restart the pipeline after unblocking
            playing = self.gst_bin.current_state == Gst.State.PLAYING
            # Block the stream
            pad = self.audio_convert_sink.sinkpads[0]
            probe = pad.add_probe(Gst.PadProbeType.BLOCK, lambda *a: 0, "")

            # Unlink the components
            self.audio_convert_sink.unlink(self.gst_bin)
            self.gst_bin.unlink(self.audio_convert_src)
            self.pipeline.remove(self.gst_bin)

            # Create the bin, when fail use a do-nothing element
            try:
                self.gst_bin = Gst.parse_bin_from_description(value, True)
            except Exception:
                self.gst_bin = Gst.ElementFactory.make("identity", None)
                self.gst_bin.set_property("signal-handoffs", False)

            # Link the components
            self.pipeline.add(self.gst_bin)
            self.audio_convert_sink.link(self.gst_bin)
            self.gst_bin.link(self.audio_convert_src)

            # Unblock the stream
            pad.remove_probe(probe)
            if playing:
                self.pipeline.set_state(Gst.State.PLAYING)