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
13 changes: 10 additions & 3 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -487,6 +493,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
33 changes: 28 additions & 5 deletions src/media/MptsPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::unique_ptr<Program>> 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.
Expand Down
12 changes: 8 additions & 4 deletions src/media/MptsPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<SourceGapLog> 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
Expand All @@ -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
Expand All @@ -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.
Expand Down
87 changes: 87 additions & 0 deletions tests/m2ts_packetizer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::int64_t>(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<std::uint64_t>(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<SequentialDevice>(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<SequentialDevice>(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.
Expand Down
Loading
Loading