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 "cpureport.h"
30 
31 
32 enum
33 {
34   PROP_0,
35 };
36 
37 GstStaticPadTemplate cpu_report_src_template = GST_STATIC_PAD_TEMPLATE ("src",
38     GST_PAD_SRC,
39     GST_PAD_ALWAYS,
40     GST_STATIC_CAPS_ANY);
41 
42 GstStaticPadTemplate cpu_report_sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
43     GST_PAD_SINK,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS_ANY);
46 
47 static GstFlowReturn gst_cpu_report_transform_ip (GstBaseTransform * trans,
48     GstBuffer * buf);
49 
50 static gboolean gst_cpu_report_start (GstBaseTransform * trans);
51 static gboolean gst_cpu_report_stop (GstBaseTransform * trans);
52 
53 #define gst_cpu_report_parent_class parent_class
54 G_DEFINE_TYPE (GstCpuReport, gst_cpu_report, GST_TYPE_BASE_TRANSFORM);
55 
56 static void
gst_cpu_report_finalize(GObject * obj)57 gst_cpu_report_finalize (GObject * obj)
58 {
59   G_OBJECT_CLASS (parent_class)->finalize (obj);
60 }
61 
62 static void
gst_cpu_report_class_init(GstCpuReportClass * g_class)63 gst_cpu_report_class_init (GstCpuReportClass * g_class)
64 {
65   GstBaseTransformClass *gstbasetrans_class;
66   GstElementClass *element_class;
67   GObjectClass *gobject_class;
68 
69   gobject_class = G_OBJECT_CLASS (g_class);
70   element_class = GST_ELEMENT_CLASS (g_class);
71   gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (g_class);
72 
73   gobject_class->finalize = gst_cpu_report_finalize;
74 
75   gst_element_class_add_static_pad_template (element_class,
76       &cpu_report_sink_template);
77   gst_element_class_add_static_pad_template (element_class,
78       &cpu_report_src_template);
79 
80   gst_element_class_set_static_metadata (element_class, "CPU report",
81       "Testing",
82       "Post cpu usage information every buffer",
83       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
84 
85   gstbasetrans_class->transform_ip =
86       GST_DEBUG_FUNCPTR (gst_cpu_report_transform_ip);
87   gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_cpu_report_start);
88   gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_cpu_report_stop);
89 }
90 
91 static void
gst_cpu_report_init(GstCpuReport * report)92 gst_cpu_report_init (GstCpuReport * report)
93 {
94   gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (report), TRUE);
95 
96 }
97 
98 static GstFlowReturn
gst_cpu_report_transform_ip(GstBaseTransform * trans,GstBuffer * buf)99 gst_cpu_report_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
100 {
101   GstCpuReport *filter;
102   GTimeVal cur_time;
103   clock_t cur_cpu_time;
104   GstMessage *msg;
105   GstStructure *s;
106   gint64 time_taken;
107 
108 
109   g_get_current_time (&cur_time);
110   cur_cpu_time = clock ();
111 
112   filter = GST_CPU_REPORT (trans);
113 
114 
115   time_taken = GST_TIMEVAL_TO_TIME (cur_time) -
116       GST_TIMEVAL_TO_TIME (filter->last_time);
117 
118   s = gst_structure_new ("cpu-report", "cpu-time", G_TYPE_DOUBLE,
119       ((gdouble) (cur_cpu_time - filter->last_cpu_time)),
120       "actual-time", G_TYPE_INT64, time_taken, "buffer-time", G_TYPE_INT64,
121       GST_BUFFER_TIMESTAMP (buf), NULL);
122   msg = gst_message_new_element (GST_OBJECT_CAST (filter), s);
123   gst_element_post_message (GST_ELEMENT_CAST (filter), msg);
124   filter->last_time = cur_time;
125   filter->last_cpu_time = cur_cpu_time;
126 
127 
128   return GST_FLOW_OK;
129 }
130 
131 static gboolean
gst_cpu_report_start(GstBaseTransform * trans)132 gst_cpu_report_start (GstBaseTransform * trans)
133 {
134   GstCpuReport *filter;
135 
136   filter = GST_CPU_REPORT (trans);
137 
138   g_get_current_time (&filter->last_time);
139   filter->start_time = filter->last_time;
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