gst_plugins_base/tests/examples/app/appsrc-stream2.c
changeset 2 5505e8908944
parent 0 0e761a78d257
equal deleted inserted replaced
1:4c282e7dd6d3 2:5505e8908944
       
     1 /* GStreamer
       
     2  *
       
     3  * appsrc-stream2.c: example for using appsrc in streaming mode.
       
     4  *
       
     5  * Copyright (C) 2008 Wim Taymans <wim.taymans@gmail.com>
       
     6  *
       
     7  * This library is free software; you can redistribute it and/or
       
     8  * modify it under the terms of the GNU Library General Public
       
     9  * License as published by the Free Software Foundation; either
       
    10  * version 2 of the License, or (at your option) any later version.
       
    11  *
       
    12  * This library is distributed in the hope that it will be useful,
       
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    15  * Library General Public License for more details.
       
    16  *
       
    17  * You should have received a copy of the GNU Library General Public
       
    18  * License along with this library; if not, write to the
       
    19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    20  * Boston, MA 02111-1307, USA.
       
    21  */
       
    22 
       
    23 #ifdef HAVE_CONFIG_H
       
    24 #include "config.h"
       
    25 #endif
       
    26 
       
    27 #include <gst/gst.h>
       
    28 
       
    29 #include <stdio.h>
       
    30 #include <string.h>
       
    31 #include <stdlib.h>
       
    32 
       
    33 GST_DEBUG_CATEGORY (appsrc_playbin_debug);
       
    34 #define GST_CAT_DEFAULT appsrc_playbin_debug
       
    35 
       
    36 /*
       
    37  * an example application of using appsrc in streaming pull mode. When the
       
    38  * appsrc request data with the need-data signal, we retrieve a buffer of an
       
    39  * arbitrary size and push it to appsrc.
       
    40  *
       
    41  * This example keeps the internal buffer queue of appsrc to a minimal size,
       
    42  * only feeding data to appsrc when needed.
       
    43  *
       
    44  * This is a good example how one would deal with a live resource, such as a udp
       
    45  * socket where one would feed the most recently acquired buffer to appsrc. 
       
    46  *
       
    47  * Usually one would timestamp the buffers with the running_time of the
       
    48  * pipeline or configure the appsrc to do timestamping by setting the 
       
    49  * do-timestamp property to TRUE.
       
    50  *
       
    51  * Appsrc in streaming mode (the default) does not support seeking so we don't
       
    52  * have to handle any seek callbacks.
       
    53  *
       
    54  * Some formats are able to estimate the duration of the media file based on the
       
    55  * file length (mp3, mpeg,..), others report an unknown length (ogg,..).
       
    56  */
       
    57 typedef struct _App App;
       
    58 
       
    59 struct _App
       
    60 {
       
    61   GstElement *playbin;
       
    62   GstElement *appsrc;
       
    63 
       
    64   GMainLoop *loop;
       
    65 
       
    66   GMappedFile *file;
       
    67   guint8 *data;
       
    68   gsize length;
       
    69   guint64 offset;
       
    70 };
       
    71 
       
    72 App s_app;
       
    73 
       
    74 #define CHUNK_SIZE  4096
       
    75 
       
    76 /* This method is called by the need-data signal callback, we feed data into the
       
    77  * appsrc.
       
    78  */
       
    79 static void
       
    80 feed_data (GstElement * appsrc, guint size, App * app)
       
    81 {
       
    82   GstBuffer *buffer;
       
    83   guint len;
       
    84   GstFlowReturn ret;
       
    85 
       
    86   buffer = gst_buffer_new ();
       
    87 
       
    88   if (app->offset >= app->length) {
       
    89     /* we are EOS, send end-of-stream */
       
    90     g_signal_emit_by_name (app->appsrc, "end-of-stream", &ret);
       
    91     return;
       
    92   }
       
    93 
       
    94   /* read the next chunk */
       
    95   len = CHUNK_SIZE;
       
    96   if (app->offset + len > app->length)
       
    97     len = app->length - app->offset;
       
    98 
       
    99   GST_BUFFER_DATA (buffer) = app->data + app->offset;
       
   100   GST_BUFFER_SIZE (buffer) = len;
       
   101 
       
   102   GST_DEBUG ("feed buffer %p, offset %" G_GUINT64_FORMAT "-%u", buffer,
       
   103       app->offset, len);
       
   104   g_signal_emit_by_name (app->appsrc, "push-buffer", buffer, &ret);
       
   105   gst_buffer_unref (buffer);
       
   106 
       
   107   app->offset += len;
       
   108 
       
   109   return;
       
   110 }
       
   111 
       
   112 /* this callback is called when playbin2 has constructed a source object to read
       
   113  * from. Since we provided the appsrc:// uri to playbin2, this will be the
       
   114  * appsrc that we must handle. We set up a signals to push data into appsrc. */
       
   115 static void
       
   116 found_source (GObject * object, GObject * orig, GParamSpec * pspec, App * app)
       
   117 {
       
   118   /* get a handle to the appsrc */
       
   119   g_object_get (orig, pspec->name, &app->appsrc, NULL);
       
   120 
       
   121   GST_DEBUG ("got appsrc %p", app->appsrc);
       
   122 
       
   123   /* we can set the length in appsrc. This allows some elements to estimate the
       
   124    * total duration of the stream. It's a good idea to set the property when you
       
   125    * can but it's not required. */
       
   126   g_object_set (app->appsrc, "size", (gint64) app->length, NULL);
       
   127 
       
   128   /* configure the appsrc, we will push a buffer to appsrc when it needs more
       
   129    * data */
       
   130   g_signal_connect (app->appsrc, "need-data", G_CALLBACK (feed_data), app);
       
   131 }
       
   132 
       
   133 static gboolean
       
   134 bus_message (GstBus * bus, GstMessage * message, App * app)
       
   135 {
       
   136   GST_DEBUG ("got message %s",
       
   137       gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
       
   138 
       
   139   switch (GST_MESSAGE_TYPE (message)) {
       
   140     case GST_MESSAGE_ERROR:
       
   141       g_error ("received error");
       
   142       g_main_loop_quit (app->loop);
       
   143       break;
       
   144     case GST_MESSAGE_EOS:
       
   145       g_main_loop_quit (app->loop);
       
   146       break;
       
   147     default:
       
   148       break;
       
   149   }
       
   150   return TRUE;
       
   151 }
       
   152 
       
   153 int
       
   154 main (int argc, char *argv[])
       
   155 {
       
   156   App *app = &s_app;
       
   157   GError *error = NULL;
       
   158   GstBus *bus;
       
   159 
       
   160   gst_init (&argc, &argv);
       
   161 
       
   162   GST_DEBUG_CATEGORY_INIT (appsrc_playbin_debug, "appsrc-playbin", 0,
       
   163       "appsrc playbin example");
       
   164 
       
   165   if (argc < 2) {
       
   166     g_print ("usage: %s <filename>\n", argv[0]);
       
   167     return -1;
       
   168   }
       
   169 
       
   170   /* try to open the file as an mmapped file */
       
   171   app->file = g_mapped_file_new (argv[1], FALSE, &error);
       
   172   if (error) {
       
   173     g_print ("failed to open file: %s\n", error->message);
       
   174     g_error_free (error);
       
   175     return -2;
       
   176   }
       
   177   /* get some vitals, this will be used to read data from the mmapped file and
       
   178    * feed it to appsrc. */
       
   179   app->length = g_mapped_file_get_length (app->file);
       
   180   app->data = (guint8 *) g_mapped_file_get_contents (app->file);
       
   181   app->offset = 0;
       
   182 
       
   183   /* create a mainloop to get messages */
       
   184   app->loop = g_main_loop_new (NULL, TRUE);
       
   185 
       
   186   app->playbin = gst_element_factory_make ("playbin2", NULL);
       
   187   g_assert (app->playbin);
       
   188 
       
   189   bus = gst_pipeline_get_bus (GST_PIPELINE (app->playbin));
       
   190 
       
   191   /* add watch for messages */
       
   192   gst_bus_add_watch (bus, (GstBusFunc) bus_message, app);
       
   193 
       
   194   /* set to read from appsrc */
       
   195   g_object_set (app->playbin, "uri", "appsrc://", NULL);
       
   196 
       
   197   /* get notification when the source is created so that we get a handle to it
       
   198    * and can configure it */
       
   199   g_signal_connect (app->playbin, "deep-notify::source",
       
   200       (GCallback) found_source, app);
       
   201 
       
   202   /* go to playing and wait in a mainloop. */
       
   203   gst_element_set_state (app->playbin, GST_STATE_PLAYING);
       
   204 
       
   205   /* this mainloop is stopped when we receive an error or EOS */
       
   206   g_main_loop_run (app->loop);
       
   207 
       
   208   GST_DEBUG ("stopping");
       
   209 
       
   210   gst_element_set_state (app->playbin, GST_STATE_NULL);
       
   211 
       
   212   /* free the file */
       
   213   g_mapped_file_free (app->file);
       
   214 
       
   215   gst_object_unref (bus);
       
   216   g_main_loop_unref (app->loop);
       
   217 
       
   218   return 0;
       
   219 }