1 /* GStreamer
2  *
3  * Copyright (C) 2010 Wim Taymans <wim.taymans@collabora.co.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 /* An example of synchronized playback and recording.
22  * The trick is to wait for the playback pipeline to preroll before starting
23  * playback and recording.
24  */
25 #include <string.h>
26 
27 #include <gst/gst.h>
28 
29 /* Define to run the asynchronous version, which has the benefit that it doesn't
30  * block the main thread but it produces slightly less clear code. */
31 #define ASYNC_VERSION
32 
33 static GMainLoop *loop;
34 static GstElement *pipeline = NULL;
35 static GstElement *play_bin, *play_source, *play_sink;
36 static GstElement *rec_bin, *rec_source, *rec_sink;
37 
38 static gboolean
message_handler(GstBus * bus,GstMessage * message,gpointer user_data)39 message_handler (GstBus * bus, GstMessage * message, gpointer user_data)
40 {
41 
42   switch (GST_MESSAGE_TYPE (message)) {
43 #ifdef ASYNC_VERSION
44     case GST_MESSAGE_ELEMENT:{
45       const GstStructure *str;
46 
47       str = gst_message_get_structure (message);
48 
49       if (gst_structure_has_name (str, "GstBinForwarded")) {
50         GstMessage *orig;
51 
52         /* unwrap the element message */
53         gst_structure_get (str, "message", GST_TYPE_MESSAGE, &orig, NULL);
54         g_assert (orig);
55 
56         switch (GST_MESSAGE_TYPE (orig)) {
57           case GST_MESSAGE_ASYNC_DONE:
58             g_print ("ASYNC done %s\n", GST_MESSAGE_SRC_NAME (orig));
59             if (GST_MESSAGE_SRC (orig) == GST_OBJECT_CAST (play_bin)) {
60               g_print
61                   ("prerolled, starting synchronized playback and recording\n");
62               /* returns ASYNC because the sink linked to the live source is not
63                * prerolled */
64               if (gst_element_set_state (pipeline,
65                       GST_STATE_PLAYING) != GST_STATE_CHANGE_ASYNC) {
66                 g_warning ("State change failed");
67               }
68             }
69             break;
70           default:
71             break;
72         }
73       }
74       break;
75     }
76 #endif
77     case GST_MESSAGE_EOS:
78       g_print ("EOS\n");
79       g_main_loop_quit (loop);
80       break;
81     case GST_MESSAGE_ERROR:{
82       GError *err = NULL;
83 
84       gst_message_parse_error (message, &err, NULL);
85       g_print ("error: %s\n", err->message);
86       g_clear_error (&err);
87 
88       g_main_loop_quit (loop);
89       break;
90     }
91     default:
92       break;
93   }
94 
95   return TRUE;
96 }
97 
98 int
main(int argc,char * argv[])99 main (int argc, char *argv[])
100 {
101   GstBus *bus;
102   gint watch_id;
103 
104   gst_init (NULL, NULL);
105 
106   loop = g_main_loop_new (NULL, TRUE);
107 
108   pipeline = gst_pipeline_new ("pipeline");
109 #ifdef ASYNC_VERSION
110   /* this enables messages of individual elements inside the pipeline */
111   g_object_set (pipeline, "message-forward", TRUE, NULL);
112 #endif
113 
114   /* make a bin with the playback elements this is a non-live pipeline */
115   play_bin = gst_bin_new ("play_bin");
116   play_source = gst_element_factory_make ("audiotestsrc", "play_source");
117   play_sink = gst_element_factory_make ("autoaudiosink", "play_sink");
118 
119   gst_bin_add (GST_BIN (play_bin), play_source);
120   gst_bin_add (GST_BIN (play_bin), play_sink);
121 
122   gst_element_link (play_source, play_sink);
123 
124   /* make bin with the record elements, this is a live pipeline */
125   rec_bin = gst_bin_new ("rec_bin");
126   rec_source = gst_element_factory_make ("autoaudiosrc", "rec_source");
127   rec_sink = gst_element_factory_make ("fakesink", "rec_sink");
128 
129   gst_bin_add (GST_BIN (rec_bin), rec_source);
130   gst_bin_add (GST_BIN (rec_bin), rec_sink);
131 
132   gst_element_link (rec_source, rec_sink);
133 
134   gst_bin_add (GST_BIN (pipeline), play_bin);
135   gst_bin_add (GST_BIN (pipeline), rec_bin);
136 
137   bus = gst_element_get_bus (pipeline);
138   watch_id = gst_bus_add_watch (bus, message_handler, NULL);
139   gst_object_unref (bus);
140 
141   g_print ("going to PAUSED\n");
142   /* returns NO_PREROLL because we have a live element */
143   if (gst_element_set_state (pipeline,
144           GST_STATE_PAUSED) != GST_STATE_CHANGE_NO_PREROLL) {
145     g_warning ("Expected state change NO_PREROLL result");
146   }
147 
148   g_print ("waiting for playback preroll\n");
149 #ifndef ASYNC_VERSION
150   /* sync wait for preroll on the playback bin and then go to PLAYING */
151   if (gst_element_get_state (play_bin, NULL, NULL,
152           GST_CLOCK_TIME_NONE) != GST_STATE_CHANGE_SUCCESS) {
153     g_warning ("Error while waiting for state change");
154   }
155   g_print ("prerolled, starting synchronized playback and recording\n");
156   /* returns ASYNC because the sink linked to the live source is not
157    * prerolled */
158   if (gst_element_set_state (pipeline,
159           GST_STATE_PLAYING) != GST_STATE_CHANGE_ASYNC) {
160     g_warning ("Expected state change NO_PREROLL result");
161   }
162 #endif
163 
164   g_main_loop_run (loop);
165 
166   g_source_remove (watch_id);
167   gst_element_set_state (pipeline, GST_STATE_NULL);
168   gst_object_unref (pipeline);
169 
170   g_main_loop_unref (loop);
171 
172   return 0;
173 }
174