2014-02-18 10:06:10 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2013 Stefan Sauer <ensonic@users.sf.net>
|
|
|
|
*
|
|
|
|
* gstlatency.c: tracing module that logs processing latency stats
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
2014-07-16 16:48:52 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstlatency
|
|
|
|
* @short_description: log processing latency stats
|
|
|
|
*
|
|
|
|
* A tracing module that determines src-to-sink latencies by injecting custom
|
2016-01-10 13:30:05 +00:00
|
|
|
* events at sources and process them at sinks.
|
2014-07-16 16:48:52 +00:00
|
|
|
*/
|
2014-02-20 10:15:20 +00:00
|
|
|
/* TODO(ensonic): if there are two sources feeding into a mixer/muxer and later
|
|
|
|
* we fan-out with tee and have two sinks, each sink would get all two events,
|
|
|
|
* the later event would overwrite the former. Unfortunately when the buffer
|
|
|
|
* arrives on the sink we don't know to which event it correlates. Better would
|
|
|
|
* be to use the buffer meta in 1.0 instead of the event. Or we track a min/max
|
|
|
|
* latency.
|
|
|
|
*/
|
2014-02-18 10:06:10 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstlatency.h"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_latency_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_latency_debug
|
|
|
|
|
|
|
|
#define _do_init \
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_latency_debug, "latency", 0, "latency tracer");
|
|
|
|
#define gst_latency_tracer_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstLatencyTracer, gst_latency_tracer, GST_TYPE_TRACER,
|
|
|
|
_do_init);
|
|
|
|
|
|
|
|
static GQuark latency_probe_id;
|
|
|
|
static GQuark latency_probe_pad;
|
|
|
|
static GQuark latency_probe_ts;
|
|
|
|
|
2016-01-12 13:59:04 +00:00
|
|
|
static GstTracerRecord *tr_latency;
|
|
|
|
|
2014-02-18 10:06:10 +00:00
|
|
|
/* data helpers */
|
|
|
|
|
|
|
|
/*
|
2016-01-10 13:30:05 +00:00
|
|
|
* Get the element/bin owning the pad.
|
2014-02-18 10:06:10 +00:00
|
|
|
*
|
|
|
|
* in: a normal pad
|
|
|
|
* out: the element
|
|
|
|
*
|
|
|
|
* in: a proxy pad
|
|
|
|
* out: the element that contains the peer of the proxy
|
|
|
|
*
|
|
|
|
* in: a ghost pad
|
|
|
|
* out: the bin owning the ghostpad
|
|
|
|
*/
|
|
|
|
/* TODO(ensonic): gst_pad_get_parent_element() would not work here, should we
|
|
|
|
* add this as new api, e.g. gst_pad_find_parent_element();
|
|
|
|
*/
|
|
|
|
static GstElement *
|
|
|
|
get_real_pad_parent (GstPad * pad)
|
|
|
|
{
|
|
|
|
GstObject *parent;
|
|
|
|
|
|
|
|
if (!pad)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
parent = GST_OBJECT_PARENT (pad);
|
|
|
|
|
|
|
|
/* if parent of pad is a ghost-pad, then pad is a proxy_pad */
|
|
|
|
if (parent && GST_IS_GHOST_PAD (parent)) {
|
|
|
|
pad = GST_PAD_CAST (parent);
|
|
|
|
parent = GST_OBJECT_PARENT (pad);
|
|
|
|
}
|
|
|
|
return GST_ELEMENT_CAST (parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hooks */
|
|
|
|
|
2014-02-20 10:15:20 +00:00
|
|
|
static void
|
|
|
|
log_latency (const GstStructure * data, GstPad * sink_pad, guint64 sink_ts)
|
|
|
|
{
|
|
|
|
GstPad *src_pad;
|
|
|
|
guint64 src_ts;
|
|
|
|
gchar *src, *sink;
|
|
|
|
|
|
|
|
gst_structure_id_get (data,
|
|
|
|
latency_probe_pad, GST_TYPE_PAD, &src_pad,
|
|
|
|
latency_probe_ts, G_TYPE_UINT64, &src_ts, NULL);
|
|
|
|
|
|
|
|
src = g_strdup_printf ("%s_%s", GST_DEBUG_PAD_NAME (src_pad));
|
|
|
|
sink = g_strdup_printf ("%s_%s", GST_DEBUG_PAD_NAME (sink_pad));
|
|
|
|
|
2016-01-12 13:59:04 +00:00
|
|
|
gst_tracer_record_log (tr_latency, src, sink,
|
|
|
|
GST_CLOCK_DIFF (src_ts, sink_ts));
|
2014-02-20 10:15:20 +00:00
|
|
|
g_free (src);
|
|
|
|
g_free (sink);
|
|
|
|
}
|
|
|
|
|
2014-02-18 10:06:10 +00:00
|
|
|
static void
|
2014-09-17 06:38:02 +00:00
|
|
|
send_latency_probe (GstElement * parent, GstPad * pad, guint64 ts)
|
2014-02-18 10:06:10 +00:00
|
|
|
{
|
2014-02-18 15:15:44 +00:00
|
|
|
if (parent && (!GST_IS_BIN (parent)) &&
|
2014-02-18 10:06:10 +00:00
|
|
|
GST_OBJECT_FLAG_IS_SET (parent, GST_ELEMENT_FLAG_SOURCE)) {
|
|
|
|
GstEvent *latency_probe = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
|
|
|
|
gst_structure_new_id (latency_probe_id,
|
|
|
|
latency_probe_pad, GST_TYPE_PAD, pad,
|
|
|
|
latency_probe_ts, G_TYPE_UINT64, ts,
|
|
|
|
NULL));
|
|
|
|
gst_pad_push_event (pad, latency_probe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-09-18 06:26:19 +00:00
|
|
|
do_push_buffer_pre (GstTracer * self, guint64 ts, GstPad * pad)
|
2014-02-18 10:06:10 +00:00
|
|
|
{
|
|
|
|
GstElement *parent = get_real_pad_parent (pad);
|
|
|
|
|
2014-09-17 06:38:02 +00:00
|
|
|
send_latency_probe (parent, pad, ts);
|
2014-02-18 10:06:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-09-18 06:26:19 +00:00
|
|
|
do_pull_range_pre (GstTracer * self, guint64 ts, GstPad * pad)
|
2014-02-18 10:06:10 +00:00
|
|
|
{
|
|
|
|
GstPad *peer_pad = GST_PAD_PEER (pad);
|
|
|
|
GstElement *parent = get_real_pad_parent (peer_pad);
|
|
|
|
|
2014-09-17 06:38:02 +00:00
|
|
|
send_latency_probe (parent, peer_pad, ts);
|
2014-02-18 10:06:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-09-17 06:38:02 +00:00
|
|
|
calculate_latency (GstElement * parent, GstPad * pad, guint64 ts)
|
2014-02-20 10:15:20 +00:00
|
|
|
{
|
|
|
|
if (parent && (!GST_IS_BIN (parent)) &&
|
|
|
|
GST_OBJECT_FLAG_IS_SET (parent, GST_ELEMENT_FLAG_SINK)) {
|
|
|
|
GstEvent *ev = g_object_get_qdata ((GObject *) pad, latency_probe_id);
|
|
|
|
|
|
|
|
log_latency (gst_event_get_structure (ev), pad, ts);
|
|
|
|
gst_event_unref (ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-09-18 06:26:19 +00:00
|
|
|
do_push_buffer_post (GstTracer * self, guint64 ts, GstPad * pad)
|
2014-02-20 10:15:20 +00:00
|
|
|
{
|
|
|
|
GstPad *peer_pad = GST_PAD_PEER (pad);
|
|
|
|
GstElement *parent = get_real_pad_parent (peer_pad);
|
|
|
|
|
2014-09-17 06:38:02 +00:00
|
|
|
calculate_latency (parent, peer_pad, ts);
|
2014-02-20 10:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-09-18 06:26:19 +00:00
|
|
|
do_pull_range_post (GstTracer * self, guint64 ts, GstPad * pad)
|
2014-02-18 10:06:10 +00:00
|
|
|
{
|
2014-02-20 10:15:20 +00:00
|
|
|
GstElement *parent = get_real_pad_parent (pad);
|
|
|
|
|
2014-09-17 06:38:02 +00:00
|
|
|
calculate_latency (parent, pad, ts);
|
2014-02-20 10:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-09-18 06:26:19 +00:00
|
|
|
do_push_event_pre (GstTracer * self, guint64 ts, GstPad * pad, GstEvent * ev)
|
2014-02-20 10:15:20 +00:00
|
|
|
{
|
2014-02-18 10:06:10 +00:00
|
|
|
GstPad *peer_pad = GST_PAD_PEER (pad);
|
|
|
|
GstElement *parent = get_real_pad_parent (peer_pad);
|
|
|
|
|
2014-02-18 15:15:44 +00:00
|
|
|
if (parent && (!GST_IS_BIN (parent)) &&
|
2014-02-18 10:06:10 +00:00
|
|
|
GST_OBJECT_FLAG_IS_SET (parent, GST_ELEMENT_FLAG_SINK)) {
|
|
|
|
if (GST_EVENT_TYPE (ev) == GST_EVENT_CUSTOM_DOWNSTREAM) {
|
|
|
|
const GstStructure *data = gst_event_get_structure (ev);
|
|
|
|
|
|
|
|
if (gst_structure_get_name_id (data) == latency_probe_id) {
|
2014-02-20 10:15:20 +00:00
|
|
|
/* store event and calculate latency when the buffer that follows
|
|
|
|
* has been processed */
|
|
|
|
g_object_set_qdata ((GObject *) peer_pad, latency_probe_id,
|
|
|
|
gst_event_ref (ev));
|
2014-02-18 10:06:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 11:15:17 +00:00
|
|
|
/* tracer class */
|
|
|
|
|
2014-02-18 10:06:10 +00:00
|
|
|
static void
|
2014-09-15 11:15:17 +00:00
|
|
|
gst_latency_tracer_class_init (GstLatencyTracerClass * klass)
|
2014-02-18 10:06:10 +00:00
|
|
|
{
|
2014-09-15 11:15:17 +00:00
|
|
|
latency_probe_id = g_quark_from_static_string ("latency_probe.id");
|
|
|
|
latency_probe_pad = g_quark_from_static_string ("latency_probe.pad");
|
|
|
|
latency_probe_ts = g_quark_from_static_string ("latency_probe.ts");
|
|
|
|
|
|
|
|
/* announce trace formats */
|
|
|
|
/* *INDENT-OFF* */
|
2016-01-21 07:12:01 +00:00
|
|
|
tr_latency = gst_tracer_record_new ("latency.class",
|
2014-09-15 11:15:17 +00:00
|
|
|
"src", GST_TYPE_STRUCTURE, gst_structure_new ("scope",
|
2016-01-12 13:59:04 +00:00
|
|
|
"type", G_TYPE_GTYPE, G_TYPE_STRING,
|
2016-01-16 12:24:16 +00:00
|
|
|
"related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PAD,
|
2014-09-15 11:15:17 +00:00
|
|
|
NULL),
|
|
|
|
"sink", GST_TYPE_STRUCTURE, gst_structure_new ("scope",
|
2016-01-12 13:59:04 +00:00
|
|
|
"type", G_TYPE_GTYPE, G_TYPE_STRING,
|
2016-01-16 12:24:16 +00:00
|
|
|
"related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PAD,
|
2014-09-15 11:15:17 +00:00
|
|
|
NULL),
|
|
|
|
"time", GST_TYPE_STRUCTURE, gst_structure_new ("value",
|
|
|
|
"type", G_TYPE_GTYPE, G_TYPE_UINT64,
|
|
|
|
"description", G_TYPE_STRING,
|
|
|
|
"time it took for the buffer to go from src to sink ns",
|
2016-01-16 17:52:32 +00:00
|
|
|
"flags", GST_TYPE_TRACER_VALUE_FLAGS, GST_TRACER_VALUE_FLAGS_AGGREGATED,
|
2014-09-15 11:15:17 +00:00
|
|
|
"min", G_TYPE_UINT64, G_GUINT64_CONSTANT (0),
|
|
|
|
"max", G_TYPE_UINT64, G_MAXUINT64,
|
|
|
|
NULL),
|
2016-01-21 07:12:01 +00:00
|
|
|
NULL);
|
2014-09-15 11:15:17 +00:00
|
|
|
/* *INDENT-ON* */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_latency_tracer_init (GstLatencyTracer * self)
|
|
|
|
{
|
|
|
|
GstTracer *tracer = GST_TRACER (self);
|
2014-09-22 07:55:56 +00:00
|
|
|
gst_tracing_register_hook (tracer, "pad-push-pre",
|
2014-09-18 06:26:19 +00:00
|
|
|
G_CALLBACK (do_push_buffer_pre));
|
2014-09-22 07:55:56 +00:00
|
|
|
gst_tracing_register_hook (tracer, "pad-push-list-pre",
|
2014-09-18 06:26:19 +00:00
|
|
|
G_CALLBACK (do_push_buffer_pre));
|
2014-09-22 07:55:56 +00:00
|
|
|
gst_tracing_register_hook (tracer, "pad-push-post",
|
2014-09-18 06:26:19 +00:00
|
|
|
G_CALLBACK (do_push_buffer_post));
|
2014-09-22 07:55:56 +00:00
|
|
|
gst_tracing_register_hook (tracer, "pad-push-list-post",
|
2014-09-18 06:26:19 +00:00
|
|
|
G_CALLBACK (do_push_buffer_post));
|
2014-09-22 07:55:56 +00:00
|
|
|
gst_tracing_register_hook (tracer, "pad-pull-range-pre",
|
2014-09-18 06:26:19 +00:00
|
|
|
G_CALLBACK (do_pull_range_pre));
|
2014-09-22 07:55:56 +00:00
|
|
|
gst_tracing_register_hook (tracer, "pad-pull-range-post",
|
2014-09-18 06:26:19 +00:00
|
|
|
G_CALLBACK (do_pull_range_post));
|
2014-09-22 07:55:56 +00:00
|
|
|
gst_tracing_register_hook (tracer, "pad-push-event-pre",
|
2014-09-18 06:26:19 +00:00
|
|
|
G_CALLBACK (do_push_event_pre));
|
2014-02-18 10:06:10 +00:00
|
|
|
}
|