2 * Copyright (C) 2010 Nokia Corporation.
4 * Author: Zeeshan Ali (Khattak) <zeeshan.ali@nokia.com>
5 * <zeeshanak@gnome.org>
7 * This file is part of Rygel.
9 * Rygel is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * Rygel is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 private errordomain Rygel.TestError {
30 private class Rygel.HTTPTranscodeHandler : GLib.Object {}
32 private abstract class Rygel.MediaItem : GLib.Object {
33 public int64 size = -1;
35 public bool is_live_stream () {
40 private class Rygel.AudioItem : MediaItem {
41 public int64 duration = 2048;
44 private class Rygel.Thumbnail : GLib.Object {}
45 private class Rygel.Subtitle : GLib.Object {}
47 private class Rygel.HTTPGet : GLib.Object {
48 public const string ITEM_URI = "http://DoesntMatterWhatThisIs";
50 public Soup.Message msg;
51 public MediaItem item;
52 public Thumbnail thumbnail;
53 public Subtitle subtitle;
55 public HTTPTranscodeHandler handler;
57 public HTTPGet (Thumbnail? thumbnail, Subtitle? subtitle) {
58 this.msg = new Soup.Message ("HTTP", ITEM_URI);
59 this.item = new AudioItem ();
60 this.handler = new HTTPTranscodeHandler ();
61 this.thumbnail = thumbnail;
62 this.subtitle = subtitle;
65 public HTTPGet.seek_start (int64 start,
68 this (thumbnail, subtitle);
70 this.add_headers (start, -1);
73 public HTTPGet.seek_stop (int64 stop,
76 this (thumbnail, subtitle);
78 this.add_headers (0, stop);
81 public HTTPGet.seek_start_stop (int64 start,
85 this (thumbnail, subtitle);
87 this.add_headers (start, stop);
90 private void add_headers (int64 start, int64 stop) requires (start >= 0) {
91 var stop_str = (stop > 0)? stop.to_string (): "";
92 var range = "npt=" + start.to_string () + "-" + stop_str;
93 this.msg.request_headers.append ("TimeSeekRange.dlna.org", range);
97 private class Rygel.HTTPTimeSeekTest : GLib.Object {
98 private Regex range_regex;
100 public static int main (string[] args) {
102 var test = new HTTPTimeSeekTest ();
105 } catch (TestError.SKIP error) {
107 } catch (Error error) {
108 critical ("%s", error.message);
116 public void run () throws HTTPSeekError {
117 var thumbnails = new Thumbnail[] { null, new Thumbnail () };
118 var subtitles = new Subtitle[] { null, new Subtitle () };
120 foreach (var thumbnail in thumbnails) {
121 foreach (var subtitle in subtitles) {
122 this.test_no_seek (thumbnail, subtitle);
123 this.test_start_only_seek (thumbnail, subtitle);
124 this.test_stop_only_seek (thumbnail, subtitle);
125 this.test_start_stop_seek (thumbnail, subtitle);
130 private HTTPTimeSeekTest () {
131 var expression = "npt=[0-9]+\\.[0-9][0-9][0-9]-" +
132 "[0-9]+\\.[0-9][0-9][0-9]/" +
133 "[0-9]+\\.[0-9][0-9][0-9]";
134 this.range_regex = new Regex (expression, RegexCompileFlags.CASELESS);
137 private void test_no_seek (Thumbnail? thumbnail,
138 Subtitle? subtitle) throws HTTPSeekError {
139 var request = new HTTPGet (thumbnail, subtitle);
140 var audio_item = request.item as AudioItem;
142 this.test_seek (request, 0, audio_item.duration * SECOND - MSECOND);
145 private void test_start_only_seek (Thumbnail? thumbnail,
147 throws HTTPSeekError {
148 var request = new HTTPGet.seek_start (128, thumbnail, subtitle);
149 var audio_item = request.item as AudioItem;
151 this.test_seek (request,
153 audio_item.duration * SECOND - MSECOND);
156 private void test_stop_only_seek (Thumbnail? thumbnail,
158 throws HTTPSeekError {
159 var request = new HTTPGet.seek_stop (128, thumbnail, subtitle);
161 this.test_seek (request, 0, 128 * SECOND);
164 private void test_start_stop_seek (Thumbnail? thumbnail,
166 throws HTTPSeekError {
167 var request = new HTTPGet.seek_start_stop (128,
172 this.test_seek (request, 128 * SECOND, 256 * SECOND);
175 private void test_seek (HTTPGet request,
177 int64 stop) throws HTTPSeekError {
178 assert (HTTPTimeSeek.needed (request));
180 var seek = new HTTPTimeSeek (request);
181 seek.add_response_headers ();
183 assert (seek != null);
184 assert (seek.start == start);
185 assert (seek.stop == stop);
186 assert (seek.length == seek.stop + MSECOND - seek.start);
188 var audio_item = request.item as AudioItem;
189 assert (seek.total_length == audio_item.duration * SECOND);
191 var header = request.msg.response_headers.get_one
192 ("TimeSeekRange.dlna.org");
193 assert (header != null);
194 assert (this.range_regex.match (header));