If subtitles need to be enabled by default (DefaultCCMode setting),
they weren't enabled in case the stream didn't have subtitles when
InitVideo() was called. Subtitles need to be re-enabled as
soon as subtitles are encountered.

Fix: Always on subtitle stream change re-enable (and reselect) the 
subtitles.

Index: mythtv/libs/libmythtv/NuppelVideoPlayer.cpp
===================================================================
--- mythtv.orig/libs/libmythtv/NuppelVideoPlayer.cpp	2006-04-25 00:53:19.000000000 +0300
+++ mythtv/libs/libmythtv/NuppelVideoPlayer.cpp	2006-04-25 21:52:28.000000000 +0300
@@ -529,7 +529,8 @@
         videoOutput->EmbedInWidget(embedid, embx, emby, embw, embh);
     }
 
-    SetCaptionsEnabled(gContext->GetNumSetting("DefaultCCMode"));
+    if (m_wantSubtitles = gContext->GetNumSetting("DefaultCCMode"))
+	SetCaptionsEnabled(true, false);
 
     return true;
 }
@@ -1426,50 +1427,53 @@
     }
 }
 
-void NuppelVideoPlayer::EnableCaptions(uint mode)
+void NuppelVideoPlayer::EnableCaptions(uint mode, bool osd_msg)
 {
-    QString msg = "";
-    if (kDisplaySubtitle & mode)
-    {
-        msg += decoder->GetTrackDesc(kTrackTypeSubtitle,
-                                     GetTrack(kTrackTypeSubtitle));
-    }
-    if (kDisplayNUVTeletextCaptions & mode)
-        msg += QObject::tr("TXT") + QString(" %1").arg(ttPageNum, 3, 16);
-    if (kDisplayCC608 & mode)
-    {
-        msg += decoder->GetTrackDesc(kTrackTypeCC608,
-                                     GetTrack(kTrackTypeCC608));
-    }
-    if (kDisplayCC708 & mode)
-    {
-        msg += decoder->GetTrackDesc(kTrackTypeCC708,
-                                     GetTrack(kTrackTypeCC708));
-    }
-    if (kDisplayTeletextCaptions & mode)
+    textDisplayMode = mode;
+
+    if (osd_msg && osd)
     {
-        msg += decoder->GetTrackDesc(kTrackTypeTeletextCaptions,
-                                     GetTrack(kTrackTypeTeletextCaptions));
-        
-        TeletextViewer *tt_view = GetOSD()->GetTeletextViewer();
-        int page = decoder->GetTrackLanguageIndex(
-            kTrackTypeTeletextCaptions,
-            GetTrack(kTrackTypeTeletextCaptions));
 
-        if ((tt_view) && (page > 0))
-        {
-            EnableTeletext();
-            tt_view->SetPage(page, -1);
-            textDisplayMode = kDisplayTeletextCaptions;
-        }
-    }
+	QString msg = "";
+	if (kDisplaySubtitle & mode)
+	{
+	    msg += decoder->GetTrackDesc(kTrackTypeSubtitle,
+					 GetTrack(kTrackTypeSubtitle));
+	}
+	if (kDisplayNUVTeletextCaptions & mode)
+	    msg += QObject::tr("TXT") + QString(" %1").arg(ttPageNum, 3, 16);
+	if (kDisplayCC608 & mode)
+	{
+	    msg += decoder->GetTrackDesc(kTrackTypeCC608,
+					 GetTrack(kTrackTypeCC608));
+	}
+	if (kDisplayCC708 & mode)
+	{
+	    msg += decoder->GetTrackDesc(kTrackTypeCC708,
+					 GetTrack(kTrackTypeCC708));
+	}
+	if (kDisplayTeletextCaptions & mode)
+	{
+	    msg += decoder->GetTrackDesc(kTrackTypeTeletextCaptions,
+					 GetTrack(kTrackTypeTeletextCaptions));
+        
+	    TeletextViewer *tt_view = GetOSD()->GetTeletextViewer();
+	    int page = decoder->GetTrackLanguageIndex(
+		kTrackTypeTeletextCaptions,
+		GetTrack(kTrackTypeTeletextCaptions));
+
+	    if ((tt_view) && (page > 0))
+	    {
+		EnableTeletext();
+		tt_view->SetPage(page, -1);
+		textDisplayMode = kDisplayTeletextCaptions;
+	    }
+	}
 
-    msg += " " + QObject::tr("On");
+	msg += " " + QObject::tr("On");
 
-    textDisplayMode = mode;
-    if (osd)
         osd->SetSettingsText(msg, 3 /* seconds until message timeout */);
-
+    }
 }
 
 void NuppelVideoPlayer::EnableTeletext(void)
