summaryrefslogtreecommitdiffstats
path: root/chrome/utility/media_galleries/media_metadata_parser.cc
blob: 22f4779958a3ef9e2afb4e423236dcde26275a5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/utility/media_galleries/media_metadata_parser.h"

#include <string>

#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_util.h"
#include "base/task_runner_util.h"
#include "base/threading/thread.h"
#include "chrome/utility/media_galleries/image_metadata_extractor.h"
#include "media/base/audio_video_metadata_extractor.h"
#include "media/base/data_source.h"

namespace metadata {

namespace {

void SetStringScopedPtr(const std::string& value,
                        scoped_ptr<std::string>* destination) {
  DCHECK(destination);
  if (!value.empty())
    destination->reset(new std::string(value));
}

void SetIntScopedPtr(int value, scoped_ptr<int>* destination) {
  DCHECK(destination);
  if (value >= 0)
    destination->reset(new int(value));
}

void SetDoubleScopedPtr(double value, scoped_ptr<double>* destination) {
  DCHECK(destination);
  if (value >= 0)
    destination->reset(new double(value));
}

void SetBoolScopedPtr(bool value, scoped_ptr<bool>* destination) {
  DCHECK(destination);
  destination->reset(new bool(value));
}

// This runs on |media_thread_|, as the underlying FFmpeg operation is
// blocking, and the utility thread must not be blocked, so the media file
// bytes can be sent from the browser process to the utility process.
scoped_ptr<MediaMetadataParser::MediaMetadata> ParseAudioVideoMetadata(
    media::DataSource* source,
    scoped_ptr<MediaMetadataParser::MediaMetadata> metadata) {
  DCHECK(source);
  DCHECK(metadata.get());
  media::AudioVideoMetadataExtractor extractor;

  if (!extractor.Extract(source))
    return metadata.Pass();

  if (extractor.duration() >= 0)
    metadata->duration.reset(new double(extractor.duration()));

  if (extractor.height() >= 0 && extractor.width() >= 0) {
    metadata->height.reset(new int(extractor.height()));
    metadata->width.reset(new int(extractor.width()));
  }

  SetStringScopedPtr(extractor.artist(), &metadata->artist);
  SetStringScopedPtr(extractor.album(), &metadata->album);
  SetStringScopedPtr(extractor.artist(), &metadata->artist);
  SetStringScopedPtr(extractor.comment(), &metadata->comment);
  SetStringScopedPtr(extractor.copyright(), &metadata->copyright);
  SetIntScopedPtr(extractor.disc(), &metadata->disc);
  SetStringScopedPtr(extractor.genre(), &metadata->genre);
  SetStringScopedPtr(extractor.language(), &metadata->language);
  SetIntScopedPtr(extractor.rotation(), &metadata->rotation);
  SetStringScopedPtr(extractor.title(), &metadata->title);
  SetIntScopedPtr(extractor.track(), &metadata->track);

  return metadata.Pass();
}

void FinishParseImageMetadata(
    ImageMetadataExtractor* extractor,
    scoped_ptr<MediaMetadataParser::MediaMetadata> metadata,
    MediaMetadataParser::MetadataCallback callback,
    bool extract_success) {
  DCHECK(extractor);
  DCHECK(metadata.get());

  if (!extract_success) {
    callback.Run(metadata.Pass());
    return;
  }

  SetIntScopedPtr(extractor->height(), &metadata->height);
  SetIntScopedPtr(extractor->width(), &metadata->width);

  SetIntScopedPtr(extractor->rotation(), &metadata->rotation);

  SetDoubleScopedPtr(extractor->x_resolution(), &metadata->x_resolution);
  SetDoubleScopedPtr(extractor->y_resolution(), &metadata->y_resolution);
  SetBoolScopedPtr(extractor->flash_fired(), &metadata->flash_fired);
  SetStringScopedPtr(extractor->camera_make(), &metadata->camera_make);
  SetStringScopedPtr(extractor->camera_model(), &metadata->camera_model);
  SetDoubleScopedPtr(extractor->exposure_time_sec(),
                     &metadata->exposure_time_seconds);

  SetDoubleScopedPtr(extractor->f_number(), &metadata->f_number);
  SetDoubleScopedPtr(extractor->focal_length_mm(), &metadata->focal_length_mm);
  SetDoubleScopedPtr(extractor->iso_equivalent(), &metadata->iso_equivalent);

  callback.Run(metadata.Pass());
}

}  // namespace

MediaMetadataParser::MediaMetadataParser(media::DataSource* source,
                                         const std::string& mime_type)
    : source_(source),
      mime_type_(mime_type) {
}

MediaMetadataParser::~MediaMetadataParser() {}

void MediaMetadataParser::Start(const MetadataCallback& callback) {
  scoped_ptr<MediaMetadata> metadata(new MediaMetadata);
  metadata->mime_type = mime_type_;

  if (StartsWithASCII(mime_type_, "audio/", true) ||
      StartsWithASCII(mime_type_, "video/", true)) {
    media_thread_.reset(new base::Thread("media_thread"));
    CHECK(media_thread_->Start());
    base::PostTaskAndReplyWithResult(
        media_thread_->message_loop_proxy(),
        FROM_HERE,
        base::Bind(&ParseAudioVideoMetadata, source_, base::Passed(&metadata)),
        callback);
    return;
  }

  if (StartsWithASCII(mime_type_, "image/", true)) {
    ImageMetadataExtractor* extractor = new ImageMetadataExtractor;
    extractor->Extract(
        source_,
        base::Bind(&FinishParseImageMetadata, base::Owned(extractor),
                   base::Passed(&metadata), callback));
    return;
  }

  // TODO(tommycli): Implement for image mime types.
  callback.Run(metadata.Pass());
}

}  // namespace metadata