luat_lib_cc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. luat_audio_conf_t* audio_conf = luat_audio_get_config(multimedia_id);
  134. luat_i2s_conf_t *i2s = luat_i2s_get_config(multimedia_id);
  135. i2s->is_full_duplex = 1;
  136. i2s->luat_i2s_event_callback = record_cb;
  137. while (1){
  138. luat_rtos_event_recv(luat_cc.task_handle, 0, &event, NULL, LUAT_WAIT_FOREVER);
  139. switch(event.id)
  140. {
  141. case VOLTE_EVENT_PLAY_TONE:
  142. if (LUAT_MOBILE_CC_PLAY_STOP == event.param1){
  143. luat_cc.record_type = 0;
  144. luat_cc.play_type = 0;
  145. if (luat_rtos_timer_is_active(luat_cc.record_timer))
  146. {
  147. luat_rtos_timer_stop(luat_cc.record_timer);
  148. rtos_msg_t msg;
  149. msg.handler = l_cc_handler;
  150. msg.arg2 = luat_cc.record_up_zbuff_point;
  151. msg.arg1 = 0;
  152. luat_msgbus_put(&msg, 0);
  153. msg.arg2 = luat_cc.record_down_zbuff_point;
  154. msg.arg1 = 1;
  155. luat_msgbus_put(&msg, 0);
  156. }
  157. luat_cc.is_call_uplink_on = 0;
  158. luat_audio_speech_stop(multimedia_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. luat_audio_speech(multimedia_id, 0, event.param1, NULL, 0, 1);
  167. luat_cc.record_up_zbuff_point = 0;
  168. if (luat_cc.record_on_off) {
  169. luat_cc.up_buff[0]->used = 0;
  170. }
  171. LLOGD("VOLTE_EVENT_RECORD_VOICE_START");
  172. break;
  173. case VOLTE_EVENT_RECORD_VOICE_UPLOAD:
  174. if (!luat_cc.is_call_uplink_on) break;
  175. if (luat_cc.upload_need_stop) {
  176. LLOGD("VOLTE RECORD VOICE ALREADY STOP");
  177. break;
  178. }
  179. if (luat_cc.record_on_off && luat_cc.record_type) {
  180. zbuff = luat_cc.up_buff[luat_cc.record_up_zbuff_point];
  181. memcpy(zbuff->addr + zbuff->used, (uint8_t *)event.param1, event.param2);
  182. zbuff->used += event.param2;
  183. }
  184. if (luat_cc.record_type) {
  185. luat_mobile_speech_upload((uint8_t *)event.param1, event.param2);
  186. }
  187. if (luat_cc.record_on_off && luat_cc.record_type) {
  188. if (zbuff->used >= zbuff->len) {
  189. rtos_msg_t msg;
  190. msg.handler = l_cc_handler;
  191. msg.arg2 = luat_cc.record_up_zbuff_point;
  192. msg.arg1 = 0;
  193. luat_msgbus_put(&msg, 0);
  194. luat_cc.record_up_zbuff_point = !luat_cc.record_up_zbuff_point;
  195. luat_cc.up_buff[luat_cc.record_up_zbuff_point]->used = 0;
  196. }
  197. }
  198. break;
  199. case VOLTE_EVENT_PLAY_VOICE:
  200. luat_cc.play_type = event.param3; //1 = 8K 2 = 16K
  201. luat_audio_speech(multimedia_id, 1, event.param3, (uint8_t *)event.param1, event.param2, 1);
  202. LLOGD("VOLTE PLAY VOICE");
  203. break;
  204. case VOLTE_EVENT_HANGUP:
  205. luat_mobile_hangup_call(event.param1);
  206. break;
  207. }
  208. }
  209. }
  210. /**
  211. 获取最后一次通话的号码
  212. @api cc.lastNum()
  213. @return string 获取最后一次通话的号码
  214. */
  215. static int l_cc_get_last_call_num(lua_State* L) {
  216. char number[64] = {0};
  217. luat_mobile_get_last_call_num(number, sizeof(number));
  218. lua_pushlstring(L, (const char*)(number),strlen(number));
  219. return 1;
  220. }
  221. /**
  222. 拨打电话
  223. @api cc.dial(sim_id, number)
  224. @number sim_id
  225. @string 电话号码
  226. @return bool 拨打电话成功与否
  227. */
  228. static int l_cc_make_call(lua_State* L) {
  229. uint8_t sim_id = luaL_optinteger(L, 1, 0);
  230. size_t len = 0;
  231. char* number = luaL_checklstring(L, 2, &len);
  232. lua_pushboolean(L, !luat_mobile_make_call(sim_id,number, len));
  233. return 1;
  234. }
  235. /**
  236. 挂断电话
  237. @api cc.hangUp(sim_id)
  238. @number sim_id
  239. */
  240. static int l_cc_hangup_call(lua_State* L) {
  241. uint8_t sim_id = luaL_optinteger(L, 1, 0);
  242. luat_rtos_event_send(luat_cc.task_handle, VOLTE_EVENT_HANGUP, sim_id, 0, 0, 0);
  243. return 0;
  244. }
  245. /**
  246. 接听电话
  247. @api cc.accept(sim_id)
  248. @number sim_id
  249. @return bool 接听电话成功与否
  250. */
  251. static int l_cc_answer_call(lua_State* L) {
  252. uint8_t sim_id = luaL_optinteger(L, 1, 0);
  253. lua_pushboolean(L, !luat_mobile_answer_call(sim_id));
  254. return 1;
  255. }
  256. /**
  257. 初始化电话功能
  258. @api cc.init(multimedia_id)
  259. @number multimedia_id 多媒体id
  260. @return bool 成功与否
  261. */
  262. static int l_cc_speech_init(lua_State* L) {
  263. uint8_t multimedia_id = luaL_optinteger(L, 1, 0);
  264. if (luat_mobile_speech_init(multimedia_id,mobile_voice_data_input)){
  265. lua_pushboolean(L, 0);
  266. return 1;
  267. }
  268. luat_cc.record_timer = luat_create_rtos_timer(download_data_callback, NULL, NULL);
  269. luat_rtos_task_create(&luat_cc.task_handle, 4*1024, 100, "volte", luat_volte_task, multimedia_id, 64);
  270. lua_pushboolean(L, 1);
  271. return 1;
  272. }
  273. /**
  274. 录音通话
  275. @api cc.record(on_off,upload_zbuff1, upload_zbuff2, download_zbuff1, download_zbuff2)
  276. @boolean 开启关闭通话录音功能,false或者nil关闭,其他开启
  277. @zbuff 上行数据保存区1,zbuff创建时的空间容量必须是640的倍数,下同
  278. @zbuff 上行数据保存区2,和上行数据保存区1组成双缓冲区
  279. @zbuff 下行数据保存区1
  280. @zbuff 下行数据保存区2,和下行数据保存区1组成双缓冲区
  281. @return bool 成功与否,如果处于通话状态,会失败
  282. @usage
  283. buff1 = zbuff.create(6400,0,zbuff.HEAP_AUTO)
  284. buff2 = zbuff.create(6400,0,zbuff.HEAP_AUTO)
  285. buff3 = zbuff.create(6400,0,zbuff.HEAP_AUTO)
  286. buff4 = zbuff.create(6400,0,zbuff.HEAP_AUTO)
  287. cc.on("record", function(type, buff_point)
  288. log.info(type, buff_point) -- type==true是下行数据,false是上行数据 buff_point指示双缓存中返回了哪一个
  289. end)
  290. cc.record(true, buff1, buff2, buff3, buff4)
  291. */
  292. static int l_cc_record_call(lua_State* L) {
  293. if (luat_cc.record_type)
  294. {
  295. lua_pushboolean(L, 0);
  296. return 1;
  297. }
  298. luat_cc.record_on_off = lua_toboolean(L, 1);
  299. if (luat_cc.record_on_off)
  300. {
  301. luat_cc.up_buff[0] = (luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE);
  302. luat_cc.up_buff[1] = (luat_zbuff_t *)luaL_checkudata(L, 3, LUAT_ZBUFF_TYPE);
  303. luat_cc.down_buff[0] = (luat_zbuff_t *)luaL_checkudata(L, 4, LUAT_ZBUFF_TYPE);
  304. luat_cc.down_buff[1] = (luat_zbuff_t *)luaL_checkudata(L, 5, LUAT_ZBUFF_TYPE);
  305. }
  306. else
  307. {
  308. luat_cc.up_buff[0] = NULL;
  309. luat_cc.up_buff[1] = NULL;
  310. luat_cc.down_buff[0] = NULL;
  311. luat_cc.down_buff[1] = NULL;
  312. }
  313. lua_pushboolean(L, 1);
  314. return 1;
  315. }
  316. /**
  317. 获取当前通话质量
  318. @api cc.quality()
  319. @return int 1为低音质(8K),2为高音质(16k),0没有在通话
  320. */
  321. static int l_cc_get_quality(lua_State* L) {
  322. lua_pushinteger(L, luat_cc.record_type);
  323. return 1;
  324. }
  325. /**
  326. 注册通话回调
  327. @api cc.on(event, func)
  328. @string 事件名称 音频录音数据为"record"
  329. @function 回调方法
  330. @return nil 无返回值
  331. @usage
  332. cc.on("record", function(type, buff_point)
  333. log.info(type, buff_point) -- type==true是下行数据,false是上行数据 buff_point指示双缓存中返回了哪一个
  334. end)
  335. */
  336. static int l_cc_on(lua_State *L) {
  337. const char* event = luaL_checkstring(L, 1);
  338. if (!strcmp("record", event)) {
  339. if (luat_cc.record_cb != 0) {
  340. luaL_unref(L, LUA_REGISTRYINDEX, luat_cc.record_cb);
  341. luat_cc.record_cb = 0;
  342. }
  343. if (lua_isfunction(L, 2)) {
  344. lua_pushvalue(L, 2);
  345. luat_cc.record_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  346. }
  347. }
  348. return 0;
  349. }
  350. #include "rotable2.h"
  351. static const rotable_Reg_t reg_cc[] =
  352. {
  353. { "init" , ROREG_FUNC(l_cc_speech_init)},
  354. { "dial" , ROREG_FUNC(l_cc_make_call)},
  355. { "accept" , ROREG_FUNC(l_cc_answer_call)},
  356. { "hangUp" , ROREG_FUNC(l_cc_hangup_call)},
  357. { "lastNum" , ROREG_FUNC(l_cc_get_last_call_num)},
  358. { "quality" , ROREG_FUNC(l_cc_get_quality)},
  359. { "on" , ROREG_FUNC(l_cc_on)},
  360. { "record", ROREG_FUNC(l_cc_record_call)},
  361. { NULL, ROREG_INT(0)}
  362. };
  363. LUAMOD_API int luaopen_cc( lua_State *L ) {
  364. luat_newlib2(L, reg_cc);
  365. return 1;
  366. }
  367. void luat_cc_start_speech(uint32_t param)
  368. {
  369. luat_cc.upload_need_stop = 0;
  370. luat_rtos_event_send(luat_cc.task_handle, VOLTE_EVENT_RECORD_VOICE_START, param, 0, 0, 0);
  371. }
  372. void luat_cc_play_tone(uint32_t param)
  373. {
  374. if (!param) luat_cc.upload_need_stop = 1;
  375. luat_rtos_event_send(luat_cc.task_handle, VOLTE_EVENT_PLAY_TONE, param, 0, 0, 0);
  376. }