summaryrefslogtreecommitdiff
path: root/tests/examples/playrec/playrec.c
blob: 9f79a038b2b8080c1ab35cfeef3f4059e88f2986 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* GStreamer
 *
 * Copyright (C) 2010 Wim Taymans <wim.taymans@collabora.co.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* An example of synchronized playback and recording.
 * The trick is to wait for the playback pipeline to preroll before starting
 * playback and recording.
 */
#include <string.h>

#include <gst/gst.h>

/* Define to run the asynchronous version. This requires 0.10.31 of the
 * GStreamer core. The async version has the benefit that it doesn't block the
 * main thread but it produces slightly less clear code. */
#define ASYNC_VERSION

static GMainLoop *loop;
static GstElement *pipeline = NULL;
static GstElement *play_bin, *play_source, *play_sink;
static GstElement *rec_bin, *rec_source, *rec_sink;

static gboolean
message_handler (GstBus * bus, GstMessage * message, gpointer user_data)
{

  switch (GST_MESSAGE_TYPE (message)) {
#ifdef ASYNC_VERSION
    case GST_MESSAGE_ELEMENT:{
      const GstStructure *str;

      str = gst_message_get_structure (message);

      if (gst_structure_has_name (str, "GstBinForwarded")) {
        GstMessage *orig;

        /* unwrap the element message */
        gst_structure_get (str, "message", GST_TYPE_MESSAGE, &orig, NULL);
        g_assert (orig);

        switch (GST_MESSAGE_TYPE (orig)) {
          case GST_MESSAGE_ASYNC_DONE:
            g_print ("ASYNC done %s\n", GST_MESSAGE_SRC_NAME (orig));
            if (GST_MESSAGE_SRC (orig) == GST_OBJECT_CAST (play_bin)) {
              g_print
                  ("prerolled, starting synchronized playback and recording\n");
              /* returns ASYNC because the sink linked to the live source is not
               * prerolled */
              if (gst_element_set_state (pipeline,
                      GST_STATE_PLAYING) != GST_STATE_CHANGE_ASYNC) {
                g_warning ("State change failed");
              }
            }
            break;
          default:
            break;
        }
      }
      break;
    }
#endif
    case GST_MESSAGE_EOS:
      g_print ("EOS\n");
      g_main_loop_quit (loop);
      break;
    case GST_MESSAGE_ERROR:{
      GError *err = NULL;

      gst_message_parse_error (message, &err, NULL);
      g_print ("error: %s\n", err->message);
      g_clear_error (&err);

      g_main_loop_quit (loop);
      break;
    }
    default:
      break;
  }

  return TRUE;
}

int
main (int argc, char *argv[])
{
  GstBus *bus;
  gint watch_id;

  gst_init (NULL, NULL);

  loop = g_main_loop_new (NULL, TRUE);

  pipeline = gst_pipeline_new ("pipeline");
#ifdef ASYNC_VERSION
  /* this enables messages of individual elements inside the pipeline */
  g_object_set (pipeline, "message-forward", TRUE, NULL);
#endif

  /* make a bin with the playback elements this is a non-live pipeline */
  play_bin = gst_bin_new ("play_bin");
  play_source = gst_element_factory_make ("audiotestsrc", "play_source");
  play_sink = gst_element_factory_make ("autoaudiosink", "play_sink");

  gst_bin_add (GST_BIN (play_bin), play_source);
  gst_bin_add (GST_BIN (play_bin), play_sink);

  gst_element_link (play_source, play_sink);

  /* make bin with the record elements, this is a live pipeline */
  rec_bin = gst_bin_new ("rec_bin");
  rec_source = gst_element_factory_make ("autoaudiosrc", "rec_source");
  rec_sink = gst_element_factory_make ("fakesink", "rec_sink");

  gst_bin_add (GST_BIN (rec_bin), rec_source);
  gst_bin_add (GST_BIN (rec_bin), rec_sink);

  gst_element_link (rec_source, rec_sink);

  gst_bin_add (GST_BIN (pipeline), play_bin);
  gst_bin_add (GST_BIN (pipeline), rec_bin);

  bus = gst_element_get_bus (pipeline);
  watch_id = gst_bus_add_watch (bus, message_handler, NULL);
  gst_object_unref (bus);

  g_print ("going to PAUSED\n");
  /* returns NO_PREROLL because we have a live element */
  if (gst_element_set_state (pipeline,
          GST_STATE_PAUSED) != GST_STATE_CHANGE_NO_PREROLL) {
    g_warning ("Expected state change NO_PREROLL result");
  }

  g_print ("waiting for playback preroll\n");
#ifndef ASYNC_VERSION
  /* sync wait for preroll on the playback bin and then go to PLAYING */
  if (gst_element_get_state (play_bin, NULL, NULL,
          GST_CLOCK_TIME_NONE) != GST_STATE_CHANGE_SUCCESS) {
    g_warning ("Error while waiting for state change");
  }
  g_print ("prerolled, starting synchronized playback and recording\n");
  /* returns ASYNC because the sink linked to the live source is not
   * prerolled */
  if (gst_element_set_state (pipeline,
          GST_STATE_PLAYING) != GST_STATE_CHANGE_ASYNC) {
    g_warning ("Expected state change NO_PREROLL result");
  }
#endif

  g_main_loop_run (loop);

  g_source_remove (watch_id);
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);

  g_main_loop_unref (loop);

  return 0;
}