q.huang | 5292166 | 2022-10-20 15:25:45 +0800 | [diff] [blame] | 1 |
|
| 2 | #include <stdio.h>
|
| 3 | #include <sys/types.h>
|
| 4 | #include <sys/socket.h>
|
| 5 | #include <arpa/inet.h>
|
| 6 | #include <fcntl.h>
|
| 7 | #include <string.h>
|
| 8 | #include <stdlib.h>
|
| 9 | #include <unistd.h>
|
| 10 | #include <binder/Parcel.h>
|
| 11 | #include <log/log.h>
|
| 12 | #include <cutils/jstring.h>
|
| 13 | #include <pthread.h>
|
| 14 | #include "liblog/lynq_deflog.h"
|
| 15 | #include <sys/time.h>
|
| 16 | #include <string.h>
|
| 17 | #include "lynq_call.h"
|
| 18 | #include "lynq_module_common.h"
|
| 19 | #include "lynq_call_common.h"
|
| 20 |
|
| 21 | #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\'"
|
q.huang | eacb812 | 2023-04-04 16:50:53 +0800 | [diff] [blame^] | 22 | #define RTP_TO_CMD "gst-launch-1.0 -v alsasrc device=\'hw:0,6\' ! audioconvert ! audioresample ! alawenc ! rtppcmapay ! %s udpsink host=%s %s auto-multicast=true port=%d"
|
q.huang | 5292166 | 2022-10-20 15:25:45 +0800 | [diff] [blame] | 23 | #define RTP_VLAN_INFO_FORMAT "multicast-iface=\"%s\""
|
q.huang | eacb812 | 2023-04-04 16:50:53 +0800 | [diff] [blame^] | 24 | #define RTP_SSRC_INFO_FORMAT " rtpmux name=mux ssrc=%u !"
|
| 25 |
|
q.huang | 5292166 | 2022-10-20 15:25:45 +0800 | [diff] [blame] | 26 | #define USER_LOG_TAG "LYNQ_CALL"
|
| 27 |
|
| 28 | typedef struct
|
| 29 | {
|
| 30 | char ip[MAX_IP_LENGTH];
|
| 31 | int port;
|
q.huang | eacb812 | 2023-04-04 16:50:53 +0800 | [diff] [blame^] | 32 | LYNQ_Rtp_Ssrc_Mode ssrc_mode;
|
| 33 | unsigned int ssrc;
|
q.huang | 5292166 | 2022-10-20 15:25:45 +0800 | [diff] [blame] | 34 | char vlan_info[MAX_VLAN_INFO_LENGTH];
|
| 35 | }lynq_rtp_server_info;
|
| 36 |
|
| 37 | typedef struct
|
| 38 | {
|
| 39 | int port;
|
| 40 | int clockrate;
|
| 41 | int latency;
|
| 42 | int channels;
|
| 43 | }lynq_rtp_client_info;
|
| 44 |
|
| 45 | static lynq_rtp_client_info g_rtp_client_info;
|
| 46 | static lynq_rtp_server_info g_rtp_server_info;
|
| 47 |
|
| 48 | static pthread_t g_rtp_thread[RTP_MODE_MAX];
|
| 49 | static bool g_rtp_thread_valid[RTP_MODE_MAX];
|
| 50 |
|
| 51 | void lynq_init_rtp()
|
| 52 | {
|
| 53 | memset(&g_rtp_client_info,0,sizeof(g_rtp_client_info));
|
| 54 | memset(&g_rtp_server_info,0,sizeof(g_rtp_server_info));
|
| 55 |
|
| 56 |
|
| 57 | lynq_set_rtp_param(8000,1,400);
|
| 58 |
|
| 59 | for(int i=0;i<RTP_MODE_MAX;i++)
|
| 60 | {
|
| 61 | lynq_set_rtp_port(i,6666);
|
| 62 | g_rtp_thread_valid[i] = 0;
|
| 63 | }
|
| 64 |
|
| 65 | LYDBGLOG("lynq init rtp success!!!");
|
| 66 | return;
|
| 67 | }
|
| 68 |
|
| 69 | /*Audio Path setting begin*/
|
| 70 | /*sub function*/
|
| 71 | void lynq_set_rtp_mixer_ctrl(int enable_rtp)
|
| 72 | {
|
| 73 | char cmd[256];
|
| 74 | LYINFLOG("set_rtp_mixer_ctrl %d", enable_rtp);
|
| 75 | if(enable_rtp)
|
| 76 | {
|
| 77 | sprintf(cmd, "amixer -D mtk_phonecall cset name=\"Speech_on\" %d", 0);
|
| 78 | system(cmd);
|
| 79 | sprintf(cmd, "amixer -D mtk_phonecall cset name=\"M2M_Speech_on\" %d", 1);
|
| 80 | system(cmd);
|
| 81 | sprintf(cmd, "amixer -c0 cset name=\"PCM_2_PB_CH1 DL2_CH1\" %d", 1);
|
| 82 | system(cmd);
|
| 83 | sprintf(cmd, "amixer -c0 cset name=\"PCM_2_PB_CH2 DL2_CH2\" %d", 1);
|
| 84 | system(cmd);
|
| 85 | sprintf(cmd, "amixer -c0 cset name=\"UL2_CH1 PCM_2_CAP_CH1\" %d", 1);
|
| 86 | system(cmd);
|
| 87 | sprintf(cmd, "amixer -c0 cset name=\"UL2_CH2 PCM_2_CAP_CH1\" %d", 1);
|
| 88 | system(cmd);
|
| 89 | }
|
| 90 | else
|
q.huang | f36a3ef | 2022-12-30 18:52:30 +0800 | [diff] [blame] | 91 | {
|
q.huang | 5292166 | 2022-10-20 15:25:45 +0800 | [diff] [blame] | 92 | sprintf(cmd, "amixer -D mtk_phonecall cset name=\"M2M_Speech_on\" %d", 0);
|
| 93 | system(cmd);
|
| 94 | sprintf(cmd, "amixer -c0 cset name=\"PCM_2_PB_CH1 DL2_CH1\" %d", 0);
|
| 95 | system(cmd);
|
| 96 | sprintf(cmd, "amixer -c0 cset name=\"PCM_2_PB_CH2 DL2_CH2\" %d", 0);
|
| 97 | system(cmd);
|
| 98 | sprintf(cmd, "amixer -c0 cset name=\"UL2_CH1 PCM_2_CAP_CH1\" %d", 0);
|
| 99 | system(cmd);
|
| 100 | sprintf(cmd, "amixer -c0 cset name=\"UL2_CH2 PCM_2_CAP_CH1\" %d", 0);
|
| 101 | system(cmd);
|
q.huang | f36a3ef | 2022-12-30 18:52:30 +0800 | [diff] [blame] | 102 | sprintf(cmd, "amixer -D mtk_phonecall cset name=\"Speech_on\" %d", 1);
|
| 103 | system(cmd);
|
q.huang | 5292166 | 2022-10-20 15:25:45 +0800 | [diff] [blame] | 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | void* lynq_start_rtp_cmd(void *arg)
|
| 108 | {
|
| 109 | int* rtp_mode= (int*) arg;
|
| 110 | char cmd[384];
|
| 111 | char vlan_para_string[sizeof(RTP_VLAN_INFO_FORMAT)+MAX_VLAN_INFO_LENGTH-2/*sizeof "%s"*/]={0};
|
q.huang | eacb812 | 2023-04-04 16:50:53 +0800 | [diff] [blame^] | 112 | char ssrc_para_string[sizeof(RTP_SSRC_INFO_FORMAT)+MAX_SSRC_INFO_LENGTH-2/*sizeof "%u"*/]={0};
|
q.huang | 5292166 | 2022-10-20 15:25:45 +0800 | [diff] [blame] | 113 | LYINFLOG("lynq_start_rtp_cmd: rtp_mode is %d",(*rtp_mode));
|
| 114 | if ((*rtp_mode) == RTP_CLIENT)
|
| 115 | {
|
| 116 | sprintf(cmd,RTP_FROM_CMD, \
|
| 117 | g_rtp_client_info.port,g_rtp_client_info.clockrate,g_rtp_client_info.channels, \
|
| 118 | g_rtp_client_info.latency);
|
| 119 | // LYINFLOG("start from rtp play: cmd is %s",cmd);
|
| 120 | system(cmd);
|
| 121 | }
|
| 122 | else if ((*rtp_mode) == RTP_SERVER)
|
| 123 | {
|
| 124 | if(strlen(g_rtp_server_info.vlan_info)>0)
|
| 125 | {
|
| 126 | sprintf(vlan_para_string,RTP_VLAN_INFO_FORMAT,g_rtp_server_info.vlan_info);
|
| 127 | }
|
q.huang | eacb812 | 2023-04-04 16:50:53 +0800 | [diff] [blame^] | 128 |
|
| 129 | if(g_rtp_server_info.ssrc_mode == RTP_Ssrc_specific)
|
| 130 | {
|
| 131 | sprintf(ssrc_para_string,RTP_SSRC_INFO_FORMAT,g_rtp_server_info.ssrc);
|
| 132 | }
|
q.huang | 5292166 | 2022-10-20 15:25:45 +0800 | [diff] [blame] | 133 | sprintf(cmd,RTP_TO_CMD, \
|
q.huang | eacb812 | 2023-04-04 16:50:53 +0800 | [diff] [blame^] | 134 | ssrc_para_string, g_rtp_server_info.ip,vlan_para_string,g_rtp_server_info.port);
|
q.huang | 5292166 | 2022-10-20 15:25:45 +0800 | [diff] [blame] | 135 |
|
q.huang | eacb812 | 2023-04-04 16:50:53 +0800 | [diff] [blame^] | 136 | LYINFLOG("start to rtp play: cmd is %s",cmd);
|
q.huang | 5292166 | 2022-10-20 15:25:45 +0800 | [diff] [blame] | 137 | system(cmd);
|
| 138 | }
|
| 139 | return NULL;
|
| 140 | }
|
| 141 |
|
| 142 | int lynq_start_rtp_thread(int rtp_mode)
|
| 143 | {
|
| 144 | int ret;
|
| 145 | pthread_attr_t attr;
|
| 146 | static int start_mode[RTP_MODE_MAX]={0,1};
|
| 147 |
|
| 148 | pthread_attr_init(&attr);
|
| 149 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
| 150 | ret = pthread_create(&(g_rtp_thread[rtp_mode]),&attr,lynq_start_rtp_cmd,&(start_mode[rtp_mode]));
|
| 151 | if(ret != 0)
|
| 152 | {
|
| 153 | g_rtp_thread_valid[rtp_mode]=0;
|
| 154 | LYERRLOG("rtp create %d pthread error, ret is %d",rtp_mode, ret);
|
| 155 | return ret;
|
| 156 | }
|
| 157 | g_rtp_thread_valid[rtp_mode]=1;
|
| 158 | return RESULT_OK;
|
| 159 | }
|
| 160 |
|
| 161 | /*set*/
|
| 162 | int lynq_set_voice_audio_mode(const LYNQ_Audio_Mode audio_mode)
|
| 163 | {
|
| 164 | int ret;
|
| 165 | int i;
|
| 166 |
|
| 167 | if(audio_mode==AUDIO_MODE_RTP)
|
| 168 | {
|
| 169 | lynq_set_rtp_mixer_ctrl(1);
|
| 170 | for(i=0;i<RTP_MODE_MAX;i++)
|
| 171 | {
|
| 172 | if(!g_rtp_thread_valid[i])
|
| 173 | {
|
| 174 | if(lynq_start_rtp_thread(i)!= 0)
|
| 175 | {
|
| 176 | LYERRLOG("start rtp %d fail",i);
|
| 177 | break;
|
| 178 | }
|
| 179 | else
|
| 180 | {
|
| 181 | LYINFLOG("start rtp %d suc",i);
|
| 182 | }
|
| 183 | }
|
| 184 | else
|
| 185 | {
|
| 186 | LYERRLOG("rtp %d needn't start",i);
|
| 187 | }
|
| 188 | }
|
| 189 | if(i!= RTP_MODE_MAX)
|
| 190 | {
|
| 191 | LYERRLOG("start rtp whole fail");
|
| 192 | lynq_set_voice_audio_mode(AUDIO_MODE_CODEC);
|
| 193 | return RESULT_ERROR;
|
| 194 | }
|
| 195 | LYINFLOG("start rtp whole suc");
|
| 196 | return RESULT_OK;
|
| 197 | }
|
| 198 | else if(audio_mode==AUDIO_MODE_CODEC)
|
| 199 | {
|
| 200 | for(i=0;i<RTP_MODE_MAX;i++)
|
| 201 | {
|
| 202 | if(g_rtp_thread_valid[i])
|
| 203 | {
|
| 204 | ret = pthread_cancel(g_rtp_thread[i]);
|
| 205 | LYINFLOG("pthread cancel rtp %d ret = %d",i,ret);
|
| 206 | ret = pthread_join(g_rtp_thread[i],NULL);
|
| 207 | LYINFLOG("pthread join rtp %d ret = %d",i,ret);
|
| 208 | g_rtp_thread_valid[i] = 0;
|
| 209 | }
|
| 210 | else
|
| 211 | {
|
| 212 | LYINFLOG("rtp %d needn't stop",i);
|
| 213 | }
|
| 214 | }
|
| 215 | lynq_set_rtp_mixer_ctrl(0);
|
| 216 | LYINFLOG("stop rtp suc");
|
| 217 | }
|
| 218 | return RESULT_OK;
|
| 219 | }
|
| 220 | int lynq_set_remote_rtp_ip(const char* ip, const int ip_length)
|
| 221 | {
|
| 222 | if (NULL == ip)
|
| 223 | {
|
| 224 | LYERRLOG("ip is NULL!!!");
|
| 225 | return LYNQ_E_PARAMETER_ANONALY;
|
| 226 | }
|
| 227 | if ((ip_length < strlen(ip)+1) || (ip_length > MAX_IP_LENGTH))
|
| 228 | {
|
| 229 | LYERRLOG("incoming ip length error %d", ip_length);
|
| 230 | return LYNQ_E_PARAMETER_ANONALY;
|
| 231 | }
|
| 232 |
|
| 233 | bzero(g_rtp_server_info.ip,MAX_IP_LENGTH);
|
| 234 | strcpy(g_rtp_server_info.ip,ip);
|
| 235 |
|
| 236 | LYINFLOG("lynq_set_remote_rtp_ip suc: ip is %s, length is %d", ip,ip_length);
|
| 237 |
|
| 238 | return RESULT_OK;
|
| 239 | }
|
| 240 | int lynq_set_vlan_info(const char* vlan_info, const int vlan_info_length)
|
| 241 | {
|
| 242 | if (NULL == vlan_info)
|
| 243 | {
|
| 244 | LYERRLOG("vlan_info is NULL!!!");
|
| 245 | return LYNQ_E_PARAMETER_ANONALY;
|
| 246 | }
|
| 247 |
|
| 248 | if ((vlan_info_length < strlen(vlan_info)+1) || (vlan_info_length > MAX_VLAN_INFO_LENGTH))
|
| 249 | {
|
| 250 | LYERRLOG("incoming vlan_info error, vlan info length %d", vlan_info_length);
|
| 251 | return LYNQ_E_PARAMETER_ANONALY;
|
| 252 | }
|
| 253 |
|
| 254 |
|
| 255 | bzero(g_rtp_server_info.vlan_info,MAX_VLAN_INFO_LENGTH);
|
| 256 | strcpy(g_rtp_server_info.vlan_info,vlan_info);
|
| 257 |
|
| 258 | LYINFLOG("lynq_set_vlan_info suc: vlan is %s, length is %d", vlan_info,vlan_info_length);
|
| 259 |
|
| 260 | return RESULT_OK;
|
| 261 | }
|
| 262 | int lynq_set_rtp_port(const LYNQ_Rtp_Mode rtp_mode, const int port)
|
| 263 | {
|
| 264 | if (port < 0)
|
| 265 | {
|
| 266 | LYERRLOG("invalid port number %d", port);
|
| 267 | return LYNQ_E_PARAMETER_ANONALY;
|
| 268 | }
|
| 269 | if (rtp_mode == 0)
|
| 270 | {
|
| 271 | g_rtp_client_info.port = port;
|
| 272 | }
|
| 273 | else if (rtp_mode == 1)
|
| 274 | {
|
| 275 | g_rtp_server_info.port = port;
|
| 276 | }
|
| 277 | LYINFLOG("lynq_set_rtp_port suc: LYNQ_Rtp_Mode is %d, port is %d", rtp_mode, port);
|
| 278 | return RESULT_OK;
|
| 279 | }
|
| 280 | int lynq_set_rtp_param(const int clock_rate,const int channels,const int latency) //only for client mode
|
| 281 | {
|
| 282 | g_rtp_client_info.clockrate = clock_rate;
|
| 283 | g_rtp_client_info.channels = channels;
|
| 284 | g_rtp_client_info.latency = latency;
|
| 285 | LYINFLOG("lynq_set_rtp_param suc: clockrate is %d, channels is %d, latency is %d", clock_rate, channels, latency);
|
| 286 | return RESULT_OK;
|
| 287 | }
|
q.huang | eacb812 | 2023-04-04 16:50:53 +0800 | [diff] [blame^] | 288 | int lynq_set_call_rtp_ssrc(const LYNQ_Rtp_Ssrc_Mode ssrc_mode,const unsigned int ssrc)
|
| 289 | {
|
| 290 | if(ssrc_mode < Rtp_Ssrc_random || ssrc_mode > RTP_Ssrc_specific)
|
| 291 | {
|
| 292 | return LYNQ_E_PARAMETER_ANONALY;
|
| 293 | }
|
| 294 | g_rtp_server_info.ssrc_mode = ssrc_mode;
|
| 295 | g_rtp_server_info.ssrc = ssrc ;
|
| 296 | LYINFLOG("%s suc: param is %d %d", __func__, ssrc_mode, ssrc);
|
| 297 | return RESULT_OK;
|
| 298 | }
|
q.huang | 5292166 | 2022-10-20 15:25:45 +0800 | [diff] [blame] | 299 | /*get*/
|
| 300 | LYNQ_Audio_Mode lynq_get_voice_audio_mode()
|
| 301 | {
|
| 302 | if(g_rtp_thread_valid[0])
|
| 303 | {
|
| 304 | return AUDIO_MODE_RTP;
|
| 305 | }
|
| 306 | else
|
| 307 | {
|
| 308 | return AUDIO_MODE_CODEC;
|
| 309 | }
|
| 310 | }
|
| 311 | int lynq_get_remote_rtp_ip(char* ip, const int ip_length)
|
| 312 | {
|
| 313 | if(ip==NULL)
|
| 314 | {
|
| 315 | LYERRLOG("ip is NULL");
|
| 316 | return LYNQ_E_PARAMETER_ANONALY;
|
| 317 | }
|
| 318 |
|
| 319 | if(ip_length < strlen(g_rtp_server_info.ip)+1)
|
| 320 | {
|
| 321 | LYERRLOG("ip length %d is shorter than saved ip length %d",ip_length,strlen(g_rtp_server_info.ip)+1);
|
| 322 | return LYNQ_E_PARAMETER_ANONALY;
|
| 323 | }
|
| 324 |
|
| 325 | bzero(ip,ip_length);
|
| 326 | strcpy(ip,g_rtp_server_info.ip);
|
| 327 |
|
| 328 | return RESULT_OK;
|
| 329 | }
|
| 330 | int lynq_get_vlan_info(char* vlan_info, const int vlan_info_length)
|
| 331 | {
|
| 332 | if(vlan_info==NULL)
|
| 333 | {
|
| 334 | LYERRLOG("vlan info is NULL");
|
| 335 | return LYNQ_E_PARAMETER_ANONALY;
|
| 336 | }
|
| 337 |
|
| 338 | if(vlan_info_length < strlen(g_rtp_server_info.vlan_info)+1)
|
| 339 | {
|
| 340 | LYERRLOG("vlan info length %d is shorter than saved vlan info length %d",vlan_info_length,strlen(g_rtp_server_info.vlan_info)+1);
|
| 341 | return LYNQ_E_PARAMETER_ANONALY;
|
| 342 | }
|
| 343 |
|
| 344 | bzero(vlan_info,vlan_info_length);
|
| 345 | strcpy(vlan_info,g_rtp_server_info.vlan_info);
|
| 346 |
|
| 347 | return RESULT_OK;
|
| 348 | }
|
| 349 | int lynq_get_rtp_port(const LYNQ_Rtp_Mode rtp_mode, int* port)
|
| 350 | {
|
| 351 | if(port==NULL)
|
| 352 | {
|
| 353 | return LYNQ_E_PARAMETER_ANONALY;
|
| 354 | }
|
| 355 | if (rtp_mode == 0)
|
| 356 | {
|
| 357 | *port = g_rtp_client_info.port;
|
| 358 | }
|
| 359 | else if (rtp_mode == 1)
|
| 360 | {
|
| 361 | *port = g_rtp_server_info.port;
|
| 362 | }
|
| 363 | return RESULT_OK;
|
| 364 | }
|
| 365 | int lynq_get_rtp_param(int* clock_rate, int* channels, int* latency)//only for client mode
|
| 366 | {
|
| 367 | if(clock_rate == NULL || channels ==NULL || latency ==NULL)
|
| 368 | {
|
| 369 | LYERRLOG("input parameter is NULL");
|
| 370 | return LYNQ_E_PARAMETER_ANONALY;
|
| 371 | }
|
| 372 |
|
| 373 | *clock_rate = g_rtp_client_info.clockrate;
|
| 374 | *channels = g_rtp_client_info.channels ;
|
| 375 | *latency = g_rtp_client_info.latency;
|
| 376 |
|
| 377 | return RESULT_OK;
|
| 378 | }
|
q.huang | eacb812 | 2023-04-04 16:50:53 +0800 | [diff] [blame^] | 379 | int lynq_get_call_rtp_ssrc(LYNQ_Rtp_Ssrc_Mode* ssrc_mode, unsigned int* ssrc)
|
| 380 | {
|
| 381 | if(ssrc_mode == NULL || ssrc ==NULL)
|
| 382 | {
|
| 383 | LYERRLOG("input parameter is NULL");
|
| 384 | return LYNQ_E_PARAMETER_ANONALY;
|
| 385 | }
|
| 386 |
|
| 387 | *ssrc_mode = g_rtp_server_info.ssrc_mode;
|
| 388 | *ssrc = g_rtp_server_info.ssrc ;
|
| 389 |
|
| 390 | return RESULT_OK;
|
| 391 | }
|