Statistics
| Branch: | Revision:

chunker-player / capture / main.c @ 1e69ae95

History | View | Annotate | Download (6.72 KB)

1
#include "capture.h"
2
#include "video.h"
3
#include "audio.h"
4

    
5
void process_frame (unsigned char *p, int bytes, const struct v4l2_format* fmt)
6
{
7
    switch(fmt->fmt.pix.pixelformat)
8
    {
9
        case V4L2_PIX_FMT_JPEG:
10
            sprintf(filename, "frames/jpg/frame%d.jpg", seq++);
11
            img = fopen(filename,"w");
12
            fwrite(p, bytes, 1, img);
13
            fclose(img);
14
            break;
15
        case V4L2_PIX_FMT_YUYV:
16
            sprintf(filename, "frames/ppm/frame%d.ppm", seq++);
17
            img = fopen(filename,"w");
18

    
19
            // RGB
20
            //fprintf( img, "P6\r\n" );
21

    
22
            // GRAY
23
            fprintf( img, "P5\r\n" );
24

    
25
            fprintf( img, "# comment .\r\n" );
26
            fprintf( img, "%d %d\r\n", fmt->fmt.pix.width, fmt->fmt.pix.height );
27
            fprintf( img, "255\r\n" );
28

    
29
            unsigned int pixels = fmt->fmt.pix.width*fmt->fmt.pix.height;
30

    
31
            // YUYV 2 GRAY            
32
            int i;
33
            for(i=0; i<bytes; i+=2)
34
                fwrite(p+i, 1, 1, img);
35

    
36
            // YUYV 2 RGB24
37
            // todo
38

    
39
            fclose(img);
40
            break;
41
        case V4L2_PIX_FMT_MJPEG:
42
            sprintf(filename, "frames/jpg/frame%d.jpg", seq++);
43
            img = fopen(filename,"w");
44
            fwrite(p, bytes, 1, img);
45
            fclose(img);
46
            break;
47
    }
48
    
49
}
50

    
51
void process_sample(unsigned char* buffer, int buffer_size)
52
{
53
    fwrite(buffer, buffer_size, 1, audio_output_file);
54
}
55

    
56
void usage (FILE *fp, int argc, char **argv)
57
{
58
    fprintf (fp,
59
        "Usage: %s [options]\n\n"
60
        "Options:\n"
61
        "-v | --video-device    Video device name default=/dev/video0\n"
62
        "-a | --audio-device    Audio device name [hw:0,1|hw:0,2|...], default=default\n"
63
        "-h | --help            Print this message\n"
64
        //"-m | --mmap            Use memory mapped buffers\n"
65
        //"-r | --read            Use read() calls\n"
66
        //"-u | --userp           Use application allocated buffers\n"
67
        "--video-format         Video format [rgb24|yuyv|jpeg|mjpeg], default=yuyv\n\n"
68
        //"--audio-format         Audio format []\n\n"
69
        "",
70
        argv[0]);
71
}
72

    
73
int main (int argc, char **argv)
74
{
75
    // VIDEO
76
    seq = 0;
77
    video_dev_name = DEFAULT_VIDEO_DEVICE;
78
    io = IO_METHOD_MMAP;
79
    video_format = DEFAULT_VIDEO_FORMAT;
80
    fd = -1;
81
    buffers = NULL;
82
    n_buffers = 0;
83
    system("rm frames/ppm/* -f");
84

    
85
    // AUDIO
86
    char* audio_dev_name = "default";
87
    audio_output_file = fopen(DEFAULT_AUDIO_OUT_FILE, "w");
88

    
89
    for (;;)
90
    {
91
        int index;
92
        int c;
93
        const struct option long_options [] = {
94
            { "video-device",   required_argument,      NULL,           'v' },
95
            { "audio-device",   required_argument,      NULL,           'a' },
96
            { "help",           no_argument,            NULL,           'h' },
97
            { "mmap",           no_argument,            NULL,           'm' },
98
            { "read",           no_argument,            NULL,           'r' },
99
            { "userp",          no_argument,            NULL,           'u' },
100
            { "video-format",   required_argument,      NULL,           'x' },
101
            //{ "audio-format",   required_argument,      NULL,           'z' },
102
            { 0, 0, 0, 0 }
103
        };
104

    
105
        c = getopt_long (argc, argv, "v:a:hmrux:", long_options, &index);
106

    
107
        if (-1 == c)
108
            break;
109

    
110
        switch (c)
111
        {
112
            case 0: /* getopt_long() flag */
113
                break;
114

    
115
            case 'v':
116
                video_dev_name = optarg;
117
                break;
118

    
119
            case 'h':
120
                usage (stdout, argc, argv);
121
                exit (EXIT_SUCCESS);
122

    
123
            case 'x':                
124
                if(strcmp(optarg, "rgb24") == 0)
125
                    video_format = V4L2_PIX_FMT_RGB24;
126
                if(strcmp(optarg, "jpeg") == 0)
127
                    video_format = V4L2_PIX_FMT_JPEG;
128
                if(strcmp(optarg, "yuyv") == 0)
129
                    video_format = V4L2_PIX_FMT_YUYV;
130
                if(strcmp(optarg, "yuv32") == 0)
131
                    video_format = V4L2_PIX_FMT_YUV32;
132
                if(strcmp(optarg, "grey") == 0)
133
                    video_format = V4L2_PIX_FMT_GREY;
134
                if(strcmp(optarg, "mjpeg") == 0)
135
                    video_format = V4L2_PIX_FMT_MJPEG;
136
                break;
137

    
138
            case 'a':
139
                audio_dev_name = optarg;
140
                break;
141

    
142
            case 'm':
143
                io = IO_METHOD_MMAP;
144
                break;
145

    
146
            case 'r':
147
                io = IO_METHOD_READ;
148
                break;
149

    
150
            case 'u':
151
                io = IO_METHOD_USERPTR;
152
                break;
153

    
154
            default:
155
                usage (stderr, argc, argv);
156
                exit (EXIT_FAILURE);
157
        }
158
    }
159

    
160
    // VIDEO INIT
161
    //printf("opening video device...\n");
162
    open_video_device ();
163
    //printf("\t success\n\n");
164
    printf("initializing video device...\n");
165
    init_video_device ();
166
    start_video_capturing();
167
    printf("OK\n\n");
168

    
169
    // AUDIO INIT
170
    printf("initializing audio device...\n");
171
    int error = init_audio(audio_dev_name);
172
    if(error<0)
173
        exit(1);
174
    printf("OK\n\n");
175

    
176
    signal(SIGINT, terminate);
177

    
178
    running = 1;
179
    pthread_create(&video_capure_thread, NULL, video_capture, NULL);
180
    pthread_create(&audio_capure_thread, NULL, audio_capture, NULL);
181

    
182
    pthread_join(video_capure_thread, NULL);
183
    pthread_join(audio_capure_thread, NULL);
184

    
185
    printf("stopping video capture...\n");
186
    stop_video_capturing ();
187
    uninit_video_device ();
188
    close_video_device ();
189
    printf("STOPPED\n\n");
190

    
191
    printf("stopping audio capture...\n");
192
    fclose(audio_output_file);
193
    close_audio_device();
194
    printf("STOPPED\n\n");
195

    
196
    exit (EXIT_SUCCESS);
197

    
198
    return 0;
199
}
200

    
201
void terminate(int signum)
202
{
203
    printf("\nterminating...\n");
204
    running = 0;
205
}
206

    
207
void* video_capture(void* ThreadParams)
208
{
209
    printf("video capture started\n");
210
    while(running)
211
    {
212
        int r = poll_video_device();
213

    
214
        if (-1 == r) {
215
            if (EINTR == errno)
216
                continue;
217

    
218
            errno_exit ("select");
219
        }
220

    
221
        if (0 == r) {
222
            fprintf (stderr, "select timeout\n");
223
            exit (EXIT_FAILURE);
224
        }
225
        read_frame();
226
    }
227
}
228

    
229
void* audio_capture(void* ThreadParams)
230
{
231
    printf("audio capture started (%d bit)\n", audio_s_bit);
232
    wav_t head = {
233
            {'R','I','F','F'}
234
            ,(1<<28)+36,
235
            {'W','A','V','E'},
236
            {'f','m','t',' '},
237
            audio_s_bit,1,AUDIO_CHANNELS, AUDIO_SAMPLE_RATE,4*AUDIO_SAMPLE_RATE,4,audio_s_bit,
238
            {'d','a','t','a'},
239
            1<<30
240
    };
241

    
242
    fwrite(&head,sizeof(wav_t),1, audio_output_file);
243
    while(running)
244
    {
245
        read_sample();
246
    }
247
}