Skip to content

Add option to default to Last Captions State - #1183

Merged
linuxdude42 merged 5 commits into
MythTV:masterfrom
SteveErl:erlMerge30
Nov 24, 2026
Merged

linuxdude42 merged 5 commits into
MythTV:masterfrom
SteveErl:erlMerge30

Conversation

@SteveErl

Copy link
Copy Markdown
Contributor

This change adds a new configuration option for mythfrontend closed captioning. Setup -> Video -> Playback OSD -> Always display Closed Captioning or Subtitles is switched to Default to display Closed Captioning or Subtitles and the user is able to pick a new configuration option.

A LastCaptions entry is added into the Settings database. This boolean gets updated each time a user toggles captions on or off, and indicates whether captions were enabled or disabled the last time it changed. The m_captionsEnabledbyDefault variable is switched from a boolean to an unsigned int so that it can hold 0, 1, or 2. It's set to 2 when the Last Captions State configuration is selected. When starting up viewing of a video recording or Live TV, by default, captions are either disabled (0), enabled (1), or set to match the last caption state (2).

When a user picks Last Captions State, mythfrontend will remember whether captions were enabled or disabled the last time something was watched, and will carry over that state for the next thing to be watched. Turn on captions, and they will continue to appear until you explicitly turn them off. Turn off captions and they will not appear until you explicitly turn them on.

Resolves: #1154

Checklist

@kmdewaal
kmdewaal self-requested a review February 2, 2026 12:50
@BobEntwhistle

Copy link
Copy Markdown
Contributor

@SteveErl The setting migration is safe, which was the first thing I checked:
MythUICheckBoxSetting stores "1"/"0" and GetBoolSetting() is GetNumSetting(...) > 0, so existing values land on the first two combo entries and an unset value still reads as 0, captions off.

Three things I noticed:

The help text under the new selections still reads "If enabled, captions will be displayed when playing back recordings...", which described the old checkbox. With three choices it no longer matches the control.

m_captionsEnabledbyDefault is a uint carrying 0/1/2 with a // 0=off, 1=on, 2=last comment, and the switch uses the bare values. The coding standards ask for enum class with an explicit underlying type, which would also let the switch read without the comment.

LastCaptions is only written in ToggleCaptions(). Captions can also change through ToggleCaptionsByType() and through track selection, so "last captions state" will follow the toggle key but not those. Deliberate, or worth hooking SetCaptionsEnabled() instead?

@SteveErl

Copy link
Copy Markdown
Contributor Author

@BobEntwhistle Thanks for your analysis. The help text has been fixed. Based on your suggestion, an enum for the m_captionsEnabledbyDefault values has been introduced. I've implemented your good idea of moving the call of SaveSetting("LastCaptions",...) into SetCaptionsEnabled() so that it will be updated through actions other than just the toggle key. ToggleCaptionsByType() now calls SaveSetting("LastCaptions",...).

This solution adds a LastCaptions entry into the Settings
database. This boolean gets updated each time a user toggles
captions on or off, and indicates whether captions were
enabled or disabled the last time it changed. The
m_captionsEnabledbyDefault variable is switched from a
boolean to an unsigned int so that it can hold 0, 1, or 2.
It's set to 2 when the Last Captions State configuration
is selected. When starting up viewing of a video recording
or Live TV, captions are either disabled (0), enabled (1),
or set to match the last caption state (2).

Resolves: MythTV#1154
The help text has been updated to describe
the three optional value. A missing space in
"duringplayback." has been added.

An enum for the m_captionsEnabledbyDefault values
has been introduced.

The call of SaveSetting("LastCaptions"...)
has been moved from ToggleCaptions() into
SetCaptionsEnabled() so that it will be updated
through actions other than just the toggle key.

ToggleCaptionsByType() has also been updated
to call SaveSetting("LastCaptions",...).

Resolves: MythTV#1154
@BobEntwhistle

Copy link
Copy Markdown
Contributor

@SteveErl Help text and the enum both look right to me now.

