stream.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package http
  2. import (
  3. "go-common/library/ecode"
  4. "go-common/library/log"
  5. bm "go-common/library/net/http/blademaster"
  6. "strconv"
  7. "strings"
  8. )
  9. // 简单的流信息的处理和返回
  10. // getStreamLastTime 得到流到最后推流时间
  11. func getStreamLastTime(c *bm.Context) {
  12. // 获取url中的room_id
  13. params := c.Request.URL.Query()
  14. room := params.Get("room_id")
  15. roomID, err := strconv.ParseInt(room, 10, 64)
  16. if err != nil || roomID <= 0 {
  17. c.Set("output_data", "room_id is not right")
  18. c.JSONMap(map[string]interface{}{"message": "room_id is not right"}, ecode.RequestErr)
  19. c.Abort()
  20. return
  21. }
  22. t, err := srv.GetStreamLastTime(c, roomID)
  23. if err != nil {
  24. log.Warn("%v", err)
  25. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  26. c.Abort()
  27. return
  28. }
  29. c.JSONMap(map[string]interface{}{"data": map[string]int64{"last_time": t}}, nil)
  30. }
  31. // getStreamNameByRoomID 根据房间号获取流名
  32. func getStreamNameByRoomID(c *bm.Context) {
  33. params := c.Request.URL.Query()
  34. room := params.Get("room_id")
  35. back := params.Get("back")
  36. roomID, err := strconv.ParseInt(room, 10, 64)
  37. if err != nil || roomID <= 0 {
  38. c.Set("output_data", "room_id is not right")
  39. c.JSONMap(map[string]interface{}{"message": "room_id is not right"}, ecode.RequestErr)
  40. c.Abort()
  41. return
  42. }
  43. flag := false
  44. if back == "1" {
  45. flag = true
  46. }
  47. info, err := srv.GetStreamNameByRoomID(c, roomID, flag)
  48. if err != nil {
  49. c.Set("output_data", err)
  50. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  51. c.Abort()
  52. return
  53. }
  54. c.Set("output_data", info)
  55. if !flag && len(info) > 0 {
  56. c.JSONMap(map[string]interface{}{"data": info[0]}, nil)
  57. return
  58. }
  59. c.JSONMap(map[string]interface{}{"data": info}, nil)
  60. }
  61. // getRoomIdByStreamName 得到房间号,传递流名,可传入备用流名
  62. func getRoomIDByStreamName(c *bm.Context) {
  63. params := c.Request.URL.Query()
  64. sname := params.Get("stream_name")
  65. sname = strings.TrimSpace(sname)
  66. if len(sname) == 0 {
  67. c.Set("output_data", "stream name is empty")
  68. c.JSONMap(map[string]interface{}{"message": "stream name is empty"}, ecode.RequestErr)
  69. c.Abort()
  70. return
  71. }
  72. rid, err := srv.GetRoomIDByStreamName(c, sname)
  73. if err != nil {
  74. c.Set("output_data", err)
  75. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  76. c.Abort()
  77. return
  78. }
  79. c.JSONMap(map[string]interface{}{"data": map[string]int64{"room_id": rid}}, nil)
  80. }
  81. // getAdapterStreamByStreamName 得到适配的流信息,迁移PHP接口
  82. func getAdapterStreamByStreamName(c *bm.Context) {
  83. params := c.Request.URL.Query()
  84. snames := params.Get("stream_names")
  85. snames = strings.TrimSpace(snames)
  86. if len(snames) == 0 {
  87. c.Set("output_data", "stream names is empty")
  88. c.JSONMap(map[string]interface{}{"message": "stream names is empty"}, ecode.RequestErr)
  89. c.Abort()
  90. return
  91. }
  92. // 最多查询500个数据
  93. nameSlice := strings.Split(snames, ",")
  94. if len(nameSlice) > 500 {
  95. c.Set("output_data", "too many names")
  96. c.JSONMap(map[string]interface{}{"message": "too many names"}, ecode.RequestErr)
  97. c.Abort()
  98. return
  99. }
  100. info := srv.GetAdapterStreamByStreamName(c, nameSlice)
  101. c.Set("output_data", info)
  102. c.JSONMap(map[string]interface{}{"data": info}, nil)
  103. }
  104. // getSrcByRoom 获取线路接口, 适配原PHP代码; 线路名称+线路编码src+是否当前选择的线路
  105. func getSrcByRoomID(c *bm.Context) {
  106. params := c.Request.URL.Query()
  107. room := params.Get("room_id")
  108. roomID, err := strconv.ParseInt(room, 10, 64)
  109. if err != nil || roomID <= 0 {
  110. c.Set("output_data", "room_id is not right")
  111. c.JSONMap(map[string]interface{}{"message": "room_id is not right"}, ecode.RequestErr)
  112. c.Abort()
  113. return
  114. }
  115. info, err := srv.GetSrcByRoomID(c, roomID)
  116. if err != nil {
  117. c.Set("output_data", err)
  118. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  119. c.Abort()
  120. return
  121. }
  122. c.Set("output_data", info)
  123. c.JSONMap(map[string]interface{}{"data": info}, nil)
  124. }
  125. // getLineListByRoomID 得下线路信息, 和getSrcByRoomID 只有返回的格式不一样
  126. func getLineListByRoomID(c *bm.Context) {
  127. params := c.Request.URL.Query()
  128. room := params.Get("room_id")
  129. roomID, err := strconv.ParseInt(room, 10, 64)
  130. if err != nil || roomID <= 0 {
  131. c.Set("output_data", "room_id is not right")
  132. c.JSONMap(map[string]interface{}{"message": "room_id is not right"}, ecode.RequestErr)
  133. c.Abort()
  134. return
  135. }
  136. info, err := srv.GetLineListByRoomID(c, roomID)
  137. if err != nil {
  138. c.Set("output_data", err)
  139. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  140. c.Abort()
  141. return
  142. }
  143. c.Set("output_data", info)
  144. c.JSONMap(map[string]interface{}{"data": info}, nil)
  145. }