[xiph-cvs] cvs commit: ogg-python/test oggtail.py

Andrew Catham Master of Python andrew at xiph.org
Tue May 1 18:49:12 PDT 2001



andrew      01/05/01 18:49:11

  Modified:    .        ChangeLog
               src      _oggmodule.c pyoggpackbuff.c pyoggpacket.c
                        pyoggpacket.h pyoggpage.c pyoggstreamstate.c
                        pyoggsyncstate.c
  Added:       test     oggtail.py
  Log:
  2001-05-01  Mike Coleman  <mkc at mathdogs.com>
          * test/oggtail.py: new script to show off some of ogg module
  
          * src/pyogg*.c: methods with no args now check that they're called
          this way
  
          * src/pyogg*.c: add repr functions
  
          * src/pyogg*.c: add type checking for PyTypeObject function table
  
          * src/pyoggpacket.c: add get/set granulepos attribute
          * src/pyoggpage.c: add set pageno attribute
  
          * src/pyoggstreamstate.c: implement pagein, packetout methods
  
          * src/pyoggsyncstate.c: implement bytesin, pageseek methods,
          comment out unneeded wrote method
  
          * src/pyogg*state.c: fix free of bogus pointer in dealloc

Revision  Changes    Path
1.4       +21 -0     ogg-python/ChangeLog

Index: ChangeLog
===================================================================
RCS file: /usr/local/cvsroot/ogg-python/ChangeLog,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ChangeLog	2001/03/27 23:54:39	1.3
+++ ChangeLog	2001/05/02 01:49:10	1.4
@@ -1,3 +1,24 @@
+2001-05-01  Mike Coleman  <mkc at mathdogs.com>
+	* test/oggtail.py: new script to show off some of ogg module
+	
+	* src/pyogg*.c: methods with no args now check that they're called
+	this way
+
+	* src/pyogg*.c: add repr functions
+
+	* src/pyogg*.c: add type checking for PyTypeObject function table
+	
+	* src/pyoggpacket.c: add get/set granulepos attribute
+	* src/pyoggpage.c: add set pageno attribute
+
+	* src/pyoggstreamstate.c: implement pagein, packetout methods
+	
+	* src/pyoggsyncstate.c: implement bytesin, pageseek methods,
+	comment out unneeded wrote method
+
+	* src/pyogg*state.c: fix free of bogus pointer in dealloc
+	
+	
 3-27-2001 Andrew H. Chatham <andrew.chatham at duke.edu>
         * src/_oggmodule.c: Added OggSyncState
 

1.4       +1 -0      ogg-python/src/_oggmodule.c

Index: _oggmodule.c
===================================================================
RCS file: /usr/local/cvsroot/ogg-python/src/_oggmodule.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- _oggmodule.c	2001/03/27 23:54:39	1.3
+++ _oggmodule.c	2001/05/02 01:49:10	1.4
@@ -4,6 +4,7 @@
 #include "_oggmodule.h"
 
 #include "pyoggstreamstate.h"
+#include "pyoggsyncstate.h"
 #include "pyoggpacket.h"
 #include "pyoggpackbuff.h"
 

1.3       +59 -14    ogg-python/src/pyoggpackbuff.c

Index: pyoggpackbuff.c
===================================================================
RCS file: /usr/local/cvsroot/ogg-python/src/pyoggpackbuff.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- pyoggpackbuff.c	2001/03/18 02:46:12	1.2
+++ pyoggpackbuff.c	2001/05/02 01:49:10	1.3
@@ -8,8 +8,9 @@
 
 char py_oggpack_buffer_doc[] = "";
 
-static void py_oggpack_buffer_dealloc(py_oggpack_buffer *);
+static void py_oggpack_buffer_dealloc(PyObject *);
 static PyObject* py_oggpack_buffer_getattr(PyObject *, char *);
+static PyObject *py_oggpack_repr(PyObject *self);
 
 FDEF(oggpack_reset) "";
 FDEF(oggpack_writeclear) "";
@@ -40,12 +41,12 @@
   0,
   
   /* Standard Methods */
