Index: DeMultiplexer.cpp
===================================================================
--- DeMultiplexer.cpp	(revision 28260)
+++ DeMultiplexer.cpp	(working copy)
@@ -47,14 +47,15 @@
 #define READ_SIZE (1316*30)
 #define INITIAL_READ_SIZE (READ_SIZE * 1024)
 
+#define AUDIO_CHANGE 0x1
+#define VIDEO_CHANGE 0x2
+
 extern void LogDebug(const char *fmt, ...);
 
 // *** UNCOMMENT THE NEXT LINE TO ENABLE DYNAMIC VIDEO PIN HANDLING!!!! ******
 #define USE_DYNAMIC_PINS
 
-extern int ShowBuffer;
 
-
 CDeMultiplexer::CDeMultiplexer(CTsDuration& duration,CTsReaderFilter& filter)
 :m_duration(duration)
 ,m_filter(filter)
@@ -88,6 +89,8 @@
   m_lastVideoPTS.IsValid = false;
   m_lastAudioPTS.IsValid = false;
   m_mpegParserTriggerFormatChange = false;
+  m_videoChanged=false;
+  m_audioChanged=false;
   SetMediaChanging(false);
   SetAudioChanging(false);
   m_DisableDiscontinuitiesFiltering = false;
@@ -172,9 +175,9 @@
 	  // here, stream is not parsed yet
       if (!IsMediaChanging())             
       {
-        LogDebug("SetAudioStream : OnMediaTypeChanged(1)");
+        LogDebug("SetAudioStream : OnMediaTypeChanged(AUDIO_CHANGE)");
         Flush() ;                   
-        m_filter.OnMediaTypeChanged(1);
+        m_filter.OnMediaTypeChanged(AUDIO_CHANGE);
         SetMediaChanging(true);
         m_filter.m_bForceSeekOnStop=true;     // Force stream to be resumed after
       }
@@ -620,6 +623,8 @@
   m_bStarting=true ;
   m_receivedPackets=0;
   m_mpegParserTriggerFormatChange=false;
+  m_videoChanged=false;
+  m_audioChanged=false;
   m_bEndOfFile=false;
   m_bHoldAudio=false;
   m_bHoldVideo=false;
@@ -1323,26 +1328,45 @@
             LogDebug("DeMultiplexer: %x video format changed: res=%dx%d aspectRatio=%d:%d fps=%d isInterlaced=%d",header.Pid,m_mpegPesParser->basicVideoInfo.width,m_mpegPesParser->basicVideoInfo.height,m_mpegPesParser->basicVideoInfo.arx,m_mpegPesParser->basicVideoInfo.ary,m_mpegPesParser->basicVideoInfo.fps,m_mpegPesParser->basicVideoInfo.isInterlaced);
             if (m_mpegParserTriggerFormatChange)
             {
-              LogDebug("DeMultiplexer: OnMediaFormatChange triggered by mpeg2Parser");
+              LogDebug("DeMultiplexer: OnMediaFormatChange triggered by mpeg2Parser, aud %d, vid 1", m_audioChanged);
               SetMediaChanging(true);
-              m_filter.OnMediaTypeChanged(3);
+              if (m_audioChanged)
+                m_filter.OnMediaTypeChanged(VIDEO_CHANGE | AUDIO_CHANGE); //Video and audio
+              else
+                m_filter.OnMediaTypeChanged(VIDEO_CHANGE); //Video only
               m_mpegParserTriggerFormatChange=false;
             }
             LogDebug("DeMultiplexer: triggering OnVideoFormatChanged");
             m_filter.OnVideoFormatChanged(m_mpegPesParser->basicVideoInfo.streamType,m_mpegPesParser->basicVideoInfo.width,m_mpegPesParser->basicVideoInfo.height,m_mpegPesParser->basicVideoInfo.arx,m_mpegPesParser->basicVideoInfo.ary,15000000,m_mpegPesParser->basicVideoInfo.isInterlaced);
           }
