Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / sys / v4l / v4lsrc_calls.c
1 /* GStreamer
2  *
3  * v4lsrc_calls.c: generic V4L source functions
4  *
5  * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32 #include <sys/mman.h>
33 #include <string.h>
34 #include <errno.h>
35 #include "v4lsrc_calls.h"
36 #include <sys/time.h>
37
38 /* number of buffers to be queued *at least* before syncing */
39 #define MIN_BUFFERS_QUEUED 2
40
41 /* On some systems MAP_FAILED seems to be missing */
42 #ifndef MAP_FAILED
43 #define MAP_FAILED ( (caddr_t) -1 )
44 #endif
45
46 GST_DEBUG_CATEGORY_EXTERN (v4l_debug);
47
48 #define GST_CAT_DEFAULT v4l_debug
49
50 #ifndef GST_DISABLE_GST_DEBUG
51 /* palette names */
52 static const char *v4l_palette_name[] = {
53   "",                           /* 0 */
54   "grayscale",                  /* VIDEO_PALETTE_GREY */
55   "Hi-420",                     /* VIDEO_PALETTE_HI420 */
56   "16-bit RGB (RGB-565)",       /* VIDEO_PALETTE_RB565 */
57   "24-bit RGB",                 /* VIDEO_PALETTE_RGB24 */
58   "32-bit RGB",                 /* VIDEO_PALETTE_RGB32 */
59   "15-bit RGB (RGB-555)",       /* VIDEO_PALETTE_RGB555 */
60   "YUV-4:2:2 (packed)",         /* VIDEO_PALETTE_YUV422 */
61   "YUYV",                       /* VIDEO_PALETTE_YUYV */
62   "UYVY",                       /* VIDEO_PALETTE_UYVY */
63   "YUV-4:2:0 (packed)",         /* VIDEO_PALETTE_YUV420 */
64   "YUV-4:1:1 (packed)",         /* VIDEO_PALETTE_YUV411 */
65   "Raw",                        /* VIDEO_PALETTE_RAW */
66   "YUV-4:2:2 (planar)",         /* VIDEO_PALETTE_YUV422P */
67   "YUV-4:1:1 (planar)",         /* VIDEO_PALETTE_YUV411P */
68   "YUV-4:2:0 (planar)/I420",    /* VIDEO_PALETTE_YUV420P */
69   "YUV-4:1:0 (planar)"          /* VIDEO_PALETTE_YUV410P */
70 };
71 #endif
72
73 /******************************************************
74  * gst_v4lsrc_queue_frame():
75  *   queue a frame for capturing
76  *   (ie. instruct the hardware to start capture)
77  *   Requires queue_state lock to be held!
78  * return value: TRUE on success, FALSE on error
79  ******************************************************/
80
81 static gboolean
82 gst_v4lsrc_queue_frame (GstV4lSrc * v4lsrc, gint num)
83 {
84   GST_LOG_OBJECT (v4lsrc, "queueing frame %d", num);
85
86   if (v4lsrc->frame_queue_state[num] != QUEUE_STATE_READY_FOR_QUEUE) {
87     return FALSE;
88   }
89
90   /* instruct the driver to prepare capture using buffer frame num */
91   v4lsrc->mmap.frame = num;
92   if (ioctl (GST_V4LELEMENT (v4lsrc)->video_fd,
93           VIDIOCMCAPTURE, &(v4lsrc->mmap)) < 0) {
94     GST_ELEMENT_ERROR (v4lsrc, RESOURCE, WRITE, (NULL),
95         ("Error queueing a buffer (%d): %s", num, g_strerror (errno)));
96     return FALSE;
97   }
98
99   v4lsrc->frame_queue_state[num] = QUEUE_STATE_QUEUED;
100   v4lsrc->num_queued++;
101
102   return TRUE;
103 }
104
105 /******************************************************
106  * gst_v4lsrc_hard_sync_frame(GstV4lSrc *v4lsrc,gint num)
107  *   sync a frame and set the timestamp correctly
108  *   Requires queue_state lock to be held
109  *****************************************************/
110
111 static gboolean
112 gst_v4lsrc_sync_frame (GstV4lSrc * v4lsrc, gint num)
113 {
114   GST_LOG_OBJECT (v4lsrc, "VIOIOCSYNC on frame %d", num);
115
116   if (v4lsrc->frame_queue_state[num] != QUEUE_STATE_QUEUED) {
117     return FALSE;
118   }
119
120   while (ioctl (GST_V4LELEMENT (v4lsrc)->video_fd, VIDIOCSYNC, &num) < 0) {
121     /* if the sync() got interrupted, we can retry */
122     if (errno != EINTR) {
123       v4lsrc->frame_queue_state[num] = QUEUE_STATE_ERROR;
124       GST_ELEMENT_ERROR (v4lsrc, RESOURCE, SYNC, (NULL), GST_ERROR_SYSTEM);
125       return FALSE;
126     }
127     GST_DEBUG_OBJECT (v4lsrc, "Sync got interrupted");
128   }
129   GST_LOG_OBJECT (v4lsrc, "VIOIOCSYNC on frame %d done", num);
130
131   v4lsrc->frame_queue_state[num] = QUEUE_STATE_SYNCED;
132   v4lsrc->num_queued--;
133
134   return TRUE;
135 }
136
137 /******************************************************
138  * gst_v4lsrc_set_capture():
139  *   set capture parameters, palette = VIDEO_PALETTE_*
140  * return value: TRUE on success, FALSE on error
141  ******************************************************/
142
143 gboolean
144 gst_v4lsrc_set_capture (GstV4lSrc * v4lsrc,
145     gint width, gint height, gint palette)
146 {
147   GST_DEBUG_OBJECT (v4lsrc,
148       "capture properties set to %dx%d, palette %d", width, height, palette);
149
150   v4lsrc->mmap.width = width;
151   v4lsrc->mmap.height = height;
152   v4lsrc->mmap.format = palette;
153
154   return TRUE;
155 }
156
157
158 /******************************************************
159  * gst_v4lsrc_capture_init():
160  *   initialize the capture system
161  * return value: TRUE on success, FALSE on error
162  ******************************************************/
163
164 gboolean
165 gst_v4lsrc_capture_init (GstV4lSrc * v4lsrc)
166 {
167   GST_DEBUG_OBJECT (v4lsrc, "initting capture subsystem");
168   GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
169   GST_V4L_CHECK_NOT_ACTIVE (GST_V4LELEMENT (v4lsrc));
170
171   /* request the mmap buffer info:
172    * total size of mmap buffer, number of frames, offsets of frames */
173   if (ioctl (GST_V4LELEMENT (v4lsrc)->video_fd, VIDIOCGMBUF,
174           &(v4lsrc->mbuf)) < 0) {
175     GST_ELEMENT_ERROR (v4lsrc, RESOURCE, READ, (NULL),
176         ("Error getting buffer information: %s", g_strerror (errno)));
177     return FALSE;
178   }
179
180   if (v4lsrc->mbuf.frames < MIN_BUFFERS_QUEUED) {
181     GST_ELEMENT_ERROR (v4lsrc, RESOURCE, READ, (NULL),
182         ("Not enough buffers. We got %d, we want at least %d",
183             v4lsrc->mbuf.frames, MIN_BUFFERS_QUEUED));
184     return FALSE;
185   }
186
187   GST_INFO_OBJECT (v4lsrc, "Got %d buffers (\'%s\') with total size %d KB",
188       v4lsrc->mbuf.frames, v4l_palette_name[v4lsrc->mmap.format],
189       v4lsrc->mbuf.size / (v4lsrc->mbuf.frames * 1024));
190
191   /* keep track of queued buffers */
192   v4lsrc->frame_queue_state = (gint8 *)
193       g_malloc (sizeof (gint8) * v4lsrc->mbuf.frames);
194
195   /* lock for the frame_state */
196   v4lsrc->mutex_queue_state = g_mutex_new ();
197   v4lsrc->cond_queue_state = g_cond_new ();
198
199   /* Map the buffers */
200   GST_V4LELEMENT (v4lsrc)->buffer = mmap (NULL, v4lsrc->mbuf.size,
201       PROT_READ | PROT_WRITE, MAP_SHARED, GST_V4LELEMENT (v4lsrc)->video_fd, 0);
202   if (GST_V4LELEMENT (v4lsrc)->buffer == MAP_FAILED) {
203     GST_ELEMENT_ERROR (v4lsrc, RESOURCE, OPEN_READ_WRITE, (NULL),
204         ("Error mapping video buffers: %s", g_strerror (errno)));
205     GST_V4LELEMENT (v4lsrc)->buffer = NULL;
206     return FALSE;
207   }
208
209   return TRUE;
210 }
211
212
213 /******************************************************
214  * gst_v4lsrc_capture_start():
215  *   start streaming capture
216  * return value: TRUE on success, FALSE on error
217  ******************************************************/
218
219 gboolean
220 gst_v4lsrc_capture_start (GstV4lSrc * v4lsrc)
221 {
222   int n;
223
224   GST_DEBUG_OBJECT (v4lsrc, "starting capture");
225   GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
226   GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lsrc));
227
228   g_mutex_lock (v4lsrc->mutex_queue_state);
229
230   v4lsrc->quit = FALSE;
231   v4lsrc->num_queued = 0;
232   v4lsrc->sync_frame = 0;
233   v4lsrc->queue_frame = 0;
234
235   /* set all buffers ready to queue, and queue captures to the device.
236    * This starts streaming capture */
237   for (n = 0; n < v4lsrc->mbuf.frames; n++) {
238     v4lsrc->frame_queue_state[n] = QUEUE_STATE_READY_FOR_QUEUE;
239     if (!gst_v4lsrc_queue_frame (v4lsrc, n)) {
240       g_mutex_unlock (v4lsrc->mutex_queue_state);
241       gst_v4lsrc_capture_stop (v4lsrc);
242       return FALSE;
243     }
244   }
245
246   v4lsrc->is_capturing = TRUE;
247   g_mutex_unlock (v4lsrc->mutex_queue_state);
248
249   return TRUE;
250 }
251
252
253 /******************************************************
254  * gst_v4lsrc_grab_frame():
255  *   capture one frame during streaming capture
256  * return value: TRUE on success, FALSE on error
257  ******************************************************/
258
259 gboolean
260 gst_v4lsrc_grab_frame (GstV4lSrc * v4lsrc, gint * num)
261 {
262   GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
263   GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lsrc));
264
265   GST_LOG_OBJECT (v4lsrc, "grabbing frame");
266
267   g_mutex_lock (v4lsrc->mutex_queue_state);
268
269   /* do we have enough frames? */
270   while (v4lsrc->num_queued < MIN_BUFFERS_QUEUED ||
271       v4lsrc->frame_queue_state[v4lsrc->queue_frame] ==
272       QUEUE_STATE_READY_FOR_QUEUE) {
273     while (v4lsrc->frame_queue_state[v4lsrc->queue_frame] !=
274         QUEUE_STATE_READY_FOR_QUEUE && !v4lsrc->quit) {
275       GST_DEBUG_OBJECT (v4lsrc,
276           "Waiting for frames to become available (queued %d < minimum %d)",
277           v4lsrc->num_queued, MIN_BUFFERS_QUEUED);
278       g_cond_wait (v4lsrc->cond_queue_state, v4lsrc->mutex_queue_state);
279     }
280     if (v4lsrc->quit) {
281       g_mutex_unlock (v4lsrc->mutex_queue_state);
282       return FALSE;
283     }
284     if (!gst_v4lsrc_queue_frame (v4lsrc, v4lsrc->queue_frame)) {
285       g_mutex_unlock (v4lsrc->mutex_queue_state);
286       return FALSE;
287     }
288     v4lsrc->queue_frame = (v4lsrc->queue_frame + 1) % v4lsrc->mbuf.frames;
289   }
290
291   /* syncing on the buffer grabs it */
292   *num = v4lsrc->sync_frame;
293   if (!gst_v4lsrc_sync_frame (v4lsrc, *num)) {
294     g_mutex_unlock (v4lsrc->mutex_queue_state);
295     return FALSE;
296   }
297   v4lsrc->sync_frame = (v4lsrc->sync_frame + 1) % v4lsrc->mbuf.frames;
298
299   g_mutex_unlock (v4lsrc->mutex_queue_state);
300
301   GST_LOG_OBJECT (v4lsrc, "grabbed frame %d", *num);
302
303   return TRUE;
304 }
305
306
307 /******************************************************
308  * gst_v4lsrc_get_buffer():
309  *   get the address of the given frame number in the mmap'd buffer
310  * return value: the buffer's address or NULL
311  ******************************************************/
312
313 guint8 *
314 gst_v4lsrc_get_buffer (GstV4lSrc * v4lsrc, gint num)
315 {
316   if (!GST_V4L_IS_ACTIVE (GST_V4LELEMENT (v4lsrc)) ||
317       !GST_V4L_IS_OPEN (GST_V4LELEMENT (v4lsrc)))
318     return NULL;
319
320   if (num < 0 || num >= v4lsrc->mbuf.frames)
321     return NULL;
322
323   return GST_V4LELEMENT (v4lsrc)->buffer + v4lsrc->mbuf.offsets[num];
324 }
325
326
327 /******************************************************
328  * gst_v4lsrc_requeue_frame():
329  *   re-queue a frame after we're done with the buffer
330  * return value: TRUE on success, FALSE on error
331  ******************************************************/
332
333 gboolean
334 gst_v4lsrc_requeue_frame (GstV4lSrc * v4lsrc, gint num)
335 {
336   GST_LOG_OBJECT (v4lsrc, "requeueing frame %d", num);
337   GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
338   GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lsrc));
339
340   /* mark frame as 'ready to requeue' */
341   g_mutex_lock (v4lsrc->mutex_queue_state);
342
343   if (v4lsrc->frame_queue_state[num] != QUEUE_STATE_SYNCED) {
344     GST_ELEMENT_ERROR (v4lsrc, RESOURCE, TOO_LAZY, (NULL),
345         ("Invalid state %d (expected %d), can't requeue",
346             v4lsrc->frame_queue_state[num], QUEUE_STATE_SYNCED));
347     return FALSE;
348   }
349
350   v4lsrc->frame_queue_state[num] = QUEUE_STATE_READY_FOR_QUEUE;
351
352   /* let an optional wait know */
353   g_cond_broadcast (v4lsrc->cond_queue_state);
354
355   g_mutex_unlock (v4lsrc->mutex_queue_state);
356
357   return TRUE;
358 }
359
360
361 /******************************************************
362  * gst_v4lsrc_capture_stop():
363  *   stop streaming capture
364  * return value: TRUE on success, FALSE on error
365  ******************************************************/
366
367 gboolean
368 gst_v4lsrc_capture_stop (GstV4lSrc * v4lsrc)
369 {
370   GST_DEBUG_OBJECT (v4lsrc, "stopping capture");
371   GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
372   GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lsrc));
373
374   g_mutex_lock (v4lsrc->mutex_queue_state);
375   v4lsrc->is_capturing = FALSE;
376
377   /* make an optional pending wait stop */
378   v4lsrc->quit = TRUE;
379   g_cond_broadcast (v4lsrc->cond_queue_state);
380
381   /* sync on remaining frames */
382   while (1) {
383     if (v4lsrc->frame_queue_state[v4lsrc->sync_frame] == QUEUE_STATE_QUEUED) {
384       gst_v4lsrc_sync_frame (v4lsrc, v4lsrc->sync_frame);
385       v4lsrc->sync_frame = (v4lsrc->sync_frame + 1) % v4lsrc->mbuf.frames;
386     } else {
387       break;
388     }
389   }
390
391   g_mutex_unlock (v4lsrc->mutex_queue_state);
392
393   return TRUE;
394 }
395
396
397 /******************************************************
398  * gst_v4lsrc_capture_deinit():
399  *   deinitialize the capture system
400  * return value: TRUE on success, FALSE on error
401  ******************************************************/
402
403 gboolean
404 gst_v4lsrc_capture_deinit (GstV4lSrc * v4lsrc)
405 {
406   GST_DEBUG_OBJECT (v4lsrc, "quitting capture subsystem");
407   GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
408   GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lsrc));
409
410   /* free buffer tracker */
411   g_mutex_free (v4lsrc->mutex_queue_state);
412   v4lsrc->mutex_queue_state = NULL;
413   g_cond_free (v4lsrc->cond_queue_state);
414   v4lsrc->cond_queue_state = NULL;
415   g_free (v4lsrc->frame_queue_state);
416   v4lsrc->frame_queue_state = NULL;
417
418   /* unmap the buffer */
419   if (munmap (GST_V4LELEMENT (v4lsrc)->buffer, v4lsrc->mbuf.size) == -1) {
420     GST_ELEMENT_ERROR (v4lsrc, RESOURCE, CLOSE, (NULL),
421         ("error munmap'ing capture buffer: %s", g_strerror (errno)));
422     return FALSE;
423   }
424   GST_V4LELEMENT (v4lsrc)->buffer = NULL;
425
426   return TRUE;
427 }
428
429 /******************************************************
430  * gst_v4lsrc_try_capture():
431  *   try out a capture on the device
432  *   This has to be done before initializing the
433  *   actual capture system, to make sure we don't
434  *   mess up anything. So we need to mini-mmap()
435  *   a buffer here, queue and sync on one buffer,
436  *   and unmap it.
437  *   This is ugly, yes, I know - but it's a major
438  *   design flaw of v4l1 that you don't know in
439  *   advance which formats will be supported...
440  *   This is better than "just assuming that it'll
441  *   work"...
442  * return value: TRUE on success, FALSE on error
443  ******************************************************/
444
445 gboolean
446 gst_v4lsrc_try_capture (GstV4lSrc * v4lsrc, gint width, gint height,
447     gint palette)
448 {
449   /* so, we need a buffer and some more stuff */
450   int frame = 0;
451   guint8 *buffer;
452   struct video_mbuf vmbuf;
453   struct video_mmap vmmap;
454
455   GST_DEBUG_OBJECT (v4lsrc, "try out %dx%d, palette format %d (%s)",
456       width, height, palette, v4l_palette_name[palette]);
457   GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
458   GST_V4L_CHECK_NOT_ACTIVE (GST_V4LELEMENT (v4lsrc));
459
460   /* let's start by requesting a buffer and mmap()'ing it */
461   if (ioctl (GST_V4LELEMENT (v4lsrc)->video_fd, VIDIOCGMBUF, &vmbuf) < 0) {
462     GST_ELEMENT_ERROR (v4lsrc, RESOURCE, READ, (NULL),
463         ("Error getting buffer information: %s", g_strerror (errno)));
464     return FALSE;
465   }
466   /* Map the buffers */
467   buffer = mmap (NULL, vmbuf.size, PROT_READ | PROT_WRITE,
468       MAP_SHARED, GST_V4LELEMENT (v4lsrc)->video_fd, 0);
469   if (buffer == MAP_FAILED) {
470     GST_ELEMENT_ERROR (v4lsrc, RESOURCE, OPEN_READ_WRITE, (NULL),
471         ("Error mapping our try-out buffer: %s", g_strerror (errno)));
472     return FALSE;
473   }
474
475   /* now that we have a buffer, let's try out our format */
476   vmmap.width = width;
477   vmmap.height = height;
478   vmmap.format = palette;
479   vmmap.frame = frame;
480   if (ioctl (GST_V4LELEMENT (v4lsrc)->video_fd, VIDIOCMCAPTURE, &vmmap) < 0) {
481     if (errno != EINVAL)        /* our format failed! */
482       GST_ERROR_OBJECT (v4lsrc,
483           "Error queueing our try-out buffer: %s", g_strerror (errno));
484     munmap (buffer, vmbuf.size);
485     return FALSE;
486   }
487
488   if (ioctl (GST_V4LELEMENT (v4lsrc)->video_fd, VIDIOCSYNC, &frame) < 0) {
489     GST_ELEMENT_ERROR (v4lsrc, RESOURCE, SYNC, (NULL), GST_ERROR_SYSTEM);
490     munmap (buffer, vmbuf.size);
491     return FALSE;
492   }
493
494   munmap (buffer, vmbuf.size);
495
496   /* if we got here, it worked! woohoo, the format is supported! */
497   return TRUE;
498 }
499
500 #ifndef GST_DISABLE_GST_DEBUG
501 const char *
502 gst_v4lsrc_palette_name (int i)
503 {
504   return v4l_palette_name[i];
505 }
506 #endif
507
508 gboolean
509 gst_v4lsrc_get_fps (GstV4lSrc * v4lsrc, gint * fps_n, gint * fps_d)
510 {
511   gint norm;
512   gint fps_index;
513   struct video_window *vwin = &GST_V4LELEMENT (v4lsrc)->vwin;
514
515   /* check if we have vwin window properties giving a framerate,
516    * as is done for webcams
517    * See http://www.smcc.demon.nl/webcam/api.html
518    * which is used for the Philips and qce-ga drivers */
519   fps_index = (vwin->flags >> 16) & 0x3F;       /* 6 bit index for framerate */
520
521   /* webcams have a non-zero fps_index */
522   if (fps_index != 0) {
523     /* index of 16 corresponds to 15 fps */
524     GST_DEBUG_OBJECT (v4lsrc, "device reports fps of %d/%d (%.4f)",
525         fps_index * 15, 16, fps_index * 15.0 / 16);
526
527     if (fps_n)
528       *fps_n = fps_index * 15;
529     if (fps_d)
530       *fps_d = 16;
531
532     return TRUE;
533   }
534
535   /* removed fps estimation code here */
536
537   /* if that failed ... */
538
539   if (!GST_V4L_IS_OPEN (GST_V4LELEMENT (v4lsrc)))
540     return FALSE;
541
542   if (!gst_v4l_get_chan_norm (GST_V4LELEMENT (v4lsrc), NULL, &norm))
543     return FALSE;
544
545   if (norm == VIDEO_MODE_NTSC) {
546     if (fps_n)
547       *fps_n = 30000;
548     if (fps_d)
549       *fps_d = 1001;
550   } else {
551     if (fps_n)
552       *fps_n = 25;
553     if (fps_d)
554       *fps_d = 1;
555   }
556
557   return TRUE;
558 }
559
560 /* get a list of possible framerates
561  * this is only done for webcams;
562  * other devices return NULL here.
563  * this function takes a LONG time to execute.
564  */
565 GValue *
566 gst_v4lsrc_get_fps_list (GstV4lSrc * v4lsrc)
567 {
568   gint fps_index;
569   struct video_window *vwin = &GST_V4LELEMENT (v4lsrc)->vwin;
570   GstV4lElement *v4lelement = GST_V4LELEMENT (v4lsrc);
571
572   /* check if we have vwin window properties giving a framerate,
573    * as is done for webcams
574    * See http://www.smcc.demon.nl/webcam/api.html
575    * which is used for the Philips and qce-ga drivers */
576   fps_index = (vwin->flags >> 16) & 0x3F;       /* 6 bit index for framerate */
577
578   /* webcams have a non-zero fps_index */
579   if (fps_index == 0) {
580     GST_DEBUG_OBJECT (v4lsrc, "fps_index is 0, no webcam");
581     return NULL;
582   }
583   GST_DEBUG_OBJECT (v4lsrc, "fps_index is %d, so webcam", fps_index);
584
585   {
586     int i;
587     GValue *list = NULL;
588     GValue value = { 0 };
589
590     /* webcam detected, so try all framerates and return a list */
591
592     list = g_new0 (GValue, 1);
593     g_value_init (list, GST_TYPE_LIST);
594
595     /* index of 16 corresponds to 15 fps */
596     GST_DEBUG_OBJECT (v4lsrc, "device reports fps of %d/%d (%.4f)",
597         fps_index * 15, 16, fps_index * 15.0 / 16);
598     for (i = 0; i < 63; ++i) {
599       /* set bits 16 to 21 to 0 */
600       vwin->flags &= (0x3F00 - 1);
601       /* set bits 16 to 21 to the index */
602       vwin->flags |= i << 16;
603       if (gst_v4l_set_window_properties (v4lelement)) {
604         /* setting it succeeded.  FIXME: get it and check. */
605         g_value_init (&value, GST_TYPE_FRACTION);
606         gst_value_set_fraction (&value, i * 15, 16);
607         gst_value_list_append_value (list, &value);
608         g_value_unset (&value);
609       }
610     }
611     /* FIXME: set back the original fps_index */
612     vwin->flags &= (0x3F00 - 1);
613     vwin->flags |= fps_index << 16;
614     gst_v4l_set_window_properties (v4lelement);
615     return list;
616   }
617 }
618
619 #define GST_TYPE_V4LSRC_BUFFER (gst_v4lsrc_buffer_get_type())
620 #define GST_IS_V4LSRC_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_V4LSRC_BUFFER))
621 #define GST_V4LSRC_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_V4LSRC_BUFFER, GstV4lSrcBuffer))
622
623 typedef struct _GstV4lSrcBuffer
624 {
625   GstBuffer buffer;
626
627   GstV4lSrc *v4lsrc;
628
629   gint num;
630 } GstV4lSrcBuffer;
631
632 static void gst_v4lsrc_buffer_class_init (gpointer g_class,
633     gpointer class_data);
634 static void gst_v4lsrc_buffer_init (GTypeInstance * instance, gpointer g_class);
635 static void gst_v4lsrc_buffer_finalize (GstV4lSrcBuffer * v4lsrc_buffer);
636
637 static GstBufferClass *v4lbuffer_parent_class = NULL;
638
639 static GType
640 gst_v4lsrc_buffer_get_type (void)
641 {
642   static GType _gst_v4lsrc_buffer_type;
643
644   if (G_UNLIKELY (_gst_v4lsrc_buffer_type == 0)) {
645     static const GTypeInfo v4lsrc_buffer_info = {
646       sizeof (GstBufferClass),
647       NULL,
648       NULL,
649       gst_v4lsrc_buffer_class_init,
650       NULL,
651       NULL,
652       sizeof (GstV4lSrcBuffer),
653       0,
654       gst_v4lsrc_buffer_init,
655       NULL
656     };
657     _gst_v4lsrc_buffer_type = g_type_register_static (GST_TYPE_BUFFER,
658         "GstV4lSrcBuffer", &v4lsrc_buffer_info, 0);
659   }
660   return _gst_v4lsrc_buffer_type;
661 }
662
663 static void
664 gst_v4lsrc_buffer_class_init (gpointer g_class, gpointer class_data)
665 {
666   GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
667
668   v4lbuffer_parent_class = g_type_class_peek_parent (g_class);
669
670   mini_object_class->finalize = (GstMiniObjectFinalizeFunction)
671       gst_v4lsrc_buffer_finalize;
672 }
673
674 static void
675 gst_v4lsrc_buffer_init (GTypeInstance * instance, gpointer g_class)
676 {
677
678 }
679
680 static void
681 gst_v4lsrc_buffer_finalize (GstV4lSrcBuffer * v4lsrc_buffer)
682 {
683   GstMiniObjectClass *miniobject_class;
684   GstV4lSrc *v4lsrc;
685   gint num;
686
687   v4lsrc = v4lsrc_buffer->v4lsrc;
688   num = v4lsrc_buffer->num;
689
690   GST_LOG_OBJECT (v4lsrc, "freeing buffer %p for frame %d", v4lsrc_buffer, num);
691
692   /* only requeue if we still have an mmap buffer */
693   if (GST_V4LELEMENT (v4lsrc)->buffer) {
694     GST_LOG_OBJECT (v4lsrc, "requeueing frame %d", num);
695     gst_v4lsrc_requeue_frame (v4lsrc, num);
696   }
697
698   gst_object_unref (v4lsrc);
699
700   miniobject_class = (GstMiniObjectClass *) v4lbuffer_parent_class;
701   miniobject_class->finalize (GST_MINI_OBJECT_CAST (v4lsrc_buffer));
702 }
703
704 /* Create a V4lSrc buffer from our mmap'd data area */
705 GstBuffer *
706 gst_v4lsrc_buffer_new (GstV4lSrc * v4lsrc, gint num)
707 {
708   GstClockTime duration, timestamp, latency;
709   GstBuffer *buf;
710   GstClock *clock;
711   gint fps_n, fps_d;
712
713   GST_DEBUG_OBJECT (v4lsrc, "creating buffer for frame %d", num);
714
715   if (!(gst_v4lsrc_get_fps (v4lsrc, &fps_n, &fps_d)))
716     return NULL;
717
718   buf = (GstBuffer *) gst_mini_object_new (GST_TYPE_V4LSRC_BUFFER);
719
720   GST_V4LSRC_BUFFER (buf)->num = num;
721   GST_V4LSRC_BUFFER (buf)->v4lsrc = gst_object_ref (v4lsrc);
722
723   GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_READONLY);
724   GST_BUFFER_DATA (buf) = gst_v4lsrc_get_buffer (v4lsrc, num);
725   GST_BUFFER_SIZE (buf) = v4lsrc->buffer_size;
726   GST_BUFFER_OFFSET (buf) = v4lsrc->offset++;
727   GST_BUFFER_OFFSET_END (buf) = v4lsrc->offset;
728
729   /* timestamps, LOCK to get clock and base time. */
730   GST_OBJECT_LOCK (v4lsrc);
731   if ((clock = GST_ELEMENT_CLOCK (v4lsrc))) {
732     /* we have a clock, get base time and ref clock */
733     timestamp = GST_ELEMENT_CAST (v4lsrc)->base_time;
734     gst_object_ref (clock);
735   } else {
736     /* no clock, can't set timestamps */
737     timestamp = GST_CLOCK_TIME_NONE;
738   }
739   GST_OBJECT_UNLOCK (v4lsrc);
740
741   duration =
742       gst_util_uint64_scale_int (GST_SECOND, fps_d * v4lsrc->offset, fps_n) -
743       gst_util_uint64_scale_int (GST_SECOND, fps_d * (v4lsrc->offset - 1),
744       fps_n);
745
746   latency = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
747
748   if (clock) {
749     /* the time now is the time of the clock minus the base time */
750     timestamp = gst_clock_get_time (clock) - timestamp;
751     gst_object_unref (clock);
752
753     /* adjust timestamp for frame latency (we assume we have a framerate) */
754     if (timestamp > latency)
755       timestamp -= latency;
756     else
757       timestamp = 0;
758   }
759
760   GST_BUFFER_TIMESTAMP (buf) = timestamp;
761   GST_BUFFER_DURATION (buf) = duration;
762
763   return buf;
764 }