blob: 13f8637a788c9d8f7659476fc948c0c75f0957a0 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/******************************************************************************
2 *(C) Copyright 2019 ASR.
3 * All Rights Reserved
4 ******************************************************************************/
5/* -------------------------------------------------------------------------------------------------------------------
6 *
7 * Filename:VoIP_app_DEMO.c
8 *
9 * Authors:
10 *
11 * Description: The DEMO will record and playback PCM stream based on ASR1826.
12 *
13 *
14 * HISTORY:
15 *
16 *
17 *
18 * Notes:
19 *
20 ******************************************************************************/
21
22/******************************************************************************
23 * Include files
24 ******************************************************************************/
25#include "audio_if_types.h"
26#include "audio_if_ubus.h"
27#include "audio_if_parameter.h"
28#include "audio_if.h"
29#include "audio_if_api.h"
30#include <unistd.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <fcntl.h>
34#include "telatparamdef_ubus.h"
35#include <libubox/blobmsg_json.h>
36#include "libubus.h"
37#include <string.h>
38#include "audio_if_audio_hw_mrvl.h"
39#include "utlEventHandler.h"
40#include "udev_monitor.h"
41#include "audio_hw_mrvl.h"
42#include <stdlib.h>
43#include <stdio.h>
44#include <cutils/str_parms.h>
45#include "vcm.h"
46
47extern void* audio_hal_install(void);
48extern void audio_hal_uninstall(void);
49extern void configure_vcm(unsigned int data[]);
50/******************************************************************************
51 * Globals
52 ******************************************************************************/
53static audio_hw_device_t *VoIP_ahw_dev_ubus;
54
55struct audio_stream_in *stream_in = NULL;
56struct audio_stream_out *stream_out = NULL;
57bool go_on_pcmloopback = true;
58unsigned int pcm_record_size = 0; //320:NB, 640:WB
59unsigned int pcm_playback_size = 0; //320:NB, 640:WB
60
61/////////////////////////////////////////////////////////////////////////////
62
63static int config_parameters(int in_out)
64{
65 unsigned int direction = 0xFF, type, srcdst, priority, dest;
66 char kvpair[128];
67 struct str_parms *param = NULL;
68 int data[5];
69 const char *key = NULL;
70 bool update_vcm = false;
71
72 direction = in_out;/* 0-play, 1-record */
73 type = 0; /* 0:PCM_NB_BUF_SIZE, 1:PCM_WB_BUF_SIZE */
74 srcdst = 1;/* 0-None, 1-Near end, 2-Far end, 3-Both ends */
75 priority = 1;/* 0-Do not combine(override), 1-Combine */
76 dest = 1;/* 0-Near codec, 1-Near Vocoder */
77
78 if(direction == 0){//output
79 if(type == 0)
80 pcm_playback_size = PCM_NB_BUF_SIZE;
81 else
82 pcm_playback_size = PCM_WB_BUF_SIZE;
83
84 printf("config playback parameters.\n");
85 }
86 else if(direction == 1){//input
87 if(type == 0)
88 pcm_record_size = PCM_NB_BUF_SIZE;
89 else
90 pcm_record_size = PCM_WB_BUF_SIZE;
91
92 printf("config record parameters.\n");
93 }
94
95 memset(kvpair, 0x00, sizeof(kvpair));
96 sprintf(kvpair, "%s=%d;%s=%d;%s=%d;%s=%d;%s=%d", VCM_CONFIG_DIRECTION, direction,
97 VCM_CONFIG_TYPE, type, VCM_CONFIG_SRC_DST, srcdst,
98 VCM_CONFIG_PRIORITY, priority, VCM_CONFIG_DEST, dest);
99
100 printf("%s: config information kvpair is %s.\n", __FUNCTION__, kvpair);
101
102 //extract the parameter and config from string
103 param = str_parms_create_str(kvpair);
104 if (!param) {
105 printf("%s: param create str is null!", __FUNCTION__);
106 return -1;
107 }
108
109 //set vcm configurations
110 key = VCM_CONFIG_DIRECTION;
111 if (str_parms_get_int(param, key, &data[0]) == 0) {
112 update_vcm = true;
113 str_parms_del(param, key);
114 }
115 key = VCM_CONFIG_TYPE;
116 if (str_parms_get_int(param, key, &data[1]) == 0) {
117 update_vcm = true;
118 str_parms_del(param, key);
119 }
120 key = VCM_CONFIG_SRC_DST;
121 if (str_parms_get_int(param, key, &data[2]) == 0) {
122 update_vcm = true;
123 str_parms_del(param, key);
124 }
125 key = VCM_CONFIG_PRIORITY;
126 if (str_parms_get_int(param, key, &data[3]) == 0) {
127 update_vcm = true;
128 str_parms_del(param, key);
129 }
130 key = VCM_CONFIG_DEST;
131 if (str_parms_get_int(param, key, &data[4]) == 0) {
132 update_vcm = true;
133 str_parms_del(param, key);
134 }
135
136 //printf("Direction is %d, Type is %d, Src_Dst is %d, Priority is %d, Dest is %d. \n",data[0], data[1], data[2], data[3], data[4]);
137
138 if (update_vcm) {
139 configure_vcm(data); /*TODO check if all inputs got all values successfully*/
140 }
141
142 return 0;
143}
144
145/*******************************************************************************\
146 * Function: VoIP_thread
147 * Description:This function will main thread to record the PCM stream from
148 * ASR1826 and playback the PCM stream to ASR1826.
149 *
150 * NOTE:
151 sample rate is 8000
152 channel number is 1
153 bit of sample is 16
154 Due to modem limited, only support 8k/16k sample rate
155 * Returns: void
156 \*******************************************************************************/
157static void VoIP_thread(void *arg)
158{
159 int rc, len, fd;
160 char buffer[PCM_WB_BUF_SIZE];
161 unsigned int frames = 0;
162 struct wav_header header;
163 char path[10];
164
165 memset(path, 0x00, sizeof(path));
166 memcpy(path, "./nb.wav", 9);
167 printf("path is %s. \n", path);
168
169 header.riff_id = ID_RIFF;
170 header.riff_sz = 0;
171 header.riff_fmt = ID_WAVE;
172 header.fmt_id = ID_FMT;
173 header.fmt_sz = 16;
174 header.audio_format = 1; //FORMAT_PCM;
175 header.num_channels = 1; //Modem ONLY support mono recording
176 header.sample_rate = (pcm_record_size == PCM_NB_BUF_SIZE ? 8000 : 16000);
177 header.bits_per_sample = 16; //PCM_SAMPLEBITS_S16_LE;
178 header.byte_rate = (header.bits_per_sample / 8) * header.num_channels * header.sample_rate;
179 header.block_align = header.num_channels * (header.bits_per_sample / 8);
180 header.data_id = ID_DATA;
181
182 printf("enter VoIP_thread.\n");
183
184 //Must set vcm_configure before pcmloopback
185 if((pcm_record_size != PCM_NB_BUF_SIZE) && (pcm_record_size != PCM_WB_BUF_SIZE)){
186 printf("%s: Please use vcm_configure to set pcm_record_size!!\n", __FUNCTION__);
187 return;
188 }
189
190 //Must set vcm_configure before playback
191 if((pcm_playback_size != PCM_NB_BUF_SIZE) && (pcm_playback_size != PCM_WB_BUF_SIZE)){
192 printf("%s: Please use vcm_configure to set pcm_playback_size!!\n", __FUNCTION__);
193 return;
194 }
195
196 //Must set vcm_configure before playback
197 if(pcm_record_size != pcm_playback_size){
198 printf("%s: Please configure pcm_record_size = pcm_playback_size!!\n", __FUNCTION__);
199 return;
200 }
201
202 fd = open(path, O_WRONLY | O_CREAT);
203 if (fd < 0) {
204 printf("%s: error opening file %s!\n", __FUNCTION__, path);
205 return;
206 }
207
208 //leave enough room for header
209 lseek(fd, sizeof(struct wav_header), SEEK_SET);
210
211 //open the audiostub_ctl, prepare for record and playback
212 VCMInit();
213
214 //open record stream
215 rc = VoIP_ahw_dev_ubus->open_input_stream(VoIP_ahw_dev_ubus, 0,
216 VoIP_ahw_dev_ubus->get_supported_devices(VoIP_ahw_dev_ubus),
217 NULL, &stream_in, 0, 0, AUDIO_SOURCE_VOICE_CALL);
218 if (rc < 0) {
219 printf("%s: error opening input device. rc = %d!\n", __FUNCTION__, rc);
220 goto bad_stream;
221 }
222
223 //open playback stream
224 rc = VoIP_ahw_dev_ubus->open_output_stream(VoIP_ahw_dev_ubus, 0,
225 VoIP_ahw_dev_ubus->get_supported_devices(VoIP_ahw_dev_ubus),
226 AUDIO_OUTPUT_FLAG_DIRECT, NULL, &stream_out, 0);
227 if (rc < 0) {
228 printf("%s: error opening output device. rc = %d!\n", __FUNCTION__, rc);
229 goto bad_stream;
230 }
231
232 printf("%s: starting pcmloopback %d bytes every 20ms!\n", __FUNCTION__, pcm_record_size);
233 go_on_pcmloopback = true;
234 while (go_on_pcmloopback) {
235 //record the needed format stream from the device.
236 //only read pcm stream, no send command.
237 len = stream_in->read(stream_in, buffer, pcm_record_size);
238 if (len < 0) {
239 printf("%s: error reading!\n", __FUNCTION__);
240 goto end_pcmloopback;
241 }
242
243 //printf("%s: record from Dev size is %d!\n", __FUNCTION__, len);
244
245
246
247 //
248 //TODO:send the above IP package to far-end
249 //
250
251
252
253 //for debug:save into file
254 rc = write(fd, buffer, len);
255 if (rc < 0) {
256 printf("%s: error writing to file!\n", __FUNCTION__);
257 goto end_pcmloopback;
258 } else if (rc < len) {
259 printf("%s: wrote less the buffer size!\n", __FUNCTION__);
260 }
261
262 //printf("%s: write to file len is %d.\n", __FUNCTION__, rc);
263
264 //playback the needed format stream to device.
265 //only write pcm stream, no send command.
266 rc = stream_out->write(stream_out, buffer, len);
267 if (rc < 0) {
268 printf("%s: error writing (child)!\n", __FUNCTION__);
269 goto end_pcmloopback;
270 } else if (rc < len) {
271 printf("%s: wrote less than buffer size!\n", __FUNCTION__);
272 goto end_pcmloopback;
273 }
274 // printf("%s: playback to Dev len is %d.\n", __FUNCTION__, rc);
275
276 printf("%s: No.%d frame loopback!\n", __FUNCTION__, ++frames);
277 }
278
279end_pcmloopback:
280 //write header now all information is known
281 header.data_sz = pcm_record_size * frames;
282 header.riff_sz = header.data_sz + sizeof(header) - 8;
283 lseek(fd, 0, SEEK_SET);
284 write(fd, &header, sizeof(struct wav_header));
285 printf("%s: save wave file:%s. sample_rate = %d, num_channels = %d!\n", __FUNCTION__, path, header.sample_rate, header.num_channels);
286
287 stream_in->common.standby(&stream_in->common);
288 VoIP_ahw_dev_ubus->close_input_stream(VoIP_ahw_dev_ubus, stream_in);
289 stream_out->common.standby(&stream_out->common);
290 VoIP_ahw_dev_ubus->close_output_stream(VoIP_ahw_dev_ubus, stream_out);
291 VCMDeinit();//close the fd of audiostub_ctl when exit the thread.
292
293 go_on_pcmloopback = false;
294bad_stream:
295 if (close(fd))
296 printf("%s: error closing file!\n", __FUNCTION__);
297 printf("%s: finished pcm loopback!\n", __FUNCTION__);
298 printf("exit VoIP_thread!\n");
299 return;
300}
301
302static void sig_handler(int sig)
303{
304 go_on_pcmloopback = false;
305 printf("output signal number: %d.\n", sig);
306}
307
308
309/*******************************************************************************\
310 * Function: main
311 \*******************************************************************************/
312int main()
313{
314 pthread_t thread_start;
315 pthread_t thread_stop;
316 int rc = 0;
317
318 //use the VoIP device file.
319 //use test device node
320 config_VoIP_device(2);
321
322 /* install signal handler and begin to capture signal for close */
323 signal(SIGINT, sig_handler);
324 signal(SIGTERM, sig_handler);
325
326 //init global variables
327 VoIP_ahw_dev_ubus = audio_hal_install();
328 if (VoIP_ahw_dev_ubus == NULL) {
329 printf("%s: audio_hal_install failed!\n", __FUNCTION__);
330 exit (-1);
331 }
332
333 //The following config parameters are needed for main thread.
334 //config record parameters.
335 config_parameters(1);
336 //config playback parameters.
337 config_parameters(0);
338
339 rc = pthread_create(&thread_start, NULL, (void *)&VoIP_thread, NULL);
340 if (rc < 0) {
341 printf("%s: error creating thread_start!\n", __FUNCTION__);
342 rc = UBUS_STATUS_PERMISSION_DENIED;
343 }
344
345 if (pthread_join(thread_start, NULL)){
346 printf("error join thread!\n");
347 abort();
348 }
349
350 audio_hal_uninstall();
351
352 return 0;
353}
354