-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapi_gb.go
184 lines (154 loc) · 4.22 KB
/
api_gb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"fmt"
"github.com/lkmio/avformat/utils"
"github.com/lkmio/lkm/gb28181"
"github.com/lkmio/lkm/log"
"github.com/lkmio/lkm/stream"
"net"
"net/http"
"strings"
)
type GBForwardParams struct {
Source string `json:"source"` //GetSourceID
Addr string `json:"addr"`
SSRC uint32 `json:"ssrc"`
Setup string `json:"setup"`
}
type GBSourceParams struct {
Source string `json:"source"` //GetSourceID
Setup string `json:"setup"` //active/passive
SSRC uint32 `json:"ssrc,omitempty"`
}
type GBConnect struct {
Source string `json:"source"` //GetSourceID
RemoteAddr string `json:"remote_addr"`
}
func (api *ApiServer) OnGBSourceCreate(v *GBSourceParams, w http.ResponseWriter, r *http.Request) {
log.Sugar.Infof("创建国标源: %v", v)
// 返回收流地址
response := &struct {
IP string `json:"ip"`
Port int `json:"port,omitempty"`
Urls []string `json:"urls"`
}{}
var err error
// 响应错误消息
defer func() {
if err != nil {
log.Sugar.Errorf("创建国标源失败 err: %s", err.Error())
httpResponseError(w, err.Error())
}
}()
source := stream.SourceManager.Find(v.Source)
if source != nil {
err = fmt.Errorf("%s 源已经存在", v.Source)
return
}
tcp := true
var active bool
if v.Setup == "passive" {
} else if v.Setup == "active" {
active = true
} else {
tcp = false
//udp收流
}
if tcp && active {
if !stream.AppConfig.GB28181.IsMultiPort() {
err = fmt.Errorf("单端口模式下不能主动拉流")
} else if !tcp {
err = fmt.Errorf("UDP不能主动拉流")
} else if !stream.AppConfig.GB28181.IsEnableTCP() {
err = fmt.Errorf("未开启TCP收流服务,UDP不能主动拉流")
}
if err != nil {
return
}
}
_, port, err := gb28181.NewGBSource(v.Source, v.SSRC, tcp, active)
if err != nil {
return
}
response.IP = stream.AppConfig.PublicIP
response.Port = port
response.Urls = stream.GetStreamPlayUrls(v.Source)
httpResponseOK(w, response)
}
func (api *ApiServer) OnGBSourceConnect(v *GBConnect, w http.ResponseWriter, r *http.Request) {
log.Sugar.Infof("设置国标主动拉流连接地址: %v", v)
var err error
// 响应错误消息
defer func() {
if err != nil {
log.Sugar.Errorf("设置国标主动拉流失败 err: %s", err.Error())
httpResponseError(w, err.Error())
}
}()
source := stream.SourceManager.Find(v.Source)
if source == nil {
err = fmt.Errorf("%s 源不存在", v.Source)
return
}
activeSource, ok := source.(*gb28181.ActiveSource)
if !ok {
err = fmt.Errorf("%s 源不是Active拉流类型", v.Source)
return
}
addr, err := net.ResolveTCPAddr("tcp", v.RemoteAddr)
if err != nil {
return
}
if err = activeSource.Connect(addr); err == nil {
httpResponseOK(w, nil)
}
}
func (api *ApiServer) OnGBSourceForward(v *GBForwardParams, w http.ResponseWriter, r *http.Request) {
log.Sugar.Infof("设置国标级联转发: %v", v)
var err error
// 响应错误消息
defer func() {
if err != nil {
log.Sugar.Errorf("设置级联转发失败 err: %s", err.Error())
httpResponseError(w, err.Error())
}
}()
source := stream.SourceManager.Find(v.Source)
if source == nil {
err = fmt.Errorf("%s 源不存在", v.Source)
} else if source.GetType() != stream.SourceType28181 {
log.Sugar.Infof("%s 源不是国标推流类型", v.Source)
return
}
var setup gb28181.SetupType
switch strings.ToLower(v.Setup) {
case "active":
setup = gb28181.SetupActive
break
case "passive":
setup = gb28181.SetupPassive
break
default:
setup = gb28181.SetupUDP
break
}
addr, _ := net.ResolveTCPAddr("tcp", r.RemoteAddr)
sinkId := stream.NetAddr2SinkId(addr)
// 添加随机数
if ipv4, ok := sinkId.(uint64); ok {
random := uint64(utils.RandomIntInRange(0x1000, 0xFFFF0000))
sinkId = (ipv4 & 0xFFFFFFFF00000000) | (random << 16) | (ipv4 & 0xFFFF)
}
sink, port, err := gb28181.NewForwardSink(v.SSRC, v.Addr, setup, sinkId, v.Source)
if err != nil {
return
}
source.AddSink(sink)
log.Sugar.Infof("设置国标级联转发成功 ID: %s", sink.GetID())
response := struct {
Sink string `json:"sink"` //sink id
IP string `json:"ip"`
Port int `json:"port"`
}{Sink: stream.SinkId2String(sinkId), IP: stream.AppConfig.PublicIP, Port: port}
httpResponseOK(w, &response)
}