2001-12-23 15:31:15 +00:00
|
|
|
/* G-Streamer hardware MJPEG video source 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 <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 "v4lmjpegsrc_calls.h"
|
|
|
|
|
|
|
|
/* 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
|
|
|
#define MIN_BUFFERS_QUEUED 2
|
|
|
|
|
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(v4lmjpegsrc), \
|
2002-09-17 17:45:19 +00:00
|
|
|
"V4LMJPEGSRC: " format, ##args)
|
2002-09-09 07:12:29 +00:00
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
enum {
|
|
|
|
QUEUE_STATE_ERROR = -1,
|
|
|
|
QUEUE_STATE_READY_FOR_QUEUE,
|
|
|
|
QUEUE_STATE_QUEUED,
|
|
|
|
QUEUE_STATE_SYNCED,
|
|
|
|
};
|
2001-12-23 15:31:15 +00:00
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsrc_queue_frame():
|
|
|
|
* queue a frame for capturing
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_v4lmjpegsrc_queue_frame (GstV4lMjpegSrc *v4lmjpegsrc,
|
|
|
|
gint num)
|
|
|
|
{
|
2002-09-09 07:12:29 +00:00
|
|
|
DEBUG("queueing frame %d", num);
|
2001-12-23 15:31:15 +00:00
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
if (v4lmjpegsrc->frame_queue_state[num] != QUEUE_STATE_READY_FOR_QUEUE) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-12-23 15:31:15 +00:00
|
|
|
if (ioctl(GST_V4LELEMENT(v4lmjpegsrc)->video_fd, MJPIOC_QBUF_CAPT, &num) < 0)
|
|
|
|
{
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, READ, (NULL),
|
2004-01-18 21:46:58 +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
|
|
|
v4lmjpegsrc->frame_queue_state[num] = QUEUE_STATE_QUEUED;
|
|
|
|
v4lmjpegsrc->num_queued++;
|
|
|
|
|
2001-12-23 15:31:15 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsrc_sync_next_frame():
|
|
|
|
* sync on the next frame for capturing
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_v4lmjpegsrc_sync_next_frame (GstV4lMjpegSrc *v4lmjpegsrc,
|
|
|
|
gint *num)
|
|
|
|
{
|
2002-09-09 07:12:29 +00:00
|
|
|
DEBUG("syncing on next frame");
|
2001-12-23 15:31:15 +00:00
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
if (v4lmjpegsrc->num_queued <= 0) {
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
while (ioctl(GST_V4LELEMENT(v4lmjpegsrc)->video_fd,
|
|
|
|
MJPIOC_SYNC, &(v4lmjpegsrc->bsync)) < 0) {
|
|
|
|
if (errno != EINTR) {
|
2004-02-27 23:36:43 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, SYNC, (NULL), GST_ERROR_SYSTEM);
|
2003-05-21 06:33:18 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
DEBUG("Sync got interrupted");
|
|
|
|
}
|
|
|
|
|
2001-12-23 15:31:15 +00:00
|
|
|
*num = v4lmjpegsrc->bsync.frame;
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
v4lmjpegsrc->frame_queue_state[*num] = QUEUE_STATE_SYNCED;
|
|
|
|
v4lmjpegsrc->num_queued--;
|
|
|
|
|
2001-12-23 15:31:15 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsrc_set_buffer():
|
|
|
|
* set buffer parameters (size/count)
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4lmjpegsrc_set_buffer (GstV4lMjpegSrc *v4lmjpegsrc,
|
|
|
|
gint numbufs,
|
|
|
|
gint bufsize)
|
|
|
|
{
|
2002-09-09 07:12:29 +00:00
|
|
|
DEBUG("setting buffer info to numbufs = %d, bufsize = %d KB",
|
2001-12-23 15:31:15 +00:00
|
|
|
numbufs, bufsize);
|
|
|
|
GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
GST_V4L_CHECK_NOT_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
|
2001-12-24 21:43:31 +00:00
|
|
|
v4lmjpegsrc->breq.size = bufsize * 1024;
|
2001-12-23 15:31:15 +00:00
|
|
|
v4lmjpegsrc->breq.count = numbufs;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsrc_set_capture():
|
|
|
|
* set capture parameters (simple)
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4lmjpegsrc_set_capture (GstV4lMjpegSrc *v4lmjpegsrc,
|
|
|
|
gint decimation,
|
|
|
|
gint quality)
|
|
|
|
{
|
|
|
|
int norm, input, mw;
|
|
|
|
struct mjpeg_params bparm;
|
|
|
|
|
2002-09-09 07:12:29 +00:00
|
|
|
DEBUG("setting decimation = %d, quality = %d",
|
2001-12-23 15:31:15 +00:00
|
|
|
decimation, quality);
|
|
|
|
GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
GST_V4L_CHECK_NOT_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
|
|
|
|
gst_v4l_get_chan_norm(GST_V4LELEMENT(v4lmjpegsrc), &input, &norm);
|
|
|
|
|
|
|
|
/* Query params for capture */
|
|
|
|
if (ioctl(GST_V4LELEMENT(v4lmjpegsrc)->video_fd, MJPIOC_G_PARAMS, &bparm) < 0)
|
|
|
|
{
|
2004-02-27 23:36:43 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, SETTINGS, (NULL),
|
|
|
|
GST_ERROR_SYSTEM);
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bparm.decimation = decimation;
|
|
|
|
bparm.quality = quality;
|
|
|
|
bparm.norm = norm;
|
|
|
|
bparm.input = input;
|
|
|
|
bparm.APP_len = 0; /* no JPEG markers - TODO: this is definately not right for decimation==1 */
|
|
|
|
|
|
|
|
mw = GST_V4LELEMENT(v4lmjpegsrc)->vcap.maxwidth;
|
|
|
|
if (mw != 768 && mw != 640)
|
|
|
|
{
|
|
|
|
if (decimation == 1)
|
|
|
|
mw = 720;
|
|
|
|
else
|
|
|
|
mw = 704;
|
|
|
|
}
|
|
|
|
v4lmjpegsrc->end_width = mw / decimation;
|
|
|
|
v4lmjpegsrc->end_height = (norm==VIDEO_MODE_NTSC?480:576) / decimation;
|
|
|
|
|
|
|
|
/* TODO: interlacing */
|
|
|
|
|
|
|
|
/* Set params for capture */
|
|
|
|
if (ioctl(GST_V4LELEMENT(v4lmjpegsrc)->video_fd, MJPIOC_S_PARAMS, &bparm) < 0)
|
|
|
|
{
|
2004-02-27 23:36:43 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, SETTINGS, (NULL),
|
|
|
|
GST_ERROR_SYSTEM);
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsrc_set_capture_m():
|
|
|
|
* set capture parameters (advanced)
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean gst_v4lmjpegsrc_set_capture_m (GstV4lMjpegSrc *v4lmjpegsrc,
|
|
|
|
gint x_offset,
|
|
|
|
gint y_offset,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
|
|
|
gint h_decimation,
|
|
|
|
gint v_decimation,
|
|
|
|
gint quality)
|
|
|
|
{
|
|
|
|
gint norm, input;
|
|
|
|
gint maxwidth;
|
|
|
|
struct mjpeg_params bparm;
|
|
|
|
|
2002-09-09 07:12:29 +00:00
|
|
|
DEBUG("setting x_offset = %d, y_offset = %d, "
|
2001-12-23 15:31:15 +00:00
|
|
|
"width = %d, height = %d, h_decimation = %d, v_decimation = %d, quality = %d\n",
|
|
|
|
x_offset, y_offset, width, height, h_decimation, v_decimation, quality);
|
|
|
|
GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
GST_V4L_CHECK_NOT_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
|
|
|
|
gst_v4l_get_chan_norm(GST_V4LELEMENT(v4lmjpegsrc), &input, &norm);
|
|
|
|
|
|
|
|
if (GST_V4LELEMENT(v4lmjpegsrc)->vcap.maxwidth != 768 &&
|
|
|
|
GST_V4LELEMENT(v4lmjpegsrc)->vcap.maxwidth != 640)
|
|
|
|
maxwidth = 720;
|
|
|
|
else
|
|
|
|
maxwidth = GST_V4LELEMENT(v4lmjpegsrc)->vcap.maxwidth;
|
|
|
|
|
|
|
|
/* Query params for capture */
|
|
|
|
if (ioctl(GST_V4LELEMENT(v4lmjpegsrc)->video_fd, MJPIOC_G_PARAMS, &bparm) < 0)
|
|
|
|
{
|
2004-02-27 23:36:43 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, SETTINGS, (NULL),
|
|
|
|
GST_ERROR_SYSTEM);
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bparm.decimation = 0;
|
|
|
|
bparm.quality = quality;
|
|
|
|
bparm.norm = norm;
|
|
|
|
bparm.input = input;
|
2003-05-21 06:33:18 +00:00
|
|
|
bparm.APP_len = 0; /* no JPEG markers - TODO: this is definately
|
|
|
|
* not right for decimation==1 */
|
2001-12-23 15:31:15 +00:00
|
|
|
|
|
|
|
if (width <= 0)
|
|
|
|
{
|
|
|
|
if (x_offset < 0) x_offset = 0;
|
|
|
|
width = (maxwidth==720&&h_decimation!=1)?704:maxwidth - 2*x_offset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (x_offset < 0)
|
|
|
|
x_offset = (maxwidth - width)/2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (height <= 0)
|
|
|
|
{
|
|
|
|
if (y_offset < 0) y_offset = 0;
|
|
|
|
height = (norm==VIDEO_MODE_NTSC)?480:576 - 2*y_offset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (y_offset < 0)
|
|
|
|
y_offset = ((norm==VIDEO_MODE_NTSC)?480:576 - height)/2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (width + x_offset > maxwidth)
|
|
|
|
{
|
2004-02-27 23:36:43 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, TOO_LAZY, (NULL),
|
2004-01-18 21:46:58 +00:00
|
|
|
("Image width+offset (%d) bigger than maximum (%d)",
|
|
|
|
width + x_offset, maxwidth));
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if ((width%(bparm.HorDcm*16))!=0)
|
|
|
|
{
|
2004-02-27 23:36:43 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsrc, STREAM, FORMAT, (NULL),
|
2004-01-18 21:46:58 +00:00
|
|
|
("Image width (%d) not multiple of %d (required for JPEG)",
|
|
|
|
width, bparm.HorDcm*16));
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (height + y_offset > (norm==VIDEO_MODE_NTSC ? 480 : 576))
|
|
|
|
{
|
2004-02-27 23:36:43 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, TOO_LAZY, (NULL),
|
2004-01-18 21:46:58 +00:00
|
|
|
("Image height+offset (%d) bigger than maximum (%d)",
|
|
|
|
height + y_offset, (norm==VIDEO_MODE_NTSC ? 480 : 576)));
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* RJ: Image height must only be a multiple of 8, but geom_height
|
|
|
|
* is double the field height
|
|
|
|
*/
|
|
|
|
if ((height%(bparm.VerDcm*16))!=0)
|
|
|
|
{
|
2004-02-27 23:36:43 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsrc, STREAM, FORMAT, (NULL),
|
2004-01-18 21:46:58 +00:00
|
|
|
("Image height (%d) not multiple of %d (required for JPEG)",
|
|
|
|
height, bparm.VerDcm*16));
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bparm.img_x = x_offset;
|
|
|
|
bparm.img_width = width;
|
|
|
|
bparm.img_y = y_offset;
|
|
|
|
bparm.img_height = height;
|
|
|
|
bparm.HorDcm = h_decimation;
|
|
|
|
bparm.VerDcm = (v_decimation==4) ? 2 : 1;
|
|
|
|
bparm.TmpDcm = (v_decimation==1) ? 1 : 2;
|
|
|
|
bparm.field_per_buff = (v_decimation==1) ? 2 : 1;
|
|
|
|
|
|
|
|
v4lmjpegsrc->end_width = width / h_decimation;
|
|
|
|
v4lmjpegsrc->end_width = height / v_decimation;
|
|
|
|
|
|
|
|
/* TODO: interlacing */
|
|
|
|
|
|
|
|
/* Set params for capture */
|
|
|
|
if (ioctl(GST_V4LELEMENT(v4lmjpegsrc)->video_fd, MJPIOC_S_PARAMS, &bparm) < 0)
|
|
|
|
{
|
2004-02-27 23:36:43 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, SETTINGS, (NULL),
|
|
|
|
GST_ERROR_SYSTEM);
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsrc_capture_init():
|
|
|
|
* initialize the capture system
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4lmjpegsrc_capture_init (GstV4lMjpegSrc *v4lmjpegsrc)
|
|
|
|
{
|
2002-09-09 07:12:29 +00:00
|
|
|
DEBUG("initting capture subsystem");
|
2001-12-23 15:31:15 +00:00
|
|
|
GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
GST_V4L_CHECK_NOT_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
|
|
|
|
/* Request buffers */
|
2003-05-21 06:33:18 +00:00
|
|
|
if (ioctl(GST_V4LELEMENT(v4lmjpegsrc)->video_fd,
|
|
|
|
MJPIOC_REQBUFS, &(v4lmjpegsrc->breq)) < 0)
|
2001-12-23 15:31:15 +00:00
|
|
|
{
|
2004-02-27 23:36:43 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
if (v4lmjpegsrc->breq.count < MIN_BUFFERS_QUEUED)
|
2003-05-02 21:16:55 +00:00
|
|
|
{
|
2004-02-27 23:36:43 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, READ, (NULL),
|
2004-01-18 21:46:58 +00:00
|
|
|
("Too little buffers. We got %ld, we want at least %d",
|
|
|
|
v4lmjpegsrc->breq.count, MIN_BUFFERS_QUEUED));
|
2003-05-02 21:16:55 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
gst_info("Got %ld buffers of size %ld KB\n",
|
|
|
|
v4lmjpegsrc->breq.count, v4lmjpegsrc->breq.size/1024);
|
|
|
|
|
|
|
|
/* keep track of queued buffers */
|
|
|
|
v4lmjpegsrc->frame_queue_state = (gint8 *)
|
|
|
|
g_malloc(sizeof(gint8) * v4lmjpegsrc->breq.count);
|
|
|
|
|
|
|
|
/* track how often to use each frame */
|
|
|
|
v4lmjpegsrc->use_num_times = (gint *)
|
|
|
|
g_malloc(sizeof(gint) * v4lmjpegsrc->breq.count);
|
|
|
|
|
|
|
|
/* lock for the frame_state */
|
|
|
|
v4lmjpegsrc->mutex_queue_state = g_mutex_new();
|
|
|
|
v4lmjpegsrc->cond_queue_state = g_cond_new();
|
|
|
|
|
2001-12-23 15:31:15 +00:00
|
|
|
/* Map the buffers */
|
2001-12-28 19:54:23 +00:00
|
|
|
GST_V4LELEMENT(v4lmjpegsrc)->buffer = mmap(0,
|
|
|
|
v4lmjpegsrc->breq.count * v4lmjpegsrc->breq.size,
|
2002-09-09 07:12:29 +00:00
|
|
|
PROT_READ|PROT_WRITE, MAP_SHARED, GST_V4LELEMENT(v4lmjpegsrc)->video_fd, 0);
|
2001-12-23 15:31:15 +00:00
|
|
|
if (GST_V4LELEMENT(v4lmjpegsrc)->buffer == MAP_FAILED)
|
|
|
|
{
|
2004-02-27 23:36:43 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, TOO_LAZY, (NULL),
|
2004-01-18 21:46:58 +00:00
|
|
|
("Error mapping video buffers: %s", g_strerror (errno)));
|
2001-12-23 15:31:15 +00:00
|
|
|
GST_V4LELEMENT(v4lmjpegsrc)->buffer = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsrc_capture_start():
|
|
|
|
* start streaming capture
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4lmjpegsrc_capture_start (GstV4lMjpegSrc *v4lmjpegsrc)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
2002-09-09 07:12:29 +00:00
|
|
|
DEBUG("starting capture");
|
2001-12-23 15:31:15 +00:00
|
|
|
GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
GST_V4L_CHECK_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
g_mutex_lock(v4lmjpegsrc->mutex_queue_state);
|
|
|
|
|
|
|
|
v4lmjpegsrc->quit = FALSE;
|
|
|
|
v4lmjpegsrc->num_queued = 0;
|
|
|
|
v4lmjpegsrc->queue_frame = 0;
|
|
|
|
|
|
|
|
/* set all buffers ready to queue , this starts streaming capture */
|
|
|
|
for (n=0;n<v4lmjpegsrc->breq.count;n++) {
|
|
|
|
v4lmjpegsrc->frame_queue_state[n] = QUEUE_STATE_READY_FOR_QUEUE;
|
|
|
|
if (!gst_v4lmjpegsrc_queue_frame(v4lmjpegsrc, n)) {
|
|
|
|
g_mutex_unlock(v4lmjpegsrc->mutex_queue_state);
|
|
|
|
gst_v4lmjpegsrc_capture_stop(v4lmjpegsrc);
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
2003-05-21 06:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_mutex_unlock(v4lmjpegsrc->mutex_queue_state);
|
2001-12-23 15:31:15 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsrc_grab_frame():
|
|
|
|
* grab one frame during streaming capture
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4lmjpegsrc_grab_frame (GstV4lMjpegSrc *v4lmjpegsrc,
|
|
|
|
gint *num,
|
|
|
|
gint *size)
|
|
|
|
{
|
2002-09-09 07:12:29 +00:00
|
|
|
DEBUG("grabbing frame");
|
2001-12-23 15:31:15 +00:00
|
|
|
GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
GST_V4L_CHECK_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
g_mutex_lock(v4lmjpegsrc->mutex_queue_state);
|
|
|
|
|
|
|
|
/* do we have enough frames? */
|
|
|
|
while (v4lmjpegsrc->num_queued < MIN_BUFFERS_QUEUED ||
|
|
|
|
v4lmjpegsrc->frame_queue_state[v4lmjpegsrc->queue_frame] ==
|
|
|
|
QUEUE_STATE_READY_FOR_QUEUE) {
|
|
|
|
while (v4lmjpegsrc->frame_queue_state[v4lmjpegsrc->queue_frame] !=
|
|
|
|
QUEUE_STATE_READY_FOR_QUEUE &&
|
|
|
|
!v4lmjpegsrc->quit) {
|
2003-06-29 19:46:12 +00:00
|
|
|
GST_DEBUG (
|
2003-05-21 06:33:18 +00:00
|
|
|
"Waiting for frames to become available (%d < %d)",
|
|
|
|
v4lmjpegsrc->num_queued, MIN_BUFFERS_QUEUED);
|
|
|
|
g_cond_wait(v4lmjpegsrc->cond_queue_state,
|
|
|
|
v4lmjpegsrc->mutex_queue_state);
|
|
|
|
}
|
|
|
|
if (v4lmjpegsrc->quit) {
|
|
|
|
g_mutex_unlock(v4lmjpegsrc->mutex_queue_state);
|
|
|
|
return TRUE; /* it won't get through anyway */
|
|
|
|
}
|
|
|
|
if (!gst_v4lmjpegsrc_queue_frame(v4lmjpegsrc, v4lmjpegsrc->queue_frame)) {
|
|
|
|
g_mutex_unlock(v4lmjpegsrc->mutex_queue_state);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
v4lmjpegsrc->queue_frame = (v4lmjpegsrc->queue_frame + 1) % v4lmjpegsrc->breq.count;
|
|
|
|
}
|
|
|
|
|
2001-12-23 15:31:15 +00:00
|
|
|
/* syncing on the buffer grabs it */
|
2003-05-21 06:33:18 +00:00
|
|
|
if (!gst_v4lmjpegsrc_sync_next_frame(v4lmjpegsrc, num)) {
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
2003-05-21 06:33:18 +00:00
|
|
|
}
|
2001-12-23 15:31:15 +00:00
|
|
|
|
|
|
|
*size = v4lmjpegsrc->bsync.length;
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
g_mutex_unlock(v4lmjpegsrc->mutex_queue_state);
|
|
|
|
|
2001-12-23 15:31:15 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsrc_get_buffer():
|
|
|
|
* get the memory address of a single buffer
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
guint8 *
|
|
|
|
gst_v4lmjpegsrc_get_buffer (GstV4lMjpegSrc *v4lmjpegsrc,
|
|
|
|
gint num)
|
|
|
|
{
|
2003-01-31 23:44:40 +00:00
|
|
|
/*DEBUG("gst_v4lmjpegsrc_get_buffer(), num = %d", num);*/
|
2001-12-23 15:31:15 +00:00
|
|
|
|
2001-12-24 21:43:31 +00:00
|
|
|
if (!GST_V4L_IS_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc)) ||
|
|
|
|
!GST_V4L_IS_OPEN(GST_V4LELEMENT(v4lmjpegsrc)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (num < 0 || num >= v4lmjpegsrc->breq.count)
|
2001-12-23 15:31:15 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return GST_V4LELEMENT(v4lmjpegsrc)->buffer+(v4lmjpegsrc->breq.size*num);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsrc_requeue_frame():
|
|
|
|
* requeue a frame for capturing
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4lmjpegsrc_requeue_frame (GstV4lMjpegSrc *v4lmjpegsrc,
|
|
|
|
gint num)
|
|
|
|
{
|
2002-09-09 07:12:29 +00:00
|
|
|
DEBUG("requeueing frame %d", num);
|
2001-12-23 15:31:15 +00:00
|
|
|
GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
GST_V4L_CHECK_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
/* mark frame as 'ready to requeue' */
|
|
|
|
g_mutex_lock(v4lmjpegsrc->mutex_queue_state);
|
|
|
|
|
|
|
|
if (v4lmjpegsrc->frame_queue_state[num] != QUEUE_STATE_SYNCED) {
|
2004-02-27 23:36:43 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsrc, RESOURCE, TOO_LAZY, (NULL),
|
2004-01-18 21:46:58 +00:00
|
|
|
("Invalid state %d (expected %d), can't requeue",
|
2003-05-21 06:33:18 +00:00
|
|
|
v4lmjpegsrc->frame_queue_state[num],
|
2004-01-18 21:46:58 +00:00
|
|
|
QUEUE_STATE_SYNCED));
|
2001-12-23 15:31:15 +00:00
|
|
|
return FALSE;
|
2003-05-21 06:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v4lmjpegsrc->frame_queue_state[num] = QUEUE_STATE_READY_FOR_QUEUE;
|
|
|
|
|
|
|
|
/* let an optional wait know */
|
|
|
|
g_cond_broadcast(v4lmjpegsrc->cond_queue_state);
|
|
|
|
|
|
|
|
g_mutex_unlock(v4lmjpegsrc->mutex_queue_state);
|
2001-12-23 15:31:15 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsrc_capture_stop():
|
|
|
|
* stop streaming capture
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4lmjpegsrc_capture_stop (GstV4lMjpegSrc *v4lmjpegsrc)
|
|
|
|
{
|
2003-05-21 06:33:18 +00:00
|
|
|
int n;
|
|
|
|
|
2002-09-09 07:12:29 +00:00
|
|
|
DEBUG("stopping capture");
|
2001-12-23 15:31:15 +00:00
|
|
|
GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
GST_V4L_CHECK_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
g_mutex_lock(v4lmjpegsrc->mutex_queue_state);
|
|
|
|
|
|
|
|
/* make an optional pending wait stop */
|
|
|
|
v4lmjpegsrc->quit = TRUE;
|
|
|
|
g_cond_broadcast(v4lmjpegsrc->cond_queue_state);
|
|
|
|
|
|
|
|
/* sync on remaining frames */
|
|
|
|
while (v4lmjpegsrc->num_queued > 0) {
|
|
|
|
gst_v4lmjpegsrc_sync_next_frame(v4lmjpegsrc, &n);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_mutex_unlock(v4lmjpegsrc->mutex_queue_state);
|
2001-12-23 15:31:15 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsrc_capture_deinit():
|
|
|
|
* deinitialize the capture system
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_v4lmjpegsrc_capture_deinit (GstV4lMjpegSrc *v4lmjpegsrc)
|
|
|
|
{
|
2002-09-09 07:12:29 +00:00
|
|
|
DEBUG("quitting capture subsystem");
|
2001-12-23 15:31:15 +00:00
|
|
|
GST_V4L_CHECK_OPEN(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
GST_V4L_CHECK_ACTIVE(GST_V4LELEMENT(v4lmjpegsrc));
|
|
|
|
|
|
|
|
/* unmap the buffer */
|
|
|
|
munmap(GST_V4LELEMENT(v4lmjpegsrc)->buffer, v4lmjpegsrc->breq.size * v4lmjpegsrc->breq.count);
|
|
|
|
GST_V4LELEMENT(v4lmjpegsrc)->buffer = NULL;
|
|
|
|
|
2003-05-21 06:33:18 +00:00
|
|
|
/* free buffer tracker */
|
|
|
|
g_mutex_free(v4lmjpegsrc->mutex_queue_state);
|
|
|
|
g_cond_free(v4lmjpegsrc->cond_queue_state);
|
|
|
|
g_free(v4lmjpegsrc->frame_queue_state);
|
|
|
|
g_free(v4lmjpegsrc->use_num_times);
|
2003-05-02 21:16:55 +00:00
|
|
|
|
2001-12-23 15:31:15 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|