LCOV - code coverage report
Current view: top level - gst-plugins-good/ext/gconf - gstswitchsink.c (source / functions) Hit Total Coverage
Test: GStreamer Lines: 82 113 72.6 %
Date: 2011-03-25 Functions: 9 10 90.0 %
Branches: 23 66 34.8 %

           Branch data     Line data    Source code
       1                 :            : /* GStreamer
       2                 :            :  * Copyright (c) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
       3                 :            :  * Copyright (c) 2006 Jürg Billeter <j@bitron.ch>
       4                 :            :  * Copyright (c) 2007 Jan Schmidt <thaytan@noraisin.net>
       5                 :            :  *
       6                 :            :  * This library is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU Library General Public
       8                 :            :  * License as published by the Free Software Foundation; either
       9                 :            :  * version 2 of the License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This library is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14                 :            :  * Library General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU Library General Public
      17                 :            :  * License along with this library; if not, write to the
      18                 :            :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      19                 :            :  * Boston, MA 02111-1307, USA.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifdef HAVE_CONFIG_H
      23                 :            : #include "config.h"
      24                 :            : #endif
      25                 :            : 
      26                 :            : #include <string.h>
      27                 :            : 
      28                 :            : #include "gstswitchsink.h"
      29                 :            : 
      30                 :            : GST_DEBUG_CATEGORY_STATIC (switch_debug);
      31                 :            : #define GST_CAT_DEFAULT switch_debug
      32                 :            : 
      33                 :            : static void gst_switch_sink_dispose (GObject * object);
      34                 :            : static GstStateChangeReturn
      35                 :            : gst_switch_sink_change_state (GstElement * element, GstStateChange transition);
      36                 :            : 
      37                 :            : enum
      38                 :            : {
      39                 :            :   PROP_0
      40                 :            : };
      41                 :            : 
      42         [ +  + ]:         54 : GST_BOILERPLATE (GstSwitchSink, gst_switch_sink, GstBin, GST_TYPE_BIN);
      43                 :            : 
      44                 :            : static void
      45                 :         24 : gst_switch_sink_base_init (gpointer klass)
      46                 :            : {
      47         [ +  + ]:         24 :   GST_DEBUG_CATEGORY_INIT (switch_debug, "switchsink", 0, "switchsink element");
      48                 :         24 : }
      49                 :            : 
      50                 :            : static void
      51                 :         10 : gst_switch_sink_class_init (GstSwitchSinkClass * klass)
      52                 :            : {
      53                 :         10 :   GObjectClass *oklass = G_OBJECT_CLASS (klass);
      54                 :         10 :   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
      55                 :            :   static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
      56                 :            :       GST_PAD_SINK,
      57                 :            :       GST_PAD_ALWAYS,
      58                 :            :       GST_STATIC_CAPS_ANY);
      59                 :            :   GstPadTemplate *child_pad_templ;
      60                 :            : 
      61                 :         10 :   oklass->dispose = gst_switch_sink_dispose;
      62                 :         10 :   eklass->change_state = gst_switch_sink_change_state;
      63                 :            : 
      64                 :            :   /* Provide a default pad template if the child didn't */
      65                 :         10 :   child_pad_templ = gst_element_class_get_pad_template (eklass, "sink");
      66         [ +  - ]:         10 :   if (child_pad_templ == NULL) {
      67                 :         10 :     gst_element_class_add_pad_template (eklass,
      68                 :            :         gst_static_pad_template_get (&sink_template));
      69                 :            :   }
      70                 :         10 : }
      71                 :            : 
      72                 :            : static gboolean
      73                 :          6 : gst_switch_sink_reset (GstSwitchSink * sink)
      74                 :            : {
      75                 :            :   /* this will install fakesink if no other child has been set,
      76                 :            :    * otherwise we rely on the subclass to know when to unset its
      77                 :            :    * custom kid */
      78         [ +  - ]:          6 :   if (sink->kid == NULL) {
      79                 :          6 :     return gst_switch_sink_set_child (sink, NULL);
      80                 :            :   }
      81                 :            : 
      82                 :          6 :   return TRUE;
      83                 :            : }
      84                 :            : 
      85                 :            : static void
      86                 :          6 : gst_switch_sink_init (GstSwitchSink * sink, GstSwitchSinkClass * g_class)
      87                 :            : {
      88                 :          6 :   GstElementClass *eklass = GST_ELEMENT_GET_CLASS (sink);
      89                 :            :   GstPadTemplate *templ;
      90                 :            : 
      91                 :          6 :   templ = gst_element_class_get_pad_template (eklass, "sink");
      92                 :          6 :   sink->pad = gst_ghost_pad_new_no_target_from_template ("sink", templ);
      93                 :          6 :   gst_element_add_pad (GST_ELEMENT (sink), sink->pad);
      94                 :            : 
      95                 :          6 :   gst_switch_sink_reset (sink);
      96                 :            : 
      97                 :          6 :   GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
      98                 :          6 : }
      99                 :            : 
     100                 :            : static void
     101                 :          6 : gst_switch_sink_dispose (GObject * object)
     102                 :            : {
     103                 :          6 :   GstSwitchSink *sink = GST_SWITCH_SINK (object);
     104                 :            :   GstObject *new_kid, *kid;
     105                 :            : 
     106                 :          6 :   GST_OBJECT_LOCK (sink);
     107                 :          6 :   new_kid = GST_OBJECT_CAST (sink->new_kid);
     108                 :          6 :   sink->new_kid = NULL;
     109                 :            : 
     110                 :          6 :   kid = GST_OBJECT_CAST (sink->kid);
     111                 :          6 :   sink->kid = NULL;
     112                 :          6 :   GST_OBJECT_UNLOCK (sink);
     113                 :            : 
     114                 :          6 :   gst_object_replace (&new_kid, NULL);
     115                 :          6 :   gst_object_replace (&kid, NULL);
     116                 :            : 
     117         [ +  - ]:          6 :   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
     118                 :          6 : }
     119                 :            : 
     120                 :            : static gboolean
     121                 :          6 : gst_switch_sink_commit_new_kid (GstSwitchSink * sink)
     122                 :            : {
     123                 :            :   GstPad *targetpad;
     124                 :            :   GstState kid_state;
     125                 :            :   GstElement *new_kid, *old_kid;
     126                 :          6 :   gboolean is_fakesink = FALSE;
     127                 :            :   GstBus *bus;
     128                 :            : 
     129                 :            :   /* need locking around member accesses */
     130                 :          6 :   GST_OBJECT_LOCK (sink);
     131                 :            :   /* If we're currently changing state, set the child to the next state
     132                 :            :    * we're transitioning too, rather than our current state which is 
     133                 :            :    * about to change */
     134         [ -  + ]:          6 :   if (GST_STATE_NEXT (sink) != GST_STATE_VOID_PENDING)
     135                 :          0 :     kid_state = GST_STATE_NEXT (sink);
     136                 :            :   else
     137                 :          6 :     kid_state = GST_STATE (sink);
     138                 :            : 
     139         [ -  + ]:          6 :   new_kid = sink->new_kid ? gst_object_ref (sink->new_kid) : NULL;
     140                 :          6 :   sink->new_kid = NULL;
     141                 :          6 :   GST_OBJECT_UNLOCK (sink);
     142                 :            : 
     143                 :            :   /* Fakesink by default if NULL is passed as the new child */
     144         [ +  - ]:          6 :   if (new_kid == NULL) {
     145         [ -  + ]:          6 :     GST_DEBUG_OBJECT (sink, "Replacing kid with fakesink");
     146                 :          6 :     new_kid = gst_element_factory_make ("fakesink", "testsink");
     147         [ -  + ]:          6 :     if (new_kid == NULL) {
     148         [ #  # ]:          0 :       GST_ERROR_OBJECT (sink, "Failed to create fakesink");
     149                 :          0 :       return FALSE;
     150                 :            :     }
     151                 :            :     /* Add a reference, as it would if the element came from sink->new_kid */
     152                 :          6 :     gst_object_ref (new_kid);
     153                 :          6 :     g_object_set (new_kid, "sync", TRUE, NULL);
     154                 :          6 :     is_fakesink = TRUE;
     155                 :            :   } else {
     156         [ #  # ]:          0 :     GST_DEBUG_OBJECT (sink, "Setting new kid");
     157                 :            :   }
     158                 :            : 
     159                 :            :   /* set temporary bus of our own to catch error messages from the child
     160                 :            :    * (could we just set our own bus on it, or would the state change messages
     161                 :            :    * from the not-yet-added element confuse the state change algorithm? Let's
     162                 :            :    * play it safe for now) */
     163                 :          6 :   bus = gst_bus_new ();
     164                 :          6 :   gst_element_set_bus (new_kid, bus);
     165                 :          6 :   gst_object_unref (bus);
     166                 :            : 
     167         [ -  + ]:          6 :   if (gst_element_set_state (new_kid, kid_state) == GST_STATE_CHANGE_FAILURE) {
     168                 :            :     GstMessage *msg;
     169                 :            : 
     170                 :            :     /* check if child posted an error message and if so re-post it on our bus
     171                 :            :      * so that the application gets to see a decent error and not our generic
     172                 :            :      * fallback error message which is completely indecipherable to the user */
     173                 :          0 :     msg = gst_bus_pop_filtered (GST_ELEMENT_BUS (new_kid), GST_MESSAGE_ERROR);
     174         [ #  # ]:          0 :     if (msg) {
     175         [ #  # ]:          0 :       GST_INFO_OBJECT (sink, "Forwarding kid error: %" GST_PTR_FORMAT, msg);
     176                 :          0 :       gst_element_post_message (GST_ELEMENT (sink), msg);
     177                 :            :     }
     178 [ #  # ][ #  # ]:          0 :     GST_ELEMENT_ERROR (sink, CORE, STATE_CHANGE, (NULL),
         [ #  # ][ #  # ]
     179                 :            :         ("Failed to set state on new child."));
     180                 :          0 :     gst_element_set_bus (new_kid, NULL);
     181                 :          0 :     gst_object_unref (new_kid);
     182                 :          0 :     return FALSE;
     183                 :            :   }
     184                 :          6 :   gst_element_set_bus (new_kid, NULL);
     185                 :          6 :   gst_bin_add (GST_BIN (sink), new_kid);
     186                 :            : 
     187                 :            :   /* Now, replace the existing child */
     188                 :          6 :   GST_OBJECT_LOCK (sink);
     189                 :          6 :   old_kid = sink->kid;
     190                 :          6 :   sink->kid = new_kid;
     191                 :            :   /* Mark whether a custom kid or fakesink has been installed */
     192                 :          6 :   sink->have_kid = !is_fakesink;
     193                 :          6 :   GST_OBJECT_UNLOCK (sink);
     194                 :            : 
     195                 :            :   /* kill old element */
     196         [ -  + ]:          6 :   if (old_kid) {
     197         [ #  # ]:          0 :     GST_DEBUG_OBJECT (sink, "Removing old kid %" GST_PTR_FORMAT, old_kid);
     198                 :          0 :     gst_element_set_state (old_kid, GST_STATE_NULL);
     199                 :          0 :     gst_bin_remove (GST_BIN (sink), old_kid);
     200                 :          0 :     gst_object_unref (old_kid);
     201                 :            :     /* Don't lose the SINK flag */
     202                 :          0 :     GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
     203                 :            :   }
     204                 :            : 
     205                 :            :   /* re-attach ghostpad */
     206         [ -  + ]:          6 :   GST_DEBUG_OBJECT (sink, "Creating new ghostpad");
     207                 :          6 :   targetpad = gst_element_get_static_pad (sink->kid, "sink");
     208                 :          6 :   gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
     209                 :          6 :   gst_object_unref (targetpad);
     210         [ -  + ]:          6 :   GST_DEBUG_OBJECT (sink, "done changing child of switchsink");
     211                 :            : 
     212                 :            :   /* FIXME: Push new-segment info and pre-roll buffer(s) into the kid */
     213                 :            : 
     214                 :          6 :   return TRUE;
     215                 :            : }
     216                 :            : 
     217                 :            : gboolean
     218                 :         12 : gst_switch_sink_set_child (GstSwitchSink * sink, GstElement * new_kid)
     219                 :            : {
     220                 :            :   GstState cur, next;
     221                 :            :   GstElement **p_kid;
     222                 :            : 
     223                 :            :   /* Nothing to do if clearing the child and we've already installed fakesink */
     224 [ +  - ][ +  + ]:         12 :   if (new_kid == NULL && sink->kid != NULL && sink->have_kid == FALSE)
                 [ +  - ]
     225                 :          6 :     return TRUE;
     226                 :            : 
     227                 :            :   /* Store the new kid to be committed later */
     228                 :          6 :   GST_OBJECT_LOCK (sink);
     229                 :          6 :   cur = GST_STATE (sink);
     230                 :          6 :   next = GST_STATE_NEXT (sink);
     231                 :          6 :   p_kid = &sink->new_kid;
     232                 :          6 :   gst_object_replace ((GstObject **) p_kid, (GstObject *) new_kid);
     233                 :          6 :   GST_OBJECT_UNLOCK (sink);
     234         [ -  + ]:          6 :   if (new_kid)
     235                 :          0 :     gst_object_unref (new_kid);
     236                 :            : 
     237                 :            :   /* Sometime, it would be lovely to allow sink changes even when
     238                 :            :    * already running, but this involves sending an appropriate new-segment
     239                 :            :    * and possibly prerolling etc */
     240                 :            :   /* FIXME: Block the pad and replace the kid when it completes */
     241 [ +  - ][ -  + ]:          6 :   if (cur > GST_STATE_READY || next == GST_STATE_PAUSED) {
     242         [ #  # ]:          0 :     GST_DEBUG_OBJECT (sink,
     243                 :            :         "Switch-sink is already running. Ignoring change of child.");
     244                 :          0 :     gst_object_unref (new_kid);
     245                 :          0 :     return TRUE;
     246                 :            :   }
     247                 :            : 
     248                 :         12 :   return gst_switch_sink_commit_new_kid (sink);
     249                 :            : }
     250                 :            : 
     251                 :            : static GstStateChangeReturn
     252                 :          0 : gst_switch_sink_change_state (GstElement * element, GstStateChange transition)
     253                 :            : {
     254                 :          0 :   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
     255                 :          0 :   GstSwitchSink *sink = GST_SWITCH_SINK (element);
     256                 :            : 
     257         [ #  # ]:          0 :   ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
     258                 :            :       (element, transition), GST_STATE_CHANGE_SUCCESS);
     259                 :            : 
     260         [ #  # ]:          0 :   switch (transition) {
     261                 :            :     case GST_STATE_CHANGE_READY_TO_NULL:
     262         [ #  # ]:          0 :       if (!gst_switch_sink_reset (sink))
     263                 :          0 :         ret = GST_STATE_CHANGE_FAILURE;
     264                 :          0 :       break;
     265                 :            :     default:
     266                 :          0 :       break;
     267                 :            :   }
     268                 :            : 
     269                 :          0 :   return ret;
     270                 :            : }

Generated by: LCOV version 1.9