Skip to content

Commit

Permalink
src/gui/widgets/subtitlelistwidget: fix timestamps for sec-sub-delay
Browse files Browse the repository at this point in the history
secondary-sub-delay was just merged into mpv. This fixes timestamps for
secondary subtitles in the subtitle list. This checks at runtime if
secondary-sub-delay is supported and uses that to send signals to
secSubDelayChanged if it does. If it does not, secSubDelayChanged
mirrors subDelayChanged.
  • Loading branch information
ripose-jp committed Nov 28, 2023
1 parent 37f101e commit bb4a0fe
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 28 deletions.
60 changes: 37 additions & 23 deletions src/gui/widgets/mpv/mpvwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,24 @@ void MpvWidget::initPropertyMap()
}
};

m_propertyMap["secondary-sub-delay"] =
[=] (mpv_event_property *prop) {
if (prop->format == MPV_FORMAT_DOUBLE)
{
m_secSubDelaySupported = true;
Q_EMIT secSubDelayChanged(*(double *)prop->data);
}
};

m_propertyMap["sub-delay"] =
[=] (mpv_event_property *prop) {
if (prop->format == MPV_FORMAT_DOUBLE)
{
Q_EMIT subDelayChanged(*(double *)prop->data);
if (!m_secSubDelaySupported)
{
Q_EMIT secSubDelayChanged(*(double *)prop->data);
}
}
};

Expand Down Expand Up @@ -513,29 +526,30 @@ void MpvWidget::initializeGL()
QCoreApplication::exit(EXIT_FAILURE);
}

mpv_observe_property(m_mpv, 0, "chapter-list", MPV_FORMAT_NODE);
mpv_observe_property(m_mpv, 0, "duration", MPV_FORMAT_DOUBLE);
mpv_observe_property(m_mpv, 0, "fullscreen", MPV_FORMAT_FLAG);
mpv_observe_property(m_mpv, 0, "media-title", MPV_FORMAT_STRING);
mpv_observe_property(m_mpv, 0, "path", MPV_FORMAT_STRING);
mpv_observe_property(m_mpv, 0, "pause", MPV_FORMAT_FLAG);
mpv_observe_property(m_mpv, 0, "time-pos", MPV_FORMAT_DOUBLE);
mpv_observe_property(m_mpv, 0, "track-list/count", MPV_FORMAT_INT64);
mpv_observe_property(m_mpv, 0, "volume", MPV_FORMAT_INT64);
mpv_observe_property(m_mpv, 0, "volume-max", MPV_FORMAT_INT64);

mpv_observe_property(m_mpv, 0, "aid", MPV_FORMAT_FLAG);
mpv_observe_property(m_mpv, 0, "aid", MPV_FORMAT_INT64);
mpv_observe_property(m_mpv, 0, "secondary-sid", MPV_FORMAT_FLAG);
mpv_observe_property(m_mpv, 0, "secondary-sid", MPV_FORMAT_INT64);
mpv_observe_property(m_mpv, 0, "sid", MPV_FORMAT_FLAG);
mpv_observe_property(m_mpv, 0, "sid", MPV_FORMAT_INT64);
mpv_observe_property(m_mpv, 0, "vid", MPV_FORMAT_FLAG);
mpv_observe_property(m_mpv, 0, "vid", MPV_FORMAT_INT64);

mpv_observe_property(m_mpv, 0, "secondary-sub-text", MPV_FORMAT_STRING);
mpv_observe_property(m_mpv, 0, "sub-delay", MPV_FORMAT_DOUBLE);
mpv_observe_property(m_mpv, 0, "sub-text", MPV_FORMAT_STRING);
mpv_observe_property(m_mpv, 0, "chapter-list", MPV_FORMAT_NODE);
mpv_observe_property(m_mpv, 0, "duration", MPV_FORMAT_DOUBLE);
mpv_observe_property(m_mpv, 0, "fullscreen", MPV_FORMAT_FLAG);
mpv_observe_property(m_mpv, 0, "media-title", MPV_FORMAT_STRING);
mpv_observe_property(m_mpv, 0, "path", MPV_FORMAT_STRING);
mpv_observe_property(m_mpv, 0, "pause", MPV_FORMAT_FLAG);
mpv_observe_property(m_mpv, 0, "time-pos", MPV_FORMAT_DOUBLE);
mpv_observe_property(m_mpv, 0, "track-list/count", MPV_FORMAT_INT64);
mpv_observe_property(m_mpv, 0, "volume", MPV_FORMAT_INT64);
mpv_observe_property(m_mpv, 0, "volume-max", MPV_FORMAT_INT64);

mpv_observe_property(m_mpv, 0, "aid", MPV_FORMAT_FLAG);
mpv_observe_property(m_mpv, 0, "aid", MPV_FORMAT_INT64);
mpv_observe_property(m_mpv, 0, "secondary-sid", MPV_FORMAT_FLAG);
mpv_observe_property(m_mpv, 0, "secondary-sid", MPV_FORMAT_INT64);
mpv_observe_property(m_mpv, 0, "sid", MPV_FORMAT_FLAG);
mpv_observe_property(m_mpv, 0, "sid", MPV_FORMAT_INT64);
mpv_observe_property(m_mpv, 0, "vid", MPV_FORMAT_FLAG);
mpv_observe_property(m_mpv, 0, "vid", MPV_FORMAT_INT64);

