controller: adapt to control binding changes
[platform/upstream/gst-plugins-good.git] / tests / examples / v4l2 / camctrl.c
1 /* GStreamer
2  * Copyright (C) 2010 Stefan Kost <stefan.kost@nokia.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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 /* demo for using gstcontroler with camera capture for e.g. bracketing
20  *
21  * gcc `pkg-config --cflags --libs gstreamer-0.10 gstreamer-controller-0.10` camctrl.c -o camctrl
22  *
23  * TODO:
24  * - handle stream status and switch capture thread to SCHED_RR/FIFO
25  * - the queue-size controls the controler offset
26  *   - right now we work with 1 queued picture and thus active settings for next
27  *     frame
28  * - we want some feedback about how precisely a program can be realized
29  *   - we might want to adjust the framerate to handle hardware limmits
30  * - we e.g. can't change resolution per frame right now
31  */
32
33 #include <gst/gst.h>
34 #include <gst/controller/gstinterpolationcontrolsource.h>
35
36 static void
37 event_loop (GstElement * bin)
38 {
39   GstBus *bus;
40   GstMessage *message = NULL;
41
42   bus = gst_element_get_bus (GST_ELEMENT (bin));
43
44   while (TRUE) {
45     message = gst_bus_poll (bus, GST_MESSAGE_ANY, -1);
46
47     switch (message->type) {
48       case GST_MESSAGE_EOS:
49         gst_message_unref (message);
50         return;
51       case GST_MESSAGE_WARNING:
52       case GST_MESSAGE_ERROR:{
53         GError *gerror;
54         gchar *debug;
55
56         gst_message_parse_error (message, &gerror, &debug);
57         gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
58         gst_message_unref (message);
59         g_error_free (gerror);
60         g_free (debug);
61         return;
62       }
63       default:
64         gst_message_unref (message);
65         break;
66     }
67   }
68 }
69
70 static void
71 set_program (GstObject * elem, GstStructure * prog)
72 {
73   const GstStructure *s;
74   GstInterpolationControlSource *cs;
75   GstClockTime ts, dur;
76   gdouble v;
77   const GValue *frame;
78   GHashTable *css;
79   gint i, j;
80   const gchar *name;
81
82   css = g_hash_table_new (g_str_hash, g_str_equal);
83
84   ts = 0;
85   dur = gst_util_uint64_scale_int (GST_SECOND, 1, 15);
86
87   /* loop over each image in prog */
88   for (i = 0; i < gst_structure_n_fields (prog); i++) {
89     GST_DEBUG ("ctrl on %" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
90
91     frame =
92         gst_structure_get_value (prog, gst_structure_nth_field_name (prog, i));
93     s = gst_value_get_structure (frame);
94     for (j = 0; j < gst_structure_n_fields (s); j++) {
95       name = gst_structure_nth_field_name (s, j);
96       cs = g_hash_table_lookup (css, name);
97       if (!cs) {
98         cs = gst_interpolation_control_source_new ();
99         gst_object_add_control_binding (elem,
100             gst_control_binding_new (elem, name, GST_CONTROL_SOURCE (cs)));
101         g_object_set (cs, "mode", GST_INTERPOLATION_MODE_NONE, NULL);
102         g_hash_table_insert (css, (gpointer) name, cs);
103         gst_object_unref (cs);
104       }
105       gst_structure_get_double (s, name, &v);
106       gst_timed_value_control_source_set ((GstTimedValueControlSource *) cs, ts,
107           v);
108       GST_DEBUG ("  %s = %lf", name, v);
109     }
110     ts += dur;
111   }
112
113   g_hash_table_unref (css);
114 }
115
116 gint
117 main (gint argc, gchar ** argv)
118 {
119   GstElement *bin;
120   GstElement *src, *fmt, *enc, *sink;
121   GstCaps *caps;
122   GstStructure *prog;
123
124   /* init gstreamer */
125   gst_init (&argc, &argv);
126
127   /* create a new bin to hold the elements */
128   bin = gst_pipeline_new ("camera");
129
130   /* create elements */
131   if (!(sink = gst_element_factory_make ("multifilesink", NULL))) {
132     GST_WARNING ("Can't create element \"multifilesink\"");
133     return -1;
134   }
135   g_object_set (sink, "location", "image%02d.jpg", NULL);
136
137   if (!(enc = gst_element_factory_make ("jpegenc", NULL))) {
138     GST_WARNING ("Can't create element \"jpegenc\"");
139     return -1;
140   }
141
142   if (!(fmt = gst_element_factory_make ("capsfilter", NULL))) {
143     GST_WARNING ("Can't create element \"capsfilter\"");
144     return -1;
145   }
146   caps =
147       gst_caps_from_string
148       ("video/x-raw-yuv, width=640, height=480, framerate=(fraction)15/1");
149   g_object_set (fmt, "caps", caps, NULL);
150
151   if (!(src = gst_element_factory_make ("v4l2src", NULL))) {
152     GST_WARNING ("Can't create element \"v4l2src\"");
153     return -1;
154   }
155   g_object_set (src, "queue-size", 1, NULL);
156
157   /* add objects to the main bin */
158   gst_bin_add_many (GST_BIN (bin), src, fmt, enc, sink, NULL);
159
160   /* link elements */
161   if (!gst_element_link_many (src, fmt, enc, sink, NULL)) {
162     GST_WARNING ("Can't link elements");
163     return -1;
164   }
165
166   /* programm a pattern of events */
167 #if 0
168   prog = gst_structure_from_string ("program"
169       ", image00=(structure)\"image\\,contrast\\=0.0\\;\""
170       ", image01=(structure)\"image\\,contrast\\=0.3\\;\""
171       ", image02=(structure)\"image\\,contrast\\=1.0\\;\""
172       ", image03=(structure)\"image\\,contrast\\=0.05\\;\";", NULL);
173 #endif
174 #if 1
175   prog = gst_structure_from_string ("program"
176       ", image00=(structure)\"image\\,brightness\\=1.0\\,contrast\\=0.0\\;\""
177       ", image01=(structure)\"image\\,brightness\\=0.5\\,contrast\\=0.3\\;\""
178       ", image02=(structure)\"image\\,brightness\\=0.25\\,contrast\\=1.0\\;\""
179       ", image03=(structure)\"image\\,brightness\\=0.0\\,contrast\\=0.05\\;\";",
180       NULL);
181 #endif
182   set_program (GST_OBJECT (src), prog);
183   g_object_set (src, "num-buffers", gst_structure_n_fields (prog), NULL);
184
185   /* prepare playback */
186   gst_element_set_state (bin, GST_STATE_PAUSED);
187
188   /* play and wait */
189   gst_element_set_state (bin, GST_STATE_PLAYING);
190
191   /* mainloop and wait for eos */
192   event_loop (bin);
193
194   /* stop and cleanup */
195   gst_element_set_state (bin, GST_STATE_NULL);
196   gst_object_unref (GST_OBJECT (bin));
197   return 0;
198 }