-  (destructor) py_oggpack_buffer_dealloc,
-  (printfunc) 0,
-  (getattrfunc) py_oggpack_buffer_getattr,
-  (setattrfunc) 0,
-  (cmpfunc) 0,
-  (reprfunc) 0,
+  /* (destructor) */ py_oggpack_buffer_dealloc,
+  /* (printfunc) */ 0,
+  /* (getattrfunc) */ py_oggpack_buffer_getattr,
+  /* (setattrfunc) */ 0,
+  /* (cmpfunc) */ 0,
+  /* (reprfunc) */ py_oggpack_repr,
   
   /* Type Categories */
   0, /* as number */
@@ -92,7 +93,7 @@
 };
 
 static void
-py_oggpack_buffer_dealloc(py_oggpack_buffer *self)
+py_oggpack_buffer_dealloc(PyObject *self)
 {
   PyMem_DEL(self);
 }
@@ -106,9 +107,13 @@
 PyObject *
 py_oggpack_buffer_new(PyObject *self, PyObject *args) 
 {
-  py_oggpack_buffer *ret = 
-    (py_oggpack_buffer *) PyObject_NEW(py_oggpack_buffer,
-				       &py_oggpack_buffer_type);
+  py_oggpack_buffer *ret;
+
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
+
+  ret = (py_oggpack_buffer *) PyObject_NEW(py_oggpack_buffer,
+					   &py_oggpack_buffer_type);
   if (ret == NULL)
     return NULL;
 
@@ -119,12 +124,18 @@
 static PyObject *
 py_oggpack_reset(PyObject *self, PyObject *args)
 {
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
+
   oggpack_reset(PY_OGGPACK_BUFF(self));
 }
 
 static PyObject *
 py_oggpack_writeclear(PyObject *self, PyObject *args)
 {
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
+
   oggpack_writeclear(PY_OGGPACK_BUFF(self));
 }
 
@@ -166,6 +177,10 @@
 py_oggpack_look1(PyObject *self, PyObject *args) 
 {
   long ret;
+
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
+
   ret = oggpack_look1(PY_OGGPACK_BUFF(self));
   return PyLong_FromLong(ret);
 }
@@ -173,14 +188,24 @@
 static PyObject *
 py_oggpack_bytes(PyObject *self, PyObject *args)
 {
-  long ret = oggpack_bytes(PY_OGGPACK_BUFF(self));
+  long ret;
+
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
+
+  ret = oggpack_bytes(PY_OGGPACK_BUFF(self));
   return PyLong_FromLong(ret);
 }
 
 static PyObject *
 py_oggpack_bits(PyObject *self, PyObject *args)
 {
-  long ret = oggpack_bits(PY_OGGPACK_BUFF(self));
+  long ret;
+
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
+
+  ret = oggpack_bits(PY_OGGPACK_BUFF(self));
   return PyLong_FromLong(ret);
 }
 
@@ -226,7 +251,12 @@
 static PyObject *
 py_oggpack_read1(PyObject *self, PyObject *args)
 {
-  long ret = oggpack_read1(PY_OGGPACK_BUFF(self));
+  long ret;
+
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
+
+  ret = oggpack_read1(PY_OGGPACK_BUFF(self));
   return PyInt_FromLong(ret);
 }
 
@@ -261,7 +291,22 @@
 static PyObject *
 py_oggpack_adv1(PyObject *self, PyObject *args)
 {
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
+
   oggpack_adv1(PY_OGGPACK_BUFF(self));
   Py_INCREF(Py_None);
   return Py_None;
 }
+
+static PyObject *
+py_oggpack_repr(PyObject *self)
+{
+  oggpack_buffer *ob = PY_OGGPACK_BUFF(self);
+  char buf[256];
+
+  sprintf(buf, "<OggPackBuff, endbyte = %ld, endbit = %d at %p>", ob->endbyte,
+	  ob->endbit, self);
+  return PyString_FromString(buf);
+}
+

1.2       +41 -8     ogg-python/src/pyoggpacket.c

Index: pyoggpacket.c
===================================================================
RCS file: /usr/local/cvsroot/ogg-python/src/pyoggpacket.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- pyoggpacket.c	2001/02/06 03:18:19	1.1
+++ pyoggpacket.c	2001/05/02 01:49:10	1.2
@@ -6,8 +6,10 @@
 
 char py_ogg_packet_doc[] = "";
 
-static void py_ogg_packet_dealloc(py_ogg_packet *);
+static void py_ogg_packet_dealloc(PyObject *);
 static PyObject* py_ogg_packet_getattr(PyObject *, char *);
+static int py_ogg_packet_setattr(PyObject *, char *, PyObject *);
+static PyObject *py_ogg_packet_repr(PyObject *self);
 
 PyTypeObject py_ogg_packet_type = {
   PyObject_HEAD_INIT(&PyType_Type)
@@ -17,12 +19,12 @@
   0,
   
   /* Standard Methods */
-  (destructor) py_ogg_packet_dealloc,
-  (printfunc) 0,
-  (getattrfunc) py_ogg_packet_getattr,
-  (setattrfunc) 0,
-  (cmpfunc) 0,
-  (reprfunc) 0,
+  /* (destructor) */ py_ogg_packet_dealloc,
+  /* (printfunc) */ 0,
+  /* (getattrfunc) */ py_ogg_packet_getattr,
+  /* (setattrfunc) */ py_ogg_packet_setattr,
+  /* (cmpfunc) */ 0,
+  /* (reprfunc) */ py_ogg_packet_repr,
   
   /* Type Categories */
   0, /* as number */
@@ -55,7 +57,7 @@
 }
 
 static void
-py_ogg_packet_dealloc(py_ogg_packet *self)
+py_ogg_packet_dealloc(PyObject *self)
 {
   PyMem_DEL(self);
 }
@@ -63,6 +65,37 @@
 static PyObject*
 py_ogg_packet_getattr(PyObject *self, char *name)
 {
+  if (strcmp(name, "granulepos") == 0)
+    return PyLong_FromLongLong(PY_OGG_PACKET(self)->granulepos);
   return Py_FindMethod(py_ogg_packet_methods, self, name);
+}
+
+static int
+py_ogg_packet_setattr(PyObject *self, char *name, PyObject *value)
+{
+  if (strcmp(name, "granulepos") == 0) {
+    ogg_int64_t v = arg_to_64(value);
+    PyObject *err = PyErr_Occurred();
+    if (err)
+      return -1;
+    PY_OGG_PACKET(self)->granulepos = v;
+    return 0;
+  }
+
+  return -1;
+}
+
+static PyObject *
+py_ogg_packet_repr(PyObject *self)
+{
+  ogg_packet *op = PY_OGG_PACKET(self);
+  char buf[256];
+  char *bos = op->b_o_s ? "BOS " : "";
+  char *eos = op->e_o_s ? "EOS " : "";
+
+  sprintf(buf, "<OggPacket, %s%spacketno = %lld, granulepos = %lld,"
+	  " length = %ld at %p>", bos, eos, op->packetno,
+	  op->granulepos, op->bytes, self);
+  return PyString_FromString(buf);
 }
 

1.2       +2 -0      ogg-python/src/pyoggpacket.h

Index: pyoggpacket.h
===================================================================
RCS file: /usr/local/cvsroot/ogg-python/src/pyoggpacket.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- pyoggpacket.h	2001/02/06 03:18:19	1.1
+++ pyoggpacket.h	2001/05/02 01:49:10	1.2
@@ -11,6 +11,8 @@
 
 extern PyTypeObject py_ogg_packet_type;
 
+#define PY_OGG_PACKET(x) (&(((py_ogg_packet *) (x))->op))
+
 PyObject *py_ogg_packet_from_packet(ogg_packet *);
 
 #endif /* PYOGGPACKET_H */

1.3       +66 -10    ogg-python/src/pyoggpage.c

Index: pyoggpage.c
===================================================================
RCS file: /usr/local/cvsroot/ogg-python/src/pyoggpage.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- pyoggpage.c	2001/03/27 23:54:39	1.2
+++ pyoggpage.c	2001/05/02 01:49:10	1.3
@@ -9,8 +9,10 @@
 
 char py_ogg_page_doc[] = "";
 
-static void py_ogg_page_dealloc(py_ogg_page *);
+static void py_ogg_page_dealloc(PyObject *);
 static PyObject* py_ogg_page_getattr(PyObject *, char *);
+static int py_ogg_page_setattr(PyObject *self, char *name, PyObject *value);
+static PyObject *py_ogg_page_repr(PyObject *self);
 
 FDEF(ogg_page_writeout) "Write the page to a given file object. Returns the number of bytes written.";
 FDEF(ogg_page_eos) "Tell whether this page is the end of a stream.";
@@ -29,12 +31,12 @@
   0,
   
   /* Standard Methods */
-  (destructor) py_ogg_page_dealloc,
-  (printfunc) 0,
-  (getattrfunc) py_ogg_page_getattr,
-  (setattrfunc) 0,
-  (cmpfunc) 0,
-  (reprfunc) 0,
+  /* (destructor) */ py_ogg_page_dealloc,
+  /* (printfunc) */ 0,
+  /* (getattrfunc) */ py_ogg_page_getattr,
+  /* (setattrfunc) */ py_ogg_page_setattr,
+  /* (cmpfunc) */ 0,
+  /* (reprfunc) */ py_ogg_page_repr,
   
   /* Type Categories */
   0, /* as number */
@@ -71,7 +73,7 @@
 };
 
 static void 
