summaryrefslogtreecommitdiff
path: root/gst/camerabin2/gstimagecapturebin.c
blob: d7c8d0101df511ddad69063edcdff699fb7b8866 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* GStreamer
 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
/**
 * SECTION:element-gstimagecapturebin
 *
 * The gstimagecapturebin element does FIXME stuff.
 *
 * <refsect2>
 * <title>Example launch line</title>
 * |[
 * gst-launch -v videotestsrc num-buffers=3 ! imagecapturebin
 * ]|
 * FIXME Describe what the pipeline does.
 * </refsect2>
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gstimagecapturebin.h"
#include "camerabingeneral.h"
#include <gst/pbutils/pbutils.h>
#include <gst/gst-i18n-plugin.h>

/* prototypes */


enum
{
  PROP_0,
  PROP_LOCATION,
  PROP_ENCODER,
  PROP_MUXER
};

#define DEFAULT_LOCATION "img_%d"
#define DEFAULT_COLORSPACE "ffmpegcolorspace"
#define DEFAULT_ENCODER "jpegenc"
#define DEFAULT_MUXER "jifmux"
#define DEFAULT_SINK "multifilesink"

/* pad templates */

static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-raw-yuv; video/x-raw-rgb")
    );

/* class initialization */

GST_BOILERPLATE (GstImageCaptureBin, gst_image_capture_bin, GstBin,
    GST_TYPE_BIN);

/* GObject callbacks */
static void gst_image_capture_bin_dispose (GObject * object);
static void gst_image_capture_bin_finalize (GObject * object);

/* Element class functions */
static GstStateChangeReturn
gst_image_capture_bin_change_state (GstElement * element, GstStateChange trans);

static void
gst_image_capture_bin_set_encoder (GstImageCaptureBin * imagebin,
    GstElement * encoder)
{
  GST_DEBUG_OBJECT (GST_OBJECT (imagebin),
      "Setting image encoder %" GST_PTR_FORMAT, encoder);

  if (imagebin->user_encoder)
    g_object_unref (imagebin->user_encoder);

  if (encoder)
    g_object_ref (encoder);

  imagebin->user_encoder = encoder;
}

static void
gst_image_capture_bin_set_muxer (GstImageCaptureBin * imagebin,
    GstElement * muxer)
{
  GST_DEBUG_OBJECT (GST_OBJECT (imagebin),
      "Setting image muxer %" GST_PTR_FORMAT, muxer);

  if (imagebin->user_muxer)
    g_object_unref (imagebin->user_muxer);

  if (muxer)
    g_object_ref (muxer);

  imagebin->user_muxer = muxer;
}

