2002-09-09 07:14:35 +00:00
|
|
|
/* G-Streamer Video4linux2 video-capture plugin - system calls
|
|
|
|
* Copyright (C) 2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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 "v4l2src_calls.h"
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#define DEBUG(format, args...) \
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG_OBJECT (\
|
2002-09-09 07:14:35 +00:00
|
|
|
GST_ELEMENT(v4l2src), \
|
2002-09-17 17:45:19 +00:00
|
|
|
"V4L2SRC: " format, ##args)
|
2002-09-09 07:14:35 +00:00
|
|
|
|
|
|
|
#define MIN_BUFFERS_QUEUED 2
|
|
|
|
|
|
|
|
/* On some systems MAP_FAILED seems to be missing */
|
|
|
|
#ifndef MAP_FAILED
|
|
|
|
#define MAP_FAILED ( (caddr_t) -1 )
|
|
|
|
#endif
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
enum {
|
|
|
|
QUEUE_STATE_ERROR = -1,
|
|
|
|
QUEUE_STATE_READY_FOR_QUEUE,
|
|
|
|
QUEUE_STATE_QUEUED,
|
|
|
|
QUEUE_STATE_SYNCED,
|
|
|
|
};
|
2002-09-09 07:14:35 +00:00
|
|
|
|
2002-11-13 12:35:56 +00:00
|
|
|
/******************************************************
|
|
|
|
* gst_v4l2src_fill_format_list():
|
|
|
|
* create list of supported capture formats
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4l2src_fill_format_list (GstV4l2Src *v4l2src)
|
|
|
|
{
|
|
|
|
gint n;
|
|
|
|
|
|
|
|
DEBUG("getting src format enumerations");
|
|
|
|
|
|
|
|
/* format enumeration */
|
|
|
|
for (n=0;;n++) {
|
|
|
|
struct v4l2_fmtdesc format, *fmtptr;
|
|
|
|
format.index = n;
|
|
|
|
format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
if (ioctl(GST_V4L2ELEMENT(v4l2src)->video_fd, VIDIOC_ENUM_FMT, &format) < 0) {
|
|
|
|
if (errno == EINVAL)
|
|
|
|
break; /* end of enumeration */
|
|
|
|
else {
|
|
|
|
gst_element_error(GST_ELEMENT(v4l2src),
|
|
|
|
"Failed to get no. %d in pixelformat enumeration for %s: %s",
|
|
|
|
n, GST_V4L2ELEMENT(v4l2src)->device, g_strerror(errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmtptr = g_malloc(sizeof(format));
|
|
|
|
memcpy(fmtptr, &format, sizeof(format));
|
|
|
|
v4l2src->formats = g_list_append(v4l2src->formats, fmtptr);
|
2003-03-02 21:58:52 +00:00
|
|
|
|
|
|
|
v4l2src->format_list = g_list_append(v4l2src->format_list, fmtptr->description);
|
2002-11-13 12:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4l2src_empty_format_list():
|
|
|
|
* free list of supported capture formats
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4l2src_empty_format_list (GstV4l2Src *v4l2src)
|
|
|
|
{
|
|
|
|
while (g_list_length(v4l2src->formats) > 0) {
|
|
|
|
gpointer data = g_list_nth_data(v4l2src->formats, 0);
|
|
|
|
v4l2src->formats = g_list_remove(v4l2src->formats, data);
|
|
|
|
g_free(data);
|
|
|
|
}
|
2003-03-02 21:58:52 +00:00
|
|
|
g_list_free(v4l2src->format_list);
|
|
|
|
v4l2src->format_list = NULL;
|
2002-11-13 12:35:56 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-09 07:14:35 +00:00
|
|
|
/******************************************************
|
|
|
|
* gst_v4l2src_queue_frame():
|
|
|
|
* queue a frame for capturing
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_v4l2src_queue_frame (GstV4l2Src *v4l2src,
|
|
|
|
gint num)
|
|
|
|
{
|
|
|
|
DEBUG("queueing frame %d", num);
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
if (v4l2src->frame_queue_state[num] != QUEUE_STATE_READY_FOR_QUEUE) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2002-09-09 07:14:35 +00:00
|
|
|
v4l2src->bufsettings.index = num;
|
2003-05-21 06:33:18 +00:00
|
|
|
if (ioctl(GST_V4L2ELEMENT(v4l2src)->video_fd,
|
|
|
|
VIDIOC_QBUF, &v4l2src->bufsettings) < 0) {
|
2002-09-09 07:14:35 +00:00
|
|
|
gst_element_error(GST_ELEMENT(v4l2src),
|
|
|
|
"Error queueing buffer %d on device %s: %s",
|
2002-10-26 09:40:55 +00:00
|
|
|
num, GST_V4L2ELEMENT(v4l2src)->device, g_strerror(errno));
|
2002-09-09 07:14:35 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
v4l2src->frame_queue_state[num] = QUEUE_STATE_QUEUED;
|
|
|
|
v4l2src->num_queued++;
|
|
|
|
|
2002-09-09 07:14:35 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
2003-05-21 06:33:18 +00:00
|
|
|
* gst_v4l2src_sync_next_frame():
|
2002-09-09 07:14:35 +00:00
|
|
|
* sync on a frame for capturing
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_v4l2src_sync_next_frame (GstV4l2Src *v4l2src,
|
|
|
|
gint *num)
|
|
|
|
{
|
2003-05-21 06:33:18 +00:00
|
|
|
if (v4l2src->num_queued <= 0) {
|
2002-09-09 07:14:35 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2003-05-21 06:33:18 +00:00
|
|
|
|
|
|
|
while (ioctl(GST_V4L2ELEMENT(v4l2src)->video_fd,
|
|
|
|
VIDIOC_DQBUF, &v4l2src->bufsettings) < 0) {
|
|
|
|
/* if the sync() got interrupted, we can retry */
|
|
|
|
if (errno != EINTR) {
|
|
|
|
gst_element_error(GST_ELEMENT(v4l2src),
|
|
|
|
"Error syncing on a buffer on device %s: %s",
|
|
|
|
GST_V4L2ELEMENT(v4l2src)->device, g_strerror(errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
DEBUG("Sync got interrupted");
|
|
|
|
}
|
|
|
|
|
2002-09-09 07:14:35 +00:00
|
|
|
DEBUG("synced on frame %d", v4l2src->bufsettings.index);
|
|
|
|
*num = v4l2src->bufsettings.index;
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
v4l2src->frame_queue_state[*num] = QUEUE_STATE_SYNCED;
|
|
|
|
v4l2src->num_queued--;
|
|
|
|
|
2002-09-09 07:14:35 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4l2src_get_capture():
|
|
|
|
* get capture parameters
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4l2src_get_capture (GstV4l2Src *v4l2src)
|
|
|
|
{
|
|
|
|
DEBUG("Getting capture format");
|
|
|
|
|
|
|
|
GST_V4L2_CHECK_OPEN(GST_V4L2ELEMENT(v4l2src));
|
|
|
|
|
2002-11-13 12:35:56 +00:00
|
|
|
v4l2src->format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
2002-09-09 07:14:35 +00:00
|
|
|
if (ioctl(GST_V4L2ELEMENT(v4l2src)->video_fd, VIDIOC_G_FMT, &v4l2src->format) < 0) {
|
|
|
|
gst_element_error(GST_ELEMENT(v4l2src),
|
|
|
|
"Failed to get pixel format for device %s: %s",
|
2002-10-26 09:40:55 +00:00
|
|
|
GST_V4L2ELEMENT(v4l2src)->device, g_strerror(errno));
|
2002-09-09 07:14:35 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4l2src_set_capture():
|
|
|
|
* set capture parameters
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4l2src_set_capture (GstV4l2Src *v4l2src,
|
|
|
|
struct v4l2_fmtdesc *fmt,
|
|
|
|
gint width,
|
|
|
|
gint height)
|
|
|
|
{
|
|
|
|
DEBUG("Setting capture format to %dx%d, format %s",
|
|
|
|
width, height, fmt->description);
|
|
|
|
|
|
|
|
GST_V4L2_CHECK_OPEN(GST_V4L2ELEMENT(v4l2src));
|
|
|
|
GST_V4L2_CHECK_NOT_ACTIVE(GST_V4L2ELEMENT(v4l2src));
|
|
|
|
|
|
|
|
memset(&v4l2src->format, 0, sizeof(struct v4l2_format));
|
|
|
|
v4l2src->format.fmt.pix.width = width;
|
|
|
|
v4l2src->format.fmt.pix.height = height;
|
|
|
|
v4l2src->format.fmt.pix.pixelformat = fmt->pixelformat;
|
2002-11-13 12:35:56 +00:00
|
|
|
v4l2src->format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
2002-09-09 07:14:35 +00:00
|
|
|
|
|
|
|
if (ioctl(GST_V4L2ELEMENT(v4l2src)->video_fd, VIDIOC_S_FMT, &v4l2src->format) < 0) {
|
|
|
|
gst_element_error(GST_ELEMENT(v4l2src),
|
|
|
|
"Failed to set pixel format to %s @ %dx%d for device %s: %s",
|
|
|
|
fmt->description, width, height,
|
2002-10-26 09:40:55 +00:00
|
|
|
GST_V4L2ELEMENT(v4l2src)->device, g_strerror(errno));
|
2002-09-09 07:14:35 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
/* update internal info */
|
|
|
|
return gst_v4l2src_get_capture(v4l2src);;
|
2002-09-09 07:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4l2src_capture_init():
|
|
|
|
* initialize the capture system
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4l2src_capture_init (GstV4l2Src *v4l2src)
|
|
|
|
{
|
|
|
|
gint n;
|
|
|
|
gchar *desc = NULL;
|
2003-05-21 06:33:18 +00:00
|
|
|
struct v4l2_buffer buf;
|
2002-09-09 07:14:35 +00:00
|
|
|
|
|
|
|
DEBUG("initting the capture system");
|
|
|
|
|
|
|
|
GST_V4L2_CHECK_OPEN(GST_V4L2ELEMENT(v4l2src));
|
|
|
|
GST_V4L2_CHECK_NOT_ACTIVE(GST_V4L2ELEMENT(v4l2src));
|
|
|
|
|
2003-05-22 16:18:21 +00:00
|
|
|
/* request buffer info */
|
2003-05-22 16:10:12 +00:00
|
|
|
if (v4l2src->breq.count < MIN_BUFFERS_QUEUED) {
|
|
|
|
v4l2src->breq.count = MIN_BUFFERS_QUEUED;
|
|
|
|
}
|
2002-09-09 07:14:35 +00:00
|
|
|
v4l2src->breq.type = v4l2src->format.type;
|
2003-05-22 16:18:21 +00:00
|
|
|
v4l2src->breq.memory = V4L2_MEMORY_MMAP;
|
2003-05-22 16:10:12 +00:00
|
|
|
if (ioctl(GST_V4L2ELEMENT(v4l2src)->video_fd,
|
|
|
|
VIDIOC_REQBUFS, &v4l2src->breq) < 0) {
|
2002-09-09 07:14:35 +00:00
|
|
|
gst_element_error(GST_ELEMENT(v4l2src),
|
|
|
|
"Error requesting buffers (%d) for %s: %s",
|
2002-10-26 09:40:55 +00:00
|
|
|
v4l2src->breq.count, GST_V4L2ELEMENT(v4l2src)->device, g_strerror(errno));
|
2002-09-09 07:14:35 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v4l2src->breq.count < MIN_BUFFERS_QUEUED) {
|
|
|
|
gst_element_error(GST_ELEMENT(v4l2src),
|
|
|
|
"Too little buffers. We got %d, we want at least %d",
|
|
|
|
v4l2src->breq.count, MIN_BUFFERS_QUEUED);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
v4l2src->bufsettings.type = v4l2src->format.type;
|
|
|
|
|
2002-11-13 12:35:56 +00:00
|
|
|
for (n=0;n<g_list_length(v4l2src->formats);n++) {
|
|
|
|
struct v4l2_fmtdesc *fmt = (struct v4l2_fmtdesc *) g_list_nth_data(v4l2src->formats, n);
|
2002-09-09 07:14:35 +00:00
|
|
|
if (v4l2src->format.fmt.pix.pixelformat == fmt->pixelformat) {
|
|
|
|
desc = fmt->description;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gst_info("Got %d buffers (%s) of size %d KB\n",
|
|
|
|
v4l2src->breq.count, desc, v4l2src->format.fmt.pix.sizeimage/1024);
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
/* keep track of queued buffers */
|
|
|
|
v4l2src->frame_queue_state = (gint8 *)
|
|
|
|
g_malloc(sizeof(gint8) * v4l2src->breq.count);
|
|
|
|
|
|
|
|
/* track how often to use each frame */
|
|
|
|
v4l2src->use_num_times = (gint *)
|
|
|
|
g_malloc(sizeof(gint) * v4l2src->breq.count);
|
|
|
|
|
|
|
|
/* lock for the frame_state */
|
|
|
|
v4l2src->mutex_queue_state = g_mutex_new();
|
|
|
|
v4l2src->cond_queue_state = g_cond_new();
|
2003-05-02 21:16:56 +00:00
|
|
|
|
2002-09-09 07:14:35 +00:00
|
|
|
/* Map the buffers */
|
2003-05-21 06:33:18 +00:00
|
|
|
GST_V4L2ELEMENT(v4l2src)->buffer = (guint8 **)
|
|
|
|
g_malloc(sizeof(guint8 *) * v4l2src->breq.count);
|
2002-09-09 07:14:35 +00:00
|
|
|
for (n=0;n<v4l2src->breq.count;n++) {
|
2003-05-21 06:33:18 +00:00
|
|
|
buf.index = n;
|
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
if (ioctl(GST_V4L2ELEMENT(v4l2src)->video_fd,
|
|
|
|
VIDIOC_QUERYBUF, &buf) < 0) {
|
|
|
|
gst_element_error(GST_ELEMENT(v4l2src),
|
|
|
|
"Failed to get buffer (%d) properties: %s",
|
|
|
|
n, g_strerror(errno));
|
|
|
|
gst_v4l2src_capture_deinit(v4l2src);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
GST_V4L2ELEMENT(v4l2src)->buffer[n] = mmap(0,
|
|
|
|
buf.length, PROT_READ|PROT_WRITE, MAP_SHARED,
|
|
|
|
GST_V4L2ELEMENT(v4l2src)->video_fd, buf.m.offset);
|
2002-09-09 07:14:35 +00:00
|
|
|
if (GST_V4L2ELEMENT(v4l2src)->buffer[n] == MAP_FAILED) {
|
|
|
|
gst_element_error(GST_ELEMENT(v4l2src),
|
2003-05-21 06:33:18 +00:00
|
|
|
"Error mapping video buffer (%d) on device %s: %s",
|
|
|
|
n, GST_V4L2ELEMENT(v4l2src)->device,
|
|
|
|
g_strerror(errno));
|
2002-09-09 07:14:35 +00:00
|
|
|
GST_V4L2ELEMENT(v4l2src)->buffer[n] = NULL;
|
2003-05-21 06:33:18 +00:00
|
|
|
gst_v4l2src_capture_deinit(v4l2src);
|
2002-09-09 07:14:35 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4l2src_capture_start():
|
|
|
|
* start streaming capture
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4l2src_capture_start (GstV4l2Src *v4l2src)
|
|
|
|
{
|
|
|
|
gint n;
|
|
|
|
|
|
|
|
DEBUG("starting the capturing");
|
|
|
|
GST_V4L2_CHECK_OPEN(GST_V4L2ELEMENT(v4l2src));
|
|
|
|
GST_V4L2_CHECK_ACTIVE(GST_V4L2ELEMENT(v4l2src));
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
g_mutex_lock(v4l2src->mutex_queue_state);
|
|
|
|
|
|
|
|
v4l2src->quit = FALSE;
|
|
|
|
v4l2src->num_queued = 0;
|
|
|
|
v4l2src->queue_frame = 0;
|
|
|
|
|
|
|
|
/* set all buffers ready to queue , this starts streaming capture */
|
|
|
|
for (n=0;n<v4l2src->breq.count;n++) {
|
|
|
|
v4l2src->frame_queue_state[n] = QUEUE_STATE_READY_FOR_QUEUE;
|
|
|
|
if (!gst_v4l2src_queue_frame(v4l2src, n)) {
|
|
|
|
g_mutex_unlock(v4l2src->mutex_queue_state);
|
|
|
|
gst_v4l2src_capture_stop(v4l2src);
|
2002-09-09 07:14:35 +00:00
|
|
|
return FALSE;
|
2003-05-21 06:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-09 07:14:35 +00:00
|
|
|
n = 1;
|
|
|
|
if (ioctl(GST_V4L2ELEMENT(v4l2src)->video_fd, VIDIOC_STREAMON, &n) < 0) {
|
|
|
|
gst_element_error(GST_ELEMENT(v4l2src),
|
|
|
|
"Error starting streaming capture for %s: %s",
|
2002-10-26 09:40:55 +00:00
|
|
|
GST_V4L2ELEMENT(v4l2src)->device, g_strerror(errno));
|
2002-09-09 07:14:35 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
g_mutex_unlock(v4l2src->mutex_queue_state);
|
|
|
|
|
2002-09-09 07:14:35 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4l2src_grab_frame():
|
|
|
|
* capture one frame during streaming capture
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4l2src_grab_frame (GstV4l2Src *v4l2src,
|
|
|
|
gint *num)
|
|
|
|
{
|
|
|
|
DEBUG("syncing on the next frame");
|
|
|
|
|
|
|
|
GST_V4L2_CHECK_OPEN(GST_V4L2ELEMENT(v4l2src));
|
|
|
|
GST_V4L2_CHECK_ACTIVE(GST_V4L2ELEMENT(v4l2src));
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
g_mutex_lock(v4l2src->mutex_queue_state);
|
|
|
|
|
|
|
|
/* do we have enough frames? */
|
|
|
|
while (v4l2src->num_queued < MIN_BUFFERS_QUEUED ||
|
|
|
|
v4l2src->frame_queue_state[v4l2src->queue_frame] ==
|
|
|
|
QUEUE_STATE_READY_FOR_QUEUE) {
|
|
|
|
while (v4l2src->frame_queue_state[v4l2src->queue_frame] !=
|
|
|
|
QUEUE_STATE_READY_FOR_QUEUE &&
|
|
|
|
!v4l2src->quit) {
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG (
|
2003-05-21 06:33:18 +00:00
|
|
|
"Waiting for frames to become available (%d < %d)",
|
|
|
|
v4l2src->num_queued, MIN_BUFFERS_QUEUED);
|
|
|
|
g_cond_wait(v4l2src->cond_queue_state,
|
|
|
|
v4l2src->mutex_queue_state);
|
|
|
|
}
|
|
|
|
if (v4l2src->quit) {
|
|
|
|
g_mutex_unlock(v4l2src->mutex_queue_state);
|
|
|
|
return TRUE; /* it won't get through anyway */
|
|
|
|
}
|
|
|
|
if (!gst_v4l2src_queue_frame(v4l2src, v4l2src->queue_frame)) {
|
|
|
|
g_mutex_unlock(v4l2src->mutex_queue_state);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
v4l2src->queue_frame = (v4l2src->queue_frame + 1) % v4l2src->breq.count;
|
|
|
|
}
|
|
|
|
|
2002-09-09 07:14:35 +00:00
|
|
|
/* syncing on the buffer grabs it */
|
2003-05-21 06:33:18 +00:00
|
|
|
if (!gst_v4l2src_sync_next_frame(v4l2src, num)) {
|
|
|
|
g_mutex_unlock(v4l2src->mutex_queue_state);
|
2002-09-09 07:14:35 +00:00
|
|
|
return FALSE;
|
2003-05-21 06:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_mutex_unlock(v4l2src->mutex_queue_state);
|
2002-09-09 07:14:35 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
/******************************************************
|
|
|
|
*
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
guint8 *
|
|
|
|
gst_v4l2src_get_buffer (GstV4l2Src *v4l2src,
|
|
|
|
gint num)
|
|
|
|
{
|
|
|
|
if (!GST_V4L2_IS_ACTIVE(GST_V4L2ELEMENT(v4l2src)) ||
|
|
|
|
!GST_V4L2_IS_OPEN(GST_V4L2ELEMENT(v4l2src)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (num < 0 || num >= v4l2src->breq.count)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return GST_V4L2ELEMENT(v4l2src)->buffer[num];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-09 07:14:35 +00:00
|
|
|
/******************************************************
|
|
|
|
* gst_v4l2src_requeue_frame():
|
|
|
|
* re-queue a frame after we're done with the buffer
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4l2src_requeue_frame (GstV4l2Src *v4l2src,
|
|
|
|
gint num)
|
|
|
|
{
|
|
|
|
DEBUG("requeueing frame %d", num);
|
|
|
|
GST_V4L2_CHECK_OPEN(GST_V4L2ELEMENT(v4l2src));
|
|
|
|
GST_V4L2_CHECK_ACTIVE(GST_V4L2ELEMENT(v4l2src));
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
/* mark frame as 'ready to requeue' */
|
|
|
|
g_mutex_lock(v4l2src->mutex_queue_state);
|
|
|
|
|
|
|
|
if (v4l2src->frame_queue_state[num] != QUEUE_STATE_SYNCED) {
|
|
|
|
gst_element_error(GST_ELEMENT(v4l2src),
|
|
|
|
"Invalid state %d (expected %d), can't requeue",
|
|
|
|
v4l2src->frame_queue_state[num],
|
|
|
|
QUEUE_STATE_SYNCED);
|
2002-09-09 07:14:35 +00:00
|
|
|
return FALSE;
|
2003-05-21 06:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v4l2src->frame_queue_state[num] = QUEUE_STATE_READY_FOR_QUEUE;
|
|
|
|
|
|
|
|
/* let an optional wait know */
|
|
|
|
g_cond_broadcast(v4l2src->cond_queue_state);
|
|
|
|
|
|
|
|
g_mutex_unlock(v4l2src->mutex_queue_state);
|
2002-09-09 07:14:35 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4l2src_capture_stop():
|
|
|
|
* stop streaming capture
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4l2src_capture_stop (GstV4l2Src *v4l2src)
|
|
|
|
{
|
|
|
|
gint n = 0;
|
|
|
|
|
|
|
|
DEBUG("stopping capturing");
|
|
|
|
GST_V4L2_CHECK_OPEN(GST_V4L2ELEMENT(v4l2src));
|
|
|
|
GST_V4L2_CHECK_ACTIVE(GST_V4L2ELEMENT(v4l2src));
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
g_mutex_lock(v4l2src->mutex_queue_state);
|
|
|
|
|
|
|
|
/* we actually need to sync on all queued buffers but not
|
|
|
|
* on the non-queued ones */
|
2002-09-09 07:14:35 +00:00
|
|
|
if (ioctl(GST_V4L2ELEMENT(v4l2src)->video_fd, VIDIOC_STREAMOFF, &n) < 0) {
|
|
|
|
gst_element_error(GST_ELEMENT(v4l2src),
|
|
|
|
"Error stopping streaming capture for %s: %s",
|
2002-10-26 09:40:55 +00:00
|
|
|
GST_V4L2ELEMENT(v4l2src)->device, g_strerror(errno));
|
2002-09-09 07:14:35 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
/* make an optional pending wait stop */
|
|
|
|
v4l2src->quit = TRUE;
|
|
|
|
g_cond_broadcast(v4l2src->cond_queue_state);
|
|
|
|
|
|
|
|
/* sync on remaining frames */
|
|
|
|
while (v4l2src->num_queued > 0) {
|
|
|
|
gst_v4l2src_sync_next_frame(v4l2src, &n);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_mutex_unlock(v4l2src->mutex_queue_state);
|
|
|
|
|
2002-09-09 07:14:35 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4l2src_capture_deinit():
|
|
|
|
* deinitialize the capture system
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4l2src_capture_deinit (GstV4l2Src *v4l2src)
|
|
|
|
{
|
2003-05-21 06:33:18 +00:00
|
|
|
int n;
|
|
|
|
|
2002-09-09 07:14:35 +00:00
|
|
|
DEBUG("deinitting capture system");
|
|
|
|
GST_V4L2_CHECK_OPEN(GST_V4L2ELEMENT(v4l2src));
|
|
|
|
GST_V4L2_CHECK_ACTIVE(GST_V4L2ELEMENT(v4l2src));
|
|
|
|
|
|
|
|
/* unmap the buffer */
|
|
|
|
for (n=0;n<v4l2src->breq.count;n++) {
|
2003-05-21 06:33:18 +00:00
|
|
|
if (!GST_V4L2ELEMENT(v4l2src)->buffer[n]) {
|
2002-09-09 07:14:35 +00:00
|
|
|
break;
|
2003-05-21 06:33:18 +00:00
|
|
|
}
|
|
|
|
munmap(GST_V4L2ELEMENT(v4l2src)->buffer[n],
|
|
|
|
v4l2src->format.fmt.pix.sizeimage);
|
2002-09-09 07:14:35 +00:00
|
|
|
GST_V4L2ELEMENT(v4l2src)->buffer[n] = NULL;
|
|
|
|
}
|
2003-05-21 06:33:18 +00:00
|
|
|
|
|
|
|
/* free buffer tracker */
|
2002-09-09 07:14:35 +00:00
|
|
|
g_free(GST_V4L2ELEMENT(v4l2src)->buffer);
|
|
|
|
GST_V4L2ELEMENT(v4l2src)->buffer = NULL;
|
2003-05-21 06:33:18 +00:00
|
|
|
g_mutex_free(v4l2src->mutex_queue_state);
|
|
|
|
g_cond_free(v4l2src->cond_queue_state);
|
|
|
|
g_free(v4l2src->frame_queue_state);
|
|
|
|
g_free(v4l2src->use_num_times);
|
2003-05-02 21:16:56 +00:00
|
|
|
|
2002-09-09 07:14:35 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2003-05-10 14:36:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4l2src_get_size_limits (GstV4l2Src *v4l2src,
|
|
|
|
struct v4l2_fmtdesc *format,
|
|
|
|
gint *min_w, gint *max_w,
|
|
|
|
gint *min_h, gint *max_h)
|
|
|
|
{
|
|
|
|
struct v4l2_format fmt;
|
|
|
|
|
|
|
|
/* get size delimiters */
|
|
|
|
memset(&fmt, 0, sizeof(fmt));
|
|
|
|
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
fmt.fmt.pix.width = 0;
|
|
|
|
fmt.fmt.pix.height = 0;
|
|
|
|
fmt.fmt.pix.pixelformat = format->pixelformat;
|
|
|
|
fmt.fmt.pix.field = V4L2_FIELD_ANY;
|
|
|
|
if (ioctl(GST_V4L2ELEMENT(v4l2src)->video_fd,
|
|
|
|
VIDIOC_TRY_FMT, &fmt) < 0) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (min_w)
|
|
|
|
*min_w = fmt.fmt.pix.width;
|
|
|
|
if (min_h)
|
|
|
|
*min_h = fmt.fmt.pix.height;
|
|
|
|
|
|
|
|
fmt.fmt.pix.width = G_MAXINT;
|
|
|
|
fmt.fmt.pix.height = G_MAXINT;
|
|
|
|
if (ioctl(GST_V4L2ELEMENT(v4l2src)->video_fd,
|
|
|
|
VIDIOC_TRY_FMT, &fmt) < 0) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_w)
|
|
|
|
*max_w = fmt.fmt.pix.width;
|
|
|
|
if (max_h)
|
|
|
|
*max_h = fmt.fmt.pix.height;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|