-py_ogg_page_dealloc(py_ogg_page *self)
+py_ogg_page_dealloc(PyObject *self)
 {
   PyMem_DEL(self);
 }
@@ -90,6 +92,8 @@
   if (pyop == NULL)
     return NULL;
 
+  // FIX: won't this leak memory?  how does the page's pointed-to
+  // memory get freed?
   pyop->op = *op;
   return (PyObject *) pyop;
 }
@@ -117,43 +121,95 @@
 static PyObject*
 py_ogg_page_eos(PyObject *self, PyObject *args)
 {
-  int eos = ogg_page_eos(PY_OGG_PAGE(self));
-  return PyInt_FromLong(eos);
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
+  return PyInt_FromLong(ogg_page_eos(PY_OGG_PAGE(self)));
 }
 
 static PyObject *
 py_ogg_page_version(PyObject *self, PyObject *args) 
 {
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
   return PyInt_FromLong(ogg_page_version(PY_OGG_PAGE(self)));
 }
 
 static PyObject *
 py_ogg_page_serialno(PyObject *self, PyObject *args)
 {
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
   return PyInt_FromLong(ogg_page_serialno(PY_OGG_PAGE(self)));
 }
 
 static PyObject *
 py_ogg_page_pageno(PyObject *self, PyObject *args)
 {
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
   return PyLong_FromLong(ogg_page_pageno(PY_OGG_PAGE(self)));
 }
 
 static PyObject *
 py_ogg_page_continued(PyObject *self, PyObject *args) 
 {
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
   return PyInt_FromLong(ogg_page_continued(PY_OGG_PAGE(self)));
 }
 
 static PyObject *
 py_ogg_page_bos(PyObject *self, PyObject *args) 
 {
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
   return PyInt_FromLong(ogg_page_bos(PY_OGG_PAGE(self)));
 }
 
 static PyObject *
 py_ogg_page_granulepos(PyObject *self, PyObject *args) 
 {
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
   return PyLong_FromLong(ogg_page_granulepos(PY_OGG_PAGE(self)));
 }
 
+static int
+py_ogg_page_setattr(PyObject *self, char *name, PyObject *value)
+{
+  // XXX: somehow the set and get forms should be unified, perhaps to
+  // make them both attribute accesses?
+
+  if (strcmp(name, "pageno") == 0) {
+    // FIX: also handle PyLong?
+    if (PyInt_Check(value)) {
+      long v = PyInt_AsLong(value);
+      char *pb = PY_OGG_PAGE(self)->header;
+      // XXX: ugh, libogg should do this instead
+      int i;
+      for (i=18; i<22; i++) {
+	pb[i] = v & 0xff;
+	v >>= 8;
+      }
+      return 0;
+    } else
+      return -1;
+  }
+  return -1;
+}
+
+static PyObject *
+py_ogg_page_repr(PyObject *self)
+{
+  ogg_page *op = PY_OGG_PAGE(self);
+  char buf[256];
+  char *cont = ogg_page_continued(op) ? "CONT " : "";
+  char *bos = ogg_page_bos(op) ? "BOS " : "";
+  char *eos = ogg_page_eos(op) ? "EOS " : "";
+
+  sprintf(buf, "<OggPage, %s%s%spageno = %ld, granulepos = %lld,"
+	  " serialno = %d, head length = %ld, body length = %ld at %p>",
+	  cont, bos, eos, ogg_page_pageno(op), ogg_page_granulepos(op),
+	  ogg_page_serialno(op), op->header_len, op->body_len, self); 
+  return PyString_FromString(buf);
+}

