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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Sep 17 15:44:28 CDT 2007


Author: rizzo
Date: Mon Sep 17 15:44:27 2007
New Revision: 82680

URL: http://svn.digium.com/view/asterisk?view=rev&rev=82680
Log:
initial implementation of a helper thread to take care of the
video read and encoding. At the moment it's only a proof of
concept - there is code to fire and shutdown the thread, and
comments on what it has to do.


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=82680&r1=82679&r2=82680
==============================================================================
--- team/rizzo/video_v2/channels/console_video.c (original)
+++ team/rizzo/video_v2/channels/console_video.c Mon Sep 17 15:44:27 2007
@@ -135,6 +135,10 @@
 struct video_desc {
 	ast_mutex_t lock;
 
+	pthread_t vthread;
+	int	shutdown;	/* set to ask the thread to shutdown */
+	struct ast_channel *owner;	/* owner channel */
+
 	int 	w;
 	int 	h;
 	int 	fps;
@@ -147,8 +151,6 @@
 	SDL_Surface             *screen;
 	int                     initialized;
 	SDL_Overlay             *bmp[2];
-
-	struct ast_frame *echo;
 
 };
 
@@ -550,15 +552,9 @@
  */
 static void console_video_uninit(struct video_desc *env)
 {
-	struct ast_frame *f;
-	int i = 0;
-
-	while ( (f = env->echo) != NULL) {
-		env->echo = AST_LIST_NEXT(f, frame_list);
-		ast_frfree(f);
-		i++;
-	}
-	ast_log(LOG_WARNING, "ffmpeg_uninit drop %d frames\n", i);
+	env->shutdown = 1;
+	pthread_join(env->vthread, NULL);
+	ast_log(LOG_WARNING, "thread terminated\n");
 	video_in_uninit(&env->in);
 	video_out_uninit(&env->out);
 	if (env->bmp[0])
@@ -1013,6 +1009,35 @@
 	return split_frame(&(env->out), buflen);
 }
 
+/*
+ * Helper thread to periodically poll the video source and enqueue the
+ * generated frames to the channel's queue.
+ * Using a separate thread also helps because the encoding can be
+ * computationally expensive so we don't want to starve the main thread.
+ */
+static void *video_thread(void *arg)
+{
+	int i = 0;
+	struct video_desc *env = arg;
+	for (;;i++) {
+		struct timeval t = { 1, 0 };
+		struct ast_frame *f;
+
+		if (env->shutdown) {
+			fprintf(stderr, "video_thread shutting down%d\n", i);
+			break;
+		}
+		// fprintf(stderr, "video_thread %d owner %p\n", i, env->owner);
+		ast_select(0, NULL, NULL, NULL, &t);
+		if (0) {
+		    f = get_video_frames(env);
+		    ast_channel_lock(env->owner);
+		    // enqueue on env->owner->readq
+		}
+	}
+	return NULL;
+}
+
 /*!
  * The first call to the video code, called by the *_new()
  * call (in turn, called by .answer or .dial) when the channel
@@ -1028,6 +1053,7 @@
 	 * Doing it once is enough.
 	 */
 	ast_mutex_init(&env->lock);
+
 	avcodec_init();
 	avcodec_register_all();
 	if (video_in_init(&env->in, CONSOLE_FORMAT_VIDEO)) {
@@ -1046,11 +1072,16 @@
 	}
 	/* create windows ? */
 	env->initialized = 1;
+	env->owner = owner;
+        ast_pthread_create_background(&env->vthread, NULL, video_thread, env);
 	video_open(env);	/* also allocate out.buf.data */
 	show_frame(env, 1);
 
 	if (env->out.buf.data && owner) {  /* talk to the local video source */
-		if (0 && env->out.fd >= 0)
+		/* 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