-          else
+          else //video resolution is the unchanged, but there may be other format changes
           {
             if (m_mpegParserTriggerFormatChange && Gop)
             {
-              LogDebug("DeMultiplexer: Got GOP after the channel change was detected without correct mpeg header parsing, so we trigger the format change now.");
-              m_filter.OnMediaTypeChanged(3);
+              if (m_audioChanged || m_videoChanged)
+              {
+                SetMediaChanging(true);
+                LogDebug("DeMultiplexer: Got GOP after channel change detected, trigger format change, aud %d, vid %d", m_audioChanged, m_videoChanged);
+                if (m_audioChanged && m_videoChanged)
+                  m_filter.OnMediaTypeChanged(VIDEO_CHANGE | AUDIO_CHANGE);
+                else if (m_audioChanged)
+                  m_filter.OnMediaTypeChanged(AUDIO_CHANGE);
+                else
+                  m_filter.OnMediaTypeChanged(VIDEO_CHANGE);
+              }
+              else
+              {
+                SetMediaChanging(false);
+              }
               m_mpegParserTriggerFormatChange=false;
             }
           }
         }
         else
+        {
           m_bSetVideoDiscontinuity = !m_mVideoValidPES;
+        }
+        
         m_pl.RemoveAll();
           
         p2->bDiscontinuity = m_p->bDiscontinuity; m_p->bDiscontinuity = FALSE;
@@ -1635,20 +1659,36 @@
               LogDebug("DeMultiplexer: %x video format changed: res=%dx%d aspectRatio=%d:%d fps=%d isInterlaced=%d",header.Pid,m_mpegPesParser->basicVideoInfo.width,m_mpegPesParser->basicVideoInfo.height,m_mpegPesParser->basicVideoInfo.arx,m_mpegPesParser->basicVideoInfo.ary,m_mpegPesParser->basicVideoInfo.fps,m_mpegPesParser->basicVideoInfo.isInterlaced);
               if (m_mpegParserTriggerFormatChange)
               {
-                LogDebug("DeMultiplexer: OnMediaFormatChange triggered by mpeg2Parser");
+                LogDebug("DeMultiplexer: OnMediaFormatChange triggered by mpeg2Parser, aud %d, vid 1", m_audioChanged);
                 SetMediaChanging(true);
-                m_filter.OnMediaTypeChanged(3);
+                if (m_audioChanged)
+                  m_filter.OnMediaTypeChanged(VIDEO_CHANGE | AUDIO_CHANGE); //Video and audio
+                else
+                  m_filter.OnMediaTypeChanged(VIDEO_CHANGE); //Video only
                 m_mpegParserTriggerFormatChange=false;
               }
               LogDebug("DeMultiplexer: triggering OnVideoFormatChanged");
               m_filter.OnVideoFormatChanged(m_mpegPesParser->basicVideoInfo.streamType,m_mpegPesParser->basicVideoInfo.width,m_mpegPesParser->basicVideoInfo.height,m_mpegPesParser->basicVideoInfo.arx,m_mpegPesParser->basicVideoInfo.ary,15000000,m_mpegPesParser->basicVideoInfo.isInterlaced);
             }
-            else
+            else //video resolution is the unchanged, but there may be other format changes
             {
               if (m_mpegParserTriggerFormatChange && Gop)
               {
-                LogDebug("DeMultiplexer: Got GOP after the channel change was detected without correct mpeg header parsing, so we trigger the format change now.");
-                m_filter.OnMediaTypeChanged(3);
+                if (m_audioChanged || m_videoChanged)
+                {
+                  SetMediaChanging(true);
+                  LogDebug("DeMultiplexer: Got GOP after channel change detected, trigger format change, aud %d, vid %d", m_audioChanged, m_videoChanged);
+                  if (m_audioChanged && m_videoChanged)
+                    m_filter.OnMediaTypeChanged(VIDEO_CHANGE | AUDIO_CHANGE);
+                  else if (m_audioChanged)
+                    m_filter.OnMediaTypeChanged(AUDIO_CHANGE);
+                  else
+                    m_filter.OnMediaTypeChanged(VIDEO_CHANGE);
+                }
+                else
+                {
+                  SetMediaChanging(false);
+                }
                 m_mpegParserTriggerFormatChange=false;
               }
             }
@@ -1860,22 +1900,35 @@
   }
 
   bool changed=false;
-  bool videoChanged=false;
+  m_videoChanged=false;
+  m_audioChanged=false;
+
+  #ifdef USE_DYNAMIC_PINS
+  //Is the video pin connected?
+  if ((m_filter.GetVideoPin()->IsConnected()) && (m_pids.videoPids.size() > 0))
+  {
+    changed=true; //force a check in the mpeg parser
+    if (oldVideoServiceType != m_pids.videoPids[0].VideoServiceType)
+    {
+      m_videoChanged=true;
+    }
+  }
+  #else
   //did the video format change?
