[banshee] GStreamerSharp: Initialize the media profiles backend



commit 5a6633960fbd3181b02d2bb9392c0c5d6716da20
Author: Bertrand Lorentz <bertrand lorentz gmail com>
Date:   Sun Jan 5 15:40:04 2014 +0100

    GStreamerSharp: Initialize the media profiles backend
    
    The new MediaProfileBackend class implements the various functions used
    in the audio profiles definitions, and set everything up so that they
    are ready to be used for encoding and transcoding.
    
    This code is mostly copied from Service.cs in the native GStreamer
    backend, which was doing this as part of the main GStreamer
    initialization.
    
    It is GStreamer-specific, and only a small part is specific to
    gstreamer-sharp, so I didn't see a better way than duplicating that
    code.

 .../Banshee.GStreamerSharp.csproj                  |    1 +
 .../Banshee.GStreamerSharp/MediaProfileBackend.cs  |  179 ++++++++++++++++++++
 .../Banshee.GStreamerSharp/PlayerEngine.cs         |    5 +
 src/Backends/Banshee.GStreamerSharp/Makefile.am    |    1 +
 4 files changed, 186 insertions(+), 0 deletions(-)
---
diff --git a/src/Backends/Banshee.GStreamerSharp/Banshee.GStreamerSharp.csproj 
b/src/Backends/Banshee.GStreamerSharp/Banshee.GStreamerSharp.csproj
index 96794ee..269808e 100644
--- a/src/Backends/Banshee.GStreamerSharp/Banshee.GStreamerSharp.csproj
+++ b/src/Backends/Banshee.GStreamerSharp/Banshee.GStreamerSharp.csproj
@@ -76,6 +76,7 @@
     <Compile Include="Banshee.GStreamerSharp\Visualization.cs" />
     <Compile Include="Banshee.GStreamerSharp\DvdManager.cs" />
     <Compile Include="Banshee.GStreamerSharp\GstExtensions.cs" />
