Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d0d8a98
chore(quest): claim quest/m1/binding-timestamps.md
kixelated Oct 27, 2026
5971d46
feat(moq-ffi)!: exact MoqTimestamp records replace microsecond fields
kixelated Oct 27, 2026
afd7836
feat(bindings)!: carry exact timestamps in every binding
kixelated Oct 28, 2026
8547267
chore(quest): finish binding-timestamps; format and regenerate dart b…
kixelated Oct 28, 2026
461edf6
Merge remote-tracking branch 'origin/main' into binding-timestamps
kixelated Oct 28, 2026
ce24401
feat(bindings)!: floor timestamp conversions, wrap Python anchors, st…
kixelated Oct 28, 2026
047432d
test(kt): the largest-scale conversion floors to just under half a se…
kixelated Oct 28, 2026
8b0907e
Merge remote-tracking branch 'origin/main' into quest/m1/binding-time…
kixelated Oct 29, 2026
bf641dd
fix(go): TimestampToDuration panics on a zero scale instead of satura…
kixelated Oct 29, 2026
d87b377
Merge remote-tracking branch 'origin/main' into timestamp-bindings-re…
kixelated Oct 29, 2026
f7b33c6
style(c): format merged video timestamp assertions
kixelated Oct 29, 2026
3db0192
Merge origin/main into quest/m1/binding-timestamps
kixelated Oct 30, 2026
5175fca
Merge origin/main into quest/m1/binding-timestamps
kixelated Oct 31, 2026
f0f03da
fix(bindings): exact timestamps on main's insert_datagram and parity …
kixelated Oct 31, 2026
8525d46
Merge base and mechanically reconcile timestamp migration conflicts
Nov 1, 2026
1afe329
Merge base and mechanically reconcile timestamp migration conflicts
Nov 1, 2026
38f577a
Merge origin/main into quest/m1/binding-timestamps
kixelated Nov 1, 2026
6080b71
kt: ktfmt the merged timestamp tests
kixelated Nov 1, 2026
751ecea
swift: swift-format the merged timestamp tests
kixelated Nov 1, 2026
17ce02a
fix(bindings): restore Go InsertDatagram build, drop stale py import,…
kixelated Nov 2, 2026
3b64c0e
quest land: merge tested base
Nov 2, 2026
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
8 changes: 4 additions & 4 deletions c/moq/test/probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,16 +370,16 @@ int main(void)
moq_frame frame = moq_frame_default();
frame.payload.data = hello;
frame.payload.len = sizeof(hello);
frame.timestamp_us.has_value = true;
frame.timestamp_us.value = 1000;
moq_timestamp timestamp = {1000, 1000000};
frame.timestamp = &timestamp;
ok(moq_track_producer_write_frame(track, &frame), "write_frame");
moq_frame *received = await(&reading, "read_frame");
moq_task_free(task);
CHECK(received);
CHECK(received->payload.len == sizeof(hello));
CHECK(memcmp(received->payload.data, hello, sizeof(hello)) == 0);
/* Lite-07 carries both the Implementation parameter and frame timestamps. */
CHECK(received->timestamp_us.has_value && received->timestamp_us.value == 1000);
/* Lite-07 carries both the Implementation parameter and frame timestamps, at any scale. */
CHECK(received->timestamp && received->timestamp->value * 1000000 == 1000 * received->timestamp->scale);
moq_frame_free(received);
CHECK(!cancelled.done);

Expand Down
5 changes: 3 additions & 2 deletions cpp/moq/bench/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ Reader read(Case c, std::shared_ptr<moq::TrackConsumer> consumer, const std::vec
require(group != nullptr);
for (int f = 0; f < c.frames(); ++f) {
auto frame = ok(co_await group->read_frame());
require(frame && frame->payload == payload && frame->timestamp_us == n * c.frames() + f);
require(frame && frame->payload == payload && frame->timestamp &&
frame->timestamp->value == n * c.frames() + f);
}
}

