~ci-train-bot/mediascanner2/mediascanner2-ubuntu-xenial-landing-079

« back to all changes in this revision

Viewing changes to src/extractor/GStreamerExtractor.cc

  • Committer: CI Train Bot
  • Author(s): James Henstridge
  • Date: 2016-02-25 01:53:20 UTC
  • mfrom: (320.2.12 taglib-extractor)
  • Revision ID: ci-train-bot@canonical.com-20160225015320-lw52fiyl9pt7bejq
Use taglib to extract metadata from Vorbis, Opus, Flac, MP3 and MP4 audio files.  Other formats will fall back to the existing GStreamer code path. Fixes: #1536832
Approved by: Michi Henning

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013-2014 Canonical, Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *    Jussi Pakkanen <jussi.pakkanen@canonical.com>
 
6
 *    James Henstridge <james.henstridge@canonical.com>
 
7
 *
 
8
 * This library is free software; you can redistribute it and/or modify it under
 
9
 * the terms of version 3 of the GNU General Public License as published
 
10
 * by the Free Software Foundation.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful, but WITHOUT
 
13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
14
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 
15
 * details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
#include "GStreamerExtractor.hh"
 
22
#include "DetectedFile.hh"
 
23
#include "../mediascanner/MediaFile.hh"
 
24
#include "../mediascanner/MediaFileBuilder.hh"
 
25
#include "../mediascanner/internal/utils.hh"
 
26
 
 
27
#include <glib-object.h>
 
28
#include <gio/gio.h>
 
29
#include <gst/gst.h>
 
30
#include <gst/pbutils/pbutils.h>
 
31
 
 
32
#include <memory>
 
33
#include <string>
 
34
#include <stdexcept>
 
35
 
 
36
using namespace std;
 
37
using mediascanner::MediaFileBuilder;
 
38
 
 
39
namespace {
 
40
 
 
41
void extract_tag_info (const GstTagList *list, const gchar *tag, void *user_data) {
 
42
    MediaFileBuilder *mfb = (MediaFileBuilder *) user_data;
 
43
    int i, num;
 
44
    string tagname(tag);
 
45
 
 
46
    if(tagname == GST_TAG_IMAGE || tagname == GST_TAG_PREVIEW_IMAGE) {
 
47
        mfb->setHasThumbnail(true);
 
48
        return;
 
49
    }
 
50
    num = gst_tag_list_get_tag_size (list, tag);
 
51
    for (i = 0; i < num; ++i) {
 
52
        const GValue *val;
 
53
 
 
54
        val = gst_tag_list_get_value_index (list, tag, i);
 
55
        if (G_VALUE_HOLDS_STRING(val)) {
 
56
            const char *value = g_value_get_string(val);
 
57
            if (!value) {
 
58
                continue;
 
59
            }
 
60
            if (tagname == GST_TAG_ARTIST)
 
61
                mfb->setAuthor(value);
 
62
            else if (tagname == GST_TAG_TITLE)
 
63
                mfb->setTitle(value);
 
64
            else if (tagname == GST_TAG_ALBUM)
 
65
                mfb->setAlbum(value);
 
66
            else if (tagname == GST_TAG_ALBUM_ARTIST)
 
67
                mfb->setAlbumArtist(value);
 
68
            else if (tagname == GST_TAG_GENRE)
 
69
                mfb->setGenre(value);
 
70
        } else if (G_VALUE_HOLDS(val, GST_TYPE_DATE_TIME)) {
 
71
            if (tagname == GST_TAG_DATE_TIME) {
 
72
                GstDateTime *dt = static_cast<GstDateTime*>(g_value_get_boxed(val));
 
73
                if (!dt) {
 
74
                    continue;
 
75
                }
 
76
                char *dt_string = gst_date_time_to_iso8601_string(dt);
 
77
                mfb->setDate(dt_string);
 
78
                g_free(dt_string);
 
79
            }
 
80
        } else if (G_VALUE_HOLDS(val, G_TYPE_DATE)) {
 
81
            if (tagname == GST_TAG_DATE) {
 
82
                GDate *dt = static_cast<GDate*>(g_value_get_boxed(val));
 
83
                if (!dt) {
 
84
                    continue;
 
85
                }
 
86
                char buf[100];
 
87
                if (g_date_strftime(buf, sizeof(buf), "%Y-%m-%d", dt) != 0) {
 
88
                    mfb->setDate(buf);
 
89
                }
 
90
            }
 
91
        } else if (G_VALUE_HOLDS_UINT(val)) {
 
92
            if (tagname == GST_TAG_TRACK_NUMBER) {
 
93
                mfb->setTrackNumber(g_value_get_uint(val));
 
94
            } else if (tagname == GST_TAG_ALBUM_VOLUME_NUMBER) {
 
95
                mfb->setDiscNumber(g_value_get_uint(val));
 
96
            }
 
97
        }
 
98
 
 
99
    }
 
100
}
 
101
 
 
102
}
 
