[feature][T8TSK-20][TCAM_T800_SW_0062] voice rtp interface realization

Change-Id: I2e084a3ddb52d59961f4b53da2c400b70da3e25b
diff --git a/src/lynq/lib/liblynq-call/lynq_call.cpp b/src/lynq/lib/liblynq-call/lynq_call.cpp
index 7359665..c7afdb0 100755
--- a/src/lynq/lib/liblynq-call/lynq_call.cpp
+++ b/src/lynq/lib/liblynq-call/lynq_call.cpp
@@ -26,6 +26,9 @@
 #define LYNQ_REQUEST_PARAM_BUF 8192

 #define LYQN_SEDN_BUF 1024*8+sizeof(int)*3

 #define INVALID_ID (-1)

+#define RTP_FROM_CMD "gst-launch-1.0 -v udpsrc port=%d caps=\'application/x-rtp, media=(string)audio, clock-rate=(int)%d, channels=(int)%d\' ! rtpjitterbuffer latency=%d ! rtppcmadepay ! alawdec ! audioresample ! audioconvert ! alsasink device=\'hw:0,2\'"

+#define RTP_TO_CMD   "gst-launch-1.0 -v alsasrc device=\'hw:0,6\' ! audioconvert ! audioresample ! alawenc ! rtppcmapay ! udpsink host=%s auto-multicast=true port=%d"

+#define MAX_IP_LENGTH 128

 #define USER_LOG_TAG "LYNQ_CALL"

 

 using ::android::Parcel;

@@ -130,6 +133,29 @@
 int isDial = 0;

 int lynqIncomingCallId = 0;

 

+/*rtp begin*/

+typedef struct

+{   

+    char ip[MAX_IP_LENGTH];

+    int port;   

+}lynq_rtp_server_info;

+

+typedef struct

+{       

+    int port;

+    int clockrate;  

+    int latency;

+    int channels;

+}lynq_rtp_client_info;

+

+static lynq_rtp_client_info g_rtp_client_info;

+static lynq_rtp_server_info g_rtp_server_info;

+

+static pthread_t g_rtp_thread[RTP_MODE_MAX];

+static bool g_rtp_thread_valid[RTP_MODE_MAX];

+

+/*rtp end*/

+

 #ifdef ECALL_SUPPORT

 int lynq_set_common_request(int request_id, int argc, const char* format,...);

 int g_ecallId = INVALID_ID;

