All pastes #820513 Raw Edit

Untitled

public c v1 · immutable
·published 2007-12-17 16:39 UTC
rendered paste body
#include <Python.h>#include <structmember.h>#include <xine.h>static xine_t *gs_xine = NULL;static xine_audio_port_t *gs_audio_port = NULL;static int gs_audioplayer_count = 0;typedef struct {    PyObject_HEAD    xine_stream_t *stream;} AudioPlayer;static void AudioPlayer_dealloc(AudioPlayer *self){    gs_audioplayer_count--;    if (self->stream)        xine_dispose(self->stream);    if (gs_audioplayer_count == 0) {        xine_close_audio_driver(gs_xine, gs_audio_port);        xine_exit(gs_xine);        gs_audio_port = NULL;        gs_xine = NULL;    }    self->ob_type->tp_free((PyObject *)self);}static int AudioPlayer_init(AudioPlayer *self, PyObject *args,                                  PyObject *kwds){    gs_audioplayer_count++;    if (!gs_xine) {        gs_xine = xine_new();        if (!gs_xine) {            PyErr_SetString(PyExc_RuntimeError,                            "Failed to initialise xine engine");            return -1;        }        xine_init(gs_xine);    }    if (!gs_audio_port) {        gs_audio_port = xine_open_audio_driver(gs_xine, NULL, NULL);        if (!gs_audio_port) {            PyErr_SetString(PyExc_RuntimeError,                            "Failed to open an audio driver");            return -1;        }    }    self->stream = xine_stream_new(gs_xine, gs_audio_port, NULL);    if (!self->stream) {        PyErr_SetString(PyExc_RuntimeError,                        "Failed to create xine_stream_t* instance");        return -1;    }    return 0;}static void AudioPlayer_open(AudioPlayer *self, PyObject *_mrl){    int res;    char *mrl;    mrl = PyString_AsString(_mrl);    if (!mrl) {        PyErr_BadArgument();        return;    }    res = xine_open(self->stream, mrl);    if (res == 0) {        PyErr_SetString(PyExc_RuntimeError, "Failed to open mrl");    }    printf("Got here\n");}static PyMemberDef AudioPlayer_members[] = {    {NULL}};static PyMethodDef AudioPlayer_methods[] = {    {"open", (PyCFunction)AudioPlayer_open, METH_O, "Open the given MRL"},    {NULL}};static PyTypeObject AudioPlayerType = {    PyObject_HEAD_INIT(NULL)    0,                                  /* ob_size */    "miniplay.audioplayer.AudioPlayer", /* tp_name */    sizeof(AudioPlayer),                /* tp_basicsize */    0,                                  /* tp_itemsize */    (destructor) AudioPlayer_dealloc,   /* tp_dealloc */    0,                                  /* tp_print */    0,                                  /* tp_getattr */    0,                                  /* tp_setattr */    0,                                  /* tp_compare */    0,                                  /* tp_repr */    0,                                  /* tp_as_number */    0,                                  /* tp_as_sequence */    0,                                  /* tp_as_mapping */    0,                                  /* tp_hash */    0,                                  /* tp_call */    0,                                  /* tp_str */    0,                                  /* tp_getattro */    0,                                  /* tp_setattro */    0,                                  /* tp_as_buffer */    Py_TPFLAGS_DEFAULT,                 /* tp_flags */    "Plays audio using libxine",        /* tp_doc */    0,                                  /* tp_traverse */    0,                                  /* tp_clear */    0,                                  /* tp_richcompare */    0,                                  /* tp_weaklistoffset */    0,                                  /* tp_iter */    0,                                  /* tp_iternext */    AudioPlayer_methods,                /* tp_methods */    AudioPlayer_members,                /* tp_members */    0,                                  /* tp_getset */    0,                                  /* tp_base */    0,                                  /* tp_dict */    0,                                  /* tp_descr_get */    0,                                  /* tp_descr_set */    0,                                  /* tp_dictoffset */    (initproc) AudioPlayer_init         /* tp_init */};static PyMethodDef methods[] = {    {NULL}};#ifndef PyMODINIT_FUNC#define PyMODINIT_FUNC void#endifPyMODINIT_FUNC initaudioplayer(void){    PyObject *module;    AudioPlayerType.tp_new = PyType_GenericNew;    if (PyType_Ready(&AudioPlayerType) < 0)        return;    module = Py_InitModule3("audioplayer", methods,                            "Implements the AudioPlayer type");    Py_INCREF(&AudioPlayerType);    PyModule_AddObject(module, "AudioPlayer", (PyObject *)&AudioPlayerType);}