1 /* GStreamer Cpu Report Element
2 * Copyright (C) <2010> Zaheer Abbas Merali <zaheerabbas merali org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <gst/gst.h>
25 #include <string.h>
26 #include <math.h>
27 #include <time.h>
28
29 #include "gstdebugutilselements.h"
30 #include "cpureport.h"
31
32
33 enum
34 {
35 PROP_0,
36 };
37
38 GstStaticPadTemplate cpu_report_src_template = GST_STATIC_PAD_TEMPLATE ("src",
39 GST_PAD_SRC,
40 GST_PAD_ALWAYS,
41 GST_STATIC_CAPS_ANY);
42
43 GstStaticPadTemplate cpu_report_sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_PAD_SINK,
45 GST_PAD_ALWAYS,
46 GST_STATIC_CAPS_ANY);
47
48 static GstFlowReturn gst_cpu_report_transform_ip (GstBaseTransform * trans,
49 GstBuffer * buf);
50
51 static gboolean gst_cpu_report_start (GstBaseTransform * trans);
52 static gboolean gst_cpu_report_stop (GstBaseTransform * trans);
53
54 #define gst_cpu_report_parent_class parent_class
55 G_DEFINE_TYPE (GstCpuReport, gst_cpu_report, GST_TYPE_BASE_TRANSFORM);
56 GST_ELEMENT_REGISTER_DEFINE (cpureport, "cpureport",
57 GST_RANK_NONE, gst_cpu_report_get_type ());
58
59 static void
gst_cpu_report_finalize(GObject * obj)60 gst_cpu_report_finalize (GObject * obj)
61 {
62 G_OBJECT_CLASS (parent_class)->finalize (obj);
63 }
64
65 static void
gst_cpu_report_class_init(GstCpuReportClass * g_class)66 gst_cpu_report_class_init (GstCpuReportClass * g_class)
67 {
68 GstBaseTransformClass *gstbasetrans_class;
69 GstElementClass *element_class;
70 GObjectClass *gobject_class;
71
72 gobject_class = G_OBJECT_CLASS (g_class);
73 element_class = GST_ELEMENT_CLASS (g_class);
74 gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (g_class);
75
76 gobject_class->finalize = gst_cpu_report_finalize;
77
78 gst_element_class_add_static_pad_template (element_class,
79 &cpu_report_sink_template);
80 gst_element_class_add_static_pad_template (element_class,
81 &cpu_report_src_template);
82
83 gst_element_class_set_static_metadata (element_class, "CPU report",
84 "Testing",
85 "Post cpu usage information every buffer",
86 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
87
88 gstbasetrans_class->transform_ip =
89 GST_DEBUG_FUNCPTR (gst_cpu_report_transform_ip);
90 gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_cpu_report_start);
91 gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_cpu_report_stop);
92 }
93
94 static void
gst_cpu_report_init(GstCpuReport * report)95 gst_cpu_report_init (GstCpuReport * report)
96 {
97 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (report), TRUE);
98
99 }
100
101 static GstFlowReturn
gst_cpu_report_transform_ip(GstBaseTransform * trans,GstBuffer * buf)102 gst_cpu_report_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
103 {
104 GstCpuReport *filter;
105 GstClockTime cur_time;
106 clock_t cur_cpu_time;
107 GstMessage *msg;
108 GstStructure *s;
109 GstClockTimeDiff time_taken;
110
111 cur_time = g_get_real_time () * GST_USECOND;
112 cur_cpu_time = clock ();
113
114 filter = GST_CPU_REPORT (trans);
115
116
117 time_taken = cur_time - filter->last_time;
118
119 s = gst_structure_new ("cpu-report", "cpu-time", G_TYPE_DOUBLE,
120 ((gdouble) (cur_cpu_time - filter->last_cpu_time)),
121 "actual-time", G_TYPE_INT64, time_taken, "buffer-time", G_TYPE_INT64,
122 GST_BUFFER_TIMESTAMP (buf), NULL);
123 msg = gst_message_new_element (GST_OBJECT_CAST (filter), s);
124 gst_element_post_message (GST_ELEMENT_CAST (filter), msg);
125 filter->last_time = cur_time;
126 filter->last_cpu_time = cur_cpu_time;
127
128
129 return GST_FLOW_OK;
130 }
131
132 static gboolean
gst_cpu_report_start(GstBaseTransform * trans)133 gst_cpu_report_start (GstBaseTransform * trans)
134 {
135 GstCpuReport *filter;
136
137 filter = GST_CPU_REPORT (trans);
138
139 filter->start_time = filter->last_time = g_get_real_time () * GST_USECOND;
140 filter->last_cpu_time = clock ();
141 return TRUE;
142 }
143
144 static gboolean
gst_cpu_report_stop(GstBaseTransform * trans)145 gst_cpu_report_stop (GstBaseTransform * trans)
146 {
147 /* anything we should be doing here? */
148 return TRUE;
149 }
150