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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Sep 14 06:38:42 CDT 2007


Author: rizzo
Date: Fri Sep 14 06:38:41 2007
New Revision: 82371

URL: http://svn.digium.com/view/asterisk?view=rev&rev=82371
Log:
lock around sdl clients to prevent conflicts

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=82371&r1=82370&r2=82371
==============================================================================
--- team/rizzo/video_v2/channels/console_video.c (original)
+++ team/rizzo/video_v2/channels/console_video.c Fri Sep 14 06:38:41 2007
@@ -116,6 +116,8 @@
 };
 
 struct video_desc {
+	ast_mutex_t lock;
+
 	int 	w;
 	int 	h;
 	int 	fps;
@@ -236,7 +238,10 @@
 	return -1;
 }
 
-/*! \brief complete a buffer from the webcam */
+/*! \brief complete a buffer from the webcam.
+ * called by get_video_frame(), in turn called by asterisk
+ * as part of the read callback
+ */
 static int webcam_read(struct video_out_desc *v)
 {
 	if (v->buf.data == NULL)	/* not initialized */
@@ -261,20 +266,6 @@
 #endif /* HAVE_V4L */
 
 static void show_frame(struct video_desc *env, int out);
-
-static int webcam_encode(struct video_desc *env)
-{
-	int n_enc;
-
-	// struct timeval now = ast_tvnow();
-	// ast_log(LOG_WARNING, "video frame ready at %d.%06d\n", (int)now.tv_sec % 1000, (int)now.tv_usec);
-	show_frame(env, 1);
-	// XXX Codichiamo il frame
-	// XXX Vedi la alsa_read per il continuo
-	n_enc = avcodec_encode_video(env->out.context, env->out.decbuf.data, env->out.decbuf.size, env->out.frame);
-
-	return n_enc;
-}
 
 /* Helper function to process incoming video.
  * For each incoming video call invoke ffmpeg_init() to intialize
@@ -313,6 +304,9 @@
 	return -1;	/* error, in case someone cares */
 }
 
+/*
+ * initialize ffmpeg resources used for decoding frames from the network.
+ */
 static int video_in_init(struct video_in_desc *v, uint32_t format)
 {
 	enum CodecID codec;
@@ -341,6 +335,8 @@
 	v->context->bit_rate_tolerance = v->context->bit_rate/5;
 	if (avcodec_open(v->context, v->codec) < 0) {
 		ast_log(LOG_WARNING, "Unable to open the codec context\n");
+		av_free(v->context);
+		v->context = NULL;
 		return video_in_uninit(v);
 	}
 
@@ -464,37 +460,6 @@
 }
 
 /*
- * It initializes the video_desc struct which contains all the structures
- * needed by ffmpeg and SDL libraries.
- * - Registering of all codecs supported by the ffmpeg.
- * - Searching for H.263+ decoder (H.263 decoder can decode H.263+ stream).
- * - Allocation and initialization of codec context.
- * - Initialization of codec parser (it should be used
- *     to reconstruct the entire bitstream from a fragmented stream)
- * - Allocation of a new frame
- * - Initializzation of the SDL environment to support the video
- */
-static void ffmpeg_init(struct video_desc *env, uint32_t format)
-{
-	if (env->initialized)
-		return;
-
-	env->screen = NULL;
-	env->bmp[0] = env->bmp[1] = NULL;
-
-	// SDL specific
-	if(SDL_Init(SDL_INIT_VIDEO)) {
-		fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
-		return;
-	}
-
-	if (video_in_init(&env->in, format))
-		return;
-
-	env->initialized = 1;
-}
-
-/*
  * Freeing all memory used and uninitialize
  * the ffmpeg and SDL environments.
  */
@@ -533,6 +498,7 @@
 		env->h = x.h;
 		env->fps = x.fps;
 	}
+	ast_mutex_destroy(&env->lock);
 }
 
 #define MAKE_MASK(bits)                ( (1<<(bits)) -1 )
@@ -636,7 +602,7 @@
 }
 
 /*
- * It displays the decoded video frame using the SDL library.
+ * Display the decoded video frame using the SDL library.
  * - Set the video mode to use the resolution specified by the codec context
  * - Create a YUV Overlay to copy into it the decoded frame
  * - After the decoded frame is copied into the overlay, we display it
@@ -654,6 +620,11 @@
 
 	if (!env->initialized)
 		return;
+fprintf(stderr, "thd %p env->out %dx%d in.ctx %dx%d default %dx%d\n",
+	pthread_self(),
+	env->out.w, env->out.h, c->width, c->height, env->w, env->h);
+
+	ast_mutex_lock(&env->lock);
 	if(env->screen == NULL) {
 		env->screen = SDL_SetVideoMode(2*w, h, 0, 0);
 		if(!env->screen) {
@@ -679,10 +650,15 @@
 
 	if (out) {	/* raw stream from camera */
 		int l4 = w*h/4;	/* U or v frame */
