Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 Nokia Corporation <multimedia@maemo.org> |
| 3 | * 2006 Zeeshan Ali <zeeshan.ali@nokia.com>. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU Lesser General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Sebastian Dröge | e6513c1 | 2013-07-14 12:12:42 +0200 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /** |
| 21 | * SECTION:element-fpsdisplaysink |
| 22 | * |
| 23 | * Can display the current and average framerate as a testoverlay or on stdout. |
| 24 | * |
| 25 | * <refsect2> |
| 26 | * <title>Example launch lines</title> |
| 27 | * |[ |
| 28 | * gst-launch videotestsrc ! fpsdisplaysink |
| 29 | * gst-launch videotestsrc ! fpsdisplaysink text-overlay=false |
Sebastian Dröge | 441328f | 2015-05-13 15:47:04 +0300 | [diff] [blame] | 30 | * gst-launch filesrc location=video.avi ! decodebin name=d ! queue ! fpsdisplaysink d. ! queue ! fakesink sync=true |
Sebastian Dröge | 6a5ec01 | 2012-10-25 14:10:09 +0200 | [diff] [blame] | 31 | * gst-launch playbin uri=file:///path/to/video.avi video-sink="fpsdisplaysink" audio-sink=fakesink |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 32 | * ]| |
| 33 | * </refsect2> |
| 34 | */ |
| 35 | /* FIXME: |
| 36 | * - can we avoid plugging the textoverlay? |
| 37 | * - gst-seek 15 "videotestsrc ! fpsdisplaysink" dies when closing gst-seek |
| 38 | * |
| 39 | * NOTE: |
| 40 | * - if we make ourself RANK_PRIMARY+10 or something that autovideosink would |
| 41 | * select and fpsdisplaysink is set to use autovideosink as its internal sink |
| 42 | * it doesn't work. Reason: autovideosink creates a fpsdisplaysink, that |
| 43 | * creates an autovideosink, that... |
| 44 | */ |
| 45 | |
| 46 | #ifdef HAVE_CONFIG_H |
| 47 | #include "config.h" |
| 48 | #endif |
| 49 | |
| 50 | #include "debugutils-marshal.h" |
| 51 | #include "fpsdisplaysink.h" |
| 52 | |
| 53 | #define DEFAULT_SIGNAL_FPS_MEASUREMENTS FALSE |
| 54 | #define DEFAULT_FPS_UPDATE_INTERVAL_MS 500 /* 500 ms */ |
| 55 | #define DEFAULT_FONT "Sans 15" |
| 56 | #define DEFAULT_SILENT FALSE |
| 57 | #define DEFAULT_LAST_MESSAGE NULL |
| 58 | |
| 59 | /* generic templates */ |
| 60 | static GstStaticPadTemplate fps_display_sink_template = |
| 61 | GST_STATIC_PAD_TEMPLATE ("sink", |
| 62 | GST_PAD_SINK, |
| 63 | GST_PAD_ALWAYS, |
| 64 | GST_STATIC_CAPS_ANY); |
| 65 | |
| 66 | GST_DEBUG_CATEGORY_STATIC (fps_display_sink_debug); |
| 67 | #define GST_CAT_DEFAULT fps_display_sink_debug |
| 68 | |
| 69 | #define DEFAULT_SYNC TRUE |
| 70 | |
| 71 | enum |
| 72 | { |
| 73 | /* FILL ME */ |
| 74 | SIGNAL_FPS_MEASUREMENTS, |
| 75 | LAST_SIGNAL |
| 76 | }; |
| 77 | |
| 78 | enum |
| 79 | { |
| 80 | PROP_0, |
| 81 | PROP_SYNC, |
| 82 | PROP_TEXT_OVERLAY, |
| 83 | PROP_VIDEO_SINK, |
| 84 | PROP_FPS_UPDATE_INTERVAL, |
| 85 | PROP_MAX_FPS, |
| 86 | PROP_MIN_FPS, |
| 87 | PROP_SIGNAL_FPS_MEASUREMENTS, |
| 88 | PROP_FRAMES_DROPPED, |
| 89 | PROP_FRAMES_RENDERED, |
| 90 | PROP_SILENT, |
| 91 | PROP_LAST_MESSAGE |
| 92 | /* FILL ME */ |
| 93 | }; |
| 94 | |
| 95 | static GstBinClass *parent_class = NULL; |
| 96 | |
| 97 | static GstStateChangeReturn fps_display_sink_change_state (GstElement * element, |
| 98 | GstStateChange transition); |
| 99 | static void fps_display_sink_set_property (GObject * object, guint prop_id, |
| 100 | const GValue * value, GParamSpec * pspec); |
| 101 | static void fps_display_sink_get_property (GObject * object, guint prop_id, |
| 102 | GValue * value, GParamSpec * pspec); |
| 103 | static void fps_display_sink_dispose (GObject * object); |
Sebastian Dröge | d1702b9 | 2015-06-24 23:26:45 +0200 | [diff] [blame] | 104 | static void fps_display_sink_handle_message (GstBin * bin, |
| 105 | GstMessage * message); |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 106 | |
| 107 | static gboolean display_current_fps (gpointer data); |
| 108 | |
| 109 | static guint fpsdisplaysink_signals[LAST_SIGNAL] = { 0 }; |
| 110 | |
| 111 | static GParamSpec *pspec_last_message = NULL; |
| 112 | |
| 113 | static void |
| 114 | fps_display_sink_class_init (GstFPSDisplaySinkClass * klass) |
| 115 | { |
| 116 | GObjectClass *gobject_klass = G_OBJECT_CLASS (klass); |
| 117 | GstElementClass *gstelement_klass = GST_ELEMENT_CLASS (klass); |
Sebastian Dröge | d1702b9 | 2015-06-24 23:26:45 +0200 | [diff] [blame] | 118 | GstBinClass *bin_class = GST_BIN_CLASS (klass); |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 119 | |
| 120 | parent_class = g_type_class_peek_parent (klass); |
| 121 | |
| 122 | gobject_klass->set_property = fps_display_sink_set_property; |
| 123 | gobject_klass->get_property = fps_display_sink_get_property; |
| 124 | gobject_klass->dispose = fps_display_sink_dispose; |
| 125 | |
Sebastian Dröge | d1702b9 | 2015-06-24 23:26:45 +0200 | [diff] [blame] | 126 | bin_class->handle_message = fps_display_sink_handle_message; |
| 127 | |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 128 | g_object_class_install_property (gobject_klass, PROP_SYNC, |
| 129 | g_param_spec_boolean ("sync", |
| 130 | "Sync", "Sync on the clock (if the internally used sink doesn't " |
| 131 | "have this property it will be ignored", DEFAULT_SYNC, |
| 132 | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE)); |
| 133 | |
| 134 | g_object_class_install_property (gobject_klass, PROP_TEXT_OVERLAY, |
| 135 | g_param_spec_boolean ("text-overlay", |
| 136 | "text-overlay", |
| 137 | "Whether to use text-overlay", TRUE, |
| 138 | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE)); |
| 139 | |
| 140 | g_object_class_install_property (gobject_klass, PROP_VIDEO_SINK, |
| 141 | g_param_spec_object ("video-sink", |
| 142 | "video-sink", |
| 143 | "Video sink to use (Must only be called on NULL state)", |
| 144 | GST_TYPE_ELEMENT, G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE)); |
| 145 | |
| 146 | g_object_class_install_property (gobject_klass, PROP_FPS_UPDATE_INTERVAL, |
| 147 | g_param_spec_int ("fps-update-interval", "Fps update interval", |
| 148 | "Time between consecutive frames per second measures and update " |
| 149 | " (in ms). Should be set on NULL state", 1, G_MAXINT, |
| 150 | DEFAULT_FPS_UPDATE_INTERVAL_MS, |
| 151 | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE)); |
| 152 | |
| 153 | g_object_class_install_property (gobject_klass, PROP_MAX_FPS, |
| 154 | g_param_spec_double ("max-fps", "Max fps", |
| 155 | "Maximum fps rate measured. Reset when going from NULL to READY." |
| 156 | "-1 means no measurement has yet been done", -1, G_MAXDOUBLE, -1, |
| 157 | G_PARAM_STATIC_STRINGS | G_PARAM_READABLE)); |
| 158 | |
| 159 | g_object_class_install_property (gobject_klass, PROP_MIN_FPS, |
| 160 | g_param_spec_double ("min-fps", "Min fps", |
| 161 | "Minimum fps rate measured. Reset when going from NULL to READY." |
| 162 | "-1 means no measurement has yet been done", -1, G_MAXDOUBLE, -1, |
| 163 | G_PARAM_STATIC_STRINGS | G_PARAM_READABLE)); |
| 164 | |
| 165 | g_object_class_install_property (gobject_klass, PROP_FRAMES_DROPPED, |
| 166 | g_param_spec_uint ("frames-dropped", "dropped frames", |
| 167 | "Number of frames dropped by the sink", 0, G_MAXUINT, 0, |
| 168 | G_PARAM_STATIC_STRINGS | G_PARAM_READABLE)); |
| 169 | |
| 170 | g_object_class_install_property (gobject_klass, PROP_FRAMES_RENDERED, |
| 171 | g_param_spec_uint ("frames-rendered", "rendered frames", |
| 172 | "Number of frames rendered", 0, G_MAXUINT, 0, |
| 173 | G_PARAM_STATIC_STRINGS | G_PARAM_READABLE)); |
| 174 | |
| 175 | g_object_class_install_property (gobject_klass, PROP_SILENT, |
| 176 | g_param_spec_boolean ("silent", "enable stdout output", |
| 177 | "Don't produce last_message events", DEFAULT_SILENT, |
| 178 | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE)); |
| 179 | |
| 180 | g_object_class_install_property (gobject_klass, PROP_SIGNAL_FPS_MEASUREMENTS, |
| 181 | g_param_spec_boolean ("signal-fps-measurements", |
| 182 | "Signal fps measurements", |
| 183 | "If the fps-measurements signal should be emited.", |
| 184 | DEFAULT_SIGNAL_FPS_MEASUREMENTS, |
| 185 | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE)); |
| 186 | |
| 187 | pspec_last_message = g_param_spec_string ("last-message", "Last Message", |
| 188 | "The message describing current status", DEFAULT_LAST_MESSAGE, |
| 189 | G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); |
| 190 | g_object_class_install_property (gobject_klass, PROP_LAST_MESSAGE, |
| 191 | pspec_last_message); |
| 192 | |
| 193 | /** |
| 194 | * GstFPSDisplaySink::fps-measurements: |
| 195 | * @fpsdisplaysink: a #GstFPSDisplaySink |
| 196 | * @fps: The current measured fps |
| 197 | * @droprate: The rate at which buffers are being dropped |
| 198 | * @avgfps: The average fps |
| 199 | * |
| 200 | * Signals the application about the measured fps |
| 201 | * |
| 202 | * Since: 0.10.20 |
| 203 | */ |
| 204 | fpsdisplaysink_signals[SIGNAL_FPS_MEASUREMENTS] = |
| 205 | g_signal_new ("fps-measurements", G_TYPE_FROM_CLASS (klass), |
| 206 | G_SIGNAL_RUN_LAST, 0, NULL, NULL, |
| 207 | __gst_debugutils_marshal_VOID__DOUBLE_DOUBLE_DOUBLE, |
| 208 | G_TYPE_NONE, 3, G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_DOUBLE); |
| 209 | |
| 210 | gstelement_klass->change_state = fps_display_sink_change_state; |
| 211 | |
| 212 | gst_element_class_add_pad_template (gstelement_klass, |
| 213 | gst_static_pad_template_get (&fps_display_sink_template)); |
| 214 | |
Sebastian Dröge | 6a5ec01 | 2012-10-25 14:10:09 +0200 | [diff] [blame] | 215 | gst_element_class_set_static_metadata (gstelement_klass, |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 216 | "Measure and show framerate on videosink", "Sink/Video", |
| 217 | "Shows the current frame-rate and drop-rate of the videosink as overlay or text on stdout", |
| 218 | "Zeeshan Ali <zeeshan.ali@nokia.com>, Stefan Kost <stefan.kost@nokia.com>"); |
| 219 | } |
| 220 | |
| 221 | static GstPadProbeReturn |
| 222 | on_video_sink_data_flow (GstPad * pad, GstPadProbeInfo * info, |
| 223 | gpointer user_data) |
| 224 | { |
| 225 | GstMiniObject *mini_obj = GST_PAD_PROBE_INFO_DATA (info); |
| 226 | GstFPSDisplaySink *self = GST_FPS_DISPLAY_SINK (user_data); |
| 227 | |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 228 | if (GST_IS_BUFFER (mini_obj)) { |
Sebastian Dröge | d1702b9 | 2015-06-24 23:26:45 +0200 | [diff] [blame] | 229 | GstClockTime ts; |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 230 | |
Sebastian Dröge | d1702b9 | 2015-06-24 23:26:45 +0200 | [diff] [blame] | 231 | /* assume the frame is going to be rendered. If it isnt', we'll get a qos |
| 232 | * message and reset ->frames_rendered from there. |
| 233 | */ |
| 234 | g_atomic_int_inc (&self->frames_rendered); |
| 235 | |
| 236 | ts = gst_util_get_timestamp (); |
| 237 | if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (self->start_ts))) { |
| 238 | self->interval_ts = self->last_ts = self->start_ts = ts; |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 239 | } |
Sebastian Dröge | d1702b9 | 2015-06-24 23:26:45 +0200 | [diff] [blame] | 240 | if (GST_CLOCK_DIFF (self->interval_ts, ts) > self->fps_update_interval) { |
| 241 | display_current_fps (self); |
| 242 | self->interval_ts = ts; |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 243 | } |
| 244 | } |
Sebastian Dröge | d1702b9 | 2015-06-24 23:26:45 +0200 | [diff] [blame] | 245 | |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 246 | return GST_PAD_PROBE_OK; |
| 247 | } |
| 248 | |
| 249 | static void |
| 250 | update_sub_sync (GstElement * sink, gpointer data) |
| 251 | { |
| 252 | /* Some sinks (like autovideosink) don't have the sync property so |
| 253 | * we check it exists before setting it to avoid a warning at |
| 254 | * runtime. */ |
| 255 | if (g_object_class_find_property (G_OBJECT_GET_CLASS (sink), "sync")) |
| 256 | g_object_set (sink, "sync", *((gboolean *) data), NULL); |
| 257 | else |
| 258 | GST_WARNING ("Internal sink doesn't have sync property"); |
| 259 | } |
| 260 | |
| 261 | static void |
| 262 | update_sub_sync_foreach (const GValue * item, gpointer data) |
| 263 | { |
| 264 | GstElement *sink = g_value_get_object (item); |
| 265 | |
| 266 | update_sub_sync (sink, data); |
| 267 | } |
| 268 | |
| 269 | static void |
| 270 | fps_display_sink_update_sink_sync (GstFPSDisplaySink * self) |
| 271 | { |
| 272 | GstIterator *iterator; |
| 273 | |
| 274 | if (self->video_sink == NULL) |
| 275 | return; |
| 276 | |
| 277 | if (GST_IS_BIN (self->video_sink)) { |
| 278 | iterator = gst_bin_iterate_sinks (GST_BIN (self->video_sink)); |
| 279 | gst_iterator_foreach (iterator, |
| 280 | (GstIteratorForeachFunction) update_sub_sync_foreach, |
| 281 | (void *) &self->sync); |
| 282 | gst_iterator_free (iterator); |
| 283 | } else |
| 284 | update_sub_sync (self->video_sink, (void *) &self->sync); |
| 285 | |
| 286 | } |
| 287 | |
| 288 | static void |
| 289 | update_video_sink (GstFPSDisplaySink * self, GstElement * video_sink) |
| 290 | { |
| 291 | GstPad *sink_pad; |
| 292 | |
| 293 | if (self->video_sink) { |
| 294 | |
| 295 | /* remove pad probe */ |
| 296 | sink_pad = gst_element_get_static_pad (self->video_sink, "sink"); |
| 297 | gst_pad_remove_probe (sink_pad, self->data_probe_id); |
| 298 | gst_object_unref (sink_pad); |
| 299 | self->data_probe_id = -1; |
| 300 | |
| 301 | /* remove ghost pad target */ |
| 302 | gst_ghost_pad_set_target (GST_GHOST_PAD (self->ghost_pad), NULL); |
| 303 | |
| 304 | /* remove old sink */ |
| 305 | gst_bin_remove (GST_BIN (self), self->video_sink); |
| 306 | gst_object_unref (self->video_sink); |
| 307 | } |
| 308 | |
| 309 | /* create child elements */ |
| 310 | self->video_sink = video_sink; |
| 311 | |
| 312 | if (self->video_sink == NULL) |
| 313 | return; |
| 314 | |
| 315 | fps_display_sink_update_sink_sync (self); |
| 316 | |
| 317 | /* take a ref before bin takes the ownership */ |
| 318 | gst_object_ref (self->video_sink); |
| 319 | |
| 320 | gst_bin_add (GST_BIN (self), self->video_sink); |
| 321 | |
| 322 | /* attach or pad probe */ |
| 323 | sink_pad = gst_element_get_static_pad (self->video_sink, "sink"); |
| 324 | self->data_probe_id = gst_pad_add_probe (sink_pad, |
| 325 | GST_PAD_PROBE_TYPE_DATA_BOTH, on_video_sink_data_flow, |
| 326 | (gpointer) self, NULL); |
| 327 | gst_object_unref (sink_pad); |
| 328 | } |
| 329 | |
| 330 | static void |
| 331 | fps_display_sink_init (GstFPSDisplaySink * self, |
| 332 | GstFPSDisplaySinkClass * g_class) |
| 333 | { |
| 334 | self->sync = DEFAULT_SYNC; |
| 335 | self->signal_measurements = DEFAULT_SIGNAL_FPS_MEASUREMENTS; |
| 336 | self->use_text_overlay = TRUE; |
| 337 | self->fps_update_interval = GST_MSECOND * DEFAULT_FPS_UPDATE_INTERVAL_MS; |
| 338 | self->video_sink = NULL; |
| 339 | self->max_fps = -1; |
| 340 | self->min_fps = -1; |
| 341 | self->silent = DEFAULT_SILENT; |
| 342 | self->last_message = g_strdup (DEFAULT_LAST_MESSAGE); |
| 343 | |
| 344 | self->ghost_pad = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK); |
| 345 | gst_element_add_pad (GST_ELEMENT (self), self->ghost_pad); |
| 346 | } |
| 347 | |
| 348 | static gboolean |
| 349 | display_current_fps (gpointer data) |
| 350 | { |
| 351 | GstFPSDisplaySink *self = GST_FPS_DISPLAY_SINK (data); |
| 352 | guint64 frames_rendered, frames_dropped; |
| 353 | gdouble rr, dr, average_fps; |
| 354 | gchar fps_message[256]; |
| 355 | gdouble time_diff, time_elapsed; |
| 356 | GstClockTime current_ts = gst_util_get_timestamp (); |
| 357 | |
| 358 | frames_rendered = g_atomic_int_get (&self->frames_rendered); |
| 359 | frames_dropped = g_atomic_int_get (&self->frames_dropped); |
| 360 | |
| 361 | if ((frames_rendered + frames_dropped) == 0) { |
| 362 | /* in case timer fired and we didn't yet get any QOS events */ |
| 363 | return TRUE; |
| 364 | } |
| 365 | |
| 366 | time_diff = (gdouble) (current_ts - self->last_ts) / GST_SECOND; |
| 367 | time_elapsed = (gdouble) (current_ts - self->start_ts) / GST_SECOND; |
| 368 | |
| 369 | rr = (gdouble) (frames_rendered - self->last_frames_rendered) / time_diff; |
| 370 | dr = (gdouble) (frames_dropped - self->last_frames_dropped) / time_diff; |
| 371 | |
| 372 | average_fps = (gdouble) frames_rendered / time_elapsed; |
| 373 | |
| 374 | if (self->max_fps == -1 || rr > self->max_fps) { |
| 375 | self->max_fps = rr; |
| 376 | GST_DEBUG_OBJECT (self, "Updated max-fps to %f", rr); |
| 377 | } |
| 378 | if (self->min_fps == -1 || rr < self->min_fps) { |
| 379 | self->min_fps = rr; |
| 380 | GST_DEBUG_OBJECT (self, "Updated min-fps to %f", rr); |
| 381 | } |
| 382 | |
| 383 | if (self->signal_measurements) { |
| 384 | GST_LOG_OBJECT (self, "Signaling measurements: fps:%f droprate:%f " |
| 385 | "avg-fps:%f", rr, dr, average_fps); |
| 386 | g_signal_emit (G_OBJECT (self), |
| 387 | fpsdisplaysink_signals[SIGNAL_FPS_MEASUREMENTS], 0, rr, dr, |
| 388 | average_fps); |
| 389 | } |
| 390 | |
| 391 | /* Display on a single line to make it easier to read and import |
| 392 | * into, for example, excel.. note: it would be nice to show |
| 393 | * timestamp too.. need to check if there is a sane way to log |
| 394 | * timestamp of last rendered buffer, so we could correlate dips |
| 395 | * in framerate to certain positions in the stream. |
| 396 | */ |
| 397 | if (dr == 0.0) { |
| 398 | g_snprintf (fps_message, 255, |
| 399 | "rendered: %" G_GUINT64_FORMAT ", dropped: %" G_GUINT64_FORMAT |
| 400 | ", current: %.2f, average: %.2f", frames_rendered, frames_dropped, rr, |
| 401 | average_fps); |
| 402 | } else { |
| 403 | g_snprintf (fps_message, 255, |
| 404 | "rendered: %" G_GUINT64_FORMAT ", dropped: %" G_GUINT64_FORMAT |
| 405 | ", fps: %.2f, drop rate: %.2f", frames_rendered, frames_dropped, rr, |
| 406 | dr); |
| 407 | } |
| 408 | |
| 409 | if (self->use_text_overlay) { |
| 410 | g_object_set (self->text_overlay, "text", fps_message, NULL); |
| 411 | } |
| 412 | |
| 413 | if (!self->silent) { |
| 414 | GST_OBJECT_LOCK (self); |
| 415 | g_free (self->last_message); |
| 416 | self->last_message = g_strdup (fps_message); |
| 417 | GST_OBJECT_UNLOCK (self); |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 418 | g_object_notify_by_pspec ((GObject *) self, pspec_last_message); |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | self->last_frames_rendered = frames_rendered; |
| 422 | self->last_frames_dropped = frames_dropped; |
| 423 | self->last_ts = current_ts; |
| 424 | |
| 425 | return TRUE; |
| 426 | } |
| 427 | |
| 428 | static void |
| 429 | fps_display_sink_start (GstFPSDisplaySink * self) |
| 430 | { |
| 431 | GstPad *target_pad = NULL; |
| 432 | |
| 433 | /* Init counters */ |
| 434 | self->frames_rendered = 0; |
| 435 | self->frames_dropped = 0; |
| 436 | self->last_frames_rendered = G_GUINT64_CONSTANT (0); |
| 437 | self->last_frames_dropped = G_GUINT64_CONSTANT (0); |
| 438 | self->max_fps = -1; |
| 439 | self->min_fps = -1; |
| 440 | |
| 441 | /* init time stamps */ |
| 442 | self->last_ts = self->start_ts = self->interval_ts = GST_CLOCK_TIME_NONE; |
| 443 | |
| 444 | GST_DEBUG_OBJECT (self, "Use text-overlay? %d", self->use_text_overlay); |
| 445 | |
| 446 | if (self->use_text_overlay) { |
| 447 | if (!self->text_overlay) { |
| 448 | self->text_overlay = |
| 449 | gst_element_factory_make ("textoverlay", "fps-display-text-overlay"); |
| 450 | if (!self->text_overlay) { |
| 451 | GST_WARNING_OBJECT (self, "text-overlay element could not be created"); |
| 452 | self->use_text_overlay = FALSE; |
| 453 | goto no_text_overlay; |
| 454 | } |
| 455 | gst_object_ref (self->text_overlay); |
| 456 | g_object_set (self->text_overlay, |
| 457 | "font-desc", DEFAULT_FONT, "silent", FALSE, NULL); |
| 458 | gst_bin_add (GST_BIN (self), self->text_overlay); |
| 459 | |
| 460 | if (!gst_element_link (self->text_overlay, self->video_sink)) { |
| 461 | GST_ERROR_OBJECT (self, "Could not link elements"); |
| 462 | } |
| 463 | } |
| 464 | target_pad = gst_element_get_static_pad (self->text_overlay, "video_sink"); |
| 465 | } |
| 466 | no_text_overlay: |
| 467 | if (!self->use_text_overlay) { |
| 468 | if (self->text_overlay) { |
| 469 | gst_element_unlink (self->text_overlay, self->video_sink); |
| 470 | gst_bin_remove (GST_BIN (self), self->text_overlay); |
| 471 | self->text_overlay = NULL; |
| 472 | } |
| 473 | target_pad = gst_element_get_static_pad (self->video_sink, "sink"); |
| 474 | } |
| 475 | gst_ghost_pad_set_target (GST_GHOST_PAD (self->ghost_pad), target_pad); |
| 476 | gst_object_unref (target_pad); |
| 477 | } |
| 478 | |
| 479 | static void |
| 480 | fps_display_sink_stop (GstFPSDisplaySink * self) |
| 481 | { |
| 482 | if (self->text_overlay) { |
| 483 | gst_element_unlink (self->text_overlay, self->video_sink); |
| 484 | gst_bin_remove (GST_BIN (self), self->text_overlay); |
| 485 | gst_object_unref (self->text_overlay); |
| 486 | self->text_overlay = NULL; |
| 487 | } |
| 488 | |
| 489 | if (!self->silent) { |
| 490 | gchar *str; |
| 491 | |
| 492 | /* print the max and minimum fps values */ |
| 493 | str = |
| 494 | g_strdup_printf ("Max-fps: %0.2f, Min-fps: %0.2f", self->max_fps, |
| 495 | self->min_fps); |
| 496 | GST_OBJECT_LOCK (self); |
| 497 | g_free (self->last_message); |
| 498 | self->last_message = str; |
| 499 | GST_OBJECT_UNLOCK (self); |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 500 | g_object_notify_by_pspec ((GObject *) self, pspec_last_message); |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | GST_OBJECT_LOCK (self); |
| 504 | g_free (self->last_message); |
| 505 | self->last_message = NULL; |
| 506 | GST_OBJECT_UNLOCK (self); |
| 507 | } |
| 508 | |
| 509 | static void |
| 510 | fps_display_sink_dispose (GObject * object) |
| 511 | { |
| 512 | GstFPSDisplaySink *self = GST_FPS_DISPLAY_SINK (object); |
| 513 | |
| 514 | if (self->video_sink) { |
| 515 | gst_object_unref (self->video_sink); |
| 516 | self->video_sink = NULL; |
| 517 | } |
| 518 | |
| 519 | if (self->text_overlay) { |
| 520 | gst_object_unref (self->text_overlay); |
| 521 | self->text_overlay = NULL; |
| 522 | } |
| 523 | |
| 524 | GST_OBJECT_LOCK (self); |
| 525 | g_free (self->last_message); |
| 526 | self->last_message = NULL; |
| 527 | GST_OBJECT_UNLOCK (self); |
| 528 | |
| 529 | G_OBJECT_CLASS (parent_class)->dispose (object); |
| 530 | } |
| 531 | |
| 532 | static void |
| 533 | fps_display_sink_set_property (GObject * object, guint prop_id, |
| 534 | const GValue * value, GParamSpec * pspec) |
| 535 | { |
| 536 | GstFPSDisplaySink *self = GST_FPS_DISPLAY_SINK (object); |
| 537 | |
| 538 | switch (prop_id) { |
| 539 | case PROP_SYNC: |
| 540 | self->sync = g_value_get_boolean (value); |
| 541 | fps_display_sink_update_sink_sync (self); |
| 542 | break; |
| 543 | case PROP_TEXT_OVERLAY: |
| 544 | self->use_text_overlay = g_value_get_boolean (value); |
| 545 | |
| 546 | if (self->text_overlay) { |
| 547 | if (!self->use_text_overlay) { |
| 548 | GST_DEBUG_OBJECT (self, "text-overlay set to false"); |
| 549 | g_object_set (self->text_overlay, "text", "", "silent", TRUE, NULL); |
| 550 | } else { |
| 551 | GST_DEBUG_OBJECT (self, "text-overlay set to true"); |
| 552 | g_object_set (self->text_overlay, "silent", FALSE, NULL); |
| 553 | } |
| 554 | } |
| 555 | break; |
| 556 | case PROP_VIDEO_SINK: |
| 557 | /* FIXME should we add a state-lock or a lock around here? |
| 558 | * need to check if it is possible that a state change NULL->READY can |
| 559 | * happen while this code is executing on a different thread */ |
| 560 | if (GST_STATE (self) != GST_STATE_NULL) { |
| 561 | g_warning ("Can't set video-sink property of fpsdisplaysink if not on " |
| 562 | "NULL state"); |
| 563 | break; |
| 564 | } |
| 565 | update_video_sink (self, (GstElement *) g_value_get_object (value)); |
| 566 | break; |
| 567 | case PROP_FPS_UPDATE_INTERVAL: |
| 568 | self->fps_update_interval = |
| 569 | GST_MSECOND * (GstClockTime) g_value_get_int (value); |
| 570 | break; |
| 571 | case PROP_SIGNAL_FPS_MEASUREMENTS: |
| 572 | self->signal_measurements = g_value_get_boolean (value); |
| 573 | break; |
| 574 | case PROP_SILENT: |
| 575 | self->silent = g_value_get_boolean (value); |
| 576 | break; |
| 577 | default: |
| 578 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 579 | break; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | static void |
| 584 | fps_display_sink_get_property (GObject * object, guint prop_id, |
| 585 | GValue * value, GParamSpec * pspec) |
| 586 | { |
| 587 | GstFPSDisplaySink *self = GST_FPS_DISPLAY_SINK (object); |
| 588 | |
| 589 | switch (prop_id) { |
| 590 | case PROP_SYNC: |
| 591 | g_value_set_boolean (value, self->sync); |
| 592 | break; |
| 593 | case PROP_TEXT_OVERLAY: |
| 594 | g_value_set_boolean (value, self->use_text_overlay); |
| 595 | break; |
| 596 | case PROP_VIDEO_SINK: |
| 597 | g_value_set_object (value, self->video_sink); |
| 598 | break; |
| 599 | case PROP_FPS_UPDATE_INTERVAL: |
| 600 | g_value_set_int (value, (gint) (self->fps_update_interval / GST_MSECOND)); |
| 601 | break; |
| 602 | case PROP_MAX_FPS: |
| 603 | g_value_set_double (value, self->max_fps); |
| 604 | break; |
| 605 | case PROP_MIN_FPS: |
| 606 | g_value_set_double (value, self->min_fps); |
| 607 | break; |
| 608 | case PROP_FRAMES_DROPPED: |
| 609 | g_value_set_uint (value, g_atomic_int_get (&self->frames_dropped)); |
| 610 | break; |
| 611 | case PROP_FRAMES_RENDERED: |
| 612 | g_value_set_uint (value, g_atomic_int_get (&self->frames_rendered)); |
| 613 | break; |
| 614 | case PROP_SIGNAL_FPS_MEASUREMENTS: |
| 615 | g_value_set_boolean (value, self->signal_measurements); |
| 616 | break; |
| 617 | case PROP_SILENT: |
| 618 | g_value_set_boolean (value, self->silent); |
| 619 | break; |
| 620 | case PROP_LAST_MESSAGE: |
| 621 | GST_OBJECT_LOCK (self); |
| 622 | g_value_set_string (value, self->last_message); |
| 623 | GST_OBJECT_UNLOCK (self); |
| 624 | break; |
| 625 | default: |
| 626 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 627 | break; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | static GstStateChangeReturn |
| 632 | fps_display_sink_change_state (GstElement * element, GstStateChange transition) |
| 633 | { |
| 634 | GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS; |
| 635 | GstFPSDisplaySink *self = GST_FPS_DISPLAY_SINK (element); |
| 636 | |
| 637 | switch (transition) { |
| 638 | case GST_STATE_CHANGE_NULL_TO_READY: |
| 639 | |
| 640 | if (self->video_sink == NULL) { |
| 641 | GstElement *video_sink; |
| 642 | |
| 643 | GST_DEBUG_OBJECT (self, "No video sink set, creating autovideosink"); |
| 644 | video_sink = gst_element_factory_make ("autovideosink", |
| 645 | "fps-display-video_sink"); |
| 646 | update_video_sink (self, video_sink); |
| 647 | } |
| 648 | |
| 649 | if (self->video_sink != NULL) { |
| 650 | fps_display_sink_start (self); |
| 651 | } else { |
| 652 | GST_ELEMENT_ERROR (self, LIBRARY, INIT, |
| 653 | ("No video sink set and autovideosink is not available"), (NULL)); |
| 654 | ret = GST_STATE_CHANGE_FAILURE; |
| 655 | } |
| 656 | break; |
| 657 | case GST_STATE_CHANGE_READY_TO_PAUSED: |
| 658 | case GST_STATE_CHANGE_PAUSED_TO_PLAYING: |
| 659 | /* reinforce our sync to children, as they might have changed |
| 660 | * internally */ |
| 661 | fps_display_sink_update_sink_sync (self); |
| 662 | break; |
| 663 | default: |
| 664 | break; |
| 665 | } |
| 666 | |
| 667 | ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state, |
| 668 | (element, transition), GST_STATE_CHANGE_SUCCESS); |
| 669 | |
| 670 | switch (transition) { |
| 671 | case GST_STATE_CHANGE_READY_TO_NULL: |
| 672 | fps_display_sink_stop (self); |
| 673 | break; |
| 674 | default: |
| 675 | break; |
| 676 | } |
| 677 | |
| 678 | return ret; |
| 679 | } |
| 680 | |
Sebastian Dröge | d1702b9 | 2015-06-24 23:26:45 +0200 | [diff] [blame] | 681 | static void |
| 682 | fps_display_sink_handle_message (GstBin * bin, GstMessage * message) |
| 683 | { |
| 684 | GstFPSDisplaySink *self = (GstFPSDisplaySink *) bin; |
| 685 | |
| 686 | if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_QOS) { |
| 687 | GstFormat format; |
| 688 | guint64 rendered, dropped; |
| 689 | |
| 690 | gst_message_parse_qos_stats (message, &format, &rendered, &dropped); |
| 691 | if (format != GST_FORMAT_UNDEFINED) { |
| 692 | if (rendered != -1) |
| 693 | g_atomic_int_set (&self->frames_rendered, rendered); |
| 694 | |
| 695 | if (dropped != -1) |
| 696 | g_atomic_int_set (&self->frames_dropped, dropped); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | GST_BIN_CLASS (parent_class)->handle_message (bin, message); |
| 701 | } |
| 702 | |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 703 | GType |
| 704 | fps_display_sink_get_type (void) |
| 705 | { |
| 706 | static GType fps_display_sink_type = 0; |
| 707 | |
| 708 | if (!fps_display_sink_type) { |
| 709 | static const GTypeInfo fps_display_sink_info = { |
| 710 | sizeof (GstFPSDisplaySinkClass), |
| 711 | NULL, |
| 712 | NULL, |
| 713 | (GClassInitFunc) fps_display_sink_class_init, |
| 714 | NULL, |
| 715 | NULL, |
| 716 | sizeof (GstFPSDisplaySink), |
| 717 | 0, |
| 718 | (GInstanceInitFunc) fps_display_sink_init, |
| 719 | }; |
| 720 | |
| 721 | fps_display_sink_type = g_type_register_static (GST_TYPE_BIN, |
| 722 | "GstFPSDisplaySink", &fps_display_sink_info, 0); |
| 723 | |
| 724 | GST_DEBUG_CATEGORY_INIT (fps_display_sink_debug, "fpsdisplaysink", 0, |
| 725 | "FPS Display Sink"); |
| 726 | } |
| 727 | |
| 728 | return fps_display_sink_type; |
| 729 | } |