f896a0c73b972a44bc52361894a629ba6e3cf0c0
[platform/upstream/gstreamer.git] / tests / examples / thumbnails.c
1 /* GStreamer Editing Services
2  * Copyright (C) 2010 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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <glib.h>
28 #include <glib/gstdio.h>
29 #include <ges/ges.h>
30 #include <gst/pbutils/encoding-profile.h>
31
32 /* GLOBAL VARIABLE */
33 static guint repeat = 0;
34 GESTimelinePipeline *pipeline = NULL;
35
36 static gboolean thumbnail_cb (gpointer pipeline);
37
38 #define TEST_PATH "test_thumbnail.jpg"
39
40 static gboolean
41 thumbnail_cb (gpointer user)
42 {
43   GstSample *b = NULL;
44   GstCaps *caps;
45   GESTimelinePipeline *p;
46
47   p = GES_TIMELINE_PIPELINE (user);
48
49   caps = gst_caps_from_string ("image/jpeg");
50   GST_INFO ("getting thumbnails");
51
52   /* check raw rgb use-case with scaling */
53   b = ges_timeline_pipeline_get_thumbnail_rgb24 (p, 320, 240);
54   g_assert (b);
55   gst_sample_unref (b);
56
57   /* check encoding use-case from caps */
58   b = NULL;
59   b = ges_timeline_pipeline_get_thumbnail (p, caps);
60   g_assert (b);
61   gst_sample_unref (b);
62
63   g_assert (ges_timeline_pipeline_save_thumbnail (p, -1, -1, (gchar *)
64           "image/jpeg", (gchar *) TEST_PATH, NULL));
65   g_assert (g_file_test (TEST_PATH, G_FILE_TEST_EXISTS));
66   g_unlink (TEST_PATH);
67
68   gst_caps_unref (caps);
69   return FALSE;
70 }
71
72 static GESTimelinePipeline *
73 create_timeline (void)
74 {
75   GESTimelinePipeline *pipeline;
76   GESLayer *layer;
77   GESTrack *tracka, *trackv;
78   GESTimeline *timeline;
79   GESClip *src;
80
81   timeline = ges_timeline_new ();
82
83   tracka = GES_TRACK (ges_audio_track_new ());
84   trackv = GES_TRACK (ges_video_track_new ());
85
86   layer = (GESLayer *) ges_simple_layer_new ();
87
88   /* Add the tracks and the layer to the timeline */
89   if (!ges_timeline_add_layer (timeline, layer) ||
90       !ges_timeline_add_track (timeline, tracka) ||
91       !ges_timeline_add_track (timeline, trackv))
92     return NULL;
93
94   /* Add the main audio/video file */
95   src = GES_CLIP (ges_test_clip_new ());
96   g_object_set (src,
97       "vpattern", GES_VIDEO_TEST_PATTERN_SNOW,
98       "duration", 10 * GST_SECOND, NULL);
99
100   ges_simple_layer_add_object ((GESSimpleLayer *) layer, GES_CLIP (src), 0);
101
102   pipeline = ges_timeline_pipeline_new ();
103
104   if (!ges_timeline_pipeline_add_timeline (pipeline, timeline))
105     return NULL;
106
107   return pipeline;
108 }
109
110 static void
111 bus_message_cb (GstBus * bus, GstMessage * message, GMainLoop * mainloop)
112 {
113   switch (GST_MESSAGE_TYPE (message)) {
114     case GST_MESSAGE_ERROR:
115       g_print ("ERROR\n");
116       g_main_loop_quit (mainloop);
117       break;
118     case GST_MESSAGE_EOS:
119       if (repeat > 0) {
120         g_print ("Looping again\n");
121         /* No need to change state before */
122         gst_element_seek_simple (GST_ELEMENT (pipeline), GST_FORMAT_TIME,
123             GST_SEEK_FLAG_FLUSH, 0);
124         gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
125         repeat -= 1;
126       } else {
127         g_print ("Done\n");
128         g_main_loop_quit (mainloop);
129       }
130       break;
131     default:
132       break;
133   }
134 }
135
136 int
137 main (int argc, gchar ** argv)
138 {
139   GError *err = NULL;
140   GOptionEntry options[] = {
141     {NULL}
142   };
143   GOptionContext *ctx;
144   GMainLoop *mainloop;
145   GstBus *bus;
146
147   ctx = g_option_context_new ("tests thumbnail supoprt (produces no output)");
148   g_option_context_set_summary (ctx, "");
149   g_option_context_add_main_entries (ctx, options, NULL);
150   g_option_context_add_group (ctx, gst_init_get_option_group ());
151
152   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
153     g_print ("Error initializing: %s\n", err->message);
154     g_option_context_free (ctx);
155     exit (1);
156   }
157
158   g_option_context_free (ctx);
159   /* Initialize the GStreamer Editing Services */
160   ges_init ();
161
162   /* Create the pipeline */
163   pipeline = create_timeline ();
164   if (!pipeline)
165     exit (-1);
166
167   ges_timeline_pipeline_set_mode (pipeline, TIMELINE_MODE_PREVIEW);
168
169   /* Play the pipeline */
170   mainloop = g_main_loop_new (NULL, FALSE);
171
172   g_print ("thumbnailing every 1 seconds\n");
173   g_timeout_add (1000, thumbnail_cb, pipeline);
174
175   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
176   gst_bus_add_signal_watch (bus);
177   g_signal_connect (bus, "message", G_CALLBACK (bus_message_cb), mainloop);
178
179   if (gst_element_set_state (GST_ELEMENT (pipeline),
180           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
181     g_print ("Failed to start the encoding\n");
182     return 1;
183   }
184   g_main_loop_run (mainloop);
185
186   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
187   gst_object_unref (pipeline);
188
189   return 0;
190 }