af75d69a86e3b1382bfd06616e6528c8b776a7cd
[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 (GstController * ctrl, GstStructure * prog)
72 {
73   const GstStructure *s;
74   GstInterpolationControlSource *cs;
75   GstClockTime ts, dur;
76   GValue val = { 0, };
77   gint v;
78   const GValue *frame;
79   GHashTable *css;
80   gint i, j;
81   const gchar *name;
82
83   css = g_hash_table_new (g_str_hash, g_str_equal);
84
85   g_value_init (&val, G_TYPE_INT);
86
87   ts = 0;
88   dur = gst_util_uint64_scale_int (GST_SECOND, 1, 15);
89
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));
93
94     frame =
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);
100       if (!cs) {
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);
106         g_object_unref (cs);
107       }
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);
112     }
113     ts += dur;
114   }
115
116   g_value_unset (&val);
117
118   g_hash_table_unref (css);
119 }
120
121 gint
122 main (gint argc, gchar ** argv)
123 {
124   GstElement *bin;
125   GstElement *src, *fmt, *enc, *sink;
126   GstCaps *caps;
127   GstController *ctrl;
128   GstStructure *prog;
129
130   /* init gstreamer */
131   gst_init (&argc, &argv);
132
133   /* create a new bin to hold the elements */
134   bin = gst_pipeline_new ("camera");
135
136   /* create elements */
137   if (!(sink = gst_element_factory_make ("multifilesink", NULL))) {
138     GST_WARNING ("Can't create element \"multifilesink\"");
139     return -1;
140   }
141   g_object_set (sink, "location", "image%02d.jpg", NULL);
142
143   if (!(enc = gst_element_factory_make ("jpegenc", NULL))) {
144     GST_WARNING ("Can't create element \"jpegenc\"");
145     return -1;
146   }
147
148   if (!(fmt = gst_element_factory_make ("capsfilter", NULL))) {
149     GST_WARNING ("Can't create element \"capsfilter\"");
150     return -1;
151   }
152   caps =
153       gst_caps_from_string
154       ("video/x-raw-yuv, width=640, height=480, framerate=(fraction)15/1");
155   g_object_set (fmt, "caps", caps, NULL);
156
157   if (!(src = gst_element_factory_make ("v4l2src", NULL))) {
158     GST_WARNING ("Can't create element \"v4l2src\"");
159     return -1;
160   }
161   g_object_set (src, "queue-size", 1, NULL);
162
163   /* add objects to the main bin */
164   gst_bin_add_many (GST_BIN (bin), src, fmt, enc, sink, NULL);
165
166   /* link elements */
167   if (!gst_element_link_many (src, fmt, enc, sink, NULL)) {
168     GST_WARNING ("Can't link elements");
169     return -1;
170   }
171
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");
176     return -1;
177   }
178
179   /* programm a pattern of events */
180 #if 0
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);
186 #endif
187 #if 1
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\\;\";",
193       NULL);
194 #endif
195   set_program (ctrl, prog);
196   g_object_set (src, "num-buffers", gst_structure_n_fields (prog), NULL);
197
198   /* prepare playback */
199   gst_element_set_state (bin, GST_STATE_PAUSED);
200
201   /* play and wait */
202   gst_element_set_state (bin, GST_STATE_PLAYING);
203
204   /* mainloop and wait for eos */
205   event_loop (bin);
206
207   /* stop and cleanup */
208   gst_element_set_state (bin, GST_STATE_NULL);
209   gst_object_unref (GST_OBJECT (bin));
210   return 0;
211 }