1 /* GStreamer
2  * Copyright (C) 2013 Rdio <ingestions@rdio.com>
3  * Copyright (C) 2013 David Schleef <ds@schleef.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
18  * Boston, MA 02110-1335, USA.
19  */
20 /**
21  * SECTION:element-gstwatchdog
22  * @title: watchdog
23  *
24  * The watchdog element watches buffers and events flowing through
25  * a pipeline.  If no buffers are seen for a configurable amount of
26  * time, a error message is sent to the bus.
27  *
28  * To use this element, insert it into a pipeline as you would an
29  * identity element.  Once activated, any pause in the flow of
30  * buffers through the element will cause an element error.  The
31  * maximum allowed pause is determined by the timeout property.
32  *
33  * This element is currently intended for transcoding pipelines,
34  * although may be useful in other contexts.
35  *
36  * ## Example launch line
37  * |[
38  * gst-launch-1.0 -v fakesrc ! watchdog ! fakesink
39  * ]|
40  *
41  */
42 
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46 
47 #include <gst/gst.h>
48 #include <gst/base/gstbasetransform.h>
49 #include "gstwatchdog.h"
50 
51 GST_DEBUG_CATEGORY_STATIC (gst_watchdog_debug_category);
52 #define GST_CAT_DEFAULT gst_watchdog_debug_category
53 
54 /* prototypes */
55 
56 static void gst_watchdog_set_property (GObject * object,
57     guint property_id, const GValue * value, GParamSpec * pspec);
58 static void gst_watchdog_get_property (GObject * object,
59     guint property_id, GValue * value, GParamSpec * pspec);
60 
61 static gboolean gst_watchdog_start (GstBaseTransform * trans);
62 static gboolean gst_watchdog_stop (GstBaseTransform * trans);
63 static gboolean gst_watchdog_sink_event (GstBaseTransform * trans,
64     GstEvent * event);
65 static gboolean gst_watchdog_src_event (GstBaseTransform * trans,
66     GstEvent * event);
67 static GstFlowReturn gst_watchdog_transform_ip (GstBaseTransform * trans,
68     GstBuffer * buf);
69 static void gst_watchdog_feed (GstWatchdog * watchdog, gpointer mini_object,
70     gboolean force);
71 
72 static GstStateChangeReturn
73 gst_watchdog_change_state (GstElement * element, GstStateChange transition);
74 
75 enum
76 {
77   PROP_0,
78   PROP_TIMEOUT
79 };
80 
81 /* class initialization */
82 
83 G_DEFINE_TYPE_WITH_CODE (GstWatchdog, gst_watchdog, GST_TYPE_BASE_TRANSFORM,
84     GST_DEBUG_CATEGORY_INIT (gst_watchdog_debug_category, "watchdog", 0,
85         "debug category for watchdog element"));
86 
87 static void
gst_watchdog_class_init(GstWatchdogClass * klass)88 gst_watchdog_class_init (GstWatchdogClass * klass)
89 {
90   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
91   GstBaseTransformClass *base_transform_class =
92       GST_BASE_TRANSFORM_CLASS (klass);
93 
94   GstElementClass *gstelement_klass = (GstElementClass *) klass;
95 
96   gst_element_class_add_pad_template (GST_ELEMENT_CLASS (klass),
97       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
98           gst_caps_new_any ()));
99   gst_element_class_add_pad_template (GST_ELEMENT_CLASS (klass),
100       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
101           gst_caps_new_any ()));
102 
103   gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
104       "Watchdog", "Generic", "Watches for pauses in stream buffers",
105       "David Schleef <ds@schleef.org>");
106 
107   gstelement_klass->change_state =
108       GST_DEBUG_FUNCPTR (gst_watchdog_change_state);
109   gobject_class->set_property = gst_watchdog_set_property;
110   gobject_class->get_property = gst_watchdog_get_property;
111   base_transform_class->start = GST_DEBUG_FUNCPTR (gst_watchdog_start);
112   base_transform_class->stop = GST_DEBUG_FUNCPTR (gst_watchdog_stop);
113   base_transform_class->sink_event =
114       GST_DEBUG_FUNCPTR (gst_watchdog_sink_event);
115   base_transform_class->src_event = GST_DEBUG_FUNCPTR (gst_watchdog_src_event);
116   base_transform_class->transform_ip =
117       GST_DEBUG_FUNCPTR (gst_watchdog_transform_ip);
118 
119   g_object_class_install_property (gobject_class, PROP_TIMEOUT,
120       g_param_spec_int ("timeout", "Timeout", "Timeout (in ms) after "
121           "which an element error is sent to the bus if no buffers are "
122           "received. 0 means disabled.", 0, G_MAXINT, 1000,
123           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
124 
125 }
126 
127 static void
gst_watchdog_init(GstWatchdog * watchdog)128 gst_watchdog_init (GstWatchdog * watchdog)
129 {
130 }
131 
132 static void
gst_watchdog_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)133 gst_watchdog_set_property (GObject * object, guint property_id,
134     const GValue * value, GParamSpec * pspec)
135 {
136   GstWatchdog *watchdog = GST_WATCHDOG (object);
137 
138   GST_DEBUG_OBJECT (watchdog, "set_property");
139 
140   switch (property_id) {
141     case PROP_TIMEOUT:
142       GST_OBJECT_LOCK (watchdog);
143       watchdog->timeout = g_value_get_int (value);
144       gst_watchdog_feed (watchdog, NULL, FALSE);
145       GST_OBJECT_UNLOCK (watchdog);
146       break;
147     default:
148       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
149       break;
150   }
151 }
152 
153 static void
gst_watchdog_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)154 gst_watchdog_get_property (GObject * object, guint property_id,
155     GValue * value, GParamSpec * pspec)
156 {
157   GstWatchdog *watchdog = GST_WATCHDOG (object);
158 
159   GST_DEBUG_OBJECT (watchdog, "get_property");
160 
161   switch (property_id) {
162     case PROP_TIMEOUT:
163       g_value_set_int (value, watchdog->timeout);
164       break;
165     default:
166       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
167       break;
168   }
169 }
170 
171 static gpointer
gst_watchdog_thread(gpointer user_data)172 gst_watchdog_thread (gpointer user_data)
173 {
174   GstWatchdog *watchdog = GST_WATCHDOG (user_data);
175 
176   GST_DEBUG_OBJECT (watchdog, "thread starting");
177 
178   g_main_loop_run (watchdog->main_loop);
179 
180   GST_DEBUG_OBJECT (watchdog, "thread exiting");
181 
182   return NULL;
183 }
184 
185 static gboolean
gst_watchdog_trigger(gpointer ptr)186 gst_watchdog_trigger (gpointer ptr)
187 {
188   GstWatchdog *watchdog = GST_WATCHDOG (ptr);
189 
190   GST_DEBUG_OBJECT (watchdog, "watchdog triggered");
191 
192   GST_ELEMENT_ERROR (watchdog, STREAM, FAILED, ("Watchdog triggered"),
193       ("Watchdog triggered"));
194 
195   return FALSE;
196 }
197 
198 static gboolean
gst_watchdog_quit_mainloop(gpointer ptr)199 gst_watchdog_quit_mainloop (gpointer ptr)
200 {
201   GstWatchdog *watchdog = GST_WATCHDOG (ptr);
202 
203   GST_DEBUG_OBJECT (watchdog, "watchdog quit");
204 
205   g_main_loop_quit (watchdog->main_loop);
206 
207   return FALSE;
208 }
209 
210 /*  Call with OBJECT_LOCK taken */
211 static void
gst_watchdog_feed(GstWatchdog * watchdog,gpointer mini_object,gboolean force)212 gst_watchdog_feed (GstWatchdog * watchdog, gpointer mini_object, gboolean force)
213 {
214   if (watchdog->source) {
215     if (watchdog->waiting_for_flush_start) {
216       if (mini_object && GST_IS_EVENT (mini_object) &&
217           GST_EVENT_TYPE (mini_object) == GST_EVENT_FLUSH_START) {
218         watchdog->waiting_for_flush_start = FALSE;
219         watchdog->waiting_for_flush_stop = TRUE;
220       }
221 
222       force = TRUE;
223     } else if (watchdog->waiting_for_flush_stop) {
224       if (mini_object && GST_IS_EVENT (mini_object) &&
225           GST_EVENT_TYPE (mini_object) == GST_EVENT_FLUSH_STOP) {
226         watchdog->waiting_for_flush_stop = FALSE;
227         watchdog->waiting_for_a_buffer = TRUE;
228       }
229 
230       force = TRUE;
231     } else if (watchdog->waiting_for_a_buffer) {
232       if (mini_object && GST_IS_BUFFER (mini_object)) {
233         watchdog->waiting_for_a_buffer = FALSE;
234         GST_DEBUG_OBJECT (watchdog, "Got a buffer \\o/");
235       } else {
236         GST_DEBUG_OBJECT (watchdog, "Waiting for a buffer and did not get it,"
237             " keep trying even in PAUSED state");
238         force = TRUE;
239       }
240     }
241     g_source_destroy (watchdog->source);
242     g_source_unref (watchdog->source);
243     watchdog->source = NULL;
244 
245   }
246 
247   if (watchdog->timeout == 0) {
248     GST_LOG_OBJECT (watchdog, "Timeout is 0 => nothing to do");
249   } else if (watchdog->main_context == NULL) {
250     GST_LOG_OBJECT (watchdog, "No maincontext => nothing to do");
251   } else if ((GST_STATE (watchdog) != GST_STATE_PLAYING) && force == FALSE) {
252     GST_LOG_OBJECT (watchdog,
253         "Not in playing and force is FALSE => Nothing to do");
254   } else {
255     watchdog->source = g_timeout_source_new (watchdog->timeout);
256     g_source_set_callback (watchdog->source, gst_watchdog_trigger,
257         gst_object_ref (watchdog), gst_object_unref);
258     g_source_attach (watchdog->source, watchdog->main_context);
259   }
260 }
261 
262 static gboolean
gst_watchdog_start(GstBaseTransform * trans)263 gst_watchdog_start (GstBaseTransform * trans)
264 {
265   GstWatchdog *watchdog = GST_WATCHDOG (trans);
266 
267   GST_DEBUG_OBJECT (watchdog, "start");
268   GST_OBJECT_LOCK (watchdog);
269 
270   watchdog->main_context = g_main_context_new ();
271   watchdog->main_loop = g_main_loop_new (watchdog->main_context, TRUE);
272   watchdog->thread = g_thread_new ("watchdog", gst_watchdog_thread, watchdog);
273 
274   GST_OBJECT_UNLOCK (watchdog);
275   return TRUE;
276 }
277 
278 static gboolean
gst_watchdog_stop(GstBaseTransform * trans)279 gst_watchdog_stop (GstBaseTransform * trans)
280 {
281   GstWatchdog *watchdog = GST_WATCHDOG (trans);
282   GSource *quit_source;
283 
284   GST_DEBUG_OBJECT (watchdog, "stop");
285   GST_OBJECT_LOCK (watchdog);
286 
287   if (watchdog->source) {
288     g_source_destroy (watchdog->source);
289     g_source_unref (watchdog->source);
290     watchdog->source = NULL;
291   }
292 
293   /* dispatch an idle event that trigger g_main_loop_quit to avoid race
294    * between g_main_loop_run and g_main_loop_quit */
295   quit_source = g_idle_source_new ();
296   g_source_set_callback (quit_source, gst_watchdog_quit_mainloop, watchdog,
297       NULL);
298   g_source_attach (quit_source, watchdog->main_context);
299   g_source_unref (quit_source);
300 
301   g_thread_join (watchdog->thread);
302   watchdog->thread = NULL;
303 
304   g_main_loop_unref (watchdog->main_loop);
305   watchdog->main_loop = NULL;
306 
307   g_main_context_unref (watchdog->main_context);
308   watchdog->main_context = NULL;
309 
310   GST_OBJECT_UNLOCK (watchdog);
311   return TRUE;
312 }
313 
314 static gboolean
gst_watchdog_sink_event(GstBaseTransform * trans,GstEvent * event)315 gst_watchdog_sink_event (GstBaseTransform * trans, GstEvent * event)
316 {
317   GstWatchdog *watchdog = GST_WATCHDOG (trans);
318 
319   GST_DEBUG_OBJECT (watchdog, "sink_event");
320 
321   GST_OBJECT_LOCK (watchdog);
322   gst_watchdog_feed (watchdog, event, FALSE);
323   GST_OBJECT_UNLOCK (watchdog);
324 
325   return
326       GST_BASE_TRANSFORM_CLASS (gst_watchdog_parent_class)->sink_event (trans,
327       event);
328 }
329 
330 static gboolean
gst_watchdog_src_event(GstBaseTransform * trans,GstEvent * event)331 gst_watchdog_src_event (GstBaseTransform * trans, GstEvent * event)
332 {
333   gboolean force = FALSE;
334   GstWatchdog *watchdog = GST_WATCHDOG (trans);
335 
336   GST_DEBUG_OBJECT (watchdog, "src_event");
337 
338   GST_OBJECT_LOCK (watchdog);
339   if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
340     GstSeekFlags flags;
341 
342     gst_event_parse_seek (event, NULL, NULL, &flags, NULL, NULL, NULL, NULL);
343 
344     if (flags & GST_SEEK_FLAG_FLUSH) {
345       force = TRUE;
346       GST_DEBUG_OBJECT (watchdog, "Got a FLUSHING seek, we need a buffer now!");
347       watchdog->waiting_for_flush_start = TRUE;
348     }
349   }
350 
351   gst_watchdog_feed (watchdog, event, force);
352   GST_OBJECT_UNLOCK (watchdog);
353 
354   return GST_BASE_TRANSFORM_CLASS (gst_watchdog_parent_class)->src_event (trans,
355       event);
356 }
357 
358 static GstFlowReturn
gst_watchdog_transform_ip(GstBaseTransform * trans,GstBuffer * buf)359 gst_watchdog_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
360 {
361   GstWatchdog *watchdog = GST_WATCHDOG (trans);
362 
363   GST_DEBUG_OBJECT (watchdog, "transform_ip");
364 
365   GST_OBJECT_LOCK (watchdog);
366   gst_watchdog_feed (watchdog, buf, FALSE);
367   GST_OBJECT_UNLOCK (watchdog);
368 
369   return GST_FLOW_OK;
370 }
371 
372 /*
373  * Change state handler for the element.
374  */
375 static GstStateChangeReturn
gst_watchdog_change_state(GstElement * element,GstStateChange transition)376 gst_watchdog_change_state (GstElement * element, GstStateChange transition)
377 {
378   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
379   GstWatchdog *watchdog = GST_WATCHDOG (element);
380 
381   GST_DEBUG_OBJECT (watchdog, "gst_watchdog_change_state");
382 
383   switch (transition) {
384     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
385       /* Activate timer */
386       GST_OBJECT_LOCK (watchdog);
387       gst_watchdog_feed (watchdog, NULL, FALSE);
388       GST_OBJECT_UNLOCK (watchdog);
389       break;
390     default:
391       break;
392   }
393 
394   ret =
395       GST_ELEMENT_CLASS (gst_watchdog_parent_class)->change_state (element,
396       transition);
397 
398   switch (transition) {
399     case GST_STATE_CHANGE_READY_TO_PAUSED:
400       GST_OBJECT_LOCK (watchdog);
401       watchdog->waiting_for_a_buffer = TRUE;
402       gst_watchdog_feed (watchdog, NULL, TRUE);
403       GST_OBJECT_UNLOCK (watchdog);
404       break;
405     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
406       /* Disable the timer */
407       GST_OBJECT_LOCK (watchdog);
408       if (watchdog->source) {
409         g_source_destroy (watchdog->source);
410         g_source_unref (watchdog->source);
411         watchdog->source = NULL;
412       }
413       GST_OBJECT_UNLOCK (watchdog);
414       break;
415     default:
416       break;
417   }
418 
419   return ret;
420 }
421