Add Russian translation provided by Валерий Крувялис <valkru@mail.ru>
[xiph-mirror.git] / ogg-python / src / pyoggstreamstate.c
blobd4def67c76de70ecdd7848ae4612fe118f4538b8
1 #include "_oggmodule.h"
2 #include "pyoggstreamstate.h"
3 #include "pyoggpacket.h"
4 #include "pyoggpage.h"
5 #include "general.h"
7 /*****************************************************************
8 OggStreamState Object
9 *****************************************************************/
12 char py_ogg_stream_state_doc[] = "";
14 static void py_ogg_stream_state_dealloc(PyObject *);
15 static PyObject* py_ogg_stream_state_getattr(PyObject *, char *);
16 static PyObject *py_ogg_stream_repr(PyObject *self);
18 FDEF(ogg_stream_packetin) "Add a packet to the stream.";
19 FDEF(ogg_stream_clear) "Clear the contents of the stream state.";
20 FDEF(ogg_stream_flush) "Produce an ogg page suitable for writing to output.";
21 FDEF(ogg_stream_eos) "Return whether the end of the stream is reached.";
22 FDEF(ogg_stream_pageout) "Extract and return an OggPage.";
23 FDEF(ogg_stream_reset) "Reset the stream state";
24 FDEF(ogg_stream_pagein) "Write a page to the stream";
25 FDEF(ogg_stream_packetout) "Extract a packet from the stream";
27 PyTypeObject py_ogg_stream_state_type = {
28 PyObject_HEAD_INIT(NULL)
30 "OggStreamState",
31 sizeof(py_ogg_stream_state),
34 /* Standard Methods */
35 /* (destructor) */ py_ogg_stream_state_dealloc,
36 /* (printfunc) */ 0,
37 /* (getattrfunc) */ py_ogg_stream_state_getattr,
38 /* (setattrfunc) */ 0,
39 /* (cmpfunc) */ 0,
40 /* (reprfunc) */ py_ogg_stream_repr,
42 /* Type Categories */
43 0, /* as number */
44 0, /* as sequence */
45 0, /* as mapping */
46 0, /* hash */
47 0, /* binary */
48 0, /* repr */
49 0, /* getattro */
50 0, /* setattro */
51 0, /* as buffer */
52 0, /* tp_flags */
53 py_ogg_stream_state_doc
56 static PyMethodDef py_ogg_stream_state_methods[] = {
57 {"clear", py_ogg_stream_clear,
58 METH_VARARGS, py_ogg_stream_clear_doc},
59 {"flush", py_ogg_stream_flush,
60 METH_VARARGS, py_ogg_stream_flush_doc},
61 {"eos", py_ogg_stream_eos,
62 METH_VARARGS, py_ogg_stream_eos_doc},
63 {"packetin", py_ogg_stream_packetin,
64 METH_VARARGS, py_ogg_stream_packetin_doc},
65 {"pageout", py_ogg_stream_pageout,
66 METH_VARARGS, py_ogg_stream_pageout_doc},
67 {"pagein", py_ogg_stream_pagein,
68 METH_VARARGS, py_ogg_stream_pagein_doc},
69 {"packetout", py_ogg_stream_packetout,
70 METH_VARARGS, py_ogg_stream_packetout_doc},
71 {"reset", py_ogg_stream_reset,
72 METH_VARARGS, py_ogg_stream_reset_doc},
73 {NULL, NULL}
76 static void
77 py_ogg_stream_state_dealloc(PyObject *self)
79 ogg_stream_clear(PY_OGG_STREAM(self));
80 PyMem_DEL(self);
83 static PyObject*
84 py_ogg_stream_state_getattr(PyObject *self, char *name)
86 return Py_FindMethod(py_ogg_stream_state_methods, self, name);
89 PyObject *
90 py_ogg_stream_state_from_serialno(int serialno)
92 py_ogg_stream_state *ret = PyObject_NEW(py_ogg_stream_state,
93 &py_ogg_stream_state_type);
95 if (ret == NULL)
96 return NULL;
98 ogg_stream_init(&ret->os, serialno);
99 return (PyObject *) ret;
102 PyObject *
103 py_ogg_stream_state_new(PyObject *self, PyObject *args)
105 int serialno;
106 if (!PyArg_ParseTuple(args, "i", &serialno))
107 return NULL;
108 return py_ogg_stream_state_from_serialno(serialno);
111 static PyObject *
112 py_ogg_stream_clear(PyObject *self, PyObject *args)
114 if (!PyArg_ParseTuple(args, ""))
115 return NULL;
116 ogg_stream_clear(PY_OGG_STREAM(self));
117 Py_INCREF(Py_None);
118 return Py_None;
121 static PyObject *
122 py_ogg_stream_packetin(PyObject *self, PyObject *args)
124 py_ogg_packet *packetobj;
126 if (!PyArg_ParseTuple(args, "O!", &py_ogg_packet_type,
127 (PyObject *) &packetobj))
128 return NULL;
130 if (ogg_stream_packetin(PY_OGG_STREAM(self), &packetobj->op)) {
131 /* currently this can't happen */
132 PyErr_SetString(Py_OggError, "error in ogg_stream_packetin");
133 return NULL;
136 Py_INCREF(Py_None);
137 return Py_None;
140 static PyObject *
141 py_ogg_stream_flush(PyObject *self, PyObject *args)
143 ogg_page op;
144 int res;
146 if (!PyArg_ParseTuple(args, ""))
147 return NULL;
148 res = ogg_stream_flush(PY_OGG_STREAM(self), &op);
149 if (res == 0) {
150 Py_INCREF(Py_None);
151 return Py_None;
153 return py_ogg_page_from_page(&op);
156 /* TODO: You can't keep a page between calls to flush or pageout!! */
158 static PyObject *
159 py_ogg_stream_pageout(PyObject *self, PyObject *args)
161 ogg_page op;
162 int res;
164 if (!PyArg_ParseTuple(args, ""))
165 return NULL;
166 res = ogg_stream_pageout(PY_OGG_STREAM(self), &op);
167 if (res == 0) {
168 Py_INCREF(Py_None);
169 return Py_None;
171 return py_ogg_page_from_page(&op);
174 static PyObject*
175 py_ogg_stream_eos(PyObject *self, PyObject *args)
177 int eos = ogg_stream_eos(PY_OGG_STREAM(self));
178 if (!PyArg_ParseTuple(args, ""))
179 return NULL;
180 return PyInt_FromLong(eos);
183 static PyObject *
184 py_ogg_stream_reset(PyObject *self, PyObject *args)
186 if (!PyArg_ParseTuple(args, ""))
187 return NULL;
188 if (ogg_stream_reset(PY_OGG_STREAM(self))) {
189 PyErr_SetString(Py_OggError, "Error resetting stream");
190 return NULL;
191 } else {
192 Py_INCREF(Py_None);
193 return Py_None;
197 static PyObject *
198 py_ogg_stream_pagein(PyObject *self, PyObject *args)
200 py_ogg_page *pageobj;
202 if (!PyArg_ParseTuple(args, "O!", &py_ogg_page_type,
203 (PyObject *) &pageobj))
204 return NULL;
206 if (ogg_stream_pagein(PY_OGG_STREAM(self), &pageobj->op)) {
207 PyErr_SetString(Py_OggError, "error in ogg_stream_pagein (bad page?)");
208 return NULL;
211 Py_INCREF(Py_None);
212 return Py_None;
215 static PyObject *
216 py_ogg_stream_packetout(PyObject *self, PyObject *args)
218 ogg_packet op;
219 int res;
221 if (!PyArg_ParseTuple(args, ""))
222 return NULL;
223 res = ogg_stream_packetout(PY_OGG_STREAM(self), &op);
224 if (res == 0) {
225 Py_INCREF(Py_None);
226 return Py_None;
227 } else if (res == -1) {
228 PyErr_SetString(Py_OggError, "lost sync");
229 return NULL;
231 return py_ogg_packet_from_packet(&op);
234 static PyObject *
235 py_ogg_stream_repr(PyObject *self)
237 ogg_stream_state *os = PY_OGG_STREAM(self);
238 char buf[256];
239 char *bos = os->b_o_s ? "BOS " : "";
240 char *eos = os->e_o_s ? "EOS " : "";
242 sprintf(buf, "<OggStreamState, %s%spageno = %ld, packetno = %lld,"
243 " granulepos = %lld, serialno = %d, at %p>",
244 bos, eos, os->pageno, os->packetno, os->granulepos,
245 os->serialno, self);
246 return PyString_FromString(buf);