1 /* |
|
2 * GStreamer |
|
3 * Copyright (C) 2008 Nokia Corporation <multimedia@maemo.org> |
|
4 * |
|
5 * This library is free software; you can redistribute it and/or |
|
6 * modify it under the terms of the GNU Library General Public |
|
7 * License as published by the Free Software Foundation; either |
|
8 * version 2 of the License, or (at your option) any later version. |
|
9 * |
|
10 * This library is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 * Library General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU Library General Public |
|
16 * License along with this library; if not, write to the |
|
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
18 * Boston, MA 02111-1307, USA. |
|
19 */ |
|
20 |
|
21 /** |
|
22 * SECTION:camerabinimage |
|
23 * @short_description: image capturing module of #GstCameraBin |
|
24 * |
|
25 * <refsect2> |
|
26 * <para> |
|
27 * |
|
28 * The pipeline for this module is: |
|
29 * |
|
30 * <informalexample> |
|
31 * <programlisting> |
|
32 *----------------------------------------------------------------------------- |
|
33 * (src0) -> queue -> |
|
34 * -> [post proc] -> tee < |
|
35 * (src1) -> imageenc -> metadatamuxer -> filesink |
|
36 *----------------------------------------------------------------------------- |
|
37 * </programlisting> |
|
38 * </informalexample> |
|
39 * |
|
40 * The property of elements are: |
|
41 * |
|
42 * queue - "max-size-buffers", 1, "leaky", 2, |
|
43 * |
|
44 * The image bin opens file for image writing in READY to PAUSED state change. |
|
45 * The image bin closes the file in PAUSED to READY state change. |
|
46 * |
|
47 * </para> |
|
48 * </refsect2> |
|
49 */ |
|
50 |
|
51 /* |
|
52 * includes |
|
53 */ |
|
54 |
|
55 #include <gst/gst.h> |
|
56 |
|
57 #include "camerabinimage.h" |
|
58 #include "camerabingeneral.h" |
|
59 |
|
60 #include "string.h" |
|
61 |
|
62 /* default internal element names */ |
|
63 |
|
64 #define DEFAULT_SINK "filesink" |
|
65 #define DEFAULT_ENC "jpegenc" |
|
66 #define DEFAULT_META_MUX "metadatamux" |
|
67 |
|
68 enum |
|
69 { |
|
70 PROP_0, |
|
71 PROP_FILENAME |
|
72 }; |
|
73 |
|
74 static gboolean gst_camerabin_image_create_elements (GstCameraBinImage * img); |
|
75 static void gst_camerabin_image_destroy_elements (GstCameraBinImage * img); |
|
76 |
|
77 static void gst_camerabin_image_dispose (GstCameraBinImage * sink); |
|
78 static GstStateChangeReturn |
|
79 gst_camerabin_image_change_state (GstElement * element, |
|
80 GstStateChange transition); |
|
81 static gboolean gst_camerabin_image_send_event (GstElement * element, |
|
82 GstEvent * event); |
|
83 static void gst_camerabin_image_set_property (GObject * object, guint prop_id, |
|
84 const GValue * value, GParamSpec * pspec); |
|
85 static void gst_camerabin_image_get_property (GObject * object, guint prop_id, |
|
86 GValue * value, GParamSpec * pspec); |
|
87 |
|
88 GST_BOILERPLATE (GstCameraBinImage, gst_camerabin_image, GstBin, GST_TYPE_BIN); |
|
89 |
|
90 static const GstElementDetails gst_camerabin_image_details = |
|
91 GST_ELEMENT_DETAILS ("Image capture bin for camerabin", |
|
92 "Bin/Image", |
|
93 "Process and store image data", |
|
94 "Edgard Lima <edgard.lima@indt.org.br>\n" |
|
95 "Nokia Corporation <multimedia@maemo.org>"); |
|
96 |
|
97 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", |
|
98 GST_PAD_SINK, |
|
99 GST_PAD_ALWAYS, |
|
100 GST_STATIC_CAPS_ANY); |
|
101 |
|
102 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", |
|
103 GST_PAD_SRC, |
|
104 GST_PAD_ALWAYS, |
|
105 GST_STATIC_CAPS_ANY); |
|
106 |
|
107 static void |
|
108 gst_camerabin_image_base_init (gpointer klass) |
|
109 { |
|
110 GstElementClass *eklass = GST_ELEMENT_CLASS (klass); |
|
111 |
|
112 gst_element_class_add_pad_template (eklass, |
|
113 gst_static_pad_template_get (&sink_template)); |
|
114 gst_element_class_add_pad_template (eklass, |
|
115 gst_static_pad_template_get (&src_template)); |
|
116 gst_element_class_set_details (eklass, &gst_camerabin_image_details); |
|
117 } |
|
118 |
|
119 static void |
|
120 gst_camerabin_image_class_init (GstCameraBinImageClass * klass) |
|
121 { |
|
122 GObjectClass *gobject_class; |
|
123 GstElementClass *eklass = GST_ELEMENT_CLASS (klass); |
|
124 |
|
125 gobject_class = G_OBJECT_CLASS (klass); |
|
126 gobject_class->dispose = |
|
127 (GObjectFinalizeFunc) GST_DEBUG_FUNCPTR (gst_camerabin_image_dispose); |
|
128 eklass->change_state = GST_DEBUG_FUNCPTR (gst_camerabin_image_change_state); |
|
129 eklass->send_event = GST_DEBUG_FUNCPTR (gst_camerabin_image_send_event); |
|
130 |
|
131 gobject_class->set_property = |
|
132 GST_DEBUG_FUNCPTR (gst_camerabin_image_set_property); |
|
133 gobject_class->get_property = |
|
134 GST_DEBUG_FUNCPTR (gst_camerabin_image_get_property); |
|
135 |
|
136 /** |
|
137 * GstCameraBinImage:filename |
|
138 * |
|
139 * This property can be used to specify the filename of the image. |
|
140 * |
|
141 **/ |
|
142 g_object_class_install_property (gobject_class, PROP_FILENAME, |
|
143 g_param_spec_string ("filename", "Filename", |
|
144 "Filename of the image to save", NULL, G_PARAM_READWRITE)); |
|
145 } |
|
146 |
|
147 static void |
|
148 gst_camerabin_image_init (GstCameraBinImage * img, |
|
149 GstCameraBinImageClass * g_class) |
|
150 { |
|
151 img->filename = g_string_new (""); |
|
152 |
|
153 img->pad_tee_enc = NULL; |
|
154 img->pad_tee_view = NULL; |
|
155 |
|
156 img->post = NULL; |
|
157 img->tee = NULL; |
|
158 img->enc = NULL; |
|
159 img->user_enc = NULL; |
|
160 img->meta_mux = NULL; |
|
161 img->sink = NULL; |
|
162 img->queue = NULL; |
|
163 |
|
164 /* Create src and sink ghost pads */ |
|
165 img->sinkpad = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK); |
|
166 gst_element_add_pad (GST_ELEMENT (img), img->sinkpad); |
|
167 |
|
168 img->srcpad = gst_ghost_pad_new_no_target ("src", GST_PAD_SRC); |
|
169 gst_element_add_pad (GST_ELEMENT (img), img->srcpad); |
|
170 |
|
171 img->elements_created = FALSE; |
|
172 } |
|
173 |
|
174 static void |
|
175 gst_camerabin_image_dispose (GstCameraBinImage * img) |
|
176 { |
|
177 g_string_free (img->filename, TRUE); |
|
178 img->filename = NULL; |
|
179 |
|
180 if (img->user_enc) { |
|
181 gst_object_unref (img->user_enc); |
|
182 img->user_enc = NULL; |
|
183 } |
|
184 |
|
185 if (img->post) { |
|
186 gst_object_unref (img->post); |
|
187 img->post = NULL; |
|
188 } |
|
189 |
|
190 G_OBJECT_CLASS (parent_class)->dispose ((GObject *) img); |
|
191 } |
|
192 |
|
193 static GstStateChangeReturn |
|
194 gst_camerabin_image_change_state (GstElement * element, |
|
195 GstStateChange transition) |
|
196 { |
|
197 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS; |
|
198 GstCameraBinImage *img = GST_CAMERABIN_IMAGE (element); |
|
199 |
|
200 switch (transition) { |
|
201 case GST_STATE_CHANGE_NULL_TO_READY: |
|
202 if (!gst_camerabin_image_create_elements (img)) { |
|
203 return GST_STATE_CHANGE_FAILURE; |
|
204 } |
|
205 /* Allow setting filename when image bin in READY state */ |
|
206 gst_element_set_locked_state (img->sink, TRUE); |
|
207 break; |
|
208 case GST_STATE_CHANGE_READY_TO_PAUSED: |
|
209 gst_element_set_locked_state (img->sink, FALSE); |
|
210 break; |
|
211 case GST_STATE_CHANGE_PAUSED_TO_READY: |
|
212 /* Set sink to NULL in order to write the file _now_ */ |
|
213 GST_INFO ("write img file: %s", img->filename->str); |
|
214 gst_element_set_locked_state (img->sink, TRUE); |
|
215 gst_element_set_state (img->sink, GST_STATE_NULL); |
|
216 break; |
|
217 default: |
|
218 break; |
|
219 } |
|
220 |
|
221 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); |
|
222 |
|
223 switch (transition) { |
|
224 case GST_STATE_CHANGE_READY_TO_NULL: |
|
225 gst_camerabin_image_destroy_elements (img); |
|
226 break; |
|
227 default: |
|
228 break; |
|
229 } |
|
230 |
|
231 return ret; |
|
232 } |
|
233 |
|
234 gboolean |
|
235 gst_camerabin_image_send_event (GstElement * element, GstEvent * event) |
|
236 { |
|
237 GstCameraBinImage *bin = GST_CAMERABIN_IMAGE (element); |
|
238 gboolean ret = FALSE; |
|
239 |
|
240 GST_INFO ("got %s event", GST_EVENT_TYPE_NAME (event)); |
|
241 |
|
242 if (GST_EVENT_IS_DOWNSTREAM (event)) { |
|
243 ret = gst_pad_send_event (bin->sinkpad, event); |
|
244 } else { |
|
245 if (bin->sink) { |
|
246 ret = gst_element_send_event (bin->sink, event); |
|
247 } else { |
|
248 GST_WARNING ("upstream event handling failed"); |
|
249 } |
|
250 } |
|
251 |
|
252 return ret; |
|
253 } |
|
254 |
|
255 static void |
|
256 gst_camerabin_image_set_property (GObject * object, guint prop_id, |
|
257 const GValue * value, GParamSpec * pspec) |
|
258 { |
|
259 GstCameraBinImage *bin = GST_CAMERABIN_IMAGE (object); |
|
260 |
|
261 switch (prop_id) { |
|
262 case PROP_FILENAME: |
|
263 g_string_assign (bin->filename, g_value_get_string (value)); |
|
264 if (bin->sink) { |
|
265 g_object_set (G_OBJECT (bin->sink), "location", bin->filename->str, |
|
266 NULL); |
|
267 } else { |
|
268 GST_INFO ("no sink, not setting name yet"); |
|
269 } |
|
270 break; |
|
271 default: |
|
272 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
|
273 break; |
|
274 } |
|
275 } |
|
276 |
|
277 static void |
|
278 gst_camerabin_image_get_property (GObject * object, guint prop_id, |
|
279 GValue * value, GParamSpec * pspec) |
|
280 { |
|
281 GstCameraBinImage *bin = GST_CAMERABIN_IMAGE (object); |
|
282 |
|
283 switch (prop_id) { |
|
284 case PROP_FILENAME: |
|
285 g_value_set_string (value, bin->filename->str); |
|
286 break; |
|
287 default: |
|
288 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
|
289 break; |
|
290 } |
|
291 } |
|
292 |
|
293 /* |
|
294 * static helper functions implementation |
|
295 */ |
|
296 |
|
297 /** |
|
298 * metadata_write_probe: |
|
299 * @pad: sink pad of metadata muxer |
|
300 * @buffer: received buffer |
|
301 * @u_data: image bin object |
|
302 * |
|
303 * Buffer probe that sets Xmp.dc.type and Xmp.dc.format tags |
|
304 * to metadata muxer based on preceding element src pad caps. |
|
305 * |
|
306 * Returns: TRUE always |
|
307 */ |
|
308 static gboolean |
|
309 metadata_write_probe (GstPad * pad, GstBuffer * buffer, gpointer u_data) |
|
310 { |
|
311 /* Add XMP tags */ |
|
312 GstCameraBinImage *img = NULL; |
|
313 GstTagSetter *setter = NULL; |
|
314 GstPad *srcpad = NULL; |
|
315 GstCaps *caps = NULL; |
|
316 GstStructure *st = NULL; |
|
317 |
|
318 img = GST_CAMERABIN_IMAGE (u_data); |
|
319 |
|
320 g_return_val_if_fail (img != NULL, TRUE); |
|
321 |
|
322 setter = GST_TAG_SETTER (img->meta_mux); |
|
323 |
|
324 if (!setter) { |
|
325 GST_WARNING_OBJECT (img, "setting tags failed"); |
|
326 goto done; |
|
327 } |
|
328 |
|
329 /* Xmp.dc.type tag */ |
|
330 gst_tag_setter_add_tags (setter, GST_TAG_MERGE_REPLACE, |
|
331 GST_TAG_CODEC, "Image", NULL); |
|
332 /* Xmp.dc.format tag */ |
|
333 if (img->enc) { |
|
334 srcpad = gst_element_get_static_pad (img->enc, "src"); |
|
335 } |
|
336 GST_LOG_OBJECT (img, "srcpad:%" GST_PTR_FORMAT, srcpad); |
|
337 if (srcpad) { |
|
338 caps = gst_pad_get_negotiated_caps (srcpad); |
|
339 GST_LOG_OBJECT (img, "caps:%" GST_PTR_FORMAT, caps); |
|
340 if (caps) { |
|
341 /* If there are many structures, we can't know which one to use */ |
|
342 if (gst_caps_get_size (caps) != 1) { |
|
343 GST_WARNING_OBJECT (img, "can't decide structure for format tag"); |
|
344 goto done; |
|
345 } |
|
346 st = gst_caps_get_structure (caps, 0); |
|
347 if (st) { |
|
348 GST_DEBUG_OBJECT (img, "Xmp.dc.format:%s", gst_structure_get_name (st)); |
|
349 gst_tag_setter_add_tags (setter, GST_TAG_MERGE_REPLACE, |
|
350 GST_TAG_VIDEO_CODEC, gst_structure_get_name (st), NULL); |
|
351 } |
|
352 } |
|
353 } |
|
354 done: |
|
355 if (caps) |
|
356 gst_caps_unref (caps); |
|
357 if (srcpad) |
|
358 gst_object_unref (srcpad); |
|
359 |
|
360 return TRUE; |
|
361 } |
|
362 |
|
363 |
|
364 /** |
|
365 * gst_camerabin_image_create_elements: |
|
366 * @img: a pointer to #GstCameraBinImage object |
|
367 * |
|
368 * This function creates needed #GstElements and resources to capture images. |
|
369 * Use gst_camerabin_image_destroy_elements to release these resources. |
|
370 * |
|
371 * Image bin: |
|
372 * img->sinkpad ! [ post process !] tee name=t0 ! encoder ! metadata ! filesink |
|
373 * t0. ! queue ! img->srcpad |
|
374 * |
|
375 * Returns: %TRUE if succeeded or FALSE if failed |
|
376 */ |
|
377 static gboolean |
|
378 gst_camerabin_image_create_elements (GstCameraBinImage * img) |
|
379 { |
|
380 GstPad *sinkpad = NULL, *img_sinkpad = NULL, *img_srcpad = NULL; |
|
381 gboolean ret = FALSE; |
|
382 GstBin *imgbin = NULL; |
|
383 |
|
384 g_return_val_if_fail (img != NULL, FALSE); |
|
385 |
|
386 GST_DEBUG ("creating image capture elements"); |
|
387 |
|
388 imgbin = GST_BIN (img); |
|
389 |
|
390 if (img->elements_created) { |
|
391 GST_WARNING ("elements already created"); |
|
392 ret = TRUE; |
|
393 goto done; |
|
394 } else { |
|
395 img->elements_created = TRUE; |
|
396 } |
|
397 |
|
398 /* Create image pre/post-processing element if any */ |
|
399 if (img->post) { |
|
400 if (!gst_camerabin_add_element (imgbin, img->post)) { |
|
401 goto done; |
|
402 } |
|
403 img_sinkpad = gst_element_get_static_pad (img->post, "sink"); |
|
404 } |
|
405 |
|
406 /* Create tee */ |
|
407 if (!(img->tee = gst_camerabin_create_and_add_element (imgbin, "tee"))) { |
|
408 goto done; |
|
409 } |
|
410 |
|
411 /* Set up sink ghost pad for img bin */ |
|
412 if (!img_sinkpad) { |
|
413 img_sinkpad = gst_element_get_static_pad (img->tee, "sink"); |
|
414 } |
|
415 gst_ghost_pad_set_target (GST_GHOST_PAD (img->sinkpad), img_sinkpad); |
|
416 |
|
417 /* Add colorspace converter */ |
|
418 img->pad_tee_enc = gst_element_get_request_pad (img->tee, "src%d"); |
|
419 if (!gst_camerabin_create_and_add_element (imgbin, "ffmpegcolorspace")) { |
|
420 goto done; |
|
421 } |
|
422 |
|
423 /* Create image encoder */ |
|
424 if (img->user_enc) { |
|
425 img->enc = img->user_enc; |
|
426 if (!gst_camerabin_add_element (imgbin, img->enc)) { |
|
427 goto done; |
|
428 } |
|
429 } else if (!(img->enc = |
|
430 gst_camerabin_create_and_add_element (imgbin, DEFAULT_ENC))) { |
|
431 goto done; |
|
432 } |
|
433 |
|
434 /* Create metadata element */ |
|
435 if (!(img->meta_mux = |
|
436 gst_camerabin_create_and_add_element (imgbin, DEFAULT_META_MUX))) { |
|
437 goto done; |
|
438 } |
|
439 /* Add probe for XMP metadata writing */ |
|
440 sinkpad = gst_element_get_static_pad (img->meta_mux, "sink"); |
|
441 gst_pad_add_buffer_probe (sinkpad, G_CALLBACK (metadata_write_probe), img); |
|
442 gst_object_unref (sinkpad); |
|
443 /* Set "Intel" exif byte-order if possible */ |
|
444 if (g_object_class_find_property (G_OBJECT_GET_CLASS (img->meta_mux), |
|
445 "exif-byte-order")) { |
|
446 g_object_set (G_OBJECT (img->meta_mux), "exif-byte-order", 1, NULL); |
|
447 } |
|
448 |
|
449 /* Create file sink element */ |
|
450 if (!(img->sink = |
|
451 gst_camerabin_create_and_add_element (imgbin, DEFAULT_SINK))) { |
|
452 goto done; |
|
453 } |
|
454 |
|
455 /* Create queue element leading to view finder, attaches it to the tee */ |
|
456 img->pad_tee_view = gst_element_get_request_pad (img->tee, "src%d"); |
|
457 if (!(img->queue = gst_camerabin_create_and_add_element (imgbin, "queue"))) { |
|
458 goto done; |
|
459 } |
|
460 |
|
461 /* Set properties */ |
|
462 g_object_set (G_OBJECT (img->sink), "location", img->filename->str, NULL); |
|
463 g_object_set (G_OBJECT (img->sink), "async", FALSE, NULL); |
|
464 |
|
465 g_object_set (G_OBJECT (img->queue), "max-size-buffers", 1, "leaky", 2, NULL); |
|
466 |
|
467 /* Set up src ghost pad for img bin */ |
|
468 img_srcpad = gst_element_get_static_pad (img->queue, "src"); |
|
469 gst_ghost_pad_set_target (GST_GHOST_PAD (img->srcpad), img_srcpad); |
|
470 |
|
471 /* Never let image bin eos events reach view finder */ |
|
472 gst_pad_add_event_probe (img->srcpad, |
|
473 G_CALLBACK (gst_camerabin_drop_eos_probe), img); |
|
474 |
|
475 ret = TRUE; |
|
476 |
|
477 done: |
|
478 |
|
479 if (img_srcpad) { |
|
480 gst_object_unref (img_srcpad); |
|
481 } |
|
482 if (img_sinkpad) { |
|
483 gst_object_unref (img_sinkpad); |
|
484 } |
|
485 if (!ret) { |
|
486 gst_camerabin_image_destroy_elements (img); |
|
487 } |
|
488 |
|
489 return ret; |
|
490 } |
|
491 |
|
492 |
|
493 /** |
|
494 * gst_camerabin_image_destroy_elements: |
|
495 * @img: a pointer to #GstCameraBinImage object |
|
496 * |
|
497 * This function releases resources allocated in |
|
498 * gst_camerabin_image_create_elements. |
|
499 * |
|
500 */ |
|
501 static void |
|
502 gst_camerabin_image_destroy_elements (GstCameraBinImage * img) |
|
503 { |
|
504 GST_LOG ("destroying img elements"); |
|
505 if (img->pad_tee_enc) { |
|
506 gst_element_release_request_pad (img->tee, img->pad_tee_enc); |
|
507 img->pad_tee_enc = NULL; |
|
508 } |
|
509 |
|
510 if (img->pad_tee_view) { |
|
511 gst_element_release_request_pad (img->tee, img->pad_tee_view); |
|
512 img->pad_tee_view = NULL; |
|
513 } |
|
514 |
|
515 gst_ghost_pad_set_target (GST_GHOST_PAD (img->sinkpad), NULL); |
|
516 gst_ghost_pad_set_target (GST_GHOST_PAD (img->srcpad), NULL); |
|
517 |
|
518 gst_camerabin_remove_elements_from_bin (GST_BIN (img)); |
|
519 |
|
520 img->post = NULL; |
|
521 img->tee = NULL; |
|
522 img->enc = NULL; |
|
523 img->meta_mux = NULL; |
|
524 img->sink = NULL; |
|
525 img->queue = NULL; |
|
526 |
|
527 img->elements_created = FALSE; |
|
528 } |
|
529 |
|
530 void |
|
531 gst_camerabin_image_set_encoder (GstCameraBinImage * img, GstElement * encoder) |
|
532 { |
|
533 if (img->user_enc) |
|
534 gst_object_unref (img->user_enc); |
|
535 if (encoder) |
|
536 gst_object_ref (encoder); |
|
537 |
|
538 img->user_enc = encoder; |
|
539 } |
|
540 |
|
541 void |
|
542 gst_camerabin_image_set_postproc (GstCameraBinImage * img, |
|
543 GstElement * postproc) |
|
544 { |
|
545 if (img->post) |
|
546 gst_object_unref (img->post); |
|
547 if (postproc) |
|
548 gst_object_ref (postproc); |
|
549 |
|
550 img->post = postproc; |
|
551 } |
|
552 |
|
553 GstElement * |
|
554 gst_camerabin_image_get_encoder (GstCameraBinImage * img) |
|
555 { |
|
556 GstElement *enc; |
|
557 |
|
558 if (img->user_enc) { |
|
559 enc = img->user_enc; |
|
560 } else { |
|
561 enc = img->enc; |
|
562 } |
|
563 |
|
564 return enc; |
|
565 } |
|
566 |
|
567 GstElement * |
|
568 gst_camerabin_image_get_postproc (GstCameraBinImage * img) |
|
569 { |
|
570 return img->post; |
|
571 } |
|