[gnome-subtitles/gstreamer-sharp-bindings] gstreamer-sharp prototype




commit 56799d55104255072cb955a4b07d783b9975152f
Author: Pedro Castro <pedro gnomesubtitles org>
Date:   Mon Jan 10 22:23:44 2022 +0000

    gstreamer-sharp prototype

 gnome-subtitles.csproj                             |  10 +-
 src/External/GStreamer/GstBackend.cs               | 185 +++++++++++++++++++++
 src/External/GStreamer/GstMediaInfo.cs             |  39 +++++
 .../GstBackend/{GstBackend.cs => GstBackendOld.cs} |  10 +-
 .../{GstMediaInfo.cs => GstMediaInfoOld.cs}        |   6 +-
 src/GnomeSubtitles/Core/Base.cs                    |   6 +-
 src/GnomeSubtitles/Ui/VideoPreview/Player.cs       |   4 +-
 7 files changed, 245 insertions(+), 15 deletions(-)
---
diff --git a/gnome-subtitles.csproj b/gnome-subtitles.csproj
index 4d55860..4164631 100644
--- a/gnome-subtitles.csproj
+++ b/gnome-subtitles.csproj
@@ -54,6 +54,9 @@
     <Reference Include="glib-sharp, Version=3.0.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
       <Package>glib-sharp-3.0</Package>
     </Reference>
+    <Reference Include="gstreamer-sharp">
+      <HintPath>..\gstreamer-sharp\build\sources\gstreamer-sharp.dll</HintPath>
+    </Reference>
   </ItemGroup>
   <ItemGroup>
     <EmbeddedResource Include="src\Glade\MainWindow.ui">
@@ -275,10 +278,12 @@
     <Compile Include="src\GnomeSubtitles\Ui\WindowState.cs" />
     <Compile Include="src\External\Enchant\Enchant.cs" />
     <Compile Include="src\External\Interop\Interop.cs" />
-    <Compile Include="src\External\GstBackend\GstBackend.cs" />
-    <Compile Include="src\External\GstBackend\GstMediaInfo.cs" />
+    <Compile Include="src\External\GstBackend\GstBackendOld.cs" />
+    <Compile Include="src\External\GstBackend\GstMediaInfoOld.cs" />
     <Compile Include="src\GnomeSubtitles\Ui\VideoPreview\MediaBackend.cs" />
     <Compile Include="src\GnomeSubtitles\Dialog\Message\DialogUtil.cs" />
+    <Compile Include="src\External\GStreamer\GstBackend.cs" />
+    <Compile Include="src\External\GStreamer\GstMediaInfo.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup>
@@ -287,6 +292,7 @@
     <Folder Include="src\External\GtkSpell\" />
     <Folder Include="src\External\Enchant\" />
     <Folder Include="src\External\Interop\" />
+    <Folder Include="src\External\GStreamer\" />
   </ItemGroup>
   <ItemGroup>
     <None Include="data\gnome-subtitles.svg">
