The following program shows hangs at random between typically 60 and 500
frames captured. Nothing is logged when the camera hangs. A ^C and 
restart normally recovers the situation but not always.

It also occasionally causes machine crashes here

(OHCI, K7)

/*
 *  I/O routines: Taken from mp1e. Why rewrite it when these guys already
 *  did the work well.
 *
 *  Removed V4L2 stuff to unclutter the code for now (its easy to paste back
 *  once this app works)
 *
 *  From:
 *
 *  MPEG-1 Real Time Encoder
 *
 *  Copyright (C) 1999-2000 Michael H. Schimek
 *  Ported back to v4l/bttv by Justin Schoeman
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* $Id: io.c,v 1.7 2000/01/21 15:45:19 michael Exp $ */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <errno.h>
#include <linux/videodev.h>

static int                      cap_fd;
static unsigned char *          cap_buffer[256];
static int                      cap_buffers;

static struct video_capability  vcap;
static struct video_channel     chan;
static struct video_mbuf        buf;
static struct video_mmap        vmmap;
static struct video_audio       vaud;
static int                      cframe;
static struct timeval           tv;

static char *cap_dev = "/dev/video1";
static char *my_name = "gvp";

static int y_offs, v_offs, u_offs;
static int stride;

static char *le4cc2str(int n)
{
        static char buf[4 * 4 + 1];
        int i, c;

        buf[0] = '\0';

        for (i = 0; i < 4; i++) {
                c = (n >> (8 * i)) & 0xFF;
                if (isprint(c))
                        sprintf(buf + strlen(buf), "%c", c);
                else
                        sprintf(buf + strlen(buf), "\\%o", c);
        }
        return buf;
}

void cap_init(int width, int height)
{
        int aligned_width  = (width + 15) & -16;
        int aligned_height = (height + 15) & -16;
        int uv_size;
        unsigned long buf_base;

        cap_fd = open(cap_dev, O_RDONLY);
        if(cap_fd == -1)
        {
                perror(cap_dev);
                exit(EXIT_FAILURE);
        }

        if(ioctl(cap_fd, VIDIOCGCAP, &vcap)<0)
        {
                fprintf(stderr, "%s: %s is not a capture device.\n",
                        my_name, cap_dev);
                exit(EXIT_FAILURE);
        }

        if (!(vcap.type&VID_TYPE_CAPTURE))
        {
                fprintf(stderr, "%s: %s ('%s') is not a capture device\n",
                        my_name, cap_dev, vcap.name);
                exit(EXIT_FAILURE);
        }

        if(ioctl(cap_fd, VIDIOCGAUDIO, &vaud)==-1)
        {
                fprintf(stderr, "%s: %s is not audio capable.\n", my_name, cap_dev);
        }
        else
        {
                vaud.flags&=~VIDEO_AUDIO_MUTE;
                vaud.volume=60000;
                if(ioctl(cap_fd, VIDIOCSAUDIO, &vaud)<0)
                        fprintf(stderr, "%s: unable to configure audio.\n",
                                my_name, cap_dev);
        }

        if(ioctl(cap_fd, VIDIOCGCHAN, &chan)<0)
        {
                fprintf(stderr, "%s: %s is not video capable ??\n",
                        my_name, cap_dev);
                exit(EXIT_FAILURE);
        }

        fprintf(stderr, "Video standard is '%s'\n", chan.norm == 0 ? "PAL" : "NTSC");

        vmmap.width     = aligned_width;
        vmmap.height    = aligned_height;
        vmmap.format    = VIDEO_PALETTE_RGB24;

        stride = vmmap.width;

        if(ioctl(cap_fd, VIDIOCGMBUF, &buf)<0)
        {
                fprintf(stderr, "%s: %s does not support mmap capture.\n",
                        my_name, cap_dev);
                /* FIXME - should handle this eg quickcam */
                exit(EXIT_FAILURE);
        }

        if (buf.frames == 0)
        {
                fprintf(stderr, "%s: No capture buffers granted\n", my_name);
                exit(EXIT_FAILURE);
        }
                
        buf_base=(unsigned long)mmap(NULL, buf.size, PROT_READ,
                  MAP_SHARED, cap_fd, 0);
        if(buf_base == -1)
        {
                fprintf(stderr, "%s: unable to map buffers\n", my_name);
                exit(EXIT_FAILURE);
        }
        cap_buffer[0] = (unsigned char *)(buf_base + buf.offsets[0]);
        for (cap_buffers = 1; cap_buffers < buf.frames; cap_buffers++)
        {
                cap_buffer[cap_buffers] = (unsigned char *)(buf_base + 
buf.offsets[cap_buffers]);
        }
}

void cap_capture_on(void)
{
        for (cframe = 0; cframe < cap_buffers; cframe++)
        {
                vmmap.frame = cframe;
                if(ioctl(cap_fd, VIDIOCMCAPTURE, &vmmap)<0)
                        fprintf(stderr, "%s: capture request refused.\n",
                                my_name);
        }
        cframe = 0;
}

void cap_wait_frame(double *time, int *buf_index, void **ptr)
{
        int r = -1;

        while (r <= 0)
        {
                r = ioctl(cap_fd, VIDIOCSYNC, &cframe);
                gettimeofday(&tv, NULL);
                if (r < 0 && errno == EINTR)
                        continue;
                if(r>0)
                        fprintf(stderr, "%s: execute video sync failed.\n",
                                my_name);
                *time = tv.tv_sec + tv.tv_usec / 1e6;
                *buf_index = cframe;
                *ptr = cap_buffer[cframe];
                break;
        }
}

void cap_frame_done(int buf_index)
{
        vmmap.frame = buf_index;
        if(ioctl(cap_fd, VIDIOCMCAPTURE, &vmmap)<0)
        {
                fprintf(stderr, "%s: requeue buffer failed.\n",
                        my_name);
                exit(EXIT_FAILURE);
        }
        cframe++;
        if (cframe == cap_buffers)
                cframe = 0;
}


int main(int argc, char *argv[])
{
        void *p;
        int idx;
        int fr=0;
        double t;
        cap_init(320,200);
        cap_capture_on();
        while(1)
        {
                cap_wait_frame(&t, &idx, &p);
                cap_frame_done(idx);
                printf("Done %d\n",++fr);
        }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to