gst_plugins_base/tests/examples/dynamic/addstream.c
changeset 0 0e761a78d257
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     1 /* GStreamer
       
     2  *
       
     3  * addstream.c: sample application to dynamically add streams to a running
       
     4  * pipeline
       
     5  *
       
     6  * Copyright (C) <2007> Wim Taymans <wim dot taymans at gmail dot com>
       
     7  *
       
     8  * This library is free software; you can redistribute it and/or
       
     9  * modify it under the terms of the GNU Library General Public
       
    10  * License as published by the Free Software Foundation; either
       
    11  * version 2 of the License, or (at your option) any later version.
       
    12  *
       
    13  * This library is distributed in the hope that it will be useful,
       
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    16  * Library General Public License for more details.
       
    17  *
       
    18  * You should have received a copy of the GNU Library General Public
       
    19  * License along with this library; if not, write to the
       
    20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    21  * Boston, MA 02111-1307, USA.
       
    22  */
       
    23 
       
    24 #ifdef HAVE_CONFIG_H
       
    25 #include "config.h"
       
    26 #endif
       
    27 
       
    28 #include <gst/gst.h>
       
    29 
       
    30 static GstElement *pipeline;
       
    31 static GstClock *theclock;
       
    32 static GMainLoop *loop;
       
    33 static GstElement *bin1, *bin2, *bin3, *bin4, *bin5;
       
    34 
       
    35 /* start a bin with the given description */
       
    36 static GstElement *
       
    37 create_stream (const gchar * descr)
       
    38 {
       
    39   GstElement *bin;
       
    40   GError *error = NULL;
       
    41 
       
    42   bin = gst_parse_launch (descr, &error);
       
    43   if (error) {
       
    44     g_print ("pipeline could not be constructed: %s\n", error->message);
       
    45     g_error_free (error);
       
    46     return NULL;
       
    47   }
       
    48 
       
    49   /* add the bin to the pipeline now, this will set the current base_time of the
       
    50    * pipeline on the new bin. */
       
    51   gst_bin_add (GST_BIN_CAST (pipeline), bin);
       
    52 
       
    53   return bin;
       
    54 }
       
    55 
       
    56 static gboolean
       
    57 pause_play_stream (GstElement * bin, gint seconds)
       
    58 {
       
    59   gboolean punch_in;
       
    60   GstStateChangeReturn ret;
       
    61   GstClockTime now, base_time, running_time;
       
    62 
       
    63   /* get current running time, we need this value to continue playback of
       
    64    * non-live pipelines. */
       
    65   now = gst_clock_get_time (theclock);
       
    66   base_time = gst_element_get_base_time (bin);
       
    67 
       
    68   running_time = now - base_time;
       
    69 
       
    70   /* set the new bin to PAUSED, the parent bin will notice (because of the ASYNC
       
    71    * message and will perform latency calculations again when going to PLAYING
       
    72    * later. */
       
    73   ret = gst_element_set_state (bin, GST_STATE_PAUSED);
       
    74 
       
    75   switch (ret) {
       
    76     case GST_STATE_CHANGE_NO_PREROLL:
       
    77       /* live source, timestamps are running_time of the pipeline clock. */
       
    78       punch_in = FALSE;
       
    79       break;
       
    80     case GST_STATE_CHANGE_SUCCESS:
       
    81       /* success, no async state changes, same as async, timestamps start
       
    82        * from 0 */
       
    83     case GST_STATE_CHANGE_ASYNC:
       
    84       /* no live source, bin will preroll. We have to punch it in because in
       
    85        * this situation timestamps start from 0.  */
       
    86       punch_in = TRUE;
       
    87       break;
       
    88     default:
       
    89     case GST_STATE_CHANGE_FAILURE:
       
    90       return FALSE;
       
    91   }
       
    92 
       
    93   if (seconds)
       
    94     g_usleep (seconds * G_USEC_PER_SEC);
       
    95 
       
    96   if (punch_in) {
       
    97     /* new bin has to be aligned with previous running_time. We do this by taking
       
    98      * the current absolute clock time and calculating the base time that would
       
    99      * give the previous running_time. We set this base_time on the bin before
       
   100      * setting it to PLAYING. */
       
   101     now = gst_clock_get_time (theclock);
       
   102     base_time = now - running_time;
       
   103 
       
   104     gst_element_set_base_time (bin, base_time);
       
   105   }
       
   106 
       
   107   /* now set the pipeline to PLAYING */
       
   108   gst_element_set_state (bin, GST_STATE_PLAYING);
       
   109 
       
   110   return TRUE;
       
   111 }
       
   112 
       
   113 static void
       
   114 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
       
   115 {
       
   116   const GstStructure *s;
       
   117 
       
   118   s = gst_message_get_structure (message);
       
   119   g_print ("message from \"%s\" (%s): ",
       
   120       GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
       
   121       gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
       
   122   if (s) {
       
   123     gchar *sstr;
       
   124 
       
   125     sstr = gst_structure_to_string (s);
       
   126     g_print ("%s\n", sstr);
       
   127     g_free (sstr);
       
   128   } else {
       
   129     g_print ("no message details\n");
       
   130   }
       
   131 }
       
   132 
       
   133 static void
       
   134 eos_message_received (GstBus * bus, GstMessage * message,
       
   135     GstPipeline * pipeline)
       
   136 {
       
   137   message_received (bus, message, pipeline);
       
   138   g_main_loop_quit (loop);
       
   139 }
       
   140 
       
   141 static gboolean
       
   142 perform_step (gpointer pstep)
       
   143 {
       
   144   gint step = GPOINTER_TO_INT (pstep);
       
   145 
       
   146   switch (step) {
       
   147     case 0:
       
   148       /* live stream locks on to running_time, pipeline configures latency. */
       
   149       g_print ("creating bin1\n");
       
   150       bin1 =
       
   151           create_stream
       
   152           ("( v4l2src ! ffmpegcolorspace ! timeoverlay ! queue ! xvimagesink name=v4llive )");
       
   153       pause_play_stream (bin1, 0);
       
   154       g_timeout_add (1000, (GSourceFunc) perform_step, GINT_TO_POINTER (1));
       
   155       break;
       
   156     case 1:
       
   157       /* live stream locks on to running_time, pipeline reconfigures latency
       
   158        * together with the previously added bin so that they run synchronized. */
       
   159       g_print ("creating bin2\n");
       
   160       bin2 = create_stream ("( alsasrc ! queue ! alsasink name=alsalive )");
       
   161       pause_play_stream (bin2, 0);
       
   162       g_timeout_add (1000, (GSourceFunc) perform_step, GINT_TO_POINTER (2));
       
   163       break;
       
   164     case 2:
       
   165       /* non-live stream, need base_time to align with current running live sources. */
       
   166       g_print ("creating bin3\n");
       
   167       bin3 = create_stream ("( audiotestsrc ! alsasink name=atnonlive )");
       
   168       pause_play_stream (bin3, 0);
       
   169       g_timeout_add (1000, (GSourceFunc) perform_step, GINT_TO_POINTER (3));
       
   170       break;
       
   171     case 3:
       
   172       g_print ("creating bin4\n");
       
   173       bin4 =
       
   174           create_stream
       
   175           ("( videotestsrc ! timeoverlay ! ffmpegcolorspace ! ximagesink name=vtnonlive )");
       
   176       pause_play_stream (bin4, 0);
       
   177       g_timeout_add (1000, (GSourceFunc) perform_step, GINT_TO_POINTER (4));
       
   178       break;
       
   179     case 4:
       
   180       /* live stream locks on to running_time */
       
   181       g_print ("creating bin5\n");
       
   182       bin5 =
       
   183           create_stream
       
   184           ("( videotestsrc is-live=1 ! timeoverlay ! ffmpegcolorspace ! ximagesink name=vtlive )");
       
   185       pause_play_stream (bin5, 0);
       
   186       g_timeout_add (1000, (GSourceFunc) perform_step, GINT_TO_POINTER (5));
       
   187       break;
       
   188     case 5:
       
   189       /* pause the fist live stream for 2 seconds */
       
   190       g_print ("PAUSE bin1 for 2 seconds\n");
       
   191       pause_play_stream (bin1, 2);
       
   192       /* pause the non-live stream for 2 seconds */
       
   193       g_print ("PAUSE bin4 for 2 seconds\n");
       
   194       pause_play_stream (bin4, 2);
       
   195       /* pause the pseudo live stream for 2 seconds */
       
   196       g_print ("PAUSE bin5 for 2 seconds\n");
       
   197       pause_play_stream (bin5, 2);
       
   198       g_print ("Waiting 5 seconds\n");
       
   199       g_timeout_add (5000, (GSourceFunc) perform_step, GINT_TO_POINTER (6));
       
   200       break;
       
   201     case 6:
       
   202       g_print ("quiting\n");
       
   203       g_main_loop_quit (loop);
       
   204       break;
       
   205     default:
       
   206       break;
       
   207   }
       
   208   return FALSE;
       
   209 }
       
   210 
       
   211 int
       
   212 main (int argc, char *argv[])
       
   213 {
       
   214   GstBus *bus;
       
   215 
       
   216   gst_init (&argc, &argv);
       
   217 
       
   218   loop = g_main_loop_new (NULL, TRUE);
       
   219 
       
   220   pipeline = gst_pipeline_new ("pipeline");
       
   221 
       
   222   /* setup message handling */
       
   223   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
       
   224   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
       
   225   g_signal_connect (bus, "message::error", (GCallback) message_received,
       
   226       pipeline);
       
   227   g_signal_connect (bus, "message::warning", (GCallback) message_received,
       
   228       pipeline);
       
   229   g_signal_connect (bus, "message::eos", (GCallback) eos_message_received,
       
   230       pipeline);
       
   231 
       
   232   /* we set the pipeline to PLAYING, this will distribute a default clock and
       
   233    * start running. no preroll is needed */
       
   234   gst_element_set_state (pipeline, GST_STATE_PLAYING);
       
   235 
       
   236   /* get the clock now. Since we never set the pipeline to PAUSED again, the
       
   237    * clock will not change, even when we add new clock providers later.  */
       
   238   theclock = gst_element_get_clock (pipeline);
       
   239 
       
   240   /* start our actions while we are in the mainloop so that we can catch errors
       
   241    * and other messages. */
       
   242   g_idle_add ((GSourceFunc) perform_step, GINT_TO_POINTER (0)),
       
   243       /* go to main loop */
       
   244       g_main_loop_run (loop);
       
   245 
       
   246   gst_element_set_state (pipeline, GST_STATE_NULL);
       
   247 
       
   248   gst_object_unref (bus);
       
   249   gst_object_unref (pipeline);
       
   250   gst_object_unref (theclock);
       
   251 
       
   252   return 0;
       
   253 }