Gestione dei file video con le funzioni esterne delle API di GStreamer

Da Gambas-it.org - Wikipedia.

Le funzioni esterne dell'API di GStreamer consentono due modalità per riprodurre un file video:

  • all'interno di una finestra distinta dal Form principale;
  • all'interno di un oggetto capace di supportare il video e in generale le immagini, posto sul Form principale.

Facciamo riferimento, per i casi e gli esempi di codice che seguono, alla pagina della WIKI, nelle quali è stata descritta la gestione del sonoro in GStreamer. Lì si potrà avere contezza anche delle funzioni esterne necessarie di GStreamer dichiarate ed utilizzate.


Riproduzione del video all'interno di una finestra distinta da Form principale

Per ottenere la riproduzione di un file video all'interno di una finestra distinta da Form principale, e generata automaticamente da GStreamer, sarà sufficiente utilizzare la funzione esterna:

GstElement * gst_element_factory_make(const gchar *factoryname, const gchar *name)

alla quale si passerà il nome dell'Elemento, denominato "playbin" ed una denominazione per l'Elemento appena creato. È possibile passare NULL come argomento name per ottenere un unico nome predefinito.


Mostriamo di seguito un semplice esempio per riprodurre un file video all'interno di una finestra generata da GStreamer distinta dal Form di Gambas:

Library "libgstreamer-1.0"

Private Enum GST_STATE_VOID_PENDING = 0, GST_STATE_NULL, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_PLAYING

' 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_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, tertium As String)

' 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


Public Sub Button1_Click()

 Dim play As Pointer
   
  gst_init(0, 0)

  play = gst_element_factory_make("playbin", "play")

  g_object_set(play, "uri", "file:///percorso/del/file/video", Null)

' Avviamo la riproduzione audio:
  gst_element_set_state(play, GST_STATE_PLAYING)
    
End


Riproduzione di un file video all'interno di una finestra generata da GStreamer con applicazione a riga di comando

Di seguito mostriamo un esempio simile al precedente, ma con applicazione a riga di comando:

Private video As Pointer


Library "libgstreamer-1.0"

Private Enum GST_STATE_VOID_PENDING = 0, GST_STATE_NULL, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_PLAYING

' 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_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, tertium As String)

' 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_duration (GstElement *element, GstFormat format, gint64 *duration)
' Queries an element for the total stream duration in nanoseconds.
Private Extern gst_element_query_duration(gstelement As Pointer, gstformat As Pointer, duration As Pointer) As Boolean

' 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 Button1_Click()
      
  gst_init(0, 0)
  
  video = gst_element_factory_make("playbin", "video")
  
  g_object_set(video, "uri", "file:///percorso/del/file/video", Null)
  
' Avviamo la riproduzione audio:
  gst_element_set_state(video, GST_STATE_PLAYING)
  
  While (posizione = 0) Or (durata > posizione)
    gst_element_query_duration(video, GST_FORMAT_TIME, VarPtr(durata))
    gst_element_query_position(video, GST_FORMAT_TIME, VarPtr(posizione))
    Write #File.Out, "\rDurata: " & Date(0, 0, 0, 0, 0, 0, durata / 1000000) & "      Pos. " & Date(0, 0, 0, 0, 0, 0, posizione / 1000000)
  Wend
  
  gst_object_unref(video)
  
  Print "\nEsecuzione terminata."
    
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(video, GST_STATE_PAUSED)
   Case "r"
     gst_element_set_state(video, GST_STATE_PLAYING)       ' Riprende la riproduzione del file mediale
   Case "s"
     stato = gst_element_set_state(video, GST_STATE_NULL)  ' Arresta la riproduzione del file mediale
 End Select

End


Mostrare il video sul Form

Per mostrare il video sul Form dell'applicazione Gambas principale (e dunque non, come nel caso precedente, in una finestra distinta) si dovrà porre sul Form un oggetto capace di supportare il video (ad esempio in Gambas una DrawingArea) e la seguente funzione esterna:

void gst_video_overlay_set_window_handle(GstVideoOverlay *overlay, guintptr handle)

la quale, però è attualmente contenuta nella libreria libgstvideo-1.0. Il parametro overlay è un pointer che rappresenta l'Elemento sonoro, mentre il parametro handle è in intero che rappresenta il numero identificativo (Id) dell'oggetto, posto sul Form, nel quale si intende mostrare il video. In Gambas, come già accennato, l'oggetto potrà essere una DrawingArea.


Mostriamo un semplice esempio:

Library "libgstreamer-1.0"

Private Enum GST_STATE_VOID_PENDING = 0, GST_STATE_NULL, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_PLAYING

' 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_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, tertium As String)

' void gst_video_overlay_set_window_handle (GstVideoOverlay *overlay, guintptr handle)
' Calls the video overlay's set_window_handle method.
Private Extern gst_video_overlay_set_window_handle(overlay As Pointer, id As Integer)

' 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
 

Public Sub Button1_Click()

 Dim play As Pointer

  gst_init(0, 0)

  play = gst_element_factory_make("playbin", "play")

  g_object_set(play, "uri", "file:///percorso/del/file/video", Null)

  gst_video_overlay_set_window_handle(play, DrawingArea1.Handle)

' Avviamo la riproduzione audio:
  gst_element_set_state(play, GST_STATE_PLAYING)
    
End