File: recordV4L.cpp

package info (click to toggle)
gem 1:0.93.3-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,548 kB
  • ctags: 29,332
  • sloc: cpp: 134,188; sh: 11,215; makefile: 2,853; ansic: 84
file content (488 lines) | stat: -rw-r--r-- 11,538 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
////////////////////////////////////////////////////////
//
// GEM - Graphics Environment for Multimedia
//
// zmoelnig@iem.kug.ac.at
//
// Implementation file 
//
//    Copyright (c) 1997-1999 Mark Danks.
//    Copyright (c) Günther Geiger.
//    Copyright (c) 2001-2011 IOhannes m zmölnig. forum::für::umläute. IEM. zmoelnig@iem.at
//    For information on usage and redistribution, and for a DISCLAIMER OF ALL
//    WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.
//
/////////////////////////////////////////////////////////
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "recordV4L.h"
#include "Gem/Manager.h"
#include "plugins/PluginFactory.h"

using namespace gem::plugins;

#include "Gem/Exception.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>

#include <stdlib.h>
#include <unistd.h>

#ifdef  HAVE_VIDEO4LINUX
REGISTER_RECORDFACTORY("V4L", recordV4L);
/////////////////////////////////////////////////////////
//
// recordV4L
//
/////////////////////////////////////////////////////////
// Constructor
//
/////////////////////////////////////////////////////////

recordV4L :: recordV4L(): 
  recordBase(),
  m_fd(-1)
{
  m_image.xsize=720;
  m_image.xsize=576;
  m_image.setCsizeByFormat(GL_YUV422_GEM);
  m_image.setCsizeByFormat(GL_RGBA);
  m_image.reallocate();



  switch(m_image.format) {
  case GL_YUV422_GEM: m_palette = VIDEO_PALETTE_YUV422; break;
  case GL_LUMINANCE:  m_palette = VIDEO_PALETTE_GREY; break;
  case GL_RGBA:       m_palette = VIDEO_PALETTE_RGB32; break;
  default: throw(new GemException("invalid colorspace"));
  }
  

}

////////////////////////////////////////////////////////
// Destructor
//
/////////////////////////////////////////////////////////
recordV4L :: ~recordV4L()
{
  close();
}

void recordV4L :: close(void)
{
  if(m_fd>=0)
    ::close(m_fd);
  m_fd=-1;

}

bool recordV4L :: open(const std::string filename)
{
  close();

  m_fd=::open(filename.c_str(), O_RDWR);
  if(m_fd<0)return false;

  struct video_picture vid_pic;
  if (ioctl(m_fd, VIDIOCGPICT, &vid_pic) == -1) {
    perror("VIDIOCGPICT");
    close(); return false;
  }
  vid_pic.palette = m_palette;
  if (ioctl(m_fd, VIDIOCSPICT, &vid_pic) == -1) {
    perror("VIDIOCSPICT");
    close(); return false;
  }

  struct video_window vid_win;
  if (ioctl(m_fd, VIDIOCGWIN, &vid_win) == -1) {
    perror("(VIDIOCGWIN)");
    close(); return false;
  }

  m_init=false;
  return true;
}

bool recordV4L::init(const imageStruct* dummyImage, const int framedur) {
  if(m_init)return true;
  if(m_fd<0)return false;

  unsigned int w=dummyImage->xsize;
  unsigned int h=dummyImage->ysize;

  struct video_picture vid_pic;
  struct video_window vid_win;

  if (ioctl(m_fd, VIDIOCGPICT, &vid_pic) == -1) {
    perror("VIDIOCGPICT");
    close(); return false;
  }

  vid_pic.palette = m_palette;
  
  if (ioctl(m_fd, VIDIOCSPICT, &vid_pic) == -1) {
    perror("VIDIOCSPICT");
    close(); return false;
  }

  if (ioctl(m_fd, VIDIOCGWIN, &vid_win) == -1) {
    perror("ioctl (VIDIOCGWIN)");
    close(); return false;
  }
  
  vid_win.width  = w;
  vid_win.height = h;
  if (ioctl(m_fd, VIDIOCSWIN, &vid_win) == -1) {
    perror("ioctl (VIDIOCSWIN)");
    close(); return false;
  }

  m_image.xsize=w;
  m_image.ysize=h;
  m_image.reallocate();

  m_init=true;
  return true;
}



/////////////////////////////////////////////////////////
// do the actual encoding and writing to file
//
/////////////////////////////////////////////////////////
bool recordV4L :: putFrame(imageStruct*img)
{
  if(!m_init){
    if(!init(img, 0))
      return false;
  }
  m_image.convertFrom(img);

  //  m_image.upsidedown=!m_image.upsidedown;
  m_image.fixUpDown();

  int size=m_image.xsize*m_image.ysize*m_image.csize;

  ::write(m_fd, m_image.data, size);

  return true;
}