mpv_observe_property(m_mpv, 0, "secondary-sub-text", MPV_FORMAT_STRING);
mpv_observe_property(m_mpv, 0, "secondary-sub-delay", MPV_FORMAT_DOUBLE); // IMPORTANT: Put this before sub-delay
mpv_observe_property(m_mpv, 0, "sub-delay", MPV_FORMAT_DOUBLE);
mpv_observe_property(m_mpv, 0, "sub-text", MPV_FORMAT_STRING);

mpv_set_wakeup_callback(m_mpv, wakeup, this);
}
Expand Down
17 changes: 16 additions & 1 deletion src/gui/widgets/mpv/mpvwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ class MpvWidget : public QOpenGLWidget
*/
void subDelayChanged(const double delay) const;

/**
* Emitted when the secondary subtitle delay is changed.
* @param delay The signed delay in seconds.
*/
void secSubDelayChanged(const double delay) const;

/**
* Emitted when MpvWidget hides the cursor.
*/
Expand Down Expand Up @@ -355,8 +361,14 @@ private Q_SLOTS:
*/
void preventScreenDimming();

/**
* Checks if a property exists.
* @return true if the property exists, false otherwise.
*/
bool propertyExists(const char *prop);

/* The mpv context */
mpv_handle *m_mpv{nullptr};
mpv_handle *m_mpv = nullptr;

/* The mpv render context */
mpv_render_context *mpv_gl;
Expand All @@ -382,6 +394,9 @@ private Q_SLOTS:
/* Regular expression used to filter subtitles */
QRegularExpression m_regex;

/* true if secondary-sub-delay is supported, false otherwise */
bool m_secSubDelaySupported = false;

#if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN)
/* The DBus cookie. Used for allowing the screen to dim again. */
uint32_t dbus_cookie;
Expand Down
13 changes: 11 additions & 2 deletions src/gui/widgets/subtitlelistwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ SubtitleListWidget::SubtitleListWidget(QWidget *parent)
);
connect(
mediator, &GlobalMediator::playerSubDelayChanged,
this, &SubtitleListWidget::updateTimestamps,
this, &SubtitleListWidget::updatePrimaryTimestamps,
Qt::QueuedConnection
);
connect(
mediator, &GlobalMediator::playerSecSubDelayChanged,
this, &SubtitleListWidget::updateSecondaryTimestamps,
Qt::QueuedConnection
);
connect(
Expand Down Expand Up @@ -702,9 +707,13 @@ void SubtitleListWidget::updateTimestampsHelper(SubtitleList &list,
list.table->resizeRowsToContents();
}

void SubtitleListWidget::updateTimestamps(const double delay)
void SubtitleListWidget::updatePrimaryTimestamps(const double delay)
{
updateTimestampsHelper(m_primary, delay);
}

void SubtitleListWidget::updateSecondaryTimestamps(const double delay)
{
updateTimestampsHelper(m_secondary, delay);
}

Expand Down
11 changes: 9 additions & 2 deletions src/gui/widgets/subtitlelistwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,18 @@ private Q_SLOTS:
double delay);

/**
* Updates the timestamps of the subtitles with a new delay.
* Updates the primary timestamps of the subtitles with a new delay.
* Subtitles with a negative timestamp will be rounded to 0.
* @param delay The signed delay.
*/
void updateTimestamps(const double delay);
void updatePrimaryTimestamps(const double delay);

/**
* Updates the secondary timestamps of the subtitles with a new delay.
* Subtitles with a negative timestamp will be rounded to 0.
* @param delay The signed delay.
*/
void updateSecondaryTimestamps(const double delay);

/**
* Resizes rows to contents.
Expand Down
4 changes: 4 additions & 0 deletions src/player/mpvadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ MpvAdapter::MpvAdapter(MpvWidget *mpv, QObject *parent)
m_mpv, &MpvWidget::subDelayChanged,
mediator, &GlobalMediator::playerSubDelayChanged
);
connect(
m_mpv, &MpvWidget::secSubDelayChanged,
mediator, &GlobalMediator::playerSecSubDelayChanged
);
connect(
m_mpv, &MpvWidget::durationChanged,
mediator, &GlobalMediator::playerDurationChanged
Expand Down
6 changes: 6 additions & 0 deletions src/util/globalmediator.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ class GlobalMediator : public QObject
*/
void playerSubDelayChanged(const double delay) const;

/**
* Emitted when the secondary subtitle delay changes.
* @param delay The secondary subtitle delay in seconds.
*/
void playerSecSubDelayChanged(const double delay) const;

/**
* Emitted when the duration of the media being played changes.
* @param value The duration in seconds.
Expand Down

0 comments on commit bb4a0fe

Please sign in to comment.