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