Expand Down Expand Up @@ -119,7 +120,7 @@ void throughput(Case c, double budget, std::vector<Result> &results)
for (auto &track : tracks) {
auto group = ok(track->append_group());
for (int f = 0; f < c.frames(); ++f)
ok(group->write_frame({payload, n * c.frames() + f}));
ok(group->write_frame({payload, moq::Timestamp{n * c.frames() + f, 1000000}}));
ok(group->finish());
}
for (auto &reader : readers) {
Expand Down
54 changes: 46 additions & 8 deletions cpp/moq/test/probe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,43 @@ Coroutine read_one(std::shared_ptr<moq::TrackConsumer> consumer, std::optional<m
}
#endif

// Whether two timestamps name the same instant at any scales; the probe's values are small
// enough that the cross products cannot overflow.
bool same_time(const moq::Timestamp &a, const moq::Timestamp &b)
{
return a.value * b.scale == b.value * a.scale;
}

// Timestamps are exact records: a 90 kHz tick and a present zero read back as written, an
// untimed frame reads back untimed, and a record past the bounds is refused.
void timestamp_roundtrip()
{
auto broadcast = ok(moq::BroadcastProducer::init(), "timestamp broadcast");
moq::TrackInfo timed;
timed.timescale = 90000;
auto track = ok(broadcast->publish_track("timed", timed), "publish timed track");
auto consumer = ok(track->consume(std::nullopt), "consume timed track");
for (uint64_t value : {0, 1}) {
ok(track->write_frame({bytes("tick"), moq::Timestamp{value, 90000}}), "write tick");
auto frame = ok(consumer->read_frame().get(), "read tick");
CHECK(frame && frame->timestamp);
CHECK(frame->timestamp->value == value && frame->timestamp->scale == 90000);
}
for (const moq::Timestamp &invalid :
{moq::Timestamp{0, 0}, moq::Timestamp{0, uint64_t(1) << 62}, moq::Timestamp{uint64_t(1) << 61, 90000}}) {
auto refused = track->write_frame({bytes("tick"), invalid});
CHECK(!refused);
CHECK(std::holds_alternative<moq::Error::kTimeOverflow>(refused.error().get_variant()));
}

auto untimed = ok(broadcast->publish_track("untimed", std::nullopt), "publish untimed track");
auto untimed_consumer = ok(untimed->consume(std::nullopt), "consume untimed track");
ok(untimed->write_frame({bytes("untimed"), std::nullopt}), "write untimed");
auto frame = ok(untimed_consumer->read_frame().get(), "read untimed");
CHECK(frame && !frame->timestamp);
ok(broadcast->close(), "close timestamp broadcast");
}

// Only the defaults carry moq-net's zero max delay; a bare record sets no subscriber limit.
void subscription_defaults()
{
Expand Down Expand Up @@ -144,7 +181,7 @@ void audio_roundtrip()
sizeof(value));
}
}
ok(audio->write({250000 + packet * 20000, data}), "write audio");
ok(audio->write({{250000 + packet * 20000, 1000000}, data}), "write audio");
}
ok(audio->finish(), "finish audio");
size_t frames = 0;
Expand All @@ -156,15 +193,15 @@ void audio_roundtrip()
CHECK(!std::holds_alternative<moq::AudioRead::kGap>(read->get_variant()));
// The finish ends the encoder where its eight 20 ms frames stop.
if (const auto *end = std::get_if<moq::AudioRead::kEnd>(&read->get_variant())) {
CHECK(end->end_us == 250000 + 8 * 20000);
CHECK(same_time(end->end, {250000 + 8 * 20000, 1000000}));
continue;
}
const auto *variant = std::get_if<moq::AudioRead::kFrame>(&read->get_variant());
CHECK(variant != nullptr);

const auto *next = &variant->frame;

CHECK(next->timestamp_us == 250000 + frames * 1000000 / 48000);
CHECK(same_time(next->timestamp, {250 * 48000 + frames * 1000, 48000000}));
CHECK(next->data.size() % (2 * sizeof(float)) == 0);
for (size_t sample = 0; sample < next->data.size() / (2 * sizeof(float)); sample++) {
for (size_t channel = 0; channel < 2; channel++) {
Expand All @@ -188,14 +225,15 @@ void audio_roundtrip()
};
CHECK(power(channel) > power(1 - channel));
}
CHECK(!audio->write({0, std::vector<uint8_t>(8)}));
CHECK(!audio->write({{0, 1000000}, std::vector<uint8_t>(8)}));
ok(broadcast->close(), "close audio broadcast");
}

} // namespace

