From 6a9999f76d546bb5c67add2ef4bb7e9648988f44 Mon Sep 17 00:00:00 2001 From: Jon Beniston Date: Sat, 17 Oct 2026 18:27:35 +0100 Subject: [PATCH] HbbTV: Support Polish EPG. Fix possibly crash when clearing profile. --- plugins/channelrx/demoddvb/dvbdemodgui.cpp | 13 ++- plugins/channelrx/demoddvb/dvbdemodhbbtv.cpp | 86 +++++++++++++++++--- plugins/channelrx/demoddvb/dvbdemodhbbtv.h | 2 + plugins/channelrx/demoddvb/hbbtv/oipf.js | 5 +- plugins/channelrx/demoddvb/readme.md | 6 ++ sdrgui/dtv/dtvguideoverlay.cpp | 13 ++- sdrgui/dtv/dtvguideoverlay.h | 2 + 7 files changed, 108 insertions(+), 19 deletions(-) diff --git a/plugins/channelrx/demoddvb/dvbdemodgui.cpp b/plugins/channelrx/demoddvb/dvbdemodgui.cpp index 3593ffe79..35f9a63bb 100644 --- a/plugins/channelrx/demoddvb/dvbdemodgui.cpp +++ b/plugins/channelrx/demoddvb/dvbdemodgui.cpp @@ -908,8 +908,6 @@ void DVBDemodGUI::hbbtvVideoArea(const QRectF& area) void DVBDemodGUI::hbbtvChannelChange(int networkId, int transportStreamId, int serviceId) { #ifdef DVBDEMOD_HBBTV - (void) transportStreamId; - if (serviceId == m_playingService) { m_hbbtv->channelChangeResult(true); @@ -917,9 +915,16 @@ void DVBDemodGUI::hbbtvChannelChange(int networkId, int transportStreamId, int s } m_hbbtvChannelRequest = serviceId; - selectService(networkId, serviceId, this, [this](const QString& error) { - qDebug("DVBDemodGUI::hbbtvChannelChange: %s", qPrintable(error)); + selectService(networkId, serviceId, this, [this, networkId, transportStreamId, serviceId](const QString& error) { + qDebug("DVBDemodGUI::hbbtvChannelChange: %d.%d.%d: %s", networkId, transportStreamId, serviceId, qPrintable(error)); m_hbbtvChannelRequest = 0; + // Said over the video for a while, rather than in a message box, which would take the keys from the application + // (as the user's chosen the channel in it) and be hidden by full screen video + const QString text = QString("Can't play the channel the application chose: %1").arg(error); + setStatusText(text); + setStatusToolTip(text); + m_statusMessageTimer.start(); + m_videoScreen.load()->setOverlay(text, 5.0); if (m_hbbtv) { m_hbbtv->channelChangeResult(false); diff --git a/plugins/channelrx/demoddvb/dvbdemodhbbtv.cpp b/plugins/channelrx/demoddvb/dvbdemodhbbtv.cpp index 29c380b95..a53b1550e 100644 --- a/plugins/channelrx/demoddvb/dvbdemodhbbtv.cpp +++ b/plugins/channelrx/demoddvb/dvbdemodhbbtv.cpp @@ -39,6 +39,7 @@ #endif #include #include +#include #include #include #include @@ -291,13 +292,24 @@ void DVBDemodHbbTV::releaseProfile() } } +// The profile's kept until its cache is cleared: deleted while that's in progress (as when no application's running, so +// this is its only user), it corrupts the heap void DVBDemodHbbTV::clearData() { QWebEngineProfile *profile = acquireProfile(); profile->cookieStore()->deleteAllCookies(); - profile->clearHttpCache(); profile->clearAllVisitedLinks(); - releaseProfile(); +#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0) + QObject::connect(profile, &QWebEngineProfile::clearHttpCacheCompleted, profile, []() { + // Not in the profile's own signal, as it may be deleted + QTimer::singleShot(0, []() { releaseProfile(); }); + }, Qt::SingleShotConnection); + profile->clearHttpCache(); +#else + // No signal when it's done: a while later + profile->clearHttpCache(); + QTimer::singleShot(10000, []() { releaseProfile(); }); +#endif } QWebEngineView *DVBDemodHbbTV::createView(QWidget *parent) @@ -1154,11 +1166,15 @@ void DVBDemodHbbTV::createApplication(const QString& uri) } } -// An HbbTV application served as application/vnd.hbbtv.xhtml+xml, which the browser would download rather than show: -// fetched, and given to it as HTML +// A page of the running application served as application/vnd.hbbtv.xhtml+xml, which the browser would download rather +// than show: fetched, and given to it as HTML. Its first page, or one it goes to, or one it's redirected to (as +// index.php to index.html) void DVBDemodHbbTV::downloadRequested(const QUrl& url, const QString& mimeType) { - if (!isRunning() || (url != QUrl(m_appUrl)) || !(mimeType.contains("hbbtv") || mimeType.contains("xhtml") || mimeType.contains("cehtml"))) + // Fetched without the interceptor, so the internet's only used if allowed + const bool allowed = m_interceptor->m_networkAllowed || m_server->isServerUrl(url.toString()); + + if (!isRunning() || !allowed || !(mimeType.contains("hbbtv") || mimeType.contains("xhtml") || mimeType.contains("cehtml"))) { emit log(QString("Download of %1 (%2) refused").arg(url.toString()).arg(mimeType)); return; @@ -1166,18 +1182,23 @@ void DVBDemodHbbTV::downloadRequested(const QUrl& url, const QString& mimeType) QNetworkRequest request(url); request.setHeader(QNetworkRequest::UserAgentHeader, m_profile->httpUserAgent()); + request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy); // Qt 5's default is not to follow them QNetworkReply *reply = m_network->get(request); + const QString appUrl = m_appUrl; - connect(reply, &QNetworkReply::finished, this, [this, reply, url]() { + connect(reply, &QNetworkReply::finished, this, [this, reply, url, appUrl]() { reply->deleteLater(); - if ((reply->error() != QNetworkReply::NoError) || !isRunning() || (QUrl(m_appUrl) != url)) + if ((reply->error() != QNetworkReply::NoError) || !isRunning() || (m_appUrl != appUrl)) { emit log(QString("Can't load %1: %2").arg(url.toString()).arg(reply->errorString())); return; } - m_page->setContent(reply->readAll(), "text/html", url); + + // With oipf.js in it as well: given so, a page's scripts aren't always injected (Qt WebEngine doesn't inject + // them in one given after another, as when the service changes from one application served so to another) + m_page->setContent(withScript(reply->readAll(), scriptSource()), "text/html", url); }); } @@ -1230,6 +1251,51 @@ QJsonObject DVBDemodHbbTV::configuration() const return config; } +// oipf.js, with the TV's state, as a script: escaped to ASCII (as a page's encoding may not be UTF-8), and without +// element) +QString DVBDemodHbbTV::scriptSource() const +{ + // Not by arg(): the script has %s of its own + const QString source = "window.__sdrangelHbbTVConfig = " + QString::fromUtf8(QJsonDocument(configuration()).toJson(QJsonDocument::Compact)) + + ";\n" + m_oipfScript; + QString ascii; + ascii.reserve(source.size()); + + for (const QChar c : source) + { + if (c.unicode() > 0x7e) { + ascii += QString("\\u%1").arg((uint) c.unicode(), 4, 16, QChar('0')); + } else { + ascii += c; + } + } + + return ascii.replace("" + script.toLatin1() + ""; + const QString text = QString::fromLatin1(page); // Only to find the tags, which are ASCII + int at = -1; + + for (const QString& tag : {QString("]*)?>", QRegularExpression::CaseInsensitiveOption).match(text); + + if (match.hasMatch()) + { + at = match.capturedEnd(); + break; + } + } + + QByteArray result = page; + result.insert(std::max(0, at), element); + return result; +} + // oipf.js, with the TV's state, in each page before its scripts void DVBDemodHbbTV::updateScript() { @@ -1250,8 +1316,6 @@ void DVBDemodHbbTV::updateScript() script.setInjectionPoint(QWebEngineScript::DocumentCreation); script.setWorldId(QWebEngineScript::MainWorld); script.setRunsOnSubFrames(true); - // Not by arg(): the script has %s of its own - script.setSourceCode("window.__sdrangelHbbTVConfig = " + QString::fromUtf8(QJsonDocument(configuration()).toJson(QJsonDocument::Compact)) - + ";\n" + m_oipfScript); + script.setSourceCode(scriptSource()); scripts.insert(script); } diff --git a/plugins/channelrx/demoddvb/dvbdemodhbbtv.h b/plugins/channelrx/demoddvb/dvbdemodhbbtv.h index 9cefb3a3f..dcc2645bf 100644 --- a/plugins/channelrx/demoddvb/dvbdemodhbbtv.h +++ b/plugins/channelrx/demoddvb/dvbdemodhbbtv.h @@ -165,6 +165,8 @@ private: void showView(); void loadFinished(bool ok); void updateScript(); + QString scriptSource() const; + static QByteArray withScript(const QByteArray& page, const QString& script); void showStatus(); //!< getStatus() in the page, over what the application shows void updateProxy(); QJsonObject configuration() const; diff --git a/plugins/channelrx/demoddvb/hbbtv/oipf.js b/plugins/channelrx/demoddvb/hbbtv/oipf.js index dfbf0bb5e..b12a06a22 100644 --- a/plugins/channelrx/demoddvb/hbbtv/oipf.js +++ b/plugins/channelrx/demoddvb/hbbtv/oipf.js @@ -918,14 +918,17 @@ } var channel = makeChannel(config.channel); + var requested = videoState(object).pendingChannel; videoState(object).pendingChannel = null; if (ok) { setPlayState(object, PLAYSTATE_PRESENTING); fire(object, 'ChannelChangeSucceeded', { channel: channel }, [channel]); } else { + // The channel asked for (OIPF DAE 7.13.1.3), not the one still playing + var failed = requested || channel; setPlayState(object, PLAYSTATE_UNREALIZED, error); - fire(object, 'ChannelChangeError', { channel: channel, errorState: error }, [channel, error]); + fire(object, 'ChannelChangeError', { channel: failed, errorState: error }, [failed, error]); } }); } diff --git a/plugins/channelrx/demoddvb/readme.md b/plugins/channelrx/demoddvb/readme.md index b9ac73960..5b25ada61 100644 --- a/plugins/channelrx/demoddvb/readme.md +++ b/plugins/channelrx/demoddvb/readme.md @@ -569,3 +569,9 @@ numbers (0 for the default region's). The standard is 0 for automatic (DVB-T or T2), 1 DVB-T, 2 DVB-T2, 3 DVB-T2-Lite, 4 automatic (DVB-S or S2), 5 DVB-S and 6 DVB-S2. The DVB-S and S2 report adds the symbol rate set and measured, the roll-off, and, for DVB-S2, the MODCOD, pilots, frame size, input stream, Es/N0, and frame, dummy frame and header miss counts. + +

Attribution

+ +The Freesat and Freeview Huffman tables, which decode compressed programme guide text, were generated from freesat_huffman.c of +[tvheadend](https://github.com/tvheadend/tvheadend) (GNU GPL version 3 or later), code originally written for tv_grab_dvb_plus and +integrated into tvheadend by Adam Sutton. diff --git a/sdrgui/dtv/dtvguideoverlay.cpp b/sdrgui/dtv/dtvguideoverlay.cpp index c86d5723a..7a588b49d 100644 --- a/sdrgui/dtv/dtvguideoverlay.cpp +++ b/sdrgui/dtv/dtvguideoverlay.cpp @@ -806,10 +806,17 @@ bool DTVGuideOverlay::handleMouse(QMouseEvent *event) // A double-click's second click comes as MouseButtonDblClick, after its first as a press. If that opened or closed // the guide or ribbon (or played a channel), what the second hits is something else: it's ignored, rather than - // playing whatever's now under the pointer, or leaving full screen - if (type == QEvent::MouseButtonPress) { + // playing whatever's now under the pointer, or leaving full screen. Only if that press was the double-click's + // first: the widget may have used it rather than give it here (as to give an HbbTV application the keys), leaving + // the last press here an earlier one + if (type == QEvent::MouseButtonPress) + { m_pressMode = m_mode; - } else if ((type == QEvent::MouseButtonDblClick) && (m_mode != m_pressMode)) { + m_pressTime.start(); + } + else if ((type == QEvent::MouseButtonDblClick) && (m_mode != m_pressMode) && m_pressTime.isValid() + && (m_pressTime.elapsed() <= QGuiApplication::styleHints()->mouseDoubleClickInterval())) + { return true; } diff --git a/sdrgui/dtv/dtvguideoverlay.h b/sdrgui/dtv/dtvguideoverlay.h index 43fe43a7f..483448864 100644 --- a/sdrgui/dtv/dtvguideoverlay.h +++ b/sdrgui/dtv/dtvguideoverlay.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -187,6 +188,7 @@ private: QSize m_renderSize; QString m_message; //!< Shown on the ribbon or the guide, as why a channel can't be played Mode m_pressMode; //!< Before the last mouse press, to ignore a double-click after one that opened or closed a view + QElapsedTimer m_pressTime; //!< Since the last mouse press std::vector m_hits; static constexpr int m_windowSeconds = 2 * 3600;