diff --git a/src/External/GStreamer/GstBackend.cs b/src/External/GStreamer/GstBackend.cs
new file mode 100644
index 0000000..5e7f6dc
--- /dev/null
+++ b/src/External/GStreamer/GstBackend.cs
@@ -0,0 +1,185 @@
+/*
+ * This file is part of Gnome Subtitles.
+ * Copyright (C) 2022 Pedro Castro
+ *
+ * Gnome Subtitles is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Gnome Subtitles is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+using GnomeSubtitles.Core;
+using GnomeSubtitles.Ui.VideoPreview;
+using Gst;
+using Mono.Unix;
+using SubLib.Util;
+using System;
+using System.Runtime.InteropServices;
+
+namespace External.GStreamer {
+
+       public class GstBackend : MediaBackend {
+
+               private Element playbin = null;
+               private Element gtkSink = null;
+               private GstMediaInfo mediaInfo = null;
+
+               /* Enums */
+               private enum ErrorType { GStreamer, NoMediaInfo, NoVideoOrAudio };
+
+               /* Error messages */
+               private readonly string ErrorMessageNoMediaInfo = Catalog.GetString("Unable to obtain the 
complete media information.");
+
+               public override string Name => throw new NotImplementedException();
+
+               public override long CurrentPosition => throw new NotImplementedException();
+
+               public override long Duration => throw new NotImplementedException();
+
+               public override bool HasVideo => throw new NotImplementedException();
+
+               public override float AspectRatio => throw new NotImplementedException();
+
+               public override float FrameRate => throw new NotImplementedException();
+
+               public override bool HasAudio => throw new NotImplementedException();
+
+               public override Gtk.Widget CreateVideoWidget () {
+                       throw new NotImplementedException();
+               }
+
+               public override void Dispose () {
+                       throw new NotImplementedException();
+               }
+
+               public override void Initialize () {
+                       //Disable LIBVA by default as it's causing lots of problems. This is done by setting 
an invalid driver name.
+                       if (!"FALSE".Equals(Environment.GetEnvironmentVariable("GS_DISABLE_LIBVA"))) {
+                               Logger.Info("[GstBackend] Disabling libva");
+                               Environment.SetEnvironmentVariable("LIBVA_DRIVER_NAME", "GNOME_SUBTITLES");
+                       }
+
+                       Gst.Application.Init();
+
+                       playbin = ElementFactory.Make("playbin", "playbin");
+                       gtkSink = ElementFactory.Make("gtksink", "gtksink");
+
+                       playbin.Data ["video-sink"] = gtkSink; //FIXME funciona?
+                                                                                                  
//g_object_set(G_OBJECT(backend->element), "video-sink", backend->video_element, NULL);
+
+                       playbin.Bus.AddSignalWatch();
+                       playbin.Bus.Message += OnGstMessage;
+               }
+
+               public override bool Load (string uri) {
+                       throw new NotImplementedException();
+               }
+
+               public override void Pause () {
+                       throw new NotImplementedException();
+               }
+
+               public override void Play () {
+                       throw new NotImplementedException();
+               }
+
+               public override void Seek (long time, bool isAbsolute) {
+                       throw new NotImplementedException();
+               }
+
+               public override void SetSpeed (float speed) {
+                       throw new NotImplementedException();
+               }
+
+               public override void Unload () {
+                       throw new NotImplementedException();
+               }
+
+               /* Private members */
+
+               private void OnGstMessage (object o, MessageArgs args) {
+
+                       switch (args.Message.Type) {
+
+                               /* ASYNC_DONE can be emitted many times (example: when returning back from
+                                * pause to play). If the info is already loaded, don't do anything.
+                                */
+                               case MessageType.AsyncDone:
+                                       if (mediaInfo == null) {
+                                               LoadMediaInfo();
+                                       }
+                                       break;
+
+                               /*case GST_MESSAGE_STATE_CHANGED: {
+                                       GstState old_state, new_state, pending_state;
+                                       gst_message_parse_state_changed(message, &old_state, &new_state, 
&pending_state);
+                                       g_print("on_gst_message: STATE CHANGED: old=%s, new(current)=%s, 
pending(target)=%s\n",
+                                               gst_element_state_get_name(old_state), 
gst_element_state_get_name(new_state), gst_element_state_get_name(pending_state));
+                                       break;
+                               }*/
+
+                               case MessageType.Error:
+                                       GLib.GException gerror;
+                                       string debug;
+                                       args.Message.ParseError(out gerror, out debug);
+
+                                       Logger.Error("[GstBackend] Gst error: '{0}'; debug message: '{1}'", 
gerror.Message, debug);
+                                       TriggerErrorFound(gerror.Message);
+                                       break;
+
+                               // Playback finished
+                               case MessageType.Eos:
+                                       TriggerEndOfStreamReached();
+                                       break;
+                       }
+               }
+
+               private void LoadMediaInfo () {
+                       mediaInfo = new GstMediaInfo();
+
+                       /* Check for video and audio */
+                       mediaInfo.HasVideo = CheckIfMediaHasVideo();
+                       mediaInfo.HasAudio = CheckIfMediaHasAudio();
+
+                       if (!mediaInfo.HasVideo && !mediaInfo.HasAudio) {
+                               TriggerErrorFound(Catalog.GetString("The file contains no video or audio."));
+                               return;
+                       }
+
+                       if (mediaInfo.HasVideo) {
+                               LoadVideoInfo();
+                               //load_media_info_video(backend);
+                       }
+
+                       LoadMediaDuration();
+                       //load_media_info_duration(backend);
+               }
+               
+               private bool CheckIfMediaHasVideo() {
+                       throw new NotImplementedException();
+               }
+               
+               private bool CheckIfMediaHasAudio() {
+                       throw new NotImplementedException();
+               }
+               
+               private bool LoadVideoInfo() {
+                       throw new NotImplementedException();
+               }
+               
+               private bool LoadMediaDuration() {
+                       throw new NotImplementedException();
+               }
+
+       }
+
+}
\ No newline at end of file
diff --git a/src/External/GStreamer/GstMediaInfo.cs b/src/External/GStreamer/GstMediaInfo.cs
new file mode 100644
index 0000000..7225a3e
--- /dev/null
+++ b/src/External/GStreamer/GstMediaInfo.cs
@@ -0,0 +1,39 @@
+/*
+ * This file is part of Gnome Subtitles.
+ * Copyright (C) 2022 Pedro Castro
+ *
+ * Gnome Subtitles is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Gnome Subtitles is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+using System;
+using System.Runtime.InteropServices;
+
+namespace External.GStreamer {
+
+public class GstMediaInfo {
+       
+       public long Duration { get; set; }
+       
+       public bool HasVideo { get; set; }
+       public int Width { get; set; }
+       public int Height { get; set; }
+       public float AspectRatio { get; set; }
+       public float FrameRate { get; set; }
+
+       public bool HasAudio { get; set; }
+
+}
+
+}
diff --git a/src/External/GstBackend/GstBackend.cs b/src/External/GstBackend/GstBackendOld.cs
similarity index 96%
rename from src/External/GstBackend/GstBackend.cs
rename to src/External/GstBackend/GstBackendOld.cs
index c084f8a..1b5708c 100644
--- a/src/External/GstBackend/GstBackend.cs
+++ b/src/External/GstBackend/GstBackendOld.cs
@@ -42,18 +42,18 @@ using System.Runtime.InteropServices;
  *   - https://developer.gnome.org/totem/stable/BaconVideoWidget.html (Totem bacon video widget)
  */
 