int main()
{
timestamp_roundtrip();
subscription_defaults();
audio_roundtrip();
auto timebase_owner = ok(moq::BroadcastProducer::init(), "timebase owner");
Expand Down Expand Up @@ -296,14 +334,14 @@ int main()

// The consumer survives the cancelled read and delivers the next frame.
auto reading = consumer->read_frame();
ok(track->write_frame({bytes("hello"), 1000}), "write_frame");
ok(track->write_frame({bytes("hello"), moq::Timestamp{1000, 1000000}}), "write_frame");
auto frame = ok(reading.get(), "read_frame");
// get() consumed the state.
CHECK(!reading.valid());
CHECK(frame.has_value());
CHECK(frame->payload == bytes("hello"));
// Lite-07 carries both the Implementation parameter and frame timestamps.
CHECK(frame->timestamp_us == 1000);
CHECK(frame->timestamp && same_time(*frame->timestamp, {1000, 1000000}));

// A continuation receives the next frame on the executor.
{
Expand All @@ -318,7 +356,7 @@ int main()
});
// then() took the future's state.
CHECK(!reading_next.valid());
ok(track->write_frame({bytes("then"), 2000}), "write_frame");
ok(track->write_frame({bytes("then"), moq::Timestamp{2000, 1000000}}), "write_frame");
CHECK(done.wait_for(5s));
CHECK(received && received->payload == bytes("then"));
}
Expand All @@ -338,7 +376,7 @@ int main()
Latch done;
std::optional<moq::Frame> received;
auto reader = read_one(consumer, received, done);
ok(track->write_frame({bytes("co_await"), 3000}), "write_frame");
ok(track->write_frame({bytes("co_await"), moq::Timestamp{3000, 1000000}}), "write_frame");
CHECK(done.wait_for(5s));
CHECK(received && received->payload == bytes("co_await"));
(void)reader;
Expand Down
19 changes: 9 additions & 10 deletions cpp/obs/src/moq-output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "moq-settings.h"
#include "moq-url.h"
#include "logger.h"
#include "util/util_uint64.h"

#include <algorithm>
#include <cstring>
Expand Down Expand Up @@ -605,20 +604,20 @@ void MoQOutput::Data(struct encoder_packet *packet)
}

// The packet's timestamp on the broadcast clock, or nothing when it falls before zero.
static std::optional<uint64_t> PacketTimestamp(const struct encoder_packet *packet)
static std::optional<moq::Timestamp> PacketTimestamp(const struct encoder_packet *packet)
{
// Add ~1 second offset to handle negative PTS from audio priming frames, and
// the same to video for A/V sync.
// TODO: This is slightly wrong when den is not evenly divisible by num, but close enough.
int64_t pts = packet->pts + packet->timebase_den / packet->timebase_num;
if (pts < 0)
// A tick is num/den seconds, so den units per second hold it exactly, 1001/30000 included.
// Offset by one second to cover the negative PTS of audio priming frames, and video by
// the same for A/V sync.
const int64_t value = packet->pts * packet->timebase_num + packet->timebase_den;
if (value < 0)
return std::nullopt;
return util_mul_div64(pts, 1000000ULL * packet->timebase_num, packet->timebase_den);
return moq::Timestamp{static_cast<uint64_t>(value), static_cast<uint64_t>(packet->timebase_den)};
}

static moq::Frame PacketFrame(const struct encoder_packet *packet, uint64_t timestamp_us)
static moq::Frame PacketFrame(const struct encoder_packet *packet, const moq::Timestamp &timestamp)
{
return moq::Frame{std::vector<uint8_t>(packet->data, packet->data + packet->size), timestamp_us};
return moq::Frame{std::vector<uint8_t>(packet->data, packet->data + packet->size), timestamp};
}

void MoQOutput::AudioData(struct encoder_packet *packet)
Expand Down
30 changes: 19 additions & 11 deletions cpp/obs/src/moq-source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <util/platform.h>
#include <util/darray.h>
#include <util/dstr.h>
#include <util/util_uint64.h>

#include <moq/moq.hpp>

