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
3 // found in the LICENSE file.
5 #include "chrome/utility/media_galleries/media_metadata_parser.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"
19 void SetStringScopedPtr(const std::string
& value
,
20 scoped_ptr
<std::string
>* destination
) {
22 destination
->reset(new std::string(value
));
25 void SetIntScopedPtr(int value
, scoped_ptr
<int>* destination
) {
27 destination
->reset(new int(value
));
32 MediaMetadataParser::MediaMetadataParser(media::DataSource
* source
,
33 const std::string
& mime_type
)
35 metadata_(new MediaMetadata
) {
36 metadata_
->mime_type
= mime_type
;
39 MediaMetadataParser::~MediaMetadataParser() {}
41 void MediaMetadataParser::Start(const MetadataCallback
& callback
) {
42 DCHECK(callback_
.is_null());
45 if (StartsWithASCII(metadata_
->mime_type
, "audio/", true) ||
46 StartsWithASCII(metadata_
->mime_type
, "video/", true)) {
47 PopulateAudioVideoMetadata();
50 // TODO(tommycli): Implement for image mime types.
51 callback_
.Run(metadata_
.Pass());
54 void MediaMetadataParser::PopulateAudioVideoMetadata() {
56 DCHECK(metadata_
.get());
57 media::AudioVideoMetadataExtractor extractor
;
59 if (!extractor
.Extract(source_
))
62 if (extractor
.duration() >= 0)
63 metadata_
->duration
.reset(new double(extractor
.duration()));
65 if (extractor
.height() >= 0 && extractor
.width() >= 0) {
66 metadata_
->height
.reset(new int(extractor
.height()));
67 metadata_
->width
.reset(new int(extractor
.width()));
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
);
82 } // namespace metadata