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