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 (GstObject * elem, 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_object_set_control_source (elem, 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;
130 gst_init (&argc, &argv);
132 /* create a new bin to hold the elements */
133 bin = gst_pipeline_new ("camera");
135 /* create elements */
136 if (!(sink = gst_element_factory_make ("multifilesink", NULL))) {
137 GST_WARNING ("Can't create element \"multifilesink\"");
140 g_object_set (sink, "location", "image%02d.jpg", NULL);
142 if (!(enc = gst_element_factory_make ("jpegenc", NULL))) {
143 GST_WARNING ("Can't create element \"jpegenc\"");
147 if (!(fmt = gst_element_factory_make ("capsfilter", NULL))) {
148 GST_WARNING ("Can't create element \"capsfilter\"");
153 ("video/x-raw-yuv, width=640, height=480, framerate=(fraction)15/1");
154 g_object_set (fmt, "caps", caps, NULL);
156 if (!(src = gst_element_factory_make ("v4l2src", NULL))) {
157 GST_WARNING ("Can't create element \"v4l2src\"");
160 g_object_set (src, "queue-size", 1, NULL);
162 /* add objects to the main bin */
163 gst_bin_add_many (GST_BIN (bin), src, fmt, enc, sink, NULL);
166 if (!gst_element_link_many (src, fmt, enc, sink, NULL)) {
167 GST_WARNING ("Can't link elements");
171 /* get the controller */
172 if (!gst_object_control_properties (GST_OBJECT (src), "brightness",
173 "contrast", "saturation", NULL)) {
174 GST_WARNING ("can't control source element");
178 /* programm a pattern of events */
180 prog = gst_structure_from_string ("program"
181 ", image00=(structure)\"image\\,contrast\\=0\\;\""
182 ", image01=(structure)\"image\\,contrast\\=79\\;\""
183 ", image02=(structure)\"image\\,contrast\\=255\\;\""
184 ", image03=(structure)\"image\\,contrast\\=15\\;\";", NULL);
187 prog = gst_structure_from_string ("program"
188 ", image00=(structure)\"image\\,brightness\\=255\\,contrast\\=0\\;\""
189 ", image01=(structure)\"image\\,brightness\\=127\\,contrast\\=79\\;\""
190 ", image02=(structure)\"image\\,brightness\\=64\\,contrast\\=255\\;\""
191 ", image03=(structure)\"image\\,brightness\\=0\\,contrast\\=15\\;\";",
194 set_program (GST_OBJECT (src), prog);
195 g_object_set (src, "num-buffers", gst_structure_n_fields (prog), NULL);
197 /* prepare playback */
198 gst_element_set_state (bin, GST_STATE_PAUSED);
201 gst_element_set_state (bin, GST_STATE_PLAYING);
203 /* mainloop and wait for eos */
206 /* stop and cleanup */
207 gst_element_set_state (bin, GST_STATE_NULL);
208 gst_object_unref (GST_OBJECT (bin));