[rygel] core: Special seek classes for each format



commit 3916ede3f349e4889ac3bd02e99a89ad56a4c9a1
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Wed Dec 9 01:20:09 2009 +0200

    core: Special seek classes for each format

 src/rygel/Makefile.am               |    2 +
 src/rygel/rygel-http-byte-seek.vala |  106 +++++++++++++++++++++++
 src/rygel/rygel-http-request.vala   |    4 +-
 src/rygel/rygel-http-seek.vala      |  157 +----------------------------------
 src/rygel/rygel-http-time-seek.vala |  100 ++++++++++++++++++++++
 5 files changed, 213 insertions(+), 156 deletions(-)
---
diff --git a/src/rygel/Makefile.am b/src/rygel/Makefile.am
index 3e8918e..bf8a961 100644
--- a/src/rygel/Makefile.am
+++ b/src/rygel/Makefile.am
@@ -57,6 +57,8 @@ VAPI_SOURCE_FILES = rygel-configuration.vala \
 		    rygel-http-identity-handler.vala \
 		    rygel-http-transcode-handler.vala \
 		    rygel-http-seek.vala \
+		    rygel-http-byte-seek.vala \
+		    rygel-http-time-seek.vala \
 		    rygel-http-response.vala \
 		    rygel-live-response.vala \
 		    rygel-seekable-response.vala \