Expand Down Expand Up @@ -76,6 +77,13 @@ namespace {

struct moq_source;

// A track timestamp in the nanoseconds OBS stamps media with, floored when the scale does not
// divide a second evenly into nanoseconds.
static uint64_t moq_source_nanoseconds(const moq::Timestamp &timestamp)
{
return util_mul_div64(timestamp.value, 1'000'000'000, timestamp.scale);
}

enum class Decoded { Pending, Ready, Failed };
// Full: the decoder refused the packet until its pending pictures are taken.
enum class Sent { Accepted, Full, Failed };
Expand All @@ -87,7 +95,7 @@ struct VideoDecoder {
SwsContext *sws_ctx = nullptr;
bool got_keyframe = false;
// End can precede the old encoder's final packets, whose timestamps are below it.
std::optional<uint64_t> end_us;
std::optional<uint64_t> end_ns;
uint32_t frames_waiting_for_keyframe = 0;
uint32_t consecutive_decode_errors = 0;
obs_source_frame frame = {};
Expand Down Expand Up @@ -728,18 +736,18 @@ static void moq_source_on_frame(struct moq_source *ctx, const std::shared_ptr<Co
auto &decoder = *track->video_decoder;
const auto &variant = (*result)->get_variant();
if (const auto *end = std::get_if<moq::MediaRead::kEnd>(&variant)) {
decoder.end_us = end->end_us;
decoder.end_ns = moq_source_nanoseconds(end->end);
moq_source_read_track(ctx, conn, track);
return;
}
const auto *read = std::get_if<moq::MediaRead::kFrame>(&variant);
if (!read || (decoder.end_us && read->frame.timestamp_us >= *decoder.end_us)) {
if (!read || (decoder.end_ns && moq_source_nanoseconds(read->frame.timestamp) >= *decoder.end_ns)) {
// A gap loses references immediately. An end does so only at the fresh encoder,
// after all old tail packets below its endpoint have entered the decoder.
moq_source_drain_video(ctx, conn, track);
avcodec_flush_buffers(decoder.codec_ctx);
decoder.got_keyframe = false;
decoder.end_us.reset();
decoder.end_ns.reset();
}
if (!read) {
moq_source_read_track(ctx, conn, track);
Expand Down Expand Up @@ -814,7 +822,7 @@ static bool moq_source_on_audio_read(struct moq_source *ctx, const std::shared_p
audio.format = AUDIO_FORMAT_FLOAT_PLANAR;
audio.samples_per_sec = layout.sample_rate;
// The same broadcast timeline video stamps, in the nanoseconds OBS expects.
audio.timestamp = frame.timestamp_us * 1000;
audio.timestamp = moq_source_nanoseconds(frame.timestamp);

if (track == conn->pending_audio)
conn->audio = std::move(conn->pending_audio);
Expand Down Expand Up @@ -1146,9 +1154,9 @@ static std::unique_ptr<VideoDecoder> moq_source_prepare_decoder(const moq::Video
}
}

// Packets carry the frame's microsecond timestamp, which the decoder hands back on the
// picture it belongs to.
decoder->codec_ctx->pkt_timebase = AVRational{1, 1'000'000};
// Packets carry the frame's timestamp in OBS's nanoseconds, which the decoder hands back
// on the picture it belongs to.
decoder->codec_ctx->pkt_timebase = AVRational{1, 1'000'000'000};

// Open codec
if (avcodec_open2(decoder->codec_ctx, codec, NULL) < 0) {
Expand Down Expand Up @@ -1200,7 +1208,7 @@ static Sent moq_source_send_packet(VideoDecoder &decoder, const moq::MediaFrame
packet->size = static_cast<int>(frame_data.payload.size());
// The decoder hands this back on the picture it belongs to. Decode order has no
// timestamp of its own here, so dts stays unset.
packet->pts = static_cast<int64_t>(frame_data.timestamp_us);
packet->pts = static_cast<int64_t>(moq_source_nanoseconds(frame_data.timestamp));

// Send packet to decoder
int ret = avcodec_send_packet(decoder.codec_ctx, packet);
Expand Down Expand Up @@ -1366,8 +1374,8 @@ static Decoded moq_source_receive_frame(VideoDecoder &decoder)
return Decoded::Failed;
}

// OBS expects nanoseconds; packets carry microseconds.
decoder.frame.timestamp = static_cast<uint64_t>(frame->best_effort_timestamp) * 1000;
// Packets already carry the nanoseconds OBS expects.
decoder.frame.timestamp = static_cast<uint64_t>(frame->best_effort_timestamp);
decoder.frame.format = VIDEO_FORMAT_RGBA;
av_frame_free(&frame);
return Decoded::Ready;
Expand Down
28 changes: 15 additions & 13 deletions cpp/obs/test/moq-audio-encoder-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ std::vector<float> NoiseFrame(uint32_t &state, size_t samples)
return pcm;
}

// MoQOutput's conversion of an OBS packet's timestamp to the track's microseconds.
uint64_t PacketMicros(const encoder_packet &packet)
// MoQOutput's conversion of an OBS packet's timestamp to the track's.
moq::Timestamp PacketTimestamp(const encoder_packet &packet)
{
const int64_t pts = packet.pts + packet.timebase_den / packet.timebase_num;
return static_cast<uint64_t>(pts) * 1000000ULL * packet.timebase_num / packet.timebase_den;
const int64_t value = packet.pts * packet.timebase_num + packet.timebase_den;
return moq::Timestamp{static_cast<uint64_t>(value), static_cast<uint64_t>(packet.timebase_den)};
}

// Publishes `count` tone packets from an encoder with `settings` the way MoQOutput
Expand Down Expand Up @@ -242,8 +242,8 @@ void CheckSubscriberDecodes(const char *preset, size_t channels)
const int count = 50;
const int64_t start = 3 * RATE;
// Where input sample `index` lands on the track: MoQOutput's one second offset on top.
auto micros = [&](int64_t index) {
return static_cast<uint64_t>((start + RATE + index) * 1000000LL / RATE);
auto sample = [&](int64_t index) {
return static_cast<uint64_t>(start + RATE + index);
};
for (int i = 0; i < count; i++) {
const int64_t pts = start + i * static_cast<int64_t>(encoder.frame_size);
Expand All @@ -253,8 +253,8 @@ void CheckSubscriberDecodes(const char *preset, size_t channels)
if (!packet)
return;
CHECK(packet->pts == pts && packet->dts == pts);
const uint64_t us = PacketMicros(*packet);
TestOk(track->write_frame({std::vector<uint8_t>(packet->data, packet->data + packet->size), us}),
TestOk(track->write_frame({std::vector<uint8_t>(packet->data, packet->data + packet->size),
PacketTimestamp(*packet)}),
"write_frame");
TestOk(track->cut(), "cut");
}
Expand All @@ -265,7 +265,7 @@ void CheckSubscriberDecodes(const char *preset, size_t channels)
// with anything but its first input sample, shifts the phase and fails this.
size_t decoded = 0;
double error = 0, energy = 0;
std::optional<uint64_t> first_decoded_us;
std::optional<moq::Timestamp> first_decoded;
for (;;) {
auto next = audio->next();
if (next.wait_for(std::chrono::seconds(10)) != std::future_status::ready) {
Expand All @@ -281,11 +281,12 @@ void CheckSubscriberDecodes(const char *preset, size_t channels)
if (!variant)
break;
const auto *frame = &variant->frame;
if (!first_decoded_us)
first_decoded_us = frame->timestamp_us;
if (!first_decoded)
first_decoded = frame->timestamp;
const auto *samples = reinterpret_cast<const float *>(frame->data.data());
const size_t frames = frame->data.size() / sizeof(float) / channels;
const int64_t offset = static_cast<int64_t>(frame->timestamp_us * RATE / 1000000ULL) - (start + RATE);
const int64_t offset =
static_cast<int64_t>(frame->timestamp.value * RATE / frame->timestamp.scale) - (start + RATE);
for (size_t i = 0; i < frames; i++) {
const double expected = Tone(offset + static_cast<int64_t>(i));
for (size_t c = 0; c < channels; c++) {
Expand All @@ -299,7 +300,8 @@ void CheckSubscriberDecodes(const char *preset, size_t channels)

// OBS never drains an audio encoder, so the decoder trims the 312-sample pre-skip
// and nothing pads the tail: every input sample but that lookahead comes back.
CHECK(first_decoded_us == micros(0));
// Compared as fractions of a second, since the track reads back at its own scale.
CHECK(first_decoded && first_decoded->value * RATE == sample(0) * first_decoded->scale);
CHECK(decoded == count * encoder.frame_size - 312);
CHECK(energy > 0 && error / energy < 0.05);
printf(" %s, %zu channels: %zu samples, error %.4f\n", preset, channels, decoded,
Expand Down
7 changes: 4 additions & 3 deletions cpp/obs/test/moq-output-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,8 @@ int main()

// Everything up to the last frame, then the end of the track.
bool ended = false;
uint64_t last_timestamp = first_frame ? first_frame->frame.timestamp_us : 0;
// Reads come back at the track's one scale, so values compare directly.
uint64_t last_timestamp = first_frame ? first_frame->frame.timestamp.value : 0;
for (;;) {
next = media->next();
if (next.wait_for(std::chrono::seconds(10)) != std::future_status::ready)
Expand All @@ -698,10 +699,10 @@ int main()
}
CHECK(!std::holds_alternative<moq::MediaRead::kGap>((*result)->get_variant()));
if (const auto *frame = std::get_if<moq::MediaRead::kFrame>(&(*result)->get_variant()))
last_timestamp = frame->frame.timestamp_us;
last_timestamp = frame->frame.timestamp.value;
}
CHECK(ended);
CHECK(first_frame && last_timestamp > first_frame->frame.timestamp_us);
CHECK(first_frame && last_timestamp > first_frame->frame.timestamp.value);
}
}
printf("stop drains the session: ok\n");
Expand Down
Loading