summaryrefslogtreecommitdiff
path: root/libgupnp-dlna/gupnp-dlna-metadata-backend.c
blob: a8cad109175a26ddbb43ee3643817ecbd423a2ec (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
/*
 * Copyright (C) 2012 Intel Corporation.
 *
 * Authors: Krzesimir Nowak <krnowak@openismus.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gmodule.h>
#include "gupnp-dlna-metadata-backend.h"

#define GET_DEFAULT_EXTRACTOR_SYMBOL "gupnp_dlna_get_default_extractor"

struct {
        GModule *module;
        GUPnPDLNAMetadataExtractor * (* get_default_extractor) (void);
} metadata_backend;

static gboolean
load_metadata_backend (void)
{
        static gsize backend_chosen = 0;

        if (g_once_init_enter (&backend_chosen)) {
                gchar **environment = g_get_environ ();
                const gchar *backend =
                               g_environ_getenv (environment,
                                                 "GUPNP_DLNA_METADATA_BACKEND");
                const gchar *backend_dir =
                           g_environ_getenv (environment,
                                             "GUPNP_DLNA_METADATA_BACKEND_DIR");
                GModule *module;
                gchar *module_path;
                gpointer get_default_extractor = NULL;
                gsize loaded = 1;

                if (!backend)
                        backend = GUPNP_DLNA_DEFAULT_METADATA_BACKEND;
                if (!backend_dir)
                        backend_dir = GUPNP_DLNA_DEFAULT_METADATA_BACKEND_DIR;
                module_path = g_module_build_path (backend_dir, backend);
                module = g_module_open (module_path, G_MODULE_BIND_MASK);

                if (!module) {
                        g_warning ("Could not load open metadata backend '%s'.",
                                   module_path);

                        goto fail;
                }
                if (!g_module_symbol (module,
                                      GET_DEFAULT_EXTRACTOR_SYMBOL,
                                      &get_default_extractor)) {
                        g_warning ("Could not find '"
                                   GET_DEFAULT_EXTRACTOR_SYMBOL
                                   "' symbol in '%s'.",
                                   module_path);

                        goto fail;
                }
                if (!get_default_extractor) {
                        g_warning ("'"
                                   GET_DEFAULT_EXTRACTOR_SYMBOL
                                   "' symbol in '%s' is invalid.",
                                   module_path);

                        goto fail;
                }
                g_module_make_resident (module);
                metadata_backend.module = module;
                metadata_backend.get_default_extractor = get_default_extractor;
                module = NULL;
                loaded = 2;
        fail:
                g_free (module_path);
                if (module)
                        g_module_close (module);
                g_strfreev (environment);
                g_once_init_leave (&backend_chosen, loaded);
        }

        return (backend_chosen == 2);
}

GUPnPDLNAMetadataExtractor *
gupnp_dlna_metadata_backend_get_extractor (void)
{
        gboolean metadata_backend_loaded = load_metadata_backend ();

        g_return_val_if_fail (metadata_backend_loaded == TRUE, NULL);

        return metadata_backend.get_default_extractor ();
}