Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,20 @@ 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 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.

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
Expand Down Expand Up @@ -487,6 +497,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.
10 changes: 9 additions & 1 deletion src/app/PublisherSpec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion src/app/PublisherSpec.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ struct PublisherSpec {
QString sourcePath;
QList<MptsProgramConfig> programs;
int segmentBytes = 64 * 1024;
// MptsConfig::strictStart.
bool strictStart = false;
};

// Reads a publisher configuration:
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/cli/main_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 31 additions & 7 deletions src/media/M2tsPacketizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -696,18 +699,29 @@ 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;
};
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
Expand All @@ -725,6 +739,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."));
Expand All @@ -739,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."));
}
Expand Down
5 changes: 3 additions & 2 deletions src/media/M2tsPacketizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading
Loading