584e3e316735d9fda222a535c1d39666a57cd17e
[platform/upstream/gstreamer.git] / gst / camerabin2 / gstcamerabin2.c
1 /* GStreamer
2  * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
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 /**
20  * SECTION:element-camerabin
21  * @title: camerabin
22  *
23  * CameraBin is a high-level camera object that encapsulates gstreamer
24  * elements, providing an API for controlling a digital camera.
25  *
26  * > Note that camerabin is still UNSTABLE and under development.
27  *
28  * CameraBin has the following main features:
29  *
30  * * Record videos
31  * * Capture pictures
32  * * Display a viewfinder
33  * * Post preview images for each capture (video and image)
34  *
35  * ## Usage
36  *
37  * Camerabin can be created using gst_element_factory_make() just like
38  * any other element. Video or image capture mode can be selected using
39  * the #GstCameraBin:mode property and the file to save the capture is
40  * selected using #GstCameraBin:location property.
41  *
42  * After creating camerabin, applications might want to do some
43  * customization (there's a section about this below), then select
44  * the desired mode and start capturing.
45  *
46  * In image capture mode, just send a #GstCameraBin::start-capture and a
47  * picture will be captured. When the picture is stored on the selected
48  * location, a %GST_MESSAGE_ELEMENT named 'image-done' will be posted on
49  * the #GstBus.
50  *
51  * In video capture mode, send a #GstCameraBin::start-capture to start
52  * recording, then send a #GstCameraBin::stop-capture to stop recording.
53  * Note that both signals are asynchronous, so, calling
54  * #GstCameraBin::stop-capture doesn't guarantee that the video has been
55  * properly finished yet. Applications should wait for the 'video-done'
56  * message to be posted on the bus.
57  *
58  * In both modes, if #GstCameraBin:post-previews is %TRUE, a #GstBuffer
59  * will be post to the #GstBus in a field named 'buffer', in a
60  * 'preview-image' message of type %GST_MESSAGE_ELEMENT.
61  *
62
63  *
64  * ## Customization
65  *
66  * Camerabin provides various customization properties, allowing the user
67  * to set custom filters, selecting the viewfinder sink and formats to
68  * use to encode the captured images/videos.
69  *
70  * #GstEncodingProfile<!-- -->s are used to tell camerabin which formats it
71  * should encode the captures to, those should be set to
72  * #GstCameraBin:image-profile and #GstCameraBin:video-profile. Default is
73  * jpeg for images, and ogg (theora and vorbis) for video. If a profile without
74  * an audio stream is set for video, audio will be disabled on recordings.
75  *
76  * #GstCameraBin:preview-caps can be used to select which format preview
77  * images should be posted on the #GstBus. It has to be a raw video format.
78  *
79  * Camerabin has a #GstCameraBin:camera-source property so applications can
80  * set their source that will provide buffers for the viewfinder and for
81  * captures. This camera source is a special type of source that has 3 pads.
82  * To use a 'regular' source with a single pad you should use
83  * #GstWrapperCameraBinSrc, it will adapt your source and provide 3 pads.
84  *
85  * Applications can also select the desired viewfinder sink using
86  * #GstCameraBin:viewfinder-sink, it is also possible to select the audio
87  * source using #GstCameraBin:audio-source.
88  *
89  * The viewfinder resolution can be configured using
90  * #GstCameraBin:viewfinder-caps, these #GstCaps should be a subset of
91  * #GstCameraBin:viewfinder-supported-caps.
92  *
93  * To select the desired resolution for captures, camerabin provides
94  * #GstCameraBin:image-capture-caps and #GstCameraBin:video-capture-caps,
95  * these caps must be a subset of what the source can produce. The allowed
96  * caps can be probed using #GstCameraBin:image-capture-supported-caps and
97  * #GstCameraBin:video-capture-supported-caps. In an analogous way, there
98  * are #GstCameraBin:audio-capture-caps and
99  * #GstCameraBin:audio-capture-supported-caps.
100  *
101  * Camerabin also allows applications to insert custom #GstElements on any
102  * of its branches: video capture, image capture, viewfinder and preview.
103  * Check #GstCameraBin:video-filter, #GstCameraBin:image-filter,
104  * #GstCameraBin:viewfinder-filter and #GstCameraBin:preview-filter.
105  *
106  * ## Example launch line
107  *
108  * Unfortunately, camerabin can't be really used from gst-launch-1.0, as you
109  * need to send signals to control it. The following pipeline might be able
110  * to show the viewfinder using all the default elements.
111  * |[
112  * gst-launch-1.0 -v -m camerabin
113  * ]|
114  *
115
116  */
117
118 /*
119  * Detail Topics:
120  *
121  * videorecordingbin state management (for now on called 'videobin')
122  * - The problem: keeping videobin state in sync with camerabin will make it
123  *                go to playing when it might not be used, causing its internal
124  *                filesink to open a file that might be left blank.
125  * - The solution: videobin state is set to locked upon its creation and camerabin
126  *                 registers itself on the notify::ready-for-capture of the src.
127  *                 Whenever the src readyness goes to FALSE it means a new
128  *                 capture is starting. If we are on video mode, the videobin's
129  *                 state is set to NULL and then PLAYING (in between this we
130  *                 have room to set the destination filename).
131  *                 There is no problem to leave it on playing after an EOS, so
132  *                 no action is taken on stop-capture.
133  *
134  * - TODO: What happens when an error pops?
135  * - TODO: Should we split properties in image/video variants? We already do so
136  *         for some of them
137  *
138  *
139  */
140
141 #ifdef HAVE_CONFIG_H
142 #include "config.h"
143 #endif
144
145 #include <string.h>
146
147 #include <gst/basecamerabinsrc/gstbasecamerasrc.h>
148 #include "gstcamerabin2.h"
149 #include <gst/gst-i18n-plugin.h>
150 #include <gst/pbutils/pbutils.h>
151 #include <gst/glib-compat-private.h>
152
153 #define GST_CAMERA_BIN2_PROCESSING_INC(c)                                \
154 {                                                                       \
155   gint bef = g_atomic_int_add (&c->processing_counter, 1); \
156   if (bef == 0)                                                         \
157     g_object_notify (G_OBJECT (c), "idle");                             \
158   GST_DEBUG_OBJECT ((c), "Processing counter incremented to: %d",       \
159       bef + 1);                                                         \
160 }
161
162 #define GST_CAMERA_BIN2_PROCESSING_DEC(c)                                \
163 {                                                                       \
164   if (g_atomic_int_dec_and_test (&c->processing_counter)) {             \
165     g_object_notify (G_OBJECT (c), "idle");                             \
166     GST_DEBUG_OBJECT ((c), "Camerabin now idle");                       \
167   }                                                                     \
168   GST_DEBUG_OBJECT ((c), "Processing counter decremented");             \
169 }
170
171 #define GST_CAMERA_BIN2_RESET_PROCESSING_COUNTER(c)                      \
172 {                                                                       \
173   g_atomic_int_set (&c->processing_counter, 0);                         \
174   GST_DEBUG_OBJECT ((c), "Processing counter reset");                   \
175 }
176
177 GST_DEBUG_CATEGORY_STATIC (gst_camera_bin_debug);
178 #define GST_CAT_DEFAULT gst_camera_bin_debug
179
180 /* prototypes */
181
182 enum
183 {
184   PROP_0,
185   PROP_MODE,
186   PROP_LOCATION,
187   PROP_CAMERA_SRC,
188   PROP_IMAGE_CAPTURE_SUPPORTED_CAPS,
189   PROP_VIDEO_CAPTURE_SUPPORTED_CAPS,
190   PROP_IMAGE_CAPTURE_CAPS,
191   PROP_VIDEO_CAPTURE_CAPS,
192   PROP_POST_PREVIEWS,
193   PROP_PREVIEW_CAPS,
194   PROP_VIDEO_ENCODING_PROFILE,
195   PROP_IMAGE_FILTER,
196   PROP_VIDEO_FILTER,
197   PROP_VIEWFINDER_FILTER,
198   PROP_PREVIEW_FILTER,
199   PROP_VIEWFINDER_SINK,
200   PROP_VIEWFINDER_SUPPORTED_CAPS,
201   PROP_VIEWFINDER_CAPS,
202   PROP_AUDIO_SRC,
203   PROP_MUTE_AUDIO,
204   PROP_AUDIO_CAPTURE_SUPPORTED_CAPS,
205   PROP_AUDIO_CAPTURE_CAPS,
206   PROP_ZOOM,
207   PROP_MAX_ZOOM,
208   PROP_IMAGE_ENCODING_PROFILE,
209   PROP_IDLE,
210   PROP_FLAGS,
211   PROP_AUDIO_FILTER
212 };
213
214 enum
215 {
216   /* action signals */
217   START_CAPTURE_SIGNAL,
218   STOP_CAPTURE_SIGNAL,
219   /* emit signals */
220   LAST_SIGNAL
221 };
222 static guint camerabin_signals[LAST_SIGNAL];
223
224 #define DEFAULT_MODE MODE_IMAGE
225 #define DEFAULT_LOCATION "cap_%d"
226 #define DEFAULT_POST_PREVIEWS FALSE
227 #define DEFAULT_MUTE_AUDIO FALSE
228 #define DEFAULT_IDLE TRUE
229 #define DEFAULT_FLAGS 0
230
231 #define DEFAULT_AUDIO_SRC "autoaudiosrc"
232
233 /********************************
234  * Standard GObject boilerplate *
235  * and GObject types            *
236  ********************************/
237
238 static GstPipelineClass *parent_class;
239 static void gst_camera_bin_class_init (GstCameraBin2Class * klass);
240 static void gst_camera_bin_base_init (gpointer klass);
241 static void gst_camera_bin_init (GstCameraBin2 * camera);
242 static void gst_camera_bin_dispose (GObject * object);
243 static void gst_camera_bin_finalize (GObject * object);
244
245 static void gst_camera_bin_handle_message (GstBin * bin, GstMessage * message);
246 static gboolean gst_camera_bin_send_event (GstElement * element,
247     GstEvent * event);
248
249 #define C_FLAGS(v) ((guint) v)
250 #define GST_TYPE_CAM_FLAGS (gst_cam_flags_get_type())
251 static GType
252 gst_cam_flags_get_type (void)
253 {
254   static const GFlagsValue values[] = {
255     {C_FLAGS (GST_CAM_FLAG_NO_AUDIO_CONVERSION), "Do not use audio conversion "
256           "elements", "no-audio-conversion"},
257     {C_FLAGS (GST_CAM_FLAG_NO_VIDEO_CONVERSION), "Do not use video conversion "
258           "elements", "no-video-conversion"},
259     {C_FLAGS (GST_CAM_FLAG_NO_VIEWFINDER_CONVERSION),
260           "Do not use viewfinder conversion " "elements",
261         "no-viewfinder-conversion"},
262     {C_FLAGS (GST_CAM_FLAG_NO_IMAGE_CONVERSION), "Do not use image conversion "
263           "elements", "no-image-conversion"},
264     {0, NULL, NULL}
265   };
266   static volatile GType id = 0;
267
268   if (g_once_init_enter ((gsize *) & id)) {
269     GType _id;
270
271     _id = g_flags_register_static ("GstCamFlags", values);
272
273     g_once_init_leave ((gsize *) & id, _id);
274   }
275
276   return id;
277 }
278
279 GType
280 gst_camera_bin2_get_type (void)
281 {
282   static GType gst_camera_bin_type = 0;
283   static const GInterfaceInfo camerabin_tagsetter_info = {
284     NULL,
285     NULL,
286     NULL,
287   };
288
289   if (!gst_camera_bin_type) {
290     static const GTypeInfo gst_camera_bin_info = {
291       sizeof (GstCameraBin2Class),
292       (GBaseInitFunc) gst_camera_bin_base_init,
293       NULL,
294       (GClassInitFunc) gst_camera_bin_class_init,
295       NULL,
296       NULL,
297       sizeof (GstCameraBin2),
298       0,
299       (GInstanceInitFunc) gst_camera_bin_init,
300       NULL
301     };
302
303     gst_camera_bin_type =
304         g_type_register_static (GST_TYPE_PIPELINE, "GstCameraBin",
305         &gst_camera_bin_info, 0);
306
307     g_type_add_interface_static (gst_camera_bin_type, GST_TYPE_TAG_SETTER,
308         &camerabin_tagsetter_info);
309   }
310
311   return gst_camera_bin_type;
312 }
313
314 /* GObject class functions */
315 static void gst_camera_bin_set_property (GObject * object, guint prop_id,
316     const GValue * value, GParamSpec * pspec);
317 static void gst_camera_bin_get_property (GObject * object, guint prop_id,
318     GValue * value, GParamSpec * pspec);
319
320 /* Element class functions */
321 static GstStateChangeReturn
322 gst_camera_bin_change_state (GstElement * element, GstStateChange trans);
323
324
325 /* Camerabin functions */
326
327 static GstEvent *
328 gst_camera_bin_new_event_file_location (const gchar * location)
329 {
330   return gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
331       gst_structure_new ("new-location", "location", G_TYPE_STRING, location,
332           NULL));
333 }
334
335 static void
336 gst_camera_bin_start_capture (GstCameraBin2 * camerabin)
337 {
338   const GstTagList *taglist;
339   gint capture_index = camerabin->capture_index;
340   gchar *location = NULL;
341   GST_DEBUG_OBJECT (camerabin, "Received start-capture");
342
343   /* check that we have a valid location */
344   if (camerabin->mode == MODE_VIDEO) {
345     if (camerabin->location == NULL) {
346       GST_ELEMENT_ERROR (camerabin, RESOURCE, OPEN_WRITE,
347           (_("File location is set to NULL, please set it to a valid filename")), (NULL));
348       return;
349     }
350
351     g_mutex_lock (&camerabin->video_capture_mutex);
352     while (camerabin->video_state == GST_CAMERA_BIN_VIDEO_FINISHING) {
353       g_cond_wait (&camerabin->video_state_cond,
354           &camerabin->video_capture_mutex);
355     }
356     if (camerabin->video_state != GST_CAMERA_BIN_VIDEO_IDLE) {
357       GST_WARNING_OBJECT (camerabin, "Another video recording is ongoing"
358           " (state %d), cannot start a new one", camerabin->video_state);
359       g_mutex_unlock (&camerabin->video_capture_mutex);
360       return;
361     }
362     camerabin->video_state = GST_CAMERA_BIN_VIDEO_STARTING;
363   }
364
365   GST_CAMERA_BIN2_PROCESSING_INC (camerabin);
366
367   if (camerabin->location)
368     location = g_strdup_printf (camerabin->location, capture_index);
369
370   if (camerabin->mode == MODE_IMAGE) {
371     /* store the next capture buffer filename */
372     g_mutex_lock (&camerabin->image_capture_mutex);
373     camerabin->image_location_list =
374         g_slist_append (camerabin->image_location_list, g_strdup (location));
375     g_mutex_unlock (&camerabin->image_capture_mutex);
376   }
377
378   if (camerabin->post_previews) {
379     /* Count processing of preview images too */
380     GST_CAMERA_BIN2_PROCESSING_INC (camerabin);
381     /* store the next preview filename */
382     g_mutex_lock (&camerabin->preview_list_mutex);
383     camerabin->preview_location_list =
384         g_slist_append (camerabin->preview_location_list, location);
385     g_mutex_unlock (&camerabin->preview_list_mutex);
386   } else {
387     g_free (location);
388   }
389
390   g_signal_emit_by_name (camerabin->src, "start-capture", NULL);
391   if (camerabin->mode == MODE_VIDEO) {
392     camerabin->audio_send_newseg = TRUE;
393     if (camerabin->audio_src)
394       gst_element_set_state (camerabin->audio_src, GST_STATE_PLAYING);
395
396     camerabin->video_state = GST_CAMERA_BIN_VIDEO_RECORDING;
397     g_mutex_unlock (&camerabin->video_capture_mutex);
398   }
399
400   /*
401    * We have to push tags after start capture because the video elements
402    * might be flushing from the previous capture and are reset only on the
403    * notify from ready for capture going to FALSE
404    */
405   taglist = gst_tag_setter_get_tag_list (GST_TAG_SETTER (camerabin));
406   GST_DEBUG_OBJECT (camerabin, "Have tags from application: %"
407       GST_PTR_FORMAT, taglist);
408
409   if (camerabin->mode == MODE_IMAGE) {
410     /* Store image tags in a list and push them later, this prevents
411        start_capture() from blocking in pad_push_event call */
412     g_mutex_lock (&camerabin->image_capture_mutex);
413     camerabin->image_tags_list =
414         g_slist_append (camerabin->image_tags_list,
415         taglist ? gst_tag_list_copy (taglist) : NULL);
416     g_mutex_unlock (&camerabin->image_capture_mutex);
417   } else if (taglist) {
418     GstPad *active_pad;
419
420     active_pad = gst_element_get_static_pad (camerabin->src,
421         GST_BASE_CAMERA_SRC_VIDEO_PAD_NAME);
422     gst_pad_push_event (active_pad,
423         gst_event_new_tag (gst_tag_list_copy (taglist)));
424
425     gst_object_unref (active_pad);
426   }
427
428   GST_DEBUG_OBJECT (camerabin, "Start-capture end");
429 }
430
431 static void
432 gst_camera_bin_stop_capture (GstCameraBin2 * camerabin)
433 {
434   GST_DEBUG_OBJECT (camerabin, "Received stop-capture");
435   if (camerabin->mode == MODE_VIDEO) {
436     g_mutex_lock (&camerabin->video_capture_mutex);
437     if (camerabin->video_state == GST_CAMERA_BIN_VIDEO_RECORDING) {
438       if (camerabin->src)
439         g_signal_emit_by_name (camerabin->src, "stop-capture", NULL);
440
441       camerabin->video_state = GST_CAMERA_BIN_VIDEO_FINISHING;
442       if (camerabin->audio_src) {
443         camerabin->audio_drop_eos = FALSE;
444         gst_element_send_event (camerabin->audio_src, gst_event_new_eos ());
445       }
446     }
447     g_mutex_unlock (&camerabin->video_capture_mutex);
448   }
449 }
450
451 static void
452 gst_camera_bin_change_mode (GstCameraBin2 * camerabin, gint mode)
453 {
454   if (mode == camerabin->mode)
455     return;
456
457   GST_DEBUG_OBJECT (camerabin, "Changing mode to %d", mode);
458
459   /* stop any ongoing capture */
460   gst_camera_bin_stop_capture (camerabin);
461   camerabin->mode = mode;
462   if (camerabin->src)
463     g_object_set (camerabin->src, "mode", mode, NULL);
464 }
465
466 static void
467 gst_camera_bin_src_notify_readyforcapture (GObject * obj, GParamSpec * pspec,
468     gpointer user_data)
469 {
470   GstCameraBin2 *camera = GST_CAMERA_BIN2_CAST (user_data);
471   gboolean ready;
472
473   g_object_get (camera->src, "ready-for-capture", &ready, NULL);
474   if (!ready) {
475     gchar *location = NULL;
476
477     if (camera->mode == MODE_VIDEO) {
478       /* a video recording is about to start, change the filesink location */
479       gst_element_set_state (camera->videosink, GST_STATE_NULL);
480       location = g_strdup_printf (camera->location, camera->capture_index);
481       GST_DEBUG_OBJECT (camera, "Switching videobin location to %s", location);
482       g_object_set (camera->videosink, "location", location, NULL);
483       g_free (location);
484       if (gst_element_set_state (camera->videosink, GST_STATE_PLAYING) ==
485           GST_STATE_CHANGE_FAILURE) {
486         /* Resets the latest state change return, that would be a failure
487          * and could cause problems in a camerabin2 state change */
488         gst_element_set_state (camera->videosink, GST_STATE_NULL);
489       }
490     }
491
492     camera->capture_index++;
493   }
494 }
495
496 static void
497 gst_camera_bin_dispose (GObject * object)
498 {
499   GstCameraBin2 *camerabin = GST_CAMERA_BIN2_CAST (object);
500
501   g_free (camerabin->location);
502   g_mutex_clear (&camerabin->preview_list_mutex);
503   g_mutex_clear (&camerabin->image_capture_mutex);
504   g_mutex_clear (&camerabin->video_capture_mutex);
505   g_cond_clear (&camerabin->video_state_cond);
506
507   if (camerabin->src_capture_notify_id)
508     g_signal_handler_disconnect (camerabin->src,
509         camerabin->src_capture_notify_id);
510   if (camerabin->src)
511     gst_object_unref (camerabin->src);
512   if (camerabin->user_src)
513     gst_object_unref (camerabin->user_src);
514
515   if (camerabin->audio_src)
516     gst_object_unref (camerabin->audio_src);
517   if (camerabin->user_audio_src)
518     gst_object_unref (camerabin->user_audio_src);
519
520   if (camerabin->audio_capsfilter)
521     gst_object_unref (camerabin->audio_capsfilter);
522   if (camerabin->audio_volume)
523     gst_object_unref (camerabin->audio_volume);
524
525   if (camerabin->viewfinderbin)
526     gst_object_unref (camerabin->viewfinderbin);
527   if (camerabin->viewfinderbin_queue)
528     gst_object_unref (camerabin->viewfinderbin_queue);
529   if (camerabin->viewfinderbin_capsfilter)
530     gst_object_unref (camerabin->viewfinderbin_capsfilter);
531
532   if (camerabin->video_encodebin_signal_id)
533     g_signal_handler_disconnect (camerabin->video_encodebin,
534         camerabin->video_encodebin_signal_id);
535
536   if (camerabin->videosink)
537     gst_object_unref (camerabin->videosink);
538   if (camerabin->video_encodebin)
539     gst_object_unref (camerabin->video_encodebin);
540   if (camerabin->videobin_capsfilter)
541     gst_object_unref (camerabin->videobin_capsfilter);
542
543   if (camerabin->image_encodebin_signal_id)
544     g_signal_handler_disconnect (camerabin->image_encodebin,
545         camerabin->image_encodebin_signal_id);
546   if (camerabin->imagesink)
547     gst_object_unref (camerabin->imagesink);
548   if (camerabin->image_encodebin)
549     gst_object_unref (camerabin->image_encodebin);
550   if (camerabin->imagebin_capsfilter)
551     gst_object_unref (camerabin->imagebin_capsfilter);
552
553   if (camerabin->video_filter)
554     gst_object_unref (camerabin->video_filter);
555   if (camerabin->image_filter)
556     gst_object_unref (camerabin->image_filter);
557   if (camerabin->viewfinder_filter)
558     gst_object_unref (camerabin->viewfinder_filter);
559   if (camerabin->audio_filter)
560     gst_object_unref (camerabin->audio_filter);
561
562   if (camerabin->user_video_filter)
563     gst_object_unref (camerabin->user_video_filter);
564   if (camerabin->user_audio_filter)
565     gst_object_unref (camerabin->user_audio_filter);
566   if (camerabin->user_image_filter)
567     gst_object_unref (camerabin->user_image_filter);
568   if (camerabin->user_viewfinder_filter)
569     gst_object_unref (camerabin->user_viewfinder_filter);
570
571   if (camerabin->video_profile)
572     gst_encoding_profile_unref (camerabin->video_profile);
573   if (camerabin->image_profile)
574     gst_encoding_profile_unref (camerabin->image_profile);
575
576   if (camerabin->preview_caps)
577     gst_caps_replace (&camerabin->preview_caps, NULL);
578   if (camerabin->preview_filter) {
579     gst_object_unref (camerabin->preview_filter);
580     camerabin->preview_filter = NULL;
581   }
582
583   G_OBJECT_CLASS (parent_class)->dispose (object);
584 }
585
586 static void
587 gst_camera_bin_finalize (GObject * object)
588 {
589   G_OBJECT_CLASS (parent_class)->finalize (object);
590 }
591
592 static void
593 gst_camera_bin_base_init (gpointer g_class)
594 {
595   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
596
597   gst_element_class_set_static_metadata (element_class, "Camera Bin",
598       "Generic/Bin/Camera",
599       "Take image snapshots and record movies from camera",
600       "Thiago Santos <thiago.sousa.santos@collabora.co.uk>");
601 }
602
603 static void
604 gst_camera_bin_class_init (GstCameraBin2Class * klass)
605 {
606   GObjectClass *object_class;
607   GstElementClass *element_class;
608   GstBinClass *bin_class;
609
610   parent_class = g_type_class_peek_parent (klass);
611   object_class = G_OBJECT_CLASS (klass);
612   element_class = GST_ELEMENT_CLASS (klass);
613   bin_class = GST_BIN_CLASS (klass);
614
615   object_class->dispose = gst_camera_bin_dispose;
616   object_class->finalize = gst_camera_bin_finalize;
617   object_class->set_property = gst_camera_bin_set_property;
618   object_class->get_property = gst_camera_bin_get_property;
619
620   element_class->change_state = GST_DEBUG_FUNCPTR (gst_camera_bin_change_state);
621   element_class->send_event = GST_DEBUG_FUNCPTR (gst_camera_bin_send_event);
622
623   bin_class->handle_message = gst_camera_bin_handle_message;
624
625   klass->start_capture = gst_camera_bin_start_capture;
626   klass->stop_capture = gst_camera_bin_stop_capture;
627
628   /**
629    * GstCameraBin2:mode:
630    *
631    * Set the mode of operation: still image capturing or video recording.
632    */
633   g_object_class_install_property (object_class, PROP_MODE,
634       g_param_spec_enum ("mode", "Mode",
635           "The capture mode (still image capture or video recording)",
636           GST_TYPE_CAMERABIN_MODE, DEFAULT_MODE,
637           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
638
639   g_object_class_install_property (object_class, PROP_LOCATION,
640       g_param_spec_string ("location", "Location",
641           "Location to save the captured files. A %d might be used on the"
642           "filename as a placeholder for a numeric index of the capture."
643           "Default is cap_%d",
644           DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
645
646   g_object_class_install_property (object_class, PROP_CAMERA_SRC,
647       g_param_spec_object ("camera-source", "Camera source",
648           "The camera source element to be used. It is only taken into use on"
649           " the next null to ready transition",
650           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
651
652   g_object_class_install_property (object_class, PROP_AUDIO_SRC,
653       g_param_spec_object ("audio-source", "Audio source",
654           "The audio source element to be used on video recordings. It is only"
655           " taken into use on the next null to ready transition",
656           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
657
658   g_object_class_install_property (object_class, PROP_MUTE_AUDIO,
659       g_param_spec_boolean ("mute", "Mute",
660           "If the audio recording should be muted. Note that this still "
661           "saves audio data to the resulting file, but they are silent. Use "
662           "a video-profile without audio to disable audio completely",
663           DEFAULT_MUTE_AUDIO, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
664
665   g_object_class_install_property (object_class,
666       PROP_AUDIO_CAPTURE_SUPPORTED_CAPS,
667       g_param_spec_boxed ("audio-capture-supported-caps",
668           "Audio capture supported caps",
669           "Formats supported for capturing audio represented as GstCaps",
670           GST_TYPE_CAPS, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
671
672   g_object_class_install_property (object_class,
673       PROP_AUDIO_CAPTURE_CAPS,
674       g_param_spec_boxed ("audio-capture-caps",
675           "Audio capture caps",
676           "Format to capture audio for video recording represented as GstCaps",
677           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
678
679   g_object_class_install_property (object_class,
680       PROP_IMAGE_CAPTURE_SUPPORTED_CAPS,
681       g_param_spec_boxed ("image-capture-supported-caps",
682           "Image capture supported caps",
683           "Formats supported for capturing images represented as GstCaps",
684           GST_TYPE_CAPS, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
685
686   g_object_class_install_property (object_class,
687       PROP_VIDEO_CAPTURE_SUPPORTED_CAPS,
688       g_param_spec_boxed ("video-capture-supported-caps",
689           "Video capture supported caps",
690           "Formats supported for capturing videos represented as GstCaps",
691           GST_TYPE_CAPS, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
692
693   g_object_class_install_property (object_class,
694       PROP_IMAGE_CAPTURE_CAPS,
695       g_param_spec_boxed ("image-capture-caps",
696           "Image capture caps",
697           "Caps for image capture",
698           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
699
700   g_object_class_install_property (object_class,
701       PROP_VIDEO_CAPTURE_CAPS,
702       g_param_spec_boxed ("video-capture-caps",
703           "Video capture caps",
704           "Caps for video capture",
705           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
706
707   g_object_class_install_property (object_class, PROP_POST_PREVIEWS,
708       g_param_spec_boolean ("post-previews", "Post Previews",
709           "If capture preview images should be posted to the bus",
710           DEFAULT_POST_PREVIEWS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
711
712   g_object_class_install_property (object_class, PROP_PREVIEW_CAPS,
713       g_param_spec_boxed ("preview-caps", "Preview caps",
714           "The caps of the preview image to be posted",
715           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
716
717   g_object_class_install_property (object_class, PROP_VIDEO_ENCODING_PROFILE,
718       g_param_spec_object ("video-profile", "Video Profile",
719           "The GstEncodingProfile to use for video recording. Audio is enabled "
720           "when this profile supports audio.", GST_TYPE_ENCODING_PROFILE,
721           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
722
723   g_object_class_install_property (object_class, PROP_IMAGE_FILTER,
724       g_param_spec_object ("image-filter", "Image filter",
725           "The element that will process captured image frames. (Should be"
726           " set on NULL state)",
727           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
728
729   g_object_class_install_property (object_class, PROP_VIDEO_FILTER,
730       g_param_spec_object ("video-filter", "Video filter",
731           "The element that will process captured video frames. (Should be"
732           " set on NULL state)",
733           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
734
735   g_object_class_install_property (object_class, PROP_VIEWFINDER_FILTER,
736       g_param_spec_object ("viewfinder-filter", "Viewfinder filter",
737           "The element that will process frames going to the viewfinder."
738           " (Should be set on NULL state)",
739           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
740
741   g_object_class_install_property (object_class, PROP_AUDIO_FILTER,
742       g_param_spec_object ("audio-filter", "Audio filter",
743           "The element that will process captured audio buffers when recording"
744           ". (Should be set on NULL state)",
745           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
746
747   g_object_class_install_property (object_class, PROP_PREVIEW_FILTER,
748       g_param_spec_object ("preview-filter", "Preview filter",
749           "The element that will process preview buffers."
750           " (Should be set on NULL state)",
751           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
752
753   g_object_class_install_property (object_class, PROP_VIEWFINDER_SINK,
754       g_param_spec_object ("viewfinder-sink", "Viewfinder sink",
755           "The video sink of the viewfinder. It is only taken into use"
756           " on the next null to ready transition",
757           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
758
759   g_object_class_install_property (object_class,
760       PROP_VIEWFINDER_CAPS,
761       g_param_spec_boxed ("viewfinder-caps",
762           "Viewfinder caps",
763           "Restricts the caps that can be used on the viewfinder",
764           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
765
766   g_object_class_install_property (object_class, PROP_ZOOM,
767       g_param_spec_float ("zoom", "Zoom",
768           "Digital zoom factor (e.g. 1.5 means 1.5x)", MIN_ZOOM, MAX_ZOOM,
769           DEFAULT_ZOOM, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
770
771   g_object_class_install_property (object_class, PROP_MAX_ZOOM,
772       g_param_spec_float ("max-zoom", "Maximum zoom level (note: may change "
773           "depending on resolution/implementation)",
774           "Digital zoom factor (e.g. 1.5 means 1.5x)", MIN_ZOOM, G_MAXFLOAT,
775           MAX_ZOOM, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
776
777   /* TODO
778    * Review before stable
779    * - One problem with using encodebin for images here is how jifmux
780    *   autoplugging works. We need to give it a higher rank and fix its
781    *   caps (it has image/jpeg on sink and src pads). Preliminary tests
782    *   show that jifmux is picked if image/jpeg is the caps of a container
783    *   profile. So this could work.
784    * - There seems to be a problem with encodebin for images currently as
785    *   it autoplugs a videorate that only starts outputting buffers after
786    *   getting the 2nd buffer.
787    */
788   g_object_class_install_property (object_class, PROP_IMAGE_ENCODING_PROFILE,
789       g_param_spec_object ("image-profile", "Image Profile",
790           "The GstEncodingProfile to use for image captures.",
791           GST_TYPE_ENCODING_PROFILE,
792           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
793
794
795   g_object_class_install_property (object_class, PROP_IDLE,
796       g_param_spec_boolean ("idle", "Idle",
797           "If camerabin2 is idle (not doing captures).", DEFAULT_IDLE,
798           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
799
800   /* TODO review before going stable
801    * We have viewfinder-supported-caps that returns the caps that the
802    * camerasrc can produce on its viewfinder pad, this could easily be
803    * confused with what the viewfinder-sink accepts.
804    *
805    * Do we want to add a 'viewfinder-sink-supported-caps' or maybe change
806    * the name of this property?
807    */
808   g_object_class_install_property (object_class,
809       PROP_VIEWFINDER_SUPPORTED_CAPS,
810       g_param_spec_boxed ("viewfinder-supported-caps",
811           "Camera source Viewfinder pad supported caps",
812           "The caps that the camera source can produce on the viewfinder pad",
813           GST_TYPE_CAPS, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
814
815    /**
816     * GstCameraBin:flags
817     *
818     * Control the behaviour of encodebin.
819     */
820   g_object_class_install_property (object_class, PROP_FLAGS,
821       g_param_spec_flags ("flags", "Flags", "Flags to control behaviour",
822           GST_TYPE_CAM_FLAGS, DEFAULT_FLAGS,
823           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
824
825   /**
826    * GstCameraBin2::capture-start:
827    * @camera: the camera bin element
828    *
829    * Starts image capture or video recording depending on the Mode.
830    */
831   camerabin_signals[START_CAPTURE_SIGNAL] =
832       g_signal_new ("start-capture",
833       G_TYPE_FROM_CLASS (klass),
834       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
835       G_STRUCT_OFFSET (GstCameraBin2Class, start_capture),
836       NULL, NULL, NULL, G_TYPE_NONE, 0);
837
838   /**
839    * GstCameraBin2::capture-stop:
840    * @camera: the camera bin element
841    */
842   camerabin_signals[STOP_CAPTURE_SIGNAL] =
843       g_signal_new ("stop-capture",
844       G_TYPE_FROM_CLASS (klass),
845       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
846       G_STRUCT_OFFSET (GstCameraBin2Class, stop_capture),
847       NULL, NULL, NULL, G_TYPE_NONE, 0);
848
849   gst_type_mark_as_plugin_api (GST_TYPE_CAM_FLAGS, 0);
850 }
851
852 static void
853 gst_camera_bin_init (GstCameraBin2 * camera)
854 {
855   camera->post_previews = DEFAULT_POST_PREVIEWS;
856   camera->mode = DEFAULT_MODE;
857   camera->location = g_strdup (DEFAULT_LOCATION);
858   camera->viewfinderbin = gst_element_factory_make ("viewfinderbin", "vf-bin");
859   camera->zoom = DEFAULT_ZOOM;
860   camera->max_zoom = MAX_ZOOM;
861   camera->flags = DEFAULT_FLAGS;
862   g_mutex_init (&camera->preview_list_mutex);
863   g_mutex_init (&camera->image_capture_mutex);
864   g_mutex_init (&camera->video_capture_mutex);
865   g_cond_init (&camera->video_state_cond);
866
867   /* capsfilters are created here as we proxy their caps properties and
868    * this way we avoid having to store the caps while on NULL state to
869    * set them later */
870   camera->videobin_capsfilter = gst_element_factory_make ("capsfilter",
871       "videobin-capsfilter");
872   camera->imagebin_capsfilter = gst_element_factory_make ("capsfilter",
873       "imagebin-capsfilter");
874   camera->viewfinderbin_capsfilter = gst_element_factory_make ("capsfilter",
875       "viewfinderbin-capsfilter");
876
877   gst_bin_add_many (GST_BIN (camera),
878       gst_object_ref (camera->viewfinderbin),
879       gst_object_ref (camera->videobin_capsfilter),
880       gst_object_ref (camera->imagebin_capsfilter),
881       gst_object_ref (camera->viewfinderbin_capsfilter), NULL);
882
883   /* these elements are only added if they are going to be used */
884   camera->audio_capsfilter = gst_element_factory_make ("capsfilter",
885       "audio-capsfilter");
886   camera->audio_volume = gst_element_factory_make ("volume", "audio-volume");
887 }
888
889 static void
890 gst_image_capture_bin_post_image_done (GstCameraBin2 * camera,
891     const gchar * filename)
892 {
893   GstMessage *msg;
894
895   g_return_if_fail (filename != NULL);
896
897   msg = gst_message_new_element (GST_OBJECT_CAST (camera),
898       gst_structure_new ("image-done", "filename", G_TYPE_STRING,
899           filename, NULL));
900
901   if (!gst_element_post_message (GST_ELEMENT_CAST (camera), msg))
902     GST_WARNING_OBJECT (camera, "Failed to post image-done message");
903 }
904
905 static void
906 gst_video_capture_bin_post_video_done (GstCameraBin2 * camera)
907 {
908   GstMessage *msg;
909
910   msg = gst_message_new_element (GST_OBJECT_CAST (camera),
911       gst_structure_new_empty ("video-done"));
912
913   if (!gst_element_post_message (GST_ELEMENT_CAST (camera), msg))
914     GST_WARNING_OBJECT (camera, "Failed to post video-done message");
915 }
916
917 static void
918 gst_camera_bin_skip_next_preview (GstCameraBin2 * camerabin)
919 {
920   gchar *location;
921
922   g_mutex_lock (&camerabin->preview_list_mutex);
923   if (camerabin->preview_location_list) {
924     location = camerabin->preview_location_list->data;
925     GST_DEBUG_OBJECT (camerabin, "Skipping preview for %s", location);
926     g_free (location);
927     camerabin->preview_location_list =
928         g_slist_delete_link (camerabin->preview_location_list,
929         camerabin->preview_location_list);
930     GST_CAMERA_BIN2_PROCESSING_DEC (camerabin);
931   } else {
932     GST_WARNING_OBJECT (camerabin, "No previews to skip");
933   }
934   g_mutex_unlock (&camerabin->preview_list_mutex);
935 }
936
937 static void
938 gst_camera_bin_finish_video_file (GstCameraBin2 * camerabin)
939 {
940   /* make sure the file is closed */
941   gst_element_set_state (camerabin->videosink, GST_STATE_NULL);
942
943   gst_video_capture_bin_post_video_done (camerabin);
944   GST_CAMERA_BIN2_PROCESSING_DEC (camerabin);
945 }
946
947 static gpointer
948 gst_camera_bin_video_reset_elements (gpointer u_data)
949 {
950   GstCameraBin2 *camerabin = GST_CAMERA_BIN2_CAST (u_data);
951
952   GST_DEBUG_OBJECT (camerabin, "Resetting video elements state");
953   g_mutex_lock (&camerabin->video_capture_mutex);
954
955   gst_camera_bin_finish_video_file (camerabin);
956
957   /* reset element states to clear eos/flushing pads */
958   gst_element_set_state (camerabin->video_encodebin, GST_STATE_READY);
959   gst_element_set_state (camerabin->videobin_capsfilter, GST_STATE_READY);
960   if (camerabin->video_filter) {
961     gst_element_set_state (camerabin->video_filter, GST_STATE_READY);
962     gst_element_sync_state_with_parent (camerabin->video_filter);
963   }
964   gst_element_sync_state_with_parent (camerabin->videobin_capsfilter);
965   gst_element_sync_state_with_parent (camerabin->video_encodebin);
966
967   if (camerabin->audio_src) {
968     gst_element_set_state (camerabin->audio_capsfilter, GST_STATE_READY);
969     gst_element_set_state (camerabin->audio_volume, GST_STATE_READY);
970     gst_element_set_state (camerabin->audio_src, GST_STATE_READY);
971
972     if (camerabin->audio_filter) {
973       gst_element_set_state (camerabin->audio_filter, GST_STATE_READY);
974       gst_element_sync_state_with_parent (camerabin->audio_filter);
975     }
976
977     gst_element_sync_state_with_parent (camerabin->audio_capsfilter);
978     gst_element_sync_state_with_parent (camerabin->audio_volume);
979
980   }
981
982   GST_DEBUG_OBJECT (camerabin, "Setting video state to idle");
983   camerabin->video_state = GST_CAMERA_BIN_VIDEO_IDLE;
984   g_cond_signal (&camerabin->video_state_cond);
985   g_mutex_unlock (&camerabin->video_capture_mutex);
986
987   gst_object_unref (camerabin);
988   return NULL;
989 }
990
991 static void
992 gst_camera_bin_handle_message (GstBin * bin, GstMessage * message)
993 {
994   GstCameraBin2 *camerabin = GST_CAMERA_BIN2_CAST (bin);
995   gboolean dec_counter = FALSE;
996
997   switch (GST_MESSAGE_TYPE (message)) {
998     case GST_MESSAGE_ELEMENT:{
999       const GstStructure *structure = gst_message_get_structure (message);
1000       const gchar *filename;
1001
1002       if (gst_structure_has_name (structure, "GstMultiFileSink")) {
1003         filename = gst_structure_get_string (structure, "filename");
1004         GST_DEBUG_OBJECT (bin, "Got file save message from multifilesink, "
1005             "image %s has been saved", filename);
1006         if (filename) {
1007           gst_image_capture_bin_post_image_done (GST_CAMERA_BIN2_CAST (bin),
1008               filename);
1009         }
1010         dec_counter = TRUE;
1011       } else if (gst_structure_has_name (structure, "preview-image")) {
1012         gchar *location = NULL;
1013
1014         g_mutex_lock (&camerabin->preview_list_mutex);
1015         if (camerabin->preview_location_list) {
1016           location = camerabin->preview_location_list->data;
1017           camerabin->preview_location_list =
1018               g_slist_delete_link (camerabin->preview_location_list,
1019               camerabin->preview_location_list);
1020           GST_DEBUG_OBJECT (camerabin, "Adding preview location to preview "
1021               "message '%s'", location);
1022         } else {
1023           GST_WARNING_OBJECT (camerabin, "Unexpected preview message received, "
1024               "won't be able to put location field into the message. This can "
1025               "happen if the source is posting previews while camerabin2 is "
1026               "shutting down");
1027         }
1028         g_mutex_unlock (&camerabin->preview_list_mutex);
1029
1030         if (location) {
1031           GstStructure *new_structure;
1032           GValue value = { 0 };
1033
1034           g_value_init (&value, G_TYPE_STRING);
1035           g_value_take_string (&value, location);
1036
1037           /* need to do a copy because the structure isn't mutable */
1038           new_structure = gst_structure_copy (structure);
1039           gst_structure_take_value (new_structure, "location", &value);
1040
1041           gst_message_unref (message);
1042           message =
1043               gst_message_new_element (GST_OBJECT_CAST (camerabin),
1044               new_structure);
1045         }
1046
1047         GST_LOG_OBJECT (bin, "received preview-image message");
1048         dec_counter = TRUE;
1049       }
1050     }
1051       break;
1052     case GST_MESSAGE_WARNING:{
1053       GError *err = NULL;
1054       gchar *debug = NULL;
1055
1056       gst_message_parse_warning (message, &err, &debug);
1057       if (err->domain == GST_RESOURCE_ERROR) {
1058         /* some capturing failed */
1059         GST_WARNING_OBJECT (bin, "Capture failed, reason: %s - %s",
1060             err->message, debug);
1061         if (camerabin->post_previews) {
1062           gst_camera_bin_skip_next_preview (camerabin);
1063         }
1064         dec_counter = TRUE;
1065       }
1066       g_error_free (err);
1067       g_free (debug);
1068     }
1069       break;
1070     case GST_MESSAGE_EOS:{
1071       GstElement *src = GST_ELEMENT (GST_MESSAGE_SRC (message));
1072       if (src == GST_CAMERA_BIN2_CAST (bin)->videosink) {
1073
1074         g_mutex_lock (&camerabin->video_capture_mutex);
1075         GST_DEBUG_OBJECT (bin, "EOS from video branch");
1076         if (camerabin->video_state == GST_CAMERA_BIN_VIDEO_FINISHING) {
1077           if (!g_thread_try_new ("reset-element-thread",
1078                   gst_camera_bin_video_reset_elements,
1079                   gst_object_ref (camerabin), NULL)) {
1080             GST_WARNING_OBJECT (camerabin,
1081                 "Failed to create thread to "
1082                 "reset video elements' state, video recordings may not work "
1083                 "anymore");
1084             gst_object_unref (camerabin);
1085             camerabin->video_state = GST_CAMERA_BIN_VIDEO_IDLE;
1086           }
1087         } else if (camerabin->video_state == GST_CAMERA_BIN_VIDEO_IDLE) {
1088           GST_DEBUG_OBJECT (camerabin, "Received EOS from video branch but "
1089               "video recording is idle, ignoring");
1090         } else {
1091           GST_WARNING_OBJECT (camerabin, "Received EOS from video branch but "
1092               "video is recording and stop-capture wasn't requested");
1093           g_assert_not_reached ();
1094         }
1095
1096         g_mutex_unlock (&camerabin->video_capture_mutex);
1097       }
1098     }
1099       break;
1100     default:
1101       break;
1102   }
1103
1104   GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1105
1106   if (dec_counter)
1107     GST_CAMERA_BIN2_PROCESSING_DEC (camerabin);
1108 }
1109
1110 /*
1111  * Transforms:
1112  * ... ! previous_element [ ! current_filter ] ! next_element ! ...
1113  *
1114  * into:
1115  * ... ! previous_element [ ! new_filter ] ! next_element ! ...
1116  *
1117  * Where current_filter and new_filter might or might not be NULL
1118  */
1119 static void
1120 gst_camera_bin_check_and_replace_filter (GstCameraBin2 * camera,
1121     GstElement ** current_filter, GstElement * new_filter,
1122     GstElement * previous_element, GstElement * next_element,
1123     const gchar * prev_elem_pad)
1124 {
1125   if (*current_filter == new_filter) {
1126     GST_DEBUG_OBJECT (camera, "Current filter is the same as the previous, "
1127         "no switch needed.");
1128     return;
1129   }
1130
1131   GST_DEBUG_OBJECT (camera, "Replacing current filter (%s) with new filter "
1132       "(%s)", *current_filter ? GST_ELEMENT_NAME (*current_filter) : "null",
1133       new_filter ? GST_ELEMENT_NAME (new_filter) : "null");
1134
1135   if (*current_filter) {
1136     gst_bin_remove (GST_BIN_CAST (camera), *current_filter);
1137     gst_object_unref (*current_filter);
1138     *current_filter = NULL;
1139   } else {
1140     /* unlink the pads */
1141     gst_element_unlink (previous_element, next_element);
1142   }
1143
1144   if (new_filter) {
1145     *current_filter = gst_object_ref (new_filter);
1146     gst_bin_add (GST_BIN_CAST (camera), gst_object_ref (new_filter));
1147   }
1148
1149   if (prev_elem_pad) {
1150     if (new_filter) {
1151       gst_element_link_pads (previous_element, prev_elem_pad, new_filter, NULL);
1152       gst_element_link (new_filter, next_element);
1153     } else {
1154       gst_element_link_pads (previous_element, prev_elem_pad, next_element,
1155           NULL);
1156     }
1157   } else {
1158     if (new_filter)
1159       gst_element_link_many (previous_element, new_filter, next_element, NULL);
1160     else
1161       gst_element_link (previous_element, next_element);
1162   }
1163 }
1164
1165 static void
1166 encodebin_element_added (GstElement * encodebin, GstElement * new_element,
1167     GstCameraBin2 * camera)
1168 {
1169   GstElementFactory *factory = gst_element_get_factory (new_element);
1170
1171   if (factory != NULL) {
1172     if (strcmp (GST_OBJECT_NAME (factory), "audiorate") == 0 ||
1173         strcmp (GST_OBJECT_NAME (factory), "videorate") == 0) {
1174       g_object_set (new_element, "skip-to-first", TRUE, NULL);
1175     }
1176   }
1177
1178   if (GST_IS_TAG_SETTER (new_element)) {
1179     GstTagSetter *tagsetter = GST_TAG_SETTER (new_element);
1180
1181     gst_tag_setter_set_tag_merge_mode (tagsetter, GST_TAG_MERGE_REPLACE);
1182   }
1183 }
1184
1185 #define VIDEO_PAD 1
1186 #define AUDIO_PAD 2
1187 static GstPad *
1188 encodebin_find_pad (GstCameraBin2 * camera, GstElement * encodebin,
1189     gint pad_type)
1190 {
1191   GValue value = { 0 };
1192   GstPad *pad = NULL;
1193   GstIterator *iter;
1194   gboolean done;
1195
1196   GST_DEBUG_OBJECT (camera, "Looking at encodebin pads, searching for %s pad",
1197       pad_type == VIDEO_PAD ? "video" : "audio");
1198
1199   iter = gst_element_iterate_sink_pads (encodebin);
1200   done = FALSE;
1201   while (!done) {
1202     switch (gst_iterator_next (iter, &value)) {
1203       case GST_ITERATOR_OK:
1204         pad = g_value_dup_object (&value);
1205         g_value_unset (&value);
1206         if (pad_type == VIDEO_PAD) {
1207           if (strstr (GST_PAD_NAME (pad), "video") != NULL) {
1208             GST_DEBUG_OBJECT (camera, "Found video pad %s", GST_PAD_NAME (pad));
1209             done = TRUE;
1210             break;
1211           }
1212         } else if (pad_type == AUDIO_PAD) {
1213           if (strstr (GST_PAD_NAME (pad), "audio") != NULL) {
1214             GST_DEBUG_OBJECT (camera, "Found audio pad %s", GST_PAD_NAME (pad));
1215             done = TRUE;
1216             break;
1217           }
1218         }
1219         gst_object_unref (pad);
1220         pad = NULL;
1221         break;
1222       case GST_ITERATOR_RESYNC:
1223         gst_iterator_resync (iter);
1224         break;
1225       case GST_ITERATOR_ERROR:
1226         pad = NULL;
1227         done = TRUE;
1228         break;
1229       case GST_ITERATOR_DONE:
1230         pad = NULL;
1231         done = TRUE;
1232         break;
1233     }
1234   }
1235   gst_iterator_free (iter);
1236
1237   /* no static pad, try requesting one */
1238   if (pad == NULL) {
1239     GstElementClass *klass;
1240     GstPadTemplate *tmpl;
1241
1242     GST_DEBUG_OBJECT (camera, "No pads found, trying to request one");
1243
1244     klass = GST_ELEMENT_GET_CLASS (encodebin);
1245     tmpl = gst_element_class_get_pad_template (klass, pad_type == VIDEO_PAD ?
1246         "video_%u" : "audio_%u");
1247
1248     if (!tmpl) {
1249       GST_DEBUG_OBJECT (camera, "No templates found, can't request pad");
1250       return NULL;
1251     }
1252
1253     pad = gst_element_request_pad (encodebin, tmpl, NULL, NULL);
1254     GST_DEBUG_OBJECT (camera, "Got pad: %s", pad ? GST_PAD_NAME (pad) : "null");
1255   }
1256
1257   return pad;
1258 }
1259
1260 static gboolean
1261 gst_camera_bin_video_profile_has_audio (GstCameraBin2 * camera)
1262 {
1263   const GList *list;
1264
1265   g_return_val_if_fail (camera->video_profile != NULL, FALSE);
1266
1267   if (GST_IS_ENCODING_VIDEO_PROFILE (camera->video_profile))
1268     return FALSE;
1269
1270   for (list =
1271       gst_encoding_container_profile_get_profiles ((GstEncodingContainerProfile
1272               *) camera->video_profile); list; list = g_list_next (list)) {
1273     GstEncodingProfile *profile = (GstEncodingProfile *) list->data;
1274
1275     if (GST_IS_ENCODING_AUDIO_PROFILE (profile))
1276       return TRUE;
1277   }
1278
1279   return FALSE;
1280 }
1281
1282 static GstPadLinkReturn
1283 gst_camera_bin_link_encodebin (GstCameraBin2 * camera, GstElement * encodebin,
1284     GstElement * element, gint padtype)
1285 {
1286   GstPadLinkReturn ret;
1287   GstPad *srcpad;
1288   GstPad *sinkpad = NULL;
1289
1290   srcpad = gst_element_get_static_pad (element, "src");
1291   g_assert (srcpad != NULL);
1292
1293   sinkpad = encodebin_find_pad (camera, encodebin, padtype);
1294
1295   /* there may be no available sink pad for encodebin in some situations:
1296    * e.g. missing elements or incompatible padtype */
1297   if (sinkpad == NULL) {
1298     gst_object_unref (srcpad);
1299     return GST_PAD_LINK_REFUSED;
1300   }
1301
1302   ret = gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_CAPS);
1303   gst_object_unref (sinkpad);
1304   gst_object_unref (srcpad);
1305
1306   return ret;
1307 }
1308
1309 static void
1310 gst_camera_bin_src_notify_max_zoom_cb (GObject * self, GParamSpec * pspec,
1311     gpointer user_data)
1312 {
1313   GParamSpecFloat *zoom_pspec;
1314   GstCameraBin2 *camera = (GstCameraBin2 *) user_data;
1315
1316   g_object_get (self, "max-zoom", &camera->max_zoom, NULL);
1317   GST_DEBUG_OBJECT (camera, "Max zoom updated to %f", camera->max_zoom);
1318
1319   /* update zoom pspec */
1320   zoom_pspec =
1321       G_PARAM_SPEC_FLOAT (g_object_class_find_property (G_OBJECT_GET_CLASS
1322           (G_OBJECT (camera)), "zoom"));
1323   zoom_pspec->maximum = camera->max_zoom;
1324
1325   g_object_notify (G_OBJECT (camera), "max-zoom");
1326 }
1327
1328 static void
1329 gst_camera_bin_src_notify_zoom_cb (GObject * self, GParamSpec * pspec,
1330     gpointer user_data)
1331 {
1332   GstCameraBin2 *camera = (GstCameraBin2 *) user_data;
1333
1334   g_object_get (self, "zoom", &camera->zoom, NULL);
1335   GST_DEBUG_OBJECT (camera, "Zoom updated to %f", camera->zoom);
1336   g_object_notify (G_OBJECT (camera), "zoom");
1337 }
1338
1339 static GstPadProbeReturn
1340 gst_camera_bin_image_src_buffer_probe (GstPad * pad, GstPadProbeInfo * info,
1341     gpointer data)
1342 {
1343   GstPadProbeReturn ret = GST_PAD_PROBE_OK;
1344   GstCameraBin2 *camerabin = data;
1345   GstEvent *evt;
1346   gchar *location = NULL;
1347   GstPad *peer;
1348   GstTagList *tags;
1349
1350   g_mutex_lock (&camerabin->image_capture_mutex);
1351
1352   /* Push pending image tags */
1353   if (camerabin->image_tags_list) {
1354     tags = camerabin->image_tags_list->data;
1355     camerabin->image_tags_list =
1356         g_slist_delete_link (camerabin->image_tags_list,
1357         camerabin->image_tags_list);
1358     GST_DEBUG_OBJECT (camerabin, "Pushing tags from application: %"
1359         GST_PTR_FORMAT, tags);
1360     if (tags) {
1361       peer = gst_pad_get_peer (pad);
1362       gst_pad_send_event (peer, gst_event_new_tag (tags));
1363       gst_object_unref (peer);
1364     }
1365   } else {
1366     GST_DEBUG_OBJECT (camerabin, "No tags from application to send");
1367   }
1368
1369   /* Push image location event */
1370   if (camerabin->image_location_list) {
1371     location = camerabin->image_location_list->data;
1372     camerabin->image_location_list =
1373         g_slist_delete_link (camerabin->image_location_list,
1374         camerabin->image_location_list);
1375     GST_DEBUG_OBJECT (camerabin, "Sending image location change to '%s'",
1376         location);
1377   } else {
1378     GST_DEBUG_OBJECT (camerabin, "No filename location change to send");
1379     g_mutex_unlock (&camerabin->image_capture_mutex);
1380     return ret;
1381   }
1382   g_mutex_unlock (&camerabin->image_capture_mutex);
1383
1384   if (location) {
1385     evt = gst_camera_bin_new_event_file_location (location);
1386     peer = gst_pad_get_peer (pad);
1387     gst_pad_send_event (peer, evt);
1388     gst_object_unref (peer);
1389     g_free (location);
1390   } else {
1391     /* This means we don't have to encode the capture, it is used for
1392      * signaling the application just wants the preview */
1393     ret = GST_PAD_PROBE_DROP;
1394     GST_CAMERA_BIN2_PROCESSING_DEC (camerabin);
1395   }
1396
1397   return ret;
1398 }
1399
1400
1401 static GstPadProbeReturn
1402 gst_camera_bin_image_sink_event_probe (GstPad * pad, GstPadProbeInfo * info,
1403     gpointer data)
1404 {
1405   GstCameraBin2 *camerabin = data;
1406   GstEvent *event = GST_EVENT (info->data);
1407
1408   switch (GST_EVENT_TYPE (event)) {
1409     case GST_EVENT_CUSTOM_DOWNSTREAM:{
1410       if (gst_event_has_name (event, "new-location")) {
1411         const GstStructure *structure = gst_event_get_structure (event);
1412         const gchar *filename = gst_structure_get_string (structure,
1413             "location");
1414
1415         gst_element_set_state (camerabin->imagesink, GST_STATE_NULL);
1416         GST_DEBUG_OBJECT (camerabin, "Setting filename to imagesink: %s",
1417             filename);
1418         g_object_set (camerabin->imagesink, "location", filename, NULL);
1419         if (gst_element_set_state (camerabin->imagesink, GST_STATE_PLAYING) ==
1420             GST_STATE_CHANGE_FAILURE) {
1421           /* Resets the latest state change return, that would be a failure
1422            * and could cause problems in a camerabin2 state change */
1423           gst_element_set_state (camerabin->imagesink, GST_STATE_NULL);
1424         }
1425       }
1426     }
1427       break;
1428     default:
1429       break;
1430   }
1431
1432   return GST_PAD_PROBE_OK;
1433 }
1434
1435 static GstPadProbeReturn
1436 gst_camera_bin_audio_src_data_probe (GstPad * pad, GstPadProbeInfo * info,
1437     gpointer data)
1438 {
1439   GstCameraBin2 *camera = data;
1440   gboolean ret = GST_PAD_PROBE_OK;
1441
1442   if (GST_IS_BUFFER (info->data)) {
1443     if (G_UNLIKELY (camera->audio_send_newseg)) {
1444       GstBuffer *buf = GST_BUFFER_CAST (info->data);
1445       GstClockTime ts = GST_BUFFER_TIMESTAMP (buf);
1446       GstPad *peer;
1447       GstSegment segment;
1448
1449       if (!GST_CLOCK_TIME_IS_VALID (ts)) {
1450         ts = 0;
1451       }
1452
1453       peer = gst_pad_get_peer (pad);
1454       g_return_val_if_fail (peer != NULL, TRUE);
1455
1456       gst_segment_init (&segment, GST_FORMAT_TIME);
1457       segment.start = ts;
1458       gst_pad_send_event (peer, gst_event_new_segment (&segment));
1459
1460       gst_object_unref (peer);
1461
1462       camera->audio_send_newseg = FALSE;
1463     }
1464   } else {
1465     GstEvent *event = GST_EVENT_CAST (data);
1466     if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
1467       /* we only let an EOS pass when the user is stopping a capture */
1468       if (camera->audio_drop_eos) {
1469         ret = GST_PAD_PROBE_DROP;
1470       } else {
1471         camera->audio_drop_eos = TRUE;
1472         /* should already be false, but reinforce in case no buffers get
1473          * pushed */
1474         camera->audio_send_newseg = FALSE;
1475       }
1476     } else if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1477       ret = GST_PAD_PROBE_DROP;
1478     }
1479   }
1480
1481   return ret;
1482 }
1483
1484 /**
1485  * gst_camera_bin_create_elements:
1486  * @param camera: the #GstCameraBin2
1487  *
1488  * Creates all elements inside #GstCameraBin2
1489  *
1490  * Each of the pads on the camera source is linked as follows:
1491  * .pad ! queue ! capsfilter ! correspondingbin
1492  *
1493  * Where 'correspondingbin' is the bin appropriate for
1494  * the camera source pad.
1495  */
1496 static gboolean
1497 gst_camera_bin_create_elements (GstCameraBin2 * camera)
1498 {
1499   gboolean new_src = FALSE;
1500   gboolean new_audio_src = FALSE;
1501   gboolean has_audio;
1502   gboolean profile_switched = FALSE;
1503   const gchar *missing_element_name;
1504   gint encbin_flags = 0;
1505
1506   if (!camera->elements_created) {
1507     /* Check that elements created in _init were really created */
1508     if (!(camera->audio_capsfilter && camera->videobin_capsfilter &&
1509             camera->imagebin_capsfilter && camera->viewfinderbin_capsfilter)) {
1510       missing_element_name = "capsfilter";
1511       goto missing_element;
1512     }
1513
1514     camera->video_encodebin =
1515         gst_element_factory_make ("encodebin", "video-encodebin");
1516     if (!camera->video_encodebin) {
1517       missing_element_name = "encodebin";
1518       goto missing_element;
1519     }
1520     camera->video_encodebin_signal_id =
1521         g_signal_connect (camera->video_encodebin, "element-added",
1522         (GCallback) encodebin_element_added, camera);
1523
1524     camera->videosink =
1525         gst_element_factory_make ("filesink", "videobin-filesink");
1526     if (!camera->videosink) {
1527       missing_element_name = "filesink";
1528       goto missing_element;
1529     }
1530     g_object_set (camera->videosink, "async", FALSE, NULL);
1531
1532     /* audio elements */
1533     if (!camera->audio_volume) {
1534       missing_element_name = "volume";
1535       goto missing_element;
1536     }
1537
1538     if (camera->video_profile == NULL) {
1539       GstEncodingContainerProfile *prof;
1540       GstCaps *caps;
1541
1542       caps = gst_caps_new_empty_simple ("application/ogg");
1543       prof = gst_encoding_container_profile_new ("ogg", "theora+vorbis+ogg",
1544           caps, NULL);
1545       gst_caps_unref (caps);
1546
1547       caps = gst_caps_new_empty_simple ("video/x-theora");
1548       if (!gst_encoding_container_profile_add_profile (prof,
1549               (GstEncodingProfile *) gst_encoding_video_profile_new (caps,
1550                   NULL, NULL, 1))) {
1551         GST_WARNING_OBJECT (camera, "Failed to create encoding profiles");
1552       }
1553       gst_caps_unref (caps);
1554
1555       caps = gst_caps_new_empty_simple ("audio/x-vorbis");
1556       if (!gst_encoding_container_profile_add_profile (prof,
1557               (GstEncodingProfile *) gst_encoding_audio_profile_new (caps,
1558                   NULL, NULL, 1))) {
1559         GST_WARNING_OBJECT (camera, "Failed to create encoding profiles");
1560       }
1561       gst_caps_unref (caps);
1562
1563       camera->video_profile = (GstEncodingProfile *) prof;
1564       camera->video_profile_switch = TRUE;
1565     }
1566
1567     camera->image_encodebin =
1568         gst_element_factory_make ("encodebin", "image-encodebin");
1569     if (!camera->image_encodebin) {
1570       missing_element_name = "encodebin";
1571       goto missing_element;
1572     }
1573     /* durations have no meaning for image captures */
1574     g_object_set (camera->image_encodebin, "queue-time-max", (guint64) 0, NULL);
1575
1576     camera->image_encodebin_signal_id =
1577         g_signal_connect (camera->image_encodebin, "element-added",
1578         (GCallback) encodebin_element_added, camera);
1579
1580     camera->imagesink =
1581         gst_element_factory_make ("multifilesink", "imagebin-filesink");
1582     if (!camera->imagesink) {
1583       missing_element_name = "multifilesink";
1584       goto missing_element;
1585     }
1586     g_object_set (camera->imagesink, "async", FALSE, "post-messages", TRUE,
1587         NULL);
1588
1589     if (camera->image_profile == NULL) {
1590       GstEncodingVideoProfile *vprof;
1591       GstCaps *caps;
1592
1593       caps = gst_caps_new_empty_simple ("image/jpeg");
1594       vprof = gst_encoding_video_profile_new (caps, NULL, NULL, 1);
1595       gst_encoding_video_profile_set_variableframerate (vprof, TRUE);
1596
1597       gst_caps_unref (caps);
1598       camera->image_profile = (GstEncodingProfile *) vprof;
1599       camera->image_profile_switch = TRUE;
1600     }
1601
1602     camera->viewfinderbin_queue =
1603         gst_element_factory_make ("queue", "viewfinderbin-queue");
1604     if (!camera->viewfinderbin_queue) {
1605       missing_element_name = "queue";
1606       goto missing_element;
1607     }
1608
1609     g_object_set (camera->viewfinderbin_queue, "leaky", 2, "silent", TRUE,
1610         "max-size-time", (guint64) 0, "max-size-bytes", (guint) 0,
1611         "max-size-buffers", (guint) 1, NULL);
1612
1613     gst_bin_add_many (GST_BIN_CAST (camera),
1614         gst_object_ref (camera->video_encodebin),
1615         gst_object_ref (camera->videosink),
1616         gst_object_ref (camera->image_encodebin),
1617         gst_object_ref (camera->imagesink),
1618         gst_object_ref (camera->viewfinderbin_queue), NULL);
1619
1620     gst_element_link_pads_full (camera->video_encodebin, "src",
1621         camera->videosink, "sink", GST_PAD_LINK_CHECK_NOTHING);
1622     gst_element_link_pads_full (camera->image_encodebin, "src",
1623         camera->imagesink, "sink", GST_PAD_LINK_CHECK_NOTHING);
1624     gst_element_link_pads_full (camera->viewfinderbin_queue, "src",
1625         camera->viewfinderbin_capsfilter, "sink", GST_PAD_LINK_CHECK_CAPS);
1626     gst_element_link_pads_full (camera->viewfinderbin_capsfilter, "src",
1627         camera->viewfinderbin, "sink", GST_PAD_LINK_CHECK_CAPS);
1628
1629     {
1630       /* set an event probe to watch for custom location changes */
1631       GstPad *srcpad;
1632
1633       srcpad = gst_element_get_static_pad (camera->image_encodebin, "src");
1634
1635       gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1636           gst_camera_bin_image_sink_event_probe, camera, NULL);
1637
1638       gst_object_unref (srcpad);
1639     }
1640
1641     /*
1642      * Video can't get into playing as its internal filesink will open
1643      * a file for writing and leave it empty if unused.
1644      *
1645      * Its state is managed using the current mode and the source's
1646      * ready-for-capture notify callback. When we are at video mode and
1647      * the source's ready-for-capture goes to FALSE it means it is
1648      * starting recording, so we should prepare the video bin.
1649      */
1650     gst_element_set_locked_state (camera->videosink, TRUE);
1651     gst_element_set_locked_state (camera->imagesink, TRUE);
1652
1653     g_object_set (camera->videosink, "location", camera->location, NULL);
1654     g_object_set (camera->imagesink, "location", camera->location, NULL);
1655   }
1656
1657   /* propagate the flags property by translating appropriate values
1658    * to GstEncFlags values */
1659   if (camera->flags & GST_CAM_FLAG_NO_AUDIO_CONVERSION)
1660     encbin_flags |= (1 << 0);
1661   if (camera->flags & GST_CAM_FLAG_NO_VIDEO_CONVERSION)
1662     encbin_flags |= (1 << 1);
1663   g_object_set (camera->video_encodebin, "flags", encbin_flags, NULL);
1664
1665   /* image encodebin has only video branch so disable its conversion elements
1666    * appropriately */
1667   if (camera->flags & GST_CAM_FLAG_NO_IMAGE_CONVERSION)
1668     g_object_set (camera->image_encodebin, "flags", (1 << 1), NULL);
1669
1670   g_object_set (camera->viewfinderbin, "disable-converters",
1671       camera->flags & GST_CAM_FLAG_NO_VIEWFINDER_CONVERSION ? TRUE : FALSE,
1672       NULL);
1673
1674   if (camera->video_profile_switch) {
1675     GST_DEBUG_OBJECT (camera, "Switching video-encodebin's profile");
1676     g_object_set (camera->video_encodebin, "profile", camera->video_profile,
1677         NULL);
1678     if (GST_PAD_LINK_FAILED (gst_camera_bin_link_encodebin (camera,
1679                 camera->video_encodebin, camera->videobin_capsfilter,
1680                 VIDEO_PAD))) {
1681       goto fail;
1682     }
1683     camera->video_profile_switch = FALSE;
1684
1685     /* used to trigger relinking further down */
1686     profile_switched = TRUE;
1687   }
1688
1689   if (camera->image_profile_switch) {
1690     GST_DEBUG_OBJECT (camera, "Switching image-encodebin's profile");
1691     g_object_set (camera->image_encodebin, "profile", camera->image_profile,
1692         NULL);
1693     if (GST_PAD_LINK_FAILED (gst_camera_bin_link_encodebin (camera,
1694                 camera->image_encodebin, camera->imagebin_capsfilter,
1695                 VIDEO_PAD))) {
1696       goto fail;
1697     }
1698     camera->image_profile_switch = FALSE;
1699   }
1700
1701   /* check if we need to replace the camera src */
1702   if (camera->src) {
1703     if (camera->user_src && camera->user_src != camera->src) {
1704
1705       if (camera->src_capture_notify_id)
1706         g_signal_handler_disconnect (camera->src,
1707             camera->src_capture_notify_id);
1708
1709       gst_bin_remove (GST_BIN_CAST (camera), camera->src);
1710       gst_object_unref (camera->src);
1711       camera->src = NULL;
1712     }
1713   }
1714
1715   if (!camera->src) {
1716     if (camera->user_src) {
1717       camera->src = gst_object_ref (camera->user_src);
1718     } else {
1719       camera->src =
1720           gst_element_factory_make ("wrappercamerabinsrc", "camerasrc");
1721     }
1722
1723     new_src = TRUE;
1724   }
1725
1726   g_assert (camera->src != NULL);
1727   g_object_set (camera->src, "mode", camera->mode, NULL);
1728   if (camera->src) {
1729     if (g_object_class_find_property (G_OBJECT_GET_CLASS (camera->src),
1730             "preview-caps")) {
1731       g_object_set (camera->src, "post-previews", camera->post_previews,
1732           "preview-caps", camera->preview_caps, "preview-filter",
1733           camera->preview_filter, NULL);
1734     }
1735     g_signal_connect (G_OBJECT (camera->src), "notify::zoom",
1736         (GCallback) gst_camera_bin_src_notify_zoom_cb, camera);
1737     g_object_set (camera->src, "zoom", camera->zoom, NULL);
1738     g_signal_connect (G_OBJECT (camera->src), "notify::max-zoom",
1739         (GCallback) gst_camera_bin_src_notify_max_zoom_cb, camera);
1740   }
1741   if (new_src) {
1742     GstPad *imgsrc = gst_element_get_static_pad (camera->src, "imgsrc");
1743
1744     gst_bin_add (GST_BIN_CAST (camera), gst_object_ref (camera->src));
1745     camera->src_capture_notify_id = g_signal_connect (G_OBJECT (camera->src),
1746         "notify::ready-for-capture",
1747         G_CALLBACK (gst_camera_bin_src_notify_readyforcapture), camera);
1748
1749     if (!gst_element_link_pads (camera->src, "vfsrc",
1750             camera->viewfinderbin_queue, "sink")) {
1751       GST_ERROR_OBJECT (camera,
1752           "Failed to link camera source's vfsrc pad to viewfinder queue");
1753       goto fail;
1754     }
1755
1756     if (!gst_element_link_pads (camera->src, "imgsrc",
1757             camera->imagebin_capsfilter, "sink")) {
1758       GST_ERROR_OBJECT (camera,
1759           "Failed to link camera source's imgsrc pad to image bin capsfilter");
1760       goto fail;
1761     }
1762     if (!gst_element_link_pads (camera->src, "vidsrc",
1763             camera->videobin_capsfilter, "sink")) {
1764       GST_ERROR_OBJECT (camera,
1765           "Failed to link camera source's vidsrc pad to video bin capsfilter");
1766       goto fail;
1767     }
1768
1769     gst_pad_add_probe (imgsrc, GST_PAD_PROBE_TYPE_BUFFER,
1770         gst_camera_bin_image_src_buffer_probe, camera, NULL);
1771     gst_object_unref (imgsrc);
1772   }
1773
1774   gst_camera_bin_check_and_replace_filter (camera, &camera->image_filter,
1775       camera->user_image_filter, camera->src, camera->imagebin_capsfilter,
1776       "imgsrc");
1777   gst_camera_bin_check_and_replace_filter (camera, &camera->video_filter,
1778       camera->user_video_filter, camera->src, camera->videobin_capsfilter,
1779       "vidsrc");
1780   gst_camera_bin_check_and_replace_filter (camera, &camera->viewfinder_filter,
1781       camera->user_viewfinder_filter, camera->viewfinderbin_queue,
1782       camera->viewfinderbin_capsfilter, NULL);
1783
1784   /* check if we need to replace the camera audio src */
1785   has_audio = gst_camera_bin_video_profile_has_audio (camera);
1786   if (camera->audio_src) {
1787     if ((camera->user_audio_src && camera->user_audio_src != camera->audio_src)
1788         || !has_audio) {
1789       gst_bin_remove (GST_BIN_CAST (camera), camera->audio_src);
1790       gst_bin_remove (GST_BIN_CAST (camera), camera->audio_volume);
1791       gst_bin_remove (GST_BIN_CAST (camera), camera->audio_capsfilter);
1792       gst_object_unref (camera->audio_src);
1793       camera->audio_src = NULL;
1794     }
1795   }
1796
1797   if (!camera->audio_src && has_audio) {
1798     if (camera->user_audio_src) {
1799       camera->audio_src = gst_object_ref (camera->user_audio_src);
1800     } else {
1801       camera->audio_src =
1802           gst_element_factory_make (DEFAULT_AUDIO_SRC, "audiosrc");
1803       if (!camera->audio_src) {
1804         missing_element_name = DEFAULT_AUDIO_SRC;
1805         goto missing_element;
1806       }
1807     }
1808
1809     gst_element_set_locked_state (camera->audio_src, TRUE);
1810     new_audio_src = TRUE;
1811   }
1812
1813   if (new_audio_src) {
1814     GstPad *srcpad;
1815
1816     if (g_object_class_find_property (G_OBJECT_GET_CLASS (camera->audio_src),
1817             "provide-clock")) {
1818       g_object_set (camera->audio_src, "provide-clock", FALSE, NULL);
1819     }
1820     gst_bin_add (GST_BIN_CAST (camera), gst_object_ref (camera->audio_src));
1821     gst_bin_add (GST_BIN_CAST (camera), gst_object_ref (camera->audio_volume));
1822     gst_bin_add (GST_BIN_CAST (camera),
1823         gst_object_ref (camera->audio_capsfilter));
1824
1825     gst_element_link_pads_full (camera->audio_src, "src",
1826         camera->audio_volume, "sink", GST_PAD_LINK_CHECK_CAPS);
1827     gst_element_link_pads_full (camera->audio_volume, "src",
1828         camera->audio_capsfilter, "sink", GST_PAD_LINK_CHECK_CAPS);
1829
1830     srcpad = gst_element_get_static_pad (camera->audio_src, "src");
1831
1832     /* drop EOS for audiosrc elements that push them on state_changes
1833      * (basesrc does this) */
1834     gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
1835         gst_camera_bin_audio_src_data_probe, camera, NULL);
1836
1837     gst_object_unref (srcpad);
1838   }
1839   if (has_audio) {
1840     gst_camera_bin_check_and_replace_filter (camera, &camera->audio_filter,
1841         camera->user_audio_filter, camera->audio_src, camera->audio_volume,
1842         "src");
1843   }
1844
1845   if ((profile_switched && has_audio) || new_audio_src) {
1846     if (GST_PAD_LINK_FAILED (gst_camera_bin_link_encodebin (camera,
1847                 camera->video_encodebin, camera->audio_capsfilter,
1848                 AUDIO_PAD))) {
1849       goto fail;
1850     }
1851   }
1852
1853   camera->elements_created = TRUE;
1854   return TRUE;
1855
1856 missing_element:
1857   gst_element_post_message (GST_ELEMENT_CAST (camera),
1858       gst_missing_element_message_new (GST_ELEMENT_CAST (camera),
1859           missing_element_name));
1860   GST_ELEMENT_ERROR (camera, CORE, MISSING_PLUGIN,
1861       (_("Missing element '%s' - check your GStreamer installation."),
1862           missing_element_name), (NULL));
1863   goto fail;
1864
1865 fail:
1866   /* FIXME properly clean up */
1867   return FALSE;
1868 }
1869
1870 static void
1871 _gst_tag_list_unref_maybe (GstTagList * taglist)
1872 {
1873   if (taglist)
1874     gst_tag_list_unref (taglist);
1875 }
1876
1877 static GstStateChangeReturn
1878 gst_camera_bin_change_state (GstElement * element, GstStateChange trans)
1879 {
1880   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1881   GstCameraBin2 *camera = GST_CAMERA_BIN2_CAST (element);
1882
1883
1884   switch (trans) {
1885     case GST_STATE_CHANGE_NULL_TO_READY:
1886       if (!gst_camera_bin_create_elements (camera)) {
1887         return GST_STATE_CHANGE_FAILURE;
1888       }
1889       break;
1890     case GST_STATE_CHANGE_READY_TO_PAUSED:
1891       GST_CAMERA_BIN2_RESET_PROCESSING_COUNTER (camera);
1892       camera->audio_drop_eos = TRUE;
1893       camera->audio_send_newseg = FALSE;
1894       break;
1895     case GST_STATE_CHANGE_PAUSED_TO_READY:
1896       if (GST_STATE (camera->videosink) >= GST_STATE_PAUSED)
1897         gst_element_set_state (camera->videosink, GST_STATE_READY);
1898       if (GST_STATE (camera->imagesink) >= GST_STATE_PAUSED)
1899         gst_element_set_state (camera->imagesink, GST_STATE_READY);
1900       break;
1901     case GST_STATE_CHANGE_READY_TO_NULL:
1902       gst_element_set_state (camera->videosink, GST_STATE_NULL);
1903       gst_element_set_state (camera->imagesink, GST_STATE_NULL);
1904       break;
1905     default:
1906       break;
1907   }
1908
1909   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, trans);
1910
1911   switch (trans) {
1912     case GST_STATE_CHANGE_PAUSED_TO_READY:
1913       if (camera->audio_src && GST_STATE (camera->audio_src) >= GST_STATE_READY)
1914         gst_element_set_state (camera->audio_src, GST_STATE_READY);
1915
1916       gst_tag_setter_reset_tags (GST_TAG_SETTER (camera));
1917       GST_CAMERA_BIN2_RESET_PROCESSING_COUNTER (camera);
1918       camera->video_state = GST_CAMERA_BIN_VIDEO_IDLE;
1919
1920       g_mutex_lock (&camera->image_capture_mutex);
1921       g_slist_foreach (camera->image_location_list, (GFunc) g_free, NULL);
1922       g_slist_free (camera->image_location_list);
1923       camera->image_location_list = NULL;
1924
1925       g_slist_foreach (camera->image_tags_list,
1926           (GFunc) _gst_tag_list_unref_maybe, NULL);
1927       g_slist_free (camera->image_tags_list);
1928       camera->image_tags_list = NULL;
1929       g_mutex_unlock (&camera->image_capture_mutex);
1930
1931       g_mutex_lock (&camera->preview_list_mutex);
1932       g_slist_foreach (camera->preview_location_list, (GFunc) g_free, NULL);
1933       g_slist_free (camera->preview_location_list);
1934       camera->preview_location_list = NULL;
1935       g_mutex_unlock (&camera->preview_list_mutex);
1936
1937       /* explicitly set to READY as they might be outside of the bin */
1938       gst_element_set_state (camera->audio_volume, GST_STATE_READY);
1939       gst_element_set_state (camera->audio_capsfilter, GST_STATE_READY);
1940       break;
1941     case GST_STATE_CHANGE_READY_TO_NULL:
1942       if (camera->audio_src)
1943         gst_element_set_state (camera->audio_src, GST_STATE_NULL);
1944
1945       /* explicitly set to NULL as they might be outside of the bin */
1946       gst_element_set_state (camera->audio_volume, GST_STATE_NULL);
1947       gst_element_set_state (camera->audio_capsfilter, GST_STATE_NULL);
1948
1949       break;
1950     default:
1951       break;
1952   }
1953
1954   return ret;
1955 }
1956
1957 static gboolean
1958 gst_camera_bin_send_event (GstElement * element, GstEvent * event)
1959 {
1960   GstCameraBin2 *camera = GST_CAMERA_BIN2_CAST (element);
1961   gboolean res;
1962
1963   /* avoid losing our ref to send_event */
1964   gst_event_ref (event);
1965
1966   res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
1967   switch (GST_EVENT_TYPE (event)) {
1968     case GST_EVENT_EOS:
1969     {
1970       GstState current;
1971
1972       if (camera->videosink) {
1973         gst_element_get_state (camera->videosink, &current, NULL, 0);
1974         if (current <= GST_STATE_READY)
1975           gst_element_post_message (camera->videosink,
1976               gst_message_new_eos (GST_OBJECT (camera->videosink)));
1977       }
1978       if (camera->imagesink) {
1979         gst_element_get_state (camera->imagesink, &current, NULL, 0);
1980         if (current <= GST_STATE_READY)
1981           gst_element_post_message (camera->imagesink,
1982               gst_message_new_eos (GST_OBJECT (camera->imagesink)));
1983       }
1984       break;
1985     }
1986
1987     default:
1988       break;
1989   }
1990
1991   gst_event_unref (event);
1992   return res;
1993 }
1994
1995 static void
1996 gst_camera_bin_set_location (GstCameraBin2 * camera, const gchar * location)
1997 {
1998   GST_DEBUG_OBJECT (camera, "Setting mode %d location to %s", camera->mode,
1999       location);
2000   g_free (camera->location);
2001   camera->location = g_strdup (location);
2002 }
2003
2004 static void
2005 gst_camera_bin_set_audio_src (GstCameraBin2 * camera, GstElement * src)
2006 {
2007   GST_DEBUG_OBJECT (GST_OBJECT (camera),
2008       "Setting audio source %" GST_PTR_FORMAT, src);
2009
2010   if (camera->user_audio_src)
2011     g_object_unref (camera->user_audio_src);
2012
2013   if (src)
2014     gst_object_ref (src);
2015   camera->user_audio_src = src;
2016 }
2017
2018 static void
2019 gst_camera_bin_set_camera_src (GstCameraBin2 * camera, GstElement * src)
2020 {
2021   GST_DEBUG_OBJECT (GST_OBJECT (camera),
2022       "Setting camera source %" GST_PTR_FORMAT, src);
2023
2024   if (camera->user_src)
2025     g_object_unref (camera->user_src);
2026
2027   if (src)
2028     gst_object_ref (src);
2029   camera->user_src = src;
2030 }
2031
2032 static void
2033 gst_camera_bin_set_property (GObject * object, guint prop_id,
2034     const GValue * value, GParamSpec * pspec)
2035 {
2036   GstCameraBin2 *camera = GST_CAMERA_BIN2_CAST (object);
2037
2038   switch (prop_id) {
2039     case PROP_MODE:
2040       gst_camera_bin_change_mode (camera, g_value_get_enum (value));
2041       break;
2042     case PROP_LOCATION:
2043       gst_camera_bin_set_location (camera, g_value_get_string (value));
2044       break;
2045     case PROP_CAMERA_SRC:
2046       gst_camera_bin_set_camera_src (camera, g_value_get_object (value));
2047       break;
2048     case PROP_AUDIO_SRC:
2049       gst_camera_bin_set_audio_src (camera, g_value_get_object (value));
2050       break;
2051     case PROP_MUTE_AUDIO:
2052       g_object_set (camera->audio_volume, "mute", g_value_get_boolean (value),
2053           NULL);
2054       break;
2055     case PROP_AUDIO_CAPTURE_CAPS:{
2056       GST_DEBUG_OBJECT (camera,
2057           "Setting audio capture caps to %" GST_PTR_FORMAT,
2058           gst_value_get_caps (value));
2059
2060       if (G_LIKELY (camera->audio_capsfilter)) {
2061         g_object_set (camera->audio_capsfilter, "caps",
2062             gst_value_get_caps (value), NULL);
2063       } else {
2064         GST_WARNING_OBJECT (camera, "Audio capsfilter missing");
2065       }
2066     }
2067       break;
2068     case PROP_IMAGE_CAPTURE_CAPS:{
2069
2070       GST_DEBUG_OBJECT (camera,
2071           "Setting image capture caps to %" GST_PTR_FORMAT,
2072           gst_value_get_caps (value));
2073
2074       if (G_LIKELY (camera->imagebin_capsfilter)) {
2075         g_object_set (camera->imagebin_capsfilter, "caps",
2076             gst_value_get_caps (value), NULL);
2077       } else {
2078         GST_WARNING_OBJECT (camera, "Image capsfilter missing");
2079       }
2080     }
2081       break;
2082     case PROP_VIDEO_CAPTURE_CAPS:{
2083       GST_DEBUG_OBJECT (camera,
2084           "Setting video capture caps to %" GST_PTR_FORMAT,
2085           gst_value_get_caps (value));
2086
2087       if (G_LIKELY (camera->videobin_capsfilter)) {
2088         g_object_set (camera->videobin_capsfilter, "caps",
2089             gst_value_get_caps (value), NULL);
2090       } else {
2091         GST_WARNING_OBJECT (camera, "Video capsfilter missing");
2092       }
2093
2094     }
2095       break;
2096     case PROP_VIEWFINDER_CAPS:{
2097       GST_DEBUG_OBJECT (camera,
2098           "Setting viewfinder capture caps to %" GST_PTR_FORMAT,
2099           gst_value_get_caps (value));
2100
2101       if (G_LIKELY (camera->viewfinderbin_capsfilter)) {
2102         g_object_set (camera->viewfinderbin_capsfilter, "caps",
2103             gst_value_get_caps (value), NULL);
2104       } else {
2105         GST_WARNING_OBJECT (camera, "Viewfinder capsfilter missing");
2106       }
2107     }
2108       break;
2109     case PROP_POST_PREVIEWS:
2110       camera->post_previews = g_value_get_boolean (value);
2111       if (camera->src
2112           && g_object_class_find_property (G_OBJECT_GET_CLASS (camera->src),
2113               "post-previews"))
2114         g_object_set (camera->src, "post-previews", camera->post_previews,
2115             NULL);
2116       break;
2117     case PROP_PREVIEW_CAPS:
2118       gst_caps_replace (&camera->preview_caps,
2119           (GstCaps *) gst_value_get_caps (value));
2120       if (camera->src
2121           && g_object_class_find_property (G_OBJECT_GET_CLASS (camera->src),
2122               "preview-caps"))
2123         g_object_set (camera->src, "preview-caps", camera->preview_caps, NULL);
2124       break;
2125     case PROP_VIDEO_ENCODING_PROFILE:
2126       if (camera->video_profile)
2127         gst_encoding_profile_unref (camera->video_profile);
2128       camera->video_profile = (GstEncodingProfile *) g_value_dup_object (value);
2129       camera->video_profile_switch = TRUE;
2130       break;
2131     case PROP_IMAGE_FILTER:
2132       if (camera->user_image_filter)
2133         g_object_unref (camera->user_image_filter);
2134
2135       camera->user_image_filter = g_value_dup_object (value);
2136       break;
2137     case PROP_VIDEO_FILTER:
2138       if (camera->user_video_filter)
2139         g_object_unref (camera->user_video_filter);
2140
2141       camera->user_video_filter = g_value_dup_object (value);
2142       break;
2143     case PROP_VIEWFINDER_FILTER:
2144       if (camera->user_viewfinder_filter)
2145         g_object_unref (camera->user_viewfinder_filter);
2146
2147       camera->user_viewfinder_filter = g_value_dup_object (value);
2148       break;
2149     case PROP_PREVIEW_FILTER:
2150       if (camera->preview_filter)
2151         g_object_unref (camera->preview_filter);
2152
2153       camera->preview_filter = g_value_dup_object (value);
2154       if (camera->src
2155           && g_object_class_find_property (G_OBJECT_GET_CLASS (camera->src),
2156               "preview-filter"))
2157         g_object_set (camera->src, "preview-filter", camera->preview_filter,
2158             NULL);
2159       break;
2160     case PROP_AUDIO_FILTER:
2161       if (camera->user_audio_filter)
2162         g_object_unref (camera->user_audio_filter);
2163
2164       camera->user_audio_filter = g_value_dup_object (value);
2165       break;
2166     case PROP_VIEWFINDER_SINK:
2167       g_object_set (camera->viewfinderbin, "video-sink",
2168           g_value_get_object (value), NULL);
2169       break;
2170     case PROP_ZOOM:
2171       camera->zoom = g_value_get_float (value);
2172       /* limit to max-zoom */
2173       if (camera->zoom > camera->max_zoom) {
2174         GST_DEBUG_OBJECT (camera, "Clipping zoom %f to max-zoom %f",
2175             camera->zoom, camera->max_zoom);
2176         camera->zoom = camera->max_zoom;
2177       }
2178       if (camera->src)
2179         g_object_set (camera->src, "zoom", camera->zoom, NULL);
2180       break;
2181     case PROP_IMAGE_ENCODING_PROFILE:
2182       if (camera->image_profile)
2183         gst_encoding_profile_unref (camera->image_profile);
2184       camera->image_profile = (GstEncodingProfile *) g_value_dup_object (value);
2185
2186       /* make sure we set variable framerate here to prevent videorate from
2187        * being used in encodebin. It will always keep a buffer stored
2188        * internally and push it when a second one arrives. This breaks
2189        * the image capture */
2190       if (GST_IS_ENCODING_VIDEO_PROFILE (camera->image_profile))
2191         gst_encoding_video_profile_set_variableframerate (
2192             (GstEncodingVideoProfile *) camera->image_profile, TRUE);
2193       else if (GST_IS_ENCODING_CONTAINER_PROFILE (camera->image_profile)) {
2194         const GList *profs =
2195             gst_encoding_container_profile_get_profiles (
2196             (GstEncodingContainerProfile *) camera->image_profile);
2197         for (; profs; profs = g_list_next (profs)) {
2198           if (GST_IS_ENCODING_VIDEO_PROFILE (profs->data)) {
2199             gst_encoding_video_profile_set_variableframerate (profs->data,
2200                 TRUE);
2201           }
2202         }
2203       }
2204       camera->image_profile_switch = TRUE;
2205       break;
2206     case PROP_FLAGS:
2207       camera->flags = g_value_get_flags (value);
2208       break;
2209     default:
2210       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2211       break;
2212   }
2213 }
2214
2215 static void
2216 gst_camera_bin_get_property (GObject * object, guint prop_id,
2217     GValue * value, GParamSpec * pspec)
2218 {
2219   GstCameraBin2 *camera = GST_CAMERA_BIN2_CAST (object);
2220
2221   switch (prop_id) {
2222     case PROP_MODE:
2223       g_value_set_enum (value, camera->mode);
2224       break;
2225     case PROP_LOCATION:
2226       g_value_set_string (value, camera->location);
2227       break;
2228     case PROP_CAMERA_SRC:
2229       g_value_set_object (value, camera->user_src);
2230       break;
2231     case PROP_AUDIO_SRC:
2232       g_value_set_object (value, camera->user_audio_src);
2233       break;
2234     case PROP_MUTE_AUDIO:{
2235       gboolean mute;
2236
2237       g_object_get (camera->audio_volume, "mute", &mute, NULL);
2238       g_value_set_boolean (value, mute);
2239       break;
2240     }
2241     case PROP_AUDIO_CAPTURE_SUPPORTED_CAPS:
2242     case PROP_VIDEO_CAPTURE_SUPPORTED_CAPS:
2243     case PROP_VIEWFINDER_SUPPORTED_CAPS:
2244     case PROP_IMAGE_CAPTURE_SUPPORTED_CAPS:{
2245       GstPad *pad;
2246       GstElement *element;
2247       GstCaps *caps;
2248       const gchar *padname;
2249
2250       if (prop_id == PROP_VIDEO_CAPTURE_SUPPORTED_CAPS) {
2251         element = camera->src;
2252         padname = GST_BASE_CAMERA_SRC_VIDEO_PAD_NAME;
2253       } else if (prop_id == PROP_IMAGE_CAPTURE_SUPPORTED_CAPS) {
2254         element = camera->src;
2255         padname = GST_BASE_CAMERA_SRC_IMAGE_PAD_NAME;
2256       } else if (prop_id == PROP_VIEWFINDER_SUPPORTED_CAPS) {
2257         element = camera->src;
2258         padname = GST_BASE_CAMERA_SRC_VIEWFINDER_PAD_NAME;
2259       } else {
2260         element = camera->audio_src;
2261         padname = "src";
2262       }
2263
2264       if (element) {
2265         pad = gst_element_get_static_pad (element, padname);
2266
2267         g_assert (pad != NULL);
2268
2269         /* TODO not sure if we want get_caps or get_allowed_caps to already
2270          * consider the full pipeline scenario and avoid picking a caps that
2271          * won't negotiate. Need to take care on the special case of the
2272          * pad being unlinked.
2273          */
2274         caps = gst_pad_query_caps (pad, NULL);
2275         if (caps) {
2276           gst_value_set_caps (value, caps);
2277           gst_caps_unref (caps);
2278         }
2279
2280         gst_object_unref (pad);
2281       } else {
2282         GST_DEBUG_OBJECT (camera, "Source not created, can't get "
2283             "supported caps");
2284       }
2285     }
2286       break;
2287     case PROP_AUDIO_CAPTURE_CAPS:{
2288       GstCaps *caps = NULL;
2289       if (G_LIKELY (camera->audio_capsfilter)) {
2290         g_object_get (camera->audio_capsfilter, "caps", &caps, NULL);
2291       } else {
2292         GST_WARNING ("Missing audio capsfilter");
2293       }
2294       gst_value_set_caps (value, caps);
2295       gst_caps_unref (caps);
2296     }
2297       break;
2298     case PROP_IMAGE_CAPTURE_CAPS:{
2299       GstCaps *caps = NULL;
2300       if (G_LIKELY (camera->imagebin_capsfilter)) {
2301         g_object_get (camera->imagebin_capsfilter, "caps", &caps, NULL);
2302       } else {
2303         GST_WARNING ("Missing imagebin capsfilter");
2304       }
2305       gst_value_set_caps (value, caps);
2306       gst_caps_unref (caps);
2307     }
2308       break;
2309     case PROP_VIDEO_CAPTURE_CAPS:{
2310       GstCaps *caps = NULL;
2311       if (G_LIKELY (camera->videobin_capsfilter)) {
2312         g_object_get (camera->videobin_capsfilter, "caps", &caps, NULL);
2313       } else {
2314         GST_WARNING ("Missing imagebin capsfilter");
2315       }
2316       gst_value_set_caps (value, caps);
2317       gst_caps_unref (caps);
2318     }
2319       break;
2320     case PROP_VIEWFINDER_CAPS:{
2321       GstCaps *caps = NULL;
2322       if (G_LIKELY (camera->viewfinderbin_capsfilter)) {
2323         g_object_get (camera->viewfinderbin_capsfilter, "caps", &caps, NULL);
2324       } else {
2325         GST_WARNING ("Missing imagebin capsfilter");
2326       }
2327       gst_value_set_caps (value, caps);
2328       gst_caps_unref (caps);
2329     }
2330       break;
2331     case PROP_POST_PREVIEWS:
2332       g_value_set_boolean (value, camera->post_previews);
2333       break;
2334     case PROP_PREVIEW_CAPS:
2335       if (camera->preview_caps)
2336         gst_value_set_caps (value, camera->preview_caps);
2337       break;
2338     case PROP_VIDEO_ENCODING_PROFILE:
2339       if (camera->video_profile) {
2340         g_value_set_object (value, camera->video_profile);
2341       }
2342       break;
2343     case PROP_VIDEO_FILTER:
2344       if (camera->user_video_filter)
2345         g_value_set_object (value, camera->user_video_filter);
2346       break;
2347     case PROP_IMAGE_FILTER:
2348       if (camera->user_image_filter)
2349         g_value_set_object (value, camera->user_image_filter);
2350       break;
2351     case PROP_VIEWFINDER_FILTER:
2352       if (camera->user_viewfinder_filter)
2353         g_value_set_object (value, camera->user_viewfinder_filter);
2354       break;
2355     case PROP_AUDIO_FILTER:
2356       if (camera->user_audio_filter)
2357         g_value_set_object (value, camera->user_audio_filter);
2358       break;
2359     case PROP_PREVIEW_FILTER:
2360       if (camera->preview_filter)
2361         g_value_set_object (value, camera->preview_filter);
2362       break;
2363     case PROP_VIEWFINDER_SINK:{
2364       GstElement *sink;
2365
2366       g_object_get (camera->viewfinderbin, "video-sink", &sink, NULL);
2367       g_value_take_object (value, sink);
2368       break;
2369     }
2370     case PROP_ZOOM:
2371       g_value_set_float (value, camera->zoom);
2372       break;
2373     case PROP_MAX_ZOOM:
2374       g_value_set_float (value, camera->max_zoom);
2375       break;
2376     case PROP_IMAGE_ENCODING_PROFILE:
2377       if (camera->image_profile) {
2378         g_value_set_object (value, camera->image_profile);
2379       }
2380       break;
2381     case PROP_IDLE:
2382       g_value_set_boolean (value,
2383           g_atomic_int_get (&camera->processing_counter) == 0);
2384       break;
2385     case PROP_FLAGS:
2386       g_value_set_flags (value, camera->flags);
2387       break;
2388     default:
2389       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2390       break;
2391   }
2392 }
2393
2394 gboolean
2395 gst_camera_bin2_plugin_init (GstPlugin * plugin)
2396 {
2397   GST_DEBUG_CATEGORY_INIT (gst_camera_bin_debug, "camerabin", 0, "CameraBin");
2398
2399   return gst_element_register (plugin, "camerabin", GST_RANK_NONE,
2400       gst_camera_bin2_get_type ());
2401 }