summaryrefslogtreecommitdiff
path: root/src/rygel/rygel-media-item.vala
blob: 5b610280ae774414cf16950ad16cae0b93645647 (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
182
183
184
185
186
187
188
189
/*
 * Copyright (C) 2008 Zeeshan Ali <zeenix@gmail.com>.
 *
 * Author: Zeeshan Ali <zeenix@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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.
 */

using GUPnP;
using Gee;

public errordomain Rygel.MediaItemError {
    UNKNOWN_URI_TYPE
}

/**
 * Represents a media (Music, Video and Image) item. Provides basic
 * serialization (to DIDLLiteWriter) implementation.
 */
public class Rygel.MediaItem : MediaObject {
    public static const string IMAGE_CLASS = "object.item.imageItem";
    public static const string VIDEO_CLASS = "object.item.videoItem";
    public static const string AUDIO_CLASS = "object.item.audioItem";
    public static const string MUSIC_CLASS = "object.item.audioItem.musicTrack";

    public string author;
    public string album;
    public string date;
    public string upnp_class;

    public DIDLLiteResource res;

    public int track_number = -1;

    protected Rygel.HTTPServer http_server;

    public MediaItem (string     id,
                      string     parent_id,
                      string     title,
                      string     upnp_class,
                      HTTPServer http_server) {
        this.id = id;
        this.parent_id = parent_id;
        this.title = title;
        this.upnp_class = upnp_class;
        this.http_server = http_server;

        this.res = DIDLLiteResource ();
        this.res.reset ();
    }

    public override void serialize (DIDLLiteWriter didl_writer) throws Error {
        didl_writer.start_item (this.id,
                                this.parent_id,
                                null,
                                false);

        /* Add fields */
        didl_writer.add_string ("title",
                                DIDLLiteWriter.NAMESPACE_DC,
                                null,
                                this.title);

        didl_writer.add_string ("class",
                                DIDLLiteWriter.NAMESPACE_UPNP,
                                null,
                                this.upnp_class);

        if (this.author != null && this.author != "") {
            didl_writer.add_string ("creator",
                                    DIDLLiteWriter.NAMESPACE_DC,
                                    null,
                                    this.author);

            if (this.upnp_class.has_prefix (VIDEO_CLASS)) {
                didl_writer.add_string ("author",
                                        DIDLLiteWriter.NAMESPACE_UPNP,
                                        null,
                                        this.author);
            } else if (this.upnp_class.has_prefix (MUSIC_CLASS)) {
                didl_writer.add_string ("artist",
                                        DIDLLiteWriter.NAMESPACE_UPNP,
                                        null,
                                        this.author);
            }
        }

        if (this.track_number >= 0) {
            didl_writer.add_int ("originalTrackNumber",
                                 DIDLLiteWriter.NAMESPACE_UPNP,
                                 null,
                                 this.track_number);
        }

        if (this.album != null && this.album != "") {
            didl_writer.add_string ("album",
                                    DIDLLiteWriter.NAMESPACE_UPNP,
                                    null,
                                    this.album);
        }

        if (this.date != null && this.date != "") {
            didl_writer.add_string ("date",
                                    DIDLLiteWriter.NAMESPACE_DC,
                                    null,
                                    this.date);
        }

        /* Add resource data */
        /* Protocol info */
        if (this.res.uri != null) {
            string protocol = get_protocol_for_uri (this.res.uri);
            this.res.protocol = protocol;
        }

        this.res.dlna_profile = "MP3"; /* FIXME */

        if (this.upnp_class.has_prefix (MediaItem.IMAGE_CLASS)) {
            this.res.dlna_flags |= DLNAFlags.INTERACTIVE_TRANSFER_MODE;
        } else {
            this.res.dlna_flags |= DLNAFlags.STREAMING_TRANSFER_MODE;
        }

        if (this.res.size > 0) {
            this.res.dlna_operation = DLNAOperation.RANGE;
            this.res.dlna_flags |= DLNAFlags.BACKGROUND_TRANSFER_MODE;
        }

        /* Now get the transcoded/proxy URIs */
        var res_list = this.get_transcoded_resources (res);
        foreach (DIDLLiteResource trans_res in res_list) {
            didl_writer.add_res (trans_res);
        }

        /* Add the original res in the end */
        if (this.res.uri != null) {
            didl_writer.add_res (res);
        }

        /* End of item */
        didl_writer.end_item ();
    }

    private string get_protocol_for_uri (string uri) throws Error {
        if (uri.has_prefix ("http")) {
            return "http-get";
        } else if (uri.has_prefix ("file")) {
            return "internal";
        } else if (uri.has_prefix ("rtsp")) {
            // FIXME: Assuming that RTSP is always accompanied with RTP over UDP
            return "rtsp-rtp-udp";
        } else {
            throw new MediaItemError.UNKNOWN_URI_TYPE
                            ("Failed to probe protocol for URI %s", uri);
        }
    }

    // FIXME: We only proxy URIs through our HTTP server for now
    private ArrayList<DIDLLiteResource?>? get_transcoded_resources
                                            (DIDLLiteResource orig_res) {
        if (orig_res.protocol == "http-get")
            return null;

        var resources = new ArrayList<DIDLLiteResource?> ();

        // Copy the original res first
        DIDLLiteResource res = orig_res;

        // Then modify the URI and protocol
        res.uri = this.http_server.create_http_uri_for_item (this);
        res.protocol = "http-get";

        resources.add (res);

        return resources;
    }
}