Catturare e riprodurre immagini video mediante una WebCam con le funzioni esterne delle API di GStreamer
La risorsa GStreamer consente anche di catturare e riprodurre immagini video mediante una WebCam.
Sarà necessario avere installata nel sistema e richiamare nell'applicazione Gambas la libreria condivisa: "libgstreamer-1.0.so.0.2405.0 ".
Indice
Effettuare un video senza audio
Mostriamo di seguito due esempi di cattura e riproduzione di immagini video senza audio con una webCam in un'applicazione a riga di comando.
Impostazione passo passo della Pipeline con varie funzioni
In questo esempio si costruirà mediante varie funzioni una Pipeline per connettere i vari elementi di GStreamer:
Private webcam As Pointer Library "libgstreamer-1.0:0.2405.0" Private Enum GST_STATE_VOID_PENDING = 0, GST_STATE_NULL, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_PLAYING Private Const GST_FORMAT_TIME As Integer = 3 ' gst_init (int *argc, char **argv[]) ' Initializes the GStreamer library, setting up internal path lists, registering built-in elements, and loading standard plugins. Private Extern gst_init(argc As Pointer, argv As Pointer) ' GstElement* gst_pipeline_new (const gchar *name) ' Create a new pipeline with the given name. Private Extern gst_pipeline_new(name As String) As Pointer ' GstElement * gst_element_factory_make(const gchar *factoryname, Const gchar * name) ' Create a new element of the type defined by the given element factory. Private Extern gst_element_factory_make(factoryname As String, name As String) As Pointer ' void g_object_set(gpointer object, const gchar *first_property_name, ...) ' Sets properties on an object. Private Extern g_object_set(gobject As Pointer, key As String, value As String, element_1 As Pointer) ' void gst_bin_add_many (GstBin *bin, GstElement *element_1, ...) ' Adds a NULL-terminated list of elements to a bin. Private Extern gst_bin_add_many(bin As Pointer, element_1 As Pointer, element_2 As Pointer, element_3 As Pointer, element_4 As Pointer) ' gboolean gst_element_link_many (GstElement *element_1, GstElement *element_2, ...) ' Chain together a series of elements. Private Extern gst_element_link_many(element_1 As Pointer, element_2 As Pointer, element_3 As Pointer, element_4 As Pointer) As Boolean ' GstStateChangeReturn gst_element_set_state(GstElement *element, GstState state) ' Sets the state of the element. Private Extern gst_element_set_state(gstelement As Pointer, state As Integer) As Integer ' gboolean gst_element_query_position (GstElement *element, GstFormat format, gint64 *cur) ' Queries an element for the stream position in nanoseconds. Private Extern gst_element_query_position(gstelement As Pointer, gstformat As Pointer, cur As Pointer) As Boolean ' void gst_object_unref(gpointer object) ' Decrements the reference count on object. Private Extern gst_object_unref(gobject As Pointer) Public Sub Main() Dim pipeline, con, sink As Pointer Dim tempus As Long gst_init(0, 0) pipeline = gst_pipeline_new("webcam") webcam = gst_element_factory_make("v4l2src", "webcam") g_object_set(webcam, "device", "/dev/video0", 0) con = gst_element_factory_make("videoconvert", "convert") sink = gst_element_factory_make("xvimagesink", "image") gst_bin_add_many(pipeline, webcam, con, sink, 0) gst_element_link_many(webcam, con, sink, 0) ' Avviamo la riproduzione video: gst_element_set_state(pipeline, GST_STATE_PLAYING) Do gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus)) Write "\rTempo: " & Time(0, 0, 0, tempus / 1000000) Wait 0.01 Loop End Public Sub Application_Read() Dim s As String Input #File.In, s Select Case s Case "p" ' Pone in pausa la riproduzione del file mediale gst_element_set_state(webcam, GST_STATE_PAUSED) Case "r" ' Riprende la riproduzione del file mediale gst_element_set_state(webcam, GST_STATE_PLAYING) Case "s" ' Arresta la riproduzione del file mediale gst_element_set_state(webcam, GST_STATE_NULL) gst_object_unref(webcam) Print "\nEsecuzione terminata." Quit End Select End
Impostazione della Pipeline con la funzione "gst_parse_launch()"
In quest'altro esempio si costruirà una Pipeline, per connettere i vari elementi di GStreamer, con una linea di comando mediante la funzione esterna gst_parse_launch().
In questo esempio rendremo la cattura delle immagini più veloce e pertanto l'esecuzione del video risulterà essere più fluida:
Private webcam As Pointer Library "libgstreamer-1.0:0.2405.0" Private Enum GST_STATE_VOID_PENDING = 0, GST_STATE_NULL, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_PLAYING Private Const GST_FORMAT_TIME As Integer = 3 ' gst_init (int *argc, char **argv[]) ' Initializes the GStreamer library, setting up internal path lists, registering built-in elements, and loading standard plugins. Private Extern gst_init(argc As Pointer, argv As Pointer) ' GstElement * gst_parse_launch (const gchar *pipeline_description, GError **error) ' Create a new pipeline based on command line syntax. Private Extern gst_parse_launch(description As String, GError As Pointer) As Pointer ' GstStateChangeReturn gst_element_set_state(GstElement *element, GstState state) ' Sets the state of the element. Private Extern gst_element_set_state(gstelement As Pointer, state As Integer) As Integer ' gboolean gst_element_query_position (GstElement *element, GstFormat format, gint64 *cur) ' Queries an element for the stream position in nanoseconds. Private Extern gst_element_query_position(gstelement As Pointer, gstformat As Pointer, cur As Pointer) As Boolean ' void gst_object_unref(gpointer object) ' Decrements the reference count on object. Private Extern gst_object_unref(gobject As Pointer) Public Sub Main() Dim tempus As Long gst_init(0, 0) webcam = gst_parse_launch("v4l2src ! tee name=t_vid ! queue ! videoconvert ! xvimagesink sync=false t_vid. ! queue ! videorate ! video/x-raw-yuv,framerate=30/1", 0) ' Avviamo la riproduzione video: gst_element_set_state(webcam, GST_STATE_PLAYING) Do gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus)) Write #File.Out, "\rTempo: " & Time(0, 0, 0, tempus / 1000000) Wait 0.01 Loop End Public Sub Application_Read() Dim s As String Input #File.In, s Select Case s Case "p" ' Pone in pausa la riproduzione del file mediale gst_element_set_state(webcam, GST_STATE_PAUSED) Case "r" ' Riprende la riproduzione del file mediale gst_element_set_state(webcam, GST_STATE_PLAYING) Case "s" ' Arresta la riproduzione del file mediale gst_element_set_state(webcam, GST_STATE_NULL) gst_object_unref(webcam) Print "\nEsecuzione terminata." Quit End Select End
Altre possibili soluzioni di ripresa video con la funzione "gst_parse_launch()"
Dimensionando la finestra video ad esempio su 640x480:
webcam = gst_parse_launch("v4l2src ! video/x-raw,width=640,height=480,framerate=30/1 ! videoconvert ! xvimagesink", 0)
Aspetto 16/9:
webcam = gst_parse_launch("v4l2src ! video/x-raw,width=640,height=480,framerate=30/1 ! aspectratiocrop aspect-ratio=16/9 ! videoconvert ! xvimagesink", 0)