1

I am using the following code to show slide show via gstreamer (for simplicity, the following just re-shows the same image every second):

# python testcase.py large-image.jpg

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

from gi.repository import GLib, GObject, Gst
import sys


class Player(object):
    def __init__(self):
        Gst.init(None)

        self.pipeline = Gst.Pipeline()

        self.source_bin = None

        self.sink = Gst.ElementFactory.make("xvimagesink")
        self.pipeline.add(self.sink)

    def play(self):
        self._load_next_file()

    def _load_next_file(self):
        self.pipeline.set_state(Gst.State.READY)

        if self.source_bin is not None:
            self.pipeline.remove(self.source_bin)
            self.source_bin.set_state(Gst.State.NULL)

        self.source_bin = self._create_image_source_bin()
        self.pipeline.add(self.source_bin)
        self.source_bin.link(self.sink)
        self.source_bin.sync_state_with_parent()
        self.pipeline.set_state(Gst.State.PLAYING)

    def _create_image_source_bin(self):
        src = Gst.ElementFactory.make("filesrc")
        src.set_property("location", sys.argv[1])

        decoder = Gst.ElementFactory.make("jpegdec")

        freezer = Gst.ElementFactory.make("imagefreeze")

        GObject.timeout_add(1000, self._on_image_timeout)

        bin = Gst.Bin()
        bin.add(src)
        bin.add(decoder)
        bin.add(freezer)
        bin.add_pad(Gst.GhostPad.new("src", freezer.get_static_pad("src")))
        src.link(decoder)
        decoder.link(freezer)
        return bin

    def _on_image_timeout(self):
        self._load_next_file()
        return False

p = Player()
p.play()

GObject.MainLoop().run()

Memory usage increases every second and never decreases; looks like, old source_bin is not being freed when new one is being created. Is it a bug in gstreamer/python-gstreamer or am I missing something?

4

1 回答 1

0

在取消链接并将 source_bin 的状态设置为 null 后,您应该取消引用它以确保已销毁。

注意:恕我直言,ref/unref 系统很适合在 C 中使用,您可以在其中精确计算引用,并且在像 python 这样的垃圾收集高级环境中不是那么方便。在你的情况下我会做的(我在类似的问题中做过)是建立一个解码箱池并重用它们。

于 2016-08-16T15:59:55.930 回答