~ubuntu-branches/ubuntu/precise/gst0.10-python/precise

« back to all changes in this revision

Viewing changes to gst/gstmodule.c

  • Committer: Bazaar Package Importer
  • Author(s): Loic Minier
  • Date: 2006-06-25 19:37:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060625193745-9yeg0wq56r24n57x
Tags: upstream-0.10.4
ImportĀ upstreamĀ versionĀ 0.10.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; ; c-file-style: "k&r"; c-basic-offset: 4 -*- */
 
2
/* gst-python
 
3
 * Copyright (C) 2002 David I. Lehn
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
19
 * 
 
20
 * Author: David I. Lehn <dlehn@users.sourceforge.net>
 
21
 */
 
22
 
 
23
#ifdef HAVE_CONFIG_H
 
24
#include "config.h"
 
25
#endif
 
26
 
 
27
#include "pygstminiobject.h"
 
28
#include "pygstexception.h"
 
29
 
 
30
#include <locale.h>
 
31
 
 
32
/* include this first, before NO_IMPORT_PYGOBJECT is defined */
 
33
#include <pygobject.h>
 
34
#include <gst/gst.h>
 
35
#include <gst/gstversion.h>
 
36
 
 
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);
 
40
                
 
41
extern PyMethodDef pygst_functions[];
 
42
 
 
43
GST_DEBUG_CATEGORY (pygst_debug);  /* for bindings code */
 
44
GST_DEBUG_CATEGORY (python_debug); /* for python code */
 
45
 
 
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)) \
 
52
        return; \
 
53
    PyDict_SetItemString(d, name, (PyObject *)&type);
 
54
 
 
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)); \
 
59
    Py_DECREF(o);
 
60
 
 
61
 
 
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.
 
65
 */
 
66
static gboolean
 
67
python_do_pending_calls(gpointer data)
 
68
{
 
69
    PyGILState_STATE state;
 
70
 
 
71
    if (PyOS_InterruptOccurred()) {
 
72
         state = pyg_gil_state_ensure();
 
73
         PyErr_SetNone(PyExc_KeyboardInterrupt);
 
74
         pyg_gil_state_release(state);
 
75
    }
 
76
 
 
77
    return TRUE;
 
78
}
 
79
 
 
80
 
 
81
static PyObject*
 
82
pygstminiobject_from_gvalue(const GValue *value)
 
83
{
 
84
     GstMiniObject      *miniobj;
 
85
 
 
86
     if ((miniobj = gst_value_get_mini_object (value)) == NULL)
 
87
          return NULL;
 
88
     return pygstminiobject_new(miniobj);
 
89
}
 
90
 
 
91
static int
 
92
pygstminiobject_to_gvalue(GValue *value, PyObject *obj)
 
93
{
 
94
     PyGstMiniObject    *self = (PyGstMiniObject*) obj;
 
95
 
 
96
     gst_value_set_mini_object(value, self->obj);
 
97
     return 0;
 
98
}
 
99
 
 
100
DL_EXPORT(void)
 
101
init_gst (void)
 
102
{
 
103
     PyObject *m, *d;
 
104
     PyObject *av, *tuple;
 
105
     int argc, i;
 
106
     guint major, minor, micro, nano;
 
107
     char **argv;
 
108
     GError     *error = NULL;
 
109
 
 
110
     init_pygobject ();
 
111
 
 
112
     /* pull in arguments */
 
113
     av = PySys_GetObject ("argv");
 
114
     if (av != NULL) {
 
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)));
 
119
     } else {
 
120
          /* gst_init_check does not like argc == 0 */
 
121
          argc = 1;
 
122
          argv = g_new (char *, argc);
 
123
          argv[0] = g_strdup("");
 
124
     }
 
125
     if (!gst_init_check (&argc, &argv, &error)) {
 
126
          gchar *errstr;
 
127
          
 
128
          if (argv != NULL) {
 
129
               for (i = 0; i < argc; i++)
 
130
                    g_free (argv[i]);
 
131
               g_free (argv);
 
132
          }
 
133
          errstr = g_strdup_printf ("can't initialize module gst: %s",
 
134
              GST_STR_NULL (error->message));
 
135
          PyErr_SetString (PyExc_RuntimeError, errstr);
 
136
          g_free (errstr);
 
137
          g_error_free (error);
 
138
          setlocale(LC_NUMERIC, "C");
 
139
          return;
 
140
     }
 
141
     
 
142
     setlocale(LC_NUMERIC, "C");
 
143
     if (argv != NULL) {
 
144
          PySys_SetArgv (argc, argv);
 
145
          for (i = 0; i < argc; i++)
 
146
               g_free (argv[i]);
 
147
          g_free (argv);
 
148
     }
 
149
 
 
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");
 
154
 
 
155
/*      _pygst_register_boxed_types (NULL); */
 
156
     pygobject_register_sinkfunc(GST_TYPE_OBJECT, pygstobject_sink);
 
157
 
 
158
     m = Py_InitModule ("_gst", pygst_functions);
 
159
     d = PyModule_GetDict (m);
 
160
 
 
161
     /* gst version */
 
162
     gst_version(&major, &minor, &micro, &nano);
 
163
     tuple = Py_BuildValue("(iii)", major, minor, micro);
 
164
     PyDict_SetItemString(d, "gst_version", tuple);    
 
165
     Py_DECREF(tuple);
 
166
     
 
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);
 
171
     Py_DECREF(tuple);
 
172
 
 
173
     /* clock stuff */
 
174
     PyModule_AddIntConstant(m, "SECOND", GST_SECOND);
 
175
     PyModule_AddIntConstant(m, "MSECOND", GST_MSECOND);
 
176
     PyModule_AddIntConstant(m, "NSECOND", GST_NSECOND);
 
177
 
 
178
     PyModule_AddObject(m, "CLOCK_TIME_NONE", PyLong_FromUnsignedLongLong(GST_CLOCK_TIME_NONE));
 
179
 
 
180
     pygst_exceptions_register_classes (d);
 
181
     
 
182
     REGISTER_TYPE(d, PyGstIterator_Type, "Iterator");
 
183
 
 
184
 
 
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);
 
190
     
 
191
     pygst_register_classes (d);
 
192
     pygst_add_constants (m, "GST_");
 
193
 
 
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));
 
201
 
 
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);
 
238
 
 
239
     g_timeout_add_full (0, 100, python_do_pending_calls, NULL, NULL);
 
240
 
 
241
     atexit(gst_deinit);
 
242
     
 
243
     if (PyErr_Occurred ()) {
 
244
          Py_FatalError ("can't initialize module gst");
 
245
     }
 
246
}