Shaka Packager SDK
Loading...
Searching...
No Matches
webm_info_parser.cc
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <packager/media/formats/webm/webm_info_parser.h>
6
7#include <ctime>
8
9#include <absl/log/log.h>
10
11#include <packager/macros/logging.h>
12#include <packager/media/formats/webm/webm_constants.h>
13
14namespace shaka {
15namespace media {
16
17// Default timecode scale if the TimecodeScale element is
18// not specified in the INFO element.
19static const int kWebMDefaultTimecodeScale = 1000000;
20
21WebMInfoParser::WebMInfoParser() : timecode_scale_(-1), duration_(-1) {}
22
23WebMInfoParser::~WebMInfoParser() {}
24
25int WebMInfoParser::Parse(const uint8_t* buf, int size) {
26 timecode_scale_ = -1;
27 duration_ = -1;
28
29 WebMListParser parser(kWebMIdInfo, this);
30 int result = parser.Parse(buf, size);
31
32 if (result <= 0)
33 return result;
34
35 // For now we do all or nothing parsing.
36 return parser.IsParsingComplete() ? result : 0;
37}
38
39WebMParserClient* WebMInfoParser::OnListStart(int /*id*/) {
40 return this;
41}
42
43bool WebMInfoParser::OnListEnd(int id) {
44 if (id == kWebMIdInfo && timecode_scale_ == -1) {
45 // Set timecode scale to default value if it isn't present in
46 // the Info element.
47 timecode_scale_ = kWebMDefaultTimecodeScale;
48 }
49 return true;
50}
51
52bool WebMInfoParser::OnUInt(int id, int64_t val) {
53 if (id != kWebMIdTimecodeScale)
54 return true;
55
56 if (timecode_scale_ != -1) {
57 DVLOG(1) << "Multiple values for id " << std::hex << id << " specified";
58 return false;
59 }
60
61 timecode_scale_ = val;
62 return true;
63}
64
65bool WebMInfoParser::OnFloat(int id, double val) {
66 if (id != kWebMIdDuration) {
67 DVLOG(1) << "Unexpected float for id" << std::hex << id;
68 return false;
69 }
70
71 if (duration_ != -1) {
72 DVLOG(1) << "Multiple values for duration.";
73 return false;
74 }
75
76 duration_ = val;
77 return true;
78}
79
80bool WebMInfoParser::OnBinary(int id, const uint8_t* data, int size) {
81 if (id == kWebMIdDateUTC) {
82 if (size != 8)
83 return false;
84
85 int64_t date_in_nanoseconds = 0;
86 for (int i = 0; i < size; ++i)
87 date_in_nanoseconds = (date_in_nanoseconds << 8) | data[i];
88
89 std::tm exploded_epoch;
90 exploded_epoch.tm_year = 2001;
91 exploded_epoch.tm_mon = 1;
92 exploded_epoch.tm_mday = 1;
93 exploded_epoch.tm_hour = 0;
94 exploded_epoch.tm_min = 0;
95 exploded_epoch.tm_sec = 0;
96
97 date_utc_ =
98 std::chrono::system_clock::from_time_t(std::mktime(&exploded_epoch)) +
99 std::chrono::microseconds(date_in_nanoseconds / 1000);
100 }
101 return true;
102}
103
104bool WebMInfoParser::OnString(int /*id*/, const std::string& /*str*/) {
105 return true;
106}
107
108} // namespace media
109} // namespace shaka
int Parse(const uint8_t *buf, int size)
All the methods that are virtual are virtual for mocking.