160d5c6b37e6b13b338a6f21f5fc4336f99231d4
[profile/ivi/rygel.git] / tests / rygel-http-time-seek-test.vala
1 /*
2  * Copyright (C) 2010 Nokia Corporation.
3  *
4  * Author: Zeeshan Ali (Khattak) <zeeshan.ali@nokia.com>
5  *                               <zeeshanak@gnome.org>
6  *
7  * This file is part of Rygel.
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 using Gst;
25
26 private errordomain Rygel.TestError {
27     SKIP
28 }
29
30 private class Rygel.HTTPTranscodeHandler : GLib.Object {}
31
32 private abstract class Rygel.MediaItem : GLib.Object {
33     public int64 size = -1;
34
35     public bool is_live_stream () {
36         return true;
37     }
38 }
39
40 private class Rygel.AudioItem : MediaItem {
41     public int64 duration = 2048;
42 }
43
44 private class Rygel.Thumbnail : GLib.Object {}
45 private class Rygel.Subtitle : GLib.Object {}
46
47 private class Rygel.HTTPGet : GLib.Object {
48     public const string ITEM_URI = "http://DoesntMatterWhatThisIs";
49
50     public Soup.Message msg;
51     public MediaItem item;
52     public Thumbnail thumbnail;
53     public Subtitle subtitle;
54
55     public HTTPTranscodeHandler handler;
56
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;
63     }
64
65     public HTTPGet.seek_start (int64      start,
66                                Thumbnail? thumbnail,
67                                Subtitle?  subtitle) {
68         this (thumbnail, subtitle);
69
70         this.add_headers (start, -1);
71     }
72
73     public HTTPGet.seek_stop (int64      stop,
74                               Thumbnail? thumbnail,
75                               Subtitle?  subtitle) {
76         this (thumbnail, subtitle);
77
78         this.add_headers (0, stop);
79     }
80
81     public HTTPGet.seek_start_stop (int64      start,
82                                     int64      stop,
83                                     Thumbnail? thumbnail,
84                                     Subtitle?  subtitle) {
85         this (thumbnail, subtitle);
86
87         this.add_headers (start, stop);
88     }
89
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);
94     }
95 }
96
97 private class Rygel.HTTPTimeSeekTest : GLib.Object {
98     private Regex range_regex;
99
100     public static int main (string[] args) {
101         try {
102             var test = new HTTPTimeSeekTest ();
103
104             test.run ();
105         } catch (TestError.SKIP error) {
106             return 77;
107         } catch (Error error) {
108             critical ("%s", error.message);
109
110             return -1;
111         }
112
113         return 0;
114     }
115
116     public void run () throws HTTPSeekError {
117         var thumbnails = new Thumbnail[] { null, new Thumbnail () };
118         var subtitles = new Subtitle[] { null, new Subtitle () };
119
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);
126             }
127         }
128     }
129
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);
135     }
136
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;
141
142         this.test_seek (request, 0, audio_item.duration * SECOND - MSECOND);
143     }
144
145     private void test_start_only_seek (Thumbnail? thumbnail,
146                                        Subtitle?  subtitle)
147                                        throws HTTPSeekError {
148         var request = new HTTPGet.seek_start (128, thumbnail, subtitle);
149         var audio_item = request.item as AudioItem;
150
151         this.test_seek (request,
152                         128 * SECOND,
153                         audio_item.duration * SECOND - MSECOND);
154     }
155
156     private void test_stop_only_seek (Thumbnail? thumbnail,
157                                       Subtitle?  subtitle)
158                                       throws HTTPSeekError {
159         var request = new HTTPGet.seek_stop (128, thumbnail, subtitle);
160
161         this.test_seek (request, 0, 128 * SECOND);
162     }
163
164     private void test_start_stop_seek (Thumbnail? thumbnail,
165                                        Subtitle?  subtitle)
166                                        throws HTTPSeekError {
167         var request = new HTTPGet.seek_start_stop (128,
168                                                    256,
169                                                    thumbnail,
170                                                    subtitle);
171
172         this.test_seek (request, 128 * SECOND, 256 * SECOND);
173     }
174
175     private void test_seek (HTTPGet request,
176                             int64   start,
177                             int64   stop) throws HTTPSeekError {
178         assert (HTTPTimeSeek.needed (request));
179
180         var seek = new HTTPTimeSeek (request);
181         seek.add_response_headers ();
182
183         assert (seek != null);
184         assert (seek.start == start);
185         assert (seek.stop == stop);
186         assert (seek.length == seek.stop + MSECOND - seek.start);
187
188         var audio_item = request.item as AudioItem;
189         assert (seek.total_length == audio_item.duration * SECOND);
190
191         var header = request.msg.response_headers.get_one
192                                         ("TimeSeekRange.dlna.org");
193         assert (header != null);
194         assert (this.range_regex.match (header));
195     }
196 }