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 <string> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "media/base/audio_video_metadata_extractor.h" |
| 13 #include "media/base/data_source.h" |
9 | 14 |
10 namespace metadata { | 15 namespace metadata { |
11 | 16 |
12 MediaMetadataParser::MediaMetadataParser(DataReader* reader, | 17 namespace { |
| 18 |
| 19 void SetStringScopedPtr(const std::string& value, |
| 20 scoped_ptr<std::string>* destination) { |
| 21 if (!value.empty()) |
| 22 destination->reset(new std::string(value)); |
| 23 } |
| 24 |
| 25 void SetIntScopedPtr(int value, scoped_ptr<int>* destination) { |
| 26 if (value >= 0) |
| 27 destination->reset(new int(value)); |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 32 MediaMetadataParser::MediaMetadataParser(media::DataSource* source, |
13 const std::string& mime_type) | 33 const std::string& mime_type) |
14 : reader_(reader), | 34 : source_(source), |
15 metadata_(new MediaMetadata) { | 35 metadata_(new MediaMetadata) { |
16 metadata_->mime_type = mime_type; | 36 metadata_->mime_type = mime_type; |
17 } | 37 } |
18 | 38 |
19 MediaMetadataParser::~MediaMetadataParser() {} | 39 MediaMetadataParser::~MediaMetadataParser() {} |
20 | 40 |
21 void MediaMetadataParser::Start(const MetadataCallback& callback) { | 41 void MediaMetadataParser::Start(const MetadataCallback& callback) { |
22 DCHECK(callback_.is_null()); | 42 DCHECK(callback_.is_null()); |
23 callback_ = callback; | 43 callback_ = callback; |
24 | 44 |
25 // TODO(tommycli): Implement for various mime types. | 45 if (StartsWithASCII(metadata_->mime_type, "audio/", true) || |
| 46 StartsWithASCII(metadata_->mime_type, "video/", true)) { |
| 47 PopulateAudioVideoMetadata(); |
| 48 } |
| 49 |
| 50 // TODO(tommycli): Implement for image mime types. |
26 callback_.Run(metadata_.Pass()); | 51 callback_.Run(metadata_.Pass()); |
27 } | 52 } |
28 | 53 |
| 54 void MediaMetadataParser::PopulateAudioVideoMetadata() { |
| 55 DCHECK(source_); |
| 56 DCHECK(metadata_.get()); |
| 57 media::AudioVideoMetadataExtractor extractor; |
| 58 |
| 59 if (!extractor.Extract(source_)) |
| 60 return; |
| 61 |
| 62 if (extractor.duration() >= 0) |
| 63 metadata_->duration.reset(new double(extractor.duration())); |
| 64 |
| 65 if (extractor.height() >= 0 && extractor.width() >= 0) { |
| 66 metadata_->height.reset(new int(extractor.height())); |
| 67 metadata_->width.reset(new int(extractor.width())); |
| 68 } |
| 69 |
| 70 SetStringScopedPtr(extractor.artist(), &metadata_->artist); |
| 71 SetStringScopedPtr(extractor.album(), &metadata_->album); |
| 72 SetStringScopedPtr(extractor.artist(), &metadata_->artist); |
| 73 SetStringScopedPtr(extractor.comment(), &metadata_->comment); |
| 74 SetStringScopedPtr(extractor.copyright(), &metadata_->copyright); |
| 75 SetIntScopedPtr(extractor.disc(), &metadata_->disc); |
| 76 SetStringScopedPtr(extractor.genre(), &metadata_->genre); |
| 77 SetStringScopedPtr(extractor.language(), &metadata_->language); |
| 78 SetStringScopedPtr(extractor.title(), &metadata_->title); |
| 79 SetIntScopedPtr(extractor.track(), &metadata_->track); |
| 80 } |
| 81 |
29 } // namespace metadata | 82 } // namespace metadata |
OLD | NEW |