1.3       +69 -19    ogg-python/src/pyoggstreamstate.c

Index: pyoggstreamstate.c
===================================================================
RCS file: /usr/local/cvsroot/ogg-python/src/pyoggstreamstate.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- pyoggstreamstate.c	2001/03/27 23:54:39	1.2
+++ pyoggstreamstate.c	2001/05/02 01:49:10	1.3
@@ -11,8 +11,9 @@
 
 char py_ogg_stream_state_doc[] = "";
 
-static void py_ogg_stream_state_dealloc(py_ogg_stream_state *);
+static void py_ogg_stream_state_dealloc(PyObject *);
 static PyObject* py_ogg_stream_state_getattr(PyObject *, char *);
+static PyObject *py_ogg_stream_repr(PyObject *self);
 
 FDEF(ogg_stream_packetin) "Add a packet to the stream.";
 FDEF(ogg_stream_clear) "Clear the contents of the stream state.";
@@ -31,12 +32,12 @@
   0,
   
   /* Standard Methods */
-  (destructor) py_ogg_stream_state_dealloc,
-  (printfunc) 0,
-  (getattrfunc) py_ogg_stream_state_getattr,
-  (setattrfunc) 0,
-  (cmpfunc) 0,
-  (reprfunc) 0,
+  /* (destructor) */ py_ogg_stream_state_dealloc,
+  /* (printfunc) */ 0,
+  /* (getattrfunc) */ py_ogg_stream_state_getattr,
+  /* (setattrfunc) */ 0,
+  /* (cmpfunc) */ 0,
+  /* (reprfunc) */ py_ogg_stream_repr,
   
   /* Type Categories */
   0, /* as number */
@@ -55,23 +56,27 @@
 static PyMethodDef py_ogg_stream_state_methods[] = {
   {"clear", py_ogg_stream_clear,
    METH_VARARGS, py_ogg_stream_clear_doc},
-  {"packetin", py_ogg_stream_packetin,
-   METH_VARARGS, py_ogg_stream_packetin_doc},
   {"flush", py_ogg_stream_flush,
    METH_VARARGS, py_ogg_stream_flush_doc},
   {"eos", py_ogg_stream_eos,
    METH_VARARGS, py_ogg_stream_eos_doc},
+  {"packetin", py_ogg_stream_packetin,
+   METH_VARARGS, py_ogg_stream_packetin_doc},
   {"pageout", py_ogg_stream_pageout,
    METH_VARARGS, py_ogg_stream_pageout_doc},
+  {"pagein", py_ogg_stream_pagein,
+   METH_VARARGS, py_ogg_stream_pagein_doc},
+  {"packetout", py_ogg_stream_packetout,
+   METH_VARARGS, py_ogg_stream_packetout_doc},
   {"reset", py_ogg_stream_reset,
    METH_VARARGS, py_ogg_stream_reset_doc},
   {NULL, NULL}
 };
 
 static void 
-py_ogg_stream_state_dealloc(py_ogg_stream_state *self)
+py_ogg_stream_state_dealloc(PyObject *self)
 {
-  ogg_stream_destroy(PY_OGG_STREAM(self));
+  ogg_stream_clear(PY_OGG_STREAM(self));
   PyMem_DEL(self);
 }
 
