~ubuntu-branches/debian/sid/v4l-utils/sid

« back to all changes in this revision

Viewing changes to contrib/test/driver-test.c

  • Committer: Bazaar Package Importer
  • Author(s): Gregor Jasny
  • Date: 2010-02-28 19:44:15 UTC
  • Revision ID: james.westby@ubuntu.com-20100228194415-067hdj8rvawj91zw
Tags: upstream-0.7.90
ImportĀ upstreamĀ versionĀ 0.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   driver-test.c - This program tests V4L2 kernel drivers
 
3
 
 
4
   Copyright (C) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
 
5
 
 
6
   This program is free software; you can redistribute it and/or modify
 
7
   it under the terms of the GNU General Public License as published by
 
8
   the Free Software Foundation; either version 2 of the License, or
 
9
   (at your option) any later version.
 
10
 
 
11
   This program is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
   GNU General Public License for more details.
 
15
 */
 
16
 
 
17
#include "../../utils/libv4l2util/v4l2_driver.h"
 
18
#include <stdio.h>
 
19
#include <string.h>
 
20
#include <unistd.h>
 
21
#include <errno.h>
 
22
 
 
23
static int recebe_buffer (struct v4l2_buffer *v4l2_buf, struct v4l2_t_buf *buf)
 
24
{
 
25
 
 
26
        return 0;
 
27
}
 
28
 
 
29
int main(void)
 
30
{
 
31
        struct v4l2_driver drv;
 
32
        struct drv_list *cur;
 
33
        unsigned int count = 10, i;
 
34
        double freq;
 
35
 
 
36
        if (v4l2_open ("/dev/video0", 1,&drv)<0) {
 
37
                perror("open /dev/video0");
 
38
                return -1;
 
39
        }
 
40
        if (v4l2_enum_stds (&drv)<0) {
 
41
                perror("enum_stds");
 
42
                printf("Error! Driver is not reporting supported STD, frames/sec and number of lines!\n Trying to continue anyway...\n");
 
43
        } else {
 
44
                /* Tries all video standards */
 
45
                for (cur=drv.stds;cur!=NULL;cur=cur->next) {
 
46
                        v4l2_std_id id=((struct v4l2_standard *)cur->curr)->id;
 
47
                        if (cur->curr)
 
48
                                if (v4l2_setget_std (&drv, V4L2_SET_GET, &id))
 
49
                                        perror("set_std");
 
50
                }
 
51
        }
 
52
 
 
53
        if (v4l2_enum_input (&drv)<0) {
 
54
                perror("enum_input");
 
55
        }
 
56
 
 
57
        /* Tries all video inputs */
 
58
        for (cur=drv.inputs;cur!=NULL;cur=cur->next) {
 
59
                struct v4l2_input input;
 
60
                input.index=((struct v4l2_input* )cur->curr)->index;
 
61
                if (cur->curr)
 
62
                        if (v4l2_setget_input (&drv, V4L2_SET_GET, &input))
 
63
                                perror("set_input");
 
64
        }
 
65
 
 
66
        if (v4l2_enum_fmt (&drv,V4L2_BUF_TYPE_VIDEO_CAPTURE)<0) {
 
67
                perror("enum_fmt_cap");
 
68
        }
 
69
 
 
70
        /* Tries all formats */
 
71
        for (cur=drv.fmt_caps;cur!=NULL;cur=cur->next) {
 
72
                struct v4l2_format fmt;
 
73
                memset (&fmt,0,sizeof(fmt));
 
74
 
 
75
                uint32_t           pixelformat=((struct v4l2_fmtdesc *)cur->curr)->pixelformat;
 
76
                if (cur->curr) {
 
77
                        if (v4l2_gettryset_fmt_cap (&drv,V4L2_SET,&fmt, 640, 480,
 
78
                                                pixelformat,V4L2_FIELD_ANY))
 
79
                                perror("set_input");
 
80
                }
 
81
        }
 
82
 
 
83
        if (v4l2_get_parm (&drv)<0) {
 
84
                perror("get_parm");
 
85
        }
 
86
 
 
87
 
 
88
        v4l2_getset_freq (&drv,V4L2_GET, &freq);
 
89
 
 
90
        freq=0;
 
91
        v4l2_getset_freq (&drv,V4L2_SET, &freq);
 
92
 
 
93
        freq=121250000; /* 121.250 MHz */
 
94
        v4l2_getset_freq (&drv,V4L2_SET, &freq);
 
95
 
 
96
        printf("Preparing for frames...\n");
 
97
        fflush (stdout);
 
98
        sleep(1);
 
99
 
 
100
        v4l2_mmap_bufs(&drv, 2);
 
101
 
 
102
        v4l2_start_streaming(&drv);
 
103
 
 
104
        printf("Waiting for frames...\n");
 
105
 
 
106
        for (i=0;i<count;i++) {
 
107
                fd_set fds;
 
108
                struct timeval tv;
 
109
                int r;
 
110
 
 
111
                FD_ZERO (&fds);
 
112
                FD_SET (drv.fd, &fds);
 
113
 
 
114
                /* Timeout. */
 
115
                tv.tv_sec = 2;
 
116
                tv.tv_usec = 0;
 
117
 
 
118
                r = select (drv.fd + 1, &fds, NULL, NULL, &tv);
 
119
                if (-1 == r) {
 
120
                        if (EINTR == errno)
 
121
                                continue;
 
122
 
 
123
                        perror ("select");
 
124
                        return errno;
 
125
                }
 
126
 
 
127
                if (0 == r) {
 
128
                        fprintf (stderr, "select timeout\n");
 
129
                        return errno;
 
130
                }
 
131
 
 
132
                if (v4l2_rcvbuf(&drv, recebe_buffer))
 
133
                        break;
 
134
        }
 
135
 
 
136
        printf("stopping streaming\n");
 
137
        v4l2_stop_streaming(&drv);
 
138
 
 
139
        if (v4l2_close (&drv)<0) {
 
140
                perror("close");
 
141
                return -1;
 
142
        }
 
143
        return 0;
 
144
}