3 * v4lsrc_calls.c: generic V4L source functions
5 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
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.
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.
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.
28 #include <sys/types.h>
31 #include <sys/ioctl.h>
35 #include "v4lsrc_calls.h"
38 /* number of buffers to be queued *at least* before syncing */
39 #define MIN_BUFFERS_QUEUED 2
41 /* On some systems MAP_FAILED seems to be missing */
43 #define MAP_FAILED ( (caddr_t) -1 )
46 GST_DEBUG_CATEGORY_EXTERN (v4l_debug);
48 #define GST_CAT_DEFAULT v4l_debug
50 #ifndef GST_DISABLE_GST_DEBUG
52 static const char *v4l_palette_name[] = {
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 */
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 ******************************************************/
82 gst_v4lsrc_queue_frame (GstV4lSrc * v4lsrc, gint num)
84 GST_LOG_OBJECT (v4lsrc, "queueing frame %d", num);
86 if (v4lsrc->frame_queue_state[num] != QUEUE_STATE_READY_FOR_QUEUE) {
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)));
99 v4lsrc->frame_queue_state[num] = QUEUE_STATE_QUEUED;
100 v4lsrc->num_queued++;
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 *****************************************************/
112 gst_v4lsrc_sync_frame (GstV4lSrc * v4lsrc, gint num)
114 GST_LOG_OBJECT (v4lsrc, "VIOIOCSYNC on frame %d", num);
116 if (v4lsrc->frame_queue_state[num] != QUEUE_STATE_QUEUED) {
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);
127 GST_DEBUG_OBJECT (v4lsrc, "Sync got interrupted");
129 GST_LOG_OBJECT (v4lsrc, "VIOIOCSYNC on frame %d done", num);
131 v4lsrc->frame_queue_state[num] = QUEUE_STATE_SYNCED;
132 v4lsrc->num_queued--;
137 /******************************************************
138 * gst_v4lsrc_set_capture():
139 * set capture parameters, palette = VIDEO_PALETTE_*
140 * return value: TRUE on success, FALSE on error
141 ******************************************************/
144 gst_v4lsrc_set_capture (GstV4lSrc * v4lsrc,
145 gint width, gint height, gint palette)
147 GST_DEBUG_OBJECT (v4lsrc,
148 "capture properties set to %dx%d, palette %d", width, height, palette);
150 v4lsrc->mmap.width = width;
151 v4lsrc->mmap.height = height;
152 v4lsrc->mmap.format = palette;
158 /******************************************************
159 * gst_v4lsrc_capture_init():
160 * initialize the capture system
161 * return value: TRUE on success, FALSE on error
162 ******************************************************/
165 gst_v4lsrc_capture_init (GstV4lSrc * v4lsrc)
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));
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)));
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));
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));
191 /* keep track of queued buffers */
192 v4lsrc->frame_queue_state = (gint8 *)
193 g_malloc (sizeof (gint8) * v4lsrc->mbuf.frames);
195 /* lock for the frame_state */
196 v4lsrc->mutex_queue_state = g_mutex_new ();
197 v4lsrc->cond_queue_state = g_cond_new ();
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;
213 /******************************************************
214 * gst_v4lsrc_capture_start():
215 * start streaming capture
216 * return value: TRUE on success, FALSE on error
217 ******************************************************/
220 gst_v4lsrc_capture_start (GstV4lSrc * v4lsrc)
224 GST_DEBUG_OBJECT (v4lsrc, "starting capture");
225 GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
226 GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lsrc));
228 g_mutex_lock (v4lsrc->mutex_queue_state);
230 v4lsrc->quit = FALSE;
231 v4lsrc->num_queued = 0;
232 v4lsrc->sync_frame = 0;
233 v4lsrc->queue_frame = 0;
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);
246 v4lsrc->is_capturing = TRUE;
247 g_mutex_unlock (v4lsrc->mutex_queue_state);
253 /******************************************************
254 * gst_v4lsrc_grab_frame():
255 * capture one frame during streaming capture
256 * return value: TRUE on success, FALSE on error
257 ******************************************************/
260 gst_v4lsrc_grab_frame (GstV4lSrc * v4lsrc, gint * num)
262 GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
263 GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lsrc));
265 GST_LOG_OBJECT (v4lsrc, "grabbing frame");
267 g_mutex_lock (v4lsrc->mutex_queue_state);
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);
281 g_mutex_unlock (v4lsrc->mutex_queue_state);
284 if (!gst_v4lsrc_queue_frame (v4lsrc, v4lsrc->queue_frame)) {
285 g_mutex_unlock (v4lsrc->mutex_queue_state);
288 v4lsrc->queue_frame = (v4lsrc->queue_frame + 1) % v4lsrc->mbuf.frames;
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);
297 v4lsrc->sync_frame = (v4lsrc->sync_frame + 1) % v4lsrc->mbuf.frames;
299 g_mutex_unlock (v4lsrc->mutex_queue_state);
301 GST_LOG_OBJECT (v4lsrc, "grabbed frame %d", *num);
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 ******************************************************/
314 gst_v4lsrc_get_buffer (GstV4lSrc * v4lsrc, gint num)
316 if (!GST_V4L_IS_ACTIVE (GST_V4LELEMENT (v4lsrc)) ||
317 !GST_V4L_IS_OPEN (GST_V4LELEMENT (v4lsrc)))
320 if (num < 0 || num >= v4lsrc->mbuf.frames)
323 return GST_V4LELEMENT (v4lsrc)->buffer + v4lsrc->mbuf.offsets[num];
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 ******************************************************/
334 gst_v4lsrc_requeue_frame (GstV4lSrc * v4lsrc, gint num)
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));
340 /* mark frame as 'ready to requeue' */
341 g_mutex_lock (v4lsrc->mutex_queue_state);
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));
350 v4lsrc->frame_queue_state[num] = QUEUE_STATE_READY_FOR_QUEUE;
352 /* let an optional wait know */
353 g_cond_broadcast (v4lsrc->cond_queue_state);
355 g_mutex_unlock (v4lsrc->mutex_queue_state);
361 /******************************************************
362 * gst_v4lsrc_capture_stop():
363 * stop streaming capture
364 * return value: TRUE on success, FALSE on error
365 ******************************************************/
368 gst_v4lsrc_capture_stop (GstV4lSrc * v4lsrc)
370 GST_DEBUG_OBJECT (v4lsrc, "stopping capture");
371 GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
372 GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lsrc));
374 g_mutex_lock (v4lsrc->mutex_queue_state);
375 v4lsrc->is_capturing = FALSE;
377 /* make an optional pending wait stop */
379 g_cond_broadcast (v4lsrc->cond_queue_state);
381 /* sync on remaining frames */
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;
391 g_mutex_unlock (v4lsrc->mutex_queue_state);
397 /******************************************************
398 * gst_v4lsrc_capture_deinit():
399 * deinitialize the capture system
400 * return value: TRUE on success, FALSE on error
401 ******************************************************/
404 gst_v4lsrc_capture_deinit (GstV4lSrc * v4lsrc)
406 GST_DEBUG_OBJECT (v4lsrc, "quitting capture subsystem");
407 GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
408 GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lsrc));
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;
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)));
424 GST_V4LELEMENT (v4lsrc)->buffer = NULL;
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,
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
442 * return value: TRUE on success, FALSE on error
443 ******************************************************/
446 gst_v4lsrc_try_capture (GstV4lSrc * v4lsrc, gint width, gint height,
449 /* so, we need a buffer and some more stuff */
452 struct video_mbuf vmbuf;
453 struct video_mmap vmmap;
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));
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)));
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)));
475 /* now that we have a buffer, let's try out our format */
477 vmmap.height = height;
478 vmmap.format = palette;
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);
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);
494 munmap (buffer, vmbuf.size);
496 /* if we got here, it worked! woohoo, the format is supported! */
500 #ifndef GST_DISABLE_GST_DEBUG
502 gst_v4lsrc_palette_name (int i)
504 return v4l_palette_name[i];
509 gst_v4lsrc_get_fps (GstV4lSrc * v4lsrc, gint * fps_n, gint * fps_d)
513 struct video_window *vwin = &GST_V4LELEMENT (v4lsrc)->vwin;
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 */
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);
528 *fps_n = fps_index * 15;
535 /* removed fps estimation code here */
537 /* if that failed ... */
539 if (!GST_V4L_IS_OPEN (GST_V4LELEMENT (v4lsrc)))
542 if (!gst_v4l_get_chan_norm (GST_V4LELEMENT (v4lsrc), NULL, &norm))
545 if (norm == VIDEO_MODE_NTSC) {
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.
566 gst_v4lsrc_get_fps_list (GstV4lSrc * v4lsrc)
569 struct video_window *vwin = &GST_V4LELEMENT (v4lsrc)->vwin;
570 GstV4lElement *v4lelement = GST_V4LELEMENT (v4lsrc);
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 */
578 /* webcams have a non-zero fps_index */
579 if (fps_index == 0) {
580 GST_DEBUG_OBJECT (v4lsrc, "fps_index is 0, no webcam");
583 GST_DEBUG_OBJECT (v4lsrc, "fps_index is %d, so webcam", fps_index);
588 GValue value = { 0 };
590 /* webcam detected, so try all framerates and return a list */
592 list = g_new0 (GValue, 1);
593 g_value_init (list, GST_TYPE_LIST);
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);
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);
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))
623 typedef struct _GstV4lSrcBuffer
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);
637 static GstBufferClass *v4lbuffer_parent_class = NULL;
640 gst_v4lsrc_buffer_get_type (void)
642 static GType _gst_v4lsrc_buffer_type;
644 if (G_UNLIKELY (_gst_v4lsrc_buffer_type == 0)) {
645 static const GTypeInfo v4lsrc_buffer_info = {
646 sizeof (GstBufferClass),
649 gst_v4lsrc_buffer_class_init,
652 sizeof (GstV4lSrcBuffer),
654 gst_v4lsrc_buffer_init,
657 _gst_v4lsrc_buffer_type = g_type_register_static (GST_TYPE_BUFFER,
658 "GstV4lSrcBuffer", &v4lsrc_buffer_info, 0);
660 return _gst_v4lsrc_buffer_type;
664 gst_v4lsrc_buffer_class_init (gpointer g_class, gpointer class_data)
666 GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
668 v4lbuffer_parent_class = g_type_class_peek_parent (g_class);
670 mini_object_class->finalize = (GstMiniObjectFinalizeFunction)
671 gst_v4lsrc_buffer_finalize;
675 gst_v4lsrc_buffer_init (GTypeInstance * instance, gpointer g_class)
681 gst_v4lsrc_buffer_finalize (GstV4lSrcBuffer * v4lsrc_buffer)
683 GstMiniObjectClass *miniobject_class;
687 v4lsrc = v4lsrc_buffer->v4lsrc;
688 num = v4lsrc_buffer->num;
690 GST_LOG_OBJECT (v4lsrc, "freeing buffer %p for frame %d", v4lsrc_buffer, num);
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);
698 gst_object_unref (v4lsrc);
700 miniobject_class = (GstMiniObjectClass *) v4lbuffer_parent_class;
701 miniobject_class->finalize (GST_MINI_OBJECT_CAST (v4lsrc_buffer));
704 /* Create a V4lSrc buffer from our mmap'd data area */
706 gst_v4lsrc_buffer_new (GstV4lSrc * v4lsrc, gint num)
708 GstClockTime duration, timestamp, latency;
713 GST_DEBUG_OBJECT (v4lsrc, "creating buffer for frame %d", num);
715 if (!(gst_v4lsrc_get_fps (v4lsrc, &fps_n, &fps_d)))
718 buf = (GstBuffer *) gst_mini_object_new (GST_TYPE_V4LSRC_BUFFER);
720 GST_V4LSRC_BUFFER (buf)->num = num;
721 GST_V4LSRC_BUFFER (buf)->v4lsrc = gst_object_ref (v4lsrc);
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;
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);
736 /* no clock, can't set timestamps */
737 timestamp = GST_CLOCK_TIME_NONE;
739 GST_OBJECT_UNLOCK (v4lsrc);
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),
746 latency = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
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);
753 /* adjust timestamp for frame latency (we assume we have a framerate) */
754 if (timestamp > latency)
755 timestamp -= latency;
760 GST_BUFFER_TIMESTAMP (buf) = timestamp;
761 GST_BUFFER_DURATION (buf) = duration;