From 254905fc38a1aa416d3299b687959f7ead69c682 Mon Sep 17 00:00:00 2001
From: sebastiii <sebastiii@team-mediaportal.com>
Date: Mon, 9 Sep 2013 13:23:37 +0200
Subject: [PATCH] IPTV Rework (to avoid loading multiple instance of
 MediaPortal IPTV Filter.

---
 .../Implementations/DVB/Graphs/DVBIP/TvCardDVBIP.cs    |  7 +++----
 .../DVB/Graphs/DVBIP/TvCardDVBIPBuiltIn.cs             | 18 ++++++++++++++++--
 .../TVLibrary/TVLibrary/Implementations/TvCardBase.cs  |  8 ++++----
 3 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/TvEngine3/TVLibrary/TVLibrary/Implementations/DVB/Graphs/DVBIP/TvCardDVBIP.cs b/TvEngine3/TVLibrary/TVLibrary/Implementations/DVB/Graphs/DVBIP/TvCardDVBIP.cs
index 47b1d8b..bd92b9e 100644
--- a/TvEngine3/TVLibrary/TVLibrary/Implementations/DVB/Graphs/DVBIP/TvCardDVBIP.cs
+++ b/TvEngine3/TVLibrary/TVLibrary/Implementations/DVB/Graphs/DVBIP/TvCardDVBIP.cs
@@ -66,7 +66,7 @@ namespace TvLibrary.Implementations.DVB
     /// <summary>
     /// Build graph
     /// </summary>
-    public override void BuildGraph()
+    public void BuildGraph(string url)
     {
       try
       {
@@ -91,7 +91,7 @@ namespace TvLibrary.Implementations.DVB
         }
 
         AddTsWriterFilterToGraph();
-        AddStreamSourceFilter(_defaultUrl);
+        AddStreamSourceFilter(url);
         IBaseFilter lastFilter = _filterStreamSource;
         AddMdPlugs(ref lastFilter);
         if (!ConnectTsWriter(lastFilter))
@@ -212,7 +212,7 @@ namespace TvLibrary.Implementations.DVB
         if (_graphState == GraphState.Idle)
         {
           Log.Log.Info("dvbip: tune: Building graph");
-          BuildGraph();
+          BuildGraph(dvbipChannel.Url);
           if (_mapSubChannels.ContainsKey(subChannelId) == false)
           {
             subChannelId = GetNewSubChannel(channel);
@@ -310,7 +310,6 @@ namespace TvLibrary.Implementations.DVB
       if (_graphState != GraphState.Idle)
       {
         RemoveStreamSourceFilter();
-        AddStreamSourceFilter(_defaultUrl);
       }
     }
 
diff --git a/TvEngine3/TVLibrary/TVLibrary/Implementations/DVB/Graphs/DVBIP/TvCardDVBIPBuiltIn.cs b/TvEngine3/TVLibrary/TVLibrary/Implementations/DVB/Graphs/DVBIP/TvCardDVBIPBuiltIn.cs
index bccdfa9..1edb5c7 100644
--- a/TvEngine3/TVLibrary/TVLibrary/Implementations/DVB/Graphs/DVBIP/TvCardDVBIPBuiltIn.cs
+++ b/TvEngine3/TVLibrary/TVLibrary/Implementations/DVB/Graphs/DVBIP/TvCardDVBIPBuiltIn.cs
@@ -21,6 +21,7 @@
 using System;
 using System.Runtime.InteropServices;
 using DirectShowLib;
+using TvLibrary.Implementations.Helper;
 using TvLibrary.Interfaces;
 
 namespace TvLibrary.Implementations.DVB
@@ -47,6 +48,8 @@ namespace TvLibrary.Implementations.DVB
       _defaultUrl = "udp://@0.0.0.0:1234";
     }
 
+    protected AMMediaType mpeg2ProgramStream = new AMMediaType();
+
     /// <summary>
     /// AddStreamSourceFilter
     /// </summary>
@@ -54,8 +57,14 @@ namespace TvLibrary.Implementations.DVB
     protected override void AddStreamSourceFilter(string url)
     {
       Log.Log.WriteFile("dvbip:Add MediaPortal IPTV Source Filter");
-      _filterStreamSource = FilterGraphTools.AddFilterFromClsid(_graphBuilder, typeof (MPIPTVSource).GUID,
-                                                                "MediaPortal IPTV Source Filter");
+      _filterStreamSource = FilterGraphTools.FindFilterByClsid(_graphBuilder, typeof(MPIPTVSource).GUID);
+
+      if (_filterStreamSource == null)
+      {
+        _filterStreamSource = FilterGraphTools.AddFilterFromClsid(_graphBuilder, typeof (MPIPTVSource).GUID,
+                                                                  "MediaPortal IPTV Source Filter");
+      }
+
       AMMediaType mpeg2ProgramStream = new AMMediaType();
       mpeg2ProgramStream.majorType = MediaType.Stream;
       mpeg2ProgramStream.subType = MediaSubType.Mpeg2Transport;
@@ -87,6 +96,11 @@ namespace TvLibrary.Implementations.DVB
         _graphBuilder.RemoveFilter(_filterStreamSource);
         Release.ComObject("MediaPortal IPTV Source Filter", _filterStreamSource);
         _filterStreamSource = null;
+        if (mpeg2ProgramStream != null)
+        {
+          DsUtils.FreeAMMediaType(mpeg2ProgramStream);
+          mpeg2ProgramStream = null;
+        }
       }
     }
 
diff --git a/TvEngine3/TVLibrary/TVLibrary/Implementations/TvCardBase.cs b/TvEngine3/TVLibrary/TVLibrary/Implementations/TvCardBase.cs
index 86c6d9c..2596f96 100644
--- a/TvEngine3/TVLibrary/TVLibrary/Implementations/TvCardBase.cs
+++ b/TvEngine3/TVLibrary/TVLibrary/Implementations/TvCardBase.cs
@@ -717,15 +717,15 @@ namespace TvLibrary.Implementations
       {
         _subChannelId = 0;
      
-        Log.Log.Info("tvcard:FreeSubChannel : no subchannels present, pausing graph");          
+        Log.Log.Info("tvcard:FreeSubChannel : no subchannels present, pausing graph");
         if (SupportsPauseGraph)
         {
-          PauseGraph();  
+          PauseGraph();
         }
         else
         {
-          StopGraph();  
-        }          
+          StopGraph();
+        }
         
       }
       else
-- 
1.8.0.msysgit.0