/////////////////////////////////////////////////////////
// get number of codecs
//
/////////////////////////////////////////////////////////

static const std::string s_codec_name=std::string("v4l");
static const std::string s_codec_desc=std::string("v4l(1) loopback device");

/////////////////////////////////////////////////////////
// set codec by name
//
/////////////////////////////////////////////////////////
bool recordV4L :: setCodec(const std::string name)
{
  if(name==s_codec_name)
    return true;

  return false;
}

/////////////////////////////////////////////////////////
// get codecs
//
/////////////////////////////////////////////////////////
std::vector<std::string>recordV4L::getCodecs() {
  std::vector<std::string>result;

  m_codecdescriptions.clear();
  result.push_back(s_codec_name);
  m_codecdescriptions[s_codec_name]=s_codec_desc;

  return result;
}


#if 0
/* handler for ioctls from the client */
static int v4l_ioctlhandler(unsigned long int cmd, void *arg)
{
	switch(cmd) {

    /* capabilities */
  case VIDIOCGCAP: {
    struct video_capability *vidcap = arg;

    snprintf(vidcap->name, 32, "Gem vloopback output");
    vidcap->type = VID_TYPE_CAPTURE;
    vidcap->channels = 1;
    vidcap->audios = 0;
    vidcap->maxwidth = MAX_WIDTH;
    vidcap->maxheight = MAX_HEIGHT;
    vidcap->minwidth = MIN_WIDTH;
    vidcap->minheight = MIN_HEIGHT;
    return 0;
  }
 
    /* channel info (rather limited */
  case VIDIOCGCHAN:	{
    struct video_channel *vidchan = arg;
    
    if(vidchan->channel != 0)
      return EINVAL;
    vidchan->flags = 0;
    vidchan->tuners = 0;
    vidchan->type = VIDEO_TYPE_CAMERA;
    strncpy(vidchan->name, "Gem", 32);
    return 0;
  }
    /* setting channel (we only have one) */
  case VIDIOCSCHAN:	{
    int *v = arg; 
    if(v[0] != 0)
      return EINVAL;
    return 0;
  }

    /* getting picture properties */
  case VIDIOCGPICT:	{
    struct video_picture *vidpic = arg;
    
    vidpic->colour = 0xffff;
    vidpic->hue = 0xffff;
    vidpic->brightness = 0xffff;
    vidpic->contrast = 0xffff;
    vidpic->whiteness = 0xffff;
    vidpic->depth = pixel_depth;
    vidpic->palette = pixel_format;
    return 0;
  }

    /* setting picture properties: we just ignore this */
    /* LATER: don't we have to set the vidpic to the "real" values? */
  case VIDIOCSPICT:	{
    struct video_picture *vidpic = arg;
   
    return 0;
  }

  case VIDIOCGWIN:	{
    struct video_window *vidwin = arg;
    
    vidwin->x = 0;
    vidwin->y = 0;
    vidwin->width = vloopback_width;
    vidwin->height = vloopback_height;
    vidwin->chromakey = 0;
    vidwin->flags = 0;
    vidwin->clipcount = 0;
  }

  case VIDIOCSWIN: {
    struct video_window *vidwin = arg;

    if(vidwin->width > MAX_WIDTH || vidwin->height > MAX_HEIGHT)
      return EINVAL;
    if(vidwin->width < MIN_WIDTH || vidwin->height < MIN_HEIGHT)
      return EINVAL;
    if(vidwin->flags)
      return EINVAL;
    vloopback_width = vidwin->width;
    vloopback_height = vidwin->height;
    return 0;
  }

  case VIDIOCSYNC: {
    int frame = *(int *)arg;
    int ret = 0;

    if(frame < 0 || frame > 1)
      return EINVAL;
    gbuf_lock();
    switch(gbufstat[frame]) {
    case GBUFFER_UNUSED:
      ret = 0;
      break;
    case GBUFFER_GRABBING:
      while(gbufstat[frame] == GBUFFER_GRABBING) {
        pthread_cond_wait(&gbufcond, &gbufmutex);
      }
      if(gbufstat[frame] == GBUFFER_DONE) {
        gbufstat[frame] = GBUFFER_UNUSED;
        ret = 0;
      } else {
        gbufstat[frame] = GBUFFER_UNUSED;
        ret = EIO;
      }
      break;
    case GBUFFER_DONE:
      gbufstat[frame] = GBUFFER_UNUSED;
      ret = 0;
      break;
    case GBUFFER_ERROR:
    default:
      gbufstat[frame] = GBUFFER_UNUSED;
      ret = EIO;
      break;
    }
    gbuf_unlock();
    return ret;
  }
    
  case VIDIOCMCAPTURE: {
    struct video_mmap *vidmmap = arg;
    
    if(vidmmap->width > MAX_WIDTH || vidmmap->height > MAX_HEIGHT) {
      fprintf(stderr, "vloopback: requested capture size is too big(%dx%d).\n",vidmmap->width, vidmmap->height);
      return EINVAL;
    }
    if(vidmmap->width < MIN_WIDTH || vidmmap->height < MIN_HEIGHT) {
      fprintf(stderr, "vloopback: requested capture size is to small(%dx%d).\n",vidmmap->width, vidmmap->height);
      return EINVAL;
    }
    if(vidmmap->format != pixel_format) {
      converter = palette_get_supported_converter_fromRGB32(vidmmap->format);
      if(converter == NULL) {
        fprintf(stderr, "vloopback: unsupported pixel format(%d) is requested.\n",vidmmap->format);
        return EINVAL;
      }
      pixel_format = vidmmap->format;
    }
    if(vidmmap->frame > 1 || vidmmap->frame < 0)
      return EINVAL;
    vloopback_width = vidmmap->width;
    vloopback_height = vidmmap->height;
    
    if(gbufstat[vidmmap->frame] != GBUFFER_UNUSED) {
      if(gbufqueue[0] == vidmmap->frame || gbufqueue[1] == vidmmap->frame) {
        return EBUSY;
      }
    }
    gbuf_lock();
    gbufstat[vidmmap->frame] = GBUFFER_GRABBING;
    if(gbufqueue[0] < 0) {
      gbufqueue[0] = vidmmap->frame;
    } else  {
      gbufqueue[1] = vidmmap->frame;
    }
    gbuf_unlock();
    return 0;
  }

  case VIDIOCGMBUF:	{
    struct video_mbuf *vidmbuf = arg;

    vidmbuf->size = max_gbufsize;
    vidmbuf->frames = 2;
    vidmbuf->offsets[0] = 0;
    vidmbuf->offsets[1] = max_framesize;
    return 0;
  }

  default:
		{
			return EPERM;
		}
	}
}

