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 "base/task_runner_util.h"
13 #include "base/threading/thread.h"
14 #include "media/base/audio_video_metadata_extractor.h"
15 #include "media/base/data_source.h"
21 void SetStringScopedPtr(const std::string
& value
,
22 scoped_ptr
<std::string
>* destination
) {
24 destination
->reset(new std::string(value
));
27 void SetIntScopedPtr(int value
, scoped_ptr
<int>* destination
) {
29 destination
->reset(new int(value
));
32 // This runs on |media_thread_|, as the underlying FFmpeg operation is
33 // blocking, and the utility thread must not be blocked, so the media file
34 // bytes can be sent from the browser process to the utility process.
35 scoped_ptr
<MediaMetadataParser::MediaMetadata
> ParseAudioVideoMetadata(
36 media::DataSource
* source
,
37 scoped_ptr
<MediaMetadataParser::MediaMetadata
> metadata
) {
39 media::AudioVideoMetadataExtractor extractor
;
41 if (!extractor
.Extract(source
))
42 return metadata
.Pass();
44 if (extractor
.duration() >= 0)
45 metadata
->duration
.reset(new double(extractor
.duration()));
47 if (extractor
.height() >= 0 && extractor
.width() >= 0) {
48 metadata
->height
.reset(new int(extractor
.height()));
49 metadata
->width
.reset(new int(extractor
.width()));
52 SetStringScopedPtr(extractor
.artist(), &metadata
->artist
);
53 SetStringScopedPtr(extractor
.album(), &metadata
->album
);
54 SetStringScopedPtr(extractor
.artist(), &metadata
->artist
);
55 SetStringScopedPtr(extractor
.comment(), &metadata
->comment
);
56 SetStringScopedPtr(extractor
.copyright(), &metadata
->copyright
);
57 SetIntScopedPtr(extractor
.disc(), &metadata
->disc
);
58 SetStringScopedPtr(extractor
.genre(), &metadata
->genre
);
59 SetStringScopedPtr(extractor
.language(), &metadata
->language
);
60 SetIntScopedPtr(extractor
.rotation(), &metadata
->rotation
);
61 SetStringScopedPtr(extractor
.title(), &metadata
->title
);
62 SetIntScopedPtr(extractor
.track(), &metadata
->track
);
64 return metadata
.Pass();
69 MediaMetadataParser::MediaMetadataParser(media::DataSource
* source
,
70 const std::string
& mime_type
)
72 mime_type_(mime_type
) {
75 MediaMetadataParser::~MediaMetadataParser() {}
77 void MediaMetadataParser::Start(const MetadataCallback
& callback
) {
78 scoped_ptr
<MediaMetadata
> metadata(new MediaMetadata
);
79 metadata
->mime_type
= mime_type_
;
81 if (StartsWithASCII(mime_type_
, "audio/", true) ||
82 StartsWithASCII(mime_type_
, "video/", true)) {
83 media_thread_
.reset(new base::Thread("media_thread"));
84 CHECK(media_thread_
->Start());
85 base::PostTaskAndReplyWithResult(
86 media_thread_
->message_loop_proxy(),
88 base::Bind(&ParseAudioVideoMetadata
, source_
, base::Passed(&metadata
)),
93 // TODO(tommycli): Implement for image mime types.
94 callback
.Run(metadata
.Pass());
97 } // namespace metadata