-
Notifications
You must be signed in to change notification settings - Fork 10
Add 1s to each timestamp and drop negatives. #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Fixes audio priming.
WalkthroughThe change modifies PTS (presentation timestamp) calculation for audio and video frame publishing. Instead of using raw packet timestamps, the code now applies an offset-based approach by adding timebase_den/timebase_num to packet->pts and converting the result to microseconds. A validation guard was added to drop frames with negative adjusted PTS values. Comments explain the approximately 1-second offset adjustment for audio priming and audio/video synchronization. Error handling for publish failures remains unchanged, with one new early-return path for invalid timestamps. 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@src/moq-output.cpp`:
- Around line 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.
🧹 Nitpick comments (1)
src/moq-output.cpp (1)
188-198: Consider extracting duplicate PTS adjustment logic.The PTS calculation and validation logic (lines 188-196) is identical to
AudioData(lines 159-167), differing only in the log message. Extracting this to a helper function would reduce duplication and ensure consistent behavior if the offset calculation changes.♻️ Suggested helper extraction
// Add as a private helper method or static function std::optional<int64_t> MoQOutput::AdjustPts(struct encoder_packet *packet, const char *type) { if (packet->timebase_num == 0) { LOG_WARNING("Invalid %s timebase (num=0), dropping frame", type); return std::nullopt; } int64_t pts = packet->pts + packet->timebase_den / packet->timebase_num; if (pts < 0) { LOG_WARNING("Dropping %s frame with negative PTS: %lld", type, (long long)packet->pts); return std::nullopt; } return util_mul_div64(pts, 1000000ULL * packet->timebase_num, packet->timebase_den); }Then in
AudioData/VideoData:auto pts_us = AdjustPts(packet, "audio"); if (!pts_us) return; auto result = moq_publish_media_frame(audio, packet->data, packet->size, *pts_us);
| // 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Fixes audio priming.