2004-06-15 09:11:06 +00:00
|
|
|
/* GStreamer
|
|
|
|
*
|
|
|
|
* v4lmjpegsink_calls.c: functions for hardware MJPEG video sink
|
|
|
|
*
|
2002-01-31 22:22:42 +00:00
|
|
|
* Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
|
2001-12-24 21:42:14 +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
|
|
|
|
|
2002-08-11 11:45:07 +00:00
|
|
|
#include <stdlib.h>
|
2001-12-24 21:42:14 +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 "v4lmjpegsink_calls.h"
|
|
|
|
|
|
|
|
/* On some systems MAP_FAILED seems to be missing */
|
|
|
|
#ifndef MAP_FAILED
|
|
|
|
#define MAP_FAILED ( (caddr_t) -1 )
|
|
|
|
#endif
|
|
|
|
|
2004-06-15 09:11:06 +00:00
|
|
|
GST_DEBUG_CATEGORY_EXTERN (v4lmjpegsink_debug);
|
|
|
|
#define GST_CAT_DEFAULT v4lmjpegsink_debug
|
2001-12-24 21:42:14 +00:00
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsink_sync_thread()
|
|
|
|
* thread keeps track of played frames
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
static void *
|
|
|
|
gst_v4lmjpegsink_sync_thread (void *arg)
|
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
GstV4lMjpegSink *v4lmjpegsink = GST_V4LMJPEGSINK (arg);
|
2004-03-15 19:32:28 +00:00
|
|
|
gint frame = 0; /* frame that we're currently syncing on */
|
2001-12-24 21:42:14 +00:00
|
|
|
|
2004-06-15 09:11:06 +00:00
|
|
|
GST_DEBUG_OBJECT (v4lmjpegsink, "starting sync thread");
|
2001-12-24 21:42:14 +00:00
|
|
|
|
2002-11-02 21:29:10 +00:00
|
|
|
#if 0
|
2001-12-24 21:42:14 +00:00
|
|
|
/* Allow easy shutting down by other processes... */
|
2004-03-14 22:34:34 +00:00
|
|
|
pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
|
|
|
|
pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
|
2002-11-02 21:29:10 +00:00
|
|
|
#endif
|
2001-12-24 21:42:14 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
while (1) {
|
|
|
|
g_mutex_lock (v4lmjpegsink->mutex_queued_frames);
|
|
|
|
if (!v4lmjpegsink->isqueued_queued_frames[frame]) {
|
|
|
|
g_cond_wait (v4lmjpegsink->cond_queued_frames[frame],
|
2004-03-15 19:32:28 +00:00
|
|
|
v4lmjpegsink->mutex_queued_frames);
|
2001-12-24 21:42:14 +00:00
|
|
|
}
|
2004-03-14 22:34:34 +00:00
|
|
|
if (v4lmjpegsink->isqueued_queued_frames[frame] != 1) {
|
|
|
|
g_mutex_unlock (v4lmjpegsink->mutex_queued_frames);
|
2001-12-24 21:42:14 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2004-03-14 22:34:34 +00:00
|
|
|
g_mutex_unlock (v4lmjpegsink->mutex_queued_frames);
|
2001-12-24 21:42:14 +00:00
|
|
|
|
2004-06-15 09:11:06 +00:00
|
|
|
GST_DEBUG_OBJECT (v4lmjpegsink, "thread-syncing on next frame");
|
2004-03-14 22:34:34 +00:00
|
|
|
if (ioctl (GST_V4LELEMENT (v4lmjpegsink)->video_fd, MJPIOC_SYNC,
|
2004-03-15 19:32:28 +00:00
|
|
|
&(v4lmjpegsink->bsync)) < 0) {
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, SYNC, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
("Failed to sync on frame %d: %s", frame, g_strerror (errno)));
|
2004-03-14 22:34:34 +00:00
|
|
|
g_mutex_lock (v4lmjpegsink->mutex_queued_frames);
|
2001-12-24 21:42:14 +00:00
|
|
|
v4lmjpegsink->isqueued_queued_frames[frame] = -1;
|
2004-03-14 22:34:34 +00:00
|
|
|
g_cond_broadcast (v4lmjpegsink->cond_queued_frames[frame]);
|
|
|
|
g_mutex_unlock (v4lmjpegsink->mutex_queued_frames);
|
2001-12-24 21:42:14 +00:00
|
|
|
goto end;
|
2004-03-14 22:34:34 +00:00
|
|
|
} else {
|
2001-12-24 21:42:14 +00:00
|
|
|
/* be sure that we're not confusing */
|
2004-03-14 22:34:34 +00:00
|
|
|
if (frame != v4lmjpegsink->bsync.frame) {
|
2004-03-15 19:32:28 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsink, CORE, TOO_LAZY, (NULL),
|
|
|
|
("Internal error: frame number confusion"));
|
|
|
|
goto end;
|
2001-12-24 21:42:14 +00:00
|
|
|
}
|
2004-03-14 22:34:34 +00:00
|
|
|
g_mutex_lock (v4lmjpegsink->mutex_queued_frames);
|
2001-12-24 21:42:14 +00:00
|
|
|
v4lmjpegsink->isqueued_queued_frames[frame] = 0;
|
2004-03-14 22:34:34 +00:00
|
|
|
g_cond_broadcast (v4lmjpegsink->cond_queued_frames[frame]);
|
|
|
|
g_mutex_unlock (v4lmjpegsink->mutex_queued_frames);
|
2001-12-24 21:42:14 +00:00
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
frame = (frame + 1) % v4lmjpegsink->breq.count;
|
2001-12-24 21:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
2004-06-15 09:11:06 +00:00
|
|
|
GST_DEBUG_OBJECT (v4lmjpegsink, "Sync thread got signalled to exit");
|
2004-03-14 22:34:34 +00:00
|
|
|
g_thread_exit (NULL);
|
2002-11-02 21:29:10 +00:00
|
|
|
return NULL;
|
2001-12-24 21:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsink_queue_frame()
|
|
|
|
* queue a frame for playback
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
static gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lmjpegsink_queue_frame (GstV4lMjpegSink * v4lmjpegsink, gint num)
|
2001-12-24 21:42:14 +00:00
|
|
|
{
|
2004-06-15 09:11:06 +00:00
|
|
|
GST_DEBUG_OBJECT (v4lmjpegsink, "queueing frame %d", num);
|
2001-12-24 21:42:14 +00:00
|
|
|
|
|
|
|
/* queue on this frame */
|
2004-03-14 22:34:34 +00:00
|
|
|
if (ioctl (GST_V4LELEMENT (v4lmjpegsink)->video_fd, MJPIOC_QBUF_PLAY,
|
2004-03-15 19:32:28 +00:00
|
|
|
&num) < 0) {
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, WRITE, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
("Failed to queue frame %d: %s", num, g_strerror (errno)));
|
2001-12-24 21:42:14 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
g_mutex_lock (v4lmjpegsink->mutex_queued_frames);
|
2001-12-24 21:42:14 +00:00
|
|
|
v4lmjpegsink->isqueued_queued_frames[num] = 1;
|
2004-03-14 22:34:34 +00:00
|
|
|
g_cond_broadcast (v4lmjpegsink->cond_queued_frames[num]);
|
|
|
|
g_mutex_unlock (v4lmjpegsink->mutex_queued_frames);
|
2001-12-24 21:42:14 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsink_sync_frame()
|
|
|
|
* wait for a frame to be finished playing
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
static gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lmjpegsink_sync_frame (GstV4lMjpegSink * v4lmjpegsink, gint * num)
|
2001-12-24 21:42:14 +00:00
|
|
|
{
|
2004-06-15 09:11:06 +00:00
|
|
|
GST_DEBUG_OBJECT (v4lmjpegsink, "syncing on next frame");
|
2001-12-24 21:42:14 +00:00
|
|
|
|
|
|
|
/* calculate next frame */
|
2004-03-14 22:34:34 +00:00
|
|
|
v4lmjpegsink->current_frame =
|
|
|
|
(v4lmjpegsink->current_frame + 1) % v4lmjpegsink->breq.count;
|
2001-12-24 21:42:14 +00:00
|
|
|
*num = v4lmjpegsink->current_frame;
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
g_mutex_lock (v4lmjpegsink->mutex_queued_frames);
|
|
|
|
if (v4lmjpegsink->isqueued_queued_frames[*num] == 1) {
|
|
|
|
g_cond_wait (v4lmjpegsink->cond_queued_frames[*num],
|
2004-03-15 19:32:28 +00:00
|
|
|
v4lmjpegsink->mutex_queued_frames);
|
2001-12-24 21:42:14 +00:00
|
|
|
}
|
2004-03-14 22:34:34 +00:00
|
|
|
if (v4lmjpegsink->isqueued_queued_frames[*num] != 0) {
|
|
|
|
g_mutex_unlock (v4lmjpegsink->mutex_queued_frames);
|
2001-12-24 21:42:14 +00:00
|
|
|
return FALSE;
|
2004-03-14 22:34:34 +00:00
|
|
|
} else
|
|
|
|
g_mutex_unlock (v4lmjpegsink->mutex_queued_frames);
|
2001-12-24 21:42:14 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsink_set_buffer()
|
|
|
|
* set buffer options
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lmjpegsink_set_buffer (GstV4lMjpegSink * v4lmjpegsink,
|
|
|
|
gint numbufs, gint bufsize)
|
2001-12-24 21:42:14 +00:00
|
|
|
{
|
2004-06-15 09:11:06 +00:00
|
|
|
GST_DEBUG_OBJECT (v4lmjpegsink,
|
|
|
|
"setting buffer info to numbufs = %d, bufsize = %d KB", numbufs, bufsize);
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lmjpegsink));
|
|
|
|
GST_V4L_CHECK_NOT_ACTIVE (GST_V4LELEMENT (v4lmjpegsink));
|
2001-12-24 21:42:14 +00:00
|
|
|
|
|
|
|
v4lmjpegsink->breq.size = bufsize * 1024;
|
|
|
|
v4lmjpegsink->breq.count = numbufs;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsink_set_playback()
|
|
|
|
* set playback options (video, interlacing, etc.)
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lmjpegsink_set_playback (GstV4lMjpegSink * v4lmjpegsink,
|
|
|
|
gint width,
|
|
|
|
gint height, gint x_offset, gint y_offset, gint norm, gint interlacing)
|
2001-12-24 21:42:14 +00:00
|
|
|
{
|
|
|
|
gint mw, mh;
|
|
|
|
struct mjpeg_params bparm;
|
|
|
|
|
2004-06-15 09:11:06 +00:00
|
|
|
GST_DEBUG_OBJECT (v4lmjpegsink,
|
|
|
|
"setting size=%dx%d, X/Y offsets=%d/%d, norm=%d, interlacing=%d\n",
|
2004-03-14 22:34:34 +00:00
|
|
|
width, height, x_offset, y_offset, norm, interlacing);
|
|
|
|
GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lmjpegsink));
|
2002-03-19 04:10:06 +00:00
|
|
|
/*GST_V4L_CHECK_NOT_ACTIVE(GST_V4LELEMENT(v4lmjpegsink)); */
|
2001-12-24 21:42:14 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
if (ioctl (GST_V4LELEMENT (v4lmjpegsink)->video_fd, MJPIOC_G_PARAMS,
|
2004-03-15 19:32:28 +00:00
|
|
|
&bparm) < 0) {
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, SETTINGS, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
GST_ERROR_SYSTEM);
|
2001-12-24 21:42:14 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bparm.input = 0;
|
|
|
|
bparm.norm = norm;
|
2004-03-15 19:32:28 +00:00
|
|
|
bparm.decimation = 0; /* we'll set proper values later on */
|
2001-12-24 21:42:14 +00:00
|
|
|
|
|
|
|
/* maxwidth is broken on marvel cards */
|
2004-03-14 22:34:34 +00:00
|
|
|
mw = GST_V4LELEMENT (v4lmjpegsink)->vcap.maxwidth;
|
2001-12-24 21:42:14 +00:00
|
|
|
if (mw != 768 && mw != 640)
|
|
|
|
mw = 720;
|
2004-03-14 22:34:34 +00:00
|
|
|
mh = (norm == VIDEO_MODE_NTSC ? 480 : 576);
|
2001-12-24 21:42:14 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
if (width > mw || height > mh) {
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, TOO_LAZY, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
("Video dimensions (%dx%d) are larger than device max (%dx%d)",
|
|
|
|
width, height, mw, mh));
|
2001-12-24 21:42:14 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
if (width <= mw / 4)
|
2001-12-24 21:42:14 +00:00
|
|
|
bparm.HorDcm = 4;
|
2004-03-14 22:34:34 +00:00
|
|
|
else if (width <= mw / 2)
|
2001-12-24 21:42:14 +00:00
|
|
|
bparm.HorDcm = 2;
|
|
|
|
else
|
|
|
|
bparm.HorDcm = 1;
|
|
|
|
|
|
|
|
/* TODO: add proper interlacing handling */
|
|
|
|
#if 0
|
2004-03-14 22:34:34 +00:00
|
|
|
if (interlacing != INTERLACING_NOT_INTERLACED) {
|
2001-12-24 21:42:14 +00:00
|
|
|
bparm.field_per_buff = 2;
|
|
|
|
bparm.TmpDcm = 1;
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
if (height <= mh / 2)
|
2001-12-24 21:42:14 +00:00
|
|
|
bparm.VerDcm = 2;
|
|
|
|
else
|
|
|
|
bparm.VerDcm = 1;
|
2004-03-14 22:34:34 +00:00
|
|
|
} else
|
2001-12-24 21:42:14 +00:00
|
|
|
#endif
|
|
|
|
{
|
2004-03-14 22:34:34 +00:00
|
|
|
if (height > mh / 2) {
|
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, TOO_LAZY, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
("Video dimensions (%dx%d) too large for non-interlaced playback (%dx%d)",
|
|
|
|
width, height, mw, mh / 2));
|
2001-12-24 21:42:14 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bparm.field_per_buff = 1;
|
|
|
|
bparm.TmpDcm = 2;
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
if (height <= mh / 4)
|
2001-12-24 21:42:14 +00:00
|
|
|
bparm.VerDcm = 2;
|
|
|
|
else
|
|
|
|
bparm.VerDcm = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: add proper interlacing handling */
|
|
|
|
#if 0
|
2004-03-14 22:34:34 +00:00
|
|
|
bparm.odd_even = (interlacing == INTERLACING_TOP_FIRST);
|
2001-12-24 21:42:14 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
bparm.quality = 100;
|
2004-03-14 22:34:34 +00:00
|
|
|
bparm.img_width = bparm.HorDcm * width;
|
|
|
|
bparm.img_height = bparm.VerDcm * height / bparm.field_per_buff;
|
2001-12-24 21:42:14 +00:00
|
|
|
|
|
|
|
/* image X/Y offset on device */
|
|
|
|
if (x_offset < 0)
|
2004-03-14 22:34:34 +00:00
|
|
|
bparm.img_x = (mw - bparm.img_width) / 2;
|
|
|
|
else {
|
2001-12-24 21:42:14 +00:00
|
|
|
if (x_offset + bparm.img_width > mw)
|
|
|
|
bparm.img_x = mw - bparm.img_width;
|
|
|
|
else
|
|
|
|
bparm.img_x = x_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y_offset < 0)
|
2004-03-14 22:34:34 +00:00
|
|
|
bparm.img_y = (mh / 2 - bparm.img_height) / 2;
|
|
|
|
else {
|
|
|
|
if (y_offset + bparm.img_height * 2 > mh)
|
|
|
|
bparm.img_y = mh / 2 - bparm.img_height;
|
2001-12-24 21:42:14 +00:00
|
|
|
else
|
2004-03-14 22:34:34 +00:00
|
|
|
bparm.img_y = y_offset / 2;
|
2001-12-24 21:42:14 +00:00
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
if (ioctl (GST_V4LELEMENT (v4lmjpegsink)->video_fd, MJPIOC_S_PARAMS,
|
2004-03-15 19:32:28 +00:00
|
|
|
&bparm) < 0) {
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, SETTINGS, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
GST_ERROR_SYSTEM);
|
2001-12-24 21:42:14 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsink_playback_init()
|
|
|
|
* initialize playback system, set up buffer, etc.
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lmjpegsink_playback_init (GstV4lMjpegSink * v4lmjpegsink)
|
2001-12-24 21:42:14 +00:00
|
|
|
{
|
|
|
|
gint n;
|
|
|
|
|
2004-06-15 09:11:06 +00:00
|
|
|
GST_DEBUG_OBJECT (v4lmjpegsink, "initting playback subsystem");
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lmjpegsink));
|
|
|
|
GST_V4L_CHECK_NOT_ACTIVE (GST_V4LELEMENT (v4lmjpegsink));
|
2001-12-24 21:42:14 +00:00
|
|
|
|
|
|
|
/* Request buffers */
|
2004-03-14 22:34:34 +00:00
|
|
|
if (ioctl (GST_V4LELEMENT (v4lmjpegsink)->video_fd, MJPIOC_REQBUFS,
|
2004-03-15 19:32:28 +00:00
|
|
|
&(v4lmjpegsink->breq)) < 0) {
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
|
2001-12-24 21:42:14 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-06-15 09:11:06 +00:00
|
|
|
GST_INFO_OBJECT (v4lmjpegsink, "Got %ld buffers of size %ld KB",
|
2004-03-14 22:34:34 +00:00
|
|
|
v4lmjpegsink->breq.count, v4lmjpegsink->breq.size / 1024);
|
2001-12-24 21:42:14 +00:00
|
|
|
|
|
|
|
/* Map the buffers */
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_V4LELEMENT (v4lmjpegsink)->buffer = mmap (0,
|
|
|
|
v4lmjpegsink->breq.count * v4lmjpegsink->breq.size,
|
|
|
|
PROT_READ | PROT_WRITE, MAP_SHARED,
|
|
|
|
GST_V4LELEMENT (v4lmjpegsink)->video_fd, 0);
|
|
|
|
if (GST_V4LELEMENT (v4lmjpegsink)->buffer == MAP_FAILED) {
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsink, 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 (v4lmjpegsink)->buffer = NULL;
|
2001-12-24 21:42:14 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2002-11-02 21:29:10 +00:00
|
|
|
/* allocate/init the GThread thingies */
|
2004-03-14 22:34:34 +00:00
|
|
|
v4lmjpegsink->mutex_queued_frames = g_mutex_new ();
|
2001-12-24 21:42:14 +00:00
|
|
|
v4lmjpegsink->isqueued_queued_frames = (gint8 *)
|
2004-03-14 22:34:34 +00:00
|
|
|
malloc (sizeof (gint8) * v4lmjpegsink->breq.count);
|
|
|
|
if (!v4lmjpegsink->isqueued_queued_frames) {
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, TOO_LAZY, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
("Failed to create queue tracker: %s", g_strerror (errno)));
|
2001-12-24 21:42:14 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2002-11-02 21:29:10 +00:00
|
|
|
v4lmjpegsink->cond_queued_frames = (GCond **)
|
2004-03-14 22:34:34 +00:00
|
|
|
malloc (sizeof (GCond *) * v4lmjpegsink->breq.count);
|
|
|
|
if (!v4lmjpegsink->cond_queued_frames) {
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, TOO_LAZY, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
("Failed to create queue condition holders: %s", g_strerror (errno)));
|
2001-12-24 21:42:14 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2004-03-14 22:34:34 +00:00
|
|
|
for (n = 0; n < v4lmjpegsink->breq.count; n++)
|
|
|
|
v4lmjpegsink->cond_queued_frames[n] = g_cond_new ();
|
2001-12-24 21:42:14 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsink_playback_start()
|
|
|
|
* start playback system
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lmjpegsink_playback_start (GstV4lMjpegSink * v4lmjpegsink)
|
2001-12-24 21:42:14 +00:00
|
|
|
{
|
2002-11-02 21:29:10 +00:00
|
|
|
GError *error;
|
2001-12-24 21:42:14 +00:00
|
|
|
gint n;
|
|
|
|
|
2004-06-15 09:11:06 +00:00
|
|
|
GST_DEBUG_OBJECT (v4lmjpegsink, "starting playback");
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lmjpegsink));
|
|
|
|
GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lmjpegsink));
|
2001-12-24 21:42:14 +00:00
|
|
|
|
|
|
|
/* mark all buffers as unqueued */
|
2004-03-14 22:34:34 +00:00
|
|
|
for (n = 0; n < v4lmjpegsink->breq.count; n++)
|
2001-12-24 21:42:14 +00:00
|
|
|
v4lmjpegsink->isqueued_queued_frames[n] = 0;
|
|
|
|
|
|
|
|
v4lmjpegsink->current_frame = -1;
|
|
|
|
|
|
|
|
/* create sync() thread */
|
2004-03-14 22:34:34 +00:00
|
|
|
v4lmjpegsink->thread_queued_frames =
|
|
|
|
g_thread_create (gst_v4lmjpegsink_sync_thread, (void *) v4lmjpegsink,
|
|
|
|
TRUE, &error);
|
|
|
|
if (!v4lmjpegsink->thread_queued_frames) {
|
2004-02-02 17:23:33 +00:00
|
|
|
GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, TOO_LAZY, (NULL),
|
2004-03-15 19:32:28 +00:00
|
|
|
("Failed to create sync thread: %s", error->message));
|
2001-12-24 21:42:14 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsink_get_buffer()
|
|
|
|
* get address of a buffer
|
|
|
|
* return value: buffer's address or NULL
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
guint8 *
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lmjpegsink_get_buffer (GstV4lMjpegSink * v4lmjpegsink, gint num)
|
2001-12-24 21:42:14 +00:00
|
|
|
{
|
2004-06-15 09:11:06 +00:00
|
|
|
/*GST_DEBUG_OBJECT (v4lmjpegsink, gst_v4lmjpegsink_get_buffer(), num = %d", num); */
|
2001-12-24 21:42:14 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
if (!GST_V4L_IS_ACTIVE (GST_V4LELEMENT (v4lmjpegsink)) ||
|
|
|
|
!GST_V4L_IS_OPEN (GST_V4LELEMENT (v4lmjpegsink)))
|
2001-12-24 21:42:14 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (num < 0 || num >= v4lmjpegsink->breq.count)
|
|
|
|
return NULL;
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
return GST_V4LELEMENT (v4lmjpegsink)->buffer +
|
|
|
|
(v4lmjpegsink->breq.size * num);
|
2001-12-24 21:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsink_play_frame()
|
|
|
|
* queue a new buffer
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lmjpegsink_play_frame (GstV4lMjpegSink * v4lmjpegsink, gint num)
|
2001-12-24 21:42:14 +00:00
|
|
|
{
|
2004-06-15 09:11:06 +00:00
|
|
|
GST_DEBUG_OBJECT (v4lmjpegsink, "playing frame %d", num);
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lmjpegsink));
|
|
|
|
GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lmjpegsink));
|
2001-12-24 21:42:14 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
if (!gst_v4lmjpegsink_queue_frame (v4lmjpegsink, num))
|
2001-12-24 21:42:14 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsink_wait_frame()
|
|
|
|
* wait for buffer to be actually played
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lmjpegsink_wait_frame (GstV4lMjpegSink * v4lmjpegsink, gint * num)
|
2001-12-24 21:42:14 +00:00
|
|
|
{
|
2004-06-15 09:11:06 +00:00
|
|
|
GST_DEBUG_OBJECT (v4lmjpegsink,
|
|
|
|
"waiting for next frame to be finished playing");
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lmjpegsink));
|
|
|
|
GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lmjpegsink));
|
2001-12-24 21:42:14 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
if (!gst_v4lmjpegsink_sync_frame (v4lmjpegsink, num))
|
2001-12-24 21:42:14 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsink_playback_stop()
|
|
|
|
* stop playback system and sync on remaining frames
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lmjpegsink_playback_stop (GstV4lMjpegSink * v4lmjpegsink)
|
2001-12-24 21:42:14 +00:00
|
|
|
{
|
|
|
|
gint num;
|
|
|
|
|
2004-06-15 09:11:06 +00:00
|
|
|
GST_DEBUG_OBJECT (v4lmjpegsink, "stopping playback");
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lmjpegsink));
|
|
|
|
GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lmjpegsink));
|
2001-12-24 21:42:14 +00:00
|
|
|
|
|
|
|
/* mark next buffer as wrong */
|
2004-03-14 22:34:34 +00:00
|
|
|
if (!gst_v4lmjpegsink_sync_frame (v4lmjpegsink, &num) ||
|
|
|
|
!gst_v4lmjpegsink_queue_frame (v4lmjpegsink, num)) {
|
2001-12-24 21:42:14 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* .. and wait for all buffers to be queued on */
|
2004-03-14 22:34:34 +00:00
|
|
|
g_thread_join (v4lmjpegsink->thread_queued_frames);
|
2001-12-24 21:42:14 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* gst_v4lmjpegsink_playback_deinit()
|
|
|
|
* deinitialize the playback system and unmap buffer
|
|
|
|
* return value: TRUE on success, FALSE on error
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_v4lmjpegsink_playback_deinit (GstV4lMjpegSink * v4lmjpegsink)
|
2001-12-24 21:42:14 +00:00
|
|
|
{
|
2002-11-02 21:29:10 +00:00
|
|
|
int n;
|
|
|
|
|
2004-06-15 09:11:06 +00:00
|
|
|
GST_DEBUG_OBJECT (v4lmjpegsink, "quitting playback subsystem");
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_V4L_CHECK_OPEN (GST_V4LELEMENT (v4lmjpegsink));
|
|
|
|
GST_V4L_CHECK_ACTIVE (GST_V4LELEMENT (v4lmjpegsink));
|
2001-12-24 21:42:14 +00:00
|
|
|
|
2002-11-02 21:29:10 +00:00
|
|
|
/* free GThread thingies */
|
2004-03-14 22:34:34 +00:00
|
|
|
g_mutex_free (v4lmjpegsink->mutex_queued_frames);
|
|
|
|
for (n = 0; n < v4lmjpegsink->breq.count; n++)
|
|
|
|
g_cond_free (v4lmjpegsink->cond_queued_frames[n]);
|
|
|
|
free (v4lmjpegsink->cond_queued_frames);
|
|
|
|
free (v4lmjpegsink->isqueued_queued_frames);
|
2001-12-24 21:42:14 +00:00
|
|
|
|
|
|
|
/* unmap the buffer */
|
2004-03-14 22:34:34 +00:00
|
|
|
munmap (GST_V4LELEMENT (v4lmjpegsink)->buffer,
|
|
|
|
v4lmjpegsink->breq.size * v4lmjpegsink->breq.count);
|
|
|
|
GST_V4LELEMENT (v4lmjpegsink)->buffer = NULL;
|
2001-12-24 21:42:14 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|