-		bcopy(env->out.buf.data, bmp->pixels[0], 4*l4);
-		bcopy(env->out.buf.data + 4*l4, bmp->pixels[2], l4);
-		bcopy(env->out.buf.data + 5*l4, bmp->pixels[1], l4);
+		if (!env->out.buf.data) {
+			ast_log(LOG_WARNING, "no buffer for show frame\n");
+		} else {
+			bcopy(env->out.buf.data, bmp->pixels[0], 4*l4);
+			bcopy(env->out.buf.data + 4*l4, bmp->pixels[2], l4);
+			bcopy(env->out.buf.data + 5*l4, bmp->pixels[1], l4);
+		}
 	} else {	/* decode */
+		/* env->initialized guarantees that in.frame exists */
 #if 0 /* XXX img_convert is deprecated */
 		img_convert(&pict, PIX_FMT_YUV420P, (AVPicture *)env->in.frame, c->pix_fmt, c->width, c->height);
 #else /* XXX replacement */
@@ -712,10 +688,12 @@
 	rect.w = w;
 	rect.h = h;
 	SDL_DisplayYUVOverlay(bmp, &rect);
-}
-
-/*
- * This function is called (by asterisk) for each video packet that needs to be processed.
+	ast_mutex_unlock(&env->lock);
+}
+
+/*
+ * This function is called (by asterisk) for each video packet
+ * coming from the network (the 'in' path) that needs to be processed.
  * We need to reconstruct the entire video frame before we can decode it.
  * After a video packet is received we have to:
  * - extract the bitstream with pre_process_data()
@@ -731,9 +709,10 @@
 	struct timeval now = ast_tvnow();
 	int i;
 
-	ffmpeg_init(env, f->subclass);
-	if(!env->initialized)
+	if(!env->initialized) {
+		ast_log(LOG_WARNING, "here env should be already initialized\n");
 		return -1;	/* error */
+	}
 
 	if (0) {	/* echo frames back to the sender */
 		struct ast_frame *f1, **fp;
@@ -957,32 +936,56 @@
 	tvnow = ast_tvnow();
 	period = 1000/env->out.fps;
 	fdiff = ast_tvdiff_ms(tvnow, env->out.msts);
-	if (fdiff < period)
+	if (fdiff < period)	/* drop frame if too early */
 		return NULL;
 
 	env->out.msts.tv_sec = tvnow.tv_sec;
 	env->out.msts.tv_usec = tvnow.tv_usec;
 	/* get frame and put them in the queue */
+	show_frame(env, 1);
 	fprintf(stderr, "webcam_encode\n");
-	webcam_buflen = webcam_encode(env);
+	webcam_buflen = avcodec_encode_video(env->out.context,
+		env->out.decbuf.data, env->out.decbuf.size, env->out.frame);
 	fprintf(stderr, "split_frame\n");
 	return split_frame(&(env->out), webcam_buflen);
 }
 
+/*!
+ * The first call to the video code, called by the *_new()
+ * call (in turn, called by .answer or .dial) when the channel
+ * is created. Here we do the following:
+ *   - make sure that avcodec is properly initialized;
+ *   - try to open the webcam
+ */
 static void console_video_start(struct video_desc *env,
 	struct ast_channel *owner)
 {
-	avcodec_init();
 	/*
 	 * Register all codecs supported by the ffmpeg library.
 	 * Doing it once is enough.
 	 */
+	ast_mutex_init(&env->lock);
+	avcodec_init();
 	avcodec_register_all();
-
-	fprintf(stderr, "webcam_open\n");
-	webcam_open(env);
-	fprintf(stderr, "ffmpeg_init\n");
-	ffmpeg_init(env, CONSOLE_FORMAT_VIDEO);
+	if (video_in_init(&env->in, CONSOLE_FORMAT_VIDEO)) {
+		fprintf(stderr, "Could not initialize input decoder - %s\n", SDL_GetError());
+	}
+
+	fprintf(stderr, "prepare SDL initialization\n");
+
+	env->screen = NULL;
+	env->bmp[0] = env->bmp[1] = NULL;
+
+	// SDL specific
+	if(SDL_Init(SDL_INIT_VIDEO)) {
+		fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
+		return;
+	}
+	/* create windows ? */
+	env->initialized = 1;
+	webcam_open(env);	/* also allocate out.buf.data */
+	show_frame(env, 1);
+
 	if (env->out.buf.data && owner) {  /* drive webcam */
 		owner->fds[1] = env->out.fd;
 		fprintf(stderr, "video_out_init\n");




More information about the asterisk-commits mailing list