From 3be46cada1d468ee0b510556ad512f1cbe16ff20 Mon Sep 17 00:00:00 2001
From: "Ronald S. Bultje" <rbultje@ronald.bitfreak.net>
Date: Sun, 19 Dec 2004 00:32:13 +0000
Subject: [PATCH] ext/ffmpeg/gstffmpegprotocol.c: Some "seeking" hacks, and
 separate peek from read (read = peek + flush) so seek can u...

Original commit message from CVS:
* ext/ffmpeg/gstffmpegprotocol.c: (gst_ffmpegdata_peek),
(gst_ffmpegdata_read), (gst_ffmpegdata_seek):
Some "seeking" hacks, and separate peek from read (read = peek +
flush) so seek can use peek as well to workaround typefind. With
this, I'm able to play several of the gaming format movies in
Totem.
---
 ChangeLog                      |  9 +++++++++
 ext/ffmpeg/gstffmpegprotocol.c | 29 ++++++++++++++++++++++-------
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 69e6903cec..d77b1b8cc6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2004-12-19  Ronald S. Bultje  <rbultje@ronald.bitfreak.net>
+
+	* ext/ffmpeg/gstffmpegprotocol.c: (gst_ffmpegdata_peek),
+	(gst_ffmpegdata_read), (gst_ffmpegdata_seek):
+	  Some "seeking" hacks, and separate peek from read (read = peek +
+	  flush) so seek can use peek as well to workaround typefind. With
+	  this, I'm able to play several of the gaming format movies in
+	  Totem.
+
 2004-12-18  Ronald S. Bultje  <rbultje@ronald.bitfreak.net>
 
 	* ext/ffmpeg/Makefile.am:
diff --git a/ext/ffmpeg/gstffmpegprotocol.c b/ext/ffmpeg/gstffmpegprotocol.c
index 51af68b0d9..b757aa8ade 100644
--- a/ext/ffmpeg/gstffmpegprotocol.c
+++ b/ext/ffmpeg/gstffmpegprotocol.c
@@ -90,7 +90,7 @@ gst_ffmpegdata_open (URLContext * h, const char *filename, int flags)
 }
 
 static int
-gst_ffmpegdata_read (URLContext * h, unsigned char *buf, int size)
+gst_ffmpegdata_peek (URLContext * h, unsigned char *buf, int size)
 {
   GstByteStream *bs;
   guint32 total, request;
@@ -159,11 +159,27 @@ gst_ffmpegdata_read (URLContext * h, unsigned char *buf, int size)
   } while ((!info->eos && total != request) || have_event);
 
   memcpy (buf, data, total);
-  gst_bytestream_flush_fast (bs, total);
 
   return total;
 }
 
+static int
+gst_ffmpegdata_read (URLContext * h, unsigned char *buf, int size)
+{
+  gint res;
+  GstByteStream *bs;
+  GstProtocolInfo *info;
+
+  info = (GstProtocolInfo *) h->priv_data;
+  bs = info->bs;
+  res = gst_ffmpegdata_peek (h, buf, size);
+  if (res > 0) {
+    gst_bytestream_flush_fast (bs, res);
+  }
+
+  return res;
+}
+
 static int
 gst_ffmpegdata_write (URLContext * h, unsigned char *buf, int size)
 {
@@ -195,19 +211,18 @@ gst_ffmpegdata_seek (URLContext * h, offset_t pos, int whence)
 
   /* get data (typefind hack) */
   if (gst_bytestream_tell (info->bs) != gst_bytestream_length (info->bs)) {
-    //gchar buf;
-    //gst_ffmpegdata_read (h, &buf, 1);
-    //peek!!!!! not read = data loss if not seekable
+    gchar buf;
+    gst_ffmpegdata_peek (h, &buf, 1);
   }
 
   /* hack in ffmpeg to get filesize... */
   if (whence == SEEK_END && pos == -1)
     return gst_bytestream_length (info->bs) - 1;
+  else if (whence == SEEK_END && pos == 0)
+    return gst_bytestream_length (info->bs);
   /* another hack to get the current position... */
   else if (whence == SEEK_CUR && pos == 0)
     return gst_bytestream_tell (info->bs);
-  //else
-    //g_assert (pos >= 0);
 
   switch (whence) {
     case SEEK_SET: