rtspsrc: make jitterbuffer drop-on-latency available (fix #682055)
[platform/upstream/gstreamer.git] / ext / gdk_pixbuf / gst_loader.c
1 /*
2  * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3  *
4  * gst_loader.c: Load GStreamer videos as gdkpixbufs
5  * 
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.
10  *
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.
15  *
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "gstgdkanimation.h"
27 #include <gst/gstinfo.h>
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32
33
34 typedef struct
35 {
36   /* stuff gdk throws at us and we're supposed to keep */
37   GdkPixbufModuleSizeFunc size_func;
38   GdkPixbufModulePreparedFunc prepared_func;
39   GdkPixbufModuleUpdatedFunc updated_func;
40   gpointer user_data;
41   /* our own stuff - we're much better at keeping fields small :p */
42   GstGdkAnimation *ani;
43   gboolean initialized;
44 }
45 GstLoaderContext;
46
47 GST_DEBUG_CATEGORY_STATIC (gst_loader_debug);
48 #define GST_CAT_DEFAULT gst_loader_debug
49
50 static gboolean
51 gst_loader_init (GError ** error)
52 {
53   static gboolean inited = FALSE;
54
55   if (inited)
56     return TRUE;
57
58   if (!g_thread_supported ()) {
59     g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
60         "The GStreamer loader requires threading support.");
61     return FALSE;
62   }
63
64   if (!gst_init_check (0, NULL)) {
65     g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
66         "GStreamer could not be initialized.");
67     return FALSE;
68   }
69
70   inited = TRUE;
71   GST_DEBUG_CATEGORY_INIT (gst_loader_debug, "gstloader", 0,
72       "entry point debugging for the GStreamer gdk pixbuf loader");
73   return TRUE;
74 }
75
76 static gpointer
77 gst_loader_begin_load (GdkPixbufModuleSizeFunc size_func,
78     GdkPixbufModulePreparedFunc prepared_func,
79     GdkPixbufModuleUpdatedFunc updated_func, gpointer user_data,
80     GError ** error)
81 {
82   GstLoaderContext *context;
83
84   if (!gst_loader_init (error))
85     return NULL;
86
87   context = g_new (GstLoaderContext, 1);
88   context->size_func = size_func;
89   context->prepared_func = prepared_func;
90   context->updated_func = updated_func;
91   context->user_data = user_data;
92   context->ani = gst_gdk_animation_new (error);
93   context->initialized = FALSE;
94
95   if (!context->ani) {
96     GST_WARNING ("creating animation failed");
97     g_free (context);
98     return NULL;
99   }
100   context->ani->temp_fd =
101       g_file_open_tmp (NULL, &context->ani->temp_location, error);
102   if (context->ani->temp_fd == 0) {
103     g_object_unref (context->ani);
104     g_free (context);
105     return NULL;
106   }
107
108   GST_LOG_OBJECT (context->ani, "begin loading");
109   return context;
110 }
111
112 static gboolean
113 gst_loader_load_increment (gpointer context_pointer, const guchar * buf,
114     guint size, GError ** error)
115 {
116   GdkPixbufAnimationIter *iter;
117   GstLoaderContext *context = (GstLoaderContext *) context_pointer;
118
119   GST_LOG_OBJECT (context->ani, "load increment: %u bytes", size);
120   gst_gdk_animation_add_data (context->ani, buf, size);
121   if (!context->initialized
122       && (iter =
123           gdk_pixbuf_animation_get_iter (GDK_PIXBUF_ANIMATION (context->ani),
124               NULL)) != NULL) {
125     int width =
126         gdk_pixbuf_animation_get_width (GDK_PIXBUF_ANIMATION (context->ani));
127     int height =
128         gdk_pixbuf_animation_get_height (GDK_PIXBUF_ANIMATION (context->ani));
129     GdkPixbuf *pixbuf =
130         gdk_pixbuf_animation_get_static_image (GDK_PIXBUF_ANIMATION (context->
131             ani));
132
133     g_object_unref (iter);
134     GST_LOG_OBJECT (context->ani, "initializing loader");
135     if (context->size_func) {
136       GST_LOG_OBJECT (context->ani, "calling size_func %p", context->size_func);
137       context->size_func (&width, &height, context->user_data);
138     }
139
140     if (context->prepared_func) {
141       GST_LOG_OBJECT (context->ani, "calling prepared_func %p",
142           context->prepared_func);
143       context->prepared_func (pixbuf, GDK_PIXBUF_ANIMATION (context->ani),
144           context->user_data);
145     }
146
147     context->initialized = TRUE;
148   }
149
150   return TRUE;
151 }
152
153 static gboolean
154 gst_loader_stop_load (gpointer context_pointer, GError ** error)
155 {
156   GstLoaderContext *context = (GstLoaderContext *) context_pointer;
157
158   GST_LOG_OBJECT (context->ani, "stop loading");
159   gst_gdk_animation_done_adding (context->ani);
160   g_object_unref (context->ani);
161   g_free (context);
162
163   return TRUE;
164 }
165
166 static GdkPixbufAnimation *
167 gst_loader_load_animation (FILE * f, GError ** error)
168 {
169   gchar *filename;
170   GstGdkAnimation *ani;
171   GdkPixbufAnimationIter *iter = NULL;
172
173   if (!gst_loader_init (error))
174     return NULL;
175
176   GST_LOG ("load_animation");
177   ani = gst_gdk_animation_new (error);
178   if (!ani)
179     return NULL;
180
181   filename = g_strdup_printf ("/proc/self/fd/%d", fileno (f));
182   ani->temp_fd = open (filename, 0);
183   if (ani->temp_fd >= 0) {
184     iter = gdk_pixbuf_animation_get_iter (GDK_PIXBUF_ANIMATION (ani), NULL);
185   } else {
186     GST_DEBUG ("open (\"%s\", 0) failed", filename);
187   }
188   g_free (filename);
189   if (iter == NULL) {
190     g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
191         "could not create an image");
192     g_object_unref (ani);
193     GST_INFO ("could not create an image");
194     return NULL;
195   }
196   g_object_unref (iter);
197   GST_LOG_OBJECT (ani, "load_animation succeeded");
198   return GDK_PIXBUF_ANIMATION (ani);
199 }
200 static GdkPixbuf *
201 gst_loader_load (FILE * f, GError ** error)
202 {
203   GdkPixbufAnimation *ani;
204   GdkPixbuf *pixbuf;
205
206   ani = gst_loader_load_animation (f, error);
207   if (ani == NULL)
208     return NULL;
209
210   pixbuf = gdk_pixbuf_animation_get_static_image (ani);
211   if (!pixbuf) {
212     GST_ERROR_OBJECT (ani, "Could not get an image in _pixbuf_load");
213     g_object_unref (ani);
214     g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
215         "Could not get an image from file.");
216     return NULL;
217   }
218
219   g_object_ref (pixbuf);
220   g_object_unref (ani);
221
222   return pixbuf;
223 }
224
225 void
226 fill_vtable (GdkPixbufModule * module)
227 {
228   module->load = gst_loader_load;
229   module->begin_load = gst_loader_begin_load;
230   module->load_increment = gst_loader_load_increment;
231   module->stop_load = gst_loader_stop_load;
232   module->load_animation = gst_loader_load_animation;
233 }
234
235 void
236 fill_info (GdkPixbufFormat * info)
237 {
238   static GdkPixbufModulePattern signature[] = {
239     /* AVI */
240     {"RIFF    AVI ", "    xxxx    ", 100},
241     /* MPEG 1 */
242     {"xx\001\272", "zz  ", 100},
243     /* MPEG 2 */
244     {"xx\001\263", "zz  ", 100},
245     /* Quicktime */
246     {"    wide", "xxxx    ", 80},
247     {"    moov", "xxxx    ", 80},
248     {"    mdat", "xxxx    ", 80},
249     {"    pnot", "xxxx    ", 80},
250     {"    PICT", "xxxx    ", 80},
251     {"    free", "xxxx    ", 80},
252     /* ASF */
253     {"\060\046\262\165\216\146\317\021\246\331 \252 \142\316\154",
254         "          z z   ", 100},
255     {NULL, NULL, 0}
256   };
257
258   static gchar *mime_types[] = {
259     "video/avi", "video/x-avi", "video/x-msvideo",
260     "video/mpeg",
261     "video/quicktime",
262     "video/x-ms-asf",
263     NULL
264   };
265
266   static gchar *extensions[] = {
267     "avi",
268     "mpeg", "mpe", "mpg",
269     "mov",
270     "asf", "wmv",
271     NULL
272   };
273
274   info->name = "GStreamer";
275   info->signature = signature;
276   info->description = "GStreamer supported video";
277   info->mime_types = mime_types;
278   info->extensions = extensions;
279   info->flags = 0;
280 }