|
@@ -1,24 +1,24 @@
|
|
|
package ipcast
|
|
|
|
|
|
import (
|
|
|
+ "crypto/rand"
|
|
|
+ "encoding/hex"
|
|
|
+ "fmt"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/goccy/go-json"
|
|
|
"github.com/sirupsen/logrus"
|
|
|
"lcfns/model/common/response"
|
|
|
ipResp "lcfns/model/ipcast/response"
|
|
|
"net/http"
|
|
|
+ "strconv"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type IpcastApi struct {
|
|
|
}
|
|
|
|
|
|
func (api *IpcastApi) Play(c *gin.Context) {
|
|
|
- baseUrl := IpcastBaseUrl(c)
|
|
|
- if baseUrl == "" {
|
|
|
- response.FailWithMessage("获取baseUrl失败", c)
|
|
|
- return
|
|
|
- }
|
|
|
- url := baseUrl + "/v1/speech"
|
|
|
+ url := GetIpcastUrl(c, Play)
|
|
|
data, err := c.GetRawData()
|
|
|
if err != nil {
|
|
|
response.FailWithMessage("获取请求参数出错", c)
|
|
@@ -33,16 +33,11 @@ func (api *IpcastApi) Play(c *gin.Context) {
|
|
|
}
|
|
|
var resp ipResp.PingResp
|
|
|
json.Unmarshal(all, &resp)
|
|
|
- response.OkWithData(resp.Message, c)
|
|
|
+ response.OkWithDetailed(resp.Message, "播放成功!", c)
|
|
|
}
|
|
|
|
|
|
func (api *IpcastApi) Stop(c *gin.Context) {
|
|
|
- baseUrl := IpcastBaseUrl(c)
|
|
|
- if baseUrl == "" {
|
|
|
- response.FailWithMessage("获取baseUrl失败", c)
|
|
|
- return
|
|
|
- }
|
|
|
- url := baseUrl + "/v1/speech"
|
|
|
+ url := GetIpcastUrl(c, Stop)
|
|
|
all, err := ipcastService.Common(url, http.MethodDelete, nil)
|
|
|
if err != nil {
|
|
|
response.Fail(c)
|
|
@@ -51,16 +46,11 @@ func (api *IpcastApi) Stop(c *gin.Context) {
|
|
|
}
|
|
|
var resp ipResp.PingResp
|
|
|
json.Unmarshal(all, &resp)
|
|
|
- response.OkWithData(resp.Message, c)
|
|
|
+ response.OkWithDetailed(resp.Message, "操作成功!", c)
|
|
|
}
|
|
|
|
|
|
func (api *IpcastApi) Status(c *gin.Context) {
|
|
|
- baseUrl := IpcastBaseUrl(c)
|
|
|
- if baseUrl == "" {
|
|
|
- response.FailWithMessage("获取baseUrl失败", c)
|
|
|
- return
|
|
|
- }
|
|
|
- url := baseUrl + "/v1/play_status"
|
|
|
+ url := GetIpcastUrl(c, Status)
|
|
|
all, err := ipcastService.Common(url, http.MethodGet, nil)
|
|
|
if err != nil {
|
|
|
response.Fail(c)
|
|
@@ -73,12 +63,7 @@ func (api *IpcastApi) Status(c *gin.Context) {
|
|
|
}
|
|
|
|
|
|
func (api *IpcastApi) Ping(c *gin.Context) {
|
|
|
- baseUrl := IpcastBaseUrl(c)
|
|
|
- if baseUrl == "" {
|
|
|
- response.FailWithMessage("获取baseUrl失败", c)
|
|
|
- return
|
|
|
- }
|
|
|
- url := baseUrl + "/v1/check_alive"
|
|
|
+ url := GetIpcastUrl(c, Ping)
|
|
|
all, err := ipcastService.Common(url, http.MethodGet, nil)
|
|
|
if err != nil {
|
|
|
response.Fail(c)
|
|
@@ -91,12 +76,7 @@ func (api *IpcastApi) Ping(c *gin.Context) {
|
|
|
}
|
|
|
|
|
|
func (api *IpcastApi) Volume(c *gin.Context) {
|
|
|
- baseUrl := IpcastBaseUrl(c)
|
|
|
- if baseUrl == "" {
|
|
|
- response.FailWithMessage("获取baseUrl失败", c)
|
|
|
- return
|
|
|
- }
|
|
|
- url := baseUrl + "/v1/volume"
|
|
|
+ url := GetIpcastUrl(c, Volume)
|
|
|
data, err := c.GetRawData()
|
|
|
if err != nil {
|
|
|
response.FailWithMessage("获取请求参数出错", c)
|
|
@@ -111,9 +91,110 @@ func (api *IpcastApi) Volume(c *gin.Context) {
|
|
|
}
|
|
|
var resp ipResp.PingResp
|
|
|
json.Unmarshal(all, &resp)
|
|
|
- response.OkWithData(resp.Message, c)
|
|
|
+ response.OkWithDetailed(resp.Message, "操作成功!", c)
|
|
|
}
|
|
|
|
|
|
func (api *IpcastApi) List(c *gin.Context) {
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+//↓实现语音喊话功能↓
|
|
|
+
|
|
|
+var audio = make(map[string][]byte)
|
|
|
+
|
|
|
+// volume 播放⾳量:取值【1~100】
|
|
|
+// duration 循环(重复)播放时⻓(秒)
|
|
|
+// times 循环(重复)播放次数(次)
|
|
|
+// gap 循环(重复)播放中的间歇时间(秒)
|
|
|
+// todo 修改ip端口 106.52.134.22
|
|
|
+var ipcastData = `
|
|
|
+{
|
|
|
+ "url": "http://192.168.110.69:8889/ipcast/AudioSource/%s",
|
|
|
+ "sync": false,
|
|
|
+ "queue": true,
|
|
|
+ "volume": 50,
|
|
|
+ "loop": {
|
|
|
+ "duration": 600,
|
|
|
+ "times": 1,
|
|
|
+ "gap": 2
|
|
|
+ }
|
|
|
+}
|
|
|
+`
|
|
|
+
|
|
|
+// PlayAudio 前端传递mp3资源
|
|
|
+func (api *IpcastApi) PlayAudio(c *gin.Context) {
|
|
|
+ //生成uuid
|
|
|
+ uuid := make([]byte, 8)
|
|
|
+ rand.Read(uuid)
|
|
|
+ uuidString := hex.EncodeToString(uuid)
|
|
|
+ defer func() {
|
|
|
+ go func() {
|
|
|
+ //播放完后清除数据
|
|
|
+ time.Sleep(10 * time.Second)
|
|
|
+ delete(audio, uuidString)
|
|
|
+ }()
|
|
|
+ }()
|
|
|
+ //读取mp3数据
|
|
|
+ mp3, err := c.GetRawData()
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("获取请求参数出错", c)
|
|
|
+ logrus.Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //保存到内存
|
|
|
+ audio[uuidString] = mp3
|
|
|
+ //播放
|
|
|
+ data := []byte(fmt.Sprintf(ipcastData, uuidString))
|
|
|
+ all, err := ipcastService.Common(GetIpcastUrl(c, Play), http.MethodPost, data)
|
|
|
+ fmt.Println("data:", string(data))
|
|
|
+ if err != nil {
|
|
|
+ response.Fail(c)
|
|
|
+ logrus.Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var resp ipResp.PingResp
|
|
|
+ json.Unmarshal(all, &resp)
|
|
|
+ response.OkWithData(resp.Message, c)
|
|
|
+}
|
|
|
+
|
|
|
+// AudioSource 给ip音柱播放的url资源
|
|
|
+func (api *IpcastApi) AudioSource(c *gin.Context) {
|
|
|
+ id := c.Param("id")
|
|
|
+ fmt.Println("id:", id)
|
|
|
+ // 设置响应头,指定内容类型为 audio/mpeg
|
|
|
+ c.Header("Content-Type", "audio/mp3")
|
|
|
+ c.Header("Access-Control-Allow-Origin", "*")
|
|
|
+ c.Header("Content-Length", strconv.Itoa(len(audio[id])))
|
|
|
+
|
|
|
+ // 将内存中的 MP3 数据作为响应返回给浏览器
|
|
|
+ c.Data(http.StatusOK, "audio/mp3", audio[id])
|
|
|
+}
|
|
|
+
|
|
|
+//↑实现语音喊话功能↑
|
|
|
+
|
|
|
+const (
|
|
|
+ Play = iota
|
|
|
+ Stop
|
|
|
+ Status
|
|
|
+ Ping
|
|
|
+ Volume
|
|
|
+)
|
|
|
+
|
|
|
+func GetIpcastUrl(c *gin.Context, expr byte) string {
|
|
|
+ baseUrl := IpcastBaseUrl(c)
|
|
|
+ if baseUrl == "" {
|
|
|
+ response.FailWithMessage("获取baseUrl失败", c)
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+ switch expr {
|
|
|
+ case Stop, Play:
|
|
|
+ return baseUrl + "/v1/speech"
|
|
|
+ case Status:
|
|
|
+ return baseUrl + "/v1/play_status"
|
|
|
+ case Ping:
|
|
|
+ return baseUrl + "/v1/check_alive"
|
|
|
+ case Volume:
|
|
|
+ return baseUrl + "/v1/volume"
|
|
|
+ }
|
|
|
+ return ""
|
|
|
+}
|