Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/utility/media_galleries/media_metadata_parser.h" | 5 #include "chrome/utility/media_galleries/media_metadata_parser.h" |
| 6 | 6 |
| 7 #include <map> | |
| 8 #include <string> | |
| 9 | |
| 7 #include "base/bind.h" | 10 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/strings/string_number_conversions.h" | |
| 13 #include "base/strings/string_util.h" | |
| 14 #include "media/base/audio_video_metadata_extractor.h" | |
| 15 #include "media/base/data_source.h" | |
| 9 | 16 |
| 10 namespace metadata { | 17 namespace metadata { |
| 11 | 18 |
| 12 MediaMetadataParser::MediaMetadataParser(DataReader* reader, | 19 namespace { |
| 20 | |
| 21 void SetStringScopedPtrFromDict(const std::map<std::string, std::string>& dict, | |
| 22 const std::string& key, | |
| 23 scoped_ptr<std::string>* result) { | |
| 24 DCHECK(result); | |
| 25 std::map<std::string, std::string>::const_iterator it = dict.find(key); | |
| 26 if (it == dict.end()) | |
| 27 return; | |
| 28 | |
| 29 result->reset(new std::string(it->second)); | |
| 30 } | |
| 31 | |
| 32 void SetIntScopedPtrFromDict(const std::map<std::string, std::string>& dict, | |
| 33 const std::string& key, | |
| 34 scoped_ptr<int>* result) { | |
| 35 DCHECK(result); | |
| 36 std::map<std::string, std::string>::const_iterator it = dict.find(key); | |
| 37 if (it == dict.end()) | |
| 38 return; | |
| 39 | |
| 40 int converted; | |
| 41 if (base::StringToInt(it->second, &converted)) | |
| 42 result->reset(new int(converted)); | |
| 43 } | |
| 44 | |
| 45 void PopulateAudioVideoMetadata(media::DataSource* source, | |
| 46 MediaMetadataParser::MediaMetadata* metadata) { | |
| 47 DCHECK(source); | |
| 48 DCHECK(metadata); | |
| 49 media::AudioVideoMetadataExtractor extractor(source); | |
| 50 | |
| 51 if (!extractor.Extract()) | |
| 52 return; | |
| 53 | |
| 54 metadata->duration_seconds.reset(new int(extractor.GetDurationSeconds())); | |
| 55 | |
| 56 if (extractor.GetHeight() > 0 && extractor.GetWidth() > 0) { | |
| 57 metadata->height.reset(new int(extractor.GetHeight())); | |
| 58 metadata->width.reset(new int(extractor.GetWidth())); | |
| 59 } | |
| 60 | |
| 61 const std::map<std::string, std::string>& tags = extractor.GetTags(); | |
| 62 SetStringScopedPtrFromDict(tags, "album", &metadata->album); | |
|
acolwell GONE FROM CHROMIUM
2014/01/03 19:14:04
This seems brittle and unnecessarily dependent on
tommycli
2014/01/03 23:08:36
Done.
| |
| 63 SetStringScopedPtrFromDict(tags, "artist", &metadata->artist); | |
| 64 SetStringScopedPtrFromDict(tags, "comment", &metadata->comment); | |
| 65 SetStringScopedPtrFromDict(tags, "copyright", &metadata->copyright); | |
| 66 SetIntScopedPtrFromDict(tags, "disc", &metadata->disc); | |
| 67 SetStringScopedPtrFromDict(tags, "genre", &metadata->genre); | |
| 68 SetStringScopedPtrFromDict(tags, "language", &metadata->language); | |
| 69 SetStringScopedPtrFromDict(tags, "title", &metadata->title); | |
| 70 SetIntScopedPtrFromDict(tags, "track", &metadata->track); | |
| 71 } | |
| 72 | |
| 73 } // namespace | |
| 74 | |
| 75 MediaMetadataParser::MediaMetadataParser(media::DataSource* source, | |
| 13 const std::string& mime_type) | 76 const std::string& mime_type) |
| 14 : reader_(reader), | 77 : source_(source), |
| 15 metadata_(new MediaMetadata) { | 78 metadata_(new MediaMetadata) { |
| 16 metadata_->mime_type = mime_type; | 79 metadata_->mime_type = mime_type; |
| 17 } | 80 } |
| 18 | 81 |
| 19 MediaMetadataParser::~MediaMetadataParser() {} | 82 MediaMetadataParser::~MediaMetadataParser() {} |
| 20 | 83 |
| 21 void MediaMetadataParser::Start(const MetadataCallback& callback) { | 84 void MediaMetadataParser::Start(const MetadataCallback& callback) { |
| 22 DCHECK(callback_.is_null()); | 85 DCHECK(callback_.is_null()); |
| 23 callback_ = callback; | 86 callback_ = callback; |
| 24 | 87 |
| 25 // TODO(tommycli): Implement for various mime types. | 88 if (StartsWithASCII(metadata_->mime_type, "audio/", true) || |
| 89 StartsWithASCII(metadata_->mime_type, "video/", true)) { | |
| 90 PopulateAudioVideoMetadata(source_.get(), metadata_.get()); | |
|
acolwell GONE FROM CHROMIUM
2014/01/03 19:14:04
nit: Why don't you simply make this a private memb
tommycli
2014/01/03 23:08:36
Done.
| |
| 91 } | |
| 92 | |
| 93 // TODO(tommycli): Implement for image mime types. | |
| 26 callback_.Run(metadata_.Pass()); | 94 callback_.Run(metadata_.Pass()); |
| 27 } | 95 } |
| 28 | 96 |
| 29 } // namespace metadata | 97 } // namespace metadata |
| OLD | NEW |