From 1edc21215e4829f9e6cc3d76c6c9d1470c43317a Mon Sep 17 00:00:00 2001
From: offbyone <bborgsdorf@gmail.com>
Date: Mon, 1 Aug 2011 19:55:04 +0200
Subject: [PATCH] rework configurable mimetype to player association

---
 .../UI/Players/VideoPlayer/PlayerRegistration.cs   |    7 +-
 .../UI/Players/VideoPlayer/VideoPlayerBuilder.cs   |  104 ++++++++++++++++++++
 .../Source/UI/Players/VideoPlayer/plugin.xml       |    8 ++
 3 files changed, 116 insertions(+), 3 deletions(-)

diff --git a/MediaPortal/Source/UI/Players/VideoPlayer/PlayerRegistration.cs b/MediaPortal/Source/UI/Players/VideoPlayer/PlayerRegistration.cs
index 09b4d79..75c2b44 100644
--- a/MediaPortal/Source/UI/Players/VideoPlayer/PlayerRegistration.cs
+++ b/MediaPortal/Source/UI/Players/VideoPlayer/PlayerRegistration.cs
@@ -52,9 +52,10 @@ namespace MediaPortal.UI.Players.Video
       EXTENSIONS2PLAYER.Add(".ifo", typeof(DvdPlayer));
       // TODO: Go on with extensions mapping
 
-      MIMETYPES2PLAYER.Add("video/x-ms-wmv", typeof(VideoPlayer));
-      MIMETYPES2PLAYER.Add("video/mp2t", typeof(TsVideoPlayer));
-      MIMETYPES2PLAYER.Add("video/dvd", typeof(DvdPlayer));
+      // mimetypes are now mapped in plugin.xml
+      //MIMETYPES2PLAYER.Add("video/x-ms-wmv", typeof(VideoPlayer));
+      //MIMETYPES2PLAYER.Add("video/mp2t", typeof(TsVideoPlayer));
+      //MIMETYPES2PLAYER.Add("video/dvd", typeof(DvdPlayer));
       // TODO: Go on with mime types mapping
     }
 
diff --git a/MediaPortal/Source/UI/Players/VideoPlayer/VideoPlayerBuilder.cs b/MediaPortal/Source/UI/Players/VideoPlayer/VideoPlayerBuilder.cs
index 912d194..82165af 100644
--- a/MediaPortal/Source/UI/Players/VideoPlayer/VideoPlayerBuilder.cs
+++ b/MediaPortal/Source/UI/Players/VideoPlayer/VideoPlayerBuilder.cs
@@ -28,14 +28,118 @@ using MediaPortal.Core.Logging;
 using MediaPortal.Core.MediaManagement.ResourceAccess;
 using MediaPortal.UI.Players.Video.Interfaces;
 using MediaPortal.UI.Presentation.Players;
