summaryrefslogtreecommitdiff
path: root/src/librygel-server/rygel-engine-loader.vala
blob: 9331bb7f18f95ab56b9388735f3fe781bf56d634 (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
/*
 * Copyright (C) 2012 Intel Corporation.
 *
 * Author: Jens Georg <jensg@openismus.com>
 *
 * This file is part of Rygel.
 *
 * Rygel 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.
 *
 * Rygel 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 program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/**
 * Load a media engine.
 */
internal class Rygel.EngineLoader : RecursiveModuleLoader {
    private delegate MediaEngine ModuleInstanceFunc ();
    private MediaEngine instance;
    private string engine_name;

    public EngineLoader () {
        Object (base_path : get_config ());
    }

    public override void constructed () {
        base.constructed ();

        if (this.base_path == null) {
            this.base_path = get_config ();
        }

        try {
            var config = MetaConfig.get_default ();
            this.engine_name = config.get_media_engine ();
            debug ("Looking for specific engine named '%s",
                   this.engine_name);
        } catch (Error error) {}
    }

    /**
     * Load a media engine.
     */
    public MediaEngine load_engine () {
        this.load_modules_sync ();

        return instance;
    }

    protected override bool load_module_from_file (File file) {
        if (this.engine_name != null) {
            if (file.get_basename () != this.engine_name) {
                return true;
            }
        }

        var module = Module.open (file.get_path (), ModuleFlags.BIND_LOCAL);
        if (module == null) {
            debug ("Failed to load engine %s: %s",
                   file.get_path (),
                   Module.error ());
            if (this.engine_name != null) {
                // If engine name is not null, we only got here because the
                // names match. If we couldn't load the engine that matches,
                // we stop loading.
                return false;
            }

            return true;
        }

        void* function;
        if (!module.symbol ("module_get_instance", out function)) {
            if (this.engine_name != null) {
                // If engine name is not null, we only got here because the
                // names match. If we couldn't load the engine that matches,
                // we stop loading.
                return false;
            }

            return true;
        }

        unowned ModuleInstanceFunc get_instance =
                                    (ModuleInstanceFunc) function;
        module.make_resident ();
        this.instance = get_instance ();

        return false;
    }

    protected override bool load_module_from_info (PluginInformation info) {
        return load_module_from_file (File.new_for_path (info.module_path));
    }

    private static string get_config () {
        var path = BuildConfig.ENGINE_DIR;
        var config = MetaConfig.get_default ();
        try {
            path = config.get_engine_path ();
        } catch (Error error) { }

        return path;
    }
}