static int signal_loop_init(void)
{
	outputfd = open(output_devname, O_RDWR);
	if(outputfd < 0) {
		fprintf(stderr, "vloopback: couldn't open output device file %s\n",output_devname);
		return -1;
	}
	pixel_format = VIDEO_PALETTE_RGB32;
	pixel_depth = 4;
	converter = palette_get_supported_converter_fromRGB32(pixel_format);
	vloopback_width = screen_width;
	vloopback_height = screen_height;
	max_framesize = vloopback_width * vloopback_height * sizeof(RGB32);
	max_gbufsize = max_framesize * 2;
	gbuf = vloopback_mmap(outputfd, max_gbufsize);
	gbuf_clear();
	return 0;
}

static void *signal_loop(void *arg)
{
	int signo;
	int size, ret;
	struct pollfd ufds;
	sigset_t sigset;
	unsigned long int cmd;

	if(signal_loop_init()) {
		signal_loop_initialized = -1;
		return NULL;
	}
	signal_loop_initialized = 1;

	sigemptyset(&sigset);
	sigaddset(&sigset, SIGIO);
	while(signal_loop_thread_flag) {
		sigwait(&sigset, &signo);
		if(signo != SIGIO)
			continue;
		ufds.fd = outputfd;
		ufds.events = POLLIN;
		ufds.revents = 0;
		poll(&ufds, 1, 1000);
		if(!ufds.revents & POLLIN) {
			fprintf(stderr, "vloopback: received signal but got negative on poll.\n");
			continue;
		}
		size = read(outputfd, ioctlbuf, MAXIOCTL);
		if(size >= sizeof(unsigned long int)) {
			memcpy(&cmd, ioctlbuf, sizeof(unsigned long int));
			if(cmd == 0) {
				fprintf(stderr, "vloopback: client closed device.\n");
				gbuf_lock();
				gbuf_clear();
				gbuf_unlock();
				continue;
			}
			ret = v4l_ioctlhandler(cmd, ioctlbuf+sizeof(unsigned long int));
			if(ret) {
				/* new vloopback patch supports a way to return EINVAL to
				 * a client. */
				memset(ioctlbuf+sizeof(unsigned long int), 0xff, MAXIOCTL-sizeof(unsigned long int));
				fprintf(stderr, "vloopback: ioctl %lx unsuccessfully handled.\n", cmd);
				ioctl(outputfd, VIDIOCSINVALID);
			}
			if(ioctl(outputfd, cmd, ioctlbuf+sizeof(unsigned long int))) {
				fprintf(stderr, "vloopback: ioctl %lx unsuccessfull.\n", cmd);
			}
		}
	}
	return NULL;
}
#endif


#else
recordV4L :: recordV4L()
{
}
////////////////////////////////////////////////////////
// Destructor
//
/////////////////////////////////////////////////////////
recordV4L :: ~recordV4L()
{
}

#endif