From 3d7fbd17beea10230740cc97be1fab6c17769dfb Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Thu, 25 Feb 2016 16:11:28 -0600
Subject: [PATCH] FS-8876 #resolve [Bind video threads to CPU alternating]

---
 src/switch_core_media.c | 39 ++++++++++++++++++++++++++++++++++++++-
 src/switch_vpx.c        |  4 ++--
 2 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/src/switch_core_media.c b/src/switch_core_media.c
index 0cb2bb6872..086ec3b9fd 100644
--- a/src/switch_core_media.c
+++ b/src/switch_core_media.c
@@ -66,7 +66,14 @@ typedef struct secure_settings_s {
 	char *remote_crypto_key;
 } switch_secure_settings_t;
 
+typedef struct core_video_globals_s {
+	int cpu_count;
+	int cur_cpu;
+	switch_memory_pool_t *pool;
+	switch_mutex_t *mutex;
+} core_video_globals_t;
 
+static core_video_globals_t video_globals = { 0 };
 
 struct media_helper {
 	switch_core_session_t *session;
@@ -5008,6 +5015,27 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_set_video_file(switch_core_ses
 	return SWITCH_STATUS_SUCCESS;
 }
 
+int next_cpu(void)
+{
+	int x = 0;
+
+	switch_mutex_lock(video_globals.mutex);
+	x = video_globals.cur_cpu++;
+	if (video_globals.cur_cpu == video_globals.cpu_count) {
+		video_globals.cur_cpu = 0;
+	}
+	switch_mutex_unlock(video_globals.mutex);
+	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Binding to CPU %d\n", x);
+
+	return x;
+}
+
+static void bind_cpu(void)
+{
+	switch_core_thread_set_cpu_affinity(next_cpu());
+}
+
+
 static void *SWITCH_THREAD_FUNC video_helper_thread(switch_thread_t *thread, void *obj)
 {
 	struct media_helper *mh = obj;
@@ -5029,6 +5057,8 @@ static void *SWITCH_THREAD_FUNC video_helper_thread(switch_thread_t *thread, voi
 		return NULL;
 	}
 
+	bind_cpu();
+
 	if ((var = switch_channel_get_variable(session->channel, "core_video_blank_image"))) {
 		blank_img = switch_img_read_png(var, SWITCH_IMG_FMT_I420);
 	}
@@ -10283,11 +10313,18 @@ SWITCH_DECLARE (void) switch_core_media_recover_session(switch_core_session_t *s
 SWITCH_DECLARE(void) switch_core_media_init(void)
 {
 	switch_core_gen_certs(DTLS_SRTP_FNAME ".pem");	
+
+	video_globals.cpu_count = switch_core_cpu_count();
+	video_globals.cur_cpu = 0;
+
+	switch_core_new_memory_pool(&video_globals.pool);
+	switch_mutex_init(&video_globals.mutex, SWITCH_MUTEX_NESTED, video_globals.pool);
+
 }
 
 SWITCH_DECLARE(void) switch_core_media_deinit(void)
 {
-	
+	switch_core_destroy_memory_pool(&video_globals.pool);
 }
 
 static int payload_number(const char *name)
diff --git a/src/switch_vpx.c b/src/switch_vpx.c
index 58b7e2e147..e49fa4c797 100644
--- a/src/switch_vpx.c
+++ b/src/switch_vpx.c
@@ -304,7 +304,7 @@ static switch_status_t init_decoder(switch_codec_t *codec)
 		//	context->decoder_init = 0;
 		//}
 
-		cfg.threads = switch_core_cpu_count();
+		cfg.threads = 1;//(switch_core_cpu_count() > 1) ? 2 : 1;
 
 		if (!context->is_vp9) { // vp8 only
 			// dec_flags = VPX_CODEC_USE_POSTPROC;
@@ -391,7 +391,7 @@ static switch_status_t init_encoder(switch_codec_t *codec)
 	config->rc_target_bitrate = context->bandwidth;
 	config->g_lag_in_frames = 0;
 	config->kf_max_dist = 2000;
-	config->g_threads = (cpus > 1) ? 2 : 1;
+	config->g_threads = 1;//(cpus > 1) ? 2 : 1;
 	
 	if (context->is_vp9) {
 		//config->rc_dropframe_thresh = 2;