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/memory/linked_ptr.h" | 10 #include "base/memory/linked_ptr.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/task_runner_util.h" | 13 #include "base/task_runner_util.h" |
14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
15 #include "chrome/utility/media_galleries/image_metadata_extractor.h" | 15 #include "chrome/utility/media_galleries/image_metadata_extractor.h" |
16 #include "media/base/audio_video_metadata_extractor.h" | 16 #include "media/base/audio_video_metadata_extractor.h" |
17 #include "media/base/data_source.h" | 17 #include "media/base/data_source.h" |
18 #include "net/base/mime_sniffer.h" | |
18 | 19 |
19 namespace MediaGalleries = extensions::api::media_galleries; | 20 namespace MediaGalleries = extensions::api::media_galleries; |
20 | 21 |
21 namespace metadata { | 22 namespace metadata { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 void SetStringScopedPtr(const std::string& value, | 26 void SetStringScopedPtr(const std::string& value, |
26 scoped_ptr<std::string>* destination) { | 27 scoped_ptr<std::string>* destination) { |
27 DCHECK(destination); | 28 DCHECK(destination); |
(...skipping 14 matching lines...) Expand all Loading... | |
42 } | 43 } |
43 | 44 |
44 void SetBoolScopedPtr(bool value, scoped_ptr<bool>* destination) { | 45 void SetBoolScopedPtr(bool value, scoped_ptr<bool>* destination) { |
45 DCHECK(destination); | 46 DCHECK(destination); |
46 destination->reset(new bool(value)); | 47 destination->reset(new bool(value)); |
47 } | 48 } |
48 | 49 |
49 // This runs on |media_thread_|, as the underlying FFmpeg operation is | 50 // This runs on |media_thread_|, as the underlying FFmpeg operation is |
50 // blocking, and the utility thread must not be blocked, so the media file | 51 // blocking, and the utility thread must not be blocked, so the media file |
51 // bytes can be sent from the browser process to the utility process. | 52 // bytes can be sent from the browser process to the utility process. |
52 scoped_ptr<MediaMetadataParser::MediaMetadata> ParseAudioVideoMetadata( | 53 void ParseAudioVideoMetadata( |
53 media::DataSource* source, | 54 media::DataSource* source, bool get_attached_pictures, |
54 scoped_ptr<MediaMetadataParser::MediaMetadata> metadata) { | 55 MediaMetadataParser::MediaMetadata* metadata, |
56 std::vector<std::string>* attached_pictures_bytes, | |
vandebo (ex-Chrome)
2014/04/23 23:22:45
Maybe there should be a struct ThumbnailData {
std
tommycli
2014/04/29 00:15:51
Done.
| |
57 std::vector<std::string>* attached_pictures_types) { | |
55 DCHECK(source); | 58 DCHECK(source); |
56 DCHECK(metadata.get()); | 59 DCHECK(metadata); |
57 media::AudioVideoMetadataExtractor extractor; | 60 media::AudioVideoMetadataExtractor extractor; |
58 | 61 |
59 if (!extractor.Extract(source)) | 62 if (!extractor.Extract(source, get_attached_pictures)) |
60 return metadata.Pass(); | 63 return; |
61 | 64 |
62 if (extractor.duration() >= 0) | 65 if (extractor.duration() >= 0) |
63 metadata->duration.reset(new double(extractor.duration())); | 66 metadata->duration.reset(new double(extractor.duration())); |
64 | 67 |
65 if (extractor.height() >= 0 && extractor.width() >= 0) { | 68 if (extractor.height() >= 0 && extractor.width() >= 0) { |
66 metadata->height.reset(new int(extractor.height())); | 69 metadata->height.reset(new int(extractor.height())); |
67 metadata->width.reset(new int(extractor.width())); | 70 metadata->width.reset(new int(extractor.width())); |
68 } | 71 } |
69 | 72 |
70 SetStringScopedPtr(extractor.artist(), &metadata->artist); | 73 SetStringScopedPtr(extractor.artist(), &metadata->artist); |
(...skipping 18 matching lines...) Expand all Loading... | |
89 for (std::map<std::string, std::string>::const_iterator tag_it = | 92 for (std::map<std::string, std::string>::const_iterator tag_it = |
90 it->tags.begin(); | 93 it->tags.begin(); |
91 tag_it != it->tags.end(); ++tag_it) { | 94 tag_it != it->tags.end(); ++tag_it) { |
92 stream_info->tags.additional_properties.SetString(tag_it->first, | 95 stream_info->tags.additional_properties.SetString(tag_it->first, |
93 tag_it->second); | 96 tag_it->second); |
94 } | 97 } |
95 | 98 |
96 metadata->raw_tags.push_back(stream_info); | 99 metadata->raw_tags.push_back(stream_info); |
97 } | 100 } |
98 | 101 |
99 return metadata.Pass(); | 102 if (get_attached_pictures) { |
103 DCHECK(attached_pictures_bytes); | |
104 DCHECK(attached_pictures_types); | |
105 *attached_pictures_bytes = extractor.attached_pictures_bytes(); | |
106 for (std::vector<std::string>::const_iterator it = | |
107 attached_pictures_bytes->begin(); | |
108 it != attached_pictures_bytes->end(); ++it) { | |
109 std::string type; | |
110 net::SniffMimeTypeFromLocalData(it->c_str(), it->length(), &type); | |
111 attached_pictures_types->push_back(type); | |
112 } | |
113 } | |
114 } | |
115 | |
116 void FinishParseAudioVideoMetadata( | |
117 MediaMetadataParser::MetadataCallback callback, | |
vandebo (ex-Chrome)
2014/04/23 23:22:45
Is this needed? Can you just bind to the callback
tommycli
2014/04/29 00:15:51
Needed so that the callback could base::Owned the
| |
118 scoped_ptr<MediaMetadataParser::MediaMetadata> metadata, | |
119 std::vector<std::string>* attached_pictures_bytes, | |
120 std::vector<std::string>* attached_pictures_types) { | |
121 DCHECK(!callback.is_null()); | |
122 DCHECK(metadata.get()); | |
123 DCHECK(attached_pictures_bytes); | |
124 DCHECK(attached_pictures_types); | |
125 | |
126 callback.Run(metadata.Pass(), *attached_pictures_bytes, | |
127 *attached_pictures_types); | |
100 } | 128 } |
101 | 129 |
102 void FinishParseImageMetadata( | 130 void FinishParseImageMetadata( |
103 ImageMetadataExtractor* extractor, | 131 ImageMetadataExtractor* extractor, |
104 scoped_ptr<MediaMetadataParser::MediaMetadata> metadata, | 132 scoped_ptr<MediaMetadataParser::MediaMetadata> metadata, |
105 MediaMetadataParser::MetadataCallback callback, | 133 MediaMetadataParser::MetadataCallback callback, |
106 bool extract_success) { | 134 bool extract_success) { |
107 DCHECK(extractor); | 135 DCHECK(extractor); |
108 DCHECK(metadata.get()); | 136 DCHECK(metadata.get()); |
109 | 137 |
110 if (!extract_success) { | 138 if (!extract_success) { |
111 callback.Run(metadata.Pass()); | 139 callback.Run(metadata.Pass(), std::vector<std::string>(), |
140 std::vector<std::string>()); | |
112 return; | 141 return; |
113 } | 142 } |
114 | 143 |
115 SetIntScopedPtr(extractor->height(), &metadata->height); | 144 SetIntScopedPtr(extractor->height(), &metadata->height); |
116 SetIntScopedPtr(extractor->width(), &metadata->width); | 145 SetIntScopedPtr(extractor->width(), &metadata->width); |
117 | 146 |
118 SetIntScopedPtr(extractor->rotation(), &metadata->rotation); | 147 SetIntScopedPtr(extractor->rotation(), &metadata->rotation); |
119 | 148 |
120 SetDoubleScopedPtr(extractor->x_resolution(), &metadata->x_resolution); | 149 SetDoubleScopedPtr(extractor->x_resolution(), &metadata->x_resolution); |
121 SetDoubleScopedPtr(extractor->y_resolution(), &metadata->y_resolution); | 150 SetDoubleScopedPtr(extractor->y_resolution(), &metadata->y_resolution); |
122 SetBoolScopedPtr(extractor->flash_fired(), &metadata->flash_fired); | 151 SetBoolScopedPtr(extractor->flash_fired(), &metadata->flash_fired); |
123 SetStringScopedPtr(extractor->camera_make(), &metadata->camera_make); | 152 SetStringScopedPtr(extractor->camera_make(), &metadata->camera_make); |
124 SetStringScopedPtr(extractor->camera_model(), &metadata->camera_model); | 153 SetStringScopedPtr(extractor->camera_model(), &metadata->camera_model); |
125 SetDoubleScopedPtr(extractor->exposure_time_sec(), | 154 SetDoubleScopedPtr(extractor->exposure_time_sec(), |
126 &metadata->exposure_time_seconds); | 155 &metadata->exposure_time_seconds); |
127 | 156 |
128 SetDoubleScopedPtr(extractor->f_number(), &metadata->f_number); | 157 SetDoubleScopedPtr(extractor->f_number(), &metadata->f_number); |
129 SetDoubleScopedPtr(extractor->focal_length_mm(), &metadata->focal_length_mm); | 158 SetDoubleScopedPtr(extractor->focal_length_mm(), &metadata->focal_length_mm); |
130 SetDoubleScopedPtr(extractor->iso_equivalent(), &metadata->iso_equivalent); | 159 SetDoubleScopedPtr(extractor->iso_equivalent(), &metadata->iso_equivalent); |
131 | 160 |
132 callback.Run(metadata.Pass()); | 161 callback.Run(metadata.Pass(), std::vector<std::string>(), |
162 std::vector<std::string>()); | |
133 } | 163 } |
134 | 164 |
135 } // namespace | 165 } // namespace |
136 | 166 |
137 MediaMetadataParser::MediaMetadataParser(media::DataSource* source, | 167 MediaMetadataParser::MediaMetadataParser(media::DataSource* source, |
138 const std::string& mime_type) | 168 const std::string& mime_type, |
169 bool get_attached_pictures) | |
139 : source_(source), | 170 : source_(source), |
140 mime_type_(mime_type) { | 171 mime_type_(mime_type), |
172 get_attached_pictures_(get_attached_pictures) { | |
141 } | 173 } |
142 | 174 |
143 MediaMetadataParser::~MediaMetadataParser() {} | 175 MediaMetadataParser::~MediaMetadataParser() {} |
144 | 176 |
145 void MediaMetadataParser::Start(const MetadataCallback& callback) { | 177 void MediaMetadataParser::Start(const MetadataCallback& callback) { |
146 scoped_ptr<MediaMetadata> metadata(new MediaMetadata); | 178 scoped_ptr<MediaMetadata> metadata(new MediaMetadata); |
147 metadata->mime_type = mime_type_; | 179 metadata->mime_type = mime_type_; |
148 | 180 |
149 if (StartsWithASCII(mime_type_, "audio/", true) || | 181 if (StartsWithASCII(mime_type_, "audio/", true) || |
150 StartsWithASCII(mime_type_, "video/", true)) { | 182 StartsWithASCII(mime_type_, "video/", true)) { |
183 std::vector<std::string>* attached_pictures_bytes = | |
184 new std::vector<std::string>; | |
185 std::vector<std::string>* attached_pictures_types = | |
186 new std::vector<std::string>; | |
187 | |
151 media_thread_.reset(new base::Thread("media_thread")); | 188 media_thread_.reset(new base::Thread("media_thread")); |
152 CHECK(media_thread_->Start()); | 189 CHECK(media_thread_->Start()); |
153 base::PostTaskAndReplyWithResult( | 190 media_thread_->message_loop_proxy()->PostTaskAndReply( |
154 media_thread_->message_loop_proxy(), | |
155 FROM_HERE, | 191 FROM_HERE, |
156 base::Bind(&ParseAudioVideoMetadata, source_, base::Passed(&metadata)), | 192 base::Bind(&ParseAudioVideoMetadata, source_, get_attached_pictures_, |
157 callback); | 193 metadata.get(), attached_pictures_bytes, |
194 attached_pictures_types), | |
195 base::Bind(&FinishParseAudioVideoMetadata, callback, | |
196 base::Passed(&metadata), | |
197 base::Owned(attached_pictures_bytes), | |
198 base::Owned(attached_pictures_types))); | |
158 return; | 199 return; |
159 } | 200 } |
160 | 201 |
161 if (StartsWithASCII(mime_type_, "image/", true)) { | 202 if (StartsWithASCII(mime_type_, "image/", true)) { |
162 ImageMetadataExtractor* extractor = new ImageMetadataExtractor; | 203 ImageMetadataExtractor* extractor = new ImageMetadataExtractor; |
163 extractor->Extract( | 204 extractor->Extract( |
164 source_, | 205 source_, |
165 base::Bind(&FinishParseImageMetadata, base::Owned(extractor), | 206 base::Bind(&FinishParseImageMetadata, base::Owned(extractor), |
166 base::Passed(&metadata), callback)); | 207 base::Passed(&metadata), callback)); |
167 return; | 208 return; |
168 } | 209 } |
169 | 210 |
170 // TODO(tommycli): Implement for image mime types. | 211 callback.Run(metadata.Pass(), std::vector<std::string>(), |
171 callback.Run(metadata.Pass()); | 212 std::vector<std::string>()); |
172 } | 213 } |
173 | 214 |
174 } // namespace metadata | 215 } // namespace metadata |
OLD | NEW |