ipcast.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package ipcast
  2. import (
  3. "crypto/rand"
  4. "encoding/hex"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "github.com/goccy/go-json"
  8. "github.com/sirupsen/logrus"
  9. "io"
  10. "lcfns/model/common/response"
  11. ipResp "lcfns/model/ipcast/response"
  12. "net/http"
  13. "strconv"
  14. "time"
  15. )
  16. type IpcastApi struct {
  17. }
  18. func (api *IpcastApi) Play(c *gin.Context) {
  19. url := GetIpcastUrl(c, Play)
  20. data, err := c.GetRawData()
  21. if err != nil {
  22. response.FailWithMessage("获取请求参数出错", c)
  23. logrus.Error(err)
  24. return
  25. }
  26. all, err := ipcastService.Common(url, http.MethodPost, data)
  27. if err != nil {
  28. response.Fail(c)
  29. logrus.Error(err)
  30. return
  31. }
  32. var resp ipResp.PingResp
  33. json.Unmarshal(all, &resp)
  34. response.OkWithDetailed(resp.Message, "播放成功!", c)
  35. }
  36. func (api *IpcastApi) Stop(c *gin.Context) {
  37. url := GetIpcastUrl(c, Stop)
  38. all, err := ipcastService.Common(url, http.MethodDelete, nil)
  39. if err != nil {
  40. response.Fail(c)
  41. logrus.Error(err)
  42. return
  43. }
  44. var resp ipResp.PingResp
  45. json.Unmarshal(all, &resp)
  46. response.OkWithDetailed(resp.Message, "操作成功!", c)
  47. }
  48. func (api *IpcastApi) Status(c *gin.Context) {
  49. url := GetIpcastUrl(c, Status)
  50. all, err := ipcastService.Common(url, http.MethodGet, nil)
  51. if err != nil {
  52. response.Fail(c)
  53. logrus.Error(err)
  54. return
  55. }
  56. var resp ipResp.StatusResp
  57. json.Unmarshal(all, &resp)
  58. response.OkWithData(resp.Data, c)
  59. }
  60. func (api *IpcastApi) Ping(c *gin.Context) {
  61. url := GetIpcastUrl(c, Ping)
  62. all, err := ipcastService.Common(url, http.MethodGet, nil)
  63. if err != nil {
  64. response.Fail(c)
  65. logrus.Error(err)
  66. return
  67. }
  68. var resp ipResp.PingResp
  69. json.Unmarshal(all, &resp)
  70. response.OkWithData(resp.Message, c)
  71. }
  72. func (api *IpcastApi) Volume(c *gin.Context) {
  73. url := GetIpcastUrl(c, Volume)
  74. data, err := c.GetRawData()
  75. if err != nil {
  76. response.FailWithMessage("获取请求参数出错", c)
  77. logrus.Error(err)
  78. return
  79. }
  80. all, err := ipcastService.Common(url, http.MethodPatch, data)
  81. if err != nil {
  82. response.Fail(c)
  83. logrus.Error(err)
  84. return
  85. }
  86. var resp ipResp.PingResp
  87. json.Unmarshal(all, &resp)
  88. response.OkWithDetailed(resp.Message, "操作成功!", c)
  89. }
  90. func (api *IpcastApi) List(c *gin.Context) {
  91. }
  92. //↓实现语音喊话功能↓
  93. var audio = make(map[string][]byte)
  94. // volume 播放⾳量:取值【1~100】
  95. // duration 循环(重复)播放时⻓(秒)
  96. // times 循环(重复)播放次数(次)
  97. // gap 循环(重复)播放中的间歇时间(秒)
  98. // todo 修改ip端口 106.52.134.22
  99. var ipcastData = `
  100. {
  101. "url": "http://192.168.110.69:8889/ipcast/AudioSource/%s",
  102. "sync": false,
  103. "queue": true,
  104. "volume": 50,
  105. "loop": {
  106. "duration": %s,
  107. "times": 1,
  108. "gap": 2
  109. }
  110. }
  111. `
  112. // PlayAudio 前端传递mp3资源
  113. func (api *IpcastApi) PlayAudio(c *gin.Context) {
  114. //生成uuid
  115. uuid := make([]byte, 8)
  116. rand.Read(uuid)
  117. uuidString := hex.EncodeToString(uuid)
  118. defer func() {
  119. go func() {
  120. //数据传输完后清除数据
  121. time.Sleep(10 * time.Second)
  122. delete(audio, uuidString)
  123. }()
  124. }()
  125. //读取mp3数据
  126. //mp3, err := c.GetRawData()
  127. mp3, err := c.FormFile("mp3")
  128. if err != nil {
  129. response.FailWithMessage("获取请求参数出错", c)
  130. logrus.Error(err)
  131. return
  132. }
  133. open, err := mp3.Open()
  134. readAll, err := io.ReadAll(open)
  135. //保存到内存
  136. audio[uuidString] = readAll
  137. //播放
  138. t := c.PostForm("timeLength")
  139. data := []byte(fmt.Sprintf(ipcastData, uuidString, t))
  140. all, err := ipcastService.Common(GetIpcastUrl(c, Play), http.MethodPost, data)
  141. if err != nil {
  142. response.Fail(c)
  143. logrus.Error(err)
  144. return
  145. }
  146. var resp ipResp.PingResp
  147. json.Unmarshal(all, &resp)
  148. if resp.Code == 200 {
  149. response.OkWithDetailed(resp.Message, "播放成功", c)
  150. } else {
  151. response.FailWithDetailed(resp.Message, "播放失败", c)
  152. }
  153. }
  154. // AudioSource 给ip音柱播放的url资源
  155. func (api *IpcastApi) AudioSource(c *gin.Context) {
  156. id := c.Param("id")
  157. fmt.Println("id:", id)
  158. // 设置响应头,指定内容类型为 audio/mpeg
  159. c.Header("Content-Type", "audio/mp3")
  160. c.Header("Access-Control-Allow-Origin", "*")
  161. c.Header("Content-Length", strconv.Itoa(len(audio[id])))
  162. // 将内存中的 MP3 数据作为响应返回给浏览器
  163. c.Data(http.StatusOK, "audio/mp3", audio[id])
  164. }
  165. //↑实现语音喊话功能↑
  166. const (
  167. Play = iota
  168. Stop
  169. Status
  170. Ping
  171. Volume
  172. )
  173. func GetIpcastUrl(c *gin.Context, expr byte) string {
  174. baseUrl := IpcastBaseUrl(c)
  175. if baseUrl == "" {
  176. response.FailWithMessage("获取baseUrl失败", c)
  177. return ""
  178. }
  179. switch expr {
  180. case Stop, Play:
  181. return baseUrl + "/v1/speech"
  182. case Status:
  183. return baseUrl + "/v1/play_status"
  184. case Ping:
  185. return baseUrl + "/v1/check_alive"
  186. case Volume:
  187. return baseUrl + "/v1/volume"
  188. }
  189. return ""
  190. }