luat_lib_cc.c 12 KB

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