2001-12-23 15:31:15 +00:00
|
|
|
/* G-Streamer BT8x8/V4L frame grabber plugin
|
2002-01-31 22:22:42 +00:00
|
|
|
* Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
|
2001-12-23 15:31:15 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2003-03-09 15:07:53 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2001-12-23 15:31:15 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include "v4lsrc_calls.h"
|
2002-01-04 23:58:11 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
/* number of buffers to be queued *at least* before syncing */
|
|
|
|
#define MIN_BUFFERS_QUEUED 2
|
2001-12-23 15:31:15 +00:00
|
|
|
|
|
|
|
/* On some systems MAP_FAILED seems to be missing */
|
|
|
|
#ifndef MAP_FAILED
|
|
|
|
#define MAP_FAILED ( (caddr_t) -1 )
|
|
|
|
#endif
|
|
|
|
|
2004-04-27 11:33:52 +00:00
|
|
|
GST_DEBUG_CATEGORY_EXTERN (v4l_debug);
|
|
|
|
|
|
|
|
#define GST_CAT_DEFAULT v4l_debug
|
|
|
|
|
2002-09-09 07:12:29 +00:00
|
|
|
#define DEBUG(format, args...) \
|
2003-06-29 19:46:12 +00:00
|
|
|
GST_DEBUG_OBJECT (\
|
2002-09-09 07:12:29 +00:00
|
|
|
GST_ELEMENT(v4lsrc), \
|
2002-09-17 17:45:19 +00:00
|
|
|
"V4LSRC: " format, ##args)
|
2001-12-23 15:31:15 +00:00
|
|
|
|
2002-01-07 09:31:34 +00:00
|
|
|
/* palette names */
|
2003-05-14 19:54:37 +00:00
|
|
|
static const char *palette_name[] = {
|
2004-03-15 19:32:28 +00:00
|
|
|
"", /* 0 */
|
|
|
|
"grayscale", /* VIDEO_PALETTE_GREY */
|
|
|
|
"Hi-420", /* VIDEO_PALETTE_HI420 */
|
|
|
|
"16-bit RGB (RGB-565)", /* VIDEO_PALETTE_RB565 */
|
|
|
|
"24-bit RGB", /* VIDEO_PALETTE_RGB24 */
|
|
|
|
"32-bit RGB", /* VIDEO_PALETTE_RGB32 */
|
|
|
|
"15-bit RGB (RGB-555)", /* VIDEO_PALETTE_RGB555 */
|
|
|
|
"YUV-4:2:2 (packed)", /* VIDEO_PALETTE_YUV422 */
|
|
|
|
"YUYV", /* VIDEO_PALETTE_YUYV */
|
|
|
|
"UYVY", /* VIDEO_PALETTE_UYVY */
|
|
|
|
"YUV-4:2:0 (packed)", /* VIDEO_PALETTE_YUV420 */
|
|
|
|
"YUV-4:1:1 (packed)", /* VIDEO_PALETTE_YUV411 */
|
|
|
|
"Raw", /* VIDEO_PALETTE_RAW */
|
|
|
|
"YUV-4:2:2 (planar)", /* VIDEO_PALETTE_YUV422P */
|
|
|
|
"YUV-4:1:1 (planar)", /* VIDEO_PALETTE_YUV411P */
|
|
|
|
"YUV-4:2:0 (planar)", /* VIDEO_PALETTE_YUV420P */
|
|
|
|
"YUV-4:1:0 (planar)" /* VIDEO_PALETTE_YUV410P */
|
2002-01-07 09:31:34 +00:00
|
|
|
};
|
|
|
|
|
2001-12-23 15:31:15 +00:00
|
|
|
/******************************************************
|
|
|
|
* gst_v4lsrc_queue_frame():
|
|
|
|
* queue a frame for capturing
|
2003-05-21 06:33:18 +00:00
|
|
|
* Requires queue_state lock to be held!
|
2001-12-23 15:31:15 +00:00
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
static gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lsrc_queue_frame (GstV4lSrc * v4lsrc, gint num)
|
2001-12-23 15:31:15 +00:00
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
DEBUG ("queueing frame %d", num);
|
2001-12-23 15:31:15 +00:00
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
if (v4lsrc->frame_queue_state[num] != QUEUE_STATE_READY_FOR_QUEUE) {
|
|
|
|
return FALSE;
|
2002-01-31 22:22:42 +00:00
|
|
|
}
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
v4lsrc->mmap.frame = num;
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
if (ioctl (GST_V4LELEMENT (v4lsrc)->video_fd,
|
2004-03-15 19:32:28 +00:00
|
|
|
VIDIOCMCAPTURE, &(v4lsrc->mmap)) < 0) {
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lsrc, RESOURCE, WRITE, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
("Error queueing a buffer (%d): %s", num, g_strerror (errno)));
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
v4lsrc->frame_queue_state[num] = QUEUE_STATE_QUEUED;
|
|
|
|
v4lsrc->num_queued++;
|
2001-12-23 19:14:18 +00:00
|
|
|
|
2001-12-23 15:31:15 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2003-05-21 06:33:18 +00:00
|
|
|
|
2003-03-01 14:35:17 +00:00
|
|
|
/******************************************************
|
|
|
|
* gst_v4lsrc_hard_sync_frame(GstV4lSrc *v4lsrc,gint num)
|
|
|
|
* sync a frame and set the timestamp correctly
|
2003-05-21 06:33:18 +00:00
|
|
|
* Requires queue_state lock to be held
|
2003-03-01 14:35:17 +00:00
|
|
|
*****************************************************/
|
2001-12-23 19:14:18 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
static gboolean
|
|
|
|
gst_v4lsrc_sync_frame (GstV4lSrc * v4lsrc, gint num)
|
2001-12-23 19:14:18 +00:00
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
DEBUG ("Syncing on frame %d", num);
|
2002-09-09 07:12:29 +00:00
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
if (v4lsrc->frame_queue_state[num] != QUEUE_STATE_QUEUED) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2001-12-23 19:14:18 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
while (ioctl (GST_V4LELEMENT (v4lsrc)->video_fd, VIDIOCSYNC, &num) < 0) {
|
2003-05-21 06:33:18 +00:00
|
|
|
/* if the sync() got interrupted, we can retry */
|
|
|
|
if (errno != EINTR) {
|
|
|
|
v4lsrc->frame_queue_state[num] = QUEUE_STATE_ERROR;
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lsrc, RESOURCE, SYNC, (NULL), GST_ERROR_SYSTEM);
|
2003-05-21 06:33:18 +00:00
|
|
|
return FALSE;
|
2001-12-23 19:14:18 +00:00
|
|
|
}
|
2004-03-14 22:34:34 +00:00
|
|
|
DEBUG ("Sync got interrupted");
|
2001-12-23 19:14:18 +00:00
|
|
|
}
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
if (v4lsrc->clock) {
|
2004-03-14 22:34:34 +00:00
|
|
|
v4lsrc->timestamp_sync = gst_clock_get_time (v4lsrc->clock);
|
2003-05-21 06:33:18 +00:00
|
|
|
} else {
|
|
|
|
GTimeVal time;
|
2004-03-14 22:34:34 +00:00
|
|
|
|
|
|
|
g_get_current_time (&time);
|
|
|
|
v4lsrc->timestamp_sync = GST_TIMEVAL_TO_TIME (time);
|
2001-12-23 15:31:15 +00:00
|
|
|
}
|
2003-03-01 14:35:17 +00:00
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
v4lsrc->frame_queue_state[num] = QUEUE_STATE_SYNCED;
|
|
|
|
v4lsrc->num_queued--;
|
2001-12-23 15:31:15 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lsrc_set_capture():
|
|
|
|
* set capture parameters, palette = VIDEO_PALETTE_*
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lsrc_set_capture (GstV4lSrc * v4lsrc,
|
|
|
|
gint width, gint height, gint palette)
|
2001-12-23 15:31:15 +00:00
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
DEBUG ("capture properties set to width = %d, height = %d, palette = %d",
|
|
|
|
width, height, palette);
|
2001-12-23 15:31:15 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
/*GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lsrc)); */
|
|
|
|
/*GST_V4L_CHECK_NOT_ACTIVE(GST_V4LELEMENT(v4lsrc)); */
|
2001-12-23 15:31:15 +00:00
|
|
|
|
|
|
|
v4lsrc->mmap.width = width;
|
|
|
|
v4lsrc->mmap.height = height;
|
|
|
|
v4lsrc->mmap.format = palette;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lsrc_capture_init():
|
|
|
|
* initialize the capture system
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lsrc_capture_init (GstV4lSrc * v4lsrc)
|
2001-12-23 15:31:15 +00:00
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
DEBUG ("initting capture subsystem");
|
|
|
|
GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
|
|
|
|
GST_V4L_CHECK_NOT_ACTIVE (GST_V4LELEMENT (v4lsrc));
|
2001-12-23 15:31:15 +00:00
|
|
|
|
|
|
|
/* request buffer info */
|
2004-03-14 22:34:34 +00:00
|
|
|
if (ioctl (GST_V4LELEMENT (v4lsrc)->video_fd, VIDIOCGMBUF,
|
2004-03-15 19:32:28 +00:00
|
|
|
&(v4lsrc->mbuf)) < 0) {
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lsrc, RESOURCE, READ, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
("Error getting buffer information: %s", g_strerror (errno)));
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
if (v4lsrc->mbuf.frames < MIN_BUFFERS_QUEUED) {
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lsrc, RESOURCE, READ, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
("Not enough buffers. We got %d, we want at least %d",
|
|
|
|
v4lsrc->mbuf.frames, MIN_BUFFERS_QUEUED));
|
2002-01-04 23:58:11 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_info ("Got %d buffers (\'%s\') of size %d KB\n",
|
|
|
|
v4lsrc->mbuf.frames, palette_name[v4lsrc->mmap.format],
|
|
|
|
v4lsrc->mbuf.size / (v4lsrc->mbuf.frames * 1024));
|
2001-12-23 15:31:15 +00:00
|
|
|
|
2003-05-02 21:16:55 +00:00
|
|
|
/* keep track of queued buffers */
|
2003-05-21 06:33:18 +00:00
|
|
|
v4lsrc->frame_queue_state = (gint8 *)
|
2004-03-14 22:34:34 +00:00
|
|
|
g_malloc (sizeof (gint8) * v4lsrc->mbuf.frames);
|
2001-12-23 15:31:15 +00:00
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
/* track how often to use each frame */
|
|
|
|
v4lsrc->use_num_times = (gint *)
|
2004-03-14 22:34:34 +00:00
|
|
|
g_malloc (sizeof (gint) * v4lsrc->mbuf.frames);
|
2001-12-23 19:14:18 +00:00
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
/* lock for the frame_state */
|
2004-03-14 22:34:34 +00:00
|
|
|
v4lsrc->mutex_queue_state = g_mutex_new ();
|
|
|
|
v4lsrc->cond_queue_state = g_cond_new ();
|
2001-12-23 19:14:18 +00:00
|
|
|
|
2001-12-23 15:31:15 +00:00
|
|
|
/* Map the buffers */
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_V4LELEMENT (v4lsrc)->buffer = mmap (0, v4lsrc->mbuf.size,
|
|
|
|
PROT_READ | PROT_WRITE, MAP_SHARED, GST_V4LELEMENT (v4lsrc)->video_fd, 0);
|
|
|
|
if (GST_V4LELEMENT (v4lsrc)->buffer == MAP_FAILED) {
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lsrc, RESOURCE, TOO_LAZY, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
("Error mapping video buffers: %s", g_strerror (errno)));
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_V4LELEMENT (v4lsrc)->buffer = NULL;
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lsrc_capture_start():
|
|
|
|
* start streaming capture
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lsrc_capture_start (GstV4lSrc * v4lsrc)
|
2001-12-23 15:31:15 +00:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
DEBUG ("starting capture");
|
|
|
|
GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
|
|
|
|
GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lsrc));
|
2001-12-23 15:31:15 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
g_mutex_lock (v4lsrc->mutex_queue_state);
|
2003-05-21 06:33:18 +00:00
|
|
|
|
2003-03-01 14:35:17 +00:00
|
|
|
v4lsrc->quit = FALSE;
|
2003-05-21 06:33:18 +00:00
|
|
|
v4lsrc->num_queued = 0;
|
|
|
|
v4lsrc->sync_frame = 0;
|
|
|
|
v4lsrc->queue_frame = 0;
|
|
|
|
|
2003-03-01 14:35:17 +00:00
|
|
|
/* set all buffers ready to queue , this starts streaming capture */
|
2004-03-14 22:34:34 +00:00
|
|
|
for (n = 0; n < v4lsrc->mbuf.frames; n++) {
|
2003-05-21 06:33:18 +00:00
|
|
|
v4lsrc->frame_queue_state[n] = QUEUE_STATE_READY_FOR_QUEUE;
|
2004-03-14 22:34:34 +00:00
|
|
|
if (!gst_v4lsrc_queue_frame (v4lsrc, n)) {
|
|
|
|
g_mutex_unlock (v4lsrc->mutex_queue_state);
|
|
|
|
gst_v4lsrc_capture_stop (v4lsrc);
|
2003-05-21 06:33:18 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2003-03-01 14:35:17 +00:00
|
|
|
}
|
2001-12-23 15:31:15 +00:00
|
|
|
|
2004-01-28 21:16:01 +00:00
|
|
|
v4lsrc->is_capturing = TRUE;
|
2004-03-14 22:34:34 +00:00
|
|
|
g_mutex_unlock (v4lsrc->mutex_queue_state);
|
2001-12-23 19:14:18 +00:00
|
|
|
|
2001-12-23 15:31:15 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lsrc_grab_frame():
|
|
|
|
* capture one frame during streaming capture
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lsrc_grab_frame (GstV4lSrc * v4lsrc, gint * num)
|
2001-12-23 15:31:15 +00:00
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
DEBUG ("grabbing frame");
|
|
|
|
GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
|
|
|
|
GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lsrc));
|
2001-12-23 15:31:15 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
g_mutex_lock (v4lsrc->mutex_queue_state);
|
2003-05-21 06:33:18 +00:00
|
|
|
|
|
|
|
/* do we have enough frames? */
|
|
|
|
while (v4lsrc->num_queued < MIN_BUFFERS_QUEUED ||
|
2004-03-14 22:34:34 +00:00
|
|
|
v4lsrc->frame_queue_state[v4lsrc->queue_frame] ==
|
|
|
|
QUEUE_STATE_READY_FOR_QUEUE) {
|
2003-05-21 06:33:18 +00:00
|
|
|
while (v4lsrc->frame_queue_state[v4lsrc->queue_frame] !=
|
2004-03-15 19:32:28 +00:00
|
|
|
QUEUE_STATE_READY_FOR_QUEUE && !v4lsrc->quit) {
|
2004-01-28 21:16:01 +00:00
|
|
|
GST_DEBUG ("Waiting for frames to become available (%d < %d)",
|
2004-03-15 19:32:28 +00:00
|
|
|
v4lsrc->num_queued, MIN_BUFFERS_QUEUED);
|
2004-03-14 22:34:34 +00:00
|
|
|
g_cond_wait (v4lsrc->cond_queue_state, v4lsrc->mutex_queue_state);
|
2003-05-21 06:33:18 +00:00
|
|
|
}
|
|
|
|
if (v4lsrc->quit) {
|
2004-03-14 22:34:34 +00:00
|
|
|
g_mutex_unlock (v4lsrc->mutex_queue_state);
|
2004-03-15 19:32:28 +00:00
|
|
|
return TRUE; /* it won't get through anyway */
|
2003-05-21 06:33:18 +00:00
|
|
|
}
|
2004-03-14 22:34:34 +00:00
|
|
|
if (!gst_v4lsrc_queue_frame (v4lsrc, v4lsrc->queue_frame)) {
|
|
|
|
g_mutex_unlock (v4lsrc->mutex_queue_state);
|
2003-05-21 06:33:18 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
v4lsrc->queue_frame = (v4lsrc->queue_frame + 1) % v4lsrc->mbuf.frames;
|
|
|
|
}
|
|
|
|
|
2001-12-23 15:31:15 +00:00
|
|
|
/* syncing on the buffer grabs it */
|
2003-05-21 06:33:18 +00:00
|
|
|
*num = v4lsrc->sync_frame;
|
2004-03-14 22:34:34 +00:00
|
|
|
if (!gst_v4lsrc_sync_frame (v4lsrc, *num)) {
|
|
|
|
g_mutex_unlock (v4lsrc->mutex_queue_state);
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
2003-05-21 06:33:18 +00:00
|
|
|
}
|
|
|
|
v4lsrc->sync_frame = (v4lsrc->sync_frame + 1) % v4lsrc->mbuf.frames;
|
|
|
|
|
2004-04-27 11:33:52 +00:00
|
|
|
GST_DEBUG ("grabbed frame %d", *num);
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
g_mutex_unlock (v4lsrc->mutex_queue_state);
|
2001-12-23 15:31:15 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lsrc_get_buffer():
|
|
|
|
* get the address of the just-capture buffer
|
2001-12-23 19:14:18 +00:00
|
|
|
* return value: the buffer's address or NULL
|
2001-12-23 15:31:15 +00:00
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
guint8 *
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lsrc_get_buffer (GstV4lSrc * v4lsrc, gint num)
|
2001-12-23 15:31:15 +00:00
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
if (!GST_V4L_IS_ACTIVE (GST_V4LELEMENT (v4lsrc)) ||
|
|
|
|
!GST_V4L_IS_OPEN (GST_V4LELEMENT (v4lsrc)))
|
2001-12-24 21:43:31 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (num < 0 || num >= v4lsrc->mbuf.frames)
|
2001-12-23 15:31:15 +00:00
|
|
|
return NULL;
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
return GST_V4LELEMENT (v4lsrc)->buffer + v4lsrc->mbuf.offsets[num];
|
2001-12-23 15:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lsrc_requeue_frame():
|
|
|
|
* re-queue a frame after we're done with the buffer
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lsrc_requeue_frame (GstV4lSrc * v4lsrc, gint num)
|
2001-12-23 15:31:15 +00:00
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
DEBUG ("requeueing frame %d", num);
|
|
|
|
GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
|
|
|
|
GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lsrc));
|
2001-12-23 15:31:15 +00:00
|
|
|
|
2003-02-06 18:45:35 +00:00
|
|
|
/* mark frame as 'ready to requeue' */
|
2004-03-14 22:34:34 +00:00
|
|
|
g_mutex_lock (v4lsrc->mutex_queue_state);
|
2003-05-21 06:33:18 +00:00
|
|
|
|
|
|
|
if (v4lsrc->frame_queue_state[num] != QUEUE_STATE_SYNCED) {
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lsrc, RESOURCE, TOO_LAZY, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
("Invalid state %d (expected %d), can't requeue",
|
|
|
|
v4lsrc->frame_queue_state[num], QUEUE_STATE_SYNCED));
|
2003-05-21 06:33:18 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
v4lsrc->frame_queue_state[num] = QUEUE_STATE_READY_FOR_QUEUE;
|
2003-03-01 14:35:17 +00:00
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
/* let an optional wait know */
|
2004-03-14 22:34:34 +00:00
|
|
|
g_cond_broadcast (v4lsrc->cond_queue_state);
|
2003-03-01 14:35:17 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
g_mutex_unlock (v4lsrc->mutex_queue_state);
|
2001-12-23 15:31:15 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lsrc_capture_stop():
|
|
|
|
* stop streaming capture
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lsrc_capture_stop (GstV4lSrc * v4lsrc)
|
2001-12-23 15:31:15 +00:00
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
DEBUG ("stopping capture");
|
|
|
|
GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
|
|
|
|
GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lsrc));
|
2001-12-23 15:31:15 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
g_mutex_lock (v4lsrc->mutex_queue_state);
|
2004-01-28 21:16:01 +00:00
|
|
|
v4lsrc->is_capturing = FALSE;
|
2003-05-21 06:33:18 +00:00
|
|
|
|
|
|
|
/* make an optional pending wait stop */
|
2003-03-01 14:35:17 +00:00
|
|
|
v4lsrc->quit = TRUE;
|
2004-03-14 22:34:34 +00:00
|
|
|
g_cond_broadcast (v4lsrc->cond_queue_state);
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
/* sync on remaining frames */
|
|
|
|
while (1) {
|
|
|
|
if (v4lsrc->frame_queue_state[v4lsrc->sync_frame] == QUEUE_STATE_QUEUED) {
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lsrc_sync_frame (v4lsrc, v4lsrc->sync_frame);
|
2003-05-21 06:33:18 +00:00
|
|
|
v4lsrc->sync_frame = (v4lsrc->sync_frame + 1) % v4lsrc->mbuf.frames;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-12-23 15:31:15 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
g_mutex_unlock (v4lsrc->mutex_queue_state);
|
2001-12-23 19:14:18 +00:00
|
|
|
|
2001-12-23 15:31:15 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lsrc_capture_deinit():
|
|
|
|
* deinitialize the capture system
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lsrc_capture_deinit (GstV4lSrc * v4lsrc)
|
2001-12-23 15:31:15 +00:00
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
DEBUG ("quitting capture subsystem");
|
|
|
|
GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
|
|
|
|
GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lsrc));
|
2001-12-23 15:31:15 +00:00
|
|
|
|
|
|
|
/* free buffer tracker */
|
2004-03-14 22:34:34 +00:00
|
|
|
g_mutex_free (v4lsrc->mutex_queue_state);
|
|
|
|
g_cond_free (v4lsrc->cond_queue_state);
|
|
|
|
g_free (v4lsrc->frame_queue_state);
|
|
|
|
g_free (v4lsrc->use_num_times);
|
2001-12-23 15:31:15 +00:00
|
|
|
|
|
|
|
/* unmap the buffer */
|
2004-03-14 22:34:34 +00:00
|
|
|
munmap (GST_V4LELEMENT (v4lsrc)->buffer, v4lsrc->mbuf.size);
|
|
|
|
GST_V4LELEMENT (v4lsrc)->buffer = NULL;
|
2001-12-23 15:31:15 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2002-09-20 09:28:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lsrc_try_palette():
|
|
|
|
* try out a palette on the device
|
|
|
|
* This has to be done before initializing the
|
|
|
|
* actual capture system, to make sure we don't
|
|
|
|
* mess up anything. So we need to mini-mmap()
|
|
|
|
* a buffer here, queue and sync on one buffer,
|
|
|
|
* and unmap it.
|
|
|
|
* This is ugly, yes, I know - but it's a major
|
|
|
|
* design flaw of v4l1 that you don't know in
|
|
|
|
* advance which formats will be supported...
|
|
|
|
* This is better than "just assuming that it'll
|
|
|
|
* work"...
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lsrc_try_palette (GstV4lSrc * v4lsrc, gint palette)
|
2002-09-20 09:28:46 +00:00
|
|
|
{
|
|
|
|
/* so, we need a buffer and some more stuff */
|
|
|
|
int frame = 0;
|
|
|
|
guint8 *buffer;
|
|
|
|
struct video_mbuf vmbuf;
|
|
|
|
struct video_mmap vmmap;
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
DEBUG ("gonna try out palette format %d (%s)",
|
|
|
|
palette, palette_name[palette]);
|
|
|
|
GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lsrc));
|
|
|
|
GST_V4L_CHECK_NOT_ACTIVE (GST_V4LELEMENT (v4lsrc));
|
2002-09-20 09:28:46 +00:00
|
|
|
|
|
|
|
/* let's start by requesting a buffer and mmap()'ing it */
|
2004-03-14 22:34:34 +00:00
|
|
|
if (ioctl (GST_V4LELEMENT (v4lsrc)->video_fd, VIDIOCGMBUF, &vmbuf) < 0) {
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lsrc, RESOURCE, READ, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
("Error getting buffer information: %s", g_strerror (errno)));
|
2002-09-20 09:28:46 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Map the buffers */
|
2004-03-14 22:34:34 +00:00
|
|
|
buffer = mmap (0, vmbuf.size, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED, GST_V4LELEMENT (v4lsrc)->video_fd, 0);
|
|
|
|
if (buffer == MAP_FAILED) {
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lsrc, RESOURCE, TOO_LAZY, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
("Error mapping our try-out buffer: %s", g_strerror (errno)));
|
2002-09-20 09:28:46 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now that we have a buffer, let's try out our format */
|
2004-03-14 22:34:34 +00:00
|
|
|
vmmap.width = GST_V4LELEMENT (v4lsrc)->vcap.minwidth;
|
|
|
|
vmmap.height = GST_V4LELEMENT (v4lsrc)->vcap.minheight;
|
2002-09-20 09:28:46 +00:00
|
|
|
vmmap.format = palette;
|
|
|
|
vmmap.frame = frame;
|
2004-03-14 22:34:34 +00:00
|
|
|
if (ioctl (GST_V4LELEMENT (v4lsrc)->video_fd, VIDIOCMCAPTURE, &vmmap) < 0) {
|
2004-03-15 19:32:28 +00:00
|
|
|
if (errno != EINVAL) /* our format failed! */
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lsrc, RESOURCE, TOO_LAZY, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
("Error queueing our try-out buffer: %s", g_strerror (errno)));
|
2004-03-14 22:34:34 +00:00
|
|
|
munmap (buffer, vmbuf.size);
|
2002-09-20 09:28:46 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
if (ioctl (GST_V4LELEMENT (v4lsrc)->video_fd, VIDIOCSYNC, &frame) < 0) {
|
2004-02-27 23:36:43 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lsrc, RESOURCE, SYNC, (NULL), GST_ERROR_SYSTEM);
|
2004-03-14 22:34:34 +00:00
|
|
|
munmap (buffer, vmbuf.size);
|
2002-09-20 09:28:46 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
munmap (buffer, vmbuf.size);
|
2002-09-20 09:28:46 +00:00
|
|
|
|
|
|
|
/* if we got here, it worked! woohoo, the format is supported! */
|
|
|
|
return TRUE;
|
|
|
|
}
|