Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / utility / media_galleries / media_metadata_parser.cc
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.
4
5 #include "chrome/utility/media_galleries/media_metadata_parser.h"
6
7 #include <string>
8
9 #include "base/bind.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"
14
15 namespace metadata {
16
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,
33                                          const std::string& mime_type)
34     : source_(source),
35       metadata_(new MediaMetadata) {
36   metadata_->mime_type = mime_type;
37 }
38
39 MediaMetadataParser::~MediaMetadataParser() {}
40
41 void MediaMetadataParser::Start(const MetadataCallback& callback) {
42   DCHECK(callback_.is_null());
43   callback_ = callback;
44
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.
51   callback_.Run(metadata_.Pass());
52 }
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   SetIntScopedPtr(extractor.rotation(), &metadata_->rotation);
79   SetStringScopedPtr(extractor.title(), &metadata_->title);
80   SetIntScopedPtr(extractor.track(), &metadata_->track);
81 }
82
83 }  // namespace metadata