123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- package http
- import (
- "fmt"
- "go-common/library/ecode"
- "go-common/library/log"
- bm "go-common/library/net/http/blademaster"
- "go-common/library/net/metadata"
- "net"
- "strconv"
- )
- // 获取上行推流地址, 一共三个方法调用
- // UpStream
- func getUpStreamRtmp(c *bm.Context) {
- params := c.Request.URL.Query()
- room := params.Get("room_id")
- // 来源:pc:表示PC端;ios:表示ios端;android:安卓端;ios_link:表示ios端;android_link: 安卓端;live_mng:表示live后台;vc_mng:表示vc后台;
- platform := params.Get("platform")
- // client_ip
- ip := params.Get("ip")
- // 分区id
- area := params.Get("area_id")
- // 免流标志
- freeFlow := params.Get("free_flow")
- attentions := params.Get("attentions")
- c.Set("input_params", params)
- if room == "" || platform == "" || area == "" {
- c.Set("output_data", "some fields are empty")
- c.JSONMap(map[string]interface{}{"message": "some fields are empty"}, ecode.RequestErr)
- c.Abort()
- return
- }
- roomID, err := strconv.ParseInt(room, 10, 64)
- if err != nil || roomID <= 0 {
- c.Set("output_data", "room_id is not right")
- c.JSONMap(map[string]interface{}{"message": "房间号错误"}, ecode.RequestErr)
- c.Abort()
- return
- }
- var attentionsInt int
- if attentions == "" {
- attentionsInt = 0
- } else {
- att, _ := strconv.ParseInt(attentions, 10, 64)
- attentionsInt = int(att)
- }
- // ip映射
- realIP := ip
- if ip == "" {
- remoteAddr := c.Request.RemoteAddr
- // 使用header: X-REAL-IP + X_FORWARED_FOR + reamoteadd
- if add := c.Request.Header.Get("X-REAL-IP"); add != "" {
- remoteAddr = add
- } else if add = c.Request.Header.Get("X_FORWARED_FOR"); add != "" {
- remoteAddr = add
- } else {
- remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
- }
- if remoteAddr == "::1" {
- remoteAddr = "127.0.0.1"
- }
- realIP = remoteAddr
- }
- areaID, _ := strconv.ParseInt(area, 10, 64)
- info, err := srv.GetUpStreamRtmp(c, roomID, freeFlow, realIP, areaID, attentionsInt, 0, platform)
- if err != nil {
- c.Set("output_data", err.Error())
- c.JSONMap(map[string]interface{}{"message": "获取线路信息失败,刷新页面或稍后重试"}, ecode.RequestErr)
- c.Abort()
- return
- }
- if info == nil {
- c.Set("output_data", fmt.Sprintf("can find any info by room_id=%d", roomID))
- c.JSONMap(map[string]interface{}{"message": "获取线路信息失败,刷新页面或稍后重试"}, ecode.RequestErr)
- c.Abort()
- return
- }
- c.Set("output_data", info)
- c.JSONMap(map[string]interface{}{"data": map[string]interface{}{"up_stream": info}}, nil)
- }
- // getWebRtmp web端调用
- func getWebRtmp(c *bm.Context) {
- // 获取room_id
- params := c.Request.URL.Query()
- room := params.Get("room_id")
- c.Set("input_params", params)
- roomID, err := strconv.ParseInt(room, 10, 64)
- if err != nil || roomID <= 0 {
- c.Set("output_data", "room_id is not right")
- c.JSONMap(map[string]interface{}{"message": "房间号不正确"}, ecode.RequestErr)
- c.Abort()
- return
- }
- // 获取uid
- uid, ok := metadata.Value(c, metadata.Mid).(int64)
- //uid = 19148701
- //ok = true
- //log.Infov(c, log.KV("log", fmt.Sprintf("uid=%v", uid)))
- if !ok {
- log.Warn("%v=%v", uid, ok)
- c.Set("output_data", "未登陆")
- c.JSONMap(map[string]interface{}{"message": fmt.Sprintf("未登陆")}, ecode.RequestErr)
- c.Abort()
- return
- }
- remoteAddr := c.Request.RemoteAddr
- // 使用header: X-REAL-IP + X_FORWARED_FOR + reamoteadd
- if add := c.Request.Header.Get("X-REAL-IP"); add != "" {
- remoteAddr = add
- } else if add = c.Request.Header.Get("X_FORWARED_FOR"); add != "" {
- remoteAddr = add
- } else {
- remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
- }
- if remoteAddr == "::1" {
- remoteAddr = "127.0.0.1"
- }
- realIP := remoteAddr
- info, err := srv.GetWebRtmp(c, roomID, uid, realIP, "web")
- if err != nil {
- c.Set("output_data", err.Error())
- c.JSONMap(map[string]interface{}{"message": "获取线路信息失败,刷新页面或稍后重试"}, ecode.RequestErr)
- c.Abort()
- return
- }
- if info == nil {
- c.Set("output_data", fmt.Sprintf("can find any info by room_id=%d", roomID))
- c.JSONMap(map[string]interface{}{"message": "获取线路信息失败,刷新页面或稍后重试"}, ecode.RequestErr)
- c.Abort()
- return
- }
- c.Set("output_data", info)
- c.JSONMap(map[string]interface{}{"data": info}, nil)
- }
- // getMobileRtmp 移动端调用
- func getMobileRtmp(c *bm.Context) {
- params := c.Request.URL.Query()
- room := params.Get("room_id")
- // 来源:pc:表示PC端;ios:表示ios端;android:安卓端;ios_link:表示ios端;android_link: 安卓端;live_mng:表示live后台;vc_mng:表示vc后台;
- platform := params.Get("platform")
- // client_ip
- ip := params.Get("ip")
- // 分区id
- area := params.Get("area_id")
- // 免流标志
- freeFlow := params.Get("free_flow")
- c.Set("input_params", params)
- if room == "" || platform == "" || area == "" {
- c.Set("output_data", "some fields are empty")
- c.JSONMap(map[string]interface{}{"message": "some fields are empty"}, ecode.RequestErr)
- c.Abort()
- return
- }
- roomID, err := strconv.ParseInt(room, 10, 64)
- if err != nil || roomID <= 0 {
- c.Set("output_data", "房间号错误")
- c.JSONMap(map[string]interface{}{"message": "房间号错误"}, ecode.RequestErr)
- c.Abort()
- return
- }
- // ip映射
- realIP := ip
- if ip == "" {
- remoteAddr := c.Request.RemoteAddr
- // 使用header: X-REAL-IP + X_FORWARED_FOR + reamoteadd
- if add := c.Request.Header.Get("X-REAL-IP"); add != "" {
- remoteAddr = add
- } else if add = c.Request.Header.Get("X_FORWARED_FOR"); add != "" {
- remoteAddr = add
- } else {
- remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
- }
- if remoteAddr == "::1" {
- remoteAddr = "127.0.0.1"
- }
- realIP = remoteAddr
- }
- areaID, _ := strconv.ParseInt(area, 10, 64)
- // 获取uid
- uid, ok := metadata.Value(c, metadata.Mid).(int64)
- //uid = 19148701
- //ok = true
- if !ok {
- c.Set("output_data", "未登陆")
- c.JSONMap(map[string]interface{}{"message": fmt.Sprintf("未登陆")}, ecode.RequestErr)
- c.Abort()
- return
- }
- info, err := srv.GetUpStreamRtmp(c, roomID, freeFlow, realIP, areaID, 0, uid, platform)
- if err != nil {
- c.Set("output_data", err.Error())
- c.JSONMap(map[string]interface{}{"message": "获取线路信息失败,刷新页面或稍后重试"}, ecode.RequestErr)
- c.Abort()
- return
- }
- if info == nil {
- c.Set("output_data", fmt.Sprintf("can find any info by room_id=%d", roomID))
- c.JSONMap(map[string]interface{}{"message": "获取线路信息失败,刷新页面或稍后重试"}, ecode.RequestErr)
- c.Abort()
- return
- }
- c.Set("output_data", info)
- c.JSONMap(map[string]interface{}{"data": map[string]interface{}{"up_stream": info}}, nil)
- }
- // getRoomRtmp 拜年祭房间推流码接口
- func getRoomRtmp(c *bm.Context) {
- params := c.Request.URL.Query()
- c.Set("input_params", params)
- room := params.Get("room_id")
- roomID, err := strconv.ParseInt(room, 10, 64)
- if err != nil || roomID <= 0 {
- c.Set("output_data", "房间号不正确")
- c.JSONMap(map[string]interface{}{"message": "房间号不正确"}, ecode.RequestErr)
- c.Abort()
- return
- }
- info, err := srv.GetRoomRtmp(c, roomID)
- if err != nil {
- c.Set("output_data", err.Error())
- c.JSONMap(map[string]interface{}{"message": "获取房间信息失败"}, ecode.RequestErr)
- c.Abort()
- return
- }
- if info == nil {
- c.Set("output_data", fmt.Sprintf("can find any info by room_id=%d", roomID))
- c.JSONMap(map[string]interface{}{"message": "获取房间信息失败,请确认是否房间存在"}, ecode.RequestErr)
- c.Abort()
- return
- }
- c.Set("output_data", info)
- c.JSONMap(map[string]interface{}{"data": info}, nil)
- }
|