ges: Port to gst_print*
[platform/upstream/gst-editing-services.git] / examples / c / test4.c
1 /* GStreamer Editing Services
2  * Copyright (C) 2009 Edward Hervey <bilboed@bilboed.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include <ges/ges.h>
21 #include <gst/pbutils/encoding-profile.h>
22
23 GstEncodingProfile *make_encoding_profile (gchar * audio, gchar * container);
24
25 /* This example will take a series of files and create a audio-only timeline
26  * containing the first second of each file and render it to the output uri
27  * using ogg/vorbis */
28
29 /* make_encoding_profile
30  * simple method creating an encoding profile. This is here in
31  * order not to clutter the main function. */
32 GstEncodingProfile *
33 make_encoding_profile (gchar * audio, gchar * container)
34 {
35   GstEncodingContainerProfile *profile;
36   GstEncodingProfile *stream;
37   GstCaps *caps;
38
39   caps = gst_caps_from_string (container);
40   profile =
41       gst_encoding_container_profile_new ((gchar *) "ges-test4", NULL, caps,
42       NULL);
43   gst_caps_unref (caps);
44
45   caps = gst_caps_from_string (audio);
46   stream = (GstEncodingProfile *)
47       gst_encoding_audio_profile_new (caps, NULL, NULL, 0);
48   gst_encoding_container_profile_add_profile (profile, stream);
49   gst_caps_unref (caps);
50
51   return (GstEncodingProfile *) profile;
52 }
53
54 int
55 main (int argc, gchar ** argv)
56 {
57   GESPipeline *pipeline;
58   GESTimeline *timeline;
59   GESTrack *tracka;
60   GESLayer *layer;
61   GMainLoop *mainloop;
62   GstEncodingProfile *profile;
63   gchar *container = (gchar *) "application/ogg";
64   gchar *audio = (gchar *) "audio/x-vorbis";
65   gchar *output_uri;
66   guint i;
67   GError *err = NULL;
68   GOptionEntry options[] = {
69     {"format", 'f', 0, G_OPTION_ARG_STRING, &container,
70         "Container format", "<GstCaps>"},
71     {"aformat", 'a', 0, G_OPTION_ARG_STRING, &audio,
72         "Audio format", "<GstCaps>"},
73     {NULL}
74   };
75   GOptionContext *ctx;
76
77   ctx = g_option_context_new ("- renders a sequence of audio files.");
78   g_option_context_add_main_entries (ctx, options, NULL);
79   g_option_context_add_group (ctx, gst_init_get_option_group ());
80
81   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
82     gst_printerr ("Error initializing: %s\n", err->message);
83     g_option_context_free (ctx);
84     g_clear_error (&err);
85     return -1;
86   }
87   g_option_context_free (ctx);
88
89   if (argc < 3) {
90     gst_print ("Usage: %s <output uri> <list of audio files>\n", argv[0]);
91     return -1;
92   }
93
94   /* Initialize GStreamer (this will parse environment variables and commandline
95    * arguments. */
96   gst_init (&argc, &argv);
97
98   /* Initialize the GStreamer Editing Services */
99   ges_init ();
100
101   /* Setup of an audio timeline */
102
103   /* This is our main GESTimeline */
104   timeline = ges_timeline_new ();
105
106   tracka = GES_TRACK (ges_audio_track_new ());
107
108   /* We are only going to be doing one layer of clips */
109   layer = ges_layer_new ();
110
111   /* Add the tracks and the layer to the timeline */
112   if (!ges_timeline_add_layer (timeline, layer))
113     return -1;
114   if (!ges_timeline_add_track (timeline, tracka))
115     return -1;
116
117   /* Here we've finished initializing our timeline, we're
118    * ready to start using it... by solely working with the layer ! */
119
120   for (i = 2; i < argc; i++) {
121     gchar *uri = gst_filename_to_uri (argv[i], NULL);
122     GESUriClip *src = ges_uri_clip_new (uri);
123
124     g_assert (src);
125     g_free (uri);
126
127     g_object_set (src, "start", ges_layer_get_duration (layer),
128         "duration", GST_SECOND, NULL);
129     /* Since we're using a GESSimpleLayer, objects will be automatically
130      * appended to the end of the layer */
131     ges_layer_add_clip (layer, (GESClip *) src);
132   }
133
134   /* In order to view our timeline, let's grab a convenience pipeline to put
135    * our timeline in. */
136   pipeline = ges_pipeline_new ();
137
138   /* Add the timeline to that pipeline */
139   if (!ges_pipeline_set_timeline (pipeline, timeline))
140     return -1;
141
142
143   /* RENDER SETTINGS ! */
144   /* We set our output URI and rendering setting on the pipeline */
145   if (gst_uri_is_valid (argv[1])) {
146     output_uri = g_strdup (argv[1]);
147   } else if (g_file_test (argv[1], G_FILE_TEST_EXISTS)) {
148     output_uri = gst_filename_to_uri (argv[1], NULL);
149   } else {
150     gst_printerr ("Unrecognised command line argument '%s'.\n"
151         "Please pass an URI or file as argument!\n", argv[1]);
152     return -1;
153   }
154   profile = make_encoding_profile (audio, container);
155   if (!ges_pipeline_set_render_settings (pipeline, output_uri, profile))
156     return -1;
157
158   /* We want the pipeline to render (without any preview) */
159   if (!ges_pipeline_set_mode (pipeline, GES_PIPELINE_MODE_SMART_RENDER))
160     return -1;
161
162
163   /* The following is standard usage of a GStreamer pipeline (note how you haven't
164    * had to care about GStreamer so far ?).
165    *
166    * We set the pipeline to playing ... */
167   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
168
169   /* ... and we start a GMainLoop. GES **REQUIRES** a GMainLoop to be running in
170    * order to function properly ! */
171   mainloop = g_main_loop_new (NULL, FALSE);
172
173   /* Simple code to have the mainloop shutdown after 4s */
174   /* FIXME : We should wait for EOS ! */
175   g_timeout_add_seconds (argc - 1, (GSourceFunc) g_main_loop_quit, mainloop);
176   g_main_loop_run (mainloop);
177
178   return 0;
179 }