@@ -157,7 +183,7 @@
 

 void sendSignalIncomingECallEvent()

 {

-    LYINFLOG("send incoming ecall event signal");;

+    LYINFLOG("send incoming ecall event signal");

     pthread_mutex_lock(&s_incoming_e_call_mutex);

     pthread_cond_signal(&s_incoming_e_call_cond);

     pthread_mutex_unlock(&s_incoming_e_call_mutex);

@@ -182,7 +208,6 @@
 {

     return (is_ecall_dial!=LYNQ_ECALL_DAILING_STATE_NONE) || (g_ecallId !=INVALID_ID);

 }

-

 #endif

 

 /**

@@ -938,6 +963,25 @@
 {

     LYDBGLOG("call state change,sim:%d",soc_id);

 }

+

+void lynq_init_rtp()

+{   

+    memset(&g_rtp_client_info,0,sizeof(g_rtp_client_info));

+    memset(&g_rtp_server_info,0,sizeof(g_rtp_server_info));    

+  

+

+    lynq_set_rtp_param(8000,1,400);

+

+    for(int i=0;i<RTP_MODE_MAX;i++)

+    {

+        lynq_set_rtp_port(i,5555);

+        g_rtp_thread_valid[i] = 0;

+    }          

+    

+    LYDBGLOG("lynq init rtp success!!!");

+    return;

+}

+

 int lynq_init_call(int uToken)

 {   

     if(g_lynq_call_init_flag == 1){

@@ -967,6 +1011,9 @@
         return -1;

     }

     memset(lynq_call_lists,0,sizeof(lynq_call_lists));

+

+    lynq_init_rtp();

+    

     LYDBGLOG("lynq init call success!!!");

     return 0;

 }

@@ -988,6 +1035,7 @@
         }

         urc_call_recive_status = 0;

         call_list_loop = 0;

+        lynq_set_voice_audio_mode(AUDIO_MODE_CODEC);

         if(lynq_call_urc_tid == -1 || lynq_call_list_loop_tid == -1)

         {

             return -1;

@@ -1607,34 +1655,217 @@
 #endif

 

 /*Audio Path setting begin*/

+/*sub function*/

+void lynq_set_rtp_mixer_ctrl(int enable_rtp)

+{

+    char cmd[256];

+    LYINFLOG("set_rtp_mixer_ctrl %d", enable_rtp);

+    if(enable_rtp)

+    {

+        sprintf(cmd, "amixer -D mtk_phonecall cset name=\"Speech_on\" %d", 0);

+        system(cmd);

+        sprintf(cmd, "amixer -D mtk_phonecall cset name=\"M2M_Speech_on\" %d", 1);

+        system(cmd);

+        sprintf(cmd, "amixer -c0 cset name=\"PCM_2_PB_CH1 DL2_CH1\" %d", 1);

+        system(cmd);

+        sprintf(cmd, "amixer -c0 cset name=\"PCM_2_PB_CH2 DL2_CH2\" %d", 1);

+        system(cmd);

+        sprintf(cmd, "amixer -c0 cset name=\"UL2_CH1 PCM_2_CAP_CH1\" %d", 1);

+        system(cmd);

+        sprintf(cmd, "amixer -c0 cset name=\"UL2_CH2 PCM_2_CAP_CH1\" %d", 1);

+        system(cmd);    

+    }

+    else 

+    {

+        sprintf(cmd, "amixer -D mtk_phonecall cset name=\"Speech_on\" %d", 1);

+        system(cmd);

+        sprintf(cmd, "amixer -D mtk_phonecall cset name=\"M2M_Speech_on\" %d", 0);

+        system(cmd);

+        sprintf(cmd, "amixer -c0 cset name=\"PCM_2_PB_CH1 DL2_CH1\" %d", 0);

+        system(cmd);

+        sprintf(cmd, "amixer -c0 cset name=\"PCM_2_PB_CH2 DL2_CH2\" %d", 0);

+        system(cmd);

+        sprintf(cmd, "amixer -c0 cset name=\"UL2_CH1 PCM_2_CAP_CH1\" %d", 0);

+        system(cmd);

+        sprintf(cmd, "amixer -c0 cset name=\"UL2_CH2 PCM_2_CAP_CH1\" %d", 0);

+        system(cmd);    

+    }    

+}

+

+void* lynq_start_rtp_cmd(void *arg)

+{

+    int* rtp_mode= (int*) arg;

+    char cmd[384];

+    LYINFLOG("lynq_start_rtp_cmd: rtp_mode is %d",(*rtp_mode));    

+    if ((*rtp_mode) == RTP_CLIENT)

+    {

+        sprintf(cmd,RTP_FROM_CMD, \

+            g_rtp_client_info.port,g_rtp_client_info.clockrate,g_rtp_client_info.channels, \

+            g_rtp_client_info.latency);            

+        LYINFLOG("start from rtp play: cmd is %s",cmd);    

+        system(cmd);

+    }

+    else if ((*rtp_mode) == RTP_SERVER)

+    {

+        sprintf(cmd,RTP_TO_CMD,   \

+            g_rtp_server_info.ip,g_rtp_server_info.port);

+        LYINFLOG("start to rtp play: cmd is %s",cmd);

+        system(cmd);    

+    }

+    return NULL;

+}

+

+int lynq_start_rtp_thread(int rtp_mode)

+{

+    int ret;

+    pthread_attr_t attr;

+    static int start_mode[RTP_MODE_MAX]={0,1};  

+

+    pthread_attr_init(&attr);

+    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

+    ret = pthread_create(&(g_rtp_thread[rtp_mode]),&attr,lynq_start_rtp_cmd,&(start_mode[rtp_mode]));

+    if(ret != 0)

+    {

+        g_rtp_thread_valid[rtp_mode]=0;

+        LYERRLOG("rtp create %d pthread error, ret is %d",rtp_mode, ret);

+        return ret;

+    }

+    g_rtp_thread_valid[rtp_mode]=1;

+    return 0;

+}

+

 /*set*/

 int lynq_set_voice_audio_mode(const LYNQ_Audio_Mode audio_mode)

 {

-    return 0;

+    int ret;

+    int i;

+    

+    if(audio_mode==AUDIO_MODE_RTP)

+    {

+        lynq_set_rtp_mixer_ctrl(1);

+        for(i=0;i<RTP_MODE_MAX;i++)

+        {

+            if(!g_rtp_thread_valid[i])

+            {

+                if(lynq_start_rtp_thread(i)!= 0)

+                {

+                    LYERRLOG("start rtp %d fail",i);

+                    break;

+                }

+                else 

+                {

+                    LYINFLOG("start rtp %d suc",i);        

+                }

+            }

+            else 

+            {

+                LYERRLOG("rtp %d needn't start",i); 

+            }

+        }

+        if(i!= RTP_MODE_MAX)            

+        {

+            LYERRLOG("start rtp whole fail");

+            lynq_set_voice_audio_mode(AUDIO_MODE_CODEC);

+            return 1;

+        }

+        LYINFLOG("start rtp whole suc");          

+        return 0;

+    }

+    else if(audio_mode==AUDIO_MODE_CODEC)

+    {

+         for(i=0;i<RTP_MODE_MAX;i++)

+        {

+            if(g_rtp_thread_valid[i]) 

+            {

+                 ret = pthread_cancel(g_rtp_thread[i]);

+                 LYINFLOG("pthread cancel rtp %d ret = %d",i,ret);

+                 ret = pthread_join(g_rtp_thread[i],NULL);

+                 LYINFLOG("pthread join rtp %d ret = %d",i,ret);

+                 g_rtp_thread_valid[i] = 0;

+            }

+            else 

+            {

+                LYINFLOG("rtp %d needn't stop",i);

+            }

+        }

+        lynq_set_rtp_mixer_ctrl(0);     

+        LYINFLOG("stop rtp suc");

+    }

+    return 0;     

 }

-int lynq_set_rtp_ip(const LYNQ_Rtp_Mode rtp_mode, const char* ip, const int ip_length)

+int lynq_set_remote_rtp_ip(const char* ip, const int ip_length)

 {

+    if (NULL == ip)

+    {

+        LYERRLOG("ip is NULL!!!");

+        return -1;

+    }

+    if ((ip_length < strlen(ip)+1) || (ip_length > MAX_IP_LENGTH))

+    {

+        LYERRLOG("incoming ip length error %d", ip_length);

+        return -1;

+    }

+

+    bzero(g_rtp_server_info.ip,MAX_IP_LENGTH);

+    strcpy(g_rtp_server_info.ip,ip);

+

+    LYINFLOG("lynq_set_remote_rtp_ip suc: ip is %s, length is %d", ip,ip_length);

+    

     return 0;

 }

 int lynq_set_rtp_port(const LYNQ_Rtp_Mode rtp_mode, const int port)

 {  

+    if (port < 0)

+    {

+        LYERRLOG("invalid port number %d", port);

+        return -1;

+    }

+    if (rtp_mode == 0)

+    {

+        g_rtp_client_info.port = port;

+    }

+    else if (rtp_mode == 1)

+    {

+        g_rtp_server_info.port = port;

+    }

+    LYINFLOG("lynq_set_rtp_port suc: LYNQ_Rtp_Mode is %d, port is %d", rtp_mode, port);

     return 0;

 }

 int lynq_set_rtp_param(const int clock_rate,const int channels,const int latency) //only for client mode

 {

+    g_rtp_client_info.clockrate = clock_rate;

+    g_rtp_client_info.channels = channels;

+    g_rtp_client_info.latency = latency;

+    LYINFLOG("lynq_set_rtp_param suc: clockrate is %d, channels is %d, latency is %d", clock_rate, channels, latency);

     return 0;

 }

 /*get*/

 LYNQ_Audio_Mode lynq_get_voice_audio_mode()

 {

-    return AUDIO_MODE_CODEC;

+    if(g_rtp_thread_valid[0])

+    {

+        return AUDIO_MODE_RTP;

+    }

+    else 

+    {

+        return AUDIO_MODE_CODEC;

+    }

 }

-int lynq_get_rtp_ip(const LYNQ_Rtp_Mode rtp_mode, char* ip, const int ip_length)

+int lynq_get_remote_rtp_ip(char* ip, const int ip_length)

 {

     if(ip==NULL)

     {

+        LYERRLOG("ip is NULL");

         return 1;

     }    

+

+    if(ip_length < strlen(g_rtp_server_info.ip)+1)

+    {

+        LYERRLOG("ip lenght %d is shorter than saved ip length %d",ip_length,strlen(g_rtp_server_info.ip)+1);

+        return 1;

+    }

+    bzero(ip,ip_length);

+    strcpy(ip,g_rtp_server_info.ip);

     return 0;

 }

 int lynq_get_rtp_port(const LYNQ_Rtp_Mode rtp_mode, int* port)

@@ -1642,16 +1873,29 @@
     if(port==NULL)

     {

         return 1;

-    }    

+    }   

+    if (rtp_mode == 0)

+    {

+        *port = g_rtp_client_info.port;

+    }

+    else if (rtp_mode == 1)

+    {

+        *port = g_rtp_server_info.port;

+    }

     return 0;

 }

 int lynq_get_rtp_param(int* clock_rate, int* channels, int* latency)//only for client mode

 {

     if(clock_rate == NULL || channels ==NULL || latency ==NULL)

     {

+        LYERRLOG("input parameter is NULL");

         return 1;

     }

 

+    *clock_rate = g_rtp_client_info.clockrate;

+    *channels = g_rtp_client_info.channels ;

+    *latency = g_rtp_client_info.latency;

+

     return 0;

 }

 /*Audio Path setting end*/