From d0f994ffda9820e5890c16c03762874880b7e34b Mon Sep 17 00:00:00 2001
From: Yalu Zhang <yalu.zhang@iopsys.eu>
Date: Fri, 31 May 2024 14:47:14 +0200
Subject: [PATCH] Reduce the jitter to handle the RTP packets from voicemngr

Change the scheduling policy and proirity of the thread to run pe_base_run.
---
 src/channels/chan_voicemngr.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/src/channels/chan_voicemngr.c b/src/channels/chan_voicemngr.c
index 16bbfdb..e986ef1 100644
--- a/src/channels/chan_voicemngr.c
+++ b/src/channels/chan_voicemngr.c
@@ -39,6 +39,8 @@
 #include <semaphore.h>
 #include <sys/types.h>
 #include <sys/stat.h>  // mkfifo()
+#include <sched.h>
+#include <string.h>
 
 #include "asterisk/lock.h"
 #include "asterisk/channel.h"
@@ -5308,9 +5310,26 @@ static int ubus_init(void) {
 
 // pthread wrapper for lib picoevent dispatcher
 static void *pe_base_run(void *unused) {
-	int delay;
+	int delay, policy, priority_min, priority_max;
+	struct sched_param param = { .sched_priority = 0, };
+	pthread_t thread = pthread_self();
+	int tid = ast_get_tid();
+
+	// Change the thread schedule policy and priority
+	priority_min = sched_get_priority_min(SCHED_FIFO);
+	priority_max = sched_get_priority_max(SCHED_FIFO);
+	param.sched_priority = priority_max;
+	if (pthread_setschedparam(thread, SCHED_FIFO, &param) != 0) {
+		ast_log(LOG_ERROR, "pthread_setschedparam failed, %s\n", strerror(errno));
+	}
+	// Get the new schedule policy and priority
+	policy = SCHED_FIFO - 1;
+	param.sched_priority = priority_min - 1; // set as an invalid value intentionally
+	if (pthread_getschedparam(thread, &policy, &param) != 0) {
+		ast_log(LOG_ERROR, "pthread_getschedparam failed, %s\n", strerror(errno));
+	}
+	ast_verbose("thread entry is %s, tid=%d, new schedule policy=%d, priority=%d\n", __func__, tid, policy, param.sched_priority);
 
-	ast_verbose("thread %d started\n", ast_get_tid());
 	for (delay = 0; delay < 5 && (!iflist || !cur_tech); delay++)
 		sleep(1);
 	pe_base_dispatch(base);
-- 
GitLab