summaryrefslogtreecommitdiff
path: root/src/librygel-server/rygel-http-subtitle-handler.vala
blob: be2043edad0c0f285848ed501148c38e7862e097 (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
/*
 * Copyright (C) 2008, 2009 Nokia Corporation.
 * Copyright (C) 2012 Intel Corporation.
 * Copyright (C) 2013  Cable Television Laboratories, Inc.
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *                               <zeeshan.ali@nokia.com>
 *         Jens Georg <jensg@openismus.com>
 *         Craig Pratt <craig@ecaspia.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
 */

using GUPnP;

internal class Rygel.HTTPSubtitleHandler : Rygel.HTTPGetHandler {
    private MediaFileItem media_item;
    private int subtitle_index;
    public Subtitle subtitle;

    public HTTPSubtitleHandler (MediaFileItem media_item,
                                int subtitle_index,
                                Cancellable? cancellable)
                                throws HTTPRequestError {
        this.media_item = media_item;
        this.subtitle_index = subtitle_index;
        this.cancellable = cancellable;

        if (subtitle_index >= 0 && media_item is VideoItem) {
            var video_item = media_item as VideoItem;

            if (subtitle_index < video_item.subtitles.size) {
                this.subtitle = video_item.subtitles.get (subtitle_index);
            }
        }

        if (this.subtitle == null) {
            var msg = /*_*/("Subtitle index %d not found for item '%s");
            throw new HTTPRequestError.NOT_FOUND (msg,
                                                  subtitle_index,
                                                  media_item.id);
        }
    }

    public override bool supports_transfer_mode (string mode) {
        // Support interactive and background transfers only
        return (mode != TRANSFER_MODE_STREAMING);
    }

    public override void add_response_headers (HTTPGet request)
                                               throws HTTPRequestError {
        // Add Content-Type
        request.msg.get_response_headers ().append ("Content-Type",
                                             subtitle.mime_type);

        // Add contentFeatures.dlna.org

        // This is functionally equivalent to how contentFeatures was formed
        // via the (deprecated) HTTPIdentityHandler
        var res = this.media_item.get_resource_list ().get (0);
        var protocol_info = res.get_protocol_info ().to_string ();
        var pi_fields = protocol_info.split (":", 4);
        request.msg.get_response_headers ().append ("contentFeatures.dlna.org",
                                             pi_fields[3]);

        // Chain-up
        base.add_response_headers (request);
    }

    public override HTTPResponse render_body (HTTPGet request)
                                              throws HTTPRequestError {
        DataSource src;
        try {
            var engine = MediaEngine.get_default ();
            src = engine.create_data_source_for_uri (this.subtitle.uri);

            return new HTTPResponse (request, this, src);
        } catch (Error err) {
            throw new HTTPRequestError.NOT_FOUND (err.message);
        }
    }

    public override int64 get_resource_size () {
        return subtitle.size;
    }

    public override bool supports_byte_seek () {
        return true;
    }
}