2012-08-08 18:00:05 +00:00
|
|
|
/* -*- Mode: C; ; c-file-style: "k&r"; c-basic-offset: 4 -*- */
|
|
|
|
/* gst-python
|
|
|
|
* Copyright (C) 2002 David I. Lehn
|
2012-08-19 06:25:13 +00:00
|
|
|
* Copyright (C) 2012 Thibault Saunier <thibault.saunier@collabora.com>
|
2012-08-08 18:00:05 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2013-11-25 17:01:48 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
2013-12-12 11:20:12 +00:00
|
|
|
* Boston, MA 02110-1301, USA.
|
2012-08-19 06:25:13 +00:00
|
|
|
*
|
2012-08-08 18:00:05 +00:00
|
|
|
* Author: David I. Lehn <dlehn@users.sourceforge.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* include this first, before NO_IMPORT_PYGOBJECT is defined */
|
|
|
|
#include <Python.h>
|
|
|
|
#include <pygobject.h>
|
|
|
|
#include <gst/gst.h>
|
|
|
|
|
|
|
|
#include <locale.h>
|
|
|
|
|
2013-09-30 10:45:59 +00:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
#define PYGLIB_MODULE_START(symbol, modname) \
|
|
|
|
static struct PyModuleDef _##symbol##module = { \
|
|
|
|
PyModuleDef_HEAD_INIT, \
|
|
|
|
modname, \
|
|
|
|
NULL, \
|
|
|
|
-1, \
|
|
|
|
symbol##_functions, \
|
|
|
|
NULL, \
|
|
|
|
NULL, \
|
|
|
|
NULL, \
|
|
|
|
NULL \
|
|
|
|
}; \
|
|
|
|
PyMODINIT_FUNC PyInit_##symbol(void); \
|
|
|
|
PyMODINIT_FUNC PyInit_##symbol(void) \
|
|
|
|
{ \
|
|
|
|
PyObject *module; \
|
|
|
|
module = PyModule_Create(&_##symbol##module);
|
|
|
|
#define PYGLIB_MODULE_END return module; }
|
|
|
|
#else
|
2012-08-19 06:25:13 +00:00
|
|
|
#define PYGLIB_MODULE_START(symbol, modname) \
|
2013-09-30 10:45:59 +00:00
|
|
|
DL_EXPORT(void) init##symbol(void); \
|
2012-08-19 06:25:13 +00:00
|
|
|
DL_EXPORT(void) init##symbol(void) \
|
2012-08-08 18:00:05 +00:00
|
|
|
{ \
|
|
|
|
PyObject *module; \
|
|
|
|
module = Py_InitModule(modname, symbol##_functions);
|
|
|
|
#define PYGLIB_MODULE_END }
|
2013-09-30 10:45:59 +00:00
|
|
|
#endif
|
2012-08-08 18:00:05 +00:00
|
|
|
|
2012-09-27 12:41:29 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (python_debug);
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (pygst_debug);
|
|
|
|
#define GST_CAT_DEFAULT pygst_debug
|
|
|
|
|
2012-08-08 18:00:05 +00:00
|
|
|
static PyObject *
|
|
|
|
gi_gst_fraction_from_value (const GValue * value)
|
|
|
|
{
|
|
|
|
PyObject *module, *dict, *fraction_type, *args, *fraction;
|
|
|
|
gint numerator, denominator;
|
|
|
|
|
|
|
|
numerator = gst_value_get_fraction_numerator (value);
|
|
|
|
denominator = gst_value_get_fraction_denominator (value);
|
|
|
|
|
|
|
|
module = PyImport_ImportModule ("gi.repository.Gst");
|
2013-08-24 01:42:37 +00:00
|
|
|
|
|
|
|
if (module == NULL) {
|
|
|
|
PyErr_SetString (PyExc_KeyError,
|
|
|
|
"Could not get module for gi.repository.Gst");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-08 18:00:05 +00:00
|
|
|
dict = PyModule_GetDict (module);
|
2013-08-24 01:42:37 +00:00
|
|
|
Py_DECREF (module);
|
|
|
|
|
2012-08-08 18:00:05 +00:00
|
|
|
/* For some reson we need this intermediary step */
|
|
|
|
module = PyMapping_GetItemString (dict, "_overrides_module");
|
2013-08-24 01:42:37 +00:00
|
|
|
if (module == NULL) {
|
|
|
|
PyErr_SetString (PyExc_KeyError,
|
|
|
|
"Could not get module for _overrides_module");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-08 18:00:05 +00:00
|
|
|
dict = PyModule_GetDict (module);
|
|
|
|
fraction_type = PyMapping_GetItemString (dict, "Fraction");
|
|
|
|
|
|
|
|
args = Py_BuildValue ("(ii)", numerator, denominator);
|
|
|
|
fraction = PyObject_Call (fraction_type, args, NULL);
|
|
|
|
Py_DECREF (args);
|
|
|
|
Py_DECREF (fraction_type);
|
|
|
|
Py_DECREF (module);
|
|
|
|
|
|
|
|
return fraction;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gi_gst_fraction_to_value (GValue * value, PyObject * object)
|
|
|
|
{
|
|
|
|
PyObject *numerator, *denominator;
|
|
|
|
|
|
|
|
numerator = PyObject_GetAttrString (object, "num");
|
|
|
|
if (numerator == NULL)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
denominator = PyObject_GetAttrString (object, "denom");
|
|
|
|
if (denominator == NULL)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
gst_value_set_fraction (value,
|
|
|
|
PyLong_AsLong (numerator), PyLong_AsLong (denominator));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gi_gst_register_types (PyObject * d)
|
|
|
|
{
|
|
|
|
pyg_register_gtype_custom (GST_TYPE_FRACTION,
|
|
|
|
gi_gst_fraction_from_value, gi_gst_fraction_to_value);
|
|
|
|
}
|
|
|
|
|
2012-08-19 06:25:13 +00:00
|
|
|
static int
|
|
|
|
add_templates (gpointer gclass, PyObject * templates)
|
|
|
|
{
|
|
|
|
if (PyTuple_Check (templates)) {
|
2012-08-29 17:05:37 +00:00
|
|
|
gint i, len;
|
|
|
|
PyGObject *templ;
|
2012-08-19 06:25:13 +00:00
|
|
|
|
|
|
|
len = PyTuple_Size (templates);
|
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
templ = (PyGObject *) PyTuple_GetItem (templates, i);
|
2016-02-15 22:26:06 +00:00
|
|
|
if (!pygobject_check (templates, &PyGObject_Type) ||
|
|
|
|
GST_IS_PAD_TEMPLATE (pygobject_get (templates)) == FALSE) {
|
2012-08-19 06:25:13 +00:00
|
|
|
PyErr_SetString (PyExc_TypeError,
|
|
|
|
"entries for __gsttemplates__ must be of type GstPadTemplate");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
templ = (PyGObject *) PyTuple_GetItem (templates, i);
|
|
|
|
gst_element_class_add_pad_template (gclass,
|
|
|
|
GST_PAD_TEMPLATE (templ->obj));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
2016-02-15 22:26:06 +00:00
|
|
|
} else if (!pygobject_check (templates, &PyGObject_Type) ||
|
|
|
|
GST_IS_PAD_TEMPLATE (pygobject_get (templates)) == FALSE) {
|
2012-08-19 06:25:13 +00:00
|
|
|
PyErr_SetString (PyExc_TypeError,
|
|
|
|
"entry for __gsttemplates__ must be of type GstPadTemplate");
|
2016-02-15 22:26:06 +00:00
|
|
|
|
2012-08-19 06:25:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (gclass,
|
|
|
|
GST_PAD_TEMPLATE (pygobject_get (templates)));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_pygst_element_set_metadata (gpointer gclass, PyObject * metadata)
|
|
|
|
{
|
|
|
|
|
|
|
|
const gchar *longname, *classification, *description, *author;
|
|
|
|
|
|
|
|
if (!PyTuple_Check (metadata)) {
|
|
|
|
PyErr_SetString (PyExc_TypeError, "__gstmetadata__ must be a tuple");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (PyTuple_Size (metadata) != 4) {
|
|
|
|
PyErr_SetString (PyExc_TypeError,
|
|
|
|
"__gstmetadata__ must contain 4 elements");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!PyArg_ParseTuple (metadata, "ssss", &longname, &classification,
|
|
|
|
&description, &author)) {
|
|
|
|
PyErr_SetString (PyExc_TypeError, "__gstmetadata__ must contain 4 strings");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
GST_DEBUG
|
|
|
|
("setting metadata on gclass %p from __gstmetadata__, longname %s",
|
|
|
|
gclass, longname);
|
|
|
|
|
|
|
|
gst_element_class_set_metadata (gclass, longname, classification,
|
|
|
|
description, author);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_pygst_element_init (gpointer gclass, PyTypeObject * pyclass)
|
|
|
|
{
|
|
|
|
PyObject *templates, *metadata;
|
|
|
|
|
|
|
|
GST_DEBUG ("_pygst_element_init for gclass %p", gclass);
|
|
|
|
templates = PyDict_GetItemString (pyclass->tp_dict, "__gsttemplates__");
|
|
|
|
if (templates) {
|
|
|
|
if (add_templates (gclass, templates) != 0)
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
PyErr_Clear ();
|
|
|
|
}
|
|
|
|
metadata = PyDict_GetItemString (pyclass->tp_dict, "__gstmetadata__");
|
|
|
|
if (metadata) {
|
|
|
|
if (_pygst_element_set_metadata (gclass, metadata) != 0)
|
|
|
|
return -1;
|
|
|
|
PyDict_DelItemString (pyclass->tp_dict, "__gstmetadata__");
|
|
|
|
} else {
|
|
|
|
PyErr_Clear ();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-27 12:41:29 +00:00
|
|
|
#include <frameobject.h>
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
pygst_debug_log (PyObject * pyobject, PyObject * string, GstDebugLevel level,
|
|
|
|
gboolean isgstobject)
|
|
|
|
{
|
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
|
|
|
gchar *str;
|
|
|
|
gchar *function;
|
|
|
|
gchar *filename;
|
|
|
|
int lineno;
|
|
|
|
PyFrameObject *frame;
|
|
|
|
GObject *object = NULL;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple (string, "s:gst.debug_log", &str)) {
|
|
|
|
PyErr_SetString (PyExc_TypeError, "Need a string!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame = PyEval_GetFrame ();
|
2013-09-30 10:45:59 +00:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
{
|
|
|
|
PyObject *utf8;
|
|
|
|
const gchar *utf8_str;
|
|
|
|
|
|
|
|
utf8 = PyUnicode_AsUTF8String (frame->f_code->co_name);
|
|
|
|
utf8_str = PyBytes_AS_STRING (utf8);
|
|
|
|
|
|
|
|
function = g_strdup (utf8_str);
|
|
|
|
Py_DECREF (utf8);
|
|
|
|
|
|
|
|
utf8 = PyUnicode_AsUTF8String (frame->f_code->co_filename);
|
|
|
|
utf8_str = PyBytes_AS_STRING (utf8);
|
|
|
|
|
|
|
|
filename = g_strdup (utf8_str);
|
|
|
|
Py_DECREF (utf8);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
function = g_strdup (PyString_AsString (frame->f_code->co_name));
|
2012-09-27 12:41:29 +00:00
|
|
|
filename =
|
|
|
|
g_path_get_basename (PyString_AsString (frame->f_code->co_filename));
|
2013-09-30 10:45:59 +00:00
|
|
|
#endif
|
2012-09-27 12:41:29 +00:00
|
|
|
lineno = PyCode_Addr2Line (frame->f_code, frame->f_lasti);
|
|
|
|
/* gst_debug_log : category, level, file, function, line, object, format, va_list */
|
|
|
|
if (isgstobject)
|
|
|
|
object = G_OBJECT (pygobject_get (pyobject));
|
|
|
|
gst_debug_log (python_debug, level, filename, function, lineno, object,
|
|
|
|
"%s", str);
|
2013-09-30 10:45:59 +00:00
|
|
|
if (function)
|
|
|
|
g_free (function);
|
2012-09-27 12:41:29 +00:00
|
|
|
if (filename)
|
|
|
|
g_free (filename);
|
|
|
|
#endif
|
|
|
|
Py_INCREF (Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
2015-11-08 10:56:28 +00:00
|
|
|
static PyObject *
|
|
|
|
_wrap_gst_trace (PyObject * whatever, PyObject * string)
|
|
|
|
{
|
|
|
|
return pygst_debug_log (whatever, string, GST_LEVEL_TRACE, FALSE);
|
|
|
|
}
|
|
|
|
|
2012-09-27 12:41:29 +00:00
|
|
|
static PyObject *
|
|
|
|
_wrap_gst_log (PyObject * whatever, PyObject * string)
|
|
|
|
{
|
|
|
|
return pygst_debug_log (whatever, string, GST_LEVEL_LOG, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_wrap_gst_debug (PyObject * whatever, PyObject * string)
|
|
|
|
{
|
|
|
|
return pygst_debug_log (whatever, string, GST_LEVEL_DEBUG, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_wrap_gst_info (PyObject * whatever, PyObject * string)
|
|
|
|
{
|
|
|
|
return pygst_debug_log (whatever, string, GST_LEVEL_INFO, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_wrap_gst_warning (PyObject * whatever, PyObject * string)
|
|
|
|
{
|
|
|
|
return pygst_debug_log (whatever, string, GST_LEVEL_WARNING, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_wrap_gst_error (PyObject * whatever, PyObject * string)
|
|
|
|
{
|
|
|
|
return pygst_debug_log (whatever, string, GST_LEVEL_ERROR, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_wrap_gst_fixme (PyObject * whatever, PyObject * string)
|
|
|
|
{
|
|
|
|
return pygst_debug_log (whatever, string, GST_LEVEL_FIXME, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_wrap_gst_memdump (PyObject * whatever, PyObject * string)
|
|
|
|
{
|
|
|
|
return pygst_debug_log (whatever, string, GST_LEVEL_MEMDUMP, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef _gi_gst_functions[] = {
|
2015-11-08 10:56:28 +00:00
|
|
|
{"trace", (PyCFunction) _wrap_gst_trace, METH_VARARGS,
|
|
|
|
NULL},
|
2012-09-27 12:41:29 +00:00
|
|
|
{"log", (PyCFunction) _wrap_gst_log, METH_VARARGS,
|
|
|
|
NULL},
|
|
|
|
{"debug", (PyCFunction) _wrap_gst_debug, METH_VARARGS,
|
|
|
|
NULL},
|
|
|
|
{"info", (PyCFunction) _wrap_gst_info, METH_VARARGS,
|
|
|
|
NULL},
|
|
|
|
{"warning", (PyCFunction) _wrap_gst_warning, METH_VARARGS,
|
|
|
|
NULL},
|
|
|
|
{"error", (PyCFunction) _wrap_gst_error, METH_VARARGS,
|
|
|
|
NULL},
|
|
|
|
{"fixme", (PyCFunction) _wrap_gst_fixme, METH_VARARGS,
|
|
|
|
NULL},
|
|
|
|
{"memdump", (PyCFunction) _wrap_gst_memdump, METH_VARARGS,
|
|
|
|
NULL}
|
|
|
|
};
|
2012-08-08 18:00:05 +00:00
|
|
|
|
|
|
|
PYGLIB_MODULE_START (_gi_gst, "_gi_gst")
|
|
|
|
{
|
|
|
|
PyObject *d;
|
|
|
|
|
2012-09-27 12:41:29 +00:00
|
|
|
/* gst should have been initialized already */
|
|
|
|
|
|
|
|
/* Initialize debugging category */
|
|
|
|
GST_DEBUG_CATEGORY_INIT (pygst_debug, "pygst", 0,
|
|
|
|
"GStreamer python bindings");
|
|
|
|
GST_DEBUG_CATEGORY_INIT (python_debug, "python", GST_DEBUG_FG_GREEN,
|
|
|
|
"python code using gst-python");
|
|
|
|
|
2012-08-08 18:00:05 +00:00
|
|
|
pygobject_init (3, 0, 0);
|
|
|
|
|
|
|
|
d = PyModule_GetDict (module);
|
|
|
|
gi_gst_register_types (d);
|
2012-08-19 06:25:13 +00:00
|
|
|
pyg_register_class_init (GST_TYPE_ELEMENT, _pygst_element_init);
|
2012-08-08 18:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PYGLIB_MODULE_END;
|