Skip to content
Open
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
24 changes: 20 additions & 4 deletions src/moq-output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,17 @@ void MoQOutput::AudioData(struct encoder_packet *packet)
return;
}

auto pts = util_mul_div64(packet->pts, 1000000ULL * packet->timebase_num, packet->timebase_den);
// Add ~1 second offset to handle negative PTS from audio priming frames.
// 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) {
LOG_WARNING("Dropping audio frame with negative PTS: %lld", (long long)packet->pts);
return;
}

auto pts_us = util_mul_div64(pts, 1000000ULL * packet->timebase_num, packet->timebase_den);

auto result = moq_publish_media_frame(audio, packet->data, packet->size, pts);
auto result = moq_publish_media_frame(audio, packet->data, packet->size, pts_us);
Comment on lines +159 to +169
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Guard against potential division by zero.

The calculation packet->timebase_den / packet->timebase_num on line 161 will cause undefined behavior if timebase_num is ever 0. While valid encoder packets should always have a non-zero timebase numerator, adding a defensive check would improve robustness.

🛡️ Suggested defensive guard
 	// Add ~1 second offset to handle negative PTS from audio priming frames.
 	// TODO: This is slightly wrong when den is not evenly divisible by num, but close enough.
+	if (packet->timebase_num == 0) {
+		LOG_WARNING("Invalid audio timebase (num=0), dropping frame");
+		return;
+	}
 	int64_t pts = packet->pts + packet->timebase_den / packet->timebase_num;
🤖 Prompt for AI Agents
In `@src/moq-output.cpp` around lines 159 - 169, The pts calculation uses
packet->timebase_den / packet->timebase_num which can divide by zero if
packet->timebase_num == 0; add a defensive guard before computing pts (e.g.,
check packet->timebase_num != 0 and packet->timebase_den != 0) and handle the
error path by logging a warning (using LOG_WARNING) and returning early so
util_mul_div64 and moq_publish_media_frame are not called with invalid values;
update the block around the pts calculation that references
packet->timebase_num, packet->timebase_den, pts, util_mul_div64(...) and
moq_publish_media_frame(...) to perform this check.

if (result < 0) {
LOG_ERROR("Failed to write audio frame: %d", result);
return;
Expand All @@ -177,9 +185,17 @@ void MoQOutput::VideoData(struct encoder_packet *packet)
return;
}

auto pts = util_mul_div64(packet->pts, 1000000ULL * packet->timebase_num, packet->timebase_den);
// Add ~1 second offset to match audio 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) {
LOG_WARNING("Dropping video frame with negative PTS: %lld", (long long)packet->pts);
return;
}

auto pts_us = util_mul_div64(pts, 1000000ULL * packet->timebase_num, packet->timebase_den);

auto result = moq_publish_media_frame(video, packet->data, packet->size, pts);
auto result = moq_publish_media_frame(video, packet->data, packet->size, pts_us);
if (result < 0) {
LOG_ERROR("Failed to write video frame: %d", result);
return;
Expand Down
Loading