~ubuntu-branches/ubuntu/trusty/rygel/trusty

« back to all changes in this revision

Viewing changes to tests/rygel-http-time-seek.vala

  • Committer: Package Import Robot
  • Author(s): Andreas Henriksson
  • Date: 2011-12-16 15:21:25 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20111216152125-qgn31dkfmhouhrf0
Upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
using Gst;
25
25
 
26
26
internal class Rygel.HTTPTimeSeek : Rygel.HTTPSeek {
27
 
    // FIXME: We are only accepting time range in this format:
28
 
    //
29
 
    // TimeSeekRange.dlna.org : npt=417.33-779.09
30
 
    //
31
 
    // and not
32
 
    //
33
 
    // TimeSeekRange.dlna.org : npt=10:19:25.7-13:23:33.6
34
27
    public HTTPTimeSeek (HTTPGet request) throws HTTPSeekError {
35
 
        string range, time;
 
28
        string range;
36
29
        string[] range_tokens;
37
30
        int64 start = 0;
38
31
        int64 duration = (request.item as AudioItem).duration * SECOND;
39
32
        int64 stop = duration - MSECOND;
 
33
        int64 parsed_value = 0;
 
34
        bool parsing_start = true;
40
35
 
41
36
        range = request.msg.request_headers.get_one ("TimeSeekRange.dlna.org");
 
37
 
42
38
        if (range != null) {
43
39
            if (!range.has_prefix ("npt=")) {
44
40
                throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'",
46
42
            }
47
43
 
48
44
            range_tokens = range.substring (4).split ("-", 2);
49
 
            if (range_tokens[0] == null || range_tokens[1] == null) {
50
 
                throw new HTTPSeekError.INVALID_RANGE (_("Invalid Range '%s'"),
51
 
                                                       range);
52
 
            }
53
 
 
54
 
            // Get start time
55
 
            time = range_tokens[0];
56
 
            if (time[0].isdigit ()) {
57
 
                start = (int64) (double.parse (time) * SECOND);
58
 
            } else if (time != "") {
59
 
                throw new HTTPSeekError.INVALID_RANGE (_("Invalid Range '%s'"),
60
 
                                                       range);
61
 
            }
62
 
 
63
 
            // Get end time
64
 
            time = range_tokens[1];
65
 
            if (time[0].isdigit()) {
66
 
                stop = (int64) (double.parse (time) * SECOND);
67
 
                if (stop < start) {
68
 
                    throw new HTTPSeekError.INVALID_RANGE
69
 
                                        (_("Invalid Range '%s'"), range);
70
 
                }
71
 
            } else if (time != "") {
72
 
                throw new HTTPSeekError.INVALID_RANGE (_("Invalid Range '%s'"),
73
 
                                                       range);
 
45
            if (range_tokens[0] == null ||
 
46
                // Start token of the range must be provided
 
47
                range_tokens[0] == "" ||
 
48
                range_tokens[1] == null) {
 
49
                throw new HTTPSeekError.INVALID_RANGE (_("Invalid Range '%s'"),
 
50
                                                       range);
 
51
            }
 
52
 
 
53
            foreach (string range_token in range_tokens) {
 
54
                if (range_token == "") {
 
55
                    continue;
 
56
                }
 
57
 
 
58
                if (range_token.index_of (":") == -1) {
 
59
                    if (!parse_seconds (range_token, ref parsed_value)) {
 
60
                        throw new HTTPSeekError.INVALID_RANGE
 
61
                                            (_("Invalid Range '%s'"),
 
62
                                               range);
 
63
                    }
 
64
                } else {
 
65
                    if (!parse_time (range_token,
 
66
                                     ref parsed_value)) {
 
67
                        throw new HTTPSeekError.INVALID_RANGE
 
68
                                            (_("Invalid Range '%s'"),
 
69
                                               range);
 
70
                    }
 
71
                }
 
72
 
 
73
                if (parsing_start) {
 
74
                    parsing_start = false;
 
75
                    start = parsed_value;
 
76
                } else {
 
77
                    stop = parsed_value;
 
78
                }
 
79
            }
 
80
 
 
81
            if (start > stop) {
 
82
                throw new HTTPSeekError.INVALID_RANGE
 
83
                                    (_("Invalid Range '%s'"),
 
84
                                       range);
74
85
            }
75
86
        }
76
87
 
107
118
 
108
119
        this.msg.response_headers.append ("TimeSeekRange.dlna.org", range);
109
120
    }
 
121
 
 
122
    // Parses npt times in the format of '417.33'
 
123
    private static bool parse_seconds (string    range_token,
 
124
                                       ref int64 value) {
 
125
        if (range_token[0].isdigit ()) {
 
126
            value = (int64) (double.parse (range_token) * SECOND);
 
127
        } else {
 
128
            return false;
 
129
        }
 
130
        return true;
 
131
    }
 
132
 
 
133
    // Parses npt times in the format of '10:19:25.7'
 
134
    private static bool parse_time (string    range_token,
 
135
                                    ref int64 value) {
 
136
        int64 seconds_sum = 0;
 
137
        int time_factor = 0;
 
138
        string[] time_tokens;
 
139
 
 
140
        seconds_sum = 0;
 
141
        time_factor = 3600;
 
142
 
 
143
        time_tokens = range_token.split (":", 3);
 
144
        if (time_tokens[0] == null ||
 
145
            time_tokens[1] == null ||
 
146
            time_tokens[2] == null) {
 
147
            return false;
 
148
        }
 
149
 
 
150
        foreach (string time in time_tokens) {
 
151
            if (time[0].isdigit ()) {
 
152
                seconds_sum += (int64) ((double.parse (time) *
 
153
                                         SECOND) * time_factor);
 
154
            } else {
 
155
                return false;
 
156
            }
 
157
            time_factor /= 60;
 
158
        }
 
159
        value = seconds_sum;
 
160
 
 
161
        return true;
 
162
    }
110
163
}