summaryrefslogtreecommitdiff
path: root/src/librygel-renderer/rygel-media-renderer-plugin.vala
blob: ab1acff033d58feadf5c4d467e272682e794c6c4 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (C) 2008,2010 Nokia Corporation.
 * Copyright (C) 2012 Intel Corporation.
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *                               <zeeshan.ali@nokia.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.1 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * This is the base class for every Rygel UPnP renderer plugin.
 *
 * This class is useful when implementing Rygel renderer plugins.
 *
 * Renderer plugins should also implement their own #RygelMediaPlayer
 * and return an instance of it from their get_player() implementation.
 */
public class Rygel.MediaRendererPlugin : Rygel.Plugin {
    private const string MEDIA_RENDERER_DESC_PATH =
                                BuildConfig.DATA_DIR +
                                "/xml/MediaRenderer2.xml";
    private const string DMR = "urn:schemas-upnp-org:device:MediaRenderer";

    private string sink_protocol_info;
    private PlayerController controller;

    private GLib.List<DLNAProfile> _supported_profiles;

    /// A list of DLNA profiles the MediaRenderer in this plug-in will accept
    /// for rendering.
    public unowned GLib.List<unowned DLNAProfile> supported_profiles {
        get {
            return _supported_profiles;
        }

        construct set {
            _supported_profiles = null;
            if (value != null) {
                foreach (var profile in value) {
                    _supported_profiles.prepend (profile);
                }

                _supported_profiles.prepend (new DLNAProfile ("DIDL_S",
                                                              "text/xml"));
                _supported_profiles.reverse ();
            }
        }
    }

    /**
     * Create an instance of the plugin.
     *
     * @param name The non-human-readable name for the plugin and its renderer, used in UPnP messages and in the Rygel configuration file.
     * @param title An optional human-readable name (friendlyName) of the UPnP renderer provided by the plugin. If the title is empty then the name will be used.
     * @param description An optional human-readable description (modelDescription) of the UPnP renderer provided by the plugin.
     */
    public MediaRendererPlugin (string  name,
                                string? title,
                                string? description = null,
                                PluginCapabilities capabilities =
                                        PluginCapabilities.NONE) {
        Object (desc_path : MEDIA_RENDERER_DESC_PATH,
                name : name,
                title : title,
                description : description,
                capabilities : capabilities);
    }

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

        var resource = new ResourceInfo (ConnectionManager.UPNP_ID,
                                         ConnectionManager.UPNP_TYPE,
                                         ConnectionManager.DESCRIPTION_PATH,
                                         typeof (SinkConnectionManager));
        this.add_resource (resource);

        resource = new ResourceInfo (AVTransport.UPNP_ID,
                                     AVTransport.UPNP_TYPE,
                                     AVTransport.DESCRIPTION_PATH,
                                     typeof (AVTransport));
        this.add_resource (resource);

        resource = new ResourceInfo (RenderingControl.UPNP_ID,
                                     RenderingControl.UPNP_TYPE,
                                     RenderingControl.DESCRIPTION_PATH,
                                     typeof (RenderingControl));
        this.add_resource (resource);
    }

    public virtual MediaPlayer? get_player () {
        return null;
    }

    internal virtual PlayerController get_controller () {
        if (this.controller == null) {
            this.controller = new DefaultPlayerController (this.get_player (),
                                                    this.get_protocol_info ());
        }

        return this.controller;
    }

    public override void apply_hacks (RootDevice device,
                                      string     description_path)
                                      throws Error {
        string[] services = { AVTransport.UPNP_TYPE,
                              RenderingControl.UPNP_TYPE,
                              ConnectionManager.UPNP_TYPE };
        var v1_hacks = new V1Hacks (DMR, services);
        v1_hacks.apply_on_device (device, description_path);
    }


    public string get_protocol_info () {
        var player = this.get_player ();
        if (player == null) {
            return "";
        }

        if (this.sink_protocol_info == null) {
            this.sink_protocol_info = "";

            var protocols = player.get_protocols ();

            foreach (var protocol in protocols) {
                if (protocols[0] != protocol) {
                    this.sink_protocol_info += ",";
                }

                foreach (var profile in this.supported_profiles) {
                    if (supported_profiles.data != profile) {
                        this.sink_protocol_info += ",";
                    }
                    this.sink_protocol_info += protocol + ":*:" +
                                               profile.mime +
                                               ":DLNA.ORG_PN=" + profile.name;
                }
            }

            var mime_types = player.get_mime_types ();

            mime_types += "audio/mpegurl";
            mime_types += "audio/x-mpegurl";
            mime_types += "video/mpegurl";
            mime_types += "video/x-mpegurl";

            foreach (var protocol in protocols) {
                if (protocols[0] != protocol ||
                    this.sink_protocol_info != "") {
                    this.sink_protocol_info += ",";
                }

                foreach (var mime_type in mime_types) {
                    if (mime_types[0] != mime_type) {
                        this.sink_protocol_info += ",";
                    }

                    this.sink_protocol_info += protocol + ":*:" + mime_type + ":*";
                }
            }
        }

        return this.sink_protocol_info;
    }
}