2001-11-10 23:32:10 +00:00
|
|
|
/*
|
2001-12-03 16:38:40 +00:00
|
|
|
Video 4 Linux input
|
2001-11-10 23:32:10 +00:00
|
|
|
|
|
|
|
(C) Alex Beregszaszi <alex@naxine.org>
|
|
|
|
|
|
|
|
Some ideas are based on xawtv/libng's grab-v4l.c written by
|
|
|
|
Gerd Knorr <kraxel@bytesex.org>
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
Multithreading, a/v sync and native ALSA support by
|
|
|
|
Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
|
|
|
|
|
2001-11-10 23:32:10 +00:00
|
|
|
CODE IS UNDER DEVELOPMENT, NO FEATURE REQUESTS PLEASE!
|
|
|
|
*/
|
|
|
|
|
2001-11-09 23:46:06 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2001-12-01 15:05:40 +00:00
|
|
|
#if defined(USE_TV) && defined(HAVE_TV_V4L)
|
2001-11-09 23:46:06 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2001-11-10 23:32:10 +00:00
|
|
|
#include <signal.h>
|
2001-11-09 23:46:06 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
2002-06-24 16:37:18 +00:00
|
|
|
#include <sys/time.h>
|
2001-11-09 23:46:06 +00:00
|
|
|
#include <linux/videodev.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
2001-11-16 21:30:10 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2002-08-21 21:31:20 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#ifdef HAVE_SYS_SYSINFO_H
|
|
|
|
#include <sys/sysinfo.h>
|
|
|
|
#endif
|
2001-11-09 23:46:06 +00:00
|
|
|
|
2001-11-11 13:47:42 +00:00
|
|
|
#include "mp_msg.h"
|
|
|
|
#include "../libao2/afmt.h"
|
|
|
|
#include "../libvo/img_format.h"
|
|
|
|
#include "../libvo/fastmemcpy.h"
|
|
|
|
|
2001-11-09 23:46:06 +00:00
|
|
|
#include "tv.h"
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
#include "audio_in.h"
|
|
|
|
|
2001-11-09 23:46:06 +00:00
|
|
|
static tvi_info_t info = {
|
2001-12-27 23:52:48 +00:00
|
|
|
"Video 4 Linux input",
|
2001-11-09 23:46:06 +00:00
|
|
|
"v4l",
|
2001-11-10 23:32:10 +00:00
|
|
|
"Alex Beregszaszi <alex@naxine.org>",
|
|
|
|
"under development"
|
|
|
|
};
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
#define PAL_WIDTH 768
|
|
|
|
#define PAL_HEIGHT 576
|
|
|
|
#define PAL_FPS 25
|
|
|
|
|
|
|
|
#define NTSC_WIDTH 640
|
|
|
|
#define NTSC_HEIGHT 480
|
2003-01-14 18:20:17 +00:00
|
|
|
#define NTSC_FPS 29.97
|
2002-08-21 21:31:20 +00:00
|
|
|
|
2001-12-27 23:52:48 +00:00
|
|
|
#define MAX_AUDIO_CHANNELS 10
|
|
|
|
|
2002-12-10 09:11:42 +00:00
|
|
|
#define VID_BUF_SIZE_IMMEDIATE 2
|
2002-08-21 21:31:20 +00:00
|
|
|
|
2001-11-09 23:46:06 +00:00
|
|
|
typedef struct {
|
2001-11-10 23:32:10 +00:00
|
|
|
/* general */
|
2001-11-09 23:46:06 +00:00
|
|
|
char *video_device;
|
2002-08-21 21:31:20 +00:00
|
|
|
int video_fd;
|
2001-11-09 23:46:06 +00:00
|
|
|
struct video_capability capability;
|
|
|
|
struct video_channel *channels;
|
2001-11-11 16:28:12 +00:00
|
|
|
int act_channel;
|
2001-11-09 23:46:06 +00:00
|
|
|
struct video_tuner tuner;
|
2001-11-10 23:32:10 +00:00
|
|
|
|
|
|
|
/* video */
|
2001-11-09 23:46:06 +00:00
|
|
|
struct video_picture picture;
|
2001-11-10 23:32:10 +00:00
|
|
|
int format; /* output format */
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
int bytesperline;
|
2002-05-02 17:12:44 +00:00
|
|
|
int fps;
|
2001-11-09 23:46:06 +00:00
|
|
|
|
|
|
|
struct video_mbuf mbuf;
|
2002-08-21 21:31:20 +00:00
|
|
|
unsigned char *mmap;
|
2001-11-09 23:46:06 +00:00
|
|
|
struct video_mmap *buf;
|
2001-11-10 23:32:10 +00:00
|
|
|
int nbuf;
|
|
|
|
|
|
|
|
/* audio */
|
2001-12-27 23:52:48 +00:00
|
|
|
char *audio_device;
|
2002-08-21 21:31:20 +00:00
|
|
|
audio_in_t audio_in;
|
|
|
|
|
|
|
|
int audio_id;
|
2001-12-27 23:52:48 +00:00
|
|
|
struct video_audio audio[MAX_AUDIO_CHANNELS];
|
|
|
|
int audio_channels[MAX_AUDIO_CHANNELS];
|
2002-06-24 16:37:18 +00:00
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
/* buffering stuff */
|
|
|
|
int immediate_mode;
|
|
|
|
|
|
|
|
int audio_buffer_size;
|
|
|
|
int aud_skew_cnt;
|
|
|
|
unsigned char *audio_ringbuffer;
|
2002-11-08 22:05:39 +00:00
|
|
|
long long *audio_skew_buffer;
|
2002-08-21 21:31:20 +00:00
|
|
|
volatile int audio_head;
|
|
|
|
volatile int audio_tail;
|
|
|
|
volatile int audio_cnt;
|
2002-11-08 22:05:39 +00:00
|
|
|
volatile long long audio_skew;
|
2002-08-21 21:31:20 +00:00
|
|
|
volatile double audio_skew_factor;
|
2002-11-08 22:05:39 +00:00
|
|
|
volatile long long audio_skew_measure_time;
|
2002-08-21 21:31:20 +00:00
|
|
|
volatile int audio_drop;
|
|
|
|
|
|
|
|
int first;
|
2002-12-10 09:11:42 +00:00
|
|
|
int video_buffer_size_max;
|
|
|
|
volatile int video_buffer_size_current;
|
|
|
|
unsigned char **video_ringbuffer;
|
|
|
|
long long *video_timebuffer;
|
2002-08-21 21:31:20 +00:00
|
|
|
volatile int video_head;
|
|
|
|
volatile int video_tail;
|
|
|
|
volatile int video_cnt;
|
|
|
|
|
|
|
|
volatile int shutdown;
|
|
|
|
|
|
|
|
pthread_t audio_grabber_thread;
|
|
|
|
pthread_t video_grabber_thread;
|
|
|
|
pthread_mutex_t audio_starter;
|
|
|
|
pthread_mutex_t skew_mutex;
|
2002-12-10 09:11:42 +00:00
|
|
|
pthread_mutex_t video_buffer_mutex;
|
2002-08-21 21:31:20 +00:00
|
|
|
|
2002-11-08 22:05:39 +00:00
|
|
|
long long starttime;
|
2002-08-21 21:31:20 +00:00
|
|
|
double audio_secs_per_block;
|
2002-11-08 22:05:39 +00:00
|
|
|
long long audio_skew_total;
|
2002-08-21 21:31:20 +00:00
|
|
|
long audio_recv_blocks_total;
|
|
|
|
long audio_sent_blocks_total;
|
|
|
|
|
2001-11-09 23:46:06 +00:00
|
|
|
} priv_t;
|
|
|
|
|
|
|
|
#include "tvi_def.h"
|
|
|
|
|
2001-11-11 16:28:12 +00:00
|
|
|
static const char *device_cap2name[] = {
|
2001-11-09 23:46:06 +00:00
|
|
|
"capture", "tuner", "teletext", "overlay", "chromakey", "clipping",
|
2001-11-10 23:32:10 +00:00
|
|
|
"frameram", "scales", "monochrome", "subcapture", "mpeg-decoder",
|
|
|
|
"mpeg-encoder", "mjpeg-decoder", "mjpeg-encoder", NULL
|
2001-11-09 23:46:06 +00:00
|
|
|
};
|
|
|
|
|
2001-11-11 16:28:12 +00:00
|
|
|
static const char *device_palette2name[] = {
|
2001-11-10 23:32:10 +00:00
|
|
|
"-", "grey", "hi240", "rgb16", "rgb24", "rgb32", "rgb15", "yuv422",
|
|
|
|
"yuyv", "uyvy", "yuv420", "yuv411", "raw", "yuv422p", "yuv411p",
|
2002-09-28 18:49:47 +00:00
|
|
|
"yuv420p", "yuv410p"
|
|
|
|
};
|
|
|
|
#define PALETTE(x) ((x < sizeof(device_palette2name)/sizeof(char*)) ? device_palette2name[x] : "UNKNOWN")
|
|
|
|
|
|
|
|
static const char *norm2name(int mode)
|
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case VIDEO_MODE_PAL:
|
|
|
|
return "pal";
|
|
|
|
case VIDEO_MODE_SECAM:
|
|
|
|
return "secam";
|
|
|
|
case VIDEO_MODE_NTSC:
|
|
|
|
return "ntsc";
|
|
|
|
case VIDEO_MODE_AUTO:
|
|
|
|
return "auto";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
2001-11-10 23:32:10 +00:00
|
|
|
};
|
|
|
|
|
2002-08-30 11:37:42 +00:00
|
|
|
static const char *audio_mode2name(int mode)
|
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case VIDEO_SOUND_MONO:
|
|
|
|
return "mono";
|
|
|
|
case VIDEO_SOUND_STEREO:
|
|
|
|
return "stereo";
|
|
|
|
case VIDEO_SOUND_LANG1:
|
|
|
|
return "language1";
|
|
|
|
case VIDEO_SOUND_LANG2:
|
|
|
|
return "language2";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
2001-11-11 16:28:12 +00:00
|
|
|
};
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
static void *audio_grabber(void *data);
|
|
|
|
static void *video_grabber(void *data);
|
|
|
|
|
2001-11-10 23:32:10 +00:00
|
|
|
static int palette2depth(int palette)
|
|
|
|
{
|
2001-11-11 01:44:54 +00:00
|
|
|
switch(palette)
|
|
|
|
{
|
2001-11-30 16:59:02 +00:00
|
|
|
/* component */
|
2001-11-11 01:44:54 +00:00
|
|
|
case VIDEO_PALETTE_RGB555:
|
|
|
|
return(15);
|
|
|
|
case VIDEO_PALETTE_RGB565:
|
|
|
|
return(16);
|
|
|
|
case VIDEO_PALETTE_RGB24:
|
|
|
|
return(24);
|
|
|
|
case VIDEO_PALETTE_RGB32:
|
|
|
|
return(32);
|
2001-11-30 16:59:02 +00:00
|
|
|
/* planar */
|
|
|
|
case VIDEO_PALETTE_YUV411P:
|
2001-11-11 01:44:54 +00:00
|
|
|
case VIDEO_PALETTE_YUV420P:
|
2001-11-30 16:59:02 +00:00
|
|
|
case VIDEO_PALETTE_YUV410P:
|
2001-11-11 01:44:54 +00:00
|
|
|
return(12);
|
2001-11-30 16:59:02 +00:00
|
|
|
/* packed */
|
2001-12-27 23:52:48 +00:00
|
|
|
case VIDEO_PALETTE_YUV422P:
|
2001-11-11 01:44:54 +00:00
|
|
|
case VIDEO_PALETTE_YUV422:
|
2001-11-30 16:59:02 +00:00
|
|
|
case VIDEO_PALETTE_YUYV:
|
2001-11-11 01:44:54 +00:00
|
|
|
case VIDEO_PALETTE_UYVY:
|
2001-11-30 16:59:02 +00:00
|
|
|
case VIDEO_PALETTE_YUV420:
|
|
|
|
case VIDEO_PALETTE_YUV411:
|
2001-11-11 01:44:54 +00:00
|
|
|
return(16);
|
|
|
|
}
|
|
|
|
return(-1);
|
2001-11-10 23:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int format2palette(int format)
|
|
|
|
{
|
2001-11-11 01:44:54 +00:00
|
|
|
switch(format)
|
|
|
|
{
|
2002-08-21 21:31:20 +00:00
|
|
|
case IMGFMT_BGR15:
|
2001-11-11 01:44:54 +00:00
|
|
|
return(VIDEO_PALETTE_RGB555);
|
2002-08-21 21:31:20 +00:00
|
|
|
case IMGFMT_BGR16:
|
2001-11-11 01:44:54 +00:00
|
|
|
return(VIDEO_PALETTE_RGB565);
|
2002-08-21 21:31:20 +00:00
|
|
|
case IMGFMT_BGR24:
|
2001-11-11 01:44:54 +00:00
|
|
|
return(VIDEO_PALETTE_RGB24);
|
2002-08-21 21:31:20 +00:00
|
|
|
case IMGFMT_BGR32:
|
2001-11-11 01:44:54 +00:00
|
|
|
return(VIDEO_PALETTE_RGB32);
|
|
|
|
case IMGFMT_YV12:
|
2001-12-24 11:01:16 +00:00
|
|
|
case IMGFMT_I420:
|
2001-11-11 01:44:54 +00:00
|
|
|
return(VIDEO_PALETTE_YUV420P);
|
2001-12-27 23:52:48 +00:00
|
|
|
case IMGFMT_YUY2:
|
2002-08-21 21:31:20 +00:00
|
|
|
return(VIDEO_PALETTE_YUV422);
|
2001-11-11 01:44:54 +00:00
|
|
|
}
|
|
|
|
return(-1);
|
2001-11-10 23:32:10 +00:00
|
|
|
}
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
// sets and sanitizes audio buffer/block sizes
|
|
|
|
static void setup_audio_buffer_sizes(priv_t *priv)
|
|
|
|
{
|
|
|
|
int bytes_per_sample = priv->audio_in.bytes_per_sample;
|
|
|
|
|
|
|
|
// make the audio buffer at least 5 seconds long
|
|
|
|
priv->audio_buffer_size = 1 + 5*priv->audio_in.samplerate
|
|
|
|
*priv->audio_in.channels
|
|
|
|
*bytes_per_sample/priv->audio_in.blocksize;
|
|
|
|
if (priv->audio_buffer_size < 256) priv->audio_buffer_size = 256;
|
|
|
|
|
|
|
|
// make the skew buffer at least 1 second long
|
|
|
|
priv->aud_skew_cnt = 1 + 1*priv->audio_in.samplerate
|
|
|
|
*priv->audio_in.channels
|
|
|
|
*bytes_per_sample/priv->audio_in.blocksize;
|
|
|
|
if (priv->aud_skew_cnt < 16) priv->aud_skew_cnt = 16;
|
|
|
|
|
|
|
|
mp_msg(MSGT_TV, MSGL_V, "Audio capture - buffer %d blocks of %d bytes, skew average from %d meas.\n",
|
|
|
|
priv->audio_buffer_size, priv->audio_in.blocksize, priv->aud_skew_cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
tvi_handle_t *tvi_init_v4l(char *device, char *adevice)
|
2001-11-09 23:46:06 +00:00
|
|
|
{
|
|
|
|
tvi_handle_t *h;
|
|
|
|
priv_t *priv;
|
|
|
|
|
|
|
|
h = new_handle();
|
|
|
|
if (!h)
|
|
|
|
return(NULL);
|
|
|
|
|
|
|
|
priv = h->priv;
|
|
|
|
|
2001-11-10 23:32:10 +00:00
|
|
|
/* set video device name */
|
2001-11-09 23:46:06 +00:00
|
|
|
if (!device)
|
2003-02-02 02:36:35 +00:00
|
|
|
priv->video_device = strdup("/dev/video0");
|
2001-11-09 23:46:06 +00:00
|
|
|
else
|
2001-12-19 12:54:06 +00:00
|
|
|
priv->video_device = strdup(device);
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
/* set video device name */
|
|
|
|
if (!adevice)
|
|
|
|
priv->audio_device = NULL;
|
|
|
|
else {
|
|
|
|
priv->audio_device = strdup(adevice);
|
|
|
|
}
|
|
|
|
|
2001-12-19 12:54:06 +00:00
|
|
|
/* allocation failed */
|
|
|
|
if (!priv->video_device) {
|
|
|
|
free_handle(h);
|
|
|
|
return(NULL);
|
2001-11-09 23:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return(h);
|
|
|
|
}
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
/* retrieves info about audio channels from the BTTV */
|
|
|
|
static void init_v4l_audio(priv_t *priv)
|
|
|
|
{
|
|
|
|
int i;
|
2002-08-30 11:37:42 +00:00
|
|
|
int reqmode;
|
2002-08-21 21:31:20 +00:00
|
|
|
|
|
|
|
if (!priv->capability.audios) return;
|
|
|
|
|
|
|
|
/* audio chanlist */
|
|
|
|
|
2002-09-08 16:04:21 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_V, " Audio devices: %d\n", priv->capability.audios);
|
2002-08-21 21:31:20 +00:00
|
|
|
|
2002-09-28 18:49:47 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_V, "Video capture card reports the audio setup as follows:\n");
|
2002-08-21 21:31:20 +00:00
|
|
|
for (i = 0; i < priv->capability.audios; i++)
|
|
|
|
{
|
|
|
|
if (i >= MAX_AUDIO_CHANNELS)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "no space for more audio channels (increase in source!) (%d > %d)\n",
|
|
|
|
i, MAX_AUDIO_CHANNELS);
|
|
|
|
i = priv->capability.audios;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->audio[i].audio = i;
|
|
|
|
if (ioctl(priv->video_fd, VIDIOCGAUDIO, &priv->audio[i]) == -1)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl get audio failed: %s\n", strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mute all channels */
|
|
|
|
priv->audio[i].volume = 0;
|
|
|
|
priv->audio[i].flags |= VIDEO_AUDIO_MUTE;
|
2002-09-28 18:49:47 +00:00
|
|
|
reqmode = -1;
|
2002-08-30 11:37:42 +00:00
|
|
|
if (tv_param_amode >= 0) {
|
|
|
|
switch (tv_param_amode) {
|
|
|
|
case 0:
|
|
|
|
reqmode = VIDEO_SOUND_MONO;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
reqmode = VIDEO_SOUND_STEREO;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
reqmode = VIDEO_SOUND_LANG1;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
reqmode = VIDEO_SOUND_LANG2;
|
|
|
|
break;
|
2002-09-28 18:49:47 +00:00
|
|
|
default:
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "Unknown audio mode requested.\n");
|
|
|
|
break;
|
2002-08-30 11:37:42 +00:00
|
|
|
}
|
2002-09-28 18:49:47 +00:00
|
|
|
if (reqmode >= 0) priv->audio[i].mode = reqmode;
|
2002-08-21 21:31:20 +00:00
|
|
|
}
|
2002-08-30 11:37:42 +00:00
|
|
|
ioctl(priv->video_fd, VIDIOCSAUDIO, &priv->audio[i]);
|
2002-08-21 21:31:20 +00:00
|
|
|
|
2002-08-30 11:37:42 +00:00
|
|
|
// get the parameters back
|
2002-08-21 21:31:20 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCGAUDIO, &priv->audio[i]) == -1)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl get audio failed: %s\n", strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(priv->audio[i].mode)
|
|
|
|
{
|
|
|
|
case VIDEO_SOUND_MONO:
|
|
|
|
case VIDEO_SOUND_LANG1:
|
|
|
|
case VIDEO_SOUND_LANG2:
|
|
|
|
priv->audio_channels[i] = 1;
|
|
|
|
break;
|
|
|
|
case VIDEO_SOUND_STEREO:
|
|
|
|
priv->audio_channels[i] = 2;
|
|
|
|
break;
|
2002-12-03 13:24:10 +00:00
|
|
|
default:
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "Card reports an unknown audio mode !\n");
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "Trying two channel audio. Use forcechan to override.\n");
|
|
|
|
priv->audio_channels[i] = 2;
|
|
|
|
break;
|
2002-08-21 21:31:20 +00:00
|
|
|
}
|
|
|
|
|
2002-09-28 18:49:47 +00:00
|
|
|
if (reqmode >= 0 && priv->audio[i].mode != reqmode) {
|
2002-08-30 11:37:42 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "Audio mode setup warning!\n");
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "Requested mode was %s, but v4l still reports %s.\n",
|
|
|
|
audio_mode2name(reqmode), audio_mode2name(priv->audio[i].mode));
|
2002-09-28 18:49:47 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "You may need \"forcechan\" option to force stereo/mono audio recording.\n");
|
2002-08-30 11:37:42 +00:00
|
|
|
}
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
/* display stuff */
|
|
|
|
mp_msg(MSGT_TV, MSGL_V, " %d: %s: ", priv->audio[i].audio,
|
|
|
|
priv->audio[i].name);
|
|
|
|
if (priv->audio[i].flags & VIDEO_AUDIO_MUTABLE) {
|
|
|
|
mp_msg(MSGT_TV, MSGL_V, "muted=%s ",
|
|
|
|
(priv->audio[i].flags & VIDEO_AUDIO_MUTE) ? "yes" : "no");
|
|
|
|
}
|
2002-09-28 18:49:47 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_V, "vol=%d bass=%d treble=%d balance=%d mode=%s",
|
2002-08-21 21:31:20 +00:00
|
|
|
priv->audio[i].volume, priv->audio[i].bass, priv->audio[i].treble,
|
2002-08-30 11:37:42 +00:00
|
|
|
priv->audio[i].balance, audio_mode2name(priv->audio[i].mode));
|
2002-09-28 18:49:47 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_V, " chan=%d\n", priv->audio_channels[i]);
|
2002-08-30 11:37:42 +00:00
|
|
|
|
|
|
|
if (tv_param_forcechan >= 0)
|
|
|
|
priv->audio_channels[i] = tv_param_forcechan;
|
|
|
|
|
|
|
|
// we'll call VIDIOCSAUDIO again when starting capture
|
|
|
|
// let's set audio mode to requested mode again for the case
|
|
|
|
// when VIDIOCGAUDIO just cannot report the mode correctly
|
2002-09-28 18:49:47 +00:00
|
|
|
if (reqmode >= 0) priv->audio[i].mode = reqmode;
|
2002-08-21 21:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-27 23:52:48 +00:00
|
|
|
static int init(priv_t *priv)
|
2001-11-09 23:46:06 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
if (tv_param_immediate == 1)
|
|
|
|
tv_param_noaudio = 1;
|
|
|
|
|
|
|
|
priv->video_ringbuffer = NULL;
|
|
|
|
priv->video_timebuffer = NULL;
|
|
|
|
priv->audio_ringbuffer = NULL;
|
|
|
|
priv->audio_skew_buffer = NULL;
|
|
|
|
|
2001-12-27 23:52:48 +00:00
|
|
|
priv->video_fd = open(priv->video_device, O_RDWR);
|
2002-03-15 16:08:14 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_DBG2, "Video fd: %d, %x\n", priv->video_fd,
|
|
|
|
priv->video_device);
|
2001-12-27 23:52:48 +00:00
|
|
|
if (priv->video_fd == -1)
|
2001-11-09 23:46:06 +00:00
|
|
|
{
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "unable to open '%s': %s\n",
|
2001-11-10 23:32:10 +00:00
|
|
|
priv->video_device, strerror(errno));
|
2001-11-09 23:46:06 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2002-05-02 17:12:44 +00:00
|
|
|
|
2001-11-10 23:32:10 +00:00
|
|
|
/* get capabilities (priv->capability is needed!) */
|
2001-12-27 23:52:48 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCGCAP, &priv->capability) == -1)
|
2001-11-09 23:46:06 +00:00
|
|
|
{
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl get capabilites failed: %s\n", strerror(errno));
|
2001-11-09 23:46:06 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2001-12-27 23:52:48 +00:00
|
|
|
fcntl(priv->video_fd, F_SETFD, FD_CLOEXEC);
|
2001-11-10 23:32:10 +00:00
|
|
|
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_INFO, "Selected device: %s\n", priv->capability.name);
|
|
|
|
mp_msg(MSGT_TV, MSGL_INFO, " Capabilites: ");
|
2001-11-11 16:28:12 +00:00
|
|
|
for (i = 0; device_cap2name[i] != NULL; i++)
|
2001-11-09 23:46:06 +00:00
|
|
|
if (priv->capability.type & (1 << i))
|
2001-11-11 16:28:12 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_INFO, "%s ", device_cap2name[i]);
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_INFO, "\n");
|
|
|
|
mp_msg(MSGT_TV, MSGL_INFO, " Device type: %d\n", priv->capability.type);
|
|
|
|
mp_msg(MSGT_TV, MSGL_INFO, " Supported sizes: %dx%d => %dx%d\n",
|
2001-11-09 23:46:06 +00:00
|
|
|
priv->capability.minwidth, priv->capability.minheight,
|
|
|
|
priv->capability.maxwidth, priv->capability.maxheight);
|
|
|
|
priv->width = priv->capability.minwidth;
|
|
|
|
priv->height = priv->capability.minheight;
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_INFO, " Inputs: %d\n", priv->capability.channels);
|
2001-11-09 23:46:06 +00:00
|
|
|
|
2001-11-11 03:53:42 +00:00
|
|
|
priv->channels = (struct video_channel *)malloc(sizeof(struct video_channel)*priv->capability.channels);
|
2001-12-19 12:54:06 +00:00
|
|
|
if (!priv->channels)
|
|
|
|
goto malloc_failed;
|
2001-11-09 23:46:06 +00:00
|
|
|
memset(priv->channels, 0, sizeof(struct video_channel)*priv->capability.channels);
|
|
|
|
for (i = 0; i < priv->capability.channels; i++)
|
|
|
|
{
|
|
|
|
priv->channels[i].channel = i;
|
2001-12-27 23:52:48 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCGCHAN, &priv->channels[i]) == -1)
|
2001-11-11 16:28:12 +00:00
|
|
|
{
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl get channel failed: %s\n", strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
2002-09-28 18:49:47 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_INFO, " %d: %s: %s%s%s%s (tuner:%d, norm:%s)\n", i,
|
2001-11-09 23:46:06 +00:00
|
|
|
priv->channels[i].name,
|
|
|
|
(priv->channels[i].flags & VIDEO_VC_TUNER) ? "tuner " : "",
|
|
|
|
(priv->channels[i].flags & VIDEO_VC_AUDIO) ? "audio " : "",
|
|
|
|
(priv->channels[i].flags & VIDEO_TYPE_TV) ? "tv " : "",
|
2001-11-10 23:32:10 +00:00
|
|
|
(priv->channels[i].flags & VIDEO_TYPE_CAMERA) ? "camera " : "",
|
|
|
|
priv->channels[i].tuners,
|
2002-09-28 18:49:47 +00:00
|
|
|
norm2name(priv->channels[i].norm));
|
2001-11-09 23:46:06 +00:00
|
|
|
}
|
2002-09-28 18:49:47 +00:00
|
|
|
priv->act_channel = 0;
|
2001-11-09 23:46:06 +00:00
|
|
|
|
2001-11-10 23:32:10 +00:00
|
|
|
if (!(priv->capability.type & VID_TYPE_CAPTURE))
|
2001-11-09 23:46:06 +00:00
|
|
|
{
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "Only grabbing supported (for overlay use another program)\n");
|
2001-11-10 23:32:10 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* map grab buffer */
|
2001-12-27 23:52:48 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCGMBUF, &priv->mbuf) == -1)
|
2001-11-10 23:32:10 +00:00
|
|
|
{
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl get mbuf failed: %s\n", strerror(errno));
|
2001-11-10 23:32:10 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2001-11-09 23:46:06 +00:00
|
|
|
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_V, "mbuf: size=%d, frames=%d\n",
|
2001-11-10 23:32:10 +00:00
|
|
|
priv->mbuf.size, priv->mbuf.frames);
|
|
|
|
priv->mmap = mmap(0, priv->mbuf.size, PROT_READ|PROT_WRITE,
|
2001-12-27 23:52:48 +00:00
|
|
|
MAP_SHARED, priv->video_fd, 0);
|
2001-11-11 03:53:42 +00:00
|
|
|
if (priv->mmap == (unsigned char *)-1)
|
2001-11-10 23:32:10 +00:00
|
|
|
{
|
2001-12-27 23:52:48 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "Unable to map memory for buffers: %s\n", strerror(errno));
|
2001-11-10 23:32:10 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_DBG2, "our buffer: %p\n", priv->mmap);
|
2001-11-10 23:32:10 +00:00
|
|
|
|
|
|
|
/* num of buffers */
|
|
|
|
priv->nbuf = priv->mbuf.frames;
|
2001-11-09 23:46:06 +00:00
|
|
|
|
2001-11-10 23:32:10 +00:00
|
|
|
/* video buffers */
|
2001-11-11 03:53:42 +00:00
|
|
|
priv->buf = (struct video_mmap *)malloc(priv->nbuf * sizeof(struct video_mmap));
|
2001-12-19 12:54:06 +00:00
|
|
|
if (!priv->buf)
|
|
|
|
goto malloc_failed;
|
2001-11-10 23:32:10 +00:00
|
|
|
memset(priv->buf, 0, priv->nbuf * sizeof(struct video_mmap));
|
2001-11-09 23:46:06 +00:00
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
/* init v4l audio even when we don't capture */
|
|
|
|
init_v4l_audio(priv);
|
2001-12-27 23:52:48 +00:00
|
|
|
|
2002-10-22 07:56:13 +00:00
|
|
|
if (!priv->capability.audios && !tv_param_force_audio) tv_param_noaudio = 1;
|
2001-12-27 23:52:48 +00:00
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
/* audio init */
|
|
|
|
if (!tv_param_noaudio) {
|
2001-12-27 23:52:48 +00:00
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
#ifdef HAVE_ALSA9
|
|
|
|
if (tv_param_alsa)
|
|
|
|
audio_in_init(&priv->audio_in, AUDIO_IN_ALSA);
|
2001-12-27 23:52:48 +00:00
|
|
|
else
|
2002-08-21 21:31:20 +00:00
|
|
|
audio_in_init(&priv->audio_in, AUDIO_IN_OSS);
|
|
|
|
#else
|
|
|
|
audio_in_init(&priv->audio_in, AUDIO_IN_OSS);
|
2001-12-27 23:52:48 +00:00
|
|
|
#endif
|
|
|
|
|
2002-08-22 23:05:58 +00:00
|
|
|
if (priv->audio_device) {
|
|
|
|
audio_in_set_device(&priv->audio_in, priv->audio_device);
|
|
|
|
}
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
if (tv_param_audio_id < priv->capability.audios)
|
|
|
|
priv->audio_id = tv_param_audio_id;
|
|
|
|
else
|
|
|
|
priv->audio_id = 0;
|
|
|
|
audio_in_set_samplerate(&priv->audio_in, 44100);
|
2002-10-22 15:33:41 +00:00
|
|
|
if (priv->capability.audios) {
|
|
|
|
audio_in_set_channels(&priv->audio_in, priv->audio_channels[priv->audio_id]);
|
|
|
|
} else {
|
|
|
|
if (tv_param_forcechan >= 0) {
|
|
|
|
audio_in_set_channels(&priv->audio_in, tv_param_forcechan);
|
|
|
|
} else {
|
|
|
|
audio_in_set_channels(&priv->audio_in, 2);
|
|
|
|
}
|
|
|
|
}
|
2002-08-21 21:31:20 +00:00
|
|
|
if (audio_in_setup(&priv->audio_in) < 0) return 0;
|
|
|
|
setup_audio_buffer_sizes(priv);
|
2001-12-27 23:52:48 +00:00
|
|
|
}
|
2001-11-09 23:46:06 +00:00
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
return(1);
|
2001-12-19 12:54:06 +00:00
|
|
|
|
|
|
|
malloc_failed:
|
|
|
|
if (priv->channels)
|
|
|
|
free(priv->channels);
|
|
|
|
if (priv->buf)
|
|
|
|
free(priv->buf);
|
2001-11-09 23:46:06 +00:00
|
|
|
err:
|
2001-12-27 23:52:48 +00:00
|
|
|
if (priv->video_fd != -1)
|
|
|
|
close(priv->video_fd);
|
2001-11-09 23:46:06 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2001-11-10 23:32:10 +00:00
|
|
|
static int uninit(priv_t *priv)
|
2001-11-09 23:46:06 +00:00
|
|
|
{
|
2002-08-21 21:31:20 +00:00
|
|
|
priv->shutdown = 1;
|
|
|
|
|
2002-09-08 16:04:21 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_V, "Waiting for threads to finish... ");
|
2002-08-21 21:31:20 +00:00
|
|
|
if (!tv_param_noaudio) {
|
|
|
|
pthread_join(priv->audio_grabber_thread, NULL);
|
|
|
|
pthread_mutex_destroy(&priv->audio_starter);
|
|
|
|
pthread_mutex_destroy(&priv->skew_mutex);
|
|
|
|
}
|
2002-12-10 09:11:42 +00:00
|
|
|
pthread_mutex_destroy(&priv->video_buffer_mutex);
|
2002-08-21 21:31:20 +00:00
|
|
|
pthread_join(priv->video_grabber_thread, NULL);
|
2002-09-08 16:04:21 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_V, "done\n");
|
2001-12-27 23:52:48 +00:00
|
|
|
|
2003-01-01 20:21:45 +00:00
|
|
|
if (priv->capability.audios) {
|
|
|
|
priv->audio[priv->audio_id].volume = 0;
|
|
|
|
priv->audio[priv->audio_id].flags |= VIDEO_AUDIO_MUTE;
|
|
|
|
ioctl(priv->video_fd, VIDIOCSAUDIO, &priv->audio[priv->audio_id]);
|
|
|
|
}
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
close(priv->video_fd);
|
|
|
|
|
|
|
|
audio_in_uninit(&priv->audio_in);
|
|
|
|
|
2002-12-10 09:11:42 +00:00
|
|
|
if (priv->video_ringbuffer) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < priv->video_buffer_size_current; i++) {
|
|
|
|
free(priv->video_ringbuffer[i]);
|
|
|
|
}
|
2002-08-21 21:31:20 +00:00
|
|
|
free(priv->video_ringbuffer);
|
2002-12-10 09:11:42 +00:00
|
|
|
}
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
if (priv->video_timebuffer)
|
|
|
|
free(priv->video_timebuffer);
|
|
|
|
if (!tv_param_noaudio) {
|
|
|
|
if (priv->audio_ringbuffer)
|
|
|
|
free(priv->audio_ringbuffer);
|
|
|
|
if (priv->audio_skew_buffer)
|
|
|
|
free(priv->audio_skew_buffer);
|
|
|
|
}
|
2001-11-16 21:30:10 +00:00
|
|
|
|
|
|
|
return(1);
|
2001-11-09 23:46:06 +00:00
|
|
|
}
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
static int get_capture_buffer_size(priv_t *priv)
|
|
|
|
{
|
|
|
|
int bufsize, cnt;
|
2002-10-25 09:42:41 +00:00
|
|
|
|
2002-10-29 10:03:58 +00:00
|
|
|
if (tv_param_buffer_size >= 0) {
|
2002-10-25 09:42:41 +00:00
|
|
|
bufsize = tv_param_buffer_size*1024*1024;
|
2002-08-21 21:31:20 +00:00
|
|
|
} else {
|
2002-10-25 09:42:41 +00:00
|
|
|
#ifdef HAVE_SYS_SYSINFO_H
|
|
|
|
struct sysinfo si;
|
|
|
|
|
|
|
|
sysinfo(&si);
|
|
|
|
if (si.totalram<2*1024*1024) {
|
|
|
|
bufsize = 1024*1024;
|
|
|
|
} else {
|
|
|
|
bufsize = si.totalram/2;
|
|
|
|
}
|
2002-08-21 21:31:20 +00:00
|
|
|
#else
|
2002-10-25 09:42:41 +00:00
|
|
|
bufsize = 16*1024*1024;
|
2002-08-21 21:31:20 +00:00
|
|
|
#endif
|
2002-10-25 09:42:41 +00:00
|
|
|
}
|
|
|
|
|
2002-09-28 18:49:47 +00:00
|
|
|
cnt = bufsize/(priv->height*priv->bytesperline);
|
2002-08-21 21:31:20 +00:00
|
|
|
if (cnt < 2) cnt = 2;
|
|
|
|
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2001-11-10 23:32:10 +00:00
|
|
|
static int start(priv_t *priv)
|
2001-11-09 23:46:06 +00:00
|
|
|
{
|
2001-11-10 23:32:10 +00:00
|
|
|
int i;
|
2002-08-21 21:31:20 +00:00
|
|
|
int bytes_per_sample;
|
2001-11-10 23:32:10 +00:00
|
|
|
|
2001-12-27 23:52:48 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCGPICT, &priv->picture) == -1)
|
2001-11-09 23:46:06 +00:00
|
|
|
{
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl get picture failed: %s\n", strerror(errno));
|
2001-11-10 23:32:10 +00:00
|
|
|
return(0);
|
2001-11-09 23:46:06 +00:00
|
|
|
}
|
|
|
|
|
2001-11-10 23:32:10 +00:00
|
|
|
priv->picture.palette = format2palette(priv->format);
|
|
|
|
priv->picture.depth = palette2depth(priv->picture.palette);
|
2001-12-27 23:52:48 +00:00
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
if (priv->format != IMGFMT_BGR15) {
|
|
|
|
priv->bytesperline = priv->width * priv->picture.depth / 8;
|
|
|
|
} else {
|
|
|
|
priv->bytesperline = priv->width * 2;
|
|
|
|
}
|
2001-12-27 23:52:48 +00:00
|
|
|
|
2002-09-08 16:04:21 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_V, "Picture values:\n");
|
2002-09-28 18:49:47 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_V, " Depth: %d, Palette: %s (Format: %s)\n", priv->picture.depth,
|
|
|
|
PALETTE(priv->picture.palette), vo_format_name(priv->format));
|
2002-09-08 16:04:21 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_V, " Brightness: %d, Hue: %d, Colour: %d, Contrast: %d\n",
|
2001-11-10 23:32:10 +00:00
|
|
|
priv->picture.brightness, priv->picture.hue,
|
|
|
|
priv->picture.colour, priv->picture.contrast);
|
|
|
|
|
|
|
|
|
2001-12-27 23:52:48 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCSPICT, &priv->picture) == -1)
|
2001-11-09 23:46:06 +00:00
|
|
|
{
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl set picture failed: %s\n", strerror(errno));
|
2001-11-10 23:32:10 +00:00
|
|
|
return(0);
|
2001-11-09 23:46:06 +00:00
|
|
|
}
|
|
|
|
|
2001-11-10 23:32:10 +00:00
|
|
|
priv->nbuf = priv->mbuf.frames;
|
|
|
|
for (i=0; i < priv->nbuf; i++)
|
|
|
|
{
|
|
|
|
priv->buf[i].format = priv->picture.palette;
|
|
|
|
priv->buf[i].frame = i;
|
|
|
|
priv->buf[i].width = priv->width;
|
|
|
|
priv->buf[i].height = priv->height;
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_DBG2, "buffer: %d => %p\n", i, &priv->buf[i]);
|
2001-11-10 23:32:10 +00:00
|
|
|
}
|
2001-11-11 15:45:23 +00:00
|
|
|
|
2001-11-16 21:30:10 +00:00
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
struct video_play_mode pmode;
|
|
|
|
|
|
|
|
pmode.mode = VID_PLAY_NORMAL;
|
|
|
|
pmode.p1 = 1;
|
|
|
|
pmode.p2 = 0;
|
2001-12-27 23:52:48 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCSPLAYMODE, &pmode) == -1)
|
2001-11-16 21:30:10 +00:00
|
|
|
{
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl set play mode failed: %s\n", strerror(errno));
|
|
|
|
// return(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2001-11-11 15:45:23 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
struct video_window win;
|
|
|
|
|
|
|
|
win.x = 0;
|
|
|
|
win.y = 0;
|
|
|
|
win.width = priv->width;
|
|
|
|
win.height = priv->height;
|
|
|
|
win.chromakey = -1;
|
|
|
|
win.flags = 0;
|
2002-03-15 16:08:14 +00:00
|
|
|
//win.clipcount = 0;
|
2001-11-11 15:45:23 +00:00
|
|
|
|
2001-12-27 23:52:48 +00:00
|
|
|
ioctl(priv->video_fd, VIDIOCSWIN, &win);
|
2001-11-11 15:45:23 +00:00
|
|
|
}
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
// initialize video capture
|
2001-12-27 23:52:48 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCCAPTURE, &one) == -1)
|
2001-11-10 23:32:10 +00:00
|
|
|
{
|
2002-08-21 21:31:20 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "FATAL: ioctl ccapture failed: %s\n", strerror(errno));
|
2001-11-10 23:32:10 +00:00
|
|
|
return(0);
|
|
|
|
}
|
2001-11-11 15:45:23 +00:00
|
|
|
#endif
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
/* setup audio parameters */
|
|
|
|
if (!tv_param_noaudio) {
|
|
|
|
setup_audio_buffer_sizes(priv);
|
|
|
|
bytes_per_sample = priv->audio_in.bytes_per_sample;
|
2002-11-08 22:05:39 +00:00
|
|
|
priv->audio_skew_buffer = (long long*)malloc(sizeof(long long)*priv->aud_skew_cnt);
|
2002-08-21 21:31:20 +00:00
|
|
|
if (!priv->audio_skew_buffer) {
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate skew buffer: %s\n", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->audio_ringbuffer = (unsigned char*)malloc(priv->audio_in.blocksize*priv->audio_buffer_size);
|
|
|
|
if (!priv->audio_ringbuffer) {
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate audio buffer: %s\n", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->audio_secs_per_block = (double)priv->audio_in.blocksize/(priv->audio_in.samplerate
|
|
|
|
*priv->audio_in.channels
|
|
|
|
*bytes_per_sample);
|
|
|
|
priv->audio_head = 0;
|
|
|
|
priv->audio_tail = 0;
|
|
|
|
priv->audio_cnt = 0;
|
|
|
|
priv->audio_drop = 0;
|
2002-11-08 22:05:39 +00:00
|
|
|
priv->audio_skew = 0;
|
|
|
|
priv->audio_skew_total = 0;
|
2002-08-21 21:31:20 +00:00
|
|
|
priv->audio_recv_blocks_total = 0;
|
|
|
|
priv->audio_sent_blocks_total = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup video parameters */
|
|
|
|
if (priv->immediate_mode) {
|
2002-12-10 09:11:42 +00:00
|
|
|
priv->video_buffer_size_max = VID_BUF_SIZE_IMMEDIATE;
|
2002-08-21 21:31:20 +00:00
|
|
|
} else {
|
2002-12-10 09:11:42 +00:00
|
|
|
priv->video_buffer_size_max = get_capture_buffer_size(priv);
|
2002-08-21 21:31:20 +00:00
|
|
|
}
|
2002-12-10 09:11:42 +00:00
|
|
|
priv->video_buffer_size_current = 0;
|
2002-08-21 21:31:20 +00:00
|
|
|
|
|
|
|
if (!tv_param_noaudio) {
|
2002-12-10 09:11:42 +00:00
|
|
|
if (priv->video_buffer_size_max < 3.0*priv->fps*priv->audio_secs_per_block) {
|
2002-08-21 21:31:20 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "Video buffer shorter than 3 times audio frame duration.\n"
|
|
|
|
"You will probably experience heavy framedrops.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-10 09:11:42 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_V, "Using a ring buffer for maximum %d frames, %d MB total size.\n",
|
|
|
|
priv->video_buffer_size_max,
|
|
|
|
priv->video_buffer_size_max*priv->height*priv->bytesperline/(1024*1024));
|
2002-09-28 18:49:47 +00:00
|
|
|
|
2002-12-10 09:11:42 +00:00
|
|
|
priv->video_ringbuffer = (unsigned char**)malloc(priv->video_buffer_size_max*sizeof(unsigned char*));
|
2002-08-21 21:31:20 +00:00
|
|
|
if (!priv->video_ringbuffer) {
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate video buffer: %s\n", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
2002-12-10 09:11:42 +00:00
|
|
|
for (i = 0; i < priv->video_buffer_size_max; i++)
|
|
|
|
priv->video_ringbuffer[i] = NULL;
|
|
|
|
|
|
|
|
priv->video_timebuffer = (long long*)malloc(sizeof(long long) * priv->video_buffer_size_max);
|
2002-08-21 21:31:20 +00:00
|
|
|
if (!priv->video_timebuffer) {
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate time buffer: %s\n", strerror(errno));
|
|
|
|
return 0;
|
2002-06-24 16:37:18 +00:00
|
|
|
}
|
2002-08-21 21:31:20 +00:00
|
|
|
priv->video_head = 0;
|
|
|
|
priv->video_tail = 0;
|
|
|
|
priv->video_cnt = 0;
|
|
|
|
priv->first = 1;
|
|
|
|
|
2002-09-28 18:49:47 +00:00
|
|
|
if (priv->capability.audios) {
|
|
|
|
/* enable audio */
|
|
|
|
if (tv_param_volume >= 0)
|
|
|
|
priv->audio[priv->audio_id].volume = tv_param_volume;
|
|
|
|
if (tv_param_bass >= 0)
|
|
|
|
priv->audio[priv->audio_id].bass = tv_param_bass;
|
|
|
|
if (tv_param_treble >= 0)
|
|
|
|
priv->audio[priv->audio_id].treble = tv_param_treble;
|
|
|
|
if (tv_param_balance >= 0)
|
|
|
|
priv->audio[priv->audio_id].balance = tv_param_balance;
|
|
|
|
priv->audio[priv->audio_id].flags &= ~VIDEO_AUDIO_MUTE;
|
|
|
|
mp_msg(MSGT_TV, MSGL_V, "Enabling tv audio. Requested setup is:\n");
|
|
|
|
mp_msg(MSGT_TV, MSGL_V, "id=%d vol=%d bass=%d treble=%d balance=%d mode=%s",
|
|
|
|
priv->audio_id,
|
|
|
|
priv->audio[priv->audio_id].volume, priv->audio[priv->audio_id].bass, priv->audio[priv->audio_id].treble,
|
|
|
|
priv->audio[priv->audio_id].balance, audio_mode2name(priv->audio[priv->audio_id].mode));
|
|
|
|
mp_msg(MSGT_TV, MSGL_V, " chan=%d\n", priv->audio_channels[priv->audio_id]);
|
|
|
|
ioctl(priv->video_fd, VIDIOCSAUDIO, &priv->audio[priv->audio_id]);
|
|
|
|
}
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
/* launch capture threads */
|
|
|
|
priv->shutdown = 0;
|
|
|
|
if (!tv_param_noaudio) {
|
|
|
|
pthread_mutex_init(&priv->audio_starter, NULL);
|
|
|
|
pthread_mutex_init(&priv->skew_mutex, NULL);
|
|
|
|
pthread_mutex_lock(&priv->audio_starter);
|
|
|
|
pthread_create(&priv->audio_grabber_thread, NULL, audio_grabber, priv);
|
|
|
|
}
|
2002-12-10 09:11:42 +00:00
|
|
|
pthread_mutex_init(&priv->video_buffer_mutex, NULL);
|
2002-08-21 21:31:20 +00:00
|
|
|
/* we'll launch the video capture later, when a first request for a frame arrives */
|
2002-06-24 16:37:18 +00:00
|
|
|
|
2001-11-11 15:45:23 +00:00
|
|
|
return(1);
|
2001-11-09 23:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int control(priv_t *priv, int cmd, void *arg)
|
|
|
|
{
|
2002-08-21 21:31:20 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_DBG2, "\ndebug: control(priv=%p, cmd=%d, arg=%p)\n",
|
2001-11-10 23:32:10 +00:00
|
|
|
priv, cmd, arg);
|
2001-11-09 23:46:06 +00:00
|
|
|
switch(cmd)
|
|
|
|
{
|
2001-11-10 23:32:10 +00:00
|
|
|
/* ========== GENERIC controls =========== */
|
|
|
|
case TVI_CONTROL_IS_VIDEO:
|
|
|
|
{
|
|
|
|
if (priv->capability.type & VID_TYPE_CAPTURE)
|
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
return(TVI_CONTROL_FALSE);
|
|
|
|
}
|
|
|
|
case TVI_CONTROL_IS_AUDIO:
|
2001-11-11 16:28:12 +00:00
|
|
|
if (priv->channels[priv->act_channel].flags & VIDEO_VC_AUDIO)
|
2001-12-27 23:52:48 +00:00
|
|
|
{
|
2001-11-11 16:28:12 +00:00
|
|
|
return(TVI_CONTROL_TRUE);
|
2001-12-27 23:52:48 +00:00
|
|
|
}
|
2003-01-04 17:30:28 +00:00
|
|
|
return(TVI_CONTROL_FALSE);
|
2001-11-10 23:32:10 +00:00
|
|
|
case TVI_CONTROL_IS_TUNER:
|
|
|
|
{
|
2001-11-11 16:28:12 +00:00
|
|
|
// if (priv->capability.type & VID_TYPE_TUNER)
|
|
|
|
if (priv->channels[priv->act_channel].flags & VIDEO_VC_TUNER)
|
2001-11-10 23:32:10 +00:00
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
return(TVI_CONTROL_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ========== VIDEO controls =========== */
|
2001-11-09 23:46:06 +00:00
|
|
|
case TVI_CONTROL_VID_GET_FORMAT:
|
2001-11-10 23:32:10 +00:00
|
|
|
{
|
|
|
|
int output_fmt = -1;
|
|
|
|
|
|
|
|
output_fmt = priv->format;
|
|
|
|
(int)*(void **)arg = output_fmt;
|
2002-09-08 16:04:21 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_V, "Output format: %s\n", vo_format_name(output_fmt));
|
2001-11-10 23:32:10 +00:00
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
}
|
|
|
|
case TVI_CONTROL_VID_SET_FORMAT:
|
|
|
|
priv->format = (int)*(void **)arg;
|
2002-08-21 21:31:20 +00:00
|
|
|
// !HACK! v4l uses BGR format instead of RGB
|
|
|
|
// and we have to correct this. Fortunately,
|
|
|
|
// tv.c reads later the format back so we
|
|
|
|
// can persuade it to use what we want.
|
|
|
|
if (IMGFMT_IS_RGB(priv->format)) {
|
|
|
|
priv->format &= ~IMGFMT_RGB_MASK;
|
|
|
|
priv->format |= IMGFMT_BGR;
|
|
|
|
}
|
2001-11-09 23:46:06 +00:00
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
case TVI_CONTROL_VID_GET_PLANES:
|
2001-11-30 16:59:02 +00:00
|
|
|
(int)*(void **)arg = 1; /* FIXME, also not needed at this time */
|
2001-11-09 23:46:06 +00:00
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
case TVI_CONTROL_VID_GET_BITS:
|
2001-11-11 01:44:54 +00:00
|
|
|
(int)*(void **)arg = palette2depth(format2palette(priv->format));
|
2001-11-09 23:46:06 +00:00
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
case TVI_CONTROL_VID_GET_WIDTH:
|
|
|
|
(int)*(void **)arg = priv->width;
|
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
case TVI_CONTROL_VID_CHK_WIDTH:
|
|
|
|
{
|
|
|
|
int req_width = (int)*(void **)arg;
|
|
|
|
|
2002-09-08 16:04:21 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_V, "Requested width: %d\n", req_width);
|
2001-11-11 01:44:54 +00:00
|
|
|
if ((req_width >= priv->capability.minwidth) &&
|
|
|
|
(req_width <= priv->capability.maxwidth))
|
2001-11-09 23:46:06 +00:00
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
return(TVI_CONTROL_FALSE);
|
|
|
|
}
|
|
|
|
case TVI_CONTROL_VID_SET_WIDTH:
|
|
|
|
priv->width = (int)*(void **)arg;
|
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
case TVI_CONTROL_VID_GET_HEIGHT:
|
|
|
|
(int)*(void **)arg = priv->height;
|
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
case TVI_CONTROL_VID_CHK_HEIGHT:
|
|
|
|
{
|
|
|
|
int req_height = (int)*(void **)arg;
|
|
|
|
|
2002-09-08 16:04:21 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_V, "Requested height: %d\n", req_height);
|
2001-11-11 01:44:54 +00:00
|
|
|
if ((req_height >= priv->capability.minheight) &&
|
|
|
|
(req_height <= priv->capability.maxheight))
|
2001-11-09 23:46:06 +00:00
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
return(TVI_CONTROL_FALSE);
|
|
|
|
}
|
|
|
|
case TVI_CONTROL_VID_SET_HEIGHT:
|
|
|
|
priv->height = (int)*(void **)arg;
|
|
|
|
return(TVI_CONTROL_TRUE);
|
2001-11-16 22:59:07 +00:00
|
|
|
case TVI_CONTROL_VID_GET_PICTURE:
|
2001-12-27 23:52:48 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCGPICT, &priv->picture) == -1)
|
2001-11-16 22:59:07 +00:00
|
|
|
{
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl get picture failed: %s\n", strerror(errno));
|
|
|
|
return(TVI_CONTROL_FALSE);
|
|
|
|
}
|
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
case TVI_CONTROL_VID_SET_PICTURE:
|
2001-12-27 23:52:48 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCSPICT, &priv->picture) == -1)
|
2001-11-16 22:59:07 +00:00
|
|
|
{
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl get picture failed: %s\n", strerror(errno));
|
|
|
|
return(TVI_CONTROL_FALSE);
|
|
|
|
}
|
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
case TVI_CONTROL_VID_SET_BRIGHTNESS:
|
|
|
|
priv->picture.brightness = (int)*(void **)arg;
|
|
|
|
control(priv, TVI_CONTROL_VID_SET_PICTURE, 0);
|
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
case TVI_CONTROL_VID_SET_HUE:
|
|
|
|
priv->picture.hue = (int)*(void **)arg;
|
|
|
|
control(priv, TVI_CONTROL_VID_SET_PICTURE, 0);
|
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
case TVI_CONTROL_VID_SET_SATURATION:
|
|
|
|
priv->picture.colour = (int)*(void **)arg;
|
|
|
|
control(priv, TVI_CONTROL_VID_SET_PICTURE, 0);
|
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
case TVI_CONTROL_VID_SET_CONTRAST:
|
|
|
|
priv->picture.contrast = (int)*(void **)arg;
|
|
|
|
control(priv, TVI_CONTROL_VID_SET_PICTURE, 0);
|
|
|
|
return(TVI_CONTROL_TRUE);
|
2002-06-23 17:28:44 +00:00
|
|
|
case TVI_CONTROL_VID_GET_FPS:
|
|
|
|
(int)*(void **)arg=priv->fps;
|
|
|
|
return(TVI_CONTROL_TRUE);
|
2001-11-09 23:46:06 +00:00
|
|
|
|
2001-11-10 23:32:10 +00:00
|
|
|
/* ========== TUNER controls =========== */
|
|
|
|
case TVI_CONTROL_TUN_GET_FREQ:
|
|
|
|
{
|
|
|
|
unsigned long freq;
|
|
|
|
|
2001-12-27 23:52:48 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCGFREQ, &freq) == -1)
|
2001-11-10 23:32:10 +00:00
|
|
|
{
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl get freq failed: %s\n", strerror(errno));
|
2001-11-10 23:32:10 +00:00
|
|
|
return(TVI_CONTROL_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tuner uses khz not mhz ! */
|
2001-11-11 15:45:23 +00:00
|
|
|
// if (priv->tuner.flags & VIDEO_TUNER_LOW)
|
|
|
|
// freq /= 1000;
|
2001-11-10 23:32:10 +00:00
|
|
|
(unsigned long)*(void **)arg = freq;
|
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
}
|
2001-11-09 23:46:06 +00:00
|
|
|
case TVI_CONTROL_TUN_SET_FREQ:
|
|
|
|
{
|
2001-11-10 23:32:10 +00:00
|
|
|
/* argument is in MHz ! */
|
|
|
|
unsigned long freq = (unsigned long)*(void **)arg;
|
2001-11-09 23:46:06 +00:00
|
|
|
|
2003-01-01 20:21:45 +00:00
|
|
|
if (priv->capability.audios) {
|
|
|
|
priv->audio[priv->audio_id].volume = 0;
|
|
|
|
priv->audio[priv->audio_id].flags |= VIDEO_AUDIO_MUTE;
|
|
|
|
ioctl(priv->video_fd, VIDIOCSAUDIO, &priv->audio[priv->audio_id]);
|
|
|
|
}
|
|
|
|
|
2001-11-11 15:45:23 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_V, "requested frequency: %.3f\n", (float)freq/16);
|
2001-11-10 23:32:10 +00:00
|
|
|
|
|
|
|
/* tuner uses khz not mhz ! */
|
2001-11-11 15:45:23 +00:00
|
|
|
// if (priv->tuner.flags & VIDEO_TUNER_LOW)
|
|
|
|
// freq *= 1000;
|
|
|
|
// mp_msg(MSGT_TV, MSGL_V, " requesting from driver: freq=%.3f\n", (float)freq/16);
|
2001-12-27 23:52:48 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCSFREQ, &freq) == -1)
|
2001-11-10 23:32:10 +00:00
|
|
|
{
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl set freq failed: %s\n", strerror(errno));
|
2001-11-10 23:32:10 +00:00
|
|
|
return(TVI_CONTROL_FALSE);
|
|
|
|
}
|
2002-08-21 21:31:20 +00:00
|
|
|
usleep(100000); // wait to supress noise during switching
|
2003-01-01 20:21:45 +00:00
|
|
|
|
|
|
|
if (priv->capability.audios) {
|
|
|
|
priv->audio[priv->audio_id].volume = tv_param_volume;
|
|
|
|
priv->audio[priv->audio_id].flags &= ~VIDEO_AUDIO_MUTE;
|
|
|
|
ioctl(priv->video_fd, VIDIOCSAUDIO, &priv->audio[priv->audio_id]);
|
|
|
|
}
|
|
|
|
|
2001-11-10 23:32:10 +00:00
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
}
|
|
|
|
case TVI_CONTROL_TUN_GET_TUNER:
|
|
|
|
{
|
2001-12-27 23:52:48 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCGTUNER, &priv->tuner) == -1)
|
2001-11-10 23:32:10 +00:00
|
|
|
{
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl get tuner failed: %s\n", strerror(errno));
|
2001-11-10 23:32:10 +00:00
|
|
|
return(TVI_CONTROL_FALSE);
|
|
|
|
}
|
|
|
|
|
2002-09-08 16:04:21 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_V, "Tuner (%s) range: %lu -> %lu\n", priv->tuner.name,
|
2001-11-10 23:32:10 +00:00
|
|
|
priv->tuner.rangelow, priv->tuner.rangehigh);
|
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
}
|
|
|
|
case TVI_CONTROL_TUN_SET_TUNER:
|
|
|
|
{
|
2001-12-27 23:52:48 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCSTUNER, &priv->tuner) == -1)
|
2001-11-10 23:32:10 +00:00
|
|
|
{
|
2002-08-21 21:31:20 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl set tuner failed: %s\n", strerror(errno));
|
2001-11-10 23:32:10 +00:00
|
|
|
return(TVI_CONTROL_FALSE);
|
|
|
|
}
|
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
}
|
|
|
|
case TVI_CONTROL_TUN_SET_NORM:
|
|
|
|
{
|
|
|
|
int req_mode = (int)*(void **)arg;
|
2002-08-21 21:31:20 +00:00
|
|
|
|
2002-12-16 17:05:49 +00:00
|
|
|
if ((req_mode != TV_NORM_PAL) && (req_mode != TV_NORM_NTSC) && (req_mode != TV_NORM_SECAM)
|
|
|
|
&& (req_mode != TV_NORM_PALNC) && (req_mode != TV_NORM_PALM) && (req_mode != TV_NORM_PALN)
|
|
|
|
&& (req_mode != TV_NORM_NTSCJP)) {
|
2002-08-21 21:31:20 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "Unknown norm!\n");
|
|
|
|
return(TVI_CONTROL_FALSE);
|
|
|
|
}
|
|
|
|
|
2002-09-28 18:49:47 +00:00
|
|
|
if (priv->channels[priv->act_channel].flags & VIDEO_VC_TUNER) {
|
2002-12-16 23:24:02 +00:00
|
|
|
int prev_mode;
|
|
|
|
|
2002-09-28 18:49:47 +00:00
|
|
|
control(priv, TVI_CONTROL_TUN_GET_TUNER, 0);
|
2002-12-16 17:05:49 +00:00
|
|
|
if (((req_mode == TV_NORM_PAL
|
|
|
|
|| req_mode == TV_NORM_PALNC
|
|
|
|
|| req_mode == TV_NORM_PALN) && !(priv->tuner.flags & VIDEO_TUNER_PAL)) ||
|
|
|
|
((req_mode == TV_NORM_NTSC
|
|
|
|
|| req_mode == TV_NORM_NTSCJP
|
|
|
|
|| req_mode == TV_NORM_PALM) && !(priv->tuner.flags & VIDEO_TUNER_NTSC)) ||
|
2002-09-28 18:49:47 +00:00
|
|
|
((req_mode == TV_NORM_SECAM) && !(priv->tuner.flags & VIDEO_TUNER_SECAM)))
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "Tuner isn't capable to set norm!\n");
|
|
|
|
return(TVI_CONTROL_FALSE);
|
|
|
|
}
|
|
|
|
|
2002-12-16 23:24:02 +00:00
|
|
|
prev_mode = priv->tuner.mode;
|
|
|
|
|
2002-09-28 18:49:47 +00:00
|
|
|
switch(req_mode) {
|
|
|
|
case TV_NORM_PAL:
|
2002-12-16 17:05:49 +00:00
|
|
|
case TV_NORM_PALNC:
|
|
|
|
case TV_NORM_PALN:
|
2002-09-28 18:49:47 +00:00
|
|
|
priv->tuner.mode = VIDEO_MODE_PAL;
|
|
|
|
break;
|
|
|
|
case TV_NORM_NTSC:
|
2002-12-16 17:05:49 +00:00
|
|
|
case TV_NORM_NTSCJP:
|
|
|
|
case TV_NORM_PALM:
|
2002-09-28 18:49:47 +00:00
|
|
|
priv->tuner.mode = VIDEO_MODE_NTSC;
|
|
|
|
break;
|
|
|
|
case TV_NORM_SECAM:
|
|
|
|
priv->tuner.mode = VIDEO_MODE_SECAM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (control(priv, TVI_CONTROL_TUN_SET_TUNER, &priv->tuner) != TVI_CONTROL_TRUE) {
|
2002-12-16 23:24:02 +00:00
|
|
|
// norm setting failed, but maybe it's only because it's fixed
|
|
|
|
if (priv->tuner.mode != prev_mode) return(TVI_CONTROL_FALSE); // no it really failed
|
2002-09-28 18:49:47 +00:00
|
|
|
}
|
|
|
|
|
2001-11-10 23:32:10 +00:00
|
|
|
}
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
switch(req_mode) {
|
|
|
|
case TV_NORM_PAL:
|
2002-09-28 18:49:47 +00:00
|
|
|
priv->channels[priv->act_channel].norm = VIDEO_MODE_PAL;
|
2002-08-21 21:31:20 +00:00
|
|
|
break;
|
|
|
|
case TV_NORM_NTSC:
|
2002-09-28 18:49:47 +00:00
|
|
|
priv->channels[priv->act_channel].norm = VIDEO_MODE_NTSC;
|
2002-08-21 21:31:20 +00:00
|
|
|
break;
|
|
|
|
case TV_NORM_SECAM:
|
2002-09-28 18:49:47 +00:00
|
|
|
priv->channels[priv->act_channel].norm = VIDEO_MODE_SECAM;
|
2002-08-21 21:31:20 +00:00
|
|
|
break;
|
2002-12-16 17:05:49 +00:00
|
|
|
case TV_NORM_PALNC:
|
|
|
|
priv->channels[priv->act_channel].norm = 3;
|
|
|
|
break;
|
|
|
|
case TV_NORM_PALM:
|
|
|
|
priv->channels[priv->act_channel].norm = 4;
|
|
|
|
break;
|
|
|
|
case TV_NORM_PALN:
|
|
|
|
priv->channels[priv->act_channel].norm = 5;
|
|
|
|
break;
|
|
|
|
case TV_NORM_NTSCJP:
|
|
|
|
priv->channels[priv->act_channel].norm = 6;
|
|
|
|
break;
|
2002-08-21 21:31:20 +00:00
|
|
|
}
|
2002-09-28 18:49:47 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCSCHAN, &priv->channels[priv->act_channel]) == -1)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl set chan failed: %s\n", strerror(errno));
|
2001-11-10 23:32:10 +00:00
|
|
|
return(TVI_CONTROL_FALSE);
|
2002-08-21 21:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(priv->video_fd, VIDIOCGCAP, &priv->capability) == -1) {
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl get capabilites failed: %s\n", strerror(errno));
|
|
|
|
return(TVI_CONTROL_FALSE);
|
|
|
|
}
|
|
|
|
|
2002-12-16 17:05:49 +00:00
|
|
|
if(req_mode == TV_NORM_PAL || req_mode == TV_NORM_SECAM || req_mode == TV_NORM_PALN || req_mode == TV_NORM_PALNC) {
|
2002-08-21 21:31:20 +00:00
|
|
|
priv->fps = PAL_FPS;
|
|
|
|
}
|
|
|
|
|
2002-12-16 17:05:49 +00:00
|
|
|
if(req_mode == TV_NORM_NTSC || req_mode == TV_NORM_NTSCJP || req_mode == TV_NORM_PALM) {
|
2002-08-21 21:31:20 +00:00
|
|
|
priv->fps = NTSC_FPS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(priv->height > priv->capability.maxheight) {
|
|
|
|
priv->height = priv->capability.maxheight;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(priv->width > priv->capability.maxwidth) {
|
|
|
|
priv->width = priv->capability.maxwidth;
|
|
|
|
}
|
|
|
|
|
2001-11-10 23:32:10 +00:00
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
}
|
|
|
|
case TVI_CONTROL_TUN_GET_NORM:
|
|
|
|
{
|
|
|
|
(int)*(void **)arg = priv->tuner.mode;
|
|
|
|
|
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ========== AUDIO controls =========== */
|
|
|
|
case TVI_CONTROL_AUD_GET_FORMAT:
|
|
|
|
{
|
2002-08-21 21:31:20 +00:00
|
|
|
(int)*(void **)arg = AFMT_S16_LE;
|
2001-11-10 23:32:10 +00:00
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
}
|
|
|
|
case TVI_CONTROL_AUD_GET_CHANNELS:
|
|
|
|
{
|
2002-08-21 21:31:20 +00:00
|
|
|
(int)*(void **)arg = priv->audio_in.channels;
|
2001-11-10 23:32:10 +00:00
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
}
|
|
|
|
case TVI_CONTROL_AUD_GET_SAMPLERATE:
|
|
|
|
{
|
2002-08-21 21:31:20 +00:00
|
|
|
(int)*(void **)arg = priv->audio_in.samplerate;
|
2001-11-10 23:32:10 +00:00
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
}
|
|
|
|
case TVI_CONTROL_AUD_GET_SAMPLESIZE:
|
|
|
|
{
|
2002-08-21 21:31:20 +00:00
|
|
|
(int)*(void **)arg = priv->audio_in.bytes_per_sample;
|
2002-05-02 17:12:44 +00:00
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
}
|
|
|
|
case TVI_CONTROL_AUD_SET_SAMPLERATE:
|
|
|
|
{
|
2002-08-21 21:31:20 +00:00
|
|
|
if (audio_in_set_samplerate(&priv->audio_in, (int)*(void **)arg) < 0) return TVI_CONTROL_FALSE;
|
|
|
|
setup_audio_buffer_sizes(priv);
|
2001-11-10 23:32:10 +00:00
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
}
|
|
|
|
/* ========== SPECIFIC controls =========== */
|
|
|
|
case TVI_CONTROL_SPC_GET_INPUT:
|
|
|
|
{
|
|
|
|
int req_chan = (int)*(void **)arg;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < priv->capability.channels; i++)
|
|
|
|
{
|
|
|
|
if (priv->channels[i].channel == req_chan)
|
|
|
|
break;
|
|
|
|
}
|
2001-11-11 16:28:12 +00:00
|
|
|
|
|
|
|
priv->act_channel = i;
|
2001-11-10 23:32:10 +00:00
|
|
|
|
2001-12-27 23:52:48 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCGCHAN, &priv->channels[i]) == -1)
|
2001-11-10 23:32:10 +00:00
|
|
|
{
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl get channel failed: %s\n", strerror(errno));
|
2001-11-10 23:32:10 +00:00
|
|
|
return(TVI_CONTROL_FALSE);
|
|
|
|
}
|
|
|
|
return(TVI_CONTROL_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
case TVI_CONTROL_SPC_SET_INPUT:
|
|
|
|
{
|
|
|
|
struct video_channel chan;
|
|
|
|
int req_chan = (int)*(void **)arg;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (req_chan >= priv->capability.channels)
|
|
|
|
{
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "Invalid input requested: %d, valid: 0-%d\n",
|
2003-02-02 02:28:59 +00:00
|
|
|
req_chan, priv->capability.channels - 1);
|
2001-11-10 23:32:10 +00:00
|
|
|
return(TVI_CONTROL_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < priv->capability.channels; i++)
|
|
|
|
{
|
|
|
|
if (priv->channels[i].channel == req_chan)
|
|
|
|
chan = priv->channels[i];
|
|
|
|
}
|
|
|
|
|
2001-12-27 23:52:48 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCSCHAN, &chan) == -1)
|
2001-11-10 23:32:10 +00:00
|
|
|
{
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "ioctl set chan failed: %s\n", strerror(errno));
|
2001-11-10 23:32:10 +00:00
|
|
|
return(TVI_CONTROL_FALSE);
|
|
|
|
}
|
2001-11-11 03:40:16 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_INFO, "Using input '%s'\n", chan.name);
|
2001-11-10 23:32:10 +00:00
|
|
|
|
2001-11-11 16:28:12 +00:00
|
|
|
priv->act_channel = i;
|
|
|
|
|
2001-11-10 23:32:10 +00:00
|
|
|
/* update tuner state */
|
2001-11-11 16:28:12 +00:00
|
|
|
// if (priv->capability.type & VID_TYPE_TUNER)
|
|
|
|
if (priv->channels[priv->act_channel].flags & VIDEO_VC_TUNER)
|
2001-11-10 23:32:10 +00:00
|
|
|
control(priv, TVI_CONTROL_TUN_GET_TUNER, 0);
|
|
|
|
|
|
|
|
/* update local channel list */
|
|
|
|
control(priv, TVI_CONTROL_SPC_GET_INPUT, &req_chan);
|
|
|
|
return(TVI_CONTROL_TRUE);
|
2002-08-21 21:31:20 +00:00
|
|
|
case TVI_CONTROL_IMMEDIATE:
|
|
|
|
priv->immediate_mode = 1;
|
|
|
|
return(TVI_CONTROL_TRUE);
|
2001-11-09 23:46:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(TVI_CONTROL_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
// copies a video frame
|
|
|
|
// for RGB (i.e. BGR in mplayer) flips the image upside down
|
|
|
|
// for YV12 swaps the 2nd and 3rd plane
|
|
|
|
static inline void copy_frame(priv_t *priv, unsigned char *dest, unsigned char *source)
|
2001-11-09 23:46:06 +00:00
|
|
|
{
|
2002-08-21 21:31:20 +00:00
|
|
|
int i;
|
|
|
|
unsigned char *sptr;
|
|
|
|
|
|
|
|
// YV12 uses VIDEO_PALETTE_YUV420P, but the planes are swapped
|
|
|
|
if (priv->format == IMGFMT_YV12) {
|
|
|
|
memcpy(dest, source, priv->width * priv->height);
|
|
|
|
memcpy(dest+priv->width * priv->height*5/4, source+priv->width * priv->height, priv->width * priv->height/4);
|
|
|
|
memcpy(dest+priv->width * priv->height, source+priv->width * priv->height*5/4, priv->width * priv->height/4);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (priv->picture.palette) {
|
|
|
|
case VIDEO_PALETTE_RGB24:
|
|
|
|
case VIDEO_PALETTE_RGB32:
|
|
|
|
case VIDEO_PALETTE_RGB555:
|
|
|
|
case VIDEO_PALETTE_RGB565:
|
|
|
|
sptr = source + (priv->height-1)*priv->bytesperline;
|
|
|
|
for (i = 0; i < priv->height; i++) {
|
|
|
|
memcpy(dest, sptr, priv->bytesperline);
|
|
|
|
dest += priv->bytesperline;
|
|
|
|
sptr -= priv->bytesperline;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VIDEO_PALETTE_UYVY:
|
|
|
|
case VIDEO_PALETTE_YUV420P:
|
|
|
|
default:
|
|
|
|
memcpy(dest, source, priv->bytesperline * priv->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// maximum skew change, in frames
|
|
|
|
#define MAX_SKEW_DELTA 0.6
|
|
|
|
static void *video_grabber(void *data)
|
|
|
|
{
|
|
|
|
priv_t *priv = (priv_t*)data;
|
2002-06-24 16:37:18 +00:00
|
|
|
struct timeval curtime;
|
2002-11-08 22:05:39 +00:00
|
|
|
long long skew, prev_skew, xskew, interval, prev_interval;
|
2002-12-29 23:10:08 +00:00
|
|
|
int frame;
|
2002-08-21 21:31:20 +00:00
|
|
|
int i;
|
|
|
|
int first = 1;
|
2002-10-19 20:03:40 +00:00
|
|
|
int framecount;
|
2001-11-10 23:32:10 +00:00
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
/* start the capture process */
|
2002-12-29 23:10:08 +00:00
|
|
|
|
|
|
|
for (i=0; i < priv->nbuf; i++) {
|
|
|
|
if (ioctl(priv->video_fd, VIDIOCMCAPTURE, &priv->buf[i]) == -1)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "\nioctl mcapture failed: %s\n", strerror(errno));
|
|
|
|
}
|
2001-11-09 23:46:06 +00:00
|
|
|
}
|
2001-12-24 13:56:43 +00:00
|
|
|
|
2002-11-08 22:05:39 +00:00
|
|
|
prev_interval = 0;
|
|
|
|
prev_skew = 0;
|
2001-12-24 13:56:43 +00:00
|
|
|
|
2002-10-19 20:03:40 +00:00
|
|
|
for (framecount = 0; !priv->shutdown;)
|
2002-08-21 21:31:20 +00:00
|
|
|
{
|
2002-10-19 20:03:40 +00:00
|
|
|
for (i = 0; i < priv->nbuf && !priv->shutdown; i++, framecount++) {
|
|
|
|
|
|
|
|
if (priv->immediate_mode) {
|
2002-12-10 09:11:42 +00:00
|
|
|
while (priv->video_cnt == priv->video_buffer_size_max) {
|
2002-10-19 20:03:40 +00:00
|
|
|
usleep(10000);
|
2002-10-22 07:29:09 +00:00
|
|
|
if (priv->shutdown) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-10-19 20:03:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
frame = i;
|
2002-12-29 23:10:08 +00:00
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
while (ioctl(priv->video_fd, VIDIOCSYNC, &priv->buf[frame].frame) < 0 &&
|
|
|
|
(errno == EAGAIN || errno == EINTR));
|
|
|
|
mp_dbg(MSGT_TV, MSGL_DBG3, "\npicture sync failed\n");
|
|
|
|
|
|
|
|
gettimeofday(&curtime, NULL);
|
|
|
|
if (first) {
|
|
|
|
// this was a first frame - let's launch the audio capture thread immediately
|
|
|
|
// before that, just initialize some variables
|
2002-11-08 22:05:39 +00:00
|
|
|
priv->starttime = (long long)1e6*curtime.tv_sec + curtime.tv_usec;
|
2002-08-21 21:31:20 +00:00
|
|
|
priv->audio_skew_measure_time = 0;
|
|
|
|
pthread_mutex_unlock(&priv->audio_starter);
|
2002-10-02 16:47:41 +00:00
|
|
|
// first frame must always have timestamp of zero
|
2002-11-08 22:05:39 +00:00
|
|
|
xskew = 0;
|
|
|
|
skew = 0;
|
|
|
|
interval = 0;
|
2002-08-21 21:31:20 +00:00
|
|
|
first = 0;
|
2002-10-02 16:47:41 +00:00
|
|
|
} else {
|
2002-10-19 20:03:40 +00:00
|
|
|
if (!priv->immediate_mode) {
|
2002-11-08 22:05:39 +00:00
|
|
|
interval = (long long)1e6*curtime.tv_sec + curtime.tv_usec - priv->starttime;
|
2002-10-19 20:03:40 +00:00
|
|
|
} else {
|
2002-12-28 22:57:39 +00:00
|
|
|
interval = (long long)1e6*framecount/priv->fps;
|
2002-10-19 20:03:40 +00:00
|
|
|
}
|
2002-08-21 21:31:20 +00:00
|
|
|
|
2002-12-16 23:24:02 +00:00
|
|
|
if (!priv->immediate_mode) {
|
|
|
|
if (interval - prev_interval == 0) {
|
|
|
|
mp_msg(MSGT_TV, MSGL_V, "\nvideo capture thread: frame delta = 0\n");
|
|
|
|
} else if ((interval - prev_interval < (long long)0.85e6/priv->fps)
|
|
|
|
|| (interval - prev_interval > (long long)1.15e6/priv->fps) ) {
|
|
|
|
mp_msg(MSGT_TV, MSGL_V, "\nvideo capture thread: frame delta ~ %.1lf fps\n",
|
|
|
|
(double)1e6/(interval - prev_interval));
|
|
|
|
}
|
2002-10-02 16:47:41 +00:00
|
|
|
}
|
2002-06-24 16:37:18 +00:00
|
|
|
|
2002-10-02 16:47:41 +00:00
|
|
|
// interpolate the skew in time
|
|
|
|
pthread_mutex_lock(&priv->skew_mutex);
|
|
|
|
xskew = priv->audio_skew + (interval - priv->audio_skew_measure_time)*priv->audio_skew_factor;
|
|
|
|
pthread_mutex_unlock(&priv->skew_mutex);
|
|
|
|
// correct extreme skew changes to avoid (especially) moving backwards in time
|
|
|
|
if (xskew - prev_skew > (interval - prev_interval)*MAX_SKEW_DELTA) {
|
|
|
|
skew = prev_skew + (interval - prev_interval)*MAX_SKEW_DELTA;
|
|
|
|
} else if (xskew - prev_skew < -(interval - prev_interval)*MAX_SKEW_DELTA) {
|
|
|
|
skew = prev_skew - (interval - prev_interval)*MAX_SKEW_DELTA;
|
|
|
|
} else {
|
|
|
|
skew = xskew;
|
|
|
|
}
|
2002-08-21 21:31:20 +00:00
|
|
|
}
|
2001-11-16 21:30:10 +00:00
|
|
|
|
2002-11-08 22:05:39 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_DBG3, "\nfps = %lf, interval = %lf, a_skew = %f, corr_skew = %f\n",
|
2002-12-16 23:24:02 +00:00
|
|
|
(interval != prev_interval) ? (double)1e6/(interval - prev_interval) : -1,
|
|
|
|
(double)1e-6*interval, (double)1e-6*xskew, (double)1e-6*skew);
|
2002-08-21 21:31:20 +00:00
|
|
|
mp_msg(MSGT_TV, MSGL_DBG3, "vcnt = %d, acnt = %d\n", priv->video_cnt, priv->audio_cnt);
|
2001-11-16 21:30:10 +00:00
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
prev_skew = skew;
|
|
|
|
prev_interval = interval;
|
|
|
|
|
2002-12-10 09:11:42 +00:00
|
|
|
/* allocate a new buffer, if needed */
|
|
|
|
pthread_mutex_lock(&priv->video_buffer_mutex);
|
|
|
|
if (priv->video_buffer_size_current < priv->video_buffer_size_max) {
|
|
|
|
if (priv->video_cnt == priv->video_buffer_size_current) {
|
|
|
|
unsigned char *newbuf = (unsigned char*)malloc(priv->bytesperline * priv->height);
|
|
|
|
if (newbuf) {
|
|
|
|
memmove(priv->video_ringbuffer+priv->video_tail+1, priv->video_ringbuffer+priv->video_tail,
|
|
|
|
(priv->video_buffer_size_current-priv->video_tail)*sizeof(unsigned char *));
|
|
|
|
memmove(priv->video_timebuffer+priv->video_tail+1, priv->video_timebuffer+priv->video_tail,
|
|
|
|
(priv->video_buffer_size_current-priv->video_tail)*sizeof(long long));
|
|
|
|
priv->video_ringbuffer[priv->video_tail] = newbuf;
|
|
|
|
if ((priv->video_head >= priv->video_tail) && (priv->video_cnt > 0)) priv->video_head++;
|
|
|
|
priv->video_buffer_size_current++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&priv->video_buffer_mutex);
|
|
|
|
|
|
|
|
if (priv->video_cnt == priv->video_buffer_size_current) {
|
2002-08-21 21:31:20 +00:00
|
|
|
if (!priv->immediate_mode) {
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "\nvideo buffer full - dropping frame\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (priv->immediate_mode) {
|
2002-10-19 20:03:40 +00:00
|
|
|
priv->video_timebuffer[priv->video_tail] = interval;
|
|
|
|
} else {
|
2002-08-21 21:31:20 +00:00
|
|
|
// compensate for audio skew
|
|
|
|
// negative skew => there are more audio samples, increase interval
|
|
|
|
// positive skew => less samples, shorten the interval
|
|
|
|
priv->video_timebuffer[priv->video_tail] = interval - skew;
|
|
|
|
}
|
|
|
|
|
2002-12-10 09:11:42 +00:00
|
|
|
copy_frame(priv, priv->video_ringbuffer[priv->video_tail], priv->mmap+priv->mbuf.offsets[frame]);
|
|
|
|
priv->video_tail = (priv->video_tail+1)%priv->video_buffer_size_current;
|
2002-08-21 21:31:20 +00:00
|
|
|
priv->video_cnt++;
|
|
|
|
}
|
2001-11-10 23:32:10 +00:00
|
|
|
|
2002-12-29 23:10:08 +00:00
|
|
|
if (ioctl(priv->video_fd, VIDIOCMCAPTURE, &priv->buf[frame]) == -1)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "\nioctl mcapture failed: %s\n", strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2002-09-22 02:33:28 +00:00
|
|
|
return NULL;
|
2002-08-21 21:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static double grab_video_frame(priv_t *priv, char *buffer, int len)
|
|
|
|
{
|
|
|
|
double interval;
|
|
|
|
|
|
|
|
if (priv->first) {
|
|
|
|
pthread_create(&priv->video_grabber_thread, NULL, video_grabber, priv);
|
|
|
|
priv->first = 0;
|
|
|
|
}
|
|
|
|
|
2002-12-10 09:11:42 +00:00
|
|
|
while (priv->video_cnt == 0) {
|
2002-08-21 21:31:20 +00:00
|
|
|
usleep(10000);
|
|
|
|
}
|
|
|
|
|
2002-12-10 09:11:42 +00:00
|
|
|
pthread_mutex_lock(&priv->video_buffer_mutex);
|
2002-11-08 22:05:39 +00:00
|
|
|
interval = (double)priv->video_timebuffer[priv->video_head]*1e-6;
|
2002-12-10 09:11:42 +00:00
|
|
|
memcpy(buffer, priv->video_ringbuffer[priv->video_head], len);
|
2002-08-21 21:31:20 +00:00
|
|
|
priv->video_cnt--;
|
2002-12-31 02:21:28 +00:00
|
|
|
priv->video_head = (priv->video_head+1)%priv->video_buffer_size_current;
|
2002-12-10 09:11:42 +00:00
|
|
|
pthread_mutex_unlock(&priv->video_buffer_mutex);
|
2002-08-21 21:31:20 +00:00
|
|
|
return interval;
|
2001-11-09 23:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int get_video_framesize(priv_t *priv)
|
|
|
|
{
|
2001-11-16 21:30:10 +00:00
|
|
|
return(priv->bytesperline * priv->height);
|
2001-11-09 23:46:06 +00:00
|
|
|
}
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
static void *audio_grabber(void *data)
|
|
|
|
{
|
|
|
|
priv_t *priv = (priv_t*)data;
|
|
|
|
struct timeval tv;
|
|
|
|
int i, audio_skew_ptr = 0;
|
2002-12-10 09:11:42 +00:00
|
|
|
long long current_time, prev_skew = 0;
|
2002-08-21 21:31:20 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&priv->audio_starter);
|
|
|
|
|
|
|
|
audio_in_start_capture(&priv->audio_in);
|
|
|
|
for (i = 0; i < priv->aud_skew_cnt; i++)
|
2002-11-08 22:05:39 +00:00
|
|
|
priv->audio_skew_buffer[i] = 0;
|
2002-08-21 21:31:20 +00:00
|
|
|
|
|
|
|
for (; !priv->shutdown;)
|
|
|
|
{
|
|
|
|
if (audio_in_read_chunk(&priv->audio_in, priv->audio_ringbuffer+priv->audio_tail*priv->audio_in.blocksize) < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
|
|
|
|
priv->audio_recv_blocks_total++;
|
2002-11-08 22:05:39 +00:00
|
|
|
current_time = (long long)1e6*tv.tv_sec + tv.tv_usec - priv->starttime;
|
|
|
|
|
|
|
|
priv->audio_skew_total -= priv->audio_skew_buffer[audio_skew_ptr];
|
|
|
|
priv->audio_skew_buffer[audio_skew_ptr] = current_time
|
|
|
|
- 1e6*priv->audio_secs_per_block*priv->audio_recv_blocks_total;
|
|
|
|
priv->audio_skew_total += priv->audio_skew_buffer[audio_skew_ptr];
|
|
|
|
audio_skew_ptr = (audio_skew_ptr+1) % priv->aud_skew_cnt;
|
2002-08-21 21:31:20 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&priv->skew_mutex);
|
|
|
|
// linear interpolation - here we interpolate current skew value
|
|
|
|
// from the moving average, which we expect to be in the middle
|
|
|
|
// of the interval
|
|
|
|
if (priv->audio_recv_blocks_total > priv->aud_skew_cnt) {
|
|
|
|
priv->audio_skew = priv->audio_skew_total/priv->aud_skew_cnt;
|
|
|
|
priv->audio_skew += (priv->audio_skew*priv->aud_skew_cnt)/(2*priv->audio_recv_blocks_total-priv->aud_skew_cnt);
|
|
|
|
} else {
|
2002-11-08 22:05:39 +00:00
|
|
|
// this smoothes the evolution of audio_skew at startup a bit
|
|
|
|
priv->audio_skew = ((priv->aud_skew_cnt+priv->audio_recv_blocks_total)*priv->audio_skew_total)/(priv->aud_skew_cnt*priv->audio_recv_blocks_total);
|
2002-08-21 21:31:20 +00:00
|
|
|
}
|
|
|
|
// current skew factor (assuming linearity)
|
|
|
|
// used for further interpolation in video_grabber
|
|
|
|
// probably overkill but seems to be necessary for
|
|
|
|
// stress testing by dropping half of the audio frames ;)
|
|
|
|
// especially when using ALSA with large block sizes
|
|
|
|
// where audio_skew remains a long while behind
|
2002-12-14 09:55:45 +00:00
|
|
|
if ((priv->audio_skew_measure_time != 0) && (current_time - priv->audio_skew_measure_time != 0)) {
|
|
|
|
priv->audio_skew_factor = (double)(priv->audio_skew-prev_skew)/(current_time - priv->audio_skew_measure_time);
|
|
|
|
} else {
|
|
|
|
priv->audio_skew_factor = 0.0;
|
|
|
|
}
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
priv->audio_skew_measure_time = current_time;
|
|
|
|
prev_skew = priv->audio_skew;
|
|
|
|
pthread_mutex_unlock(&priv->skew_mutex);
|
|
|
|
|
|
|
|
if ((priv->audio_tail+1) % priv->audio_buffer_size == priv->audio_head) {
|
|
|
|
mp_msg(MSGT_TV, MSGL_ERR, "\ntoo bad - dropping audio frame !\n");
|
|
|
|
priv->audio_drop++;
|
|
|
|
} else {
|
2002-12-31 02:21:28 +00:00
|
|
|
priv->audio_tail = (priv->audio_tail+1) % priv->audio_buffer_size;
|
2002-08-21 21:31:20 +00:00
|
|
|
priv->audio_cnt++;
|
|
|
|
}
|
|
|
|
}
|
2002-09-22 02:33:28 +00:00
|
|
|
return NULL;
|
2002-08-21 21:31:20 +00:00
|
|
|
}
|
|
|
|
|
2002-04-12 10:40:38 +00:00
|
|
|
static double grab_audio_frame(priv_t *priv, char *buffer, int len)
|
2001-11-09 23:46:06 +00:00
|
|
|
{
|
2002-03-15 16:08:14 +00:00
|
|
|
mp_dbg(MSGT_TV, MSGL_DBG2, "grab_audio_frame(priv=%p, buffer=%p, len=%d)\n",
|
2001-12-27 23:52:48 +00:00
|
|
|
priv, buffer, len);
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
// compensate for dropped audio frames
|
|
|
|
if (priv->audio_drop && (priv->audio_head == priv->audio_tail)) {
|
|
|
|
priv->audio_drop--;
|
|
|
|
priv->audio_sent_blocks_total++;
|
|
|
|
memset(buffer, 0, len);
|
|
|
|
return (double)priv->audio_sent_blocks_total*priv->audio_secs_per_block;
|
2001-12-27 23:52:48 +00:00
|
|
|
}
|
|
|
|
|
2002-08-21 21:31:20 +00:00
|
|
|
while (priv->audio_head == priv->audio_tail) {
|
|
|
|
usleep(10000);
|
|
|
|
}
|
|
|
|
memcpy(buffer, priv->audio_ringbuffer+priv->audio_head*priv->audio_in.blocksize, len);
|
2002-12-31 02:21:28 +00:00
|
|
|
priv->audio_head = (priv->audio_head+1) % priv->audio_buffer_size;
|
2002-08-21 21:31:20 +00:00
|
|
|
priv->audio_cnt--;
|
|
|
|
priv->audio_sent_blocks_total++;
|
|
|
|
return (double)priv->audio_sent_blocks_total*priv->audio_secs_per_block;
|
2001-11-09 23:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int get_audio_framesize(priv_t *priv)
|
|
|
|
{
|
2002-08-21 21:31:20 +00:00
|
|
|
return(priv->audio_in.blocksize);
|
2001-11-09 23:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* USE_TV */
|