On LastCaptions, I sent you to the wrong function. SetCaptionsEnabled() and ToggleCaptionsByType() are two entry points among several, and the others go straight past them to EnableCaptions()/DisableCaptions():

  • the TV::EnableCaptions / TV::DisableCaptions signals, connected at mythplayercaptionsui.cpp:32-33, emitted from tv_play.cpp:3129, 3131 and 4523-4573 — the per-type actions for AV subtitles, CC708, CC608, teletext and raw text
  • SetTrack(), :363-364
  • ChangeCaptionTrack(), :425-435
  • DisableTeletext(), :507

None of those are new; they are all in current master, so this is something I should have spotted the first time rather than something your branch introduced.

There is also a side effect in the current shape. The disable branch of SetCaptionsEnabled() saves unconditionally:

    if (!Enable)
    {
        DisableCaptions(origmode, UpdateOSD);
        gCoreContext->SaveSetting("LastCaptions", false);
        return;
    }

and VideoStart() calls SetCaptionsEnabled(false, false) for StartCaptionsOff (mythplayerui.cpp:471). So while the default is "Captions off", every playback start writes LastCaptions=false. Someone who later switches the setting to "Last captions state" gets whatever the last start wrote rather than their last real preference. ToggleCaptionsByType() already guards its save with if (isEnabledOnExit != isEnabledOnEntry), so the two paths disagree about whether a programmatic call counts.

A suggestion that covers all of it in one place. EnableCaptions() and DisableCaptions() already work out whether the display mode genuinely changed, because they already have to, in order to emit CaptionsStateChanged:

    auto oldcaptions = m_captionsState.m_textDisplayMode;
    m_captionsState.m_textDisplayMode = Mode;          // or &= ~Mode in DisableCaptions
    if (m_captionsState.m_textDisplayMode != kDisplayNone)
        m_lastValidTextDisplayMode = m_captionsState.m_textDisplayMode;
    if (oldcaptions != m_captionsState.m_textDisplayMode)
        emit CaptionsStateChanged(m_captionsState);

So a small helper called from inside that existing if, in both functions, and the four scattered SaveSetting calls removed:

void MythPlayerCaptionsUI::SaveLastCaptionsState()
{
    bool enabled = m_captionsState.m_textDisplayMode != kDisplayNone;
    if (enabled != m_lastCaptionsEnabled)
    {
        m_lastCaptionsEnabled = enabled;
        gCoreContext->SaveSetting("LastCaptions", enabled);
    }
}

That gets you four things:

  • every path is covered, including the ones above, because they all end up in EnableCaptions()/DisableCaptions()
  • the VideoStart() overwrite goes away by itself: m_textDisplayMode starts at kDisplayNone (mythplayerstate.h:70), so in "Captions off" mode DisableCaptions() does &= ~kDisplayNone, the mode does not change, the if is not entered, and nothing is written
  • switching between two enabled modes, say CC608 to CC708, no longer writes, because the derived boolean is unchanged
  • m_lastCaptionsEnabled stays in step with what is in the database — at the moment it is read once in the constructor (mythplayer.cpp:94) and never updated

One wrinkle worth knowing rather than fixing: SetTrack() calls DisableCaptions() then EnableCaptions(), so a track change writes false and then true — two database writes with the right end state. If that bothers you, recording the state once when playback ends would avoid it, at the cost of losing it on an unclean exit.

The StartCaptionsOn default still writes true at each start under this scheme, since captions really are being turned on. That seems defensible, but if you would rather the startup application never counted as a preference, a flag set after VideoStart() has applied the default would do it.

@SteveErl

Copy link
Copy Markdown
Contributor Author

@BobEntwhistle I implemented your helper function, and added a temporary LOG message to see how often it was getting invoked.

156         LOG(VB_GENERAL, LOG_INFO, QString("SaveLastCaptionsState enabled = %1").arg(enabled));
157         m_lastCaptionsEnabled = enabled;
158         gCoreContext->SaveSetting("LastCaptions", enabled);

In Live TV, I enabled captions.

2026-10-24 21:44:22.035791 I [2734523/2734523] CoreContext mythplayercaptionsui.cpp:332:SetCaptionsEnabled  SetCaptionsEnabled calling DisableCaptions
2026-10-24 21:44:22.035827 I [2734523/2734523] CoreContext mythplayercaptionsui.cpp:343:SetCaptionsEnabled  SetCaptionsEnabled calling EnableCaptions
2026-10-24 21:44:22.036841 I [2734523/2734523] CoreContext mythplayercaptionsui.cpp:156:SaveLastCaptionsState  SaveLastCaptionsState enabled = 1