@@ -1552,37 +1556,42 @@
     return textDisplayMode;
 }
 
-void NuppelVideoPlayer::SetCaptionsEnabled(bool enable)
+void NuppelVideoPlayer::SetCaptionsEnabled(bool enable, bool showOsd)
 {
     uint origMode = textDisplayMode;
 
     if (!enable)
     {
-        DisableCaptions(origMode);
+        DisableCaptions(origMode, showOsd);
+	m_wantSubtitles = false;
         return;
     }
+    m_wantSubtitles = true;
     
     // figure out which text type to enable..
     bool captions_found = true;
-    if (decoder->GetTrackCount(kTrackTypeSubtitle))
-        EnableCaptions(kDisplaySubtitle);
+    if (decoder->GetTrackCount(kTrackTypeSubtitle)) 
+    {
+	decoder->AutoSelectTrack(kTrackTypeSubtitle);
+        EnableCaptions(kDisplaySubtitle, showOsd);
+    } 
     else if (decoder->GetTrackCount(kTrackTypeCC708))
-        EnableCaptions(kDisplayCC708);
+        EnableCaptions(kDisplayCC708, showOsd);
     else if (decoder->GetTrackCount(kTrackTypeTeletextCaptions))
-        EnableCaptions(kDisplayTeletextCaptions);
+        EnableCaptions(kDisplayTeletextCaptions, showOsd);
     else if (vbimode == VBIMode::PAL_TT)
-        EnableCaptions(kDisplayNUVTeletextCaptions);
+        EnableCaptions(kDisplayNUVTeletextCaptions, showOsd);
     else if (vbimode == VBIMode::NTSC_CC)
     {
         if (decoder->GetTrackCount(kTrackTypeCC608))
-            EnableCaptions(kDisplayCC608);
+            EnableCaptions(kDisplayCC608, showOsd);
         else
             captions_found = false;
     }
     else 
         captions_found = false;
 
-    if (!captions_found && osd)
+    if (!captions_found && osd && showOsd)
     {
         QString msg = QObject::tr(
             "No captions", "CC/Teletext/Subtitle text not available");
@@ -5851,6 +5860,20 @@
     subtitleLock.unlock();
 }
 
+/** \fn NuppelVideoPlayer::SubtitleTracksChanged()
+ *  \brief Used to notify of subtitle track changes.
+ */
+void NuppelVideoPlayer::SubtitleTracksChanged()
+{
+    // this might be called before the decoder is initialized
+    if (decoder == NULL)
+	return;
+    if (m_wantSubtitles) {
+	SetCaptionsEnabled(m_wantSubtitles, false);
+    }
+}
+
+
 /** \fn NuppelVideoPlayer::SetDecoder(DecoderBase*)
  *  \brief Sets the stream decoder, deleting any existing recorder.
  */
Index: mythtv/libs/libmythtv/NuppelVideoPlayer.h
===================================================================
--- mythtv.orig/libs/libmythtv/NuppelVideoPlayer.h	2006-04-25 00:53:19.000000000 +0300
+++ mythtv/libs/libmythtv/NuppelVideoPlayer.h	2006-04-25 10:08:37.000000000 +0300
@@ -277,15 +277,16 @@
     void AddTextData(unsigned char *buffer, int len,
                      long long timecode, char type);
     void AddSubtitle(const AVSubtitle& subtitle);
+    void SubtitleTracksChanged();
 
     // Closed caption and teletext stuff
     uint GetCaptionMode(void) const { return textDisplayMode; }
     void ResetCaptions(uint mode_override = 0);
     void DisableCaptions(uint mode, bool osd_msg = true);
-    void EnableCaptions(uint mode);
+    void EnableCaptions(uint mode, bool osd_msg = true);
     bool ToggleCaptions(void);
     bool ToggleCaptions(uint mode);
-    void SetCaptionsEnabled(bool);
+    void SetCaptionsEnabled(bool, bool showOsd = true);
 
     // Teletext Menu and non-NUV teletext decoder
     void EnableTeletext(void);
@@ -608,6 +609,12 @@
     long long osdSubtitlesExpireAt;
     MythDeque<AVSubtitle> nonDisplayedSubtitles;
 
+    /// Used for figuring out whether subtitles should be enabled
+    /// when they are encountered in the played stream. A workaround 
+    /// for the case in which the subtitles are not available immediately 
+    /// when loading the video, thus won't get enabled correctly.
+    bool m_wantSubtitles;
+
     CC708Service CC708services[64];
     QString    osdfontname;
     QString    osdccfontname;
Index: mythtv/libs/libmythtv/avformatdecoder.cpp
===================================================================
--- mythtv.orig/libs/libmythtv/avformatdecoder.cpp	2006-04-25 10:08:25.000000000 +0300
+++ mythtv/libs/libmythtv/avformatdecoder.cpp	2006-04-25 21:49:02.000000000 +0300
@@ -1179,6 +1179,7 @@
     map<int,uint> lang_sub_cnt;
     map<int,uint> lang_aud_cnt;
 
+    bool subtitleTracksFound = false;
     for (int i = 0; i < ic->nb_streams; i++)
     {
         AVCodecContext *enc = ic->streams[i]->codec;
@@ -1289,6 +1290,7 @@
             }
             case CODEC_TYPE_SUBTITLE:
             {
+		subtitleTracksFound = true;
                 bitrate += enc->bit_rate;
                 VERBOSE(VB_PLAYBACK, LOC + QString("subtitle codec (%1)")
                         .arg(codec_type_string(enc->codec_type)));
@@ -1432,11 +1434,14 @@
                 }
             }
         }
