ipcast.go 4.4 KB

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