Then I exited, and started it again.

2026-10-24 21:45:43.682181 I [2734523/2734523] CoreContext mythplayercaptionsui.cpp:332:SetCaptionsEnabled  SetCaptionsEnabled calling DisableCaptions
2026-10-24 21:45:43.682204 I [2734523/2734523] CoreContext mythplayercaptionsui.cpp:156:SaveLastCaptionsState  SaveLastCaptionsState enabled = 0
2026-10-24 21:45:43.682978 I [2734523/2734523] CoreContext mythplayercaptionsui.cpp:343:SetCaptionsEnabled  SetCaptionsEnabled calling EnableCaptions
2026-10-24 21:45:43.683153 I [2734523/2734523] CoreContext mythplayercaptionsui.cpp:156:SaveLastCaptionsState  SaveLastCaptionsState enabled = 1

As you predicted, when the Last Caption State is Enabled, we end up writing to the database to turn it off and then immediately to turn it on again. I don't like these wasteful, unnecessary database writes. I'm going to try adding an optional third argument to DisableCaptions(..., ..., bool saveInDb = true);

In DisableCaptions() the call to SaveLastCaptionsState() will only be made when saveInDb is true. In SetCaptionsEnabled() and SetTrack() this third argument will be set to false to avoid writing to the database twice. It's a bit of a hack, but it eliminates nonsense database writes.

A helper function SaveLastCaptionsState() has
been added to check for Captions Enabled state
changes before writing to the database. This
function will now get called from DisableCaptions()
and EnableCaptions().

A third argument has been added to DisableCaptions()
which specifies whether we want to write to the
database, or not. Normally we will write to the
database. However, in SetCaptionsEnabled() and
SetTrack() there are calls to DisableCaptions()
right before calling EnableCaptions(). We don't
want to pay the cost of a database write which
immediately gets overwritten, so we'll set the
new third argument to false in DisableCaptions().

Resolves: MythTV#1154
@BobEntwhistle

Copy link
Copy Markdown
Contributor

@SteveErl That looks good to me. With saveInDb false in SetCaptionsEnabled() and SetTrack(), and the m_lastCaptionsEnabled check inside SaveLastCaptionsState(), the playback-start case you logged now writes nothing at all when the stored value already matches.

For completeness, a few paths still do a disable followed by an enable with the default saveInDb = true. They are ToggleCaptionsByType() when switching from one caption type to another, ChangeCaptionTrack() (:431-433 and :441), and SetTeletextPage(). Each writes false and then true. They only run on a keypress and the stored value always ends up right, so I'd leave them unless you want them consistent.

Tiny style point: the other parameters in these signatures are PascalCase (Mode, UpdateOSD), so SaveInDb would match.

To follow the PascalCase naming convention, the
new argument name has been slightly altered.

Resolves: MythTV#1154
@SteveErl

Copy link
Copy Markdown
Contributor Author

@BobEntwhistle For completeness, I have code to cover the ToggleCaptionsByType() case and the ChangeCaptionTrack() case, but I don't see the write to true in the SetTeletextPage() case.

The new SaveInDb argument has been used to
defer a database write which is about to be
reversed in ToggleCaptionsByType() and
ChangeCaptionTrack().

Resolves: MythTV#1154
@SteveErl

Copy link
Copy Markdown
Contributor Author

@BobEntwhistle : Could you please add a Review Passed indicator? I think that would make it more likely that someone with merge privileges would merge in my changes. To give your approval to the Pull Request, click the Files changed tab near the top. Then click the green Submit review button. Click the Approve radio button, and optionally add some text, if you wish. Finally click the Submit review button at the bottom. This will add your name as a reviewer and a check-mark to indicate that it passed your review. Thanks!

@linuxdude42
linuxdude42 merged commit 4995d7e into MythTV:master Nov 24, 2026
10 of 11 checks passed
@SteveErl
SteveErl deleted the erlMerge30 branch November 25, 2026 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Option to default to Last Captions State

3 participants