static gboolean
setup_pipeline (void)
{
  gboolean res = TRUE;
  GstBus *bus;
  GstElement *sink = NULL, *ipp = NULL;
  GstEncodingProfile *prof = NULL;
  camerabin = gst_element_factory_make ("camerabin2", NULL);
  if (NULL == camerabin) {
    g_warning ("can't create camerabin element\n");
    goto error;
  }

  bus = gst_pipeline_get_bus (GST_PIPELINE (camerabin));
  /* Add sync handler for time critical messages that need to be handled fast */
  gst_bus_set_sync_handler (bus, sync_bus_callback, NULL);
  /* Handle normal messages asynchronously */
  gst_bus_add_watch (bus, bus_callback, NULL);
  gst_object_unref (bus);

  GST_INFO_OBJECT (camerabin, "camerabin2 created");

  if (videosrc_name) {
    GstElement *wrapper;
    GstElement *videosrc;

    if (wrappersrc_name)
      wrapper = gst_element_factory_make (wrappersrc_name, NULL);
    else
      wrapper = gst_element_factory_make ("wrappercamerabinsrc", NULL);

    if (setup_pipeline_element (wrapper, "video-src", videosrc_name, NULL)) {
      g_object_set (camerabin, "camera-src", wrapper, NULL);
    } else {
      GST_WARNING ("Failed to set videosrc to %s", videosrc_name);
    }

    g_object_get (wrapper, "video-src", &videosrc, NULL);
    if (videosrc && videodevice_name &&
        g_object_class_find_property (G_OBJECT_GET_CLASS (videosrc),
            "device")) {
      g_object_set (videosrc, "device", videodevice_name, NULL);
    }
  }

  /* configure used elements */
  res &= setup_pipeline_element (camerabin, "audio-src", audiosrc_name, NULL);
  res &= setup_pipeline_element (camerabin, "viewfinder-sink", vfsink_name,
      &sink);
  res &= setup_pipeline_element (camerabin, "viewfinder-filter",
      viewfinder_filter, NULL);

  if (imagepp_name) {
    ipp = create_ipp_bin ();
    if (ipp)
      g_object_set (camerabin, "image-filter", ipp, NULL);
    else
      GST_WARNING ("Could not create ipp elements");
  }

  prof = load_encoding_profile ();
  if (prof)
    g_object_set (G_OBJECT (camerabin), "video-profile", prof, NULL);

  GST_INFO_OBJECT (camerabin, "elements created");

  if (sink)
    g_object_set (sink, "sync", TRUE, NULL);

  GST_INFO_OBJECT (camerabin, "elements configured");

  /* configure a resolution and framerate */
  if (image_width > 0 && image_height > 0) {
    if (mode == MODE_VIDEO) {
      GstCaps *caps = NULL;
      if (view_framerate_num > 0)
        caps = gst_caps_new_full (gst_structure_new ("video/x-raw-yuv",
                "width", G_TYPE_INT, image_width,
                "height", G_TYPE_INT, image_height,
                "framerate", GST_TYPE_FRACTION, view_framerate_num,
                view_framerate_den, NULL),
            gst_structure_new ("video/x-raw-rgb",
                "width", G_TYPE_INT, image_width,
                "height", G_TYPE_INT, image_height,
                "framerate", GST_TYPE_FRACTION, view_framerate_num,
                view_framerate_den, NULL), NULL);
      else
        caps = gst_caps_new_full (gst_structure_new ("video/x-raw-yuv",
                "width", G_TYPE_INT, image_width,
                "height", G_TYPE_INT, image_height, NULL),
            gst_structure_new ("video/x-raw-rgb",
                "width", G_TYPE_INT, image_width,
                "height", G_TYPE_INT, image_height, NULL), NULL);

      g_object_set (camerabin, "video-capture-caps", caps, NULL);
      gst_caps_unref (caps);
    } else {
      GstCaps *caps = gst_caps_new_full (gst_structure_new ("video/x-raw-yuv",
              "width", G_TYPE_INT, image_width,
              "height", G_TYPE_INT, image_height, NULL),
          gst_structure_new ("video/x-raw-rgb",
              "width", G_TYPE_INT, image_width,
              "height", G_TYPE_INT, image_height, NULL), NULL);

      g_object_set (camerabin, "image-capture-caps", caps, NULL);
      gst_caps_unref (caps);
    }
  }

  set_camerabin2_caps_from_string ();

  if (GST_STATE_CHANGE_FAILURE ==
      gst_element_set_state (camerabin, GST_STATE_READY)) {
    g_warning ("can't set camerabin to ready\n");
    goto error;
  }
  GST_INFO_OBJECT (camerabin, "camera ready");

  if (GST_STATE_CHANGE_FAILURE ==
      gst_element_set_state (camerabin, GST_STATE_PLAYING)) {
    g_warning ("can't set camerabin to playing\n");
    goto error;
  }

  GST_INFO_OBJECT (camerabin, "camera started");
  return TRUE;
error:
  cleanup_pipeline ();
  return FALSE;
}
static gboolean
setup_pipeline (void)
{
  GstBus *bus;
  gboolean res = TRUE;
  GstElement *vmux = NULL, *ienc = NULL, *sink = NULL, *aenc = NULL, *ipp =
      NULL;
  GstCaps *filter_caps = NULL;

  camera_bin = gst_element_factory_make ("camerabin", NULL);
  if (NULL == camera_bin) {
    g_warning ("can't create camerabin element\n");
    goto error;
  }
  g_object_set (camera_bin, "flags", flags, NULL);

  g_signal_connect (camera_bin, "image-done", (GCallback) img_capture_done,
      NULL);

  bus = gst_pipeline_get_bus (GST_PIPELINE (camera_bin));
  /* Add sync handler for time critical messages that need to be handled fast */
  gst_bus_set_sync_handler (bus, sync_bus_callback, NULL);
  /* Handle normal messages asynchronously */
  gst_bus_add_watch (bus, bus_callback, NULL);
  gst_object_unref (bus);

  GST_INFO_OBJECT (camera_bin, "camerabin created");

  /* configure used elements */
  res &= setup_pipeline_element ("viewfinder-sink", vfsink_name, &sink);
  res &= setup_pipeline_element ("audio-source", audiosrc_name, NULL);
  res &= setup_pipeline_element ("video-source", videosrc_name, NULL);
  res &= setup_pipeline_element ("video-source-filter", video_src_filter, NULL);
  res &= setup_pipeline_element ("viewfinder-filter", viewfinder_filter, NULL);

  if (audioenc_name) {
    aenc = create_audioencoder_bin ();
    if (aenc)
      g_object_set (camera_bin, "audio-encoder", aenc, NULL);
    else
      GST_WARNING ("Could not make audio encoder element");
  }

  if (imagepp_name) {
    ipp = create_ipp_bin ();
    if (ipp)
      g_object_set (camera_bin, "image-post-processing", ipp, NULL);
    else
      GST_WARNING ("Could not create ipp elements");
  }

  res &= setup_pipeline_element ("video-encoder", videoenc_name, NULL);
  res &= setup_pipeline_element ("image-encoder", imageenc_name, &ienc);
  res &= setup_pipeline_element ("image-formatter", imageformatter_name, NULL);
  res &= setup_pipeline_element ("video-muxer", videomux_name, &vmux);
  if (!res) {
    goto error;
  }
  GST_INFO_OBJECT (camera_bin, "elements created");

  /* set properties */
  if (src_format) {
    filter_caps = gst_caps_from_string (src_format);
  } else if (src_csp && strlen (src_csp) == 4) {
    /* Set requested colorspace format, this is needed if the default 
       colorspace negotiated for viewfinder doesn't match with e.g. encoders. */
    filter_caps = gst_caps_new_simple ("video/x-raw-yuv",
        "format", GST_TYPE_FOURCC,
        GST_MAKE_FOURCC (src_csp[0], src_csp[1], src_csp[2], src_csp[3]), NULL);
  }

  if (filter_caps) {
    g_object_set (camera_bin, "filter-caps", filter_caps, NULL);
    gst_caps_unref (filter_caps);
  }

  g_object_set (sink, "sync", TRUE, NULL);

  GST_INFO_OBJECT (camera_bin, "elements configured");

  /* configure a resolution and framerate */
  if (mode == 1) {
    g_signal_emit_by_name (camera_bin, "set-video-resolution-fps", image_width,
        image_height, view_framerate_num, view_framerate_den, NULL);
  } else {
    g_signal_emit_by_name (camera_bin, "set-image-resolution", image_width,
        image_height, NULL);
  }

  if (GST_STATE_CHANGE_FAILURE ==
      gst_element_set_state (camera_bin, GST_STATE_READY)) {
    g_warning ("can't set camerabin to ready\n");
    goto error;
  }
  GST_INFO_OBJECT (camera_bin, "camera ready");

  if (GST_STATE_CHANGE_FAILURE ==
      gst_element_set_state (camera_bin, GST_STATE_PLAYING)) {
    g_warning ("can't set camerabin to playing\n");
    goto error;
  }

  GST_INFO_OBJECT (camera_bin, "camera started");
  return TRUE;
error:
  cleanup_pipeline ();
  return FALSE;
}