@@ -106,6 +111,8 @@
 static PyObject *
 py_ogg_stream_clear(PyObject *self, PyObject *args) 
 {
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
   ogg_stream_clear(PY_OGG_STREAM(self));
   Py_INCREF(Py_None);
   return Py_None;
@@ -121,6 +128,7 @@
     return NULL;
   
   if (ogg_stream_packetin(PY_OGG_STREAM(self), &packetobj->op)) {
+    /* currently this can't happen */
     PyErr_SetString(Py_OggError, "error in ogg_stream_packetin");
     return NULL;
   }
@@ -135,6 +143,8 @@
   ogg_page op;
   int res;
   
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
   res = ogg_stream_flush(PY_OGG_STREAM(self), &op);
   if (res == 0) {
     Py_INCREF(Py_None);
@@ -151,6 +161,8 @@
   ogg_page op;
   int res;
   
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
   res = ogg_stream_pageout(PY_OGG_STREAM(self), &op);
   if (res == 0) {
     Py_INCREF(Py_None);
@@ -163,35 +175,73 @@
 py_ogg_stream_eos(PyObject *self, PyObject *args)
 {
   int eos = ogg_stream_eos(PY_OGG_STREAM(self));
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
   return PyInt_FromLong(eos);
 }
 
 static PyObject *
 py_ogg_stream_reset(PyObject *self, PyObject *args)
 {
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
   if (ogg_stream_reset(PY_OGG_STREAM(self))) {
-
     PyErr_SetString(Py_OggError, "Error resetting stream");
     return NULL;
-
   } else {
-
     Py_INCREF(Py_None);
     return Py_None;
-
   }
 }
 
 static PyObject *
 py_ogg_stream_pagein(PyObject *self, PyObject *args)
 {
-  int val;
-  return PyInt_FromLong(val);
+  py_ogg_page *pageobj;
+  
+  if (!PyArg_ParseTuple(args, "O!", &py_ogg_page_type,
+			(PyObject *) &pageobj))
+    return NULL;
+  
+  if (ogg_stream_pagein(PY_OGG_STREAM(self), &pageobj->op)) {
+    PyErr_SetString(Py_OggError, "error in ogg_stream_pagein (bad page?)");
+    return NULL;
+  }
+
+  Py_INCREF(Py_None);
+  return Py_None;
 }
 
 static PyObject *
 py_ogg_stream_packetout(PyObject *self, PyObject *args)
 {
-  int val;
-  return PyInt_FromLong(val);
+  ogg_packet op;
+  int res;
+  
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
+  res = ogg_stream_packetout(PY_OGG_STREAM(self), &op);
+  if (res == 0) {
+    Py_INCREF(Py_None);
+    return Py_None;
+  } else if (res == -1) {
+    PyErr_SetString(Py_OggError, "lost sync");
+    return NULL;
+  }
+  return py_ogg_packet_from_packet(&op);
+}
+
+static PyObject *
+py_ogg_stream_repr(PyObject *self)
+{
+  ogg_stream_state *os = PY_OGG_STREAM(self);
+  char buf[256];
+  char *bos = os->b_o_s ? "BOS " : "";
+  char *eos = os->e_o_s ? "EOS " : "";
+
+  sprintf(buf, "<OggStreamState, %s%spageno = %ld, packetno = %lld,"
+	  " granulepos = %lld, serialno = %d, at %p>",
+	  bos, eos, os->pageno, os->packetno, os->granulepos,
+	  os->serialno, self); 
+  return PyString_FromString(buf);
 }

1.4       +62 -19    ogg-python/src/pyoggsyncstate.c

Index: pyoggsyncstate.c
===================================================================
RCS file: /usr/local/cvsroot/ogg-python/src/pyoggsyncstate.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- pyoggsyncstate.c	2001/03/27 23:54:39	1.3
+++ pyoggsyncstate.c	2001/05/02 01:49:10	1.4
@@ -9,12 +9,15 @@
 
 char py_ogg_sync_state_doc[] = "";
 
-static void py_ogg_sync_state_dealloc(py_ogg_sync_state *);
+static void py_ogg_sync_state_dealloc(PyObject *);
 static PyObject* py_ogg_sync_state_getattr(PyObject *, char *);
 
 FDEF(ogg_sync_clear) "Clear the contents of this object.";
 FDEF(ogg_sync_reset) "";
+#if 0
 FDEF(ogg_sync_wrote) "Tell how many bytes were written to the buffer.";
+#endif
+FDEF(ogg_sync_bytesin) "Append bytes to the sync state buffer.";
 FDEF(ogg_sync_pageseek) "Synchronize with the given OggPage.";
 
 PyTypeObject py_ogg_sync_state_type = {
@@ -25,12 +28,12 @@
   0,
   
   /* Standard Methods */
-  (destructor) py_ogg_sync_state_dealloc,
-  (printfunc) 0,
-  (getattrfunc) py_ogg_sync_state_getattr,
-  (setattrfunc) 0,
-  (cmpfunc) 0,
-  (reprfunc) 0,
+  /* (destructor) */ py_ogg_sync_state_dealloc,
+  /* (printfunc) */ 0,
+  /* (getattrfunc) */ py_ogg_sync_state_getattr,
+  /* (setattrfunc) */ 0,
+  /* (cmpfunc) */ 0,
+  /* (reprfunc) */ 0,
   
   /* Type Categories */
   0, /* as number */
@@ -54,19 +57,26 @@
    METH_VARARGS, py_ogg_sync_reset_doc},
   {"clear", py_ogg_sync_clear,
    METH_VARARGS, py_ogg_sync_clear_doc},
+#if 0
   {"wrote", py_ogg_sync_wrote,
    METH_VARARGS, py_ogg_sync_wrote_doc},
-  {"pageseek", py_ogg_pageseek,
-   METH_VARARGS, py_ogg_pageseek_doc},
+#endif
+  {"bytesin", py_ogg_sync_bytesin,
+   METH_VARARGS, py_ogg_sync_bytesin_doc},
+  {"pageseek", py_ogg_sync_pageseek,
+   METH_VARARGS, py_ogg_sync_pageseek_doc},
   {NULL, NULL}
 };
 
 PyObject *
 py_ogg_sync_state_new(PyObject *self, PyObject *args)
 {
-  py_ogg_sync_state *ret = PyObject_NEW(py_ogg_sync_state,
-					&py_ogg_sync_state_type);
+  py_ogg_sync_state *ret;
 
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
+  ret = PyObject_NEW(py_ogg_sync_state, &py_ogg_sync_state_type);
+
   if (ret == NULL) 
     return NULL;
 
@@ -75,9 +85,9 @@
 }
 
 static void 
-py_ogg_sync_state_dealloc(py_ogg_sync_state *self)
+py_ogg_sync_state_dealloc(PyObject *self)
 {
-  ogg_sync_destroy(PY_OGG_SYNC_STATE(self));
+  ogg_sync_clear(PY_OGG_SYNC_STATE(self));
   PyMem_DEL(self);
 }
 
@@ -91,6 +101,8 @@
 py_ogg_sync_reset(PyObject *self, PyObject *args)
 {
   int ret;
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
   ret = ogg_sync_reset(PY_OGG_SYNC_STATE(self));
   Py_INCREF(Py_None);
   return Py_None;
@@ -100,11 +112,14 @@
 py_ogg_sync_clear(PyObject *self, PyObject *args)
 {
   int ret;
+  if (!PyArg_ParseTuple(args, ""))
+    return NULL;
   ret = ogg_sync_clear(PY_OGG_SYNC_STATE(self));
   Py_INCREF(Py_None);
   return Py_None;
 }
 
+#if 0
 static PyObject *
 py_ogg_sync_wrote(PyObject *self, PyObject *args)
 {
@@ -122,18 +137,46 @@
   Py_INCREF(Py_None);
   return Py_None;
 }
+#endif
+
+
+static PyObject *
+py_ogg_sync_bytesin(PyObject *self, PyObject *args)
+{
+  char *bytes;
+  int byte_count;
+  char *ogg_buffer;
+  int ret;
+
+  if (!PyArg_ParseTuple(args, "s#", &bytes, &byte_count))
+    return NULL;
+
+  // TODO: ogg_sync_buffer should be modified to fail gracefully when
+  // out of memory
+  ogg_buffer = ogg_sync_buffer(PY_OGG_SYNC_STATE(self), byte_count);
+  memcpy(ogg_buffer, bytes, byte_count);
+  ret = ogg_sync_wrote(PY_OGG_SYNC_STATE(self), byte_count);
+  if (ret == -1) {
+    PyErr_SetString(Py_OggError, "internal error: wrote too much!");
+    return NULL;
+  }
+
+  Py_INCREF(Py_None);
+  return Py_None;
+}
 
 static PyObject *
 py_ogg_sync_pageseek(PyObject *self, PyObject *args) 
 {
-  py_ogg_page *page;
+  py_ogg_page page;
   int skipped;
 
-  if (!PyArg_ParseTuple(args, "O!", &py_ogg_page_type, &page))
+  if (!PyArg_ParseTuple(args, ""))
     return NULL;
-
-  skipped = ogg_sync_pageseek(PY_OGG_SYNC_STATE(self),
-			   PY_OGG_PAGE(page));
 
-  return skipped;
+  skipped = ogg_sync_pageseek(PY_OGG_SYNC_STATE(self), &page.op);
+  if (skipped > 0)
+    return Py_BuildValue("iO", skipped, py_ogg_page_from_page(&page.op));
+  else
+    return Py_BuildValue("iO", skipped, Py_None);
 }

1.1                  ogg-python/test/oggtail.py

Index: oggtail.py
===================================================================
#!/usr/bin/env python

#	$Id: oggtail.py,v 1.1 2001/05/02 01:49:11 andrew Exp $

# Send to stdout an ogg stream composed of the last PERCENT percent of
# file OGGFILE.
#
# example:  oggtail.py --percent=PERCENT OGGFILE
#
# (PERCENT is rounded down to the nearest page.  Cutting at the
# nearest packet would be more complicated.)

# First version by Mike Coleman <mkc at mathdogs.com>, April 2001

# TODO: This still doesn't work quite right.  'ogg123' complains with
# a warning if we pipe oggtail's output directly into it, so probably
# there's a minor error in the stream formatting somewhere.

import getopt
import ogg
import sys

_debug = 0

def usage():
    sys.exit("usage: oggtail.py [--debug] --percent=PERCENT OGGFILE")

def debug(s):
    if _debug:
        sys.stderr.write(s)
        sys.stderr.write('\n')

def copy_packets(from_, to_, outstream=None, count=None, start_pageno=0, start_granulepos=0):
    """
    Copy 'count' packets from file 'from_' to file 'to_'.  If 'count'
    is None, copy the rest of 'from_'.  Use 'start_granulepos' as the
    initial granulepos tag for pages.  Bytes will be skipped in
    'from_' to sync, as needed, and copying will stop if a chain
    boundary is encountered.
    """

    # XXX: warn on desync, except at beginning?

    copied = 0
    written = 0
    pageno = start_pageno
    granule_offset = None

    instream = None

    # get it?  insync?  get it?  i'm so funny you can't stand it!
    insync = ogg.OggSyncState()

    while count == None or copied < count:
        b = from_.read(65536)           # size doesn't really matter
        if not b:
            break
        insync.bytesin(b)

        skipped = 1
        while skipped != 0:
            skipped, page = insync.pageseek()
            if skipped > 0:
                if instream and page.serialno() != serialno:
                    # we hit a chain boundary
                    break
                if not instream:
                    serialno = page.serialno()
                    instream = ogg.OggStreamState(serialno)
                    outstream = ogg.OggStreamState(serialno)
                page.pageno = pageno
                debug('*** %s' % page.pageno())
                pageno = pageno + 1
                instream.pagein(page)
                while count == None or copied < count:
                    p = instream.packetout()
                    if not p:
                        break
                    if p.granulepos != -1:
                        if granule_offset == None:
                            granule_offset = p.granulepos
                        p.granulepos = p.granulepos - granule_offset
                    debug('copied %s' % p)
                    outstream.packetin(p)
                    copied = copied + 1
                while 1:
                    pg = outstream.pageout()
                    if not pg:
                        break
                    debug('writing %s' % pg)
                    # FIX: should check success of write
                    written = written + pg.writeout(to_)
            elif skipped < 0:
                print 'skipped', -skipped, 'bytes'

    pg = outstream.flush()
    if pg:
        written = written + pg.writeout(to_)

    return (written, pageno - start_pageno, outstream)
    

opts, pargs = getopt.getopt(sys.argv[1:], '', ['debug', 'percent='])

print sys.argv, opts, pargs
if len(opts) < 1 or len(pargs) != 1:
    usage()

for o, a in opts:
    print o, a
    if o == '--percent':
        try:
            percent = float(a)
        except ValueError:
            usage()
    elif o == '--debug':
        _debug = 1
    else:
        usage()

if not 0.0 <= percent <= 100.0:
    usage()

file = pargs[0]

f = open(file, 'rb')

f.seek(0, 2)
f_size = f.tell()
f.seek(0)

# first copy the three header packets
n, p, os = copy_packets(f, sys.stdout, None, 3)
debug('copied header: %d bytes, %d pages' % (n, p))
debug(str(os))

# then seek to percent and copy out the rest
f.seek(int((100.0 - percent) / 100.0 * f_size))
n, p, os = copy_packets(f, sys.stdout, os)
debug('copied tail: %d bytes, %d pages' % (n, p))
debug(str(os))

--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list