1
/* -*- Mode: C; ; c-file-style: "k&r"; c-basic-offset: 4 -*- */
3
* Copyright (C) 2002 David I. Lehn
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Library General Public
7
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Library General Public License for more details.
15
* You should have received a copy of the GNU Library General Public
16
* License along with this library; if not, write to the
17
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
* Boston, MA 02111-1307, USA.
20
* Author: David I. Lehn <dlehn@users.sourceforge.net>
27
#include "pygstminiobject.h"
28
#include "pygstexception.h"
32
/* include this first, before NO_IMPORT_PYGOBJECT is defined */
33
#include <pygobject.h>
35
#include <gst/gstversion.h>
37
void pygst_register_classes (PyObject *d);
38
void pygst_add_constants(PyObject *module, const gchar *strip_prefix);
39
void _pygst_register_boxed_types(PyObject *moddict);
41
extern PyMethodDef pygst_functions[];
43
GST_DEBUG_CATEGORY (pygst_debug); /* for bindings code */
44
GST_DEBUG_CATEGORY (python_debug); /* for python code */
46
/* copied from pygtk to register GType */
47
#define REGISTER_TYPE(d, type, name) \
48
type.ob_type = &PyType_Type; \
49
type.tp_alloc = PyType_GenericAlloc; \
50
type.tp_new = PyType_GenericNew; \
51
if (PyType_Ready(&type)) \
53
PyDict_SetItemString(d, name, (PyObject *)&type);
55
#define REGISTER_GTYPE(d, type, name, gtype) \
56
REGISTER_TYPE(d, type, name); \
57
PyDict_SetItemString(type.tp_dict, "__gtype__", \
58
o=pyg_type_wrapper_new(gtype)); \
62
/* This is a timeout that gets added to the mainloop to handle SIGINT (Ctrl-C)
63
* Other signals get handled at some other point where transition from
64
* C -> Python is being made.
67
python_do_pending_calls(gpointer data)
69
PyGILState_STATE state;
71
if (PyOS_InterruptOccurred()) {
72
state = pyg_gil_state_ensure();
73
PyErr_SetNone(PyExc_KeyboardInterrupt);
74
pyg_gil_state_release(state);
82
pygstminiobject_from_gvalue(const GValue *value)
84
GstMiniObject *miniobj;
86
if ((miniobj = gst_value_get_mini_object (value)) == NULL)
88
return pygstminiobject_new(miniobj);
92
pygstminiobject_to_gvalue(GValue *value, PyObject *obj)
94
PyGstMiniObject *self = (PyGstMiniObject*) obj;
96
gst_value_set_mini_object(value, self->obj);
104
PyObject *av, *tuple;
106
guint major, minor, micro, nano;
108
GError *error = NULL;
112
/* pull in arguments */
113
av = PySys_GetObject ("argv");
115
argc = PyList_Size (av);
116
argv = g_new (char *, argc);
117
for (i = 0; i < argc; i++)
118
argv[i] = g_strdup (PyString_AsString (PyList_GetItem (av, i)));
120
/* gst_init_check does not like argc == 0 */
122
argv = g_new (char *, argc);
123
argv[0] = g_strdup("");
125
if (!gst_init_check (&argc, &argv, &error)) {
129
for (i = 0; i < argc; i++)
133
errstr = g_strdup_printf ("can't initialize module gst: %s",
134
GST_STR_NULL (error->message));
135
PyErr_SetString (PyExc_RuntimeError, errstr);
137
g_error_free (error);
138
setlocale(LC_NUMERIC, "C");
142
setlocale(LC_NUMERIC, "C");
144
PySys_SetArgv (argc, argv);
145
for (i = 0; i < argc; i++)
150
/* Initialize debugging category */
151
GST_DEBUG_CATEGORY_INIT (pygst_debug, "pygst", 0, "GStreamer python bindings");
152
GST_DEBUG_CATEGORY_INIT (python_debug, "python",
153
GST_DEBUG_FG_GREEN, "python code using gst-python");
155
/* _pygst_register_boxed_types (NULL); */
156
pygobject_register_sinkfunc(GST_TYPE_OBJECT, pygstobject_sink);
158
m = Py_InitModule ("_gst", pygst_functions);
159
d = PyModule_GetDict (m);
162
gst_version(&major, &minor, µ, &nano);
163
tuple = Py_BuildValue("(iii)", major, minor, micro);
164
PyDict_SetItemString(d, "gst_version", tuple);
167
/* gst-python version */
168
tuple = Py_BuildValue ("(iii)", PYGST_MAJOR_VERSION, PYGST_MINOR_VERSION,
169
PYGST_MICRO_VERSION);
170
PyDict_SetItemString(d, "pygst_version", tuple);
174
PyModule_AddIntConstant(m, "SECOND", GST_SECOND);
175
PyModule_AddIntConstant(m, "MSECOND", GST_MSECOND);
176
PyModule_AddIntConstant(m, "NSECOND", GST_NSECOND);
178
PyModule_AddObject(m, "CLOCK_TIME_NONE", PyLong_FromUnsignedLongLong(GST_CLOCK_TIME_NONE));
180
pygst_exceptions_register_classes (d);
182
REGISTER_TYPE(d, PyGstIterator_Type, "Iterator");
185
pygstminiobject_register_class(d, "GstMiniObject", GST_TYPE_MINI_OBJECT,
186
&PyGstMiniObject_Type, NULL);
187
pyg_register_boxed_custom(GST_TYPE_MINI_OBJECT,
188
pygstminiobject_from_gvalue,
189
pygstminiobject_to_gvalue);
191
pygst_register_classes (d);
192
pygst_add_constants (m, "GST_");
194
/* make our types available */
195
PyModule_AddObject (m, "TYPE_ELEMENT_FACTORY",
196
pyg_type_wrapper_new(GST_TYPE_ELEMENT_FACTORY));
197
PyModule_AddObject (m, "TYPE_INDEX_FACTORY",
198
pyg_type_wrapper_new(GST_TYPE_INDEX_FACTORY));
199
PyModule_AddObject (m, "TYPE_TYPE_FIND_FACTORY",
200
pyg_type_wrapper_new(GST_TYPE_TYPE_FIND_FACTORY));
202
/* GStreamer core tags */
203
PyModule_AddStringConstant (m, "TAG_TITLE", GST_TAG_TITLE);
204
PyModule_AddStringConstant (m, "TAG_ARTIST", GST_TAG_ARTIST);
205
PyModule_AddStringConstant (m, "TAG_ALBUM", GST_TAG_ALBUM);
206
PyModule_AddStringConstant (m, "TAG_DATE", GST_TAG_DATE);
207
PyModule_AddStringConstant (m, "TAG_GENRE", GST_TAG_GENRE);
208
PyModule_AddStringConstant (m, "TAG_COMMENT", GST_TAG_COMMENT);
209
PyModule_AddStringConstant (m, "TAG_TRACK_NUMBER", GST_TAG_TRACK_NUMBER);
210
PyModule_AddStringConstant (m, "TAG_TRACK_COUNT", GST_TAG_TRACK_COUNT);
211
PyModule_AddStringConstant (m, "TAG_ALBUM_VOLUME_NUMBER", GST_TAG_ALBUM_VOLUME_NUMBER);
212
PyModule_AddStringConstant (m, "TAG_ALBUM_VOLUME_COUNT", GST_TAG_ALBUM_VOLUME_COUNT);
213
PyModule_AddStringConstant (m, "TAG_LOCATION", GST_TAG_LOCATION);
214
PyModule_AddStringConstant (m, "TAG_DESCRIPTION", GST_TAG_DESCRIPTION);
215
PyModule_AddStringConstant (m, "TAG_VERSION", GST_TAG_VERSION);
216
PyModule_AddStringConstant (m, "TAG_ISRC", GST_TAG_ISRC);
217
PyModule_AddStringConstant (m, "TAG_ORGANIZATION", GST_TAG_ORGANIZATION);
218
PyModule_AddStringConstant (m, "TAG_COPYRIGHT", GST_TAG_COPYRIGHT);
219
PyModule_AddStringConstant (m, "TAG_CONTACT", GST_TAG_CONTACT);
220
PyModule_AddStringConstant (m, "TAG_LICENSE", GST_TAG_LICENSE);
221
PyModule_AddStringConstant (m, "TAG_PERFORMER", GST_TAG_PERFORMER);
222
PyModule_AddStringConstant (m, "TAG_DURATION", GST_TAG_DURATION);
223
PyModule_AddStringConstant (m, "TAG_CODEC", GST_TAG_CODEC);
224
PyModule_AddStringConstant (m, "TAG_VIDEO_CODEC", GST_TAG_VIDEO_CODEC);
225
PyModule_AddStringConstant (m, "TAG_AUDIO_CODEC", GST_TAG_AUDIO_CODEC);
226
PyModule_AddStringConstant (m, "TAG_BITRATE", GST_TAG_BITRATE);
227
PyModule_AddStringConstant (m, "TAG_NOMINAL_BITRATE", GST_TAG_NOMINAL_BITRATE);
228
PyModule_AddStringConstant (m, "TAG_MINIMUM_BITRATE", GST_TAG_MINIMUM_BITRATE);
229
PyModule_AddStringConstant (m, "TAG_MAXIMUM_BITRATE", GST_TAG_MAXIMUM_BITRATE);
230
PyModule_AddStringConstant (m, "TAG_SERIAL", GST_TAG_SERIAL);
231
PyModule_AddStringConstant (m, "TAG_ENCODER", GST_TAG_ENCODER);
232
PyModule_AddStringConstant (m, "TAG_ENCODER_VERSION", GST_TAG_ENCODER_VERSION);
233
PyModule_AddStringConstant (m, "TAG_TRACK_GAIN", GST_TAG_TRACK_GAIN);
234
PyModule_AddStringConstant (m, "TAG_TRACK_PEAK", GST_TAG_TRACK_PEAK);
235
PyModule_AddStringConstant (m, "TAG_ALBUM_GAIN", GST_TAG_ALBUM_GAIN);
236
PyModule_AddStringConstant (m, "TAG_ALBUM_PEAK", GST_TAG_ALBUM_PEAK);
237
PyModule_AddStringConstant (m, "TAG_LANGUAGE_CODE", GST_TAG_LANGUAGE_CODE);
239
g_timeout_add_full (0, 100, python_do_pending_calls, NULL, NULL);
243
if (PyErr_Occurred ()) {
244
Py_FatalError ("can't initialize module gst");