-    }
+    } 
 
     // Select a new track at the next opportunity.
     ResetTracks();
 
+    if (subtitleTracksFound && GetNVP() != NULL)
+	GetNVP()->SubtitleTracksChanged();
+
     // We have to do this here to avoid the NVP getting stuck
     // waiting on audio.
     if (GetNVP()->HasAudioIn() && tracks[kTrackTypeAudio].empty())
Index: mythtv/libs/libmythtv/decoderbase.cpp
===================================================================
--- mythtv.orig/libs/libmythtv/decoderbase.cpp	2006-04-25 00:53:19.000000000 +0300
+++ mythtv/libs/libmythtv/decoderbase.cpp	2006-04-25 21:44:44.000000000 +0300
@@ -819,18 +819,22 @@
 {
     QMutexLocker locker(&avcodeclock);
 
+    if (type == kTrackTypeSubtitle && GetNVP() != NULL)
+	GetNVP()->SubtitleTracksChanged();
+
     for (uint i = 0; i < tracks[type].size(); i++)
         if (info.stream_id == tracks[type][i].stream_id)
             return false;
 
     tracks[type].push_back(info);
+	
     return true;
 }
 
 /** \fn DecoderBase::AutoSelectTrack(uint)
  *  \brief Select best track.
  *
- *   If case there's only one track available, always choose it.
+ *   In case there's only one track available, always choose it.
  *
  *   If there is a user selected track we try to find it.
  *
Index: mythtv/libs/libmythtv/decoderbase.h
===================================================================
--- mythtv.orig/libs/libmythtv/decoderbase.h	2006-04-25 00:53:19.000000000 +0300
+++ mythtv/libs/libmythtv/decoderbase.h	2006-04-25 10:12:42.000000000 +0300
@@ -136,11 +136,12 @@
 
     virtual bool SetAudioByComponentTag(int) { return false; }
     virtual bool SetVideoByComponentTag(int) { return false; }
+    virtual int  AutoSelectTrack(uint type);
 
   protected:
-    virtual int  AutoSelectTrack(uint type);
     inline  void AutoSelectTracks(void);
     inline  void ResetTracks(void);
+    void AnnounceTrackTypeChange(uint type);
 
     void FileChanged(void);
 
@@ -245,8 +246,15 @@
 
 inline void DecoderBase::ResetTracks(void)
 {
-    for (uint i = 0; i < kTrackTypeCount; i++)
-        currentTrack[i] = -1;
+    for (uint i = 0; i < kTrackTypeCount; i++) 
+    {
+	if (currentTrack[i] != -1) 
+	{
+	    if (i == kTrackTypeSubtitle && GetNVP() != NULL)
+		GetNVP()->SubtitleTracksChanged();
+	    currentTrack[i] = -1;
+	}	
+    }    
 }
 
 #endif
