7 #include "packager/media/formats/webvtt/webvtt_timestamp.h" 12 #include "packager/base/logging.h" 13 #include "packager/base/strings/string_number_conversions.h" 14 #include "packager/base/strings/stringprintf.h" 20 bool GetTotalMilliseconds(uint64_t hours,
26 if (minutes > 59 || seconds > 59 || ms > 999) {
27 VLOG(1) <<
"Hours:" << hours <<
" Minutes:" << minutes
28 <<
" Seconds:" << seconds <<
" MS:" << ms
29 <<
" shoud have never made it to GetTotalMilliseconds";
32 *out = 60 * 60 * 1000 * hours + 60 * 1000 * minutes + 1000 * seconds + ms;
37 bool WebVttTimestampToMs(
const base::StringPiece& source, uint64_t* out) {
40 if (source.length() < 9) {
41 LOG(WARNING) <<
"Timestamp '" << source <<
"' is mal-formed";
45 const size_t minutes_begin = source.length() - 9;
46 const size_t seconds_begin = source.length() - 6;
47 const size_t milliseconds_begin = source.length() - 3;
54 const bool has_hours =
55 minutes_begin >= 3 && source[minutes_begin - 1] ==
':' &&
56 base::StringToUint64(source.substr(0, minutes_begin - 1), &hours);
58 if ((minutes_begin == 0 || has_hours) && source[seconds_begin - 1] ==
':' &&
59 source[milliseconds_begin - 1] ==
'.' &&
60 base::StringToUint64(source.substr(minutes_begin, 2), &minutes) &&
61 base::StringToUint64(source.substr(seconds_begin, 2), &seconds) &&
62 base::StringToUint64(source.substr(milliseconds_begin, 3), &ms)) {
63 return GetTotalMilliseconds(hours, minutes, seconds, ms, out);
66 LOG(WARNING) <<
"Timestamp '" << source <<
"' is mal-formed";
70 std::string MsToWebVttTimestamp(uint64_t ms) {
71 uint64_t remaining = ms;
73 uint64_t only_ms = remaining % 1000;
75 uint64_t only_seconds = remaining % 60;
77 uint64_t only_minutes = remaining % 60;
79 uint64_t only_hours = remaining;
81 return base::StringPrintf(
"%02" PRIu64
":%02" PRIu64
":%02" PRIu64
83 only_hours, only_minutes, only_seconds, only_ms);
All the methods that are virtual are virtual for mocking.