stream-info.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package http
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "go-common/app/service/video/stream-mng/model"
  6. "go-common/library/ecode"
  7. bm "go-common/library/net/http/blademaster"
  8. "io/ioutil"
  9. "strconv"
  10. "strings"
  11. )
  12. // addHotStream 增加房间热流标记
  13. func addHotStream(c *bm.Context) {
  14. req := c.Request
  15. bs, err := ioutil.ReadAll(req.Body)
  16. if err != nil {
  17. c.Set("output_data", "ioutil.ReadAll() error")
  18. c.JSONMap(map[string]interface{}{"message": "outil.ReadAll() error"}, err)
  19. c.Abort()
  20. return
  21. }
  22. req.Body.Close()
  23. var hrbody []string
  24. if err := json.Unmarshal(bs, &hrbody); err != nil {
  25. c.Set("output_data", "json.Unmarshal() error")
  26. c.JSONMap(map[string]interface{}{"message": "json.Unmarshal() error"}, err)
  27. c.Abort()
  28. return
  29. }
  30. if len(hrbody) <= 0 {
  31. c.Set("output_data", "some fields are empty")
  32. c.JSONMap(map[string]interface{}{"message": "some fields are empty"}, ecode.RequestErr)
  33. c.Abort()
  34. return
  35. }
  36. for _, streamName := range hrbody {
  37. srv.AddHotStreamInfo(c, streamName)
  38. }
  39. c.Set("output_data", "success")
  40. c.JSONMap(map[string]interface{}{"data": "success"}, nil)
  41. }
  42. // getStream 获取单个流信息
  43. func getStream(c *bm.Context) {
  44. params := c.Request.URL.Query()
  45. rid := params.Get("room_id")
  46. sname := params.Get("stream_name")
  47. if rid == "" && sname == "" {
  48. c.Set("output_data", "some fields are empty")
  49. c.JSONMap(map[string]interface{}{"message": "some fields are empty"}, ecode.RequestErr)
  50. c.Abort()
  51. return
  52. }
  53. var roomID int64
  54. var err error
  55. var info *model.StreamFullInfo
  56. if sname == "" {
  57. roomID, err = strconv.ParseInt(rid, 10, 64)
  58. // 验证传参数
  59. if err != nil || roomID <= 0 {
  60. c.Set("output_data", "roomid is not right")
  61. c.JSONMap(map[string]interface{}{"message": "roomid is not right"}, ecode.RequestErr)
  62. c.Abort()
  63. return
  64. }
  65. info, err = srv.GetStreamInfo(c, roomID, "")
  66. } else {
  67. info, err = srv.GetStreamInfo(c, 0, sname)
  68. }
  69. if err != nil {
  70. c.Set("output_data", err.Error())
  71. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  72. c.Abort()
  73. return
  74. }
  75. c.Set("output_data", info)
  76. c.JSONMap(map[string]interface{}{"data": info}, nil)
  77. }
  78. // getMulitiStreams 批量查询流接口
  79. func getMultiStreams(c *bm.Context) {
  80. params := c.Request.URL.Query()
  81. roomID := params.Get("room_ids")
  82. if roomID == "" {
  83. c.Set("output_data", "some fields are empty")
  84. c.JSONMap(map[string]interface{}{"message": "some fields are empty"}, ecode.RequestErr)
  85. c.Abort()
  86. return
  87. }
  88. roomIDs := []int64{}
  89. rids := strings.Split(roomID, ",")
  90. for _, v := range rids {
  91. rid, err := strconv.ParseInt(v, 10, 64)
  92. // 验证传参数
  93. if err == nil && rid > 0 {
  94. roomIDs = append(roomIDs, rid)
  95. }
  96. }
  97. if len(roomIDs) > 30 {
  98. c.Set("output_data", "The number of rooms must be less than 30")
  99. c.JSONMap(map[string]interface{}{"message": "The number of rooms must be less than 30"}, ecode.RequestErr)
  100. c.Abort()
  101. return
  102. }
  103. info, err := srv.GetMultiStreamInfo(c, roomIDs)
  104. if err != nil {
  105. c.Set("output_data", err.Error())
  106. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  107. c.Abort()
  108. return
  109. }
  110. if info == nil || len(info) == 0 {
  111. c.Set("output_data", fmt.Sprintf("can not find any info by room_ids=%s", roomID))
  112. c.JSONMap(map[string]interface{}{"message": fmt.Sprintf("can not find any info by room_ids=%s", roomID)}, ecode.RequestErr)
  113. c.Abort()
  114. return
  115. }
  116. c.Set("output_data", info)
  117. c.JSONMap(map[string]interface{}{"data": info}, nil)
  118. }
  119. // getOldStreamInfoByRoomID map 到原始src数据
  120. func getOldStreamInfoByRoomID(c *bm.Context) {
  121. params := c.Request.URL.Query()
  122. room := params.Get("roomid")
  123. room2 := params.Get("room_id")
  124. rid := ""
  125. if room == "" {
  126. rid = room2
  127. } else {
  128. rid = room
  129. }
  130. roomID, err := strconv.ParseInt(rid, 10, 64)
  131. // 验证传参数
  132. if err != nil || roomID <= 0 {
  133. c.Set("output_data", "roomid is not right")
  134. c.JSONMap(map[string]interface{}{"message": "roomid is not right"}, ecode.RequestErr)
  135. c.Abort()
  136. return
  137. }
  138. info, err := srv.GetStreamInfoByRIDMapSrcFromDB(c, roomID)
  139. if err != nil {
  140. c.Set("output_data", err.Error())
  141. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  142. c.Abort()
  143. return
  144. }
  145. c.Set("output_data", info)
  146. c.JSONMap(map[string]interface{}{"data": info}, nil)
  147. }
  148. // getOldStreamInfoByStreamName map到原始src数据
  149. func getOldStreamInfoByStreamName(c *bm.Context) {
  150. params := c.Request.URL.Query()
  151. sname := params.Get("stream_name")
  152. sname = strings.TrimSpace(sname)
  153. if sname == "" {
  154. c.JSONMap(map[string]interface{}{"message": "stream name is empty"}, ecode.RequestErr)
  155. c.Abort()
  156. return
  157. }
  158. info, err := srv.GetStreamInfoBySNameMapSrcFromDB(c, sname)
  159. if err != nil {
  160. c.Set("output_data", err.Error())
  161. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  162. c.Abort()
  163. return
  164. }
  165. c.Set("output_data", info)
  166. c.JSONMap(map[string]interface{}{"data": info}, nil)
  167. }