-  if (m_pids.videoPids.size() > 0 && oldVideoServiceType != m_pids.videoPids[0].VideoServiceType )
+  if (m_pids.videoPids.size() > 0 && oldVideoServiceType != m_pids.videoPids[0].VideoServiceType)
   {
-    //yes, is the video pin connected?
+    //yes, is the audio pin connected?
     if (m_filter.GetVideoPin()->IsConnected())
     {
       changed=true;
-      videoChanged=true;
+      m_videoChanged=true;
     }
   }
-
+  #endif
+  
   m_iAudioStream = 0;
 
   LogDebug ("Setting initial audio index to : %i", m_iAudioStream);
-  bool audioChanged=false;
 
   //get the new audio format
   int newAudioStreamType=SERVICE_TYPE_AUDIO_MPEG2;
@@ -1891,7 +1944,7 @@
     if (m_filter.GetAudioPin()->IsConnected())
     {
       changed=true;
-      audioChanged=true;
+      m_audioChanged=true;
     }
   }
 
@@ -1899,10 +1952,10 @@
   if (changed)
   {
     #ifdef USE_DYNAMIC_PINS
-    // if we have a video stream and it's format changed, let the mpeg parser trigger the OnMediaTypeChanged
-    if (m_pids.videoPids.size() > 0 && m_pids.videoPids[0].Pid>0x1 && videoChanged)  
+    // if we have a video stream, let the mpeg parser trigger the OnMediaTypeChanged
+    if (m_pids.videoPids.size() > 0 && m_pids.videoPids[0].Pid>0x1)  
     {
-      LogDebug("DeMultiplexer: We detected a new media type change which has a video stream, so we let the mpegParser trigger the event");
+      LogDebug("DeMultiplexer: We have a video stream, so we let the mpegParser check/trigger format changes");
       m_receivedPackets=0;
       m_mpegParserTriggerFormatChange=true;
       SetMediaChanging(true);
@@ -1916,19 +1969,19 @@
           m_AudioStreamType = newAudioStreamType ;
           // notify the ITSReaderCallback. MP will then rebuild the graph
           LogDebug("DeMultiplexer: Audio media types changed. Trigger OnMediaTypeChanged()...");
-          m_filter.OnMediaTypeChanged(1);
+          m_filter.OnMediaTypeChanged(AUDIO_CHANGE);
           SetMediaChanging(true); 
         }
       }
     }
     #else
-    if (audioChanged && videoChanged)
-      m_filter.OnMediaTypeChanged(3);
+    if (m_audioChanged && m_videoChanged)
+      m_filter.OnMediaTypeChanged(VIDEO_CHANGE | AUDIO_CHANGE);
     else
-      if (audioChanged)
-        m_filter.OnMediaTypeChanged(1);
+      if (m_audioChanged)
+        m_filter.OnMediaTypeChanged(AUDIO_CHANGE);
       else
-        m_filter.OnMediaTypeChanged(2);
+        m_filter.OnMediaTypeChanged(VIDEO_CHANGE);
     #endif
   }
 
Index: DeMultiplexer.h
===================================================================
--- DeMultiplexer.h	(revision 28260)
+++ DeMultiplexer.h	(working copy)
@@ -219,6 +219,8 @@
   bool m_bStarting;
 
   bool m_mpegParserTriggerFormatChange;
+  bool m_videoChanged;
+  bool m_audioChanged;
   bool m_bSetAudioDiscontinuity;
   bool m_bSetVideoDiscontinuity;
   CPcr m_subtitlePcr;
Index: TsReader.cpp
===================================================================
--- TsReader.cpp	(revision 28260)
+++ TsReader.cpp	(working copy)
@@ -170,7 +170,8 @@
   TCHAR filename[1024];
   GetLogFile(filename);
   ::DeleteFile(filename);
-  LogDebug("---------- v0.4.12 -------------------");
+  LogDebug("----- Graph rebuild patch -----");
+  LogDebug("--------------- v0.4.14 -------------------");
 
   m_fileReader=NULL;
   m_fileDuration=NULL;
