[asterisk-commits] rizzo: branch rizzo/video_v2 r82750 - /team/rizzo/video_v2/channels/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Sep 18 06:08:07 CDT 2007


Author: rizzo
Date: Tue Sep 18 06:08:07 2007
New Revision: 82750

URL: http://svn.digium.com/view/asterisk?view=rev&rev=82750
Log:
some cleanup on struct and variable names

Modified:
    team/rizzo/video_v2/channels/console_video.c

Modified: team/rizzo/video_v2/channels/console_video.c
URL: http://svn.digium.com/view/asterisk/team/rizzo/video_v2/channels/console_video.c?view=diff&rev=82750&r1=82749&r2=82750
==============================================================================
--- team/rizzo/video_v2/channels/console_video.c (original)
+++ team/rizzo/video_v2/channels/console_video.c Tue Sep 18 06:08:07 2007
@@ -87,7 +87,7 @@
  * - one descriptor per window (SDL).
  */
 
-struct dbuf_t {		/* buffers, dynamically allocated */
+struct fbuf_t {		/* frame buffers, dynamically allocated */
 	uint8_t	*data;
 	int	size;
 	int	used;
@@ -99,19 +99,23 @@
 	 * At the right time we try to open it and allocate a buffer.
 	 * If we are successful, webcam_bufsize > 0 and we can read.
 	 */
-	int 		w;		/* geometry */
+	/* all the following is config file info copied from the parent */
+	const char *	device;		/* device name (or X11) */
+	int 		w;		/* geometry and other info,  */
 	int 		h;
+	int		fps;
+	int		bitrate;
+
+	/* this is generated here */
 	int 		fd;		/* file descriptor */
 	int pix_fmt;			/* source pixel format */
 
-	struct dbuf_t	buf;
+	struct fbuf_t	buf;
 	AVCodecContext	*context;	/* encoding context */
 	AVCodec		*codec;
 	AVFrame		*frame;
 	int		lasttxframe;	/* XXX - useless: RTP overwrite seqno */
-	int		fps;
-	int		bitrate;
-	struct dbuf_t	decbuf;
+	struct fbuf_t	encbuf;		/* encoding buffer */
 	int		mtu;
 
 	struct timeval	last_frame;
@@ -125,7 +129,7 @@
 	AVFrame                 *frame;		/* place to store the frame */
 	AVCodecParserContext    *parser;
 	int                     completed;	/* probably unnecessary */
-	struct dbuf_t buf;
+	struct fbuf_t buf;
 	uint16_t 		next_seq;	/* must be 16 bit */
 	int                     discard;
 	struct timeval	ts;
@@ -173,33 +177,19 @@
 #include <linux/videodev.h>
 #endif
 
-/* open the local video source */
-static int video_open(struct video_desc *env)
+/* open the local video source and allocate a buffer
+ * for storing the image. Return 0 on success, -1 on error
+ */
+static int video_open(struct video_out_desc *v)
 {
 	int i;
-	const char *device = env->videodevice;
-	struct video_out_desc *v = &env->out;
-
-	/* copy config from parent */
-	v->w = env->w;
-	v->h = env->h;
-	v->fps = env->fps;
-	if (v->fps == 0) {
-		ast_log(LOG_WARNING, "fps unset, forcing to 5\n");
-		v->fps = env->fps = 5;
-	}
-	if (env->bitrate == 0) {
-		ast_log(LOG_WARNING, "bitrate unset, forcing to 65000\n");
-		env->bitrate = 65000;
-	}
-	v->bitrate = env->bitrate;
 
 	if (v->buf.data)	/* buffer allocated means device already open */
 		return v->fd;
 	/*
 	 * if the device is "X11", then open the x11 grabber
 	 */
-    if (!strcmp(device, "X11")) {
+    if (!strcmp(v->device, "X11")) {
 	int x_ofs = 0;
 	int y_ofs = 0;
 	XImage *im;
@@ -225,22 +215,22 @@
 	struct video_window vw = { 0 };	/* camera attributes */
 	struct video_picture vp;
 
-	v->fd = open(device, O_RDONLY | O_NONBLOCK);
+	v->fd = open(v->device, O_RDONLY | O_NONBLOCK);
 	if (v->fd < 0) {
-		ast_log(LOG_WARNING, "error opening camera %s\n", device);
+		ast_log(LOG_WARNING, "error opening camera %s\n", v->device);
 		return v->fd;
 	}
 
 	i = fcntl(v->fd, F_GETFL);
 	if (-1 == fcntl(v->fd, F_SETFL, i | O_NONBLOCK)) {
-		ast_log(LOG_WARNING, "error F_SETFL for %s [%s]\n", device, strerror(errno));
+		ast_log(LOG_WARNING, "error F_SETFL for %s [%s]\n", v->device, strerror(errno));
 	}
 	/* set format */
 	vw.width = v->w;
 	vw.height = v->h;
 	vw.flags = v->fps << 16;
 	if (ioctl(v->fd, VIDIOCSWIN, &vw) == -1) {
-		ast_log(LOG_WARNING, "error setting format for %s [%s]\n", device, strerror(errno));
+		ast_log(LOG_WARNING, "error setting format for %s [%s]\n", v->device, strerror(errno));
 		goto error;
 	}
 	if (ioctl(v->fd, VIDIOCGPICT, &vp) == -1) {
@@ -259,7 +249,7 @@
     }
 #endif /* HAVE_V4L */
 	v->buf.size = (v->w * v->h * 3)/2;	/* yuv411 */
-	ast_log(LOG_WARNING, "videodev %s opened, size %dx%d %d\n", device, v->w, v->h, v->buf.size);
+	ast_log(LOG_WARNING, "videodev %s opened, size %dx%d %d\n", v->device, v->w, v->h, v->buf.size);
 	v->buf.data = ast_calloc(1, v->buf.size);
 	if (!v->buf.data) {
 		ast_log(LOG_WARNING, "error allocating buffer %d bytes\n", v->buf.size);
@@ -267,7 +257,7 @@
 	}
 
 	v->buf.used = 0;
-	return v->fd;
+	return (v->image || v->fd >= 0) ? 0 : -1 ;
 
 error:
 	if (v->dpy)
@@ -455,8 +445,8 @@
 	if(v->fd >= 0) 
 		close(v->fd);
 
-	if(v->decbuf.data)
-		free(v->decbuf.data);
+	if(v->encbuf.data)
+		free(v->encbuf.data);
 
 	v->fd = -1;
 
@@ -464,11 +454,11 @@
 }
 
 /*
- * Initialize:
+ * Initialize the encoder for the local source.
  * - AVCodecContext
  * - AVCodec
  * - AVFrame
- * - decbuf
+ * - encbuf
  * - mtu
  */
 static int video_out_init(struct video_out_desc *v, uint32_t format)
@@ -480,7 +470,7 @@
 	v->codec		= NULL;
 	v->frame		= NULL;
 	v->lasttxframe		= -1;
-	v->decbuf.data		= NULL;
+	v->encbuf.data		= NULL;
 
 	codec = map_video_format(format, CM_WR);
 	v->codec = avcodec_find_encoder(codec);
@@ -537,9 +527,9 @@
 	v->frame->linesize[1] = v->context->width/2;
 	v->frame->linesize[2] = v->context->width/2;
 
-	v->decbuf.data = ast_calloc(1, v->buf.size);
-	v->decbuf.size = v->buf.size;
-	v->decbuf.used = 0;
+	v->encbuf.data = ast_calloc(1, v->buf.size);
+	v->encbuf.size = v->buf.size;
+	v->encbuf.used = 0;
 
 	v->mtu = 1400;
 
@@ -904,7 +894,7 @@
 static struct ast_frame *split_frame(struct video_out_desc *out, int len)
 {
 	struct ast_frame *cur = NULL, *first = NULL;
-	uint8_t *d = out->decbuf.data;
+	uint8_t *d = out->encbuf.data;
 	int l = len; /* size of the current fragment. If 0, must look for a psc */
 	int frags = 0;
 
@@ -985,19 +975,19 @@
 static struct ast_frame *get_video_frames(struct video_desc *env)
 {
 	int buflen;
-
-	if (!video_read(&env->out))
+	struct video_out_desc *v = &env->out;
+	struct fbuf_t *b = &v->encbuf;
+
+	if (!video_read(v))
 		return NULL;
 	// fprintf(stderr, "video read\n");
 
 	/* get frame and put them in the queue */
 	show_frame(env, 1);
-	if (env->out.decbuf.size == 0)
+	if (b->data == NULL)
 		return NULL;
-	buflen = avcodec_encode_video(env->out.context,
-		env->out.decbuf.data, env->out.decbuf.size, env->out.frame);
-	// fprintf(stderr, "avcodec_encode_video in %d returns %d\n", env->out.decbuf.size, buflen);
-	return split_frame(&(env->out), buflen);
+	buflen = avcodec_encode_video(v->context, b->data, b->size, v->frame);
+	return split_frame(v, buflen);
 }
 
 /*
@@ -1041,6 +1031,8 @@
 {
 	int fmt = SDL_IYUV_OVERLAY;	/* YV12 causes flicker in SDL */
 
+	if (owner == NULL)	/* nothing to do if we don't have a channel */
+		return;
 	/*
 	 * Register all codecs supported by the ffmpeg library.
 	 * Doing it once is enough.
@@ -1056,7 +1048,7 @@
 	/*
 	 * initialize the SDL environment
 	 */
-	fprintf(stderr, "prepare SDL initialization\n");
+	ast_log(LOG_WARNING, "prepare SDL initialization\n");
 
 	env->screen = NULL;
 	env->bmp[0] = env->bmp[1] = NULL;
@@ -1065,9 +1057,6 @@
 		fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
 		return;
 	}
-	ast_mutex_lock(&env->lock);
-	// ast_log(LOG_WARNING, "in critical section, screen %p\n", env->screen);
-	/* first time we run, we also initialize sdl and the two bitmaps we use */
 	env->screen = SDL_SetVideoMode(2 * env->w, env->h, 0, 0);
 	if (!env->screen) {
 		ast_log(LOG_ERROR, "SDL: could not set video mode - exiting\n");
@@ -1077,23 +1066,38 @@
 
 	env->bmp[0] = SDL_CreateYUVOverlay(env->w, env->h, fmt, env->screen);
 	env->bmp[1] = SDL_CreateYUVOverlay(env->w, env->h, fmt, env->screen);
-	ast_mutex_unlock(&env->lock);
 
 	/* create windows ? */
+	env->owner = owner;
 	env->initialized = 1;
-	env->owner = owner;
+
+	/*
+	 * now handle the local video source
+	 */
         ast_pthread_create_background(&env->vthread, NULL, video_thread, env);
-	video_open(env);	/* also allocate out.buf.data */
-	// show_frame(env, 2);
-
-	ast_log(LOG_WARNING, "----------------- video_out_init buf %p owner %p\n", env->out.buf.data, owner);
-	if (env->out.buf.data && owner) {  /* talk to the local video source */
+	/* copy config from parent */
+	env->out.device = env->videodevice;
+	env->out.w = env->w;
+	env->out.h = env->h;
+	if (env->fps == 0) {
+		ast_log(LOG_WARNING, "fps unset, forcing to 5\n");
+		env->fps = 5;
+	}
+	env->out.fps = env->fps;
+	if (env->bitrate == 0) {
+		ast_log(LOG_WARNING, "bitrate unset, forcing to 65000\n");
+		env->bitrate = 65000;
+	}
+	env->out.bitrate = env->bitrate;
+
+	if (video_open(&env->out)) {
+		ast_log(LOG_WARNING, "cannot open local video source\n");
+	} else {
 		/* try to register the fd. Unfortunately, if the webcam
 		 * driver does not support select/poll we are out of luck.
 		 */
 		if (env->out.fd >= 0)
 			ast_channel_set_fd(owner, 1, env->out.fd);
-		fprintf(stderr, "video_out_init fd %d\n", env->out.fd);
 		video_out_init(&env->out, CONSOLE_FORMAT_VIDEO);
 	}
 }




More information about the asterisk-commits mailing list