static void
gst_image_capture_bin_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstImageCaptureBin *imagebin = GST_IMAGE_CAPTURE_BIN_CAST (object);

  switch (prop_id) {
    case PROP_LOCATION:
      g_free (imagebin->location);
      imagebin->location = g_value_dup_string (value);
      GST_DEBUG_OBJECT (imagebin, "setting location to %s", imagebin->location);
      if (imagebin->sink) {
        g_object_set (imagebin->sink, "location", imagebin->location, NULL);
      }
      break;
    case PROP_ENCODER:
      gst_image_capture_bin_set_encoder (imagebin, g_value_get_object (value));
      break;
    case PROP_MUXER:
      gst_image_capture_bin_set_muxer (imagebin, g_value_get_object (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_image_capture_bin_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstImageCaptureBin *imagebin = GST_IMAGE_CAPTURE_BIN_CAST (object);

  switch (prop_id) {
    case PROP_LOCATION:
      g_value_set_string (value, imagebin->location);
      break;
    case PROP_ENCODER:
      g_value_set_object (value, imagebin->encoder);
      break;
    case PROP_MUXER:
      g_value_set_object (value, imagebin->muxer);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_image_capture_bin_finalize (GObject * object)
{
  GstImageCaptureBin *imgbin = GST_IMAGE_CAPTURE_BIN_CAST (object);

  g_free (imgbin->location);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gst_image_capture_bin_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&sink_template));

  gst_element_class_set_details_simple (element_class, "Image Capture Bin",
      "Sink/Video", "Image Capture Bin used in camerabin2",
      "Thiago Santos <thiago.sousa.santos@collabora.co.uk>");
}

static void
gst_image_capture_bin_class_init (GstImageCaptureBinClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *element_class;

  gobject_class = G_OBJECT_CLASS (klass);
  element_class = GST_ELEMENT_CLASS (klass);

  gobject_class->dispose = gst_image_capture_bin_dispose;
  gobject_class->finalize = gst_image_capture_bin_finalize;
  gobject_class->set_property = gst_image_capture_bin_set_property;
  gobject_class->get_property = gst_image_capture_bin_get_property;

  element_class->change_state =
      GST_DEBUG_FUNCPTR (gst_image_capture_bin_change_state);

  g_object_class_install_property (gobject_class, PROP_LOCATION,
      g_param_spec_string ("location", "Location",
          "Location to save the captured files. A %%d can be used as a "
          "placeholder for a capture count",
          DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (gobject_class, PROP_ENCODER,
      g_param_spec_object ("image-encoder", "Image encoder",
          "Image encoder GStreamer element (default is jpegenc)",
          GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (gobject_class, PROP_MUXER,
      g_param_spec_object ("image-muxer", "Image muxer",
          "Image muxer GStreamer element (default is jifmux)",
          GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}

static void
gst_image_capture_bin_init (GstImageCaptureBin * imagebin,
    GstImageCaptureBinClass * imagebin_class)
{
  GstPadTemplate *tmpl;

  tmpl = gst_static_pad_template_get (&sink_template);
  imagebin->ghostpad = gst_ghost_pad_new_no_target_from_template ("sink", tmpl);
  gst_object_unref (tmpl);
  gst_element_add_pad (GST_ELEMENT_CAST (imagebin), imagebin->ghostpad);

  imagebin->sink = NULL;

  imagebin->location = g_strdup (DEFAULT_LOCATION);
  imagebin->encoder = NULL;
  imagebin->user_encoder = NULL;
  imagebin->muxer = NULL;
  imagebin->user_muxer = NULL;
}

static void
gst_image_capture_bin_dispose (GObject * object)
{
  GstImageCaptureBin *imagebin = GST_IMAGE_CAPTURE_BIN_CAST (object);

  if (imagebin->user_encoder) {
    gst_object_unref (imagebin->user_encoder);
    imagebin->user_encoder = NULL;
  }

  if (imagebin->user_muxer) {
    gst_object_unref (imagebin->user_muxer);
    imagebin->user_muxer = NULL;
  }
  G_OBJECT_CLASS (parent_class)->dispose ((GObject *) imagebin);
}

static gboolean
gst_image_capture_bin_create_elements (GstImageCaptureBin * imagebin)
{
  GstElement *colorspace;
  GstPad *pad = NULL;
  const gchar *missing_element_name;

  if (imagebin->elements_created)
    return TRUE;

  /* create elements */
  colorspace =
      gst_camerabin_create_and_add_element (GST_BIN (imagebin),
      DEFAULT_COLORSPACE, "imagebin-colorspace");
  if (!colorspace) {
    missing_element_name = DEFAULT_COLORSPACE;
    goto missing_element;
  }

  if (imagebin->user_encoder) {
    imagebin->encoder = imagebin->user_encoder;
    if (!gst_camerabin_add_element (GST_BIN (imagebin), imagebin->encoder)) {
      goto error;
    }
  } else {
    imagebin->encoder =
        gst_camerabin_create_and_add_element (GST_BIN (imagebin),
        DEFAULT_ENCODER, "imagebin-encoder");
    if (!imagebin->encoder) {
      missing_element_name = DEFAULT_ENCODER;
      goto missing_element;
    }
  }

  if (imagebin->user_muxer) {
    imagebin->muxer = imagebin->user_muxer;
    if (!gst_camerabin_add_element (GST_BIN (imagebin), imagebin->muxer)) {
      goto error;
    }
  } else {
    imagebin->muxer =
        gst_camerabin_create_and_add_element (GST_BIN (imagebin),
        DEFAULT_MUXER, "imagebin-muxer");
    if (!imagebin->muxer) {
      missing_element_name = DEFAULT_MUXER;
      goto missing_element;
    }
  }

  imagebin->sink =
      gst_camerabin_create_and_add_element (GST_BIN (imagebin), DEFAULT_SINK,
      "imagebin-sink");
  if (!imagebin->sink) {
    missing_element_name = DEFAULT_SINK;
    goto missing_element;
  }

  g_object_set (imagebin->sink, "location", imagebin->location, "async", FALSE,
      "post-messages", TRUE, NULL);

  /* add ghostpad */
  pad = gst_element_get_static_pad (colorspace, "sink");
  if (!gst_ghost_pad_set_target (GST_GHOST_PAD (imagebin->ghostpad), pad))
    goto error;
  gst_object_unref (pad);

  imagebin->elements_created = TRUE;
  return TRUE;

missing_element:
  gst_element_post_message (GST_ELEMENT_CAST (imagebin),
      gst_missing_element_message_new (GST_ELEMENT_CAST (imagebin),
          missing_element_name));
  GST_ELEMENT_ERROR (imagebin, CORE, MISSING_PLUGIN,
      (_("Missing element '%s' - check your GStreamer installation."),
          missing_element_name), (NULL));
  goto error;

error:
  if (pad)
    gst_object_unref (pad);
  return FALSE;
}

static GstStateChangeReturn
gst_image_capture_bin_change_state (GstElement * element, GstStateChange trans)
{
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
  GstImageCaptureBin *imagebin = GST_IMAGE_CAPTURE_BIN_CAST (element);

  switch (trans) {
    case GST_STATE_CHANGE_NULL_TO_READY:
      if (!gst_image_capture_bin_create_elements (imagebin)) {
        return GST_STATE_CHANGE_FAILURE;
      }

      /* set our image muxer to MERGE_REPLACE mode if it is a tagsetter */
      if (imagebin->muxer && gst_element_implements_interface (imagebin->muxer,
              GST_TYPE_TAG_SETTER)) {
        gst_tag_setter_set_tag_merge_mode (GST_TAG_SETTER (imagebin->muxer),
            GST_TAG_MERGE_REPLACE);
      }

      break;
    default:
      break;
  }

  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, trans);

  switch (trans) {
    case GST_STATE_CHANGE_READY_TO_NULL:
      break;
    default:
      break;
  }

  return ret;
}

gboolean
gst_image_capture_bin_plugin_init (GstPlugin * plugin)
{
  return gst_element_register (plugin, "imagecapturebin", GST_RANK_NONE,
      gst_image_capture_bin_get_type ());
}