upstream-rtmp.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package http
  2. import (
  3. "fmt"
  4. "go-common/library/ecode"
  5. "go-common/library/log"
  6. bm "go-common/library/net/http/blademaster"
  7. "go-common/library/net/metadata"
  8. "net"
  9. "strconv"
  10. )
  11. // 获取上行推流地址, 一共三个方法调用
  12. // UpStream
  13. func getUpStreamRtmp(c *bm.Context) {
  14. params := c.Request.URL.Query()
  15. room := params.Get("room_id")
  16. // 来源:pc:表示PC端;ios:表示ios端;android:安卓端;ios_link:表示ios端;android_link: 安卓端;live_mng:表示live后台;vc_mng:表示vc后台;
  17. platform := params.Get("platform")
  18. // client_ip
  19. ip := params.Get("ip")
  20. // 分区id
  21. area := params.Get("area_id")
  22. // 免流标志
  23. freeFlow := params.Get("free_flow")
  24. attentions := params.Get("attentions")
  25. c.Set("input_params", params)
  26. if room == "" || platform == "" || area == "" {
  27. c.Set("output_data", "some fields are empty")
  28. c.JSONMap(map[string]interface{}{"message": "some fields are empty"}, ecode.RequestErr)
  29. c.Abort()
  30. return
  31. }
  32. roomID, err := strconv.ParseInt(room, 10, 64)
  33. if err != nil || roomID <= 0 {
  34. c.Set("output_data", "room_id is not right")
  35. c.JSONMap(map[string]interface{}{"message": "房间号错误"}, ecode.RequestErr)
  36. c.Abort()
  37. return
  38. }
  39. var attentionsInt int
  40. if attentions == "" {
  41. attentionsInt = 0
  42. } else {
  43. att, _ := strconv.ParseInt(attentions, 10, 64)
  44. attentionsInt = int(att)
  45. }
  46. // ip映射
  47. realIP := ip
  48. if ip == "" {
  49. remoteAddr := c.Request.RemoteAddr
  50. // 使用header: X-REAL-IP + X_FORWARED_FOR + reamoteadd
  51. if add := c.Request.Header.Get("X-REAL-IP"); add != "" {
  52. remoteAddr = add
  53. } else if add = c.Request.Header.Get("X_FORWARED_FOR"); add != "" {
  54. remoteAddr = add
  55. } else {
  56. remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
  57. }
  58. if remoteAddr == "::1" {
  59. remoteAddr = "127.0.0.1"
  60. }
  61. realIP = remoteAddr
  62. }
  63. areaID, _ := strconv.ParseInt(area, 10, 64)
  64. info, err := srv.GetUpStreamRtmp(c, roomID, freeFlow, realIP, areaID, attentionsInt, 0, platform)
  65. if err != nil {
  66. c.Set("output_data", err.Error())
  67. c.JSONMap(map[string]interface{}{"message": "获取线路信息失败,刷新页面或稍后重试"}, ecode.RequestErr)
  68. c.Abort()
  69. return
  70. }
  71. if info == nil {
  72. c.Set("output_data", fmt.Sprintf("can find any info by room_id=%d", roomID))
  73. c.JSONMap(map[string]interface{}{"message": "获取线路信息失败,刷新页面或稍后重试"}, ecode.RequestErr)
  74. c.Abort()
  75. return
  76. }
  77. c.Set("output_data", info)
  78. c.JSONMap(map[string]interface{}{"data": map[string]interface{}{"up_stream": info}}, nil)
  79. }
  80. // getWebRtmp web端调用
  81. func getWebRtmp(c *bm.Context) {
  82. // 获取room_id
  83. params := c.Request.URL.Query()
  84. room := params.Get("room_id")
  85. c.Set("input_params", params)
  86. roomID, err := strconv.ParseInt(room, 10, 64)
  87. if err != nil || roomID <= 0 {
  88. c.Set("output_data", "room_id is not right")
  89. c.JSONMap(map[string]interface{}{"message": "房间号不正确"}, ecode.RequestErr)
  90. c.Abort()
  91. return
  92. }
  93. // 获取uid
  94. uid, ok := metadata.Value(c, metadata.Mid).(int64)
  95. //uid = 19148701
  96. //ok = true
  97. //log.Infov(c, log.KV("log", fmt.Sprintf("uid=%v", uid)))
  98. if !ok {
  99. log.Warn("%v=%v", uid, ok)
  100. c.Set("output_data", "未登陆")
  101. c.JSONMap(map[string]interface{}{"message": fmt.Sprintf("未登陆")}, ecode.RequestErr)
  102. c.Abort()
  103. return
  104. }
  105. remoteAddr := c.Request.RemoteAddr
  106. // 使用header: X-REAL-IP + X_FORWARED_FOR + reamoteadd
  107. if add := c.Request.Header.Get("X-REAL-IP"); add != "" {
  108. remoteAddr = add
  109. } else if add = c.Request.Header.Get("X_FORWARED_FOR"); add != "" {
  110. remoteAddr = add
  111. } else {
  112. remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
  113. }
  114. if remoteAddr == "::1" {
  115. remoteAddr = "127.0.0.1"
  116. }
  117. realIP := remoteAddr
  118. info, err := srv.GetWebRtmp(c, roomID, uid, realIP, "web")
  119. if err != nil {
  120. c.Set("output_data", err.Error())
  121. c.JSONMap(map[string]interface{}{"message": "获取线路信息失败,刷新页面或稍后重试"}, ecode.RequestErr)
  122. c.Abort()
  123. return
  124. }
  125. if info == nil {
  126. c.Set("output_data", fmt.Sprintf("can find any info by room_id=%d", roomID))
  127. c.JSONMap(map[string]interface{}{"message": "获取线路信息失败,刷新页面或稍后重试"}, ecode.RequestErr)
  128. c.Abort()
  129. return
  130. }
  131. c.Set("output_data", info)
  132. c.JSONMap(map[string]interface{}{"data": info}, nil)
  133. }
  134. // getMobileRtmp 移动端调用
  135. func getMobileRtmp(c *bm.Context) {
  136. params := c.Request.URL.Query()
  137. room := params.Get("room_id")
  138. // 来源:pc:表示PC端;ios:表示ios端;android:安卓端;ios_link:表示ios端;android_link: 安卓端;live_mng:表示live后台;vc_mng:表示vc后台;
  139. platform := params.Get("platform")
  140. // client_ip
  141. ip := params.Get("ip")
  142. // 分区id
  143. area := params.Get("area_id")
  144. // 免流标志
  145. freeFlow := params.Get("free_flow")
  146. c.Set("input_params", params)
  147. if room == "" || platform == "" || area == "" {
  148. c.Set("output_data", "some fields are empty")
  149. c.JSONMap(map[string]interface{}{"message": "some fields are empty"}, ecode.RequestErr)
  150. c.Abort()
  151. return
  152. }
  153. roomID, err := strconv.ParseInt(room, 10, 64)
  154. if err != nil || roomID <= 0 {
  155. c.Set("output_data", "房间号错误")
  156. c.JSONMap(map[string]interface{}{"message": "房间号错误"}, ecode.RequestErr)
  157. c.Abort()
  158. return
  159. }
  160. // ip映射
  161. realIP := ip
  162. if ip == "" {
  163. remoteAddr := c.Request.RemoteAddr
  164. // 使用header: X-REAL-IP + X_FORWARED_FOR + reamoteadd
  165. if add := c.Request.Header.Get("X-REAL-IP"); add != "" {
  166. remoteAddr = add
  167. } else if add = c.Request.Header.Get("X_FORWARED_FOR"); add != "" {
  168. remoteAddr = add
  169. } else {
  170. remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
  171. }
  172. if remoteAddr == "::1" {
  173. remoteAddr = "127.0.0.1"
  174. }
  175. realIP = remoteAddr
  176. }
  177. areaID, _ := strconv.ParseInt(area, 10, 64)
  178. // 获取uid
  179. uid, ok := metadata.Value(c, metadata.Mid).(int64)
  180. //uid = 19148701
  181. //ok = true
  182. if !ok {
  183. c.Set("output_data", "未登陆")
  184. c.JSONMap(map[string]interface{}{"message": fmt.Sprintf("未登陆")}, ecode.RequestErr)
  185. c.Abort()
  186. return
  187. }
  188. info, err := srv.GetUpStreamRtmp(c, roomID, freeFlow, realIP, areaID, 0, uid, platform)
  189. if err != nil {
  190. c.Set("output_data", err.Error())
  191. c.JSONMap(map[string]interface{}{"message": "获取线路信息失败,刷新页面或稍后重试"}, ecode.RequestErr)
  192. c.Abort()
  193. return
  194. }
  195. if info == nil {
  196. c.Set("output_data", fmt.Sprintf("can find any info by room_id=%d", roomID))
  197. c.JSONMap(map[string]interface{}{"message": "获取线路信息失败,刷新页面或稍后重试"}, ecode.RequestErr)
  198. c.Abort()
  199. return
  200. }
  201. c.Set("output_data", info)
  202. c.JSONMap(map[string]interface{}{"data": map[string]interface{}{"up_stream": info}}, nil)
  203. }
  204. // getRoomRtmp 拜年祭房间推流码接口
  205. func getRoomRtmp(c *bm.Context) {
  206. params := c.Request.URL.Query()
  207. c.Set("input_params", params)
  208. room := params.Get("room_id")
  209. roomID, err := strconv.ParseInt(room, 10, 64)
  210. if err != nil || roomID <= 0 {
  211. c.Set("output_data", "房间号不正确")
  212. c.JSONMap(map[string]interface{}{"message": "房间号不正确"}, ecode.RequestErr)
  213. c.Abort()
  214. return
  215. }
  216. info, err := srv.GetRoomRtmp(c, roomID)
  217. if err != nil {
  218. c.Set("output_data", err.Error())
  219. c.JSONMap(map[string]interface{}{"message": "获取房间信息失败"}, ecode.RequestErr)
  220. c.Abort()
  221. return
  222. }
  223. if info == nil {
  224. c.Set("output_data", fmt.Sprintf("can find any info by room_id=%d", roomID))
  225. c.JSONMap(map[string]interface{}{"message": "获取房间信息失败,请确认是否房间存在"}, ecode.RequestErr)
  226. c.Abort()
  227. return
  228. }
  229. c.Set("output_data", info)
  230. c.JSONMap(map[string]interface{}{"data": info}, nil)
  231. }