103
 
 
104
namespace mediascanner {
 
105
 
 
106
GStreamerExtractor::GStreamerExtractor(int seconds)
 
107
    : discoverer(nullptr, g_object_unref) {
 
108
    GError *error = nullptr;
 
109
 
 
110
    discoverer.reset(gst_discoverer_new(GST_SECOND * seconds, &error));
 
111
    if (not discoverer) {
 
112
        string errortxt(error->message);
 
113
        g_error_free(error);
 
114
 
 
115
        string msg = "Failed to create discoverer: ";
 
116
        msg += errortxt;
 
117
        throw runtime_error(msg);
 
118
    }
 
119
    if (error) {
 
120
        // Sometimes this is filled in even though no error happens.
 
121
        g_error_free(error);
 
122
    }
 
123
}
 
124
 
 
125
GStreamerExtractor::~GStreamerExtractor() = default;
 
126
 
 
127
void GStreamerExtractor::extract(const DetectedFile &d, MediaFileBuilder &mfb) {
 
128
    string uri = getUri(d.filename);
 
129
 
 
130
    GError *error = nullptr;
 
131
    unique_ptr<GstDiscovererInfo, void(*)(void *)> info(
 
132
        gst_discoverer_discover_uri(discoverer.get(), uri.c_str(), &error),
 
133
        g_object_unref);
 
134
    if (info.get() == nullptr) {
 
135
        string errortxt(error->message);
 
136
        g_error_free(error);
 
137
 
 
138
        string msg = "Discovery of file ";
 
139
        msg += d.filename;
 
140
        msg += " failed: ";
 
141
        msg += errortxt;
 
142
        throw runtime_error(msg);
 
143
    }
 
144
    if (error) {
 
145
        // Sometimes this gets filled in even if no error actually occurs.
 
146
        g_error_free(error);
 
147
        error = nullptr;
 
148
    }
 
149
 
 
150
    if (gst_discoverer_info_get_result(info.get()) != GST_DISCOVERER_OK) {
 
151
        throw runtime_error("Unable to discover file " + d.filename);
 
152
    }
 
153
 
 
154
    const GstTagList *tags = gst_discoverer_info_get_tags(info.get());
 
155
    if (tags != nullptr) {
 
156
        gst_tag_list_foreach(tags, extract_tag_info, &mfb);
 
157
    }
 
158
    mfb.setDuration(static_cast<int>(
 
159
        gst_discoverer_info_get_duration(info.get())/GST_SECOND));
 
160
 
 
161
    /* Check for video specific information */
 
162
    unique_ptr<GList, void(*)(GList*)> streams(
 
163
        gst_discoverer_info_get_stream_list(info.get()),
 
164
        gst_discoverer_stream_info_list_free);
 
165
    for (const GList *l = streams.get(); l != nullptr; l = l->next) {
 
166
        auto stream_info = static_cast<GstDiscovererStreamInfo*>(l->data);
 
167
 
 
168
        if (GST_IS_DISCOVERER_VIDEO_INFO(stream_info)) {
 
169
            mfb.setWidth(gst_discoverer_video_info_get_width(
 
170
                             GST_DISCOVERER_VIDEO_INFO(stream_info)));
 
171
            mfb.setHeight(gst_discoverer_video_info_get_height(
 
172
                              GST_DISCOVERER_VIDEO_INFO(stream_info)));
 
173
            break;
 
174
        }
 
175
    }
 
176
}
 
177
 
 
178
}