testCall.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. --- 模块功能:通话功能测试.
  2. -- @author openLuat
  3. -- @module call.testCall
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2018.03.20
  7. module(...,package.seeall)
  8. require"cc"
  9. require"audio"
  10. require"common"
  11. --来电铃声播放协程ID
  12. local coIncoming
  13. local function callVolTest()
  14. local curVol = audio.getCallVolume()
  15. curVol = (curVol>=7) and 1 or (curVol+1)
  16. log.info("testCall.setCallVolume",curVol)
  17. audio.setCallVolume(curVol)
  18. end
  19. -- audiocore.streamplay
  20. local function testAudioStream(streamType)
  21. sys.taskInit(
  22. function()
  23. while true do
  24. tStreamType = streamType
  25. log.info("AudioTest.AudioStreamTest", "AudioStreamPlay Start", streamType)
  26. local tAudioFile =
  27. {
  28. [audiocore.AMR] = "tip.amr",
  29. [audiocore.SPX] = "record.spx",
  30. [audiocore.PCM] = "alarm_door.pcm",
  31. [audiocore.MP3] = "sms.mp3"
  32. }
  33. local fileHandle = io.open("/lua/" .. tAudioFile[streamType], "rb")
  34. if not fileHandle then
  35. log.error("AudioTest.AudioStreamTest", "Open file error")
  36. return
  37. end
  38. while true do
  39. local data = fileHandle:read(streamType == audiocore.SPX and 1200 or 1024)
  40. if not data then
  41. fileHandle:close()
  42. while audiocore.streamremain() ~= 0 do
  43. sys.wait(20)
  44. end
  45. sys.wait(1000)
  46. audiocore.stop() --添加audiocore.stop()接口,否则再次播放会播放不出来
  47. log.warn("AudioTest.AudioStreamTest", "AudioStreamPlay Over")
  48. return
  49. end
  50. local data_len = string.len(data)
  51. local curr_len = 1
  52. while true do
  53. curr_len = curr_len + audiocore.streamplay(tStreamType,string.sub(data,curr_len,-1),audiocore.PLAY_VOLTE)
  54. if curr_len>=data_len then
  55. break
  56. elseif curr_len == 0 then
  57. log.error("AudioTest.AudioStreamTest", "AudioStreamPlay Error", streamType)
  58. return
  59. end
  60. sys.wait(10)
  61. end
  62. sys.wait(10)
  63. end
  64. end
  65. end
  66. )
  67. end
  68. --- “通话已建立”消息处理函数
  69. -- @string num,建立通话的对方号码
  70. -- @return 无
  71. local function connected(num)
  72. log.info("testCall.connected")
  73. coIncoming = nil
  74. --通话中设置mic增益,必须在通话建立以后设置
  75. --audio.setMicGain("call",7)
  76. --通话中音量测试
  77. sys.timerLoopStart(callVolTest,5000)
  78. --通话中向对方播放TTS测试
  79. audio.play(7,"TTS","通话中TTS测试",7,nil,true,2000)
  80. --通话中向对方播放音频
  81. --[[
  82. audio.setVolume(2)
  83. log.info("AudioTest.AudioStreamTest.AMRFilePlayTest", "Start")
  84. testAudioStream(audiocore.AMR)
  85. ]]
  86. --110秒之后主动结束通话
  87. sys.timerStart(cc.hangUp,110000,num)
  88. end
  89. --- “通话已结束”消息处理函数
  90. -- @string discReason,通话结束原因值,取值范围如下:
  91. -- "CHUP"表示本端调用cc.hungUp()接口主动挂断
  92. -- "NO ANSWER"表示呼出后,到达对方,对方无应答,通话超时断开
  93. -- "BUSY"表示呼出后,到达对方,对方主动挂断
  94. -- "NO CARRIER"表示通话未建立或者其他未知原因的断开
  95. -- nil表示没有检测到原因值
  96. -- @return 无
  97. local function disconnected(discReason)
  98. coIncoming = nil
  99. log.info("testCall.disconnected",discReason)
  100. sys.timerStopAll(cc.hangUp)
  101. sys.timerStop(callVolTest)
  102. audio.stop()
  103. end
  104. --- “来电”消息处理函数
  105. -- @string num,来电号码
  106. -- @return 无
  107. local function incoming(num)
  108. log.info("testCall.incoming:"..num)
  109. if not coIncoming then
  110. coIncoming = sys.taskInit(function()
  111. --audio.play(1,"TTS","来电话啦",4,function() sys.publish("PLAY_INCOMING_RING_IND") end,true)
  112. audio.play(1,"FILE","/lua/call.mp3",4,function() sys.publish("PLAY_INCOMING_RING_IND") end,true)
  113. sys.waitUntil("PLAY_INCOMING_RING_IND")
  114. end)
  115. sys.subscribe("POWER_KEY_IND",function() audio.stop(function() cc.accept(num) end) end)
  116. end
  117. --[[
  118. if not coIncoming then
  119. coIncoming = sys.taskInit(function()
  120. for i=1,7 do
  121. --audio.play(1,"TTS","来电话啦",i,function() sys.publish("PLAY_INCOMING_RING_IND") end)
  122. audio.play(1,"FILE","/lua/call.mp3",i,function() sys.publish("PLAY_INCOMING_RING_IND") end)
  123. sys.waitUntil("PLAY_INCOMING_RING_IND")
  124. end
  125. --接听来电
  126. --cc.accept(num)
  127. end)
  128. end]]
  129. --接听来电
  130. --cc.accept(num)
  131. end
  132. --- “通话功能模块准备就绪””消息处理函数
  133. -- @return 无
  134. local function ready()
  135. log.info("tesCall.ready")
  136. --呼叫10086
  137. --sys.timerStart(cc.dial,10000,"10086")
  138. end
  139. --- “通话中收到对方的DTMF”消息处理函数
  140. -- @string dtmf,收到的DTMF字符
  141. -- @return 无
  142. local function dtmfDetected(dtmf)
  143. log.info("testCall.dtmfDetected",dtmf)
  144. end
  145. --订阅消息的用户回调函数
  146. --sys.subscribe("CALL_READY",ready)
  147. sys.subscribe("NET_STATE_REGISTERED",ready)
  148. sys.subscribe("CALL_INCOMING",incoming)
  149. sys.subscribe("CALL_CONNECTED",connected)
  150. sys.subscribe("CALL_DISCONNECTED",disconnected)
  151. cc.dtmfDetect(true)
  152. sys.subscribe("CALL_DTMF_DETECT",dtmfDetected)