From c12a8ac599a44b6b6ff659bae49146b316d37126 Mon Sep 17 00:00:00 2001 From: Gwendal Date: Mon, 16 Nov 2026 18:42:01 +0100 Subject: [PATCH 1/3] fix(media): wait for the PAT and PMT of a live source in PCR time The PSI scan of a single-program live source read at most 4096 packets, which is 154 ms at 40 Mbit/s. An encoder that sends a PAT every 476 ms, within the 500 ms of ETSI TR 101 290, then failed most starts with "No PAT found at the start of the source". * On a live source, wait for the PAT up to 1 s of PCR time (kPsiWindowPcr), then for its PMT up to 1 s after the PAT. * Cap the single-program live scan, which keeps what it reads, at 106,400 packets (2 s at 80 Mbit/s, about 20 MB). A source without a PCR stops at that cap. * A program of a multiplex (timed windows) now also waits up to 1 s after the PAT for its PMT, where it stopped 1 s after the first PCR. Its 600,000-packet cap and stall error are unchanged. * Keep the 4096-packet scan for seekable files. Co-Authored-By: Claude Opus 5.5 --- docs/CLI.md | 7 +-- src/media/M2tsPacketizer.cpp | 29 +++++++++--- src/media/M2tsPacketizer.h | 5 +- tests/m2ts_packetizer_test.cpp | 87 ++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 12 deletions(-) diff --git a/docs/CLI.md b/docs/CLI.md index a0bf000..eae375c 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -487,6 +487,7 @@ no longer produced. automatically and advertised as `isLive: true`; VOD duration probing is skipped (it would otherwise open and consume the pipe a second time). - **Per-program mode over a pipe** works too: the PAT/PMT init scan is buffered and - replayed so no leading packets are lost. If the source never emits PAT/PMT within - the first ~4096 packets, per-program init fails with a clear error - use - `--unmodified` for such feeds. + replayed so no leading packets are lost. The scan waits for the PAT up to 1 s of + PCR time, then for its PMT up to 1 s after the PAT. A source without a PCR is + read up to 106,400 packets, about 20 MB (160 s at 1 Mbit/s). If the tables do not arrive, + per-program init fails with a clear error - use `--unmodified` for such feeds. diff --git a/src/media/M2tsPacketizer.cpp b/src/media/M2tsPacketizer.cpp index b852416..04e26da 100644 --- a/src/media/M2tsPacketizer.cpp +++ b/src/media/M2tsPacketizer.cpp @@ -26,8 +26,11 @@ constexpr std::int64_t kPcrHz = 27000000; constexpr std::int64_t kPsiWindowPcr = 1 * kPcrHz; // twice the 500 ms PSI limit of ETSI TR 101 290 constexpr std::int64_t kRapWindowPcr = 10 * kPcrHz; constexpr int kTimedWindowMaxPackets = 600000; -// A window whose PCR does not advance for this long ends with an error: the -// packets of about 2 s at 80 Mbit/s, or 2 s of wall time on a slower source. +// The PSI scan of a live source of one program keeps the packets it reads, so +// it reads at most its two windows at 80 Mbit/s (about 20 MB). +constexpr int kLivePsiMaxPackets = 106400; +// A timed window whose PCR does not advance for this long ends with an error: +// the packets of about 2 s at 80 Mbit/s, or 2 s of wall time on a slower source. constexpr int kPcrStallPackets = 100000; constexpr auto kPcrStallTime = std::chrono::seconds(2); // The largest step between two PCRs of a PID that counts as time: ISO/IEC @@ -696,14 +699,23 @@ bool M2tsPacketizer::scanPsi(QString* error) { return fail(QStringLiteral("Failed to seek M2TS source while reading the PAT and PMT.")); } - // A file, or the source of one program, reads 4096 packets at most. A - // program of a live multiplex reads the whole multiplex, so its window is - // PCR time. + // A file reads 4096 packets at most. A live source waits for the PAT up to + // kPsiWindowPcr of PCR time, then for the PMT up to kPsiWindowPcr after the + // PAT (ETSI TR 101 290 allows 500 ms between two of each), since a fixed + // number of packets is a shorter time at a higher rate. A live source of + // one program without an advancing PCR stops at kLivePsiMaxPackets. constexpr int maxPacketsToScan = 4096; PcrSpan span(-1); + std::int64_t patAt = -1; // PCR time of the first PAT read const auto inWindow = [&](int index) { - return m_timedStartWindows ? index < kTimedWindowMaxPackets && span.elapsed() < kPsiWindowPcr - : index < maxPacketsToScan; + if (!m_sequential && !m_timedStartWindows) { + return index < maxPacketsToScan; + } + const std::int64_t limit = patAt < 0 ? kPsiWindowPcr : patAt + kPsiWindowPcr; + if (m_timedStartWindows) { + return index < kTimedWindowMaxPackets && span.elapsed() < limit; + } + return index < kLivePsiMaxPackets && span.elapsed() < limit; }; for (int index = 0; inWindow(index) && !psiComplete(); ++index) { const QByteArray sourcePacket = readSourcePacket(); @@ -725,6 +737,9 @@ bool M2tsPacketizer::scanPsi(QString* error) { const QByteArray tsPacket = tsPacketView(sourcePacket); span.add(pidOf(tsPacket), tsPacket); handlePsiPacket(tsPacket, sourcePacket); + if (patAt < 0 && !m_pat.bytes.isEmpty()) { + patAt = span.elapsed(); + } if (m_timedStartWindows && !psiComplete() && span.stalled()) { return fail(QStringLiteral("No advancing PCR in the live source while reading the PAT and PMT; the " "start-up windows of a program of a multiplex are PCR time.")); diff --git a/src/media/M2tsPacketizer.h b/src/media/M2tsPacketizer.h index b24e3c9..8caa6ca 100644 --- a/src/media/M2tsPacketizer.h +++ b/src/media/M2tsPacketizer.h @@ -233,8 +233,9 @@ class M2tsPacketizer final { private: bool detectPacketSize(QString* error); - // Reads up to 4096 packets (or 1 s of PCR time, see setTimedStartWindows) - // for the PAT and the selected program's PMT. + // Reads the PAT and the selected program's PMT: up to 4096 packets of a + // file; on a live source, up to 1 s of PCR time for the PAT, then 1 s + // after it for the PMT. bool scanPsi(QString* error); void handlePsiPacket(const QByteArray& tsPacket, const QByteArray& sourcePacket); void onPat(const PsiAssembler::Section& section); diff --git a/tests/m2ts_packetizer_test.cpp b/tests/m2ts_packetizer_test.cpp index 301b2b3..ca2d641 100644 --- a/tests/m2ts_packetizer_test.cpp +++ b/tests/m2ts_packetizer_test.cpp @@ -1674,6 +1674,93 @@ int main() { } } + // The PSI scan of a live source waits for the PAT, up to 1 s of PCR time, + // then for its PMT, up to 1 s after the PAT (ETSI TR 101 290 allows 500 ms + // between two of each). 4096 packets are only 154 ms at 40 Mbit/s. The + // source runs at 40 Mbit/s (532 packets per 20 ms PCR step) for 3 s and + // starts just after a PAT. + // The same windows apply to a program of a multiplex (timed windows, + // per-program carriage), and a PCR that jumps back at the start only + // stretches the window. + struct PsiCase { + std::string label; + double patFirst; // s: the first PAT, then one every 476 ms; < 0: none + double pmtDelay; // s: each PMT after its PAT + bool timed; // timed windows and per-program carriage of program 1 + double pcrJump; // s: the PCR restarts from 0 at this time; < 0: never + }; + for (const PsiCase& c : {PsiCase{"live PSI, PAT at 476 ms: ", 0.476, 0.0, false, -1.0}, + PsiCase{"live PSI, PMT 450 ms after a PAT at 950 ms: ", 0.95, 0.45, false, -1.0}, + PsiCase{"live PSI, no PAT: ", -1.0, 0.0, false, -1.0}, + PsiCase{"timed PSI, PMT 450 ms after a PAT at 950 ms: ", 0.95, 0.45, true, -1.0}, + PsiCase{"live PSI, PCR back to 0 at 100 ms: ", 0.476, 0.0, false, 0.1}}) { + QByteArray ts; + int videoCc = 0; + int psiCc = 0; + double nextPat = c.patFirst; + double nextPmt = c.patFirst < 0 ? -1.0 : c.patFirst + c.pmtDelay; + for (int step = 0; step < 150; ++step) { + const double t = step * 0.02; + const double pcrT = c.pcrJump >= 0 && t >= c.pcrJump ? t - c.pcrJump : t + (c.pcrJump >= 0 ? 10.0 : 0.0); + ts += pcrPacket(0x100, static_cast(pcrT * 27e6), videoCc++); + int left = 531; + if (nextPat >= 0 && t >= nextPat) { + ts += tb::psiPacket(0x0000, tb::patSection({{1, 0x1000}}), psiCc); + nextPat += 0.476; + --left; + } + if (nextPmt >= 0 && t >= nextPmt) { + ts += tb::psiPacket(0x1000, tb::pmtSection(1, 0x100, {{0x1B, 0x100}}), psiCc++); + ts += tb::tsPacket(0x100, true, tb::pesHeaderWithPts(static_cast(t * 90000)), + videoCc++, 0x40); + nextPmt += 0.476; + left -= 2; + } + for (int k = 0; k < left; ++k) { + ts += tb::tsPacket(0x100, false, QByteArray(184, char(k)), videoCc++); + } + } + auto device = std::make_shared(ts); + M2tsPacketizer packetizer(device, QStringLiteral("live")); + packetizer.setTransparent(!c.timed); + packetizer.setTimedStartWindows(c.timed); + QString error; + const bool opened = packetizer.open(c.timed ? 1 : 0, &error); + if (c.patFirst >= 0) { + ok &= expect(opened, c.label + "opens: " + error.toStdString()); + ok &= expect(packetizer.pcrPid() == 0x100 && !packetizer.initData().isEmpty(), + c.label + "reads the PMT, so initData carries it"); + } else { + const qint64 read = ts.size() - device->bytesAvailable(); + ok &= expect(!opened && read <= ts.size() / 2, + c.label + "fails after about 1 s, read " + std::to_string(read / 188) + " packets"); + } + } + + // The scan keeps what it reads, so a live source whose PCR never ends the + // window (none at all, or one that advances too slowly) stops at the + // packet cap (106,400 packets). + for (const bool withPcr : {false, true}) { + const std::string label = withPcr ? "live PSI, slow PCR: " : "live PSI, no PCR: "; + QByteArray ts; + int videoCc = 0; + for (int step = 0; step < 240; ++step) { + ts += withPcr ? pcrPacket(0x100, step * 27, videoCc++) // 1 us per step + : tb::tsPacket(0x100, false, QByteArray(184, char(0)), videoCc++); + for (int k = 0; k < 499; ++k) { + ts += tb::tsPacket(0x100, false, QByteArray(184, char(k)), videoCc++); + } + } + auto device = std::make_shared(ts); + M2tsPacketizer packetizer(device, QStringLiteral("live")); + packetizer.setTransparent(true); + QString error; + const bool opened = packetizer.open(0, &error); + const qint64 read = (ts.size() - device->bytesAvailable()) / 188; + ok &= expect(!opened && read >= 106400 && read < 107000, + label + "stops at the packet cap, read " + std::to_string(read) + " packets"); + } + // Timed Groups for the sources that #40 left in Group 0 (#49), on 20 s of // a live source (the PCR advances 0.1 s per round): // - audio: an audio-only program whose PCR travels on a PID of its own. From a993891ae17b7d11694f00af46581d8bb2a977fb Mon Sep 17 00:00:00 2001 From: Gwendal Date: Tue, 17 Nov 2026 23:21:08 +0100 Subject: [PATCH 2/3] fix(mpts): publish the programs that open when one does not In a multiplex, a program that failed to open at start-up stopped every program, for example a service that the PAT lists while its encoder is down. After start-up, a read error already ends only its own program. Start-up now treats one dead service the same way (#62). * MptsPipeline leaves out a program that does not open, reports it once with programEnded(), and publishes the others. The catalog lists the opened programs only. The CLI logs the reason and exits 1 at the end, as for a program that ends early. * Only when no program opens does the start fail, with the first program's error, as before. * "strict_start": true in the --config file keeps the old start: one program that does not open fails every program. --config takes no other option, so the setting is a key of the file. * M2tsPacketizer names a missing PMT: "The PAT lists program N, but its PMT (PID P) did not come within 1000 ms of PCR time after the PAT." It replaces the generic initData message only when the PAT listed the program and the source did not end. * Tests: a program missing from the PAT (the others publish, one programEnded, a two-program catalog), the same with strict_start (fails as before), no program open (fails), a PMT that never comes (the other program publishes), and the strict_start key in the configuration parser. The missing-program test now expects the new start. Co-Authored-By: Claude Opus 5.5 --- docs/CLI.md | 6 +++ src/app/PublisherSpec.cpp | 10 ++++- src/app/PublisherSpec.h | 4 +- src/cli/main_cli.cpp | 1 + src/media/M2tsPacketizer.cpp | 9 +++++ src/media/MptsPipeline.cpp | 33 +++++++++++++--- src/media/MptsPipeline.h | 12 ++++-- tests/mpts_pipeline_test.cpp | 71 ++++++++++++++++++++++++++++++++--- tests/publisher_spec_test.cpp | 16 ++++++++ 9 files changed, 146 insertions(+), 16 deletions(-) diff --git a/docs/CLI.md b/docs/CLI.md index eae375c..9b2581c 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -331,10 +331,16 @@ macOS only; a Windows build rejects it. | `segment_bytes` | `65536` | As `--segment-bytes`, from 1 to 1048576 (1 MiB) | | `timeline` | `true` | `false` publishes no media timeline track, as `--no-timeline` | | `accessible_window_ms` | `300000` | As `--accessible-window`, in milliseconds, from 0 to 2000000000 | +| `strict_start` | `false` | `true` stops every program when one does not open at start-up | `ingest` holds exactly one of `srt` or `path`. An unknown key is refused. An invalid file exits with code 2, and the error names the field and the value. A program that ends, or whose read fails, ends its own track; the others go on. +A program that does not open at start-up, because the PAT does not list it or +its PMT does not come within 1 s of PCR time, is logged as an error and left +out of the catalog; the others publish, and the exit code is 1. With +`strict_start`, it stops every program instead. Only when no program opens does +the start fail. The tracks start at the live edge. While the relay has no subscriber, moq2ts keeps reading the source and drops its oldest packets. When the first diff --git a/src/app/PublisherSpec.cpp b/src/app/PublisherSpec.cpp index 63f0d1d..6b29a59 100644 --- a/src/app/PublisherSpec.cpp +++ b/src/app/PublisherSpec.cpp @@ -290,7 +290,8 @@ bool parsePublisherSpec(const QByteArray& json, PublisherSpec* out, QString* err const QJsonObject root = document.object(); if (!checkKeys(root, QString(), {QStringLiteral("relay"), QStringLiteral("ingest"), QStringLiteral("programs"), - QStringLiteral("segment_bytes"), QStringLiteral("timeline"), QStringLiteral("accessible_window_ms")}, + QStringLiteral("segment_bytes"), QStringLiteral("timeline"), QStringLiteral("accessible_window_ms"), + QStringLiteral("strict_start")}, error)) { return false; } @@ -315,6 +316,13 @@ bool parsePublisherSpec(const QByteArray& json, PublisherSpec* out, QString* err } spec.relay.timelineTrack = timeline.toBool(); } + const QJsonValue strictStart = root.value(QStringLiteral("strict_start")); + if (!strictStart.isUndefined()) { + if (!strictStart.isBool()) { + return fail(error, QStringLiteral("strict_start: %1 is not true or false.").arg(jsonText(strictStart))); + } + spec.strictStart = strictStart.toBool(); + } if (out != nullptr) { *out = spec; } diff --git a/src/app/PublisherSpec.h b/src/app/PublisherSpec.h index 76bd121..65faf23 100644 --- a/src/app/PublisherSpec.h +++ b/src/app/PublisherSpec.h @@ -30,6 +30,8 @@ struct PublisherSpec { QString sourcePath; QList programs; int segmentBytes = 64 * 1024; + // MptsConfig::strictStart. + bool strictStart = false; }; // Reads a publisher configuration: @@ -40,7 +42,7 @@ struct PublisherSpec { // "programs": [ { "number": 101, "mux_rate_bps": 6000000, "si": ["sdt", "eit-pf", "tdt-tot"] }, // { "number": 102 } ], // "segment_bytes": 65536, -// "timeline": true, "accessible_window_ms": 300000 +// "timeline": true, "accessible_window_ms": 300000, "strict_start": false // } // "ingest" holds exactly one of "srt" or "path" (a FIFO). An unknown key // fails. On failure, error names the field and the value, and out is left diff --git a/src/cli/main_cli.cpp b/src/cli/main_cli.cpp index 4514594..c65c62b 100644 --- a/src/cli/main_cli.cpp +++ b/src/cli/main_cli.cpp @@ -153,6 +153,7 @@ int runConfig(QCoreApplication& app, const QString& path) { mpts.sourcePath = spec.sourcePath; mpts.programs = spec.programs; mpts.targetSegmentBytes = spec.segmentBytes; + mpts.strictStart = spec.strictStart; const bool useSrt = !spec.srt.mode.isEmpty(); #ifndef MOQ2TS_HAS_SRT diff --git a/src/media/M2tsPacketizer.cpp b/src/media/M2tsPacketizer.cpp index 04e26da..e8ebaff 100644 --- a/src/media/M2tsPacketizer.cpp +++ b/src/media/M2tsPacketizer.cpp @@ -717,9 +717,11 @@ bool M2tsPacketizer::scanPsi(QString* error) { } return index < kLivePsiMaxPackets && span.elapsed() < limit; }; + bool sourceEnded = false; for (int index = 0; inWindow(index) && !psiComplete(); ++index) { const QByteArray sourcePacket = readSourcePacket(); if (sourcePacket.size() != m_packetSize) { + sourceEnded = true; break; } // On non-seekable input we cannot rewind after the scan, so retain every @@ -754,6 +756,13 @@ bool M2tsPacketizer::scanPsi(QString* error) { if (m_pmtPid < 0 && m_requestedProgramNumber != 0 && !m_patPrograms.empty()) { return fail(QStringLiteral("Requested program %1 was not found in PAT.").arg(m_requestedProgramNumber)); } + if (m_pmt.bytes.isEmpty() && m_pmtPid >= 0 && patAt >= 0 && !sourceEnded) { + return fail(QStringLiteral("The PAT lists program %1, but its PMT (PID %2) did not come within %3 ms of PCR " + "time after the PAT.") + .arg(m_requestedProgramNumber) + .arg(m_pmtPid) + .arg(kPsiWindowPcr * 1000 / kPcrHz)); + } if (m_pmt.bytes.isEmpty()) { return fail(QStringLiteral("Failed to collect PAT/PMT packets for catalog initData.")); } diff --git a/src/media/MptsPipeline.cpp b/src/media/MptsPipeline.cpp index f7fdd80..e64efe2 100644 --- a/src/media/MptsPipeline.cpp +++ b/src/media/MptsPipeline.cpp @@ -405,20 +405,43 @@ void MptsPipeline::run() { opener.join(); } } + // A program that did not open is left out and reported, and the others + // publish. Strict start, or no program open, fails as one error. + std::vector> openPrograms; + QStringList notOpened; for (std::size_t index = 0; index < programs.size(); ++index) { Program* program = programs[index].get(); const M2tsPacketizer& packetizer = *program->packetizer; if (!programOpened[index]) { // A PAT that was read and does not list the program. - if (packetizer.patProgramCount() > 0 && packetizer.pmtPid() < 0) { - fail(QStringLiteral("program %1 is not in the source PAT").arg(program->config.programNumber)); - } else { - fail(QStringLiteral("program %1: %2").arg(program->config.programNumber).arg(openErrors[index])); + const QString why = packetizer.patProgramCount() > 0 && packetizer.pmtPid() < 0 + ? QStringLiteral("program %1 is not in the source PAT").arg(program->config.programNumber) + : QStringLiteral("program %1: %2").arg(program->config.programNumber).arg(openErrors[index]); + if (m_config.strictStart) { + fail(why); + return; } - return; + notOpened.append(why); + continue; } program->packetsPerObject = std::max(1, m_config.targetSegmentBytes / packetizer.packetSize()); + openPrograms.push_back(std::move(programs[index])); + } + if (openPrograms.empty()) { + fail(notOpened.front()); + return; + } + for (std::size_t index = 0; index < programs.size(); ++index) { + if (programs[index]) { + QString reason = notOpened.takeFirst(); + if (!reason.endsWith(QLatin1Char('.'))) { + reason += QLatin1Char('.'); + } + emit programEnded(programs[index]->config.programNumber, + reason + QStringLiteral(" The other programs publish without it.")); + } } + programs = std::move(openPrograms); // One catalog entry per program, as LivePipeline builds a live per-program // track. diff --git a/src/media/MptsPipeline.h b/src/media/MptsPipeline.h index c954fdf..f398e5d 100644 --- a/src/media/MptsPipeline.h +++ b/src/media/MptsPipeline.h @@ -38,6 +38,9 @@ struct MptsConfig { int targetSegmentBytes = 64 * 1024; // Where SRT ingest skipped ahead in the source; null outside SRT ingest. std::shared_ptr sourceGaps; + // A program that does not open at start-up fails every program. Without + // it, the others publish and programEnded() reports that one. + bool strictStart = false; }; // Publishes several programs of one live multi-program source in one MOQT @@ -63,8 +66,8 @@ class MptsPipeline final : public QObject { ~MptsPipeline() override; // Starts the pipeline on a thread of its own and returns. Start-up errors - // (a seekable source, a program missing from the PAT) come as error(), and - // running() then turns false. The publisher (a MoqxrPublisher, or a + // (a seekable source, no program open, or with strictStart one program not + // open) come as error(), and running() then turns false. The publisher (a MoqxrPublisher, or a // stand-in in tests) must be connected. void start(const PublishConfig& relay, const MptsConfig& config, IMoqOutput* publisher); // Stops the source, the workers, and the publish loop, and joins them. A @@ -82,8 +85,9 @@ class MptsPipeline final : public QObject { void status(const QString& message); // A start-up or publish error, which ends the pipeline. void error(const QString& message); - // A read error of one program, which ends that program's track only; the - // session goes on with the other programs. + // A program that did not open at start-up, or a read error of one program, + // which ends that program's track only; the session goes on with the + // other programs. void programEnded(int program, const QString& reason); // The publisher's first pull: every program queue dropped what it held, // so what the source sends from now on is what the tracks start with. diff --git a/tests/mpts_pipeline_test.cpp b/tests/mpts_pipeline_test.cpp index ce1fc36..95ec860 100644 --- a/tests/mpts_pipeline_test.cpp +++ b/tests/mpts_pipeline_test.cpp @@ -314,6 +314,7 @@ using Feed = std::function sourceGaps; + bool strictStart = false; std::function relay; std::function& running)> beforeFirstPull; }; @@ -338,6 +339,7 @@ Run publish(const QString& source, const QList& programs, con config.programs = programs; config.targetSegmentBytes = 4 * 188; config.sourceGaps = options.sourceGaps; + config.strictStart = options.strictStart; Run run; std::mutex errorMutex; @@ -494,6 +496,18 @@ int pidOf(const QByteArray& packet) { } // The flags octet of the adaptation field, or 0 without one. +// data without the packets of these PIDs. +QByteArray withoutPids(const QByteArray& data, const QList& pids) { + QByteArray out; + for (qsizetype offset = 0; offset + 188 <= data.size(); offset += 188) { + const QByteArray packet = data.mid(offset, 188); + if (!pids.contains(pidOf(packet))) { + out += packet; + } + } + return out; +} + int adaptationFlags(const QByteArray& packet) { const bool adaptation = ((static_cast(packet[3]) >> 4) & 0x03) >= 2; return adaptation && static_cast(packet[4]) >= 1 ? static_cast(packet[5]) : 0; @@ -620,14 +634,61 @@ int main() { "low mux rate: one warning, for program 102: " + joined(below)); } - // Program 103 missing from the PAT: start fails with an error naming 103. + // Program 103 missing from the PAT: programs 101 and 102 publish, and + // programEnded reports 103 once, before anything is published. { const Run run = publish(dir.filePath("missing.fifo"), {program(101), program(102), program(103)}, - mptsStream({101, 102}, 60)); - ok &= expect(run.finished, "missing program: the pipeline stops"); + QByteArray(), liveFeed(mptsStream({101, 102}, 60), roundsBytes(2, 15))); + ok &= expect(run.finished, "missing program: the pipeline ends with the source"); + ok &= expect(run.errors.isEmpty(), "missing program: no pipeline error: " + joined(run.errors)); + ok &= expect(run.ended == QStringList({"103: program 103 is not in the source PAT. The other programs " + "publish without it."}), + "missing program: programEnded names 103 once: " + joined(run.ended)); + const QStringList tracks = publishedTracks(run.log); + ok &= expect(tracks.count(QStringLiteral("program-101")) > 0 && tracks.count(QStringLiteral("program-102")) > 0 && + !tracks.contains(QStringLiteral("program-103")), + "missing program: 101 and 102 publish, 103 does not"); + ok &= expect(catalogTracks(run.catalog) == QStringList({"program-101", "program-101.timeline", "program-102", + "program-102.timeline"}), + "missing program: the catalog lists 101 and 102 only: " + run.catalog.toStdString()); + } + + // The same with strict_start: start fails with an error naming 103, and + // nothing is published, as before. + { + const Run run = publish(dir.filePath("missing-strict.fifo"), {program(101), program(102), program(103)}, + mptsStream({101, 102}, 60), {}, {}, Options{.strictStart = true}); + ok &= expect(run.finished, "strict start: the pipeline stops"); + ok &= expect(run.errors.contains(QStringLiteral("program 103 is not in the source PAT")), + "strict start: the error names 103: " + joined(run.errors)); + ok &= expect(publishedTracks(run.log).isEmpty(), "strict start: nothing is published"); + } + + // No program opens: start fails, as before. + { + const Run run = publish(dir.filePath("none.fifo"), {program(103), program(104)}, mptsStream({101, 102}, 60)); + ok &= expect(run.finished, "no program opens: the pipeline stops"); ok &= expect(run.errors.contains(QStringLiteral("program 103 is not in the source PAT")), - "missing program: the error names 103: " + joined(run.errors)); - ok &= expect(publishedTracks(run.log).isEmpty(), "missing program: nothing is published"); + "no program opens: the error names the first program: " + joined(run.errors)); + ok &= expect(run.ended.isEmpty(), "no program opens: no programEnded: " + joined(run.ended)); + ok &= expect(publishedTracks(run.log).isEmpty(), "no program opens: nothing is published"); + } + + // The PAT lists program 102, but its PMT never comes, as when its encoder + // is down: 101 publishes, and programEnded says that 102's PMT is missing. + { + const QByteArray data = withoutPids(mptsStream({101, 102}, 60), {0x1001, 0x101}); + const Run run = publish(dir.filePath("no-pmt.fifo"), {program(101), program(102)}, QByteArray(), + liveFeed(data, roundsBytes(1, 15))); + ok &= expect(run.finished, "missing PMT: the pipeline ends with the source"); + ok &= expect(run.errors.isEmpty(), "missing PMT: no pipeline error: " + joined(run.errors)); + ok &= expect(run.ended == QStringList({"102: program 102: The PAT lists program 102, but its PMT (PID 4097) " + "did not come within 1000 ms of PCR time after the PAT. The other " + "programs publish without it."}), + "missing PMT: programEnded names 102 and its PMT: " + joined(run.ended)); + const QStringList tracks = publishedTracks(run.log); + ok &= expect(tracks.count(QStringLiteral("program-101")) > 0 && !tracks.contains(QStringLiteral("program-102")), + "missing PMT: 101 publishes, 102 does not"); } // Program 101's PMT changes: one new catalog Object, and program 102 keeps diff --git a/tests/publisher_spec_test.cpp b/tests/publisher_spec_test.cpp index 87526da..ecbdc08 100644 --- a/tests/publisher_spec_test.cpp +++ b/tests/publisher_spec_test.cpp @@ -160,6 +160,22 @@ int main() { {QStringLiteral("accessible_window_ms: -1 is not an integer from 0 to 2000000000.")}, "a negative accessible window"); + // strict_start: off by default, so a program that does not open leaves the + // others publishing; true stops every program, as before. + { + PublisherSpec spec; + QString error; + ok &= expect(parsePublisherSpec(kExample, &spec, &error) && !spec.strictStart, + "strict_start defaults to off: " + error.toStdString()); + ok &= expect(parsePublisherSpec(exampleWith(R"("segment_bytes": 65536)", + R"("segment_bytes": 65536, "strict_start": true)"), + &spec, &error) && + spec.strictStart, + "strict_start parses: " + error.toStdString()); + } + ok &= rejects(exampleWith(R"("segment_bytes": 65536)", R"("segment_bytes": 65536, "strict_start": 1)"), + {QStringLiteral("strict_start: 1 is not true or false.")}, "a strict_start that is not a boolean"); + // The relay authorization token, opaque to moq2ts: a value or a file, of // the token type that the relay expects (16 by default, as moqx). { From 1f3d016e1089ec22bc548e0cc322e3759ece2e9b Mon Sep 17 00:00:00 2001 From: Gwendal Date: Wed, 18 Nov 2026 15:16:36 +0100 Subject: [PATCH 3/3] feat(mpts): let a late program join the catalog when it opens After #63, a multiplex program that did not open at start-up stayed out for the whole session. It now keeps waiting and joins: when its PAT and PMT come, a new catalog lists it before its first Object, as a PSI change does (#62). * A program that does not open gets a thread that opens it again on its own queue until it opens, its source ends, or the pipeline stops. Each attempt goes on from the last one's offset in the queue's stream, which the first pull's live edge uses. * Shared keeps every program's catalog entry and lists the opened ones only; join() adds an entry and queues the new catalog. A program still waiting counts as live, so the broadcast does not end while it waits, and a clean end of its source ends it cleanly. * Every configured track is declared to the publisher from the start, because moqxr's track list is fixed per connection. * While it waits, the program is reported as a status, "... It joins the catalog when it opens.", then "program N joined the catalog.". One that never opened by the end of its source is reported once by programEnded(), with the reason of its last full attempt and "It never joined the catalog.", so the exit status stays 1 as in #63; the other programs' broadcast still ends cleanly. An interrupt reports nothing. * PacketQueueDevice::ended() tells a waiting program that its source ended. * Tests: a PMT that comes at round 20 (101 publishes from the start; 102 joins with a catalog update before its first Object, from round 20-32, since open() measures the rates over 1 s of PCR time; one update and one closing catalog, no programEnded); the missing-program and missing-PMT cases now expect the status, then one programEnded at the end of the source. Co-Authored-By: Claude Opus 5.5 --- docs/CLI.md | 8 +- src/media/MptsPipeline.cpp | 278 +++++++++++++++++++++----------- src/media/MptsPipeline.h | 9 +- src/media/PacketQueueDevice.cpp | 5 + src/media/PacketQueueDevice.h | 3 + tests/mpts_pipeline_test.cpp | 77 +++++++-- 6 files changed, 268 insertions(+), 112 deletions(-) diff --git a/docs/CLI.md b/docs/CLI.md index 9b2581c..fce5cb2 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -337,8 +337,12 @@ macOS only; a Windows build rejects it. invalid file exits with code 2, and the error names the field and the value. A program that ends, or whose read fails, ends its own track; the others go on. A program that does not open at start-up, because the PAT does not list it or -its PMT does not come within 1 s of PCR time, is logged as an error and left -out of the catalog; the others publish, and the exit code is 1. With +its PMT does not come within 1 s of PCR time, is logged and left out of the +catalog while the others publish. It keeps waiting, and when its PAT and PMT +come, it joins: a new catalog lists it before its first Object. A program that +never opened by the end of the source is logged as an error then, and the exit +code is 1. While it waits, each attempt reads about 1 s of the source's PCR +time. With `strict_start`, it stops every program instead. Only when no program opens does the start fail. diff --git a/src/media/MptsPipeline.cpp b/src/media/MptsPipeline.cpp index e64efe2..cfe9e6c 100644 --- a/src/media/MptsPipeline.cpp +++ b/src/media/MptsPipeline.cpp @@ -63,6 +63,7 @@ struct MptsPipeline::Shared { int liveWorkers = 0; bool stopping = false; QList catalogs; // one per program, in configuration order + std::vector listed; // per program: opened, so in the catalog QByteArray catalogJson; std::uint64_t catalogGroupId = 0; // The queue of each program, and from the first pull on, the offset in @@ -162,11 +163,12 @@ struct MptsPipeline::Shared { // interrupt, which commit to nothing about the content that follows. // moqxr ends every other track before it sends this one. std::optional endBroadcast() { - if (unclean || broadcastEnded || catalogs.isEmpty() || !catalogs.first().isLive) { + const QList current = listedCatalogs(); + if (unclean || broadcastEnded || current.isEmpty() || !current.first().isLive) { return std::nullopt; } broadcastEnded = true; - MsftsCatalog closing = catalogs.first(); + MsftsCatalog closing = current.first(); closing.generatedAtMs = QDateTime::currentMSecsSinceEpoch(); PublishedObject catalog; catalog.trackName = QLatin1String(kCatalogTrackName); @@ -179,6 +181,33 @@ struct MptsPipeline::Shared { return catalog; } + // The catalog entries of the programs that opened, in configuration order. + QList listedCatalogs() const { + QList current; + for (qsizetype index = 0; index < catalogs.size(); ++index) { + if (listed[static_cast(index)]) { + current.append(catalogs[index]); + } + } + return current; + } + + // A program that opened late joins the catalog: a new catalog in the next + // catalog Group, before any of its Objects, as a PSI change. Waits for + // room; false once stopping. + bool join(int program, const MsftsCatalog& entry) { + std::unique_lock lock(mutex); + notFull.wait(lock, [&] { return stopping || queue.size() + 1 <= capacity; }); + if (stopping) { + return false; + } + catalogs[program] = entry; + listed[static_cast(program)] = true; + queue.push_back(updateCatalog(program, entry.initData)); + notEmpty.notify_one(); + return true; + } + void workerEnded(bool clean) { std::lock_guard lock(mutex); unclean = unclean || !clean; @@ -202,7 +231,7 @@ struct MptsPipeline::Shared { for (MsftsCatalog& catalog : catalogs) { catalog.generatedAtMs = now; } - catalogJson = MsftsMuxer::catalogJson(catalogs); + catalogJson = MsftsMuxer::catalogJson(listedCatalogs()); PublishedObject catalog; catalog.trackName = QLatin1String(kCatalogTrackName); catalog.payload = catalogJson; @@ -379,6 +408,24 @@ void MptsPipeline::run() { return; } + // A packetizer for a program's queue, one per open attempt. + const auto newPacketizer = [](Program* program) { + auto packetizer = std::make_shared(program->device, program->track); + // Draft "Per-Program": the program's own SI selection, none when empty. + packetizer->setSiTables(program->config.siTables); + packetizer->setSourceGaps(program->device->gaps()); + // It reads the whole multiplex, so its start-up windows are PCR time. + packetizer->setTimedStartWindows(true); + return packetizer; + }; + // Why a program did not open. + const auto openFailure = [](const Program& program, const QString& error) { + // A PAT that was read and does not list the program. + return program.packetizer->patProgramCount() > 0 && program.packetizer->pmtPid() < 0 + ? QStringLiteral("program %1 is not in the source PAT").arg(program.config.programNumber) + : QStringLiteral("program %1: %2").arg(program.config.programNumber).arg(error); + }; + // Every program opens at once, each on a thread of its own: an open reads // up to 10 s of the source for a random access point, and one after the // other, they would add up while the program queues not yet read drop @@ -390,13 +437,7 @@ void MptsPipeline::run() { std::vector openers; for (std::size_t index = 0; index < programs.size(); ++index) { Program* program = programs[index].get(); - auto packetizer = std::make_shared(program->device, program->track); - // Draft "Per-Program": the program's own SI selection, none when empty. - packetizer->setSiTables(program->config.siTables); - packetizer->setSourceGaps(program->device->gaps()); - // It reads the whole multiplex, so its start-up windows are PCR time. - packetizer->setTimedStartWindows(true); - program->packetizer = packetizer; + program->packetizer = newPacketizer(program); openers.emplace_back([program, &openErrors, &programOpened, index] { programOpened[index] = program->packetizer->open(program->config.programNumber, &openErrors[index]); }); @@ -405,49 +446,37 @@ void MptsPipeline::run() { opener.join(); } } - // A program that did not open is left out and reported, and the others - // publish. Strict start, or no program open, fails as one error. - std::vector> openPrograms; - QStringList notOpened; + // A program that did not open keeps waiting for its PAT and PMT, and joins + // the catalog when it opens; the others publish now. Strict start, or no + // program open, fails as one error. + std::vector notOpened(programs.size()); for (std::size_t index = 0; index < programs.size(); ++index) { - Program* program = programs[index].get(); - const M2tsPacketizer& packetizer = *program->packetizer; if (!programOpened[index]) { - // A PAT that was read and does not list the program. - const QString why = packetizer.patProgramCount() > 0 && packetizer.pmtPid() < 0 - ? QStringLiteral("program %1 is not in the source PAT").arg(program->config.programNumber) - : QStringLiteral("program %1: %2").arg(program->config.programNumber).arg(openErrors[index]); + notOpened[index] = openFailure(*programs[index], openErrors[index]); if (m_config.strictStart) { - fail(why); + fail(notOpened[index]); return; } - notOpened.append(why); - continue; } - program->packetsPerObject = std::max(1, m_config.targetSegmentBytes / packetizer.packetSize()); - openPrograms.push_back(std::move(programs[index])); } - if (openPrograms.empty()) { + if (std::find(programOpened.begin(), programOpened.end(), 1) == programOpened.end()) { fail(notOpened.front()); return; } for (std::size_t index = 0; index < programs.size(); ++index) { - if (programs[index]) { - QString reason = notOpened.takeFirst(); + if (!programOpened[index]) { + QString reason = notOpened[index]; if (!reason.endsWith(QLatin1Char('.'))) { reason += QLatin1Char('.'); } - emit programEnded(programs[index]->config.programNumber, - reason + QStringLiteral(" The other programs publish without it.")); + emit status(reason + QStringLiteral(" It joins the catalog when it opens.")); } } - programs = std::move(openPrograms); - // One catalog entry per program, as LivePipeline builds a live per-program - // track. - const qint64 generatedAtMs = QDateTime::currentMSecsSinceEpoch(); - QList catalogs; - for (const std::unique_ptr& program : programs) { + // A program's catalog entry, once it opened, as LivePipeline builds a live + // per-program track. + const auto catalogEntry = [this](Program* program, qint64 generatedAtMs) { + program->packetsPerObject = std::max(1, m_config.targetSegmentBytes / program->packetizer->packetSize()); const M2tsPacketizer& packetizer = *program->packetizer; // MSFTS 6.7: the configured rate, or one worked out above the rate // measured at start-up. @@ -480,17 +509,27 @@ void MptsPipeline::run() { "PCR. Give the program a mux rate to declare both.", program->config.programNumber); } - catalogs.append(entry); emit status(QStringLiteral("per-program track %1: PCR PID %2, mpeg2tsMuxRate %3 bit/s.") .arg(program->track) .arg(packetizer.pcrPid()) .arg(muxRate)); - } - const QByteArray catalog = MsftsMuxer::catalogJson(catalogs); + return entry; + }; + const qint64 generatedAtMs = QDateTime::currentMSecsSinceEpoch(); + QByteArray catalog; { + QList catalogs; + std::vector listed; + for (std::size_t index = 0; index < programs.size(); ++index) { + catalogs.append(programOpened[index] ? catalogEntry(programs[index].get(), generatedAtMs) : MsftsCatalog()); + listed.push_back(programOpened[index] != 0); + } std::lock_guard lock(shared->mutex); shared->catalogs = catalogs; - shared->catalogJson = catalog; + shared->listed = listed; + shared->catalogJson = MsftsMuxer::catalogJson(shared->listedCatalogs()); + catalog = shared->catalogJson; + // A program still waiting to open counts as live until it ends. shared->liveWorkers = static_cast(programs.size()); shared->capacity = kMergedQueueSlotsPerProgram * programs.size(); for (const std::unique_ptr& program : programs) { @@ -500,70 +539,115 @@ void MptsPipeline::run() { shared->liveStarted.assign(programs.size(), false); } + // A program's worker: its Objects go into the merged queue until its + // source ends, it fails, or the pipeline stops. const std::uint64_t fragmentUs = static_cast(m_relay.fragmentDurationMs) * 1000ULL; + const auto runProgram = [this, shared, fragmentUs](Program* program, int index) { + QString readError; + while (true) { + M2tsObject object; + if (!program->packetizer->readObject(program->packetsPerObject, &object, &readError)) { + break; + } + PublishedObject published; + published.trackName = program->track; + published.payload = std::move(object.payload); + published.groupId = object.groupId; + published.objectId = object.objectId; + published.startsGroup = object.startsGroup; + published.mediaTimeUs = static_cast(program->objects) * fragmentUs; + published.mediaDurationUs = fragmentUs; + ++program->objects; + // Draft "Use of MSF Initialization Data": the new initData + // reaches the catalog before the Objects that rely on the + // changed PSI, this one first. + QByteArray initData; + const bool initDataChanged = program->packetizer->takeInitDataChange(&initData); + const Shared::Pushed pushed = + shared->push(index, {published}, initDataChanged ? &initData : nullptr, object.sourceOffset, true); + if (pushed == Shared::Pushed::Stopped) { + break; + } + if (pushed == Shared::Pushed::Dropped) { + continue; + } + // The timeline history records the Groups that went out: none + // from before the first pull. + if (pushed == Shared::Pushed::QueuedFirstLive) { + program->timeline = TimelineHistory(); + } + // One timeline record per media Group, for its first Object + // that starts a video PES, with that PES's PTS (MSF 7.1.1). It + // follows the media Object it records, and goes with it when + // the first pull comes in between and drops it. + if (!program->timelineTrack.isEmpty() && object.ptsUs.has_value() && + program->timeline.lastGroupId != published.groupId && + shared->push(index, {timelineObject(&program->timeline, program->timelineTrack, published, + *object.ptsUs, nowUnixUs(), nowSteadyUs(), + m_relay.accessibleWindowMs)}, + nullptr, object.sourceOffset, false) == Shared::Pushed::Stopped) { + break; + } + } + // The track has no more Objects: its last one closes its Group + // now, rather than when the session ends. Timeline Objects are + // each final in their Group, so that track needs no marker. The + // marker is queued before programEnded() reports why the track ended. + PublishedObject end; + end.trackName = program->track; + end.endsTrack = true; + shared->push(index, {end}, nullptr, std::nullopt, false); + const bool running = m_running.load(std::memory_order_acquire); + if (!readError.isEmpty() && running) { + emit programEnded(program->config.programNumber, readError); + } + // Only a clean end of the source: not an error, and not an + // interrupt, which closes the SRT pipe after it clears m_running. + shared->workerEnded(readError.isEmpty() && running); + }; for (int index = 0; index < static_cast(programs.size()); ++index) { - workers.emplace_back([this, shared, program = programs[index].get(), index, fragmentUs] { - QString readError; - while (true) { - M2tsObject object; - if (!program->packetizer->readObject(program->packetsPerObject, &object, &readError)) { - break; - } - PublishedObject published; - published.trackName = program->track; - published.payload = std::move(object.payload); - published.groupId = object.groupId; - published.objectId = object.objectId; - published.startsGroup = object.startsGroup; - published.mediaTimeUs = static_cast(program->objects) * fragmentUs; - published.mediaDurationUs = fragmentUs; - ++program->objects; - // Draft "Use of MSF Initialization Data": the new initData - // reaches the catalog before the Objects that rely on the - // changed PSI, this one first. - QByteArray initData; - const bool initDataChanged = program->packetizer->takeInitDataChange(&initData); - const Shared::Pushed pushed = - shared->push(index, {published}, initDataChanged ? &initData : nullptr, object.sourceOffset, true); - if (pushed == Shared::Pushed::Stopped) { - break; - } - if (pushed == Shared::Pushed::Dropped) { + Program* program = programs[index].get(); + if (programOpened[index]) { + workers.emplace_back([runProgram, program, index] { runProgram(program, index); }); + continue; + } + // A late program: open again until it opens, its source ends, or the + // pipeline stops. Its catalog entry goes out before its first Object. + workers.emplace_back([this, shared, program, index, runProgram, newPacketizer, openFailure, catalogEntry, + lastFailure = notOpened[index]]() mutable { + while (m_running.load(std::memory_order_acquire) && !program->device->ended()) { + // The offsets in the queue's stream go on from the last + // attempt: the first pull's live edge is in that scale. + auto next = newPacketizer(program); + next->continueFrom(*program->packetizer); + program->packetizer = next; + QString error; + if (!program->packetizer->open(program->config.programNumber, &error)) { + // An attempt that the end of the source cut short says less. + if (!program->device->ended()) { + lastFailure = openFailure(*program, error); + } continue; } - // The timeline history records the Groups that went out: none - // from before the first pull. - if (pushed == Shared::Pushed::QueuedFirstLive) { - program->timeline = TimelineHistory(); - } - // One timeline record per media Group, for its first Object - // that starts a video PES, with that PES's PTS (MSF 7.1.1). It - // follows the media Object it records, and goes with it when - // the first pull comes in between and drops it. - if (!program->timelineTrack.isEmpty() && object.ptsUs.has_value() && - program->timeline.lastGroupId != published.groupId && - shared->push(index, {timelineObject(&program->timeline, program->timelineTrack, published, - *object.ptsUs, nowUnixUs(), nowSteadyUs(), - m_relay.accessibleWindowMs)}, - nullptr, object.sourceOffset, false) == Shared::Pushed::Stopped) { - break; + if (shared->join(index, catalogEntry(program, QDateTime::currentMSecsSinceEpoch()))) { + emit status(QStringLiteral("program %1 joined the catalog.").arg(program->config.programNumber)); + runProgram(program, index); + return; } + break; } - // The track has no more Objects: its last one closes its Group - // now, rather than when the session ends. Timeline Objects are - // each final in their Group, so that track needs no marker. The - // marker is queued before programEnded() reports why the track ended. - PublishedObject end; - end.trackName = program->track; - end.endsTrack = true; - shared->push(index, {end}, nullptr, std::nullopt, false); - const bool running = m_running.load(std::memory_order_acquire); - if (!readError.isEmpty() && running) { - emit programEnded(program->config.programNumber, readError); + // It never opened. At the end of its source, programEnded() says + // why, once, as for a track that ends on an error; the others' + // broadcast still ends cleanly. An interrupt says nothing. + const bool ended = program->device->ended() && m_running.load(std::memory_order_acquire); + if (ended) { + if (!lastFailure.endsWith(QLatin1Char('.'))) { + lastFailure += QLatin1Char('.'); + } + emit programEnded(program->config.programNumber, + lastFailure + QStringLiteral(" It never joined the catalog.")); } - // Only a clean end of the source: not an error, and not an - // interrupt, which closes the SRT pipe after it clears m_running. - shared->workerEnded(readError.isEmpty() && running); + shared->workerEnded(ended); }); } emit status(QStringLiteral("MSFTS multi-program pipeline started: %1 programs.").arg(programs.size())); diff --git a/src/media/MptsPipeline.h b/src/media/MptsPipeline.h index f398e5d..b4efe56 100644 --- a/src/media/MptsPipeline.h +++ b/src/media/MptsPipeline.h @@ -39,7 +39,8 @@ struct MptsConfig { // Where SRT ingest skipped ahead in the source; null outside SRT ingest. std::shared_ptr sourceGaps; // A program that does not open at start-up fails every program. Without - // it, the others publish and programEnded() reports that one. + // it, the others publish, and that program joins the catalog when it + // opens (status() reports both). bool strictStart = false; }; @@ -85,9 +86,9 @@ class MptsPipeline final : public QObject { void status(const QString& message); // A start-up or publish error, which ends the pipeline. void error(const QString& message); - // A program that did not open at start-up, or a read error of one program, - // which ends that program's track only; the session goes on with the - // other programs. + // A read error of one program, which ends that program's track only, or a + // program that never opened by the end of its source; the session goes + // on with the other programs. void programEnded(int program, const QString& reason); // The publisher's first pull: every program queue dropped what it held, // so what the source sends from now on is what the tracks start with. diff --git a/src/media/PacketQueueDevice.cpp b/src/media/PacketQueueDevice.cpp index db43cf6..c187fd5 100644 --- a/src/media/PacketQueueDevice.cpp +++ b/src/media/PacketQueueDevice.cpp @@ -104,6 +104,11 @@ void PacketQueueDevice::abort() { m_wake.notify_all(); } +bool PacketQueueDevice::ended() const { + std::lock_guard lock(m_mutex); + return m_aborted || (m_finished && m_queue.size() == m_head); +} + qint64 PacketQueueDevice::droppedBytes() const { std::lock_guard lock(m_mutex); return m_dropped; diff --git a/src/media/PacketQueueDevice.h b/src/media/PacketQueueDevice.h index bc0c4c7..a70f9e1 100644 --- a/src/media/PacketQueueDevice.h +++ b/src/media/PacketQueueDevice.h @@ -60,6 +60,9 @@ class PacketQueueDevice final : public QIODevice { void finish(); // Wakes a blocked reader at once, and discards what is queued. void abort(); + // No more data will come, and none is queued: after finish() once the + // reader took the rest, or after abort(). + bool ended() const; // Bytes dropped by overflow so far. qint64 droppedBytes() const; std::shared_ptr gaps() const; diff --git a/tests/mpts_pipeline_test.cpp b/tests/mpts_pipeline_test.cpp index 95ec860..d4d3c80 100644 --- a/tests/mpts_pipeline_test.cpp +++ b/tests/mpts_pipeline_test.cpp @@ -300,6 +300,7 @@ struct Run { bool finished = false; // the pipeline stopped on its own within kWait QStringList errors; QStringList ended; // ": " for each program that ended early + QStringList statuses; QStringList log; QByteArray catalog; std::vector sent; // what the publisher sent, in order @@ -359,6 +360,10 @@ Run publish(const QString& source, const QList& programs, con onError(message); } }, Qt::DirectConnection); + QObject::connect(&pipeline, &MptsPipeline::status, &pipeline, [&](const QString& message) { + std::lock_guard lock(errorMutex); + run.statuses.append(message); + }, Qt::DirectConnection); // onError hears why a program ended too. QObject::connect(&pipeline, &MptsPipeline::programEnded, &pipeline, [&](int number, const QString& reason) { { @@ -634,16 +639,18 @@ int main() { "low mux rate: one warning, for program 102: " + joined(below)); } - // Program 103 missing from the PAT: programs 101 and 102 publish, and - // programEnded reports 103 once, before anything is published. + // Program 103 missing from the PAT: programs 101 and 102 publish, and a + // status says that 103 joins the catalog when it opens. It never does. { const Run run = publish(dir.filePath("missing.fifo"), {program(101), program(102), program(103)}, QByteArray(), liveFeed(mptsStream({101, 102}, 60), roundsBytes(2, 15))); ok &= expect(run.finished, "missing program: the pipeline ends with the source"); ok &= expect(run.errors.isEmpty(), "missing program: no pipeline error: " + joined(run.errors)); - ok &= expect(run.ended == QStringList({"103: program 103 is not in the source PAT. The other programs " - "publish without it."}), - "missing program: programEnded names 103 once: " + joined(run.ended)); + ok &= expect(run.ended == QStringList({"103: program 103 is not in the source PAT. It never joined the catalog."}), + "missing program: programEnded names 103 once, at the end: " + joined(run.ended)); + ok &= expect(run.statuses.filter(QStringLiteral("program 103")) == + QStringList({"program 103 is not in the source PAT. It joins the catalog when it opens."}), + "missing program: one status names 103: " + joined(run.statuses)); const QStringList tracks = publishedTracks(run.log); ok &= expect(tracks.count(QStringLiteral("program-101")) > 0 && tracks.count(QStringLiteral("program-102")) > 0 && !tracks.contains(QStringLiteral("program-103")), @@ -675,7 +682,7 @@ int main() { } // The PAT lists program 102, but its PMT never comes, as when its encoder - // is down: 101 publishes, and programEnded says that 102's PMT is missing. + // is down: 101 publishes, and a status says that 102's PMT is missing. { const QByteArray data = withoutPids(mptsStream({101, 102}, 60), {0x1001, 0x101}); const Run run = publish(dir.filePath("no-pmt.fifo"), {program(101), program(102)}, QByteArray(), @@ -683,12 +690,64 @@ int main() { ok &= expect(run.finished, "missing PMT: the pipeline ends with the source"); ok &= expect(run.errors.isEmpty(), "missing PMT: no pipeline error: " + joined(run.errors)); ok &= expect(run.ended == QStringList({"102: program 102: The PAT lists program 102, but its PMT (PID 4097) " - "did not come within 1000 ms of PCR time after the PAT. The other " - "programs publish without it."}), - "missing PMT: programEnded names 102 and its PMT: " + joined(run.ended)); + "did not come within 1000 ms of PCR time after the PAT. It never " + "joined the catalog."}), + "missing PMT: programEnded names 102 once, at the end: " + joined(run.ended)); + ok &= expect(run.statuses.filter(QStringLiteral("program 102")) == + QStringList({"program 102: The PAT lists program 102, but its PMT (PID 4097) did not come " + "within 1000 ms of PCR time after the PAT. It joins the catalog when it opens."}), + "missing PMT: one status names 102 and its PMT: " + joined(run.statuses)); const QStringList tracks = publishedTracks(run.log); ok &= expect(tracks.count(QStringLiteral("program-101")) > 0 && !tracks.contains(QStringLiteral("program-102")), "missing PMT: 101 publishes, 102 does not"); + // The source ended cleanly, with 102 still waiting: the broadcast ends. + ok &= expect(tracks.count(QStringLiteral("catalog")) == 1, "missing PMT: one closing catalog"); + } + + // Program 102's PMT and media start at round 20, as when its encoder comes + // up late: 101 publishes from the start, and 102 joins with a catalog + // update that lists it, before its first Object. + { + const QByteArray full = mptsStream({101, 102}, 60); + const QByteArray data = withoutPids(full.left(roundsBytes(2, 20)), {0x1001, 0x101}) + + full.mid(roundsBytes(2, 20)); + const qsizetype lead = withoutPids(full.left(roundsBytes(2, 15)), {0x1001, 0x101}).size(); + const Run run = publish(dir.filePath("late-pmt.fifo"), {program(101), program(102)}, QByteArray(), + pacedFeed(data, lead, roundsBytes(2, 1), std::chrono::milliseconds(20))); + ok &= expect(run.finished, "late PMT: the pipeline ends with the source"); + ok &= expect(run.errors.isEmpty() && run.ended.isEmpty(), + "late PMT: no error, no programEnded: " + joined(run.errors) + joined(run.ended)); + ok &= expect(run.statuses.contains(QStringLiteral("program 102 joined the catalog.")), + "late PMT: a status says that 102 joined: " + joined(run.statuses)); + ok &= expect(catalogTracks(run.catalog) == QStringList({"program-101", "program-101.timeline", "program-102", + "program-102.timeline"}), + "late PMT: the last catalog lists both programs: " + run.catalog.toStdString()); + const QStringList tracks = publishedTracks(run.log); + const qsizetype update = tracks.indexOf(QStringLiteral("catalog")); + const qsizetype first102 = tracks.indexOf(QStringLiteral("program-102")); + ok &= expect(tracks.count(QStringLiteral("program-101")) > 0 && tracks.indexOf(QStringLiteral("program-101")) < update, + "late PMT: 101 publishes before the update"); + ok &= expect(update >= 0 && first102 > update, "late PMT: 102's first Object follows the catalog update"); + // 102 opens on the PMT at round 20, after which open() measures its + // rates over 1 s of PCR time (10 rounds), so its first Object is from + // round 31 or so; one more open attempt would put it after round 40. + int firstRound102 = -1; + for (const ReadyObject& ready : run.sent) { + if (ready.object.trackName != QLatin1String("program-102")) { + continue; + } + for (const QByteArray& packet : packetsOf(ready.object.payload)) { + if (pidOf(packet) == 0x101 && roundOf(packet) >= 0) { + firstRound102 = roundOf(packet); + break; + } + } + break; + } + ok &= expect(firstRound102 >= 20 && firstRound102 <= 32, + "late PMT: 102's first Object is from round 20-32, not " + std::to_string(firstRound102)); + // the update, then the closing catalog + ok &= expect(tracks.count(QStringLiteral("catalog")) == 2, "late PMT: one update and one closing catalog"); } // Program 101's PMT changes: one new catalog Object, and program 102 keeps