|
|
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. |
Line 13: |
Line 14: |
| 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 25: |
Line 26: |
| | | |
| ==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 ([https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/maemo-examples/example_camera.c see example_camera.c in maemo-examples]):
| + | 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"> * ->|queue |->|sink |-> Display</font></span>''</span>
| + | |
- | <span>''<span><font color="#9A1900"> * |Camera| |CSP | |Tee|/</font></span>''</span>
| + | |
- | <span>''<span><font color="#9A1900"> * |src |->|Filter|->| | |Image| |Image | |Image|</font></span>''</span>
| + | |
- | <span>''<span><font color="#9A1900"> * ->|queue|-> |filter|->|sink |-> 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 49: |
| 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 ([https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/maemo-examples/example_camera.c see example_camera.c in maemo-examples]): | + | 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]): |
| | | |
- | <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>
| + | static gboolean initialize_pipeline(AppData *appdata, |
- | <span><font color="#FF0000">{</font></span>
| + | int *argc, char ***argv) |
- | <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>
| + | GstElement *pipeline, *camera_src, *screen_sink, *image_sink; |
- | <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>
| + | GstElement *screen_queue, *image_queue; |
- | <span><font color="#008080">GstCaps</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>caps<span><font color="#990000"><nowiki>;</nowiki></font></span>
| + | GstElement *csp_filter, *image_filter, *tee; |
- | <span><font color="#008080">GstBus</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>bus<span><font color="#990000"><nowiki>;</nowiki></font></span>
| + | GstCaps *caps; |
- | <span>''<span><font color="#9A1900">/* Initialize Gstreamer */</font></span>''</span>
| + | GstBus *bus; |
- | <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>
| + | /* Initialize Gstreamer */ |
- | <span>''<span><font color="#9A1900">/* Create pipeline and attach a callback to it's</font></span>''</span>
| + | gst_init(argc, argv); |
- | <span>''<span><font color="#9A1900"> * message bus */</font></span>''</span>
| + | /* Create pipeline and attach a callback to it's |
- | 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>
| + | * message bus */ |
- | 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>
| + | pipeline = gst_pipeline_new("test-camera"); |
- | <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>
| + | bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); |
- | <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>
| + | gst_bus_add_watch(bus, (GstBusFunc)bus_callback, appdata); |
- | <span>''<span><font color="#9A1900">/* Save pipeline to the AppData structure */</font></span>''</span>
| + | gst_object_unref(GST_OBJECT(bus)); |
- | appdata<span><font color="#990000">-></font></span>pipeline <span><font color="#990000"><nowiki>=</nowiki></font></span> pipeline<span><font color="#990000"><nowiki>;</nowiki></font></span>
| + | /* Save pipeline to the AppData structure */ |
- | <span>''<span><font color="#9A1900">/* Create elements */</font></span>''</span>
| + | appdata->pipeline = pipeline; |
- | <span>''<span><font color="#9A1900">/* Camera video stream comes from a Video4Linux driver */</font></span>''</span>
| + | /* Create elements */ |
- | 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>
| + | /* Camera video stream comes from a Video4Linux driver */ |
- | <span>''<span><font color="#9A1900">/* Colorspace filter is needed to make sure that sinks understands</font></span>''</span>
| + | camera_src = gst_element_factory_make(VIDEO_SRC, "camera_src"); |
- | <span>''<span><font color="#9A1900"> * the stream coming from the camera */</font></span>''</span>
| + | /* Colorspace filter is needed to make sure that sinks understands |
- | 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>
| + | * the stream coming from the camera */ |
- | <span>''<span><font color="#9A1900">/* Tee that copies the stream to multiple outputs */</font></span>''</span>
| + | csp_filter = gst_element_factory_make("ffmpegcolorspace", "csp_filter"); |
- | 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>
| + | /* Tee that copies the stream to multiple outputs */ |
- | <span>''<span><font color="#9A1900">/* Queue creates new thread for the stream */</font></span>''</span>
| + | tee = gst_element_factory_make("tee", "tee"); |
- | 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>
| + | /* Queue creates new thread for the stream */ |
- | <span>''<span><font color="#9A1900">/* Sink that shows the image on screen. Xephyr doesn't support XVideo</font></span>''</span>
| + | screen_queue = gst_element_factory_make("queue", "screen_queue"); |
- | <span>''<span><font color="#9A1900"> * extension, so it needs to use ximagesink, but the device uses</font></span>''</span>
| + | /* Sink that shows the image on screen. Xephyr doesn't support XVideo |
- | <span>''<span><font color="#9A1900"> * xvimagesink */</font></span>''</span>
| + | * extension, so it needs to use ximagesink, but the device uses |
- | 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>
| + | * xvimagesink */ |
- | <span>''<span><font color="#9A1900">/* Creates separate thread for the stream from which the image</font></span>''</span>
| + | screen_sink = gst_element_factory_make(VIDEO_SINK, "screen_sink"); |
- | <span>''<span><font color="#9A1900"> * is captured */</font></span>''</span>
| + | /* Creates separate thread for the stream from which the image |
- | 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>
| + | * is captured */ |
- | <span>''<span><font color="#9A1900">/* Filter to convert stream to use format that the gdkpixbuf library</font></span>''</span>
| + | image_queue = gst_element_factory_make("queue", "image_queue"); |
- | <span>''<span><font color="#9A1900"> * can use */</font></span>''</span>
| + | /* Filter to convert stream to use format that the gdkpixbuf library |
- | 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>
| + | * can use */ |
- | <span>''<span><font color="#9A1900">/* A dummy sink for the image stream. Goes to bitheaven */</font></span>''</span>
| + | image_filter = gst_element_factory_make("ffmpegcolorspace", "image_filter"); |
- | 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>
| + | /* A dummy sink for the image stream. Goes to bitheaven */ |
- | <span>''<span><font color="#9A1900">/* Check that elements are correctly initialized */</font></span>''</span>
| + | image_sink = gst_element_factory_make("fakesink", "image_sink"); |
- | <span>'''<span><font color="#0000FF">if</font></span>'''</span><span><font color="#990000">(!(</font></span>pipeline <span><font color="#990000">&&</font></span> camera_src <span><font color="#990000">&&</font></span> screen_sink <span><font color="#990000">&&</font></span> csp_filter <span><font color="#990000">&&</font></span> screen_queue
| + | /* Check that elements are correctly initialized */ |
- | <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>
| + | if(!(pipeline && camera_src && screen_sink && csp_filter && screen_queue |
- | <span><font color="#FF0000">{</font></span>
| + | && image_queue && image_filter && image_sink)) |
- | <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>
| + | g_critical("Couldn't create pipeline elements"); |
- | <span><font color="#FF0000">}</font></span>
| + | return FALSE; |
- | <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>
| + | /* Set image sink to emit handoff-signal before throwing away |
- | <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>
| + | * it's buffer */ |
- | <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>
| + | g_object_set(G_OBJECT(image_sink), |
- | <span>''<span><font color="#9A1900">/* Add elements to the pipeline. This has to be done prior to</font></span>''</span>
| + | "signal-handoffs", TRUE, NULL); |
- | <span>''<span><font color="#9A1900"> * linking them */</font></span>''</span>
| + | /* Add elements to the pipeline. This has to be done prior to |
- | <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>
| + | * linking them */ |
- | 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>
| + | gst_bin_add_many(GST_BIN(pipeline), camera_src, csp_filter, |
- | image_filter<span><font color="#990000">,</font></span> image_sink<span><font color="#990000">,</font></span> NULL<span><font color="#990000">);</font></span>
| + | tee, screen_queue, screen_sink, image_queue, |
- | <span>''<span><font color="#9A1900">/* Specify what kind of video is wanted from the camera */</font></span>''</span>
| + | image_filter, image_sink, NULL); |
- | 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>
| + | /* Specify what kind of video is wanted from the camera */ |
- | <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>
| + | caps = gst_caps_new_simple("video/x-raw-rgb", |
- | <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>
| + | "width", G_TYPE_INT, 640, |
- | NULL<span><font color="#990000">);</font></span>
| + | "height", G_TYPE_INT, 480, |
- | <span>''<span><font color="#9A1900">/* Link the camera source and colorspace filter using capabilities</font></span>''</span>
| + | NULL); |
- | <span>''<span><font color="#9A1900"> * specified */</font></span>''</span>
| + | /* Link the camera source and colorspace filter using capabilities |
- | <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>
| + | * specified */ |
- | <span><font color="#FF0000">{</font></span>
| + | if(!gst_element_link_filtered(camera_src, csp_filter, caps)) |
- | <span>'''<span><font color="#0000FF">return</font></span>'''</span> FALSE<span><font color="#990000"><nowiki>;</nowiki></font></span>
| + | { |
- | <span><font color="#FF0000">}</font></span>
| + | return FALSE; |
- | <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 -> Tee -> Screen Queue -> Screen Sink</font></span>''</span>
| + | gst_caps_unref(caps); |
- | <span>''<span><font color="#9A1900"> * This finalizes the initialization of the screen-part of the pipeline */</font></span>''</span>
| + | /* Connect Colorspace Filter -> Tee -> Screen Queue -> Screen Sink |
- | <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>
| + | * This finalizes the initialization of the screen-part of the pipeline */ |
- | <span><font color="#FF0000">{</font></span>
| + | if(!gst_element_link_many(csp_filter, tee, screen_queue, screen_sink, NULL)) |
- | <span>'''<span><font color="#0000FF">return</font></span>'''</span> FALSE<span><font color="#990000"><nowiki>;</nowiki></font></span>
| + | { |
- | <span><font color="#FF0000">}</font></span>
| + | return FALSE; |
- | <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>
| + | /* gdkpixbuf requires 8 bits per sample which is 24 bits per |
- | 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>
| + | * pixel */ |
- | <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>
| + | caps = gst_caps_new_simple("video/x-raw-rgb", |
- | <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>
| + | "width", G_TYPE_INT, 640, |
- | <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>
| + | "height", G_TYPE_INT, 480, |
- | <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>
| + | "bpp", G_TYPE_INT, 24, |
- | <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>
| + | "depth", G_TYPE_INT, 24, |
- | NULL<span><font color="#990000">);</font></span>
| + | "framerate", GST_TYPE_FRACTION, 15, 1, |
- | <span>''<span><font color="#9A1900">/* Link the image-branch of the pipeline. The pipeline is</font></span>''</span>
| + | NULL); |
- | <span>''<span><font color="#9A1900"> * ready after this */</font></span>''</span>
| + | /* Link the image-branch of the pipeline. The pipeline is |
- | <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>
| + | * ready after this */ |
- | <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>
| + | if(!gst_element_link_many(tee, image_queue, image_filter, NULL)) return FALSE; |
- | <span>'''<span><font color="#000000">gst_caps_unref</font></span>'''</span><span><font color="#990000">(</font></span>caps<span><font color="#990000">);</font></span>
| + | if(!gst_element_link_filtered(image_filter, image_sink, caps)) return FALSE; |
- | <span>''<span><font color="#9A1900">/* As soon as screen is exposed, window ID will be advised to the sink */</font></span>''</span>
| + | gst_caps_unref(caps); |
- | <span>'''<span><font color="#000000">g_signal_connect</font></span>'''</span><span><font color="#990000">(</font></span>appdata<span><font color="#990000">-></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>
| + | /* As soon as screen is exposed, window ID will be advised to the sink */ |
- | screen_sink<span><font color="#990000">);</font></span>
| + | g_signal_connect(appdata->screen, "expose-event", G_CALLBACK(expose_cb), |
- | <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>
| + | screen_sink); |
- | <span>'''<span><font color="#0000FF">return</font></span>'''</span> TRUE<span><font color="#990000"><nowiki>;</nowiki></font></span>
| + | gst_element_set_state(pipeline, GST_STATE_PLAYING); |
- | <span><font color="#FF0000">}</font></span></tt>
| + | 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 ''create_jpeg()'' ([https://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/maemo-examples/example_camera.c see example_camera.c in maemo-examples]): | + | 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]): |
- | <tt><span>''<span><font color="#9A1900">/* This callback will be registered to the image sink</font></span>''</span>
| + | |
- | <span>''<span><font color="#9A1900"> * after user requests a photo */</font></span>''</span>
| + | |
- | <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>
| + | |
- | <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>
| + | |
- | <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">-></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">-></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">-></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 ([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> |
| | | |
- | <tt><span>''<span><font color="#9A1900">/* Callback to be called when the screen-widget is exposed */</font></span>''</span>
| + | 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]): |
- | <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>
| + | |
- | <span><font color="#FF0000">{</font></span>
| + | <source lang="c"> |
- | <span>''<span><font color="#9A1900">/* Tell the xvimagesink/ximagesink the x-window-id of the screen</font></span>''</span>
| + | /* Callback to be called when the screen-widget is exposed */ |
- | <span>''<span><font color="#9A1900"> * widget in which the video is shown. After this the video</font></span>''</span>
| + | static gboolean expose_cb(GtkWidget * widget, GdkEventExpose * event, gpointer data) |
- | <span>''<span><font color="#9A1900"> * is shown in the correct widget */</font></span>''</span>
| + | { |
- | <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>
| + | /* Tell the xvimagesink/ximagesink the x-window-id of the screen |
- | <span>'''<span><font color="#000000">GDK_WINDOW_XWINDOW</font></span>'''</span><span><font color="#990000">(</font></span>widget<span><font color="#990000">-></font></span>window<span><font color="#990000">));</font></span>
| + | * widget in which the video is shown. After this the video |
- | <span>'''<span><font color="#0000FF">return</font></span>'''</span> FALSE<span><font color="#990000"><nowiki>;</nowiki></font></span>
| + | * is shown in the correct widget */ |
- | <span><font color="#FF0000">}</font></span></tt>
| + | 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]): | | 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="#9A1900">/* Creates a jpeg file from the buffer's raw image data */</font></span>''</span>
| + | |
- | <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>
| + | <source lang="c"> |
- | <span><font color="#FF0000">{</font></span>
| + | /* Creates a jpeg file from the buffer's raw image data */ |
- | <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>
| + | static gboolean create_jpeg(unsigned char *data) |
- | <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>
| + | { |
- | <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>
| + | GdkPixbuf *pixbuf = NULL; |
- | <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>
| + | GError *error = NULL; |
- | <span><font color="#008080">GString</font></span> <span><font color="#990000"><nowiki>*</nowiki></font></span>filename<span><font color="#990000"><nowiki>;</nowiki></font></span>
| + | guint height, width, bpp; |
- | <span><font color="#008080">guint</font></span> base_len<span><font color="#990000">,</font></span> i<span><font color="#990000"><nowiki>;</nowiki></font></span>
| + | const gchar *directory; |
- | <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>
| + | GString *filename; |
- |
| + | guint base_len, i; |
- | 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>
| + | struct stat statbuf; |
- |
| + | |
- | <span>''<span><font color="#9A1900">/* Define the save folder */</font></span>''</span>
| + | width = 640; height = 480; bpp = 24; |
- | 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>
| + | /* Define the save folder */ |
- | <span><font color="#FF0000">{</font></span>
| + | directory = SAVE_FOLDER_DEFAULT; |
- | 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>
| + | if(directory == NULL) |
- | <span><font color="#FF0000">}</font></span>
| + | { |
- |
| + | directory = g_get_tmp_dir(); |
- | <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">-></font></span>len<span><font color="#990000"><nowiki>;</nowiki></font></span>
| + | /* Create an unique file name */ |
- | <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>
| + | filename = g_string_new(g_build_filename(directory, PHOTO_NAME_DEFAULT, NULL)); |
- | <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">-></font></span>str<span><font color="#990000">,</font></span> <span><font color="#990000">&</font></span>statbuf<span><font color="#990000">);</font></span> <span><font color="#990000">++</font></span>i<span><font color="#990000">)</font></span>
| + | base_len = filename->len; |
- | <span><font color="#FF0000">{</font></span>
| + | g_string_append(filename, PHOTO_NAME_SUFFIX_DEFAULT); |
- | <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>
| + | for(i = 1; !stat(filename->str, &statbuf); ++i) |
- | <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>
| + | g_string_truncate(filename, base_len); |
- |
| + | g_string_append_printf(filename, "%d%s", i, PHOTO_NAME_SUFFIX_DEFAULT); |
- | <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>
| + | /* Create a pixbuf object from the data */ |
- | FALSE<span><font color="#990000">,</font></span> <span>''<span><font color="#9A1900">/* No alpha-channel */</font></span>''</span>
| + | pixbuf = gdk_pixbuf_new_from_data(data, |
- | 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>
| + | GDK_COLORSPACE_RGB, /* RGB-colorspace */ |
- | width<span><font color="#990000">,</font></span> height<span><font color="#990000">,</font></span> <span>''<span><font color="#9A1900">/* Dimensions */</font></span>''</span>
| + | FALSE, /* No alpha-channel */ |
- | <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>
| + | bpp/3, /* Bits per RGB-component */ |
- | NULL<span><font color="#990000">,</font></span> NULL<span><font color="#990000">);</font></span> <span>''<span><font color="#9A1900">/* Callbacks */</font></span>''</span>
| + | width, height, /* Dimensions */ |
- |
| + | 3*width, /* Number of bytes between lines (ie stride) */ |
- | <span>''<span><font color="#9A1900">/* Save the pixbuf content's in to a jpeg file and check for</font></span>''</span>
| + | NULL, NULL); /* Callbacks */ |
- | <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">-></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">&</font></span>error<span><font color="#990000">,</font></span> NULL<span><font color="#990000">))</font></span>
| + | /* Save the pixbuf content's in to a jpeg file and check for |
- | <span><font color="#FF0000">{</font></span>
| + | * errors */ |
- | <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">-></font></span>message<span><font color="#990000">);</font></span>
| + | if(!gdk_pixbuf_save(pixbuf, filename->str, "jpeg", &error, NULL)) |
- | <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>
| + | g_warning("%s\n", error->message); |
- | <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>
| + | g_error_free(error); |
- | <span>'''<span><font color="#0000FF">return</font></span>'''</span> FALSE<span><font color="#990000"><nowiki>;</nowiki></font></span>
| + | gdk_pixbuf_unref(pixbuf); |
- | <span><font color="#FF0000">}</font></span>
| + | g_string_free(filename, TRUE); |
- |
| + | return FALSE; |
- | <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>
| + | /* Free allocated resources and return TRUE which means |
- | <span>'''<span><font color="#000000">gdk_pixbuf_unref</font></span>'''</span><span><font color="#990000">(</font></span>pixbuf<span><font color="#990000">);</font></span>
| + | * that the operation was succesful */ |
- | <span>'''<span><font color="#0000FF">return</font></span>'''</span> TRUE<span><font color="#990000"><nowiki>;</nowiki></font></span>
| + | g_string_free(filename, TRUE); |
- | <span><font color="#FF0000">}</font></span></tt>
| + | gdk_pixbuf_unref(pixbuf); |
| + | return TRUE; |
| + | } |
| + | </source> |
| | | |
| [[Category:Development]] | | [[Category:Development]] |
| [[Category:Documentation]] | | [[Category:Documentation]] |
| [[Category:Fremantle]] | | [[Category:Fremantle]] |
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.
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.
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.
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).
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):
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):
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):