Documentation/Maemo 5 Developer Guide/Using Multimedia Components/Camera API Usage

(use <code>)
 
(8 intermediate revisions not shown)
Line 1: Line 1:
__FORCETOC__
__FORCETOC__
-
= Camera API Usage =
+
 
This section explains how to use the Camera API to access the camera hardware that is present in some models of Nokia Internet Tablets.
This section explains how to use the Camera API to access the camera hardware that is present in some models of Nokia Internet Tablets.
== Camera Hardware and Linux ==
== Camera Hardware and Linux ==
 +
The Linux operating system supports live video and audio hardware, such as webcams, TV tuners, video capture cards, FM radio tuners, video ''output'' devices etc. The primary API for the applications to access those devices is [http://linux.bytesex.org/v4l2/ Video4Linux2].
The Linux operating system supports live video and audio hardware, such as webcams, TV tuners, video capture cards, FM radio tuners, video ''output'' devices etc. The primary API for the applications to access those devices is [http://linux.bytesex.org/v4l2/ Video4Linux2].
-
Video4Linux2 is a ''kernel'' API, so there must be kernel drivers for each supported device. At the user level, device access is standardized via device files. In the case of video capture devices like cameras, which are the focus of this material, the files would be ''/dev/video0'', ''/dev/video1'', etc., as many as there are connected devices.
+
Video4Linux2 is a ''kernel'' API, so there must be kernel drivers for each supported device. At the user level, device access is standardized via device files. In the case of video capture devices like cameras, which are the focus of this material, the files would be <code>/dev/video0</code>, <code>/dev/video1</code>, etc., as many as there are connected devices.
Data exchanged between the device file and user-level application has a standardized format for each device class. This allows the application to be instantly compatible with every video capture device that has a driver for Linux.
Data exchanged between the device file and user-level application has a standardized format for each device class. This allows the application to be instantly compatible with every video capture device that has a driver for Linux.
-
The built-in cameras present in Maemo devices are compatible with [http://linux.bytesex.org/v4l2/ Video-4-Linux version 2 API]. In principle, any application compatible with this API is easily portable to the maemo platform.
+
The built-in cameras present in Maemo devices are compatible with [http://linux.bytesex.org/v4l2/ Video-4-Linux version 2 API]. In principle, any application compatible with this API is easily portable to the Maemo platform.
-
Since the maemo platform delegates all multimedia handling to the GStreamer framework, applications that need access to the built-in camera should employ GStreamer for this, instead of directly accessing Video4Linux devices, via the ''v4l2src'' GStreamer module.
+
Since the Maemo platform delegates all multimedia handling to the GStreamer framework, applications that need access to the built-in camera should employ GStreamer for this, instead of directly accessing Video4Linux devices, via the <code>v4l2src</code> GStreamer module.
Thanks to the flexibility of GStreamer, a developer can fully test any given application in a regular desktop PC with a connected webcam, and then perform the final test in the Internet Tablet itself, without a single change in the source code, since GStreamer refers to modules as text names.
Thanks to the flexibility of GStreamer, a developer can fully test any given application in a regular desktop PC with a connected webcam, and then perform the final test in the Internet Tablet itself, without a single change in the source code, since GStreamer refers to modules as text names.
Line 19: Line 20:
One important note about the camera in the maemo device: only one application can use it at any given time. So, while using the camera in an application, other tasks that could possibly make use of it (e.g. a video call) will be blocked.
One important note about the camera in the maemo device: only one application can use it at any given time. So, while using the camera in an application, other tasks that could possibly make use of it (e.g. a video call) will be blocked.
-
To ease developing an application that does video recording and still image capture, a high level gstreamer element has been developed. CameraBin follows the idea behind other high level elements like playbin2. One can add capture support to an application without the need to deal with the lowlevel media-processing graph.
+
To ease developing an application that does video recording and still image capture, a high level gstreamer element has been developed. CameraBin follows the idea behind other high level elements like <code>playbin2</code>. One can add capture support to an application without the need to deal with the lowlevel media-processing graph.
To demonstrate how the camera manipulation is performed, a low-level example application is provided and discussed.
To demonstrate how the camera manipulation is performed, a low-level example application is provided and discussed.
-
 
==Camera Manipulation in C Language ==
==Camera Manipulation in C Language ==
-
This C application allows to use the maemo device as a "mirror" (i.e. showing the camera input in the screen), as well as allows to take pictures to be saved as JPEG files (which illustrates the video frame buffer manipulation).
 
-
In this example, the function initialize_pipeline() is most interesting, since it is responsible for creating the GStreamer pipeline, sourcing data from Video4Linux and sinking it to a ''xvimagesink'' (which is an optimized X framebuffer). The pipeline scheme is as follows: example_camera.c
+
This C application allows to use the Maemo device as a "mirror" (i.e. showing the camera input in the screen), as well as allows to take pictures to be saved as JPEG files (which illustrates the video frame buffer manipulation).
-
<tt><span>''<span><font color="#9A1900">/* Initialize the the Gstreamer pipeline. Below is a diagram</font></span>''</span>
+
In this example, the function <code>initialize_pipeline()</code> is most interesting, since it is responsible for creating the GStreamer pipeline, sourcing data from Video4Linux and sinking it to a <code>xvimagesink</code> (which is an optimized X framebuffer). The pipeline scheme is as follows ([https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/maemo-examples/example_camera.c see example_camera.c in maemo-examples]):
-
<span>''<span><font color="#9A1900"> * of the pipeline that will be created:</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * </font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> *                            |Screen|  |Screen|</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> *                          -&gt;|queue |-&gt;|sink  |-&gt; Display</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * |Camera|  |CSP  |  |Tee|/</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * |src  |-&gt;|Filter|-&gt;|  |  |Image|  |Image |  |Image|</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> *                          -&gt;|queue|-&gt; |filter|-&gt;|sink |-&gt; JPEG file</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> */</font></span>''</span></tt>
+
-
Between the source and sinks, there are two ''ffmpegcolorspace'' filters, one to configure the camera frame rate and the picture size expected by the JPEG encoder, and the second to satisfy the video sink. Capabilities ("caps") are employed to tell which format the data needs to have when exiting the filter.
+
<source lang="c">
 +
/* Initialize the the Gstreamer pipeline. Below is a diagram
 +
* of the pipeline that will be created:
 +
*
 +
*                            |Screen|  |Screen|
 +
*                          ->|queue |->|sink  |-> Display
 +
* |Camera|  |CSP  |  |Tee|/
 +
* |src  |->|Filter|->|  |  |Image|  |Image |  |Image|
 +
*                          ->|queue|-> |filter|->|sink |-> JPEG file
 +
*/
 +
</source>
 +
 
 +
Between the source and sinks, there are two <code>ffmpegcolorspace</code> filters, one to configure the camera frame rate and the picture size expected by the JPEG encoder, and the second to satisfy the video sink. Capabilities ("caps") are employed to tell which format the data needs to have when exiting the filter.
The second filter is necessary, since the video sink may have different requirements (bit depth, color space) from the JPEG encoder. Those requirements can vary also according to the hardware.
The second filter is necessary, since the video sink may have different requirements (bit depth, color space) from the JPEG encoder. Those requirements can vary also according to the hardware.
Line 45: Line 48:
Because there are two sinks, the queues are important, since they guarantee that each pipeline segment operates on its own thread downstream the queue. This ensures that the different sinks can synchronize without waiting for each other.
Because there are two sinks, the queues are important, since they guarantee that each pipeline segment operates on its own thread downstream the queue. This ensures that the different sinks can synchronize without waiting for each other.
-
This sample application is not different from other GStreamer applications, be it Linux-generic or maemo-specific apps: example_camera.c
+
This sample application is not different from other GStreamer applications, be it Linux-generic or Maemo-specific apps ([https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/maemo-examples/example_camera.c see example_camera.c in maemo-examples]):
 +
 
 +
<source lang="c">
 +
static gboolean initialize_pipeline(AppData *appdata,
 +
int *argc, char ***argv)
 +
{
 +
GstElement *pipeline, *camera_src, *screen_sink, *image_sink;
 +
GstElement *screen_queue, *image_queue;
 +
GstElement *csp_filter, *image_filter, *tee;
 +
GstCaps *caps;
 +
GstBus *bus;
 +
/* Initialize Gstreamer */
 +
gst_init(argc, argv);
 +
/* Create pipeline and attach a callback to it's
 +
* message bus */
 +
pipeline = gst_pipeline_new("test-camera");
 +
bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
 +
gst_bus_add_watch(bus, (GstBusFunc)bus_callback, appdata);
 +
gst_object_unref(GST_OBJECT(bus));
 +
/* Save pipeline to the AppData structure */
 +
appdata->pipeline = pipeline;
 +
/* Create elements */
 +
/* Camera video stream comes from a Video4Linux driver */
 +
camera_src = gst_element_factory_make(VIDEO_SRC, "camera_src");
 +
/* Colorspace filter is needed to make sure that sinks understands
 +
* the stream coming from the camera */
 +
csp_filter = gst_element_factory_make("ffmpegcolorspace", "csp_filter");
 +
/* Tee that copies the stream to multiple outputs */
 +
tee = gst_element_factory_make("tee", "tee");
 +
/* Queue creates new thread for the stream */
 +
screen_queue = gst_element_factory_make("queue", "screen_queue");
 +
/* Sink that shows the image on screen. Xephyr doesn't support XVideo
 +
* extension, so it needs to use ximagesink, but the device uses
 +
* xvimagesink */
 +
screen_sink = gst_element_factory_make(VIDEO_SINK, "screen_sink");
 +
/* Creates separate thread for the stream from which the image
 +
* is captured */
 +
image_queue = gst_element_factory_make("queue", "image_queue");
 +
/* Filter to convert stream to use format that the gdkpixbuf library
 +
* can use */
 +
image_filter = gst_element_factory_make("ffmpegcolorspace", "image_filter");
 +
/* A dummy sink for the image stream. Goes to bitheaven */
 +
image_sink = gst_element_factory_make("fakesink", "image_sink");
 +
/* Check that elements are correctly initialized */
 +
if(!(pipeline && camera_src && screen_sink && csp_filter && screen_queue
 +
&& image_queue && image_filter && image_sink))
 +
{
 +
g_critical("Couldn't create pipeline elements");
 +
return FALSE;
 +
}
 +
/* Set image sink to emit handoff-signal before throwing away
 +
* it's buffer */
 +
g_object_set(G_OBJECT(image_sink),
 +
"signal-handoffs", TRUE, NULL);
 +
/* Add elements to the pipeline. This has to be done prior to
 +
* linking them */
 +
gst_bin_add_many(GST_BIN(pipeline), camera_src, csp_filter,
 +
tee, screen_queue, screen_sink, image_queue,
 +
image_filter, image_sink, NULL);
 +
/* Specify what kind of video is wanted from the camera */
 +
caps = gst_caps_new_simple("video/x-raw-rgb",
 +
"width", G_TYPE_INT, 640,
 +
"height", G_TYPE_INT, 480,
 +
NULL);
 +
/* Link the camera source and colorspace filter using capabilities
 +
* specified */
 +
if(!gst_element_link_filtered(camera_src, csp_filter, caps))
 +
{
 +
return FALSE;
 +
}
 +
gst_caps_unref(caps);
 +
/* Connect Colorspace Filter -> Tee -> Screen Queue -> Screen Sink
 +
* This finalizes the initialization of the screen-part of the pipeline */
 +
if(!gst_element_link_many(csp_filter, tee, screen_queue, screen_sink, NULL))
 +
{
 +
return FALSE;
 +
}
 +
/* gdkpixbuf requires 8 bits per sample which is 24 bits per
 +
* pixel */
 +
caps = gst_caps_new_simple("video/x-raw-rgb",
 +
"width", G_TYPE_INT, 640,
 +
"height", G_TYPE_INT, 480,
 +
"bpp", G_TYPE_INT, 24,
 +
"depth", G_TYPE_INT, 24,
 +
"framerate", GST_TYPE_FRACTION, 15, 1,
 +
NULL);
 +
/* Link the image-branch of the pipeline. The pipeline is
 +
* ready after this */
 +
if(!gst_element_link_many(tee, image_queue, image_filter, NULL)) return FALSE;
 +
if(!gst_element_link_filtered(image_filter, image_sink, caps)) return FALSE;
 +
gst_caps_unref(caps);
 +
/* As soon as screen is exposed, window ID will be advised to the sink */
 +
g_signal_connect(appdata->screen, "expose-event", G_CALLBACK(expose_cb),
 +
screen_sink);
 +
gst_element_set_state(pipeline, GST_STATE_PLAYING);
 +
return TRUE;
 +
}
 +
</source>
 +
 
 +
The following function is called back when the user has pressed the "Take photo" button, and the image sink has data. It will forward the image buffer to <code>create_jpeg()</code> ([https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/maemo-examples/example_camera.c see example_camera.c in maemo-examples]):
 +
 
 +
<source lang="c">
 +
/* This callback will be registered to the image sink
 +
* after user requests a photo */
 +
static gboolean buffer_probe_callback(
 +
GstElement *image_sink,
 +
GstBuffer *buffer, GstPad *pad, AppData *appdata)
 +
{
 +
GstMessage *message;
 +
gchar *message_name;
 +
/* This is the raw RGB-data that image sink is about
 +
* to discard */
 +
unsigned char *data_photo =
 +
    (unsigned char *) GST_BUFFER_DATA(buffer);
 +
/* Create a JPEG of the data and check the status */
 +
if(!create_jpeg(data_photo))
 +
message_name = "photo-failed";
 +
else
 +
message_name = "photo-taken";
 +
/* Disconnect the handler so no more photos
 +
* are taken */
 +
g_signal_handler_disconnect(G_OBJECT(image_sink),
 +
appdata->buffer_cb_id);
 +
/* Create and send an application message which will be
 +
* catched in the bus watcher function. This has to be
 +
* sent as a message because this callback is called in
 +
* a gstreamer thread and calling GUI-functions here would
 +
* lead to X-server synchronization problems */
 +
message = gst_message_new_application(GST_OBJECT(appdata->pipeline),
 +
gst_structure_new(message_name, NULL));
 +
gst_element_post_message(appdata->pipeline, message);
 +
/* Returning TRUE means that the buffer can is OK to be
 +
* sent forward. When using fakesink this doesn't really
 +
* matter because the data is discarded anyway */
 +
return TRUE;
 +
}
 +
</source>
 +
 
 +
The <code>xvimagesink</code> GStreamer module will normally create a new window just for itself. Since the video is supposed to be shown inside the main application window, the X-Window window ID needs to be passed to the module, as soon as the ID exists ([https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/maemo-examples/example_camera.c see example_camera.c in maemo-examples]):
 +
 
 +
<source lang="c">
 +
/* Callback to be called when the screen-widget is exposed */
 +
static gboolean expose_cb(GtkWidget * widget, GdkEventExpose * event, gpointer data)
 +
{
 +
/* Tell the xvimagesink/ximagesink the x-window-id of the screen
 +
* widget in which the video is shown. After this the video
 +
* is shown in the correct widget */
 +
gst_x_overlay_set_xwindow_id(GST_X_OVERLAY(data),
 +
    GDK_WINDOW_XWINDOW(widget->window));
 +
return FALSE;
 +
}
 +
</source>
 +
 
 +
For the sake of completeness, it follows the JPEG encoding function. It is worthwhile to mention that the buffer that came from GStreamer is a simple linear framebuffer ([https://garage.maemo.org/svn/maemoexamples/trunk/maemo-examples/example_camera.c see example_camera.c in maemo-examples]):
-
<tt><span>'''<span><font color="#0000FF">static</font></span>'''</span> <span><font color="#008080">gboolean</font></span> <span>'''<span><font color="#000000">initialize_pipeline</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#008080">AppData</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>appdata<span><font color="#990000">,</font></span>
+
<source lang="c">
-
<span><font color="#009900">int</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>argc<span><font color="#990000">,</font></span> <span><font color="#009900">char</font></span> <span><font color="#990000"><nowiki>***</nowiki></font></span>argv<span><font color="#990000">)</font></span>
+
/* Creates a jpeg file from the buffer's raw image data */
-
<span><font color="#FF0000">{</font></span>
+
static gboolean create_jpeg(unsigned char *data)
-
<span><font color="#008080">GstElement</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>pipeline<span><font color="#990000">,</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>camera_src<span><font color="#990000">,</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>screen_sink<span><font color="#990000">,</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>image_sink<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
{
-
<span><font color="#008080">GstElement</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>screen_queue<span><font color="#990000">,</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>image_queue<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
GdkPixbuf *pixbuf = NULL;
-
<span><font color="#008080">GstElement</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>csp_filter<span><font color="#990000">,</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>image_filter<span><font color="#990000">,</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>tee<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
GError *error = NULL;
-
<span><font color="#008080">GstCaps</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>caps<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
guint height, width, bpp;
-
<span><font color="#008080">GstBus</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>bus<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
const gchar *directory;
-
<span>''<span><font color="#9A1900">/* Initialize Gstreamer */</font></span>''</span>
+
GString *filename;
-
<span>'''<span><font color="#000000">gst_init</font></span>'''</span><span><font color="#990000">(</font></span>argc<span><font color="#990000">,</font></span> argv<span><font color="#990000">);</font></span>
+
guint base_len, i;
-
<span>''<span><font color="#9A1900">/* Create pipeline and attach a callback to it's</font></span>''</span>
+
struct stat statbuf;
-
<span>''<span><font color="#9A1900"> * message bus */</font></span>''</span>
+
-
pipeline <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">gst_pipeline_new</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"test-camera"</font></span><span><font color="#990000">);</font></span>
+
-
bus <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">gst_pipeline_get_bus</font></span>'''</span><span><font color="#990000">(</font></span><span>'''<span><font color="#000000">GST_PIPELINE</font></span>'''</span><span><font color="#990000">(</font></span>pipeline<span><font color="#990000">));</font></span>
+
-
<span>'''<span><font color="#000000">gst_bus_add_watch</font></span>'''</span><span><font color="#990000">(</font></span>bus<span><font color="#990000">,</font></span> <span><font color="#990000">(</font></span>GstBusFunc<span><font color="#990000">)</font></span>bus_callback<span><font color="#990000">,</font></span> appdata<span><font color="#990000">);</font></span>
+
-
<span>'''<span><font color="#000000">gst_object_unref</font></span>'''</span><span><font color="#990000">(</font></span><span>'''<span><font color="#000000">GST_OBJECT</font></span>'''</span><span><font color="#990000">(</font></span>bus<span><font color="#990000">));</font></span>
+
-
<span>''<span><font color="#9A1900">/* Save pipeline to the AppData structure */</font></span>''</span>
+
-
appdata<span><font color="#990000">-&gt;</font></span>pipeline <span><font color="#990000"><nowiki>=</nowiki></font></span> pipeline<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span>''<span><font color="#9A1900">/* Create elements */</font></span>''</span>
+
-
<span>''<span><font color="#9A1900">/* Camera video stream comes from a Video4Linux driver */</font></span>''</span>
+
-
camera_src <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">gst_element_factory_make</font></span>'''</span><span><font color="#990000">(</font></span>VIDEO_SRC<span><font color="#990000">,</font></span> <span><font color="#FF0000">"camera_src"</font></span><span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* Colorspace filter is needed to make sure that sinks understands</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * the stream coming from the camera */</font></span>''</span>
+
-
csp_filter <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">gst_element_factory_make</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"ffmpegcolorspace"</font></span><span><font color="#990000">,</font></span> <span><font color="#FF0000">"csp_filter"</font></span><span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* Tee that copies the stream to multiple outputs */</font></span>''</span>
+
-
tee <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">gst_element_factory_make</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"tee"</font></span><span><font color="#990000">,</font></span> <span><font color="#FF0000">"tee"</font></span><span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* Queue creates new thread for the stream */</font></span>''</span>
+
-
screen_queue <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">gst_element_factory_make</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"queue"</font></span><span><font color="#990000">,</font></span> <span><font color="#FF0000">"screen_queue"</font></span><span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* Sink that shows the image on screen. Xephyr doesn't support XVideo</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * extension, so it needs to use ximagesink, but the device uses</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * xvimagesink */</font></span>''</span>
+
-
screen_sink <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">gst_element_factory_make</font></span>'''</span><span><font color="#990000">(</font></span>VIDEO_SINK<span><font color="#990000">,</font></span> <span><font color="#FF0000">"screen_sink"</font></span><span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* Creates separate thread for the stream from which the image</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * is captured */</font></span>''</span>
+
-
image_queue <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">gst_element_factory_make</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"queue"</font></span><span><font color="#990000">,</font></span> <span><font color="#FF0000">"image_queue"</font></span><span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* Filter to convert stream to use format that the gdkpixbuf library</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * can use */</font></span>''</span>
+
-
image_filter <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">gst_element_factory_make</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"ffmpegcolorspace"</font></span><span><font color="#990000">,</font></span> <span><font color="#FF0000">"image_filter"</font></span><span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* A dummy sink for the image stream. Goes to bitheaven */</font></span>''</span>
+
-
image_sink <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">gst_element_factory_make</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"fakesink"</font></span><span><font color="#990000">,</font></span> <span><font color="#FF0000">"image_sink"</font></span><span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* Check that elements are correctly initialized */</font></span>''</span>
+
-
<span>'''<span><font color="#0000FF">if</font></span>'''</span><span><font color="#990000">(!(</font></span>pipeline <span><font color="#990000">&amp;&amp;</font></span> camera_src <span><font color="#990000">&amp;&amp;</font></span> screen_sink <span><font color="#990000">&amp;&amp;</font></span> csp_filter <span><font color="#990000">&amp;&amp;</font></span> screen_queue
+
-
<span><font color="#990000">&amp;&amp;</font></span> image_queue <span><font color="#990000">&amp;&amp;</font></span> image_filter <span><font color="#990000">&amp;&amp;</font></span> image_sink<span><font color="#990000">))</font></span>
+
-
<span><font color="#FF0000">{</font></span>
+
-
<span>'''<span><font color="#000000">g_critical</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Couldn't create pipeline elements"</font></span><span><font color="#990000">);</font></span>
+
-
<span>'''<span><font color="#0000FF">return</font></span>'''</span> FALSE<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span><font color="#FF0000">}</font></span>
+
-
<span>''<span><font color="#9A1900">/* Set image sink to emit handoff-signal before throwing away</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * it's buffer */</font></span>''</span>
+
-
<span>'''<span><font color="#000000">g_object_set</font></span>'''</span><span><font color="#990000">(</font></span><span>'''<span><font color="#000000">G_OBJECT</font></span>'''</span><span><font color="#990000">(</font></span>image_sink<span><font color="#990000">),</font></span>
+
-
<span><font color="#FF0000">"signal-handoffs"</font></span><span><font color="#990000">,</font></span> TRUE<span><font color="#990000">,</font></span> NULL<span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* Add elements to the pipeline. This has to be done prior to</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * linking them */</font></span>''</span>
+
-
<span>'''<span><font color="#000000">gst_bin_add_many</font></span>'''</span><span><font color="#990000">(</font></span><span>'''<span><font color="#000000">GST_BIN</font></span>'''</span><span><font color="#990000">(</font></span>pipeline<span><font color="#990000">),</font></span> camera_src<span><font color="#990000">,</font></span> csp_filter<span><font color="#990000">,</font></span>
+
-
tee<span><font color="#990000">,</font></span> screen_queue<span><font color="#990000">,</font></span> screen_sink<span><font color="#990000">,</font></span> image_queue<span><font color="#990000">,</font></span>
+
-
image_filter<span><font color="#990000">,</font></span> image_sink<span><font color="#990000">,</font></span> NULL<span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* Specify what kind of video is wanted from the camera */</font></span>''</span>
+
-
caps <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">gst_caps_new_simple</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"video/x-raw-rgb"</font></span><span><font color="#990000">,</font></span>
+
-
<span><font color="#FF0000">"width"</font></span><span><font color="#990000">,</font></span> G_TYPE_INT<span><font color="#990000">,</font></span> <span><font color="#993399">640</font></span><span><font color="#990000">,</font></span>
+
-
<span><font color="#FF0000">"height"</font></span><span><font color="#990000">,</font></span> G_TYPE_INT<span><font color="#990000">,</font></span> <span><font color="#993399">480</font></span><span><font color="#990000">,</font></span>
+
-
NULL<span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* Link the camera source and colorspace filter using capabilities</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * specified */</font></span>''</span>
+
-
<span>'''<span><font color="#0000FF">if</font></span>'''</span><span><font color="#990000">(!</font></span><span>'''<span><font color="#000000">gst_element_link_filtered</font></span>'''</span><span><font color="#990000">(</font></span>camera_src<span><font color="#990000">,</font></span> csp_filter<span><font color="#990000">,</font></span> caps<span><font color="#990000">))</font></span>
+
-
<span><font color="#FF0000">{</font></span>
+
-
<span>'''<span><font color="#0000FF">return</font></span>'''</span> FALSE<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span><font color="#FF0000">}</font></span>
+
-
<span>'''<span><font color="#000000">gst_caps_unref</font></span>'''</span><span><font color="#990000">(</font></span>caps<span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* Connect Colorspace Filter -&gt; Tee -&gt; Screen Queue -&gt; Screen Sink</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * This finalizes the initialization of the screen-part of the pipeline */</font></span>''</span>
+
-
<span>'''<span><font color="#0000FF">if</font></span>'''</span><span><font color="#990000">(!</font></span><span>'''<span><font color="#000000">gst_element_link_many</font></span>'''</span><span><font color="#990000">(</font></span>csp_filter<span><font color="#990000">,</font></span> tee<span><font color="#990000">,</font></span> screen_queue<span><font color="#990000">,</font></span> screen_sink<span><font color="#990000">,</font></span> NULL<span><font color="#990000">))</font></span>
+
-
<span><font color="#FF0000">{</font></span>
+
-
<span>'''<span><font color="#0000FF">return</font></span>'''</span> FALSE<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span><font color="#FF0000">}</font></span>
+
-
<span>''<span><font color="#9A1900">/* gdkpixbuf requires 8 bits per sample which is 24 bits per</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * pixel */</font></span>''</span>
+
-
caps <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">gst_caps_new_simple</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"video/x-raw-rgb"</font></span><span><font color="#990000">,</font></span>
+
-
<span><font color="#FF0000">"width"</font></span><span><font color="#990000">,</font></span> G_TYPE_INT<span><font color="#990000">,</font></span> <span><font color="#993399">640</font></span><span><font color="#990000">,</font></span>
+
-
<span><font color="#FF0000">"height"</font></span><span><font color="#990000">,</font></span> G_TYPE_INT<span><font color="#990000">,</font></span> <span><font color="#993399">480</font></span><span><font color="#990000">,</font></span>
+
-
<span><font color="#FF0000">"bpp"</font></span><span><font color="#990000">,</font></span> G_TYPE_INT<span><font color="#990000">,</font></span> <span><font color="#993399">24</font></span><span><font color="#990000">,</font></span>
+
-
<span><font color="#FF0000">"depth"</font></span><span><font color="#990000">,</font></span> G_TYPE_INT<span><font color="#990000">,</font></span> <span><font color="#993399">24</font></span><span><font color="#990000">,</font></span>
+
-
<span><font color="#FF0000">"framerate"</font></span><span><font color="#990000">,</font></span> GST_TYPE_FRACTION<span><font color="#990000">,</font></span> <span><font color="#993399">15</font></span><span><font color="#990000">,</font></span> <span><font color="#993399">1</font></span><span><font color="#990000">,</font></span>
+
-
NULL<span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* Link the image-branch of the pipeline. The pipeline is</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * ready after this */</font></span>''</span>
+
-
<span>'''<span><font color="#0000FF">if</font></span>'''</span><span><font color="#990000">(!</font></span><span>'''<span><font color="#000000">gst_element_link_many</font></span>'''</span><span><font color="#990000">(</font></span>tee<span><font color="#990000">,</font></span> image_queue<span><font color="#990000">,</font></span> image_filter<span><font color="#990000">,</font></span> NULL<span><font color="#990000">))</font></span> <span>'''<span><font color="#0000FF">return</font></span>'''</span> FALSE<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span>'''<span><font color="#0000FF">if</font></span>'''</span><span><font color="#990000">(!</font></span><span>'''<span><font color="#000000">gst_element_link_filtered</font></span>'''</span><span><font color="#990000">(</font></span>image_filter<span><font color="#990000">,</font></span> image_sink<span><font color="#990000">,</font></span> caps<span><font color="#990000">))</font></span> <span>'''<span><font color="#0000FF">return</font></span>'''</span> FALSE<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span>'''<span><font color="#000000">gst_caps_unref</font></span>'''</span><span><font color="#990000">(</font></span>caps<span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* As soon as screen is exposed, window ID will be advised to the sink */</font></span>''</span>
+
-
<span>'''<span><font color="#000000">g_signal_connect</font></span>'''</span><span><font color="#990000">(</font></span>appdata<span><font color="#990000">-&gt;</font></span>screen<span><font color="#990000">,</font></span> <span><font color="#FF0000">"expose-event"</font></span><span><font color="#990000">,</font></span> <span>'''<span><font color="#000000">G_CALLBACK</font></span>'''</span><span><font color="#990000">(</font></span>expose_cb<span><font color="#990000">),</font></span>
+
-
screen_sink<span><font color="#990000">);</font></span>
+
-
<span>'''<span><font color="#000000">gst_element_set_state</font></span>'''</span><span><font color="#990000">(</font></span>pipeline<span><font color="#990000">,</font></span> GST_STATE_PLAYING<span><font color="#990000">);</font></span>
+
-
<span>'''<span><font color="#0000FF">return</font></span>'''</span> TRUE<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span><font color="#FF0000">}</font></span></tt>
+
-
The following function is called back when the user has pressed the "Take photo" button, and the image sink has data. It will forward the image buffer to ''create_jpeg()''<nowiki>: </nowiki>example_camera.c
+
width = 640; height = 480; bpp = 24;
-
<tt><span>''<span><font color="#9A1900">/* This callback will be registered to the image sink</font></span>''</span>
+
/* Define the save folder */
-
<span>''<span><font color="#9A1900"> * after user requests a photo */</font></span>''</span>
+
directory = SAVE_FOLDER_DEFAULT;
-
<span>'''<span><font color="#0000FF">static</font></span>'''</span> <span><font color="#008080">gboolean</font></span> <span>'''<span><font color="#000000">buffer_probe_callback</font></span>'''</span><span><font color="#990000">(</font></span>
+
if(directory == NULL)
-
<span><font color="#008080">GstElement</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>image_sink<span><font color="#990000">,</font></span>
+
{
-
<span><font color="#008080">GstBuffer</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>buffer<span><font color="#990000">,</font></span> <span><font color="#008080">GstPad</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>pad<span><font color="#990000">,</font></span> <span><font color="#008080">AppData</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>appdata<span><font color="#990000">)</font></span>
+
directory = g_get_tmp_dir();
-
<span><font color="#FF0000">{</font></span>
+
}
-
<span><font color="#008080">GstMessage</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>message<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span><font color="#008080">gchar</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>message_name<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span>''<span><font color="#9A1900">/* This is the raw RGB-data that image sink is about</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * to discard */</font></span>''</span>
+
-
<span><font color="#009900">unsigned</font></span> <span><font color="#009900">char</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>data_photo <span><font color="#990000"><nowiki>=</nowiki></font></span>
+
-
    <span><font color="#990000">(</font></span><span><font color="#009900">unsigned</font></span> <span><font color="#009900">char</font></span> <span><font color="#990000"><nowiki>*)</nowiki></font></span> <span>'''<span><font color="#000000">GST_BUFFER_DATA</font></span>'''</span><span><font color="#990000">(</font></span>buffer<span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* Create a JPEG of the data and check the status */</font></span>''</span>
+
-
<span>'''<span><font color="#0000FF">if</font></span>'''</span><span><font color="#990000">(!</font></span><span>'''<span><font color="#000000">create_jpeg</font></span>'''</span><span><font color="#990000">(</font></span>data_photo<span><font color="#990000">))</font></span>
+
-
message_name <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#FF0000">"photo-failed"</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span>'''<span><font color="#0000FF">else</font></span>'''</span>
+
-
message_name <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#FF0000">"photo-taken"</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span>''<span><font color="#9A1900">/* Disconnect the handler so no more photos</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * are taken */</font></span>''</span>
+
-
<span>'''<span><font color="#000000">g_signal_handler_disconnect</font></span>'''</span><span><font color="#990000">(</font></span><span>'''<span><font color="#000000">G_OBJECT</font></span>'''</span><span><font color="#990000">(</font></span>image_sink<span><font color="#990000">),</font></span>
+
-
appdata<span><font color="#990000">-&gt;</font></span>buffer_cb_id<span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* Create and send an application message which will be</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * catched in the bus watcher function. This has to be</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * sent as a message because this callback is called in</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * a gstreamer thread and calling GUI-functions here would</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * lead to X-server synchronization problems */</font></span>''</span>
+
-
message <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">gst_message_new_application</font></span>'''</span><span><font color="#990000">(</font></span><span>'''<span><font color="#000000">GST_OBJECT</font></span>'''</span><span><font color="#990000">(</font></span>appdata<span><font color="#990000">-&gt;</font></span>pipeline<span><font color="#990000">),</font></span>
+
-
<span>'''<span><font color="#000000">gst_structure_new</font></span>'''</span><span><font color="#990000">(</font></span>message_name<span><font color="#990000">,</font></span> NULL<span><font color="#990000">));</font></span>
+
-
<span>'''<span><font color="#000000">gst_element_post_message</font></span>'''</span><span><font color="#990000">(</font></span>appdata<span><font color="#990000">-&gt;</font></span>pipeline<span><font color="#990000">,</font></span> message<span><font color="#990000">);</font></span>
+
-
<span>''<span><font color="#9A1900">/* Returning TRUE means that the buffer can is OK to be</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * sent forward. When using fakesink this doesn't really</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * matter because the data is discarded anyway */</font></span>''</span>
+
-
<span>'''<span><font color="#0000FF">return</font></span>'''</span> TRUE<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span><font color="#FF0000">}</font></span></tt>
+
-
The ''xvimagesink'' GStreamer module will normally create a new window just for itself. Since the video is supposed to be shown inside the main application window, the X-Window window ID needs to be passed to the module, as soon as the ID exists: example_camera.c
+
/* Create an unique file name */
 +
filename = g_string_new(g_build_filename(directory, PHOTO_NAME_DEFAULT, NULL));
 +
base_len = filename->len;
 +
g_string_append(filename, PHOTO_NAME_SUFFIX_DEFAULT);
 +
for(i = 1; !stat(filename->str, &statbuf); ++i)
 +
{
 +
g_string_truncate(filename, base_len);
 +
g_string_append_printf(filename, "%d%s", i, PHOTO_NAME_SUFFIX_DEFAULT);
 +
}
-
<tt><span>''<span><font color="#9A1900">/* Callback to be called when the screen-widget is exposed */</font></span>''</span>
+
/* Create a pixbuf object from the data */
-
<span>'''<span><font color="#0000FF">static</font></span>'''</span> <span><font color="#008080">gboolean</font></span> <span>'''<span><font color="#000000">expose_cb</font></span>'''</span><span><font color="#990000">(</font></span>GtkWidget <span><font color="#990000"><nowiki>*</nowiki></font></span> widget<span><font color="#990000">,</font></span> GdkEventExpose <span><font color="#990000"><nowiki>*</nowiki></font></span> event<span><font color="#990000">,</font></span> <span><font color="#008080">gpointer</font></span> data<span><font color="#990000">)</font></span>
+
pixbuf = gdk_pixbuf_new_from_data(data,
-
<span><font color="#FF0000">{</font></span>
+
GDK_COLORSPACE_RGB, /* RGB-colorspace */
-
<span>''<span><font color="#9A1900">/* Tell the xvimagesink/ximagesink the x-window-id of the screen</font></span>''</span>
+
FALSE, /* No alpha-channel */
-
<span>''<span><font color="#9A1900"> * widget in which the video is shown. After this the video</font></span>''</span>
+
bpp/3, /* Bits per RGB-component */
-
<span>''<span><font color="#9A1900"> * is shown in the correct widget */</font></span>''</span>
+
width, height, /* Dimensions */
-
<span>'''<span><font color="#000000">gst_x_overlay_set_xwindow_id</font></span>'''</span><span><font color="#990000">(</font></span><span>'''<span><font color="#000000">GST_X_OVERLAY</font></span>'''</span><span><font color="#990000">(</font></span>data<span><font color="#990000">),</font></span>
+
3*width, /* Number of bytes between lines (ie stride) */
-
    <span>'''<span><font color="#000000">GDK_WINDOW_XWINDOW</font></span>'''</span><span><font color="#990000">(</font></span>widget<span><font color="#990000">-&gt;</font></span>window<span><font color="#990000">));</font></span>
+
NULL, NULL); /* Callbacks */
-
<span>'''<span><font color="#0000FF">return</font></span>'''</span> FALSE<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span><font color="#FF0000">}</font></span></tt>
+
-
For the sake of completeness, it follows the JPEG encoding function. It is worthwhile to mention that the buffer that came from GStreamer is a simple linear framebuffer: example_camera.c
+
/* Save the pixbuf content's in to a jpeg file and check for
 +
* errors */
 +
if(!gdk_pixbuf_save(pixbuf, filename->str, "jpeg", &error, NULL))
 +
{
 +
g_warning("%s\n", error->message);
 +
g_error_free(error);
 +
gdk_pixbuf_unref(pixbuf);
 +
g_string_free(filename, TRUE);
 +
return FALSE;
 +
}
-
<tt><span>''<span><font color="#9A1900">/* Creates a jpeg file from the buffer's raw image data */</font></span>''</span>
+
/* Free allocated resources and return TRUE which means
-
<span>'''<span><font color="#0000FF">static</font></span>'''</span> <span><font color="#008080">gboolean</font></span> <span>'''<span><font color="#000000">create_jpeg</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#009900">unsigned</font></span> <span><font color="#009900">char</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>data<span><font color="#990000">)</font></span>
+
* that the operation was succesful */
-
<span><font color="#FF0000">{</font></span>
+
g_string_free(filename, TRUE);
-
<span><font color="#008080">GdkPixbuf</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>pixbuf <span><font color="#990000"><nowiki>=</nowiki></font></span> NULL<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
gdk_pixbuf_unref(pixbuf);
-
<span><font color="#008080">GError</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>error <span><font color="#990000"><nowiki>=</nowiki></font></span> NULL<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
return TRUE;
-
<span><font color="#008080">guint</font></span> height<span><font color="#990000">,</font></span> width<span><font color="#990000">,</font></span> bpp<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
}
-
<span>'''<span><font color="#0000FF">const</font></span>'''</span> <span><font color="#008080">gchar</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>directory<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
</source>
-
<span><font color="#008080">GString</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>filename<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span><font color="#008080">guint</font></span> base_len<span><font color="#990000">,</font></span> i<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span>'''<span><font color="#0000FF">struct</font></span>'''</span> <span><font color="#008080">stat</font></span> statbuf<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
+
-
width <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#993399">640</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span> height <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#993399">480</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span> bpp <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#993399">24</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
+
-
<span>''<span><font color="#9A1900">/* Define the save folder */</font></span>''</span>
+
-
directory <span><font color="#990000"><nowiki>=</nowiki></font></span> SAVE_FOLDER_DEFAULT<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span>'''<span><font color="#0000FF">if</font></span>'''</span><span><font color="#990000">(</font></span>directory <span><font color="#990000"><nowiki>==</nowiki></font></span> NULL<span><font color="#990000">)</font></span>
+
-
<span><font color="#FF0000">{</font></span>
+
-
directory <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">g_get_tmp_dir</font></span>'''</span><span><font color="#990000">();</font></span>
+
-
<span><font color="#FF0000">}</font></span>
+
-
+
-
<span>''<span><font color="#9A1900">/* Create an unique file name */</font></span>''</span>
+
-
filename <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">g_string_new</font></span>'''</span><span><font color="#990000">(</font></span><span>'''<span><font color="#000000">g_build_filename</font></span>'''</span><span><font color="#990000">(</font></span>directory<span><font color="#990000">,</font></span> PHOTO_NAME_DEFAULT<span><font color="#990000">,</font></span> NULL<span><font color="#990000">));</font></span>
+
-
base_len <span><font color="#990000"><nowiki>=</nowiki></font></span> filename<span><font color="#990000">-&gt;</font></span>len<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span>'''<span><font color="#000000">g_string_append</font></span>'''</span><span><font color="#990000">(</font></span>filename<span><font color="#990000">,</font></span> PHOTO_NAME_SUFFIX_DEFAULT<span><font color="#990000">);</font></span>
+
-
<span>'''<span><font color="#0000FF">for</font></span>'''</span><span><font color="#990000">(</font></span>i <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#993399">1</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span> <span><font color="#990000"><nowiki>!</nowiki></font></span><span>'''<span><font color="#000000">stat</font></span>'''</span><span><font color="#990000">(</font></span>filename<span><font color="#990000">-&gt;</font></span>str<span><font color="#990000">,</font></span> <span><font color="#990000">&amp;</font></span>statbuf<span><font color="#990000">);</font></span> <span><font color="#990000">++</font></span>i<span><font color="#990000">)</font></span>
+
-
<span><font color="#FF0000">{</font></span>
+
-
<span>'''<span><font color="#000000">g_string_truncate</font></span>'''</span><span><font color="#990000">(</font></span>filename<span><font color="#990000">,</font></span> base_len<span><font color="#990000">);</font></span>
+
-
<span>'''<span><font color="#000000">g_string_append_printf</font></span>'''</span><span><font color="#990000">(</font></span>filename<span><font color="#990000">,</font></span> <span><font color="#FF0000">"%d%s"</font></span><span><font color="#990000">,</font></span> i<span><font color="#990000">,</font></span> PHOTO_NAME_SUFFIX_DEFAULT<span><font color="#990000">);</font></span>
+
-
<span><font color="#FF0000">}</font></span>
+
-
+
-
<span>''<span><font color="#9A1900">/* Create a pixbuf object from the data */</font></span>''</span>
+
-
pixbuf <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">gdk_pixbuf_new_from_data</font></span>'''</span><span><font color="#990000">(</font></span>data<span><font color="#990000">,</font></span>
+
-
GDK_COLORSPACE_RGB<span><font color="#990000">,</font></span> <span>''<span><font color="#9A1900">/* RGB-colorspace */</font></span>''</span>
+
-
FALSE<span><font color="#990000">,</font></span> <span>''<span><font color="#9A1900">/* No alpha-channel */</font></span>''</span>
+
-
bpp<span><font color="#990000">/</font></span><span><font color="#993399">3</font></span><span><font color="#990000">,</font></span> <span>''<span><font color="#9A1900">/* Bits per RGB-component */</font></span>''</span>
+
-
width<span><font color="#990000">,</font></span> height<span><font color="#990000">,</font></span> <span>''<span><font color="#9A1900">/* Dimensions */</font></span>''</span>
+
-
<span><font color="#993399">3</font></span><span><font color="#990000"><nowiki>*</nowiki></font></span>width<span><font color="#990000">,</font></span> <span>''<span><font color="#9A1900">/* Number of bytes between lines (ie stride) */</font></span>''</span>
+
-
NULL<span><font color="#990000">,</font></span> NULL<span><font color="#990000">);</font></span> <span>''<span><font color="#9A1900">/* Callbacks */</font></span>''</span>
+
-
+
-
<span>''<span><font color="#9A1900">/* Save the pixbuf content's in to a jpeg file and check for</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * errors */</font></span>''</span>
+
-
<span>'''<span><font color="#0000FF">if</font></span>'''</span><span><font color="#990000">(!</font></span><span>'''<span><font color="#000000">gdk_pixbuf_save</font></span>'''</span><span><font color="#990000">(</font></span>pixbuf<span><font color="#990000">,</font></span> filename<span><font color="#990000">-&gt;</font></span>str<span><font color="#990000">,</font></span> <span><font color="#FF0000">"jpeg"</font></span><span><font color="#990000">,</font></span> <span><font color="#990000">&amp;</font></span>error<span><font color="#990000">,</font></span> NULL<span><font color="#990000">))</font></span>
+
-
<span><font color="#FF0000">{</font></span>
+
-
<span>'''<span><font color="#000000">g_warning</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"%s</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">,</font></span> error<span><font color="#990000">-&gt;</font></span>message<span><font color="#990000">);</font></span>
+
-
<span>'''<span><font color="#000000">g_error_free</font></span>'''</span><span><font color="#990000">(</font></span>error<span><font color="#990000">);</font></span>
+
-
<span>'''<span><font color="#000000">gdk_pixbuf_unref</font></span>'''</span><span><font color="#990000">(</font></span>pixbuf<span><font color="#990000">);</font></span>
+
-
<span>'''<span><font color="#000000">g_string_free</font></span>'''</span><span><font color="#990000">(</font></span>filename<span><font color="#990000">,</font></span> TRUE<span><font color="#990000">);</font></span>
+
-
<span>'''<span><font color="#0000FF">return</font></span>'''</span> FALSE<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span><font color="#FF0000">}</font></span>
+
-
+
-
<span>''<span><font color="#9A1900">/* Free allocated resources and return TRUE which means</font></span>''</span>
+
-
<span>''<span><font color="#9A1900"> * that the operation was succesful */</font></span>''</span>
+
-
<span>'''<span><font color="#000000">g_string_free</font></span>'''</span><span><font color="#990000">(</font></span>filename<span><font color="#990000">,</font></span> TRUE<span><font color="#990000">);</font></span>
+
-
<span>'''<span><font color="#000000">gdk_pixbuf_unref</font></span>'''</span><span><font color="#990000">(</font></span>pixbuf<span><font color="#990000">);</font></span>
+
-
<span>'''<span><font color="#0000FF">return</font></span>'''</span> TRUE<span><font color="#990000"><nowiki>;</nowiki></font></span>
+
-
<span><font color="#FF0000">}</font></span></tt>
+
-
[[Category:Maemo training material]]
+
[[Category:Development]]
 +
[[Category:Documentation]]
 +
[[Category:Fremantle]]

Latest revision as of 09:49, 17 August 2010


This section explains how to use the Camera API to access the camera hardware that is present in some models of Nokia Internet Tablets.


Contents

[edit] Camera Hardware and Linux

The Linux operating system supports live video and audio hardware, such as webcams, TV tuners, video capture cards, FM radio tuners, video output devices etc. The primary API for the applications to access those devices is Video4Linux2.

Video4Linux2 is a kernel API, so there must be kernel drivers for each supported device. At the user level, device access is standardized via device files. In the case of video capture devices like cameras, which are the focus of this material, the files would be /dev/video0, /dev/video1, etc., as many as there are connected devices.

Data exchanged between the device file and user-level application has a standardized format for each device class. This allows the application to be instantly compatible with every video capture device that has a driver for Linux.

The built-in cameras present in Maemo devices are compatible with Video-4-Linux version 2 API. In principle, any application compatible with this API is easily portable to the Maemo platform.

Since the Maemo platform delegates all multimedia handling to the GStreamer framework, applications that need access to the built-in camera should employ GStreamer for this, instead of directly accessing Video4Linux devices, via the v4l2src GStreamer module.

Thanks to the flexibility of GStreamer, a developer can fully test any given application in a regular desktop PC with a connected webcam, and then perform the final test in the Internet Tablet itself, without a single change in the source code, since GStreamer refers to modules as text names.

One important note about the camera in the maemo device: only one application can use it at any given time. So, while using the camera in an application, other tasks that could possibly make use of it (e.g. a video call) will be blocked.

To ease developing an application that does video recording and still image capture, a high level gstreamer element has been developed. CameraBin follows the idea behind other high level elements like playbin2. One can add capture support to an application without the need to deal with the lowlevel media-processing graph.

To demonstrate how the camera manipulation is performed, a low-level example application is provided and discussed.

[edit] Camera Manipulation in C Language

This C application allows to use the Maemo device as a "mirror" (i.e. showing the camera input in the screen), as well as allows to take pictures to be saved as JPEG files (which illustrates the video frame buffer manipulation).

In this example, the function initialize_pipeline() is most interesting, since it is responsible for creating the GStreamer pipeline, sourcing data from Video4Linux and sinking it to a xvimagesink (which is an optimized X framebuffer). The pipeline scheme is as follows (see example_camera.c in maemo-examples):

/* Initialize the the Gstreamer pipeline. Below is a diagram
 * of the pipeline that will be created:
 * 
 *                             |Screen|  |Screen|
 *                           ->|queue |->|sink  |-> Display
 * |Camera|  |CSP   |  |Tee|/
 * |src   |->|Filter|->|   |  |Image|   |Image |  |Image|
 *                           ->|queue|-> |filter|->|sink |-> JPEG file
 */

Between the source and sinks, there are two ffmpegcolorspace filters, one to configure the camera frame rate and the picture size expected by the JPEG encoder, and the second to satisfy the video sink. Capabilities ("caps") are employed to tell which format the data needs to have when exiting the filter.

The second filter is necessary, since the video sink may have different requirements (bit depth, color space) from the JPEG encoder. Those requirements can vary also according to the hardware.

Because there are two sinks, the queues are important, since they guarantee that each pipeline segment operates on its own thread downstream the queue. This ensures that the different sinks can synchronize without waiting for each other.

This sample application is not different from other GStreamer applications, be it Linux-generic or Maemo-specific apps (see example_camera.c in maemo-examples):

static gboolean initialize_pipeline(AppData *appdata,
		int *argc, char ***argv)
{
	GstElement *pipeline, *camera_src, *screen_sink, *image_sink;
	GstElement *screen_queue, *image_queue;
	GstElement *csp_filter, *image_filter, *tee;
	GstCaps *caps;
	GstBus *bus;
	/* Initialize Gstreamer */
	gst_init(argc, argv);
	/* Create pipeline and attach a callback to it's
	 * message bus */
	pipeline = gst_pipeline_new("test-camera");
	bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
	gst_bus_add_watch(bus, (GstBusFunc)bus_callback, appdata);
	gst_object_unref(GST_OBJECT(bus));
	/* Save pipeline to the AppData structure */
	appdata->pipeline = pipeline;
	/* Create elements */
	/* Camera video stream comes from a Video4Linux driver */
	camera_src = gst_element_factory_make(VIDEO_SRC, "camera_src");
	/* Colorspace filter is needed to make sure that sinks understands
	 * the stream coming from the camera */
	csp_filter = gst_element_factory_make("ffmpegcolorspace", "csp_filter");
	/* Tee that copies the stream to multiple outputs */
	tee = gst_element_factory_make("tee", "tee");
	/* Queue creates new thread for the stream */
	screen_queue = gst_element_factory_make("queue", "screen_queue");
	/* Sink that shows the image on screen. Xephyr doesn't support XVideo
	 * extension, so it needs to use ximagesink, but the device uses
	 * xvimagesink */
	screen_sink = gst_element_factory_make(VIDEO_SINK, "screen_sink");
	/* Creates separate thread for the stream from which the image
	 * is captured */
	image_queue = gst_element_factory_make("queue", "image_queue");
	/* Filter to convert stream to use format that the gdkpixbuf library
	 * can use */
	image_filter = gst_element_factory_make("ffmpegcolorspace", "image_filter");
	/* A dummy sink for the image stream. Goes to bitheaven */
	image_sink = gst_element_factory_make("fakesink", "image_sink");
	/* Check that elements are correctly initialized */
	if(!(pipeline && camera_src && screen_sink && csp_filter && screen_queue
		&& image_queue && image_filter && image_sink))
	{
		g_critical("Couldn't create pipeline elements");
		return FALSE;
	}
	/* Set image sink to emit handoff-signal before throwing away
	 * it's buffer */
	g_object_set(G_OBJECT(image_sink),
			"signal-handoffs", TRUE, NULL);
	/* Add elements to the pipeline. This has to be done prior to
	 * linking them */
	gst_bin_add_many(GST_BIN(pipeline), camera_src, csp_filter,
			tee, screen_queue, screen_sink, image_queue,
			image_filter, image_sink, NULL);
	/* Specify what kind of video is wanted from the camera */
	caps = gst_caps_new_simple("video/x-raw-rgb",
			"width", G_TYPE_INT, 640,
			"height", G_TYPE_INT, 480,
			NULL);
	/* Link the camera source and colorspace filter using capabilities
	 * specified */
	if(!gst_element_link_filtered(camera_src, csp_filter, caps))
	{
		return FALSE;
	}
	gst_caps_unref(caps);
	/* Connect Colorspace Filter -> Tee -> Screen Queue -> Screen Sink
	 * This finalizes the initialization of the screen-part of the pipeline */
	if(!gst_element_link_many(csp_filter, tee, screen_queue, screen_sink, NULL))
	{
		return FALSE;
	}
	/* gdkpixbuf requires 8 bits per sample which is 24 bits per
	 * pixel */
	caps = gst_caps_new_simple("video/x-raw-rgb",
			"width", G_TYPE_INT, 640,
			"height", G_TYPE_INT, 480,
			"bpp", G_TYPE_INT, 24,
			"depth", G_TYPE_INT, 24,
			"framerate", GST_TYPE_FRACTION, 15, 1,
			NULL);
	/* Link the image-branch of the pipeline. The pipeline is
	 * ready after this */
	if(!gst_element_link_many(tee, image_queue, image_filter, NULL)) return FALSE;
	if(!gst_element_link_filtered(image_filter, image_sink, caps)) return FALSE;
	gst_caps_unref(caps);
	/* As soon as screen is exposed, window ID will be advised to the sink */
	g_signal_connect(appdata->screen, "expose-event", G_CALLBACK(expose_cb),
			 screen_sink);
	gst_element_set_state(pipeline, GST_STATE_PLAYING);
	return TRUE;
}

The following function is called back when the user has pressed the "Take photo" button, and the image sink has data. It will forward the image buffer to create_jpeg() (see example_camera.c in maemo-examples):

/* This callback will be registered to the image sink
 * after user requests a photo */
static gboolean buffer_probe_callback(
		GstElement *image_sink,
		GstBuffer *buffer, GstPad *pad, AppData *appdata)
{
	GstMessage *message;
	gchar *message_name;
	/* This is the raw RGB-data that image sink is about
	 * to discard */
	unsigned char *data_photo =
	    (unsigned char *) GST_BUFFER_DATA(buffer);
	/* Create a JPEG of the data and check the status */
	if(!create_jpeg(data_photo))
		message_name = "photo-failed";
	else
		message_name = "photo-taken";
	/* Disconnect the handler so no more photos
	 * are taken */
	g_signal_handler_disconnect(G_OBJECT(image_sink),
			appdata->buffer_cb_id);
	/* Create and send an application message which will be
	 * catched in the bus watcher function. This has to be
	 * sent as a message because this callback is called in
	 * a gstreamer thread and calling GUI-functions here would
	 * lead to X-server synchronization problems */
	message = gst_message_new_application(GST_OBJECT(appdata->pipeline),
			gst_structure_new(message_name, NULL));
	gst_element_post_message(appdata->pipeline, message);
	/* Returning TRUE means that the buffer can is OK to be
	 * sent forward. When using fakesink this doesn't really
	 * matter because the data is discarded anyway */
	return TRUE;
}

The xvimagesink GStreamer module will normally create a new window just for itself. Since the video is supposed to be shown inside the main application window, the X-Window window ID needs to be passed to the module, as soon as the ID exists (see example_camera.c in maemo-examples):

/* Callback to be called when the screen-widget is exposed */
static gboolean expose_cb(GtkWidget * widget, GdkEventExpose * event, gpointer data)
{
	/* Tell the xvimagesink/ximagesink the x-window-id of the screen
	 * widget in which the video is shown. After this the video
	 * is shown in the correct widget */
	gst_x_overlay_set_xwindow_id(GST_X_OVERLAY(data),
				     GDK_WINDOW_XWINDOW(widget->window));
	return FALSE;
}

For the sake of completeness, it follows the JPEG encoding function. It is worthwhile to mention that the buffer that came from GStreamer is a simple linear framebuffer (see example_camera.c in maemo-examples):

/* Creates a jpeg file from the buffer's raw image data */
static gboolean create_jpeg(unsigned char *data)
{
	GdkPixbuf *pixbuf = NULL;
	GError *error = NULL;
	guint height, width, bpp;
	const gchar *directory;
	GString *filename;
	guint base_len, i;
	struct stat statbuf;
 
	width = 640; height = 480; bpp = 24;
 
	/* Define the save folder */
	directory = SAVE_FOLDER_DEFAULT;
	if(directory == NULL)
	{
		directory = g_get_tmp_dir();
	}
 
	/* Create an unique file name */
	filename = g_string_new(g_build_filename(directory, PHOTO_NAME_DEFAULT, NULL));
	base_len = filename->len;
	g_string_append(filename, PHOTO_NAME_SUFFIX_DEFAULT);
	for(i = 1; !stat(filename->str, &statbuf); ++i)
	{
		g_string_truncate(filename, base_len);
		g_string_append_printf(filename, "%d%s", i, PHOTO_NAME_SUFFIX_DEFAULT);
	}
 
	/* Create a pixbuf object from the data */
	pixbuf = gdk_pixbuf_new_from_data(data,
			GDK_COLORSPACE_RGB, /* RGB-colorspace */
			FALSE, /* No alpha-channel */
			bpp/3, /* Bits per RGB-component */
			width, height, /* Dimensions */
			3*width, /* Number of bytes between lines (ie stride) */
			NULL, NULL); /* Callbacks */
 
	/* Save the pixbuf content's in to a jpeg file and check for
	 * errors */
	if(!gdk_pixbuf_save(pixbuf, filename->str, "jpeg", &error, NULL))
	{
		g_warning("%s\n", error->message);
		g_error_free(error);
		gdk_pixbuf_unref(pixbuf);
		g_string_free(filename, TRUE);
		return FALSE;
	}
 
	/* Free allocated resources and return TRUE which means
	 * that the operation was succesful */
	g_string_free(filename, TRUE);
	gdk_pixbuf_unref(pixbuf);
	return TRUE;
}