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