Blame tests/examples/playrec/playrec.c

Packit 971217
/* GStreamer
Packit 971217
 *
Packit 971217
 * Copyright (C) 2010 Wim Taymans <wim.taymans@collabora.co.uk>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
/* An example of synchronized playback and recording.
Packit 971217
 * The trick is to wait for the playback pipeline to preroll before starting
Packit 971217
 * playback and recording.
Packit 971217
 */
Packit 971217
#include <string.h>
Packit 971217
Packit 971217
#include <gst/gst.h>
Packit 971217
Packit 971217
/* Define to run the asynchronous version, which has the benefit that it doesn't
Packit 971217
 * block the main thread but it produces slightly less clear code. */
Packit 971217
#define ASYNC_VERSION
Packit 971217
Packit 971217
static GMainLoop *loop;
Packit 971217
static GstElement *pipeline = NULL;
Packit 971217
static GstElement *play_bin, *play_source, *play_sink;
Packit 971217
static GstElement *rec_bin, *rec_source, *rec_sink;
Packit 971217
Packit 971217
static gboolean
Packit 971217
message_handler (GstBus * bus, GstMessage * message, gpointer user_data)
Packit 971217
{
Packit 971217
Packit 971217
  switch (GST_MESSAGE_TYPE (message)) {
Packit 971217
#ifdef ASYNC_VERSION
Packit 971217
    case GST_MESSAGE_ELEMENT:{
Packit 971217
      const GstStructure *str;
Packit 971217
Packit 971217
      str = gst_message_get_structure (message);
Packit 971217
Packit 971217
      if (gst_structure_has_name (str, "GstBinForwarded")) {
Packit 971217
        GstMessage *orig;
Packit 971217
Packit 971217
        /* unwrap the element message */
Packit 971217
        gst_structure_get (str, "message", GST_TYPE_MESSAGE, &orig, NULL);
Packit 971217
        g_assert (orig);
Packit 971217
Packit 971217
        switch (GST_MESSAGE_TYPE (orig)) {
Packit 971217
          case GST_MESSAGE_ASYNC_DONE:
Packit 971217
            g_print ("ASYNC done %s\n", GST_MESSAGE_SRC_NAME (orig));
Packit 971217
            if (GST_MESSAGE_SRC (orig) == GST_OBJECT_CAST (play_bin)) {
Packit 971217
              g_print
Packit 971217
                  ("prerolled, starting synchronized playback and recording\n");
Packit 971217
              /* returns ASYNC because the sink linked to the live source is not
Packit 971217
               * prerolled */
Packit 971217
              if (gst_element_set_state (pipeline,
Packit 971217
                      GST_STATE_PLAYING) != GST_STATE_CHANGE_ASYNC) {
Packit 971217
                g_warning ("State change failed");
Packit 971217
              }
Packit 971217
            }
Packit 971217
            break;
Packit 971217
          default:
Packit 971217
            break;
Packit 971217
        }
Packit 971217
      }
Packit 971217
      break;
Packit 971217
    }
Packit 971217
#endif
Packit 971217
    case GST_MESSAGE_EOS:
Packit 971217
      g_print ("EOS\n");
Packit 971217
      g_main_loop_quit (loop);
Packit 971217
      break;
Packit 971217
    case GST_MESSAGE_ERROR:{
Packit 971217
      GError *err = NULL;
Packit 971217
Packit 971217
      gst_message_parse_error (message, &err, NULL);
Packit 971217
      g_print ("error: %s\n", err->message);
Packit 971217
      g_clear_error (&err;;
Packit 971217
Packit 971217
      g_main_loop_quit (loop);
Packit 971217
      break;
Packit 971217
    }
Packit 971217
    default:
Packit 971217
      break;
Packit 971217
  }
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
int
Packit 971217
main (int argc, char *argv[])
Packit 971217
{
Packit 971217
  GstBus *bus;
Packit 971217
  gint watch_id;
Packit 971217
Packit 971217
  gst_init (NULL, NULL);
Packit 971217
Packit 971217
  loop = g_main_loop_new (NULL, TRUE);
Packit 971217
Packit 971217
  pipeline = gst_pipeline_new ("pipeline");
Packit 971217
#ifdef ASYNC_VERSION
Packit 971217
  /* this enables messages of individual elements inside the pipeline */
Packit 971217
  g_object_set (pipeline, "message-forward", TRUE, NULL);
Packit 971217
#endif
Packit 971217
Packit 971217
  /* make a bin with the playback elements this is a non-live pipeline */
Packit 971217
  play_bin = gst_bin_new ("play_bin");
Packit 971217
  play_source = gst_element_factory_make ("audiotestsrc", "play_source");
Packit 971217
  play_sink = gst_element_factory_make ("autoaudiosink", "play_sink");
Packit 971217
Packit 971217
  gst_bin_add (GST_BIN (play_bin), play_source);
Packit 971217
  gst_bin_add (GST_BIN (play_bin), play_sink);
Packit 971217
Packit 971217
  gst_element_link (play_source, play_sink);
Packit 971217
Packit 971217
  /* make bin with the record elements, this is a live pipeline */
Packit 971217
  rec_bin = gst_bin_new ("rec_bin");
Packit 971217
  rec_source = gst_element_factory_make ("autoaudiosrc", "rec_source");
Packit 971217
  rec_sink = gst_element_factory_make ("fakesink", "rec_sink");
Packit 971217
Packit 971217
  gst_bin_add (GST_BIN (rec_bin), rec_source);
Packit 971217
  gst_bin_add (GST_BIN (rec_bin), rec_sink);
Packit 971217
Packit 971217
  gst_element_link (rec_source, rec_sink);
Packit 971217
Packit 971217
  gst_bin_add (GST_BIN (pipeline), play_bin);
Packit 971217
  gst_bin_add (GST_BIN (pipeline), rec_bin);
Packit 971217
Packit 971217
  bus = gst_element_get_bus (pipeline);
Packit 971217
  watch_id = gst_bus_add_watch (bus, message_handler, NULL);
Packit 971217
  gst_object_unref (bus);
Packit 971217
Packit 971217
  g_print ("going to PAUSED\n");
Packit 971217
  /* returns NO_PREROLL because we have a live element */
Packit 971217
  if (gst_element_set_state (pipeline,
Packit 971217
          GST_STATE_PAUSED) != GST_STATE_CHANGE_NO_PREROLL) {
Packit 971217
    g_warning ("Expected state change NO_PREROLL result");
Packit 971217
  }
Packit 971217
Packit 971217
  g_print ("waiting for playback preroll\n");
Packit 971217
#ifndef ASYNC_VERSION
Packit 971217
  /* sync wait for preroll on the playback bin and then go to PLAYING */
Packit 971217
  if (gst_element_get_state (play_bin, NULL, NULL,
Packit 971217
          GST_CLOCK_TIME_NONE) != GST_STATE_CHANGE_SUCCESS) {
Packit 971217
    g_warning ("Error while waiting for state change");
Packit 971217
  }
Packit 971217
  g_print ("prerolled, starting synchronized playback and recording\n");
Packit 971217
  /* returns ASYNC because the sink linked to the live source is not
Packit 971217
   * prerolled */
Packit 971217
  if (gst_element_set_state (pipeline,
Packit 971217
          GST_STATE_PLAYING) != GST_STATE_CHANGE_ASYNC) {
Packit 971217
    g_warning ("Expected state change NO_PREROLL result");
Packit 971217
  }
Packit 971217
#endif
Packit 971217
Packit 971217
  g_main_loop_run (loop);
Packit 971217
Packit 971217
  g_source_remove (watch_id);
Packit 971217
  gst_element_set_state (pipeline, GST_STATE_NULL);
Packit 971217
  gst_object_unref (pipeline);
Packit 971217
Packit 971217
  g_main_loop_unref (loop);
Packit 971217
Packit 971217
  return 0;
Packit 971217
}