+    <Compile Include="Banshee.GStreamerSharp\MediaProfileBackend.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup>
diff --git a/src/Backends/Banshee.GStreamerSharp/Banshee.GStreamerSharp/MediaProfileBackend.cs 
b/src/Backends/Banshee.GStreamerSharp/Banshee.GStreamerSharp/MediaProfileBackend.cs
new file mode 100644
index 0000000..bc7b66f
--- /dev/null
+++ b/src/Backends/Banshee.GStreamerSharp/Banshee.GStreamerSharp/MediaProfileBackend.cs
@@ -0,0 +1,179 @@
+//
+// MediaProfileBackend.cs
+//
+// Authors:
+//   Aaron Bockover <abockover novell com>
+//   Bertrand Lorentz <bertrand lorentz gmail com>
+//
+// Copyright 2006-2010 Novell, Inc.
+// Copyright 2014 Bertrand Lorentz
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+using Hyena;
+using Hyena.SExpEngine;
+using Banshee.MediaProfiles;
+using Banshee.ServiceStack;
+
+namespace Banshee.GStreamerSharp
+{
+    public static class MediaProfileBackend
+    {
+        public static void OnMediaProfileManagerInitialized (object o, EventArgs args)
+        {
+            MediaProfileManager profile_manager = ServiceManager.MediaProfileManager;
+            if (profile_manager != null) {
+                Pipeline.AddSExprFunction ("gst-element-is-available", SExprTestElement);
+                Pipeline.AddSExprFunction ("gst-construct-pipeline", SExprConstructPipeline);
+                Pipeline.AddSExprFunction ("gst-construct-caps", SExprConstructCaps);
+                Pipeline.AddSExprFunction ("gst-construct-element", SExprConstructElement);
+
+                profile_manager.TestProfile += OnTestMediaProfile;
+                profile_manager.TestAll ();
+            }
+        }
+
+        private static void OnTestMediaProfile (object o, TestProfileArgs args)
+        {
+            bool no_test = ApplicationContext.EnvironmentIsSet ("BANSHEE_PROFILES_NO_TEST");
+            bool available = false;
+
+            foreach (Pipeline.Process process in args.Profile.Pipeline.GetPendingProcessesById 
("gstreamer")) {
+                string pipeline = args.Profile.Pipeline.CompileProcess (process);
+                if (no_test || TestPipeline (pipeline)) {
+                    args.Profile.Pipeline.AddProcess (process);
+                    available = true;
+                    break;
+                } else if (!no_test) {
+                    Hyena.Log.DebugFormat ("GStreamer pipeline does not run: {0}", pipeline);
+                }
+            }
+
+            args.ProfileAvailable = available;
+        }
+
+        internal static bool TestPipeline (string pipeline)
+        {
+            if (String.IsNullOrEmpty (pipeline)) {
+                return false;
+            }
+
+            try {
+                Gst.Parse.Launch (pipeline);
+            } catch (GLib.GException) {
+                return false;
+            }
+            return true;
+        }
+
+        private static TreeNode SExprTestElement (EvaluatorBase evaluator, TreeNode [] args)
+        {
+            if (args.Length != 1) {
+                throw new ArgumentException ("gst-test-element accepts one argument");
+            }
+
+            TreeNode arg = evaluator.Evaluate (args[0]);
+            if (!(arg is StringLiteral)) {
+                throw new ArgumentException ("gst-test-element requires a string argument");
+            }
+
+            StringLiteral element_node = (StringLiteral)arg;
+            return new BooleanLiteral (TestPipeline (element_node.Value));
+        }
+
+        private static TreeNode SExprConstructPipeline (EvaluatorBase evaluator, TreeNode [] args)
+        {
+            StringBuilder builder = new StringBuilder ();
+            List<string> elements = new List<string> ();
+
+            for (int i = 0; i < args.Length; i++) {
+                TreeNode node = evaluator.Evaluate (args[i]);
+                if (!(node is LiteralNodeBase)) {
+                    throw new ArgumentException ("node must evaluate to a literal");
+                }
+
+                string value = node.ToString ().Trim ();
+
+                if (value.Length == 0) {
+                    continue;
+                }
+
+                elements.Add (value);
+            }
+
+            for (int i = 0; i < elements.Count; i++) {
+                builder.Append (elements[i]);
+                if (i < elements.Count - 1) {
+                    builder.Append (" ! ");
+                }
+            }
+
+            return new StringLiteral (builder.ToString ());
+        }
+
+        private static TreeNode SExprConstructElement (EvaluatorBase evaluator, TreeNode [] args)
+        {
+            return SExprConstructPipelinePart (evaluator, args, true);
+        }
+
+        private static TreeNode SExprConstructCaps (EvaluatorBase evaluator, TreeNode [] args)
+        {
+            return SExprConstructPipelinePart (evaluator, args, false);
+        }
+
+        private static TreeNode SExprConstructPipelinePart (EvaluatorBase evaluator, TreeNode [] args, bool 
element)
+        {
+            StringBuilder builder = new StringBuilder ();
+
+            TreeNode list = new TreeNode ();
+            foreach (TreeNode arg in args) {
+                list.AddChild (evaluator.Evaluate (arg));
+            }
+
+            list = list.Flatten ();
+
+            for (int i = 0; i < list.ChildCount; i++) {
+                TreeNode node = list.Children[i];
+
+                string value = node.ToString ().Trim ();
+
+                builder.Append (value);
+
+                if (i == 0) {
+                    if (list.ChildCount > 1) {
+                        builder.Append (element ? ' ' : ',');
+                    }
+
+                    continue;
+                } else if (i % 2 == 1) {
+                    builder.Append ('=');
+                } else if (i < list.ChildCount - 1) {
+                    builder.Append (element ? ' ' : ',');
+                }
+            }
+
+            return new StringLiteral (builder.ToString ());
+        }
+    }
+}
+
diff --git a/src/Backends/Banshee.GStreamerSharp/Banshee.GStreamerSharp/PlayerEngine.cs 
b/src/Backends/Banshee.GStreamerSharp/Banshee.GStreamerSharp/PlayerEngine.cs
index 5780a98..78c51dc 100644
--- a/src/Backends/Banshee.GStreamerSharp/Banshee.GStreamerSharp/PlayerEngine.cs
+++ b/src/Backends/Banshee.GStreamerSharp/Banshee.GStreamerSharp/PlayerEngine.cs
@@ -320,6 +320,11 @@ namespace Banshee.GStreamerSharp
             }
 
             Gst.Application.Init ();
+
+            var profile_manager = ServiceManager.MediaProfileManager;
+            if (profile_manager != null) {
+                profile_manager.Initialized += MediaProfileBackend.OnMediaProfileManagerInitialized;
+            }
         }
 
         protected override bool DelayedInitialize { get { return true; } }
diff --git a/src/Backends/Banshee.GStreamerSharp/Makefile.am b/src/Backends/Banshee.GStreamerSharp/Makefile.am
index 76257b0..e98cfae 100644
--- a/src/Backends/Banshee.GStreamerSharp/Makefile.am
+++ b/src/Backends/Banshee.GStreamerSharp/Makefile.am
@@ -9,6 +9,7 @@ SOURCES =  \
        Banshee.GStreamerSharp/CddaManager.cs \
        Banshee.GStreamerSharp/DvdManager.cs \
        Banshee.GStreamerSharp/GstExtensions.cs \
+       Banshee.GStreamerSharp/MediaProfileBackend.cs \
        Banshee.GStreamerSharp/PlayerEngine.cs \
        Banshee.GStreamerSharp/Transcoder.cs \
        Banshee.GStreamerSharp/VideoManager.cs \


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