diff --git a/src/rygel/rygel-http-byte-seek.vala b/src/rygel/rygel-http-byte-seek.vala
new file mode 100644
index 0000000..96e8241
--- /dev/null
+++ b/src/rygel/rygel-http-byte-seek.vala
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2009 Nokia Corporation.
+ *
+ * Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
+ *                               <zeeshan ali nokia 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 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 program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+using Gst;
+
+internal class Rygel.HTTPByteSeek : Rygel.HTTPSeek {
+    public HTTPByteSeek (HTTPRequest request) throws HTTPSeekError {
+        string range, pos;
+        string[] range_tokens;
+        int64 start = 0, length;
+
+        if (request.thumbnail != null) {
+            length = request.thumbnail.size;
+        } else {
+            length = request.item.size;
+        }
+        var stop = length - 1;
+
+        range = request.msg.request_headers.get ("Range");
+        if (range != null) {
+            // We have a Range header. Parse.
+            if (!range.has_prefix ("bytes=")) {
+                throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'",
+                                                       range);
+            }
+
+            range_tokens = range.offset (6).split ("-", 2);
+            if (range_tokens[0] == null || range_tokens[1] == null) {
+                throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'",
+                                                       range);
+            }
+
+            // Get first byte position
+            pos = range_tokens[0];
+            if (pos[0].isdigit ()) {
+                start = pos.to_int64 ();
+            } else if (pos  != "") {
+                throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'",
+                                                       range);
+            }
+
+            // Get last byte position if specified
+            pos = range_tokens[1];
+            if (pos[0].isdigit ()) {
+                stop = pos.to_int64 ();
+                if (stop < start) {
+                    throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'",
+                                                           range);
+                }
+            } else if (pos != "") {
+                throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'",
+                                                       range);
+            }
+        }
+
+        base (request.msg,
+              Format.BYTES,
+              start,
+              stop,
+              length);
+    }
+
+    public override void add_response_headers () {
+        // Content-Range: bytes START_BYTE-STOP_BYTE/TOTAL_LENGTH
+        var range = "bytes ";
+        double start = (double) this.start;
+        double stop = (double) this.stop;
+
+        this.msg.response_headers.append ("Accept-Ranges", "bytes");
+
+        range += start.to_string () + "-";
+
+        if (stop >= 0.0) {
+            range += stop.to_string ();
+        }
+
+        if (this.length > 0) {
+            range += "/" + this.length.to_string ();
+        } else {
+            range += "/*";
+        }
+
+        this.msg.response_headers.append ("Content-Range", range);
+        this.msg.response_headers.set_content_length (this.length);
+    }
+}
diff --git a/src/rygel/rygel-http-request.vala b/src/rygel/rygel-http-request.vala
index f755f0d..ac834a8 100644
--- a/src/rygel/rygel-http-request.vala
+++ b/src/rygel/rygel-http-request.vala
@@ -132,9 +132,9 @@ internal class Rygel.HTTPRequest : GLib.Object, Rygel.StateMachine {
     private async void handle_item_request () {
         try {
             if (this.thumbnail == null && this.item.should_stream ()) {
-                this.seek = HTTPSeek.from_time_range (this);
+                this.seek = new HTTPTimeSeek (this);
             } else {
-                this.seek = HTTPSeek.from_byte_range (this);
+                this.seek = new HTTPByteSeek (this);
             }
 
             // Add headers
diff --git a/src/rygel/rygel-http-seek.vala b/src/rygel/rygel-http-seek.vala
index 69de79b..29fb9c2 100644
--- a/src/rygel/rygel-http-seek.vala
+++ b/src/rygel/rygel-http-seek.vala
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 Nokia Corporation.
+ * Copyright (C) 2008-2009 Nokia Corporation.
  *
  * Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
  *                               <zeeshan ali nokia com>
@@ -28,7 +28,7 @@ internal errordomain Rygel.HTTPSeekError {
     OUT_OF_RANGE = Soup.KnownStatusCode.REQUESTED_RANGE_NOT_SATISFIABLE,
 }
 
-internal class Rygel.HTTPSeek : GLib.Object {
+internal abstract class Rygel.HTTPSeek : GLib.Object {
     public Soup.Message msg { get; private set; }
     public Format format { get; private set; }
 
@@ -53,156 +53,5 @@ internal class Rygel.HTTPSeek : GLib.Object {
         }
     }
 
-    public static HTTPSeek? from_byte_range (HTTPRequest request)
-                                             throws HTTPSeekError {
-        string range, pos;
-        string[] range_tokens;
-        int64 start = 0, length;
-
-        if (request.thumbnail != null) {
-            length = request.thumbnail.size;
-        } else {
-            length = request.item.size;
-        }
-        var stop = length - 1;
-
-        range = request.msg.request_headers.get ("Range");
-        if (range == null) {
-            return new HTTPSeek (request.msg,
-                                 Format.BYTES,
-                                 start,
-                                 stop,
-                                 length);
-        }
-
-        // We have a Range header. Parse.
-        if (!range.has_prefix ("bytes=")) {
-            throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'", range);
-        }
-
-        range_tokens = range.offset (6).split ("-", 2);
-        if (range_tokens[0] == null || range_tokens[1] == null) {
-            throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'", range);
-        }
-
-        // Get first byte position
-        pos = range_tokens[0];
-        if (pos[0].isdigit ()) {
-            start = pos.to_int64 ();
-        } else if (pos  != "") {
-            throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'", range);
-        }
-
-        // Get last byte position if specified
-        pos = range_tokens[1];
-        if (pos[0].isdigit ()) {
-            stop = pos.to_int64 ();
-            if (stop < start) {
-                throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'",
-                                                       range);
-            }
-        } else if (pos != "") {
-            throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'", range);
-        }
-
-        return new HTTPSeek (request.msg,
-                             Format.BYTES,
-                             start,
-                             stop,
-                             length);
-    }
-
-    // FIXME: We are only accepting time range in this format:
-    //
-    // TimeSeekRange.dlna.org : npt=417.33-779.09
-    //
-    // and not
-    //
-    // TimeSeekRange.dlna.org : npt=10:19:25.7-13:23:33.6
-    public static HTTPSeek? from_time_range (HTTPRequest request)
-                                             throws HTTPSeekError {
-        string range, time;
-        string[] range_tokens;
-        int64 start = 0, stop = -1;
-
-        range = request.msg.request_headers.get ("TimeSeekRange.dlna.org");
-        if (range == null) {
-            return null;
-        }
-
-        if (!range.has_prefix ("npt=")) {
-            throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'", range);
-        }
-
-        range_tokens = range.offset (4).split ("-", 2);
-        if (range_tokens[0] == null || range_tokens[1] == null) {
-            throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'", range);
-        }
-
-        // Get start time
-        time = range_tokens[0];
-        if (time[0].isdigit ()) {
-            start = (int64) (time.to_double () * SECOND);
-        } else if (time != "") {
-            throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'", range);
-        }
-
-        // Get end time
-        time = range_tokens[1];
-        if (time[0].isdigit()) {
-            stop = (int64) (time.to_double () * SECOND);
-            if (stop < start) {
-                throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'",
-                                                       range);
-            }
-        } else if (time == "") {
-            stop = request.item.duration - 1;
-        } else {
-            throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'", range);
-        }
-
-        return new HTTPSeek (request.msg,
-                             Format.TIME,
-                             start,
-                             stop,
-                             request.item.duration);
-    }
-
-    public void add_response_headers () {
-        string header;
-        string value;
-        double start = 0;
-        double stop;
-
-        if (this.format == Format.TIME) {
-            // TimeSeekRange.dlna.org: npt=START_TIME-END_TIME/DURATION
-            header = "TimeSeekRange.dlna.org";
-            value = "npt=";
-            start = (double) this.start / SECOND;
-            stop = (double) this.stop / SECOND;
-        } else {
-            // Content-Range: bytes START_BYTE-STOP_BYTE/TOTAL_LENGTH
-            header = "Content-Range";
-            value = "bytes ";
-            start = (double) this.start;
-            stop = (double) this.stop;
-
-            this.msg.response_headers.append ("Accept-Ranges", "bytes");
-        }
-
-        value += start.to_string () + "-";
-
-        if (stop >= 0.0) {
-            value += stop.to_string ();
-        }
-
-        if (this.length > 0) {
-            value += "/" + this.length.to_string ();
-        } else {
-            value += "/*";
-        }
-
-        this.msg.response_headers.append (header, value);
-        this.msg.response_headers.set_content_length (this.length);
-    }
+    public abstract void add_response_headers ();
 }
diff --git a/src/rygel/rygel-http-time-seek.vala b/src/rygel/rygel-http-time-seek.vala
new file mode 100644
index 0000000..789202a
--- /dev/null
+++ b/src/rygel/rygel-http-time-seek.vala
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2009 Nokia Corporation.
+ *
+ * Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
+ *                               <zeeshan ali nokia 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 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 program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+using Gst;
+
+internal class Rygel.HTTPTimeSeek : Rygel.HTTPSeek {
+    // FIXME: We are only accepting time range in this format:
+    //
+    // TimeSeekRange.dlna.org : npt=417.33-779.09
+    //
+    // and not
+    //
+    // TimeSeekRange.dlna.org : npt=10:19:25.7-13:23:33.6
+    public HTTPTimeSeek (HTTPRequest request) throws HTTPSeekError {
+        string range, time;
+        string[] range_tokens;
+        int64 start = 0, stop = -1;
+
+        range = request.msg.request_headers.get ("TimeSeekRange.dlna.org");
+        assert (range != null);
+
+        if (!range.has_prefix ("npt=")) {
+            throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'", range);
+        }
+
+        range_tokens = range.offset (4).split ("-", 2);
+        if (range_tokens[0] == null || range_tokens[1] == null) {
+            throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'", range);
+        }
+
+        // Get start time
+        time = range_tokens[0];
+        if (time[0].isdigit ()) {
+            start = (int64) (time.to_double () * SECOND);
+        } else if (time != "") {
+            throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'", range);
+        }
+
+        // Get end time
+        time = range_tokens[1];
+        if (time[0].isdigit()) {
+            stop = (int64) (time.to_double () * SECOND);
+            if (stop < start) {
+                throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'",
+                                                       range);
+            }
+        } else if (time == "") {
+            stop = request.item.duration - 1;
+        } else {
+            throw new HTTPSeekError.INVALID_RANGE ("Invalid Range '%s'", range);
+        }
+
+        base (request.msg,
+              Format.TIME,
+              start,
+              stop,
+              request.item.duration);
+    }
+
+    public override void add_response_headers () {
+        // TimeSeekRange.dlna.org: npt=START_TIME-END_TIME/DURATION
+        var range = "npt=";
+        double start = (double) this.start / SECOND;
+        double stop = (double) this.stop / SECOND;
+
+        range += start.to_string () + "-";
+
+        if (stop >= 0.0) {
+            range += stop.to_string ();
+        }
+
+        if (this.length > 0) {
+            range += "/" + this.length.to_string ();
+        } else {
+            range += "/*";
+        }
+
+        this.msg.response_headers.append ("TimeSeekRange.dlna.org", range);
+    }
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]