tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.git] / gst / debugutils / cpureport.c
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, 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   ARG_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 GST_BOILERPLATE (GstCpuReport, gst_cpu_report, GstBaseTransform,
54     GST_TYPE_BASE_TRANSFORM);
55
56 static void
57 gst_cpu_report_base_init (gpointer g_class)
58 {
59   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
60
61   gst_element_class_add_static_pad_template (element_class,
62       &cpu_report_sink_template);
63   gst_element_class_add_static_pad_template (element_class,
64       &cpu_report_src_template);
65
66   gst_element_class_set_details_simple (element_class, "CPU report",
67       "Testing",
68       "Post cpu usage information every buffer",
69       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
70 }
71
72 static void
73 gst_cpu_report_finalize (GObject * obj)
74 {
75   G_OBJECT_CLASS (parent_class)->finalize (obj);
76 }
77
78 static void
79 gst_cpu_report_class_init (GstCpuReportClass * g_class)
80 {
81   GstBaseTransformClass *gstbasetrans_class;
82   GObjectClass *gobject_class;
83
84   gobject_class = G_OBJECT_CLASS (g_class);
85   gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (g_class);
86
87   gobject_class->finalize = gst_cpu_report_finalize;
88
89   gstbasetrans_class->transform_ip =
90       GST_DEBUG_FUNCPTR (gst_cpu_report_transform_ip);
91   gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_cpu_report_start);
92   gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_cpu_report_stop);
93 }
94
95 static void
96 gst_cpu_report_init (GstCpuReport * report, GstCpuReportClass * g_class)
97 {
98   gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (report), TRUE);
99
100 }
101
102 static GstFlowReturn
103 gst_cpu_report_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
104 {
105   GstCpuReport *filter;
106   GTimeVal cur_time;
107   clock_t cur_cpu_time;
108   GstMessage *msg;
109   GstStructure *s;
110   gint64 time_taken;
111
112
113   g_get_current_time (&cur_time);
114   cur_cpu_time = clock ();
115
116   filter = GST_CPU_REPORT (trans);
117
118
119   time_taken = GST_TIMEVAL_TO_TIME (cur_time) -
120       GST_TIMEVAL_TO_TIME (filter->last_time);
121
122   s = gst_structure_new ("cpu-report", "cpu-time", G_TYPE_DOUBLE,
123       ((gdouble) (cur_cpu_time - filter->last_cpu_time)),
124       "actual-time", G_TYPE_INT64, time_taken, "buffer-time", G_TYPE_INT64,
125       GST_BUFFER_TIMESTAMP (buf), NULL);
126   msg = gst_message_new_element (GST_OBJECT_CAST (filter), s);
127   gst_element_post_message (GST_ELEMENT_CAST (filter), msg);
128   filter->last_time = cur_time;
129   filter->last_cpu_time = cur_cpu_time;
130
131
132   return GST_FLOW_OK;
133 }
134
135 static gboolean
136 gst_cpu_report_start (GstBaseTransform * trans)
137 {
138   GstCpuReport *filter;
139
140   filter = GST_CPU_REPORT (trans);
141
142   g_get_current_time (&filter->last_time);
143   filter->start_time = filter->last_time;
144   filter->last_cpu_time = clock ();
145   return TRUE;
146 }
147
148 static gboolean
149 gst_cpu_report_stop (GstBaseTransform * trans)
150 {
151   /* anything we should be doing here? */
152   return TRUE;
153 }