21 Classes and functions for building Matplotlib-based GStreamer elements 23 __author__ =
"Leo Singer <leo.singer@ligo.org>" 24 __all__ = (
"padtemplate",
"figure",
"render",
"BaseMatplotlibTransform")
28 gi.require_version(
'Gst',
'1.0')
29 from gi.repository
import GObject
30 from gi.repository
import Gst
31 GObject.threads_init()
34 from gstlal
import pipeio
37 """Pad template suitable for producing video frames using Matplotlib. 38 The Agg backend supports rgba, argb, and bgra.""" 39 padtemplate = Gst.PadTemplate(
41 Gst.PadDirection.SRC, Gst.PadPresence.ALWAYS,
42 Gst.caps_from_string(
""" 44 format = (string) {RGB, ARGB, RGBA, BGRA}, 45 width = (int) [1, MAX], 46 height = (int) [1, MAX], 47 framerate = (fraction) [0/1, MAX] 53 """Create a Matplotlib Figure object suitable for rendering video frames.""" 55 matplotlib.rcParams.update({
57 "axes.titlesize": 10.0,
58 "axes.labelsize": 10.0,
59 "xtick.labelsize": 8.0,
60 "ytick.labelsize": 8.0,
61 "legend.fontsize": 8.0,
67 from matplotlib
import figure
68 from matplotlib.backends.backend_agg
import FigureCanvasAgg
69 figure = figure.Figure()
70 FigureCanvasAgg(figure)
74 def render(fig, buf, (width, height), fmt):
75 """Render a Matplotlib figure to a GStreamer buffer.""" 77 width / float(fig.get_dpi()),
78 height / float(fig.get_dpi())
82 imgdata = fig.canvas.renderer._renderer.tostring_rgb()
84 imgdata = fig.canvas.renderer._renderer.tostring_argb()
86 imgdata = fig.canvas.renderer._renderer.buffer_rgba()
88 imgdata = fig.canvas.renderer._renderer.tostring_bgra()
90 raise ValueError(
'invalid format "%s"' % fmt)
91 datasize = len(imgdata)
92 buf[:datasize] = imgdata
93 buf.datasize = datasize
97 """Base class for transform elements that use Matplotlib to render video.""" 99 __gsttemplates__ = padtemplate
106 """GstBaseTransform->transform_caps virtual method.""" 107 if direction == Gst.PadDirection.SRC:
108 return self.get_static_pad(
"sink").get_fixed_caps_func()
109 elif direction == Gst.PadDirection.SINK:
110 return self.get_static_pad(
"src").get_fixed_caps_func()
111 raise ValueError(direction)
114 """GstBaseTransform->transform_size virtual method.""" 115 if direction == Gst.PadDirection.SINK:
116 return pipeio.get_unit_size(self.get_static_pad(
"src").query_caps(
None))
117 raise ValueError(direction)
119 GObject.type_register(BaseMatplotlibTransform)