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