| 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> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/task_runner_util.h" | 12 #include "base/task_runner_util.h" |
| 13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 14 #include "chrome/utility/media_galleries/image_metadata_extractor.h" | |
| 15 #include "media/base/audio_video_metadata_extractor.h" | 14 #include "media/base/audio_video_metadata_extractor.h" |
| 16 #include "media/base/data_source.h" | 15 #include "media/base/data_source.h" |
| 17 | 16 |
| 18 namespace metadata { | 17 namespace metadata { |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 void SetStringScopedPtr(const std::string& value, | 21 void SetStringScopedPtr(const std::string& value, |
| 23 scoped_ptr<std::string>* destination) { | 22 scoped_ptr<std::string>* destination) { |
| 24 DCHECK(destination); | |
| 25 if (!value.empty()) | 23 if (!value.empty()) |
| 26 destination->reset(new std::string(value)); | 24 destination->reset(new std::string(value)); |
| 27 } | 25 } |
| 28 | 26 |
| 29 void SetIntScopedPtr(int value, scoped_ptr<int>* destination) { | 27 void SetIntScopedPtr(int value, scoped_ptr<int>* destination) { |
| 30 DCHECK(destination); | |
| 31 if (value >= 0) | 28 if (value >= 0) |
| 32 destination->reset(new int(value)); | 29 destination->reset(new int(value)); |
| 33 } | 30 } |
| 34 | 31 |
| 35 void SetDoubleScopedPtr(double value, scoped_ptr<double>* destination) { | |
| 36 DCHECK(destination); | |
| 37 if (value >= 0) | |
| 38 destination->reset(new double(value)); | |
| 39 } | |
| 40 | |
| 41 void SetBoolScopedPtr(bool value, scoped_ptr<bool>* destination) { | |
| 42 DCHECK(destination); | |
| 43 destination->reset(new bool(value)); | |
| 44 } | |
| 45 | |
| 46 // This runs on |media_thread_|, as the underlying FFmpeg operation is | 32 // This runs on |media_thread_|, as the underlying FFmpeg operation is |
| 47 // blocking, and the utility thread must not be blocked, so the media file | 33 // blocking, and the utility thread must not be blocked, so the media file |
| 48 // bytes can be sent from the browser process to the utility process. | 34 // bytes can be sent from the browser process to the utility process. |
| 49 scoped_ptr<MediaMetadataParser::MediaMetadata> ParseAudioVideoMetadata( | 35 scoped_ptr<MediaMetadataParser::MediaMetadata> ParseAudioVideoMetadata( |
| 50 media::DataSource* source, | 36 media::DataSource* source, |
| 51 scoped_ptr<MediaMetadataParser::MediaMetadata> metadata) { | 37 scoped_ptr<MediaMetadataParser::MediaMetadata> metadata) { |
| 52 DCHECK(source); | 38 DCHECK(source); |
| 53 DCHECK(metadata.get()); | |
| 54 media::AudioVideoMetadataExtractor extractor; | 39 media::AudioVideoMetadataExtractor extractor; |
| 55 | 40 |
| 56 if (!extractor.Extract(source)) | 41 if (!extractor.Extract(source)) |
| 57 return metadata.Pass(); | 42 return metadata.Pass(); |
| 58 | 43 |
| 59 if (extractor.duration() >= 0) | 44 if (extractor.duration() >= 0) |
| 60 metadata->duration.reset(new double(extractor.duration())); | 45 metadata->duration.reset(new double(extractor.duration())); |
| 61 | 46 |
| 62 if (extractor.height() >= 0 && extractor.width() >= 0) { | 47 if (extractor.height() >= 0 && extractor.width() >= 0) { |
| 63 metadata->height.reset(new int(extractor.height())); | 48 metadata->height.reset(new int(extractor.height())); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 78 | 63 |
| 79 for (std::map<std::string, std::string>::const_iterator it = | 64 for (std::map<std::string, std::string>::const_iterator it = |
| 80 extractor.raw_tags().begin(); | 65 extractor.raw_tags().begin(); |
| 81 it != extractor.raw_tags().end(); ++it) { | 66 it != extractor.raw_tags().end(); ++it) { |
| 82 metadata->raw_tags.additional_properties.SetString(it->first, it->second); | 67 metadata->raw_tags.additional_properties.SetString(it->first, it->second); |
| 83 } | 68 } |
| 84 | 69 |
| 85 return metadata.Pass(); | 70 return metadata.Pass(); |
| 86 } | 71 } |
| 87 | 72 |
| 88 void FinishParseImageMetadata( | |
| 89 ImageMetadataExtractor* extractor, | |
| 90 scoped_ptr<MediaMetadataParser::MediaMetadata> metadata, | |
| 91 MediaMetadataParser::MetadataCallback callback, | |
| 92 bool extract_success) { | |
| 93 DCHECK(extractor); | |
| 94 DCHECK(metadata.get()); | |
| 95 | |
| 96 if (!extract_success) { | |
| 97 callback.Run(metadata.Pass()); | |
| 98 return; | |
| 99 } | |
| 100 | |
| 101 SetIntScopedPtr(extractor->height(), &metadata->height); | |
| 102 SetIntScopedPtr(extractor->width(), &metadata->width); | |
| 103 | |
| 104 SetIntScopedPtr(extractor->rotation(), &metadata->rotation); | |
| 105 | |
| 106 SetDoubleScopedPtr(extractor->x_resolution(), &metadata->x_resolution); | |
| 107 SetDoubleScopedPtr(extractor->y_resolution(), &metadata->y_resolution); | |
| 108 SetBoolScopedPtr(extractor->flash_fired(), &metadata->flash_fired); | |
| 109 SetStringScopedPtr(extractor->camera_make(), &metadata->camera_make); | |
| 110 SetStringScopedPtr(extractor->camera_model(), &metadata->camera_model); | |
| 111 SetDoubleScopedPtr(extractor->exposure_time_sec(), | |
| 112 &metadata->exposure_time_seconds); | |
| 113 | |
| 114 SetDoubleScopedPtr(extractor->f_number(), &metadata->f_number); | |
| 115 SetDoubleScopedPtr(extractor->focal_length_mm(), &metadata->focal_length_mm); | |
| 116 SetDoubleScopedPtr(extractor->iso_equivalent(), &metadata->iso_equivalent); | |
| 117 | |
| 118 callback.Run(metadata.Pass()); | |
| 119 } | |
| 120 | |
| 121 } // namespace | 73 } // namespace |
| 122 | 74 |
| 123 MediaMetadataParser::MediaMetadataParser(media::DataSource* source, | 75 MediaMetadataParser::MediaMetadataParser(media::DataSource* source, |
| 124 const std::string& mime_type) | 76 const std::string& mime_type) |
| 125 : source_(source), | 77 : source_(source), |
| 126 mime_type_(mime_type) { | 78 mime_type_(mime_type) { |
| 127 } | 79 } |
| 128 | 80 |
| 129 MediaMetadataParser::~MediaMetadataParser() {} | 81 MediaMetadataParser::~MediaMetadataParser() {} |
| 130 | 82 |
| 131 void MediaMetadataParser::Start(const MetadataCallback& callback) { | 83 void MediaMetadataParser::Start(const MetadataCallback& callback) { |
| 132 scoped_ptr<MediaMetadata> metadata(new MediaMetadata); | 84 scoped_ptr<MediaMetadata> metadata(new MediaMetadata); |
| 133 metadata->mime_type = mime_type_; | 85 metadata->mime_type = mime_type_; |
| 134 | 86 |
| 135 if (StartsWithASCII(mime_type_, "audio/", true) || | 87 if (StartsWithASCII(mime_type_, "audio/", true) || |
| 136 StartsWithASCII(mime_type_, "video/", true)) { | 88 StartsWithASCII(mime_type_, "video/", true)) { |
| 137 media_thread_.reset(new base::Thread("media_thread")); | 89 media_thread_.reset(new base::Thread("media_thread")); |
| 138 CHECK(media_thread_->Start()); | 90 CHECK(media_thread_->Start()); |
| 139 base::PostTaskAndReplyWithResult( | 91 base::PostTaskAndReplyWithResult( |
| 140 media_thread_->message_loop_proxy(), | 92 media_thread_->message_loop_proxy(), |
| 141 FROM_HERE, | 93 FROM_HERE, |
| 142 base::Bind(&ParseAudioVideoMetadata, source_, base::Passed(&metadata)), | 94 base::Bind(&ParseAudioVideoMetadata, source_, base::Passed(&metadata)), |
| 143 callback); | 95 callback); |
| 144 return; | 96 return; |
| 145 } | 97 } |
| 146 | 98 |
| 147 if (StartsWithASCII(mime_type_, "image/", true)) { | |
| 148 ImageMetadataExtractor* extractor = new ImageMetadataExtractor; | |
| 149 extractor->Extract( | |
| 150 source_, | |
| 151 base::Bind(&FinishParseImageMetadata, base::Owned(extractor), | |
| 152 base::Passed(&metadata), callback)); | |
| 153 return; | |
| 154 } | |
| 155 | |
| 156 // TODO(tommycli): Implement for image mime types. | 99 // TODO(tommycli): Implement for image mime types. |
| 157 callback.Run(metadata.Pass()); | 100 callback.Run(metadata.Pass()); |
| 158 } | 101 } |
| 159 | 102 |
| 160 } // namespace metadata | 103 } // namespace metadata |
| OLD | NEW |