Shaka Packager SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 "packager/base/logging.h"
8 #include "packager/media/formats/webm/webm_constants.h"
9 
10 namespace shaka {
11 namespace media {
12 
13 // Default timecode scale if the TimecodeScale element is
14 // not specified in the INFO element.
15 static const int kWebMDefaultTimecodeScale = 1000000;
16 
17 WebMInfoParser::WebMInfoParser()
18  : timecode_scale_(-1),
19  duration_(-1) {
20 }
21 
22 WebMInfoParser::~WebMInfoParser() {}
23 
24 int WebMInfoParser::Parse(const uint8_t* buf, int size) {
25  timecode_scale_ = -1;
26  duration_ = -1;
27 
28  WebMListParser parser(kWebMIdInfo, this);
29  int result = parser.Parse(buf, size);
30 
31  if (result <= 0)
32  return result;
33 
34  // For now we do all or nothing parsing.
35  return parser.IsParsingComplete() ? result : 0;
36 }
37 
38 WebMParserClient* WebMInfoParser::OnListStart(int id) { return this; }
39 
40 bool WebMInfoParser::OnListEnd(int id) {
41  if (id == kWebMIdInfo && timecode_scale_ == -1) {
42  // Set timecode scale to default value if it isn't present in
43  // the Info element.
44  timecode_scale_ = kWebMDefaultTimecodeScale;
45  }
46  return true;
47 }
48 
49 bool WebMInfoParser::OnUInt(int id, int64_t val) {
50  if (id != kWebMIdTimecodeScale)
51  return true;
52 
53  if (timecode_scale_ != -1) {
54  DVLOG(1) << "Multiple values for id " << std::hex << id << " specified";
55  return false;
56  }
57 
58  timecode_scale_ = val;
59  return true;
60 }
61 
62 bool WebMInfoParser::OnFloat(int id, double val) {
63  if (id != kWebMIdDuration) {
64  DVLOG(1) << "Unexpected float for id" << std::hex << id;
65  return false;
66  }
67 
68  if (duration_ != -1) {
69  DVLOG(1) << "Multiple values for duration.";
70  return false;
71  }
72 
73  duration_ = val;
74  return true;
75 }
76 
77 bool WebMInfoParser::OnBinary(int id, const uint8_t* data, int size) {
78  if (id == kWebMIdDateUTC) {
79  if (size != 8)
80  return false;
81 
82  int64_t date_in_nanoseconds = 0;
83  for (int i = 0; i < size; ++i)
84  date_in_nanoseconds = (date_in_nanoseconds << 8) | data[i];
85 
86  base::Time::Exploded exploded_epoch;
87  exploded_epoch.year = 2001;
88  exploded_epoch.month = 1;
89  exploded_epoch.day_of_month = 1;
90  exploded_epoch.hour = 0;
91  exploded_epoch.minute = 0;
92  exploded_epoch.second = 0;
93  exploded_epoch.millisecond = 0;
94  date_utc_ = base::Time::FromUTCExploded(exploded_epoch) +
95  base::TimeDelta::FromMicroseconds(date_in_nanoseconds / 1000);
96  }
97  return true;
98 }
99 
100 bool WebMInfoParser::OnString(int id, const std::string& str) {
101  return true;
102 }
103 
104 } // namespace media
105 } // namespace shaka
int Parse(const uint8_t *buf, int size)
Definition: webm_parser.cc:738
int Parse(const uint8_t *buf, int size)