Shaka Packager SDK
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 
14 namespace shaka {
15 namespace media {
16 
17 // Default timecode scale if the TimecodeScale element is
18 // not specified in the INFO element.
19 static const int kWebMDefaultTimecodeScale = 1000000;
20 
21 WebMInfoParser::WebMInfoParser()
22  : timecode_scale_(-1),
23  duration_(-1) {
24 }
25 
26 WebMInfoParser::~WebMInfoParser() {}
27 
28 int WebMInfoParser::Parse(const uint8_t* buf, int size) {
29  timecode_scale_ = -1;
30  duration_ = -1;
31 
32  WebMListParser parser(kWebMIdInfo, this);
33  int result = parser.Parse(buf, size);
34 
35  if (result <= 0)
36  return result;
37 
38  // For now we do all or nothing parsing.
39  return parser.IsParsingComplete() ? result : 0;
40 }
41 
42 WebMParserClient* WebMInfoParser::OnListStart(int /*id*/) {
43  return this;
44 }
45 
46 bool WebMInfoParser::OnListEnd(int id) {
47  if (id == kWebMIdInfo && timecode_scale_ == -1) {
48  // Set timecode scale to default value if it isn't present in
49  // the Info element.
50  timecode_scale_ = kWebMDefaultTimecodeScale;
51  }
52  return true;
53 }
54 
55 bool WebMInfoParser::OnUInt(int id, int64_t val) {
56  if (id != kWebMIdTimecodeScale)
57  return true;
58 
59  if (timecode_scale_ != -1) {
60  DVLOG(1) << "Multiple values for id " << std::hex << id << " specified";
61  return false;
62  }
63 
64  timecode_scale_ = val;
65  return true;
66 }
67 
68 bool WebMInfoParser::OnFloat(int id, double val) {
69  if (id != kWebMIdDuration) {
70  DVLOG(1) << "Unexpected float for id" << std::hex << id;
71  return false;
72  }
73 
74  if (duration_ != -1) {
75  DVLOG(1) << "Multiple values for duration.";
76  return false;
77  }
78 
79  duration_ = val;
80  return true;
81 }
82 
83 bool WebMInfoParser::OnBinary(int id, const uint8_t* data, int size) {
84  if (id == kWebMIdDateUTC) {
85  if (size != 8)
86  return false;
87 
88  int64_t date_in_nanoseconds = 0;
89  for (int i = 0; i < size; ++i)
90  date_in_nanoseconds = (date_in_nanoseconds << 8) | data[i];
91 
92  std::tm exploded_epoch;
93  exploded_epoch.tm_year = 2001;
94  exploded_epoch.tm_mon = 1;
95  exploded_epoch.tm_mday = 1;
96  exploded_epoch.tm_hour = 0;
97  exploded_epoch.tm_min = 0;
98  exploded_epoch.tm_sec = 0;
99 
100  date_utc_ =
101  std::chrono::system_clock::from_time_t(std::mktime(&exploded_epoch)) +
102  std::chrono::microseconds(date_in_nanoseconds / 1000);
103  }
104  return true;
105 }
106 
107 bool WebMInfoParser::OnString(int /*id*/, const std::string& /*str*/) {
108  return true;
109 }
110 
111 } // namespace media
112 } // namespace shaka
int Parse(const uint8_t *buf, int size)
Definition: webm_parser.cc:747
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66