Skip to content

Commit

Permalink
write a custom double to string formatting function to trim trailing 0s
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmin committed Jul 21, 2023
1 parent 1dc1fa1 commit 4a642a1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
21 changes: 19 additions & 2 deletions packager/mpd/base/mpd_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,30 @@ std::string GetAdaptationSetKey(const MediaInfo& media_info,
return key;
}

std::string FloatToXmlString(double number) {
// Keep up to microsecond accuracy but trim trailing 0s
std::string formatted = absl::StrFormat("%.6g", number);
size_t decimalPos = formatted.find('.');
if (decimalPos != std::string::npos) {
size_t lastNonZeroPos = formatted.find_last_not_of('0');
if (lastNonZeroPos >= decimalPos) {
formatted.erase(lastNonZeroPos + 1);
}
if (formatted.back() == '.') {
formatted.pop_back();
}
}

return formatted;
}

std::string SecondsToXmlDuration(double seconds) {
// Chrome internally uses time accurate to microseconds, which is implemented
// per MSE spec (https://www.w3.org/TR/media-source/).
// We need a string formatter that has at least microseconds accuracy for a
// normal video (with duration up to 3 hours). Chrome's DoubleToString
// normal video (with duration up to 3 hours). FloatToXmlString
// implementation meets the requirement.
return absl::StrFormat("PT%gS", seconds);
return absl::StrFormat("PT%sS", FloatToXmlString(seconds));
}

bool GetDurationAttribute(xmlNodePtr node, float* duration) {
Expand Down
2 changes: 2 additions & 0 deletions packager/mpd/base/mpd_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ std::string GetBaseCodec(const MediaInfo& media_info);
// Returns a key made from the characteristics that separate AdaptationSets.
std::string GetAdaptationSetKey(const MediaInfo& media_info, bool ignore_codec);

std::string FloatToXmlString(double number);

std::string SecondsToXmlDuration(double seconds);

// Tries to get "duration" attribute from |node|. On success |duration| is set.
Expand Down
2 changes: 1 addition & 1 deletion packager/mpd/base/xml/xml_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ bool XmlNode::SetFloatingPointAttribute(const std::string& attribute_name,
double number) {
DCHECK(impl_->node);
return xmlSetProp(impl_->node.get(), BAD_CAST attribute_name.c_str(),
BAD_CAST(absl::StrFormat("%g", number).c_str())) != nullptr;
BAD_CAST(FloatToXmlString(number).c_str())) != nullptr;
}

bool XmlNode::SetId(uint32_t id) {
Expand Down

0 comments on commit 4a642a1

Please sign in to comment.