luat_lib_cc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. @module cc
  3. @summary VoLTE通话功能
  4. @version 1.0
  5. @date 2024.1.17
  6. @demo cc
  7. @tag LUAT_USE_VOLTE
  8. @usage
  9. -- 选型手册上支持VoLTE通话功能的模组支持
  10. */
  11. #include "luat_base.h"
  12. #include "luat_mem.h"
  13. #include "luat_rtos.h"
  14. #include "luat_msgbus.h"
  15. #include "luat_zbuff.h"
  16. #include "luat_mobile.h"
  17. #include "luat_network_adapter.h"
  18. #include "luat_i2s.h"
  19. #include "luat_audio.h"
  20. #define LUAT_LOG_TAG "cc"
  21. #include "luat_log.h"
  22. enum{
  23. VOLTE_EVENT_PLAY_TONE = 1,
  24. VOLTE_EVENT_RECORD_VOICE_START,
  25. VOLTE_EVENT_RECORD_VOICE_UPLOAD,
  26. VOLTE_EVENT_PLAY_VOICE,
  27. VOLTE_EVENT_HANGUP,
  28. VOLTE_EVENT_CALL_READY,
  29. };
  30. //播放控制
  31. typedef struct
  32. {
  33. luat_rtos_task_handle task_handle;
  34. luat_zbuff_t *up_buff[2];
  35. luat_zbuff_t *down_buff[2];
  36. int record_cb;
  37. HANDLE record_timer;
  38. uint32_t next_download_point;
  39. uint8_t *download_buffer;
  40. uint8_t total_download_cnt;
  41. uint8_t play_type;
  42. uint8_t record_type;
  43. uint8_t is_call_uplink_on;
  44. uint8_t record_on_off;
  45. uint8_t record_start;
  46. uint8_t upload_need_stop;
  47. volatile uint8_t record_down_zbuff_point;
  48. volatile uint8_t record_up_zbuff_point;
  49. }luat_cc_ctrl_t;
  50. static luat_cc_ctrl_t luat_cc;
  51. static int l_cc_handler(lua_State *L, void* ptr) {
  52. (void)ptr;
  53. //LLOGD("l_uart_handler");
  54. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  55. lua_pop(L, 1);
  56. if (luat_cc.record_on_off && luat_cc.record_cb)
  57. {
  58. lua_geti(L, LUA_REGISTRYINDEX, luat_cc.record_cb);
  59. if (lua_isfunction(L, -1)) {
  60. lua_pushboolean(L, msg->arg1);
  61. lua_pushinteger(L, msg->arg2);
  62. lua_call(L, 2, 0);
  63. }
  64. }
  65. // 给rtos.recv方法返回个空数据
  66. lua_pushinteger(L, 0);
  67. return 1;
  68. }
  69. static void mobile_voice_data_input(uint8_t *input, uint32_t len, uint32_t sample_rate, uint8_t bits){
  70. if (luat_cc.record_on_off) {
  71. luat_cc.record_down_zbuff_point = 0;
  72. luat_cc.download_buffer = (uint8_t *)input;
  73. if (1 == sample_rate)
  74. {
  75. luat_cc.total_download_cnt = 6;
  76. }
  77. else
  78. {
  79. luat_cc.total_download_cnt = 3;
  80. }
  81. memcpy(luat_cc.down_buff[0]->addr, luat_cc.download_buffer, sample_rate * 320);
  82. luat_cc.down_buff[0]->used = sample_rate * 320;
  83. luat_cc.next_download_point = 1;
  84. luat_start_rtos_timer(luat_cc.record_timer, 20, 1);
  85. if (luat_cc.down_buff[0]->used >= luat_cc.down_buff[0]->len) {
  86. rtos_msg_t msg;
  87. msg.handler = l_cc_handler;
  88. msg.arg1 = 1;
  89. msg.arg2 = 0;
  90. luat_msgbus_put(&msg, 0);
  91. luat_cc.record_down_zbuff_point = !luat_cc.record_down_zbuff_point;
  92. luat_cc.down_buff[luat_cc.record_down_zbuff_point]->used = 0;
  93. }
  94. }
  95. luat_rtos_event_send(luat_cc.task_handle, VOLTE_EVENT_PLAY_VOICE, (uint32_t)input, len, sample_rate, 0);
  96. }
  97. static int record_cb(uint8_t id ,luat_i2s_event_t event, uint8_t *rx_data, uint32_t rx_len, void *param){
  98. if (luat_cc.upload_need_stop) return 0;
  99. switch(event){
  100. case LUAT_I2S_EVENT_RX_DONE:
  101. luat_rtos_event_send(luat_cc.task_handle, VOLTE_EVENT_RECORD_VOICE_UPLOAD, (uint32_t)rx_data, rx_len, 0, 0);
  102. break;
  103. case LUAT_I2S_EVENT_TX_DONE:
  104. case LUAT_I2S_EVENT_TRANSFER_DONE:
  105. break;
  106. default:
  107. break;
  108. }
  109. return 0;
  110. }
  111. static LUAT_RT_RET_TYPE download_data_callback(LUAT_RT_CB_PARAM)
  112. {
  113. if (luat_cc.record_type && luat_cc.record_on_off) {
  114. luat_zbuff_t *buff = luat_cc.down_buff[luat_cc.record_down_zbuff_point];
  115. memcpy(buff->addr + buff->used, luat_cc.download_buffer + luat_cc.next_download_point * luat_cc.record_type * 320, luat_cc.record_type * 320);//20ms录音完成
  116. luat_cc.next_download_point = (luat_cc.next_download_point + 1) % luat_cc.total_download_cnt;
  117. buff->used += luat_cc.record_type * 320;
  118. if (buff->used >= buff->len) {
  119. rtos_msg_t msg;
  120. msg.handler = l_cc_handler;
  121. msg.arg2 = luat_cc.record_down_zbuff_point;
  122. msg.arg1 = 1;
  123. luat_msgbus_put(&msg, 0);
  124. luat_cc.record_down_zbuff_point = !luat_cc.record_down_zbuff_point;
  125. luat_cc.down_buff[luat_cc.record_down_zbuff_point]->used = 0;
  126. }
  127. }
  128. }
  129. static void luat_volte_task(void *param){
  130. luat_zbuff_t *zbuff = NULL;
  131. luat_event_t event;
  132. uint8_t multimedia_id = (int)param;
  133. // uint8_t speech_on_off = 0;
  134. luat_audio_conf_t* audio_conf = luat_audio_get_config(multimedia_id);
  135. luat_i2s_conf_t *i2s = luat_i2s_get_config(audio_conf->codec_conf.i2s_id);
  136. while (1){
  137. luat_rtos_event_recv(luat_cc.task_handle, 0, &event, NULL, LUAT_WAIT_FOREVER);
  138. switch(event.id)
  139. {
  140. case VOLTE_EVENT_PLAY_TONE:
  141. if (LUAT_MOBILE_CC_PLAY_STOP == event.param1){
  142. luat_cc.record_type = 0;
  143. luat_cc.play_type = 0;
  144. if (luat_rtos_timer_is_active(luat_cc.record_timer))
  145. {
  146. luat_rtos_timer_stop(luat_cc.record_timer);
  147. rtos_msg_t msg;
  148. msg.handler = l_cc_handler;
  149. msg.arg2 = luat_cc.record_up_zbuff_point;
  150. msg.arg1 = 0;
  151. luat_msgbus_put(&msg, 0);
  152. msg.arg2 = luat_cc.record_down_zbuff_point;
  153. msg.arg1 = 1;
  154. luat_msgbus_put(&msg, 0);
  155. }
  156. luat_cc.is_call_uplink_on = 0;
  157. luat_audio_speech_stop(multimedia_id);
  158. luat_i2s_load_old_config(audio_conf->codec_conf.i2s_id);
  159. LLOGD("VOLTE_EVENT_PLAY_STOP");
  160. break;
  161. }
  162. break;
  163. case VOLTE_EVENT_RECORD_VOICE_START:
  164. luat_cc.record_type = event.param1;
  165. luat_cc.is_call_uplink_on = 1;
  166. if (i2s->luat_i2s_event_callback != record_cb)
  167. {
  168. luat_i2s_save_old_config(audio_conf->codec_conf.i2s_id);
  169. i2s->is_full_duplex = 1;
  170. i2s->luat_i2s_event_callback = record_cb;
  171. }
  172. luat_audio_speech(multimedia_id, 0, event.param1, NULL, 0, 1);
  173. luat_cc.record_up_zbuff_point = 0;
  174. if (luat_cc.record_on_off) {
  175. luat_cc.up_buff[0]->used = 0;
  176. }
  177. LLOGD("VOLTE_EVENT_RECORD_VOICE_START");
  178. break;
  179. case VOLTE_EVENT_RECORD_VOICE_UPLOAD:
  180. if (!luat_cc.is_call_uplink_on) break;
  181. if (luat_cc.upload_need_stop) {
  182. LLOGD("VOLTE RECORD VOICE ALREADY STOP");
  183. break;
  184. }
  185. if (luat_cc.record_on_off && luat_cc.record_type) {
  186. zbuff = luat_cc.up_buff[luat_cc.record_up_zbuff_point];
  187. memcpy(zbuff->addr + zbuff->used, (uint8_t *)event.param1, event.param2);
  188. zbuff->used += event.param2;
  189. }
  190. if (luat_cc.record_type) {
  191. luat_mobile_speech_upload((uint8_t *)event.param1, event.param2);
  192. }
  193. if (luat_cc.record_on_off && luat_cc.record_type) {
  194. if (zbuff->used >= zbuff->len) {
  195. rtos_msg_t msg;
  196. msg.handler = l_cc_handler;
  197. msg.arg2 = luat_cc.record_up_zbuff_point;
  198. msg.arg1 = 0;
  199. luat_msgbus_put(&msg, 0);
  200. luat_cc.record_up_zbuff_point = !luat_cc.record_up_zbuff_point;
  201. luat_cc.up_buff[luat_cc.record_up_zbuff_point]->used = 0;
  202. }
  203. }
  204. break;
  205. case VOLTE_EVENT_PLAY_VOICE:
  206. luat_cc.play_type = event.param3; //1 = 8K 2 = 16K
  207. if (i2s->luat_i2s_event_callback != record_cb)
  208. {
  209. luat_i2s_save_old_config(audio_conf->codec_conf.i2s_id);
  210. i2s->is_full_duplex = 1;
  211. i2s->luat_i2s_event_callback = record_cb;
  212. }
  213. luat_audio_speech(multimedia_id, 1, event.param3, (uint8_t *)event.param1, event.param2, 1);
  214. LLOGD("VOLTE PLAY VOICE");
  215. break;
  216. case VOLTE_EVENT_HANGUP:
  217. luat_mobile_hangup_call(event.param1);
  218. break;
  219. }
  220. }
  221. }
  222. /**
  223. 获取最后一次通话的号码
  224. @api cc.lastNum()
  225. @return string 获取最后一次通话的号码
  226. */
  227. static int l_cc_get_last_call_num(lua_State* L) {
  228. char number[64] = {0};
  229. luat_mobile_get_last_call_num(number, sizeof(number));
  230. lua_pushlstring(L, (const char*)(number),strlen(number));
  231. return 1;
  232. }
  233. /**
  234. 拨打电话
  235. @api cc.dial(sim_id, number)
  236. @number sim_id
  237. @string 电话号码
  238. @return bool 拨打电话成功与否
  239. */
  240. static int l_cc_make_call(lua_State* L) {
  241. uint8_t sim_id = luaL_optinteger(L, 1, 0);
  242. size_t len = 0;
  243. char* number = luaL_checklstring(L, 2, &len);
  244. lua_pushboolean(L, !luat_mobile_make_call(sim_id,number, len));
  245. return 1;
  246. }
  247. /**
  248. 挂断电话
  249. @api cc.hangUp(sim_id)
  250. @number sim_id
  251. */
  252. static int l_cc_hangup_call(lua_State* L) {
  253. uint8_t sim_id = luaL_optinteger(L, 1, 0);
  254. luat_rtos_event_send(luat_cc.task_handle, VOLTE_EVENT_HANGUP, sim_id, 0, 0, 0);
  255. return 0;
  256. }
  257. /**
  258. 接听电话
  259. @api cc.accept(sim_id)
  260. @number sim_id
  261. @return bool 接听电话成功与否
  262. */
  263. static int l_cc_answer_call(lua_State* L) {
  264. uint8_t sim_id = luaL_optinteger(L, 1, 0);
  265. lua_pushboolean(L, !luat_mobile_answer_call(sim_id));
  266. return 1;
  267. }
  268. /**
  269. 初始化电话功能
  270. @api cc.init(multimedia_id)
  271. @number multimedia_id 多媒体id
  272. @return bool 成功与否
  273. */
  274. static int l_cc_speech_init(lua_State* L) {
  275. uint8_t multimedia_id = luaL_optinteger(L, 1, 0);
  276. if (luat_mobile_speech_init(multimedia_id,mobile_voice_data_input)){
  277. lua_pushboolean(L, 0);
  278. return 1;
  279. }
  280. luat_cc.record_timer = luat_create_rtos_timer(download_data_callback, NULL, NULL);
  281. luat_rtos_task_create(&luat_cc.task_handle, 4*1024, 100, "volte", luat_volte_task, multimedia_id, 64);
  282. lua_pushboolean(L, 1);
  283. return 1;
  284. }
  285. /**
  286. 录音通话
  287. @api cc.record(on_off,upload_zbuff1, upload_zbuff2, download_zbuff1, download_zbuff2)
  288. @boolean 开启关闭通话录音功能,false或者nil关闭,其他开启
  289. @zbuff 上行数据保存区1,zbuff创建时的空间容量必须是640的倍数,下同
  290. @zbuff 上行数据保存区2,和上行数据保存区1组成双缓冲区
  291. @zbuff 下行数据保存区1
  292. @zbuff 下行数据保存区2,和下行数据保存区1组成双缓冲区
  293. @return bool 成功与否,如果处于通话状态,会失败
  294. @usage
  295. buff1 = zbuff.create(6400,0,zbuff.HEAP_AUTO)
  296. buff2 = zbuff.create(6400,0,zbuff.HEAP_AUTO)
  297. buff3 = zbuff.create(6400,0,zbuff.HEAP_AUTO)
  298. buff4 = zbuff.create(6400,0,zbuff.HEAP_AUTO)
  299. cc.on("record", function(type, buff_point)
  300. log.info(type, buff_point) -- type==true是下行数据,false是上行数据 buff_point指示双缓存中返回了哪一个
  301. end)
  302. cc.record(true, buff1, buff2, buff3, buff4)
  303. */
  304. static int l_cc_record_call(lua_State* L) {
  305. if (luat_cc.record_type)
  306. {
  307. lua_pushboolean(L, 0);
  308. return 1;
  309. }
  310. luat_cc.record_on_off = lua_toboolean(L, 1);
  311. if (luat_cc.record_on_off)
  312. {
  313. luat_cc.up_buff[0] = (luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE);
  314. luat_cc.up_buff[1] = (luat_zbuff_t *)luaL_checkudata(L, 3, LUAT_ZBUFF_TYPE);
  315. luat_cc.down_buff[0] = (luat_zbuff_t *)luaL_checkudata(L, 4, LUAT_ZBUFF_TYPE);
  316. luat_cc.down_buff[1] = (luat_zbuff_t *)luaL_checkudata(L, 5, LUAT_ZBUFF_TYPE);
  317. }
  318. else
  319. {
  320. luat_cc.up_buff[0] = NULL;
  321. luat_cc.up_buff[1] = NULL;
  322. luat_cc.down_buff[0] = NULL;
  323. luat_cc.down_buff[1] = NULL;
  324. }
  325. lua_pushboolean(L, 1);
  326. return 1;
  327. }
  328. /**
  329. 获取当前通话质量
  330. @api cc.quality()
  331. @return int 1为低音质(8K),2为高音质(16k),0没有在通话
  332. */
  333. static int l_cc_get_quality(lua_State* L) {
  334. lua_pushinteger(L, luat_cc.record_type);
  335. return 1;
  336. }
  337. /**
  338. 注册通话回调
  339. @api cc.on(event, func)
  340. @string 事件名称 音频录音数据为"record"
  341. @function 回调方法
  342. @return nil 无返回值
  343. @usage
  344. cc.on("record", function(type, buff_point)
  345. log.info(type, buff_point) -- type==true是下行数据,false是上行数据 buff_point指示双缓存中返回了哪一个
  346. end)
  347. */
  348. static int l_cc_on(lua_State *L) {
  349. const char* event = luaL_checkstring(L, 1);
  350. if (!strcmp("record", event)) {
  351. if (luat_cc.record_cb != 0) {
  352. luaL_unref(L, LUA_REGISTRYINDEX, luat_cc.record_cb);
  353. luat_cc.record_cb = 0;
  354. }
  355. if (lua_isfunction(L, 2)) {
  356. lua_pushvalue(L, 2);
  357. luat_cc.record_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  358. }
  359. }
  360. return 0;
  361. }
  362. #include "rotable2.h"
  363. static const rotable_Reg_t reg_cc[] =
  364. {
  365. { "init" , ROREG_FUNC(l_cc_speech_init)},
  366. { "dial" , ROREG_FUNC(l_cc_make_call)},
  367. { "accept" , ROREG_FUNC(l_cc_answer_call)},
  368. { "hangUp" , ROREG_FUNC(l_cc_hangup_call)},
  369. { "lastNum" , ROREG_FUNC(l_cc_get_last_call_num)},
  370. { "quality" , ROREG_FUNC(l_cc_get_quality)},
  371. { "on" , ROREG_FUNC(l_cc_on)},
  372. { "record", ROREG_FUNC(l_cc_record_call)},
  373. { NULL, ROREG_INT(0)}
  374. };
  375. LUAMOD_API int luaopen_cc( lua_State *L ) {
  376. luat_newlib2(L, reg_cc);
  377. return 1;
  378. }
  379. void luat_cc_start_speech(uint32_t param)
  380. {
  381. luat_cc.upload_need_stop = 0;
  382. luat_rtos_event_send(luat_cc.task_handle, VOLTE_EVENT_RECORD_VOICE_START, param, 0, 0, 0);
  383. }
  384. void luat_cc_play_tone(uint32_t param)
  385. {
  386. if (!param) luat_cc.upload_need_stop = 1;
  387. luat_rtos_event_send(luat_cc.task_handle, VOLTE_EVENT_PLAY_TONE, param, 0, 0, 0);
  388. }