1 /* -*- Mode: C; ; c-file-style: "k&r"; c-basic-offset: 4 -*- */
3 * Copyright (C) 2002 David I. Lehn
4 * Copyright (C) 2012 Thibault Saunier <thibault.saunier@collabora.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
21 * Author: David I. Lehn <dlehn@users.sourceforge.net>
28 /* include this first, before NO_IMPORT_PYGOBJECT is defined */
30 #include <pygobject.h>
35 #define PYGLIB_MODULE_START(symbol, modname) \
36 static struct PyModuleDef _##symbol##module = { \
37 PyModuleDef_HEAD_INIT, \
47 PyMODINIT_FUNC PyInit_##symbol(void); \
48 PyMODINIT_FUNC PyInit_##symbol(void) \
51 module = PyModule_Create(&_##symbol##module);
52 #define PYGLIB_MODULE_END return module; }
54 GST_DEBUG_CATEGORY_STATIC (python_debug);
55 GST_DEBUG_CATEGORY_STATIC (pygst_debug);
56 #define GST_CAT_DEFAULT pygst_debug
59 gi_gst_get_type (const gchar * type_name)
61 PyObject *module, *dict;
63 module = PyImport_ImportModule ("gi.repository.Gst");
66 PyErr_SetString (PyExc_KeyError,
67 "Could not get module for gi.repository.Gst");
71 dict = PyModule_GetDict (module);
74 /* For some reason we need this intermediary step */
75 module = PyMapping_GetItemString (dict, "_overrides_module");
77 PyErr_SetString (PyExc_KeyError,
78 "Could not get module for _overrides_module");
82 dict = PyModule_GetDict (module);
83 return PyMapping_GetItemString (dict, type_name);
87 gi_gst_fraction_from_value (const GValue * value)
89 PyObject *fraction_type, *args, *fraction;
90 gint numerator, denominator;
92 numerator = gst_value_get_fraction_numerator (value);
93 denominator = gst_value_get_fraction_denominator (value);
95 fraction_type = gi_gst_get_type ("Fraction");
97 args = Py_BuildValue ("(ii)", numerator, denominator);
98 fraction = PyObject_Call (fraction_type, args, NULL);
105 gi_gst_fraction_to_value (GValue * value, PyObject * object)
107 glong numerator, denominator;
108 PyObject *numerator_obj, *denominator_obj, *is_integer;
110 numerator_obj = PyObject_GetAttrString (object, "num");
111 if (numerator_obj == NULL)
114 is_integer = PyObject_CallMethod (numerator_obj, "is_integer", NULL);
115 if (is_integer != Py_True) {
116 PyErr_Format (PyExc_TypeError,
117 "numerator %f is not an integer.", PyFloat_AsDouble (numerator_obj));
118 Py_DECREF (is_integer);
121 Py_DECREF (is_integer);
123 numerator = PyFloat_AsDouble (numerator_obj);
124 if (numerator < -G_MAXINT || numerator > G_MAXINT) {
125 PyErr_Format (PyExc_ValueError,
126 "numerator %" G_GINT64_FORMAT " is out of bound. [-%d - %d]",
127 numerator, G_MAXINT, G_MAXINT);
131 denominator_obj = PyObject_GetAttrString (object, "denom");
132 if (denominator_obj == NULL)
135 is_integer = PyObject_CallMethod (denominator_obj, "is_integer", NULL);
136 if (is_integer != Py_True) {
137 PyErr_Format (PyExc_TypeError,
138 "denominator %f is not an integer.",
139 PyFloat_AsDouble (denominator_obj));
140 Py_DECREF (is_integer);
143 Py_DECREF (is_integer);
145 denominator = PyFloat_AsDouble (denominator_obj);
146 if (denominator == 0) {
147 PyErr_SetString (PyExc_ValueError, "denominator is 0.");
151 if (denominator < -G_MAXINT || denominator > G_MAXINT) {
152 PyErr_Format (PyExc_ValueError,
153 "denominator %" G_GINT64_FORMAT " is out of bound. [-%d - %d]",
154 denominator, G_MAXINT, G_MAXINT);
158 gst_value_set_fraction (value, numerator, denominator);
167 gi_gst_int_range_from_value (const GValue * value)
170 PyObject *int_range_type, *int_range, *range;
172 min = gst_value_get_int_range_min (value);
173 max = gst_value_get_int_range_max (value);
174 step = gst_value_get_int_range_step (value);
176 int_range_type = gi_gst_get_type ("IntRange");
177 range = PyObject_CallFunction ((PyObject *) & PyRange_Type, "iii",
179 int_range = PyObject_CallFunction (int_range_type, "O", range);
181 Py_DECREF (int_range_type);
188 gi_gst_int_range_to_value (GValue * value, PyObject * object)
190 PyObject *range, *min, *max, *step;
192 range = PyObject_GetAttrString (object, "range");
196 min = PyObject_GetAttrString (range, "start");
200 max = PyObject_GetAttrString (range, "stop");
204 step = PyObject_GetAttrString (range, "step");
208 gst_value_set_int_range_step (value, PyLong_AsLong (min),
209 PyLong_AsLong (max), PyLong_AsLong (step));
214 PyErr_SetString (PyExc_KeyError,
215 "Object is not compatible with Gst.IntRange");
220 gi_gst_int64_range_from_value (const GValue * value)
222 gint64 min, max, step;
223 PyObject *int64_range_type, *int64_range, *range;
225 min = gst_value_get_int64_range_min (value);
226 max = gst_value_get_int64_range_max (value);
227 step = gst_value_get_int64_range_step (value);
229 range = PyObject_CallFunction ((PyObject *) & PyRange_Type, "LLL",
231 int64_range_type = gi_gst_get_type ("Int64Range");
232 int64_range = PyObject_CallFunction (int64_range_type, "O", range);
234 Py_DECREF (int64_range_type);
241 gi_gst_int64_range_to_value (GValue * value, PyObject * object)
243 PyObject *range, *min, *max, *step;
245 range = PyObject_GetAttrString (object, "range");
249 min = PyObject_GetAttrString (range, "start");
253 max = PyObject_GetAttrString (range, "stop");
257 step = PyObject_GetAttrString (range, "step");
261 gst_value_set_int64_range_step (value, PyLong_AsLongLong (min),
262 PyLong_AsLongLong (max), PyLong_AsLongLong (step));
267 PyErr_SetString (PyExc_KeyError,
268 "Object is not compatible with Gst.Int64Range");
273 gi_gst_double_range_from_value (const GValue * value)
275 PyObject *double_range_type, *double_range;
278 min = gst_value_get_double_range_min (value);
279 max = gst_value_get_double_range_max (value);
281 double_range_type = gi_gst_get_type ("DoubleRange");
282 double_range = PyObject_CallFunction (double_range_type, "dd", min, max);
284 Py_DECREF (double_range_type);
290 gi_gst_double_range_to_value (GValue * value, PyObject * object)
294 min = PyObject_GetAttrString (object, "start");
298 max = PyObject_GetAttrString (object, "stop");
302 gst_value_set_double_range (value, PyFloat_AsDouble (min),
303 PyFloat_AsDouble (max));
308 PyErr_SetString (PyExc_KeyError,
309 "Object is not compatible with Gst.DoubleRange");
314 gi_gst_fraction_range_from_value (const GValue * value)
316 PyObject *min, *max, *fraction_range_type, *fraction_range;
317 const GValue *fraction;
319 fraction = gst_value_get_fraction_range_min (value);
320 min = gi_gst_fraction_from_value (fraction);
322 fraction = gst_value_get_fraction_range_max (value);
323 max = gi_gst_fraction_from_value (fraction);
325 fraction_range_type = gi_gst_get_type ("FractionRange");
326 fraction_range = PyObject_CallFunction (fraction_range_type, "NN", min, max);
328 Py_DECREF (fraction_range_type);
330 return fraction_range;
334 gi_gst_fraction_range_to_value (GValue * value, PyObject * object)
337 GValue vmin = G_VALUE_INIT, vmax = G_VALUE_INIT;
339 min = PyObject_GetAttrString (object, "start");
343 max = PyObject_GetAttrString (object, "stop");
347 g_value_init (&vmin, GST_TYPE_FRACTION);
348 if (gi_gst_fraction_to_value (&vmin, min) < 0)
351 g_value_init (&vmax, GST_TYPE_FRACTION);
352 if (gi_gst_fraction_to_value (&vmax, max) < 0) {
353 g_value_unset (&vmin);
357 gst_value_set_fraction_range (value, &vmin, &vmax);
358 g_value_unset (&vmin);
359 g_value_unset (&vmax);
364 PyErr_SetString (PyExc_KeyError,
365 "Object is not compatible with Gst.FractionRange");
370 gi_gst_array_from_value (const GValue * value)
372 PyObject *list, *array_type, *array;
375 list = PyList_New (gst_value_array_get_size (value));
377 for (i = 0; i < gst_value_array_get_size (value); i++) {
378 const GValue *v = gst_value_array_get_value (value, i);
379 PyList_SET_ITEM (list, i, pyg_value_as_pyobject (v, TRUE));
382 array_type = gi_gst_get_type ("ValueArray");
383 array = PyObject_CallFunction (array_type, "N", list);
385 Py_DECREF (array_type);
391 gi_gst_array_to_value (GValue * value, PyObject * object)
395 len = PySequence_Length (object);
397 for (i = 0; i < len; i++) {
398 GValue v = G_VALUE_INIT;
402 item = PySequence_GetItem (object, i);
405 type = G_TYPE_POINTER;
407 type = pyg_type_from_object ((PyObject *) Py_TYPE (item));
409 if (type == G_TYPE_NONE) {
414 g_value_init (&v, type);
416 if (pyg_value_from_pyobject (&v, item) < 0) {
421 gst_value_array_append_and_take_value (value, &v);
428 PyErr_SetString (PyExc_KeyError,
429 "Object is not compatible with Gst.ValueArray");
434 gi_gst_bitmask_from_value (const GValue * value)
436 PyObject *val, *bitmask_type;
438 bitmask_type = gi_gst_get_type ("Bitmask");
439 val = PyObject_CallFunction (bitmask_type, "L",
440 gst_value_get_bitmask (value));
441 Py_DECREF (bitmask_type);
447 gi_gst_bitmask_to_value (GValue * value, PyObject * object)
449 PyObject *v = PyObject_GetAttrString (object, "v");
453 gst_value_set_bitmask (value, PyLong_AsLong (v));
458 PyErr_SetString (PyExc_KeyError, "Object is not compatible with Gst.Bitmask");
463 gi_gst_list_from_value (const GValue * value)
465 PyObject *list, *value_list_type, *value_list;
468 list = PyList_New (gst_value_list_get_size (value));
470 for (i = 0; i < gst_value_list_get_size (value); i++) {
471 const GValue *v = gst_value_list_get_value (value, i);
472 PyList_SET_ITEM (list, i, pyg_value_as_pyobject (v, TRUE));
475 value_list_type = gi_gst_get_type ("ValueList");
476 value_list = PyObject_CallFunction (value_list_type, "N", list);
478 Py_DECREF (value_list_type);
484 gi_gst_list_to_value (GValue * value, PyObject * object)
488 len = PySequence_Length (object);
490 for (i = 0; i < len; i++) {
491 GValue v = G_VALUE_INIT;
495 item = PySequence_GetItem (object, i);
498 type = G_TYPE_POINTER;
500 type = pyg_type_from_object ((PyObject *) Py_TYPE (item));
502 if (type == G_TYPE_NONE) {
507 g_value_init (&v, type);
509 if (pyg_value_from_pyobject (&v, item) < 0) {
514 gst_value_list_append_and_take_value (value, &v);
521 PyErr_SetString (PyExc_KeyError,
522 "Object is not compatible with Gst.ValueList");
527 gi_gst_register_types (PyObject * d)
529 pyg_register_gtype_custom (GST_TYPE_FRACTION,
530 gi_gst_fraction_from_value, gi_gst_fraction_to_value);
531 pyg_register_gtype_custom (GST_TYPE_INT_RANGE,
532 gi_gst_int_range_from_value, gi_gst_int_range_to_value);
533 pyg_register_gtype_custom (GST_TYPE_INT64_RANGE,
534 gi_gst_int64_range_from_value, gi_gst_int64_range_to_value);
535 pyg_register_gtype_custom (GST_TYPE_DOUBLE_RANGE,
536 gi_gst_double_range_from_value, gi_gst_double_range_to_value);
537 pyg_register_gtype_custom (GST_TYPE_FRACTION_RANGE,
538 gi_gst_fraction_range_from_value, gi_gst_fraction_range_to_value);
539 pyg_register_gtype_custom (GST_TYPE_ARRAY,
540 gi_gst_array_from_value, gi_gst_array_to_value);
541 pyg_register_gtype_custom (GST_TYPE_LIST,
542 gi_gst_list_from_value, gi_gst_list_to_value);
545 pyg_register_gtype_custom (GST_TYPE_DATE_TIME,
546 gi_gst_date_time_from_value, gi_gst_date_time_to_value);
547 pyg_register_gtype_custom (GST_TYPE_FLAG_SET,
548 gi_gst_flag_set_from_value, gi_gst_flag_set_to_value);
550 pyg_register_gtype_custom (GST_TYPE_BITMASK,
551 gi_gst_bitmask_from_value, gi_gst_bitmask_to_value);
555 add_templates (gpointer gclass, PyObject * templates)
557 if (PyTuple_Check (templates)) {
561 len = PyTuple_Size (templates);
565 for (i = 0; i < len; i++) {
566 templ = (PyGObject *) PyTuple_GetItem (templates, i);
568 if (!pygobject_check (templ, &PyGObject_Type)) {
569 PyObject *repr = PyObject_Repr ((PyObject *) templ);
570 #if PY_VERSION_HEX < 0x03000000
571 PyErr_Format (PyExc_TypeError, "expected GObject but got %s",
572 PyString_AsString (repr));
574 PyErr_Format (PyExc_TypeError, "expected GObject but got %s",
575 _PyUnicode_AsString (repr));
580 } else if (!GST_IS_PAD_TEMPLATE (pygobject_get (templ))) {
583 ("entries for __gsttemplates__ must be of type GstPadTemplate (%s)",
584 G_OBJECT_TYPE_NAME (pygobject_get (templ)));
585 PyErr_SetString (PyExc_TypeError, error);
592 for (i = 0; i < len; i++) {
593 templ = (PyGObject *) PyTuple_GetItem (templates, i);
594 gst_element_class_add_pad_template (gclass,
595 GST_PAD_TEMPLATE (templ->obj));
599 } else if (!pygobject_check (templates, &PyGObject_Type) ||
600 GST_IS_PAD_TEMPLATE (pygobject_get (templates)) == FALSE) {
601 PyErr_SetString (PyExc_TypeError,
602 "entry for __gsttemplates__ must be of type GstPadTemplate");
607 gst_element_class_add_pad_template (gclass,
608 GST_PAD_TEMPLATE (pygobject_get (templates)));
614 _pygst_element_set_metadata (gpointer gclass, PyObject * metadata)
617 const gchar *longname, *classification, *description, *author;
619 if (!PyTuple_Check (metadata)) {
620 PyErr_SetString (PyExc_TypeError, "__gstmetadata__ must be a tuple");
623 if (PyTuple_Size (metadata) != 4) {
624 PyErr_SetString (PyExc_TypeError,
625 "__gstmetadata__ must contain 4 elements");
628 if (!PyArg_ParseTuple (metadata, "ssss", &longname, &classification,
629 &description, &author)) {
630 PyErr_SetString (PyExc_TypeError, "__gstmetadata__ must contain 4 strings");
634 ("setting metadata on gclass %p from __gstmetadata__, longname %s",
637 gst_element_class_set_metadata (gclass, longname, classification,
638 description, author);
643 _pygst_element_init (gpointer gclass, PyTypeObject * pyclass)
645 PyObject *templates, *metadata;
647 GST_DEBUG ("_pygst_element_init for gclass %p", gclass);
648 templates = PyDict_GetItemString (pyclass->tp_dict, "__gsttemplates__");
650 if (add_templates (gclass, templates) != 0)
655 metadata = PyDict_GetItemString (pyclass->tp_dict, "__gstmetadata__");
657 if (_pygst_element_set_metadata (gclass, metadata) != 0)
659 PyDict_DelItemString (pyclass->tp_dict, "__gstmetadata__");
667 #include <frameobject.h>
670 pygst_debug_log (PyObject * pyobject, PyObject * string, GstDebugLevel level,
671 gboolean isgstobject)
673 #ifndef GST_DISABLE_GST_DEBUG
678 PyFrameObject *frame;
679 GObject *object = NULL;
681 if (!PyArg_ParseTuple (string, "s:gst.debug_log", &str)) {
682 PyErr_SetString (PyExc_TypeError, "Need a string!");
686 frame = PyEval_GetFrame ();
689 const gchar *utf8_str;
691 utf8 = PyUnicode_AsUTF8String (frame->f_code->co_name);
692 utf8_str = PyBytes_AS_STRING (utf8);
694 function = g_strdup (utf8_str);
697 utf8 = PyUnicode_AsUTF8String (frame->f_code->co_filename);
698 utf8_str = PyBytes_AS_STRING (utf8);
700 filename = g_strdup (utf8_str);
703 lineno = PyCode_Addr2Line (frame->f_code, frame->f_lasti);
704 /* gst_debug_log : category, level, file, function, line, object, format, va_list */
706 object = G_OBJECT (pygobject_get (pyobject));
707 gst_debug_log (python_debug, level, filename, function, lineno, object,
719 _wrap_gst_trace (PyObject * whatever, PyObject * string)
721 return pygst_debug_log (whatever, string, GST_LEVEL_TRACE, FALSE);
725 _wrap_gst_log (PyObject * whatever, PyObject * string)
727 return pygst_debug_log (whatever, string, GST_LEVEL_LOG, FALSE);
731 _wrap_gst_debug (PyObject * whatever, PyObject * string)
733 return pygst_debug_log (whatever, string, GST_LEVEL_DEBUG, FALSE);
737 _wrap_gst_info (PyObject * whatever, PyObject * string)
739 return pygst_debug_log (whatever, string, GST_LEVEL_INFO, FALSE);
743 _wrap_gst_warning (PyObject * whatever, PyObject * string)
745 return pygst_debug_log (whatever, string, GST_LEVEL_WARNING, FALSE);
749 _wrap_gst_error (PyObject * whatever, PyObject * string)
751 return pygst_debug_log (whatever, string, GST_LEVEL_ERROR, FALSE);
755 _wrap_gst_fixme (PyObject * whatever, PyObject * string)
757 return pygst_debug_log (whatever, string, GST_LEVEL_FIXME, FALSE);
761 _wrap_gst_memdump (PyObject * whatever, PyObject * string)
763 return pygst_debug_log (whatever, string, GST_LEVEL_MEMDUMP, FALSE);
767 _remap (GstMapInfo * mapinfo, PyObject * py_mapinfo)
769 PyObject *success = NULL;
770 PyObject *py_cmapinfo = NULL;
771 PyObject *py_mview = NULL;
772 PyObject *py_memory = NULL;
773 PyObject *py_flags = NULL;
774 PyObject *py_size = NULL;
775 PyObject *py_maxsize = NULL;
777 /* Fill and encapsulating the mapinfo pointer */
778 py_cmapinfo = PyCapsule_New (mapinfo, "__cmapinfo", NULL);
780 || PyObject_SetAttrString (py_mapinfo, "__cmapinfo", py_cmapinfo))
783 /* Fill and create memoryview with compatible flags */
785 flags = (mapinfo->flags & GST_MAP_WRITE) ? PyBUF_WRITE : PyBUF_READ;
787 PyMemoryView_FromMemory ((char *) mapinfo->data, mapinfo->size, flags);
788 if (!py_mview || PyObject_SetAttrString (py_mapinfo, "data", py_mview))
791 /* Fill and box GstMemory into a Gst.Memory */
792 py_memory = pyg_boxed_new (_gst_memory_type, mapinfo->memory, FALSE, FALSE);
793 if (!py_memory || PyObject_SetAttrString (py_mapinfo, "memory", py_memory))
796 /* Fill out Gst.MapInfo with values corresponding to GstMapInfo */
797 py_flags = Py_BuildValue ("i", mapinfo->flags);
798 if (!py_flags || PyObject_SetAttrString (py_mapinfo, "flags", py_flags))
801 py_size = Py_BuildValue ("i", mapinfo->size);
802 if (!py_size || PyObject_SetAttrString (py_mapinfo, "size", py_size))
805 py_maxsize = Py_BuildValue ("i", mapinfo->maxsize);
806 if (!py_maxsize || PyObject_SetAttrString (py_mapinfo, "maxsize", py_maxsize))
814 GST_ERROR ("Could not map the Gst.MapInfo PyObject with GstMapInfo");
816 PyObject_CallMethod (py_mview, "release", NULL);
819 Py_XDECREF (py_cmapinfo);
820 Py_XDECREF (py_mview);
821 Py_XDECREF (py_memory);
822 Py_XDECREF (py_flags);
823 Py_XDECREF (py_size);
824 Py_XDECREF (py_maxsize);
829 _unmap (GstMapInfo ** mapinfo, PyObject * py_mapinfo)
831 PyObject *py_cmapinfo = NULL, *py_mview = NULL, *success = NULL;
833 if (!PyObject_HasAttrString (py_mapinfo, "__cmapinfo"))
836 /* Extract attributes from Gst.MapInfo */
837 py_mview = PyObject_GetAttrString (py_mapinfo, "data");
841 /* Call the memoryview.release() Python method, there is no C API */
842 if (!PyObject_CallMethod (py_mview, "release", NULL))
845 py_cmapinfo = PyObject_GetAttrString (py_mapinfo, "__cmapinfo");
849 /* Reconstruct GstMapInfo from Gst.MapInfo contents */
850 *mapinfo = PyCapsule_GetPointer (py_cmapinfo, "__cmapinfo");
854 if (PyObject_DelAttrString (py_mapinfo, "__cmapinfo") == -1)
863 GST_ERROR ("Could not unmap the GstMapInfo from Gst.MapInfo PyObject");
864 Py_INCREF (Py_False);
868 Py_XDECREF (py_mview);
869 Py_XDECREF (py_cmapinfo);
874 _gst_memory_override_map (PyObject * self, PyObject * args)
876 PyTypeObject *gst_memory_type;
877 PyObject *py_memory, *py_mapinfo, *success;
883 /* Look up Gst.memory, Gst.MapInfo, and Gst.MapFlags parameters */
884 gst_memory_type = pygobject_lookup_class (_gst_memory_type);
885 if (!PyArg_ParseTuple (args, "O!Oi", gst_memory_type, &py_memory,
886 &py_mapinfo, &flags))
889 /* Since Python does only support r/o or r/w it has to be changed to either */
890 flags = (flags & GST_MAP_WRITE) ? GST_MAP_READWRITE : GST_MAP_READ;
892 /* Extract GstMemory from Gst.Memory parameter */
893 memory = GST_MEMORY_CAST (pygobject_get (py_memory));
895 /* Map the memory, fill out GstMapInfo */
896 mapinfo = g_new0 (GstMapInfo, 1);
897 ok = gst_memory_map (memory, mapinfo, flags);
903 success = _remap (mapinfo, py_mapinfo);
905 gst_memory_unmap (memory, mapinfo);
911 Py_INCREF (Py_False);
916 _gst_memory_override_unmap (PyObject * self, PyObject * args)
918 PyTypeObject *gst_memory_type;
919 PyObject *py_memory, *py_mapinfo, *success;
921 GstMapInfo *mapinfo = NULL;
923 /* Look up Gst.Buffer and Gst.Mapinfo parameters */
924 gst_memory_type = pygobject_lookup_class (_gst_memory_type);
925 if (!PyArg_ParseTuple (args, "O!O", gst_memory_type, &py_memory, &py_mapinfo)) {
926 PyErr_BadArgument ();
930 success = _unmap (&mapinfo, py_mapinfo);
931 if (PyBool_Check (success) && mapinfo) {
932 /* Extract GstBuffer from Gst.Buffer parameter */
933 memory = GST_MEMORY_CAST (pygobject_get (py_memory));
935 /* Unmap the buffer, using reconstructed GstMapInfo */
936 gst_memory_unmap (memory, mapinfo);
944 _gst_buffer_override_map_range (PyObject * self, PyObject * args)
946 PyTypeObject *gst_buffer_type;
947 PyObject *py_buffer, *py_mapinfo, *success;
954 /* Look up Gst.Buffer, Gst.MapInfo, idx, range, and Gst.MapFlags parameters */
955 gst_buffer_type = pygobject_lookup_class (_gst_buffer_type);
956 if (!PyArg_ParseTuple (args, "O!OIii", gst_buffer_type, &py_buffer,
957 &py_mapinfo, &idx, &range, &flags))
960 /* Since Python does only support r/o or r/w it has to be changed to either */
961 flags = (flags & GST_MAP_WRITE) ? GST_MAP_READWRITE : GST_MAP_READ;
963 /* Extract GstBuffer from Gst.Buffer parameter */
964 buffer = GST_BUFFER (pygobject_get (py_buffer));
966 /* Map the buffer, fill out GstMapInfo */
967 mapinfo = g_new0 (GstMapInfo, 1);
968 ok = gst_buffer_map_range (buffer, idx, range, mapinfo, flags);
974 success = _remap (mapinfo, py_mapinfo);
976 gst_buffer_unmap (buffer, mapinfo);
982 Py_INCREF (Py_False);
987 _gst_buffer_override_map (PyObject * self, PyObject * args)
989 PyTypeObject *gst_buffer_type;
990 PyObject *py_buffer, *py_mapinfo, *success;
996 /* Look up Gst.Buffer, Gst.MapInfo, and Gst.MapFlags parameters */
997 gst_buffer_type = pygobject_lookup_class (_gst_buffer_type);
998 if (!PyArg_ParseTuple (args, "O!Oi", gst_buffer_type, &py_buffer, &py_mapinfo,
1000 PyErr_BadArgument ();
1004 /* Since Python does only support r/o or r/w it has to be changed to either */
1005 flags = (flags & GST_MAP_WRITE) ? GST_MAP_READWRITE : GST_MAP_READ;
1007 /* Extract GstBuffer from Gst.Buffer parameter */
1008 buffer = GST_BUFFER (pygobject_get (py_buffer));
1010 /* Map the buffer, fill out GstMapInfo */
1011 mapinfo = g_new0 (GstMapInfo, 1);
1012 ok = gst_buffer_map (buffer, mapinfo, flags);
1018 success = _remap (mapinfo, py_mapinfo);
1020 gst_buffer_unmap (buffer, mapinfo);
1026 Py_INCREF (Py_False);
1031 _gst_buffer_override_unmap (PyObject * self, PyObject * args)
1033 PyTypeObject *gst_buf_type;
1034 PyObject *py_buffer, *py_mapinfo, *success;
1036 GstMapInfo *mapinfo = NULL;
1038 /* Look up Gst.Buffer and Gst.Mapinfo parameters */
1039 gst_buf_type = pygobject_lookup_class (_gst_buffer_type);
1040 if (!PyArg_ParseTuple (args, "O!O", gst_buf_type, &py_buffer, &py_mapinfo)) {
1041 PyErr_BadArgument ();
1045 success = _unmap (&mapinfo, py_mapinfo);
1046 if (PyBool_Check (success) && mapinfo) {
1047 /* Extract GstBuffer from Gst.Buffer parameter */
1048 buffer = GST_BUFFER (pygobject_get (py_buffer));
1050 /* Unmap the buffer, using reconstructed GstMapInfo */
1051 gst_buffer_unmap (buffer, mapinfo);
1058 static PyMethodDef _gi_gst_functions[] = {
1059 {"trace", (PyCFunction) _wrap_gst_trace, METH_VARARGS,
1061 {"log", (PyCFunction) _wrap_gst_log, METH_VARARGS,
1063 {"debug", (PyCFunction) _wrap_gst_debug, METH_VARARGS,
1065 {"info", (PyCFunction) _wrap_gst_info, METH_VARARGS,
1067 {"warning", (PyCFunction) _wrap_gst_warning, METH_VARARGS,
1069 {"error", (PyCFunction) _wrap_gst_error, METH_VARARGS,
1071 {"fixme", (PyCFunction) _wrap_gst_fixme, METH_VARARGS,
1073 {"memdump", (PyCFunction) _wrap_gst_memdump, METH_VARARGS,
1075 {"buffer_override_map_range", (PyCFunction) _gst_buffer_override_map_range,
1078 {"buffer_override_map", (PyCFunction) _gst_buffer_override_map, METH_VARARGS,
1080 {"buffer_override_unmap", (PyCFunction) _gst_buffer_override_unmap,
1083 {"memory_override_map", (PyCFunction) _gst_memory_override_map, METH_VARARGS,
1085 {"memory_override_unmap", (PyCFunction) _gst_memory_override_unmap,
1088 {NULL, NULL, 0, NULL}
1091 PYGLIB_MODULE_START (_gi_gst, "_gi_gst")
1095 /* gst should have been initialized already */
1097 /* Initialize debugging category */
1098 GST_DEBUG_CATEGORY_INIT (pygst_debug, "pygst", 0,
1099 "GStreamer python bindings");
1100 GST_DEBUG_CATEGORY_INIT (python_debug, "python", GST_DEBUG_FG_GREEN,
1101 "python code using gst-python");
1103 pygobject_init (3, 0, 0);
1105 d = PyModule_GetDict (module);
1106 gi_gst_register_types (d);
1107 pyg_register_class_init (GST_TYPE_ELEMENT, _pygst_element_init);