void cleanup_stack(void) { cleanup_pipeline(); setup_cleanup_vlink(VLINKNLCMD_STOP_HOOK_DEVICE); printd("Stack cleaned up!\n"); }
int GStreamer_setMedia(const char *uri) { GstElement *src, *sink; int is_video, is_http = 1; int ret = 0; if (!g_initialized) { g_error("GStreamer: library not initialized!\n"); return -1; } cleanup_pipeline(); pthread_mutex_lock(&g_mutex); g_position = 0; g_duration = 0; g_print("GStreamer: playing : %s\n", uri); is_video = strstr(uri, "264") != NULL; is_http = !strncmp(uri, "http://", strlen("http://")); g_printf("GStreamer: playing %s via %s\n", is_video ? "video" : "audio", is_http ? "http" : "filesrc"); if (is_http) g_object_set(G_OBJECT(g_httpsrc), "location", uri, NULL); else g_object_set(G_OBJECT(g_filesrc), "location", uri, NULL); //sink = (is_video) ? g_videosink : g_audiosink; sink = (is_video) ? create_video_sink() : create_audio_sink(); src = (is_http) ? g_httpsrc : g_filesrc; gst_bin_add_many(GST_BIN(g_pipeline), src, sink, NULL); if (!gst_element_link(src, sink)) { g_error("GStreamer: failed to link %s with %s\n", gst_element_get_name(src), gst_element_get_name(sink)); return -1; } gst_element_set_state(GST_ELEMENT(g_pipeline), GST_STATE_PLAYING); //gst_element_get_state(GST_ELEMENT(g_pipeline), ...); /* TODO what is signalled? */ pthread_cond_signal(&g_main_cond); pthread_mutex_unlock(&g_mutex); return ret; }
int main (int argc, char *argv[]) { gchar *target_times = NULL; gchar *ev_option = NULL; gchar *fn_option = NULL; GOptionEntry options[] = { {"ev-compensation", '\0', 0, G_OPTION_ARG_STRING, &ev_option, "EV compensation for source element GstPhotography interface", NULL}, {"aperture", '\0', 0, G_OPTION_ARG_INT, &aperture, "Aperture (size of lens opening) for source element GstPhotography interface", NULL}, {"flash-mode", '\0', 0, G_OPTION_ARG_INT, &flash_mode, "Flash mode for source element GstPhotography interface", NULL}, {"scene-mode", '\0', 0, G_OPTION_ARG_INT, &scene_mode, "Scene mode for source element GstPhotography interface", NULL}, {"exposure", '\0', 0, G_OPTION_ARG_INT64, &exposure, "Exposure time (in ms) for source element GstPhotography interface", NULL}, {"iso-speed", '\0', 0, G_OPTION_ARG_INT, &iso_speed, "ISO speed for source element GstPhotography interface", NULL}, {"white-balance-mode", '\0', 0, G_OPTION_ARG_INT, &wb_mode, "White balance mode for source element GstPhotography interface", NULL}, {"colour-tone-mode", '\0', 0, G_OPTION_ARG_INT, &color_mode, "Colour tone mode for source element GstPhotography interface", NULL}, {"directory", '\0', 0, G_OPTION_ARG_STRING, &fn_option, "Directory for capture file(s) (default is current directory)", NULL}, {"mode", '\0', 0, G_OPTION_ARG_INT, &mode, "Capture mode (default = 1 (image), 2 = video)", NULL}, {"capture-time", '\0', 0, G_OPTION_ARG_INT, &capture_time, "Time to capture video in seconds (default = 10)", NULL}, {"capture-total", '\0', 0, G_OPTION_ARG_INT, &capture_total, "Total number of captures to be done (default = 1)", NULL}, {"zoom", '\0', 0, G_OPTION_ARG_INT, &zoom, "Zoom (100 = 1x (default), 200 = 2x etc.)", NULL}, {"wrapper-source", '\0', 0, G_OPTION_ARG_STRING, &wrappersrc_name, "Camera source wrapper used for setting the video source (default is wrappercamerabinsrc)", NULL}, {"video-source", '\0', 0, G_OPTION_ARG_STRING, &videosrc_name, "Video source used in still capture and video recording", NULL}, {"video-device", '\0', 0, G_OPTION_ARG_STRING, &videodevice_name, "Video device to be set on the video source", NULL}, {"audio-source", '\0', 0, G_OPTION_ARG_STRING, &audiosrc_name, "Audio source used in video recording", NULL}, {"image-pp", '\0', 0, G_OPTION_ARG_STRING, &imagepp_name, "List of image post-processing elements separated with comma", NULL}, {"viewfinder-sink", '\0', 0, G_OPTION_ARG_STRING, &vfsink_name, "Viewfinder sink (default = fakesink)", NULL}, {"image-width", '\0', 0, G_OPTION_ARG_INT, &image_width, "Width for image capture", NULL}, {"image-height", '\0', 0, G_OPTION_ARG_INT, &image_height, "Height for image capture", NULL}, {"view-framerate-num", '\0', 0, G_OPTION_ARG_INT, &view_framerate_num, "Framerate numerator for viewfinder", NULL}, {"view-framerate-den", '\0', 0, G_OPTION_ARG_INT, &view_framerate_den, "Framerate denominator for viewfinder", NULL}, {"preview-caps", '\0', 0, G_OPTION_ARG_STRING, &preview_caps_name, "Preview caps (e.g. video/x-raw-rgb,width=320,height=240)", NULL}, {"viewfinder-filter", '\0', 0, G_OPTION_ARG_STRING, &viewfinder_filter, "Filter to process all frames going to viewfinder sink", NULL}, {"x-width", '\0', 0, G_OPTION_ARG_INT, &x_width, "X window width (default = 320)", NULL}, {"x-height", '\0', 0, G_OPTION_ARG_INT, &x_height, "X window height (default = 240)", NULL}, {"no-xwindow", '\0', 0, G_OPTION_ARG_NONE, &no_xwindow, "Do not create XWindow", NULL}, {"encoding-target", '\0', 0, G_OPTION_ARG_STRING, &gep_targetname, "Video encoding target name", NULL}, {"encoding-profile", '\0', 0, G_OPTION_ARG_STRING, &gep_profilename, "Video encoding profile name", NULL}, {"encoding-profile-filename", '\0', 0, G_OPTION_ARG_STRING, &gep_filename, "Video encoding profile filename", NULL}, {"image-capture-caps", '\0', 0, G_OPTION_ARG_STRING, &image_capture_caps_str, "Image capture caps (e.g. video/x-raw-rgb,width=640,height=480)", NULL}, {"viewfinder-caps", '\0', 0, G_OPTION_ARG_STRING, &viewfinder_caps_str, "Viewfinder caps (e.g. video/x-raw-rgb,width=640,height=480)", NULL}, {"video-capture-caps", '\0', 0, G_OPTION_ARG_STRING, &video_capture_caps_str, "Video capture caps (e.g. video/x-raw-rgb,width=640,height=480)", NULL}, {NULL} }; GOptionContext *ctx; GError *err = NULL; if (!g_thread_supported ()) g_thread_init (NULL); ctx = g_option_context_new ("\n\ncamerabin command line test application."); g_option_context_add_main_entries (ctx, options, NULL); g_option_context_add_group (ctx, gst_init_get_option_group ()); if (!g_option_context_parse (ctx, &argc, &argv, &err)) { g_print ("Error initializing: %s\n", err->message); exit (1); } g_option_context_free (ctx); GST_DEBUG_CATEGORY_INIT (camerabin_test, "camerabin-test", 0, "camerabin test"); /* if we fail to create xwindow should we care? */ if (!no_xwindow) create_host_window (); /* FIXME: error handling */ if (ev_option != NULL) ev_compensation = strtod (ev_option, (char **) NULL); if (vfsink_name == NULL) vfsink_name = g_strdup ("fakesink"); filename = g_string_new (fn_option); if (filename->len == 0) filename = g_string_append (filename, "."); timer = g_timer_new (); /* init */ if (setup_pipeline ()) { loop = g_main_loop_new (NULL, FALSE); g_idle_add ((GSourceFunc) run_pipeline, NULL); g_main_loop_run (loop); cleanup_pipeline (); g_main_loop_unref (loop); } /* free */ g_string_free (filename, TRUE); g_free (ev_option); g_free (wrappersrc_name); g_free (videosrc_name); g_free (videodevice_name); g_free (audiosrc_name); g_free (imagepp_name); g_free (vfsink_name); g_free (target_times); g_free (gep_targetname); g_free (gep_profilename); g_free (gep_filename); g_timer_destroy (timer); if (window) XDestroyWindow (display, window); if (display) XCloseDisplay (display); return 0; }
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; }
int main (int argc, char *argv[]) { gchar *target_times = NULL; gchar *ev_option = NULL; gchar *fn_option = NULL; GOptionEntry options[] = { {"ev-compensation", '\0', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_STRING, &ev_option, "EV compensation (-2.5..2.5, default = 0)", NULL}, {"aperture", '\0', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &aperture, "Aperture (size of lens opening, default = 0 (auto))", NULL}, {"flash-mode", '\0', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &flash_mode, "Flash mode (default = 0 (auto))", NULL}, {"scene-mode", '\0', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &scene_mode, "Scene mode (default = 6 (auto))", NULL}, {"exposure", '\0', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT64, &exposure, "Exposure (default = 0 (auto))", NULL}, {"iso-speed", '\0', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &iso_speed, "ISO speed (default = 0 (auto))", NULL}, {"white-balance-mode", '\0', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &wb_mode, "White balance mode (default = 0 (auto))", NULL}, {"colour-tone-mode", '\0', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &color_mode, "Colour tone mode (default = 0 (auto))", NULL}, {"directory", '\0', 0, G_OPTION_ARG_STRING, &fn_option, "Directory for capture file(s) (default is current directory)", NULL}, {"mode", '\0', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &mode, "Capture mode (default = 0 (image), 1 = video)", NULL}, {"capture-time", '\0', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &capture_time, "Time to capture video in seconds (default = 10)", NULL}, {"capture-total", '\0', 0, G_OPTION_ARG_INT, &capture_total, "Total number of captures to be done (default = 1)", NULL}, {"flags", '\0', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_INT, &flags, "Flags for camerabin, (default = 0x9)", NULL}, {"mute", '\0', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_STRING, &mute, "Mute audio (default = 0 (no))", NULL}, {"zoom", '\0', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_STRING, &zoom, "Zoom (100 = 1x (default), 200 = 2x etc.)", NULL}, {"audio-src", '\0', 0, G_OPTION_ARG_STRING, &audiosrc_name, "Audio source used in video recording", NULL}, {"audio-bitrate", '\0', 0, G_OPTION_ARG_INT, &audio_bitrate, "Audio bitrate (default 128000)", NULL}, {"audio-samplerate", '\0', 0, G_OPTION_ARG_INT, &audio_samplerate, "Audio samplerate (default 48000)", NULL}, {"audio-channels", '\0', 0, G_OPTION_ARG_INT, &audio_channels, "Audio channels (default 1)", NULL}, {"video-src", '\0', 0, G_OPTION_ARG_STRING, &videosrc_name, "Video source used in still capture and video recording", NULL}, {"audio-enc", '\0', 0, G_OPTION_ARG_STRING, &audioenc_name, "Audio encoder used in video recording", NULL}, {"video-enc", '\0', 0, G_OPTION_ARG_STRING, &videoenc_name, "Video encoder used in video recording", NULL}, {"image-enc", '\0', 0, G_OPTION_ARG_STRING, &imageenc_name, "Image encoder used in still capture", NULL}, {"image-pp", '\0', 0, G_OPTION_ARG_STRING, &imagepp_name, "Image post-processing element", NULL}, {"video-mux", '\0', 0, G_OPTION_ARG_STRING, &videomux_name, "Muxer used in video recording", NULL}, {"viewfinder-sink", '\0', 0, G_OPTION_ARG_STRING, &vfsink_name, "Viewfinder sink (default = fakesink)", NULL}, {"image-width", '\0', 0, G_OPTION_ARG_INT, &image_width, "Width for image capture", NULL}, {"image-height", '\0', 0, G_OPTION_ARG_INT, &image_height, "Height for image capture", NULL}, {"view-framerate-num", '\0', 0, G_OPTION_ARG_INT, &view_framerate_num, "Framerate numerator for viewfinder", NULL}, {"view-framerate-den", '\0', 0, G_OPTION_ARG_INT, &view_framerate_den, "Framerate denominator for viewfinder", NULL}, {"src-colorspace", '\0', 0, G_OPTION_ARG_STRING, &src_csp, "Colorspace format for video source (e.g. YUY2, UYVY)", NULL}, {"src-format", '\0', 0, G_OPTION_ARG_STRING, &src_format, "Video format for video source", NULL}, {"preview-caps", '\0', 0, G_OPTION_ARG_STRING, &preview_caps_name, "Preview caps (e.g. video/x-raw-rgb,width=320,height=240)", NULL}, {"video-source-filter", '\0', 0, G_OPTION_ARG_STRING, &video_src_filter, "Video filter to process all frames from video source", NULL}, {"viewfinder-filter", '\0', 0, G_OPTION_ARG_STRING, &viewfinder_filter, "Filter to process all frames going to viewfinder sink", NULL}, {"x-width", '\0', 0, G_OPTION_ARG_INT, &x_width, "X window width (default = 320)", NULL}, {"x-height", '\0', 0, G_OPTION_ARG_INT, &x_height, "X window height (default = 240)", NULL}, {NULL} }; GOptionContext *ctx; GError *err = NULL; /* if we fail to create xwindow should we care? */ create_host_window (); if (!g_thread_supported ()) g_thread_init (NULL); ctx = g_option_context_new ("\n\ncamerabin command line test application."); g_option_context_add_main_entries (ctx, options, NULL); g_option_context_add_group (ctx, gst_init_get_option_group ()); if (!g_option_context_parse (ctx, &argc, &argv, &err)) { g_print ("Error initializing: %s\n", err->message); exit (1); } g_option_context_free (ctx); GST_DEBUG_CATEGORY_INIT (camerabin_test, "camerabin-test", 0, "camerabin test"); /* FIXME: error handling */ if (ev_option != NULL) ev_compensation = strtod (ev_option, (char **) NULL); if (vfsink_name == NULL) vfsink_name = g_strdup ("fakesink"); filename = g_string_new (fn_option); if (filename->len == 0) filename = g_string_append (filename, "."); filename = g_string_append (filename, "/test_%04u"); if (mode == 1) filename = g_string_append (filename, ".mp4"); else filename = g_string_append (filename, ".jpg"); /* init */ if (setup_pipeline ()) { loop = g_main_loop_new (NULL, FALSE); g_idle_add ((GSourceFunc) run_pipeline, NULL); g_main_loop_run (loop); cleanup_pipeline (); g_main_loop_unref (loop); } /* free */ g_string_free (filename, TRUE); g_free (ev_option); g_free (audiosrc_name); g_free (videosrc_name); g_free (audioenc_name); g_free (videoenc_name); g_free (imageenc_name); g_free (imagepp_name); g_free (videomux_name); g_free (vfsink_name); g_free (src_csp); g_free (src_format); g_free (target_times); if (window) XDestroyWindow (display, window); if (display) XCloseDisplay (display); return 0; }