+using MediaPortal.Core.PluginManager;
+using MediaPortal.Core.Services.PluginManager.Builders;
 
 namespace MediaPortal.UI.Players.Video
 {
+  public class VideoPlayerMimeTypeMappingBuilder : IPluginItemBuilder
+  {
+    #region IPluginItemBuilder Member
+
+    public object BuildItem(PluginItemMetadata itemData, PluginRuntime plugin)
+    {
+      BuilderHelper.CheckParameter("ClassName", itemData);
+      BuilderHelper.CheckParameter("MimeType", itemData);
+      return new VideoPlayerMimeTypeMapping(plugin.GetPluginType(itemData.Attributes["ClassName"]), itemData.Attributes["MimeType"]);
+    }
+
+    public void RevokeItem(object item, PluginItemMetadata itemData, PluginRuntime plugin)
+    {
+
+    }
+
+    public bool NeedsPluginActive(PluginItemMetadata itemData, PluginRuntime plugin)
+    {
+      return true;
+    }
+
+    #endregion
+  }
+
+  public class VideoPlayerMimeTypeMapping
+  {
+    public Type ClassName { get; private set; }
+    public string MimeType { get; private set; }
+
+    public VideoPlayerMimeTypeMapping(Type className, string mimetype)
+    {
+      ClassName = className;
+      MimeType = mimetype;
+    }
+  }
+
   /// <summary>
   /// Player builder for all video players of the VideoPlayers plugin.
   /// </summary>
   public class VideoPlayerBuilder : IPlayerBuilder
   {
+    #region Classes
+
+    protected class VideoPlayerBuilderPluginItemStateTracker : IPluginItemStateTracker
+    {
+      public string UsageDescription
+      {
+        get { return ""; }
+      }
+
+      public bool RequestEnd(PluginItemRegistration itemRegistration)
+      {
+        return false;
+      }
+
+      public void Stop(PluginItemRegistration itemRegistration)
+      {
+        // Nothing to do
+      }
+
+      public void Continue(PluginItemRegistration itemRegistration)
+      {
+        // Nothing to do
+      }
+    }
+
+    #endregion
+
+    #region Consts
+
+    /// <summary>
+    /// registration path where mappings of mimetypes to a playerbuilder are registered
+    /// </summary>
+    public const string VIDEOPLAYERBUILDEROVERRIDEMIMETYPES_REGISTRATION_PATH = "/VideoPlayers/OverrideMimeTypes";
+
+    #endregion
+
+    #region Protected fields
+
+    protected VideoPlayerBuilderPluginItemStateTracker _videoPlayerBuilderPluginItemStateTracker;
+
+    #endregion
+
+    #region Ctor
+    
+    public VideoPlayerBuilder()
+    {
+      _videoPlayerBuilderPluginItemStateTracker = new VideoPlayerBuilderPluginItemStateTracker();
+
+      IPluginManager pluginManager = ServiceRegistration.Get<IPluginManager>();
+      foreach (PluginItemMetadata itemMetadata in pluginManager.GetAllPluginItemMetadata(VIDEOPLAYERBUILDEROVERRIDEMIMETYPES_REGISTRATION_PATH))
+      {
+        VideoPlayerMimeTypeMapping playerMapping = pluginManager.RequestPluginItem<VideoPlayerMimeTypeMapping>(VIDEOPLAYERBUILDEROVERRIDEMIMETYPES_REGISTRATION_PATH,
+                itemMetadata.Id, _videoPlayerBuilderPluginItemStateTracker);
+        if (playerMapping == null)
+        {
+          ServiceRegistration.Get<ILogger>().Warn("Could not instantiate VideoPlayerMimeTypeMapping with id '{0}'", itemMetadata.Id);
+        }
+        else
+        {
+          PlayerRegistration.MIMETYPES2PLAYER[playerMapping.MimeType] = playerMapping.ClassName;
+        }
+      }
+    }
+    
+    #endregion
+
     #region IPlayerBuilder implementation
 
     public IPlayer GetPlayer(IResourceLocator locator, string mimeType)
diff --git a/MediaPortal/Source/UI/Players/VideoPlayer/plugin.xml b/MediaPortal/Source/UI/Players/VideoPlayer/plugin.xml
index 21257b4..8ee483d 100644
--- a/MediaPortal/Source/UI/Players/VideoPlayer/plugin.xml
+++ b/MediaPortal/Source/UI/Players/VideoPlayer/plugin.xml
@@ -21,6 +21,14 @@
     <PlayerBuilder Id="47924D05-C8B6-4a75-8EFE-8A9CD73AA518" ClassName="MediaPortal.UI.Players.Video.VideoPlayerBuilder"/>
   </Register>
 
+  <Builder Name="VideoPlayerMimeTypeMapping" ClassName="MediaPortal.UI.Players.Video.VideoPlayerMimeTypeMappingBuilder"/>
+
+  <Register Location="/VideoPlayers/OverrideMimeTypes">
+    <VideoPlayerMimeTypeMapping Id="wmv" ClassName="MediaPortal.UI.Players.Video.VideoPlayer" MimeType="video/x-ms-wmv"/>
+    <VideoPlayerMimeTypeMapping Id="ts" ClassName="MediaPortal.UI.Players.Video.TsVideoPlayer" MimeType="video/mp2t"/>
+    <VideoPlayerMimeTypeMapping Id="dvd" ClassName="MediaPortal.UI.Players.Video.DvdPlayer" MimeType="video/dvd"/>
+  </Register>
+
   <Register Location="/Resources/Language">
     <Resource Id="VideoPlayersLanguage" Directory="Language" Type="Language"/>
   </Register>
-- 
1.7.4.msysgit.0