-namespace External.GStreamer {
+namespace External.GstBackendOld {
 
        /* Enums */
        
-       public class GstBackend : MediaBackend {
+       public class GstBackendOld : MediaBackend {
                private HandleRef backend;
-               private GstMediaInfo mediaInfo = null;
+               private GstMediaInfoOld mediaInfo = null;
                private float speed = 1f; //Keeping it here as we need to pass it to every 'seek' call
 
                /* Delegates */
                private delegate void ErrorFoundHandler(ErrorType type, string errorMessage, string 
debugMessage);
-               private delegate void LoadCompleteHandler(GstMediaInfo mediaInfo);
+               private delegate void LoadCompleteHandler(GstMediaInfoOld mediaInfo);
 
                /* Enums */
                private enum ErrorType { GStreamer, NoMediaInfo, NoVideoOrAudio};
@@ -161,7 +161,7 @@ namespace External.GStreamer {
        
                /* Event handlers */
        
-               private void OnLoadComplete(GstMediaInfo mediaInfo) {
+               private void OnLoadComplete(GstMediaInfoOld mediaInfo) {
                        this.mediaInfo = mediaInfo;
                        SetStatus(MediaStatus.Loaded);
                }
diff --git a/src/External/GstBackend/GstMediaInfo.cs b/src/External/GstBackend/GstMediaInfoOld.cs
similarity index 93%
rename from src/External/GstBackend/GstMediaInfo.cs
rename to src/External/GstBackend/GstMediaInfoOld.cs
index e7022a1..20b6665 100644
--- a/src/External/GstBackend/GstMediaInfo.cs
+++ b/src/External/GstBackend/GstMediaInfoOld.cs
@@ -20,10 +20,10 @@
 using System;
 using System.Runtime.InteropServices;
 
-namespace External.GStreamer {
+namespace External.GstBackendOld {
 
 [StructLayout(LayoutKind.Sequential)]
-public class GstMediaInfo {
+public class GstMediaInfoOld {
        long duration;
        
        bool has_video;
@@ -34,7 +34,7 @@ public class GstMediaInfo {
        
        bool has_audio;
 
-       public GstMediaInfo(IntPtr ptr) {
+       public GstMediaInfoOld(IntPtr ptr) {
                if (ptr != IntPtr.Zero) {
                        Marshal.PtrToStructure(ptr, this);
                }
diff --git a/src/GnomeSubtitles/Core/Base.cs b/src/GnomeSubtitles/Core/Base.cs
index 5277bf8..eb9874d 100644
--- a/src/GnomeSubtitles/Core/Base.cs
+++ b/src/GnomeSubtitles/Core/Base.cs
@@ -225,9 +225,9 @@ public static class Base {
        public static void Open (string path, Encoding encoding, Uri videoUri) {
                OpenDocument(path, encoding);
                
-               //OpenVideo(videoUri); //FIXME
-               videoUriToOpenWithTimeout = videoUri; //FIXME
-               GLib.Timeout.Add(1000, OpenVideoWithTimeout); //FIXME
+               OpenVideo(videoUri); //FIXME
+               //videoUriToOpenWithTimeout = videoUri; //FIXME
+               //GLib.Timeout.Add(1000, OpenVideoWithTimeout); //FIXME
        }
        
        /* Nasty hack while there isn't a proper fix for #184 */
diff --git a/src/GnomeSubtitles/Ui/VideoPreview/Player.cs b/src/GnomeSubtitles/Ui/VideoPreview/Player.cs
index 08d3e29..86e0d61 100644
--- a/src/GnomeSubtitles/Ui/VideoPreview/Player.cs
+++ b/src/GnomeSubtitles/Ui/VideoPreview/Player.cs
@@ -17,7 +17,7 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-using External.GStreamer;
+using External.GstBackendOld;
 using GnomeSubtitles.Core;
 using Gtk;
 using SubLib.Util;
@@ -174,7 +174,7 @@ public class Player {
        /* Private members */
 
        private MediaBackend InitializeBackend() {
-               MediaBackend backend = new GstBackend();
+               MediaBackend backend = new GstBackendOld();
                backend.Initialize();
                
                Widget videoWidget = backend.CreateVideoWidget();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]