2 * Copyright (C) 2010 Stefan Kost <stefan.kost@nokia.com>
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.
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.
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.
19 /* demo for using gstcontroler with camera capture for e.g. bracketing
21 * gcc `pkg-config --cflags --libs gstreamer-0.10 gstreamer-controller-0.10` camctrl.c -o camctrl
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
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
34 #include <gst/controller/gstinterpolationcontrolsource.h>
37 event_loop (GstElement * bin)
40 GstMessage *message = NULL;
42 bus = gst_element_get_bus (GST_ELEMENT (bin));
45 message = gst_bus_poll (bus, GST_MESSAGE_ANY, -1);
47 switch (message->type) {
49 gst_message_unref (message);
51 case GST_MESSAGE_WARNING:
52 case GST_MESSAGE_ERROR:{
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);
64 gst_message_unref (message);
71 set_program (GstController * ctrl, GstStructure * prog)
73 const GstStructure *s;
74 GstInterpolationControlSource *cs;
83 css = g_hash_table_new (g_str_hash, g_str_equal);
85 g_value_init (&val, G_TYPE_INT);
88 dur = gst_util_uint64_scale_int (GST_SECOND, 1, 15);
90 /* loop over each image in prog */
91 for (i = 0; i < gst_structure_n_fields (prog); i++) {
92 GST_DEBUG ("ctrl on %" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
95 gst_structure_get_value (prog, gst_structure_nth_field_name (prog, i));
96 s = gst_value_get_structure (frame);
97 for (j = 0; j < gst_structure_n_fields (s); j++) {
98 name = gst_structure_nth_field_name (s, j);
99 cs = g_hash_table_lookup (css, name);
101 cs = gst_interpolation_control_source_new ();
102 gst_controller_set_control_source (ctrl, name, GST_CONTROL_SOURCE (cs));
103 gst_interpolation_control_source_set_interpolation_mode (cs,
104 GST_INTERPOLATE_NONE);
105 g_hash_table_insert (css, (gpointer) name, cs);
108 gst_structure_get_int (s, name, &v);
109 g_value_set_int (&val, v);
110 gst_interpolation_control_source_set (cs, ts, &val);
111 GST_DEBUG (" %s = %d", name, v);
116 g_value_unset (&val);
118 g_hash_table_unref (css);
122 main (gint argc, gchar ** argv)
125 GstElement *src, *fmt, *enc, *sink;
131 gst_init (&argc, &argv);
133 /* create a new bin to hold the elements */
134 bin = gst_pipeline_new ("camera");
136 /* create elements */
137 if (!(sink = gst_element_factory_make ("multifilesink", NULL))) {
138 GST_WARNING ("Can't create element \"multifilesink\"");
141 g_object_set (sink, "location", "image%02d.jpg", NULL);
143 if (!(enc = gst_element_factory_make ("jpegenc", NULL))) {
144 GST_WARNING ("Can't create element \"jpegenc\"");
148 if (!(fmt = gst_element_factory_make ("capsfilter", NULL))) {
149 GST_WARNING ("Can't create element \"capsfilter\"");
154 ("video/x-raw-yuv, width=640, height=480, framerate=(fraction)15/1");
155 g_object_set (fmt, "caps", caps, NULL);
157 if (!(src = gst_element_factory_make ("v4l2src", NULL))) {
158 GST_WARNING ("Can't create element \"v4l2src\"");
161 g_object_set (src, "queue-size", 1, NULL);
163 /* add objects to the main bin */
164 gst_bin_add_many (GST_BIN (bin), src, fmt, enc, sink, NULL);
167 if (!gst_element_link_many (src, fmt, enc, sink, NULL)) {
168 GST_WARNING ("Can't link elements");
172 /* get the controller */
173 if (!(ctrl = gst_controller_new (GST_OBJECT (src), "brightness", "contrast",
174 "saturation", NULL))) {
175 GST_WARNING ("can't control source element");
179 /* programm a pattern of events */
181 prog = gst_structure_from_string ("program"
182 ", image00=(structure)\"image\\,contrast\\=0\\;\""
183 ", image01=(structure)\"image\\,contrast\\=79\\;\""
184 ", image02=(structure)\"image\\,contrast\\=255\\;\""
185 ", image03=(structure)\"image\\,contrast\\=15\\;\";", NULL);
188 prog = gst_structure_from_string ("program"
189 ", image00=(structure)\"image\\,brightness\\=255\\,contrast\\=0\\;\""
190 ", image01=(structure)\"image\\,brightness\\=127\\,contrast\\=79\\;\""
191 ", image02=(structure)\"image\\,brightness\\=64\\,contrast\\=255\\;\""
192 ", image03=(structure)\"image\\,brightness\\=0\\,contrast\\=15\\;\";",
195 set_program (ctrl, prog);
196 g_object_set (src, "num-buffers", gst_structure_n_fields (prog), NULL);
198 /* prepare playback */
199 gst_element_set_state (bin, GST_STATE_PAUSED);
202 gst_element_set_state (bin, GST_STATE_PLAYING);
204 /* mainloop and wait for eos */
207 /* stop and cleanup */
208 gst_element_set_state (bin, GST_STATE_NULL);
209 gst_object_unref (GST_OBJECT (bin));