-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathapi_gb.go
More file actions
383 lines (325 loc) · 10.7 KB
/
api_gb.go
File metadata and controls
383 lines (325 loc) · 10.7 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package main
import (
"encoding/base64"
"fmt"
"github.com/gorilla/mux"
audio_transcoder "github.com/lkmio/audio-transcoder"
"github.com/lkmio/avformat/bufio"
"github.com/lkmio/lkm/gb28181"
"github.com/lkmio/lkm/log"
"github.com/lkmio/lkm/stream"
"net"
"net/http"
"strconv"
"time"
)
const (
InviteTypePlay = "play"
InviteTypePlayback = "playback"
InviteTypeDownload = "download"
InviteTypeBroadcast = "broadcast"
InviteTypeTalk = "talk"
)
type SDP struct {
SessionName string `json:"session_name,omitempty"` // play/download/playback/talk/broadcast
Addr string `json:"addr,omitempty"` // 连接地址
SSRC string `json:"ssrc,omitempty"`
Setup string `json:"setup,omitempty"` // active/passive
Transport string `json:"transport,omitempty"` // tcp/udp
Speed float64 `json:"speed,omitempty"`
StartTime int `json:"start_time,omitempty"`
EndTime int `json:"end_time,omitempty"`
FileSize int `json:"file_size,omitempty"`
}
type DownloadInfo struct {
PlaybackDuration int // 回放/下载时长
PlaybackSpeed float64 // 回放/下载速度
PlaybackFileURL string // 回放/下载文件URL
PlaybackStartTime string // 回放/下载开始时间
PlaybackEndTime string // 回放/下载结束时间
PlaybackFileSize int // 回放/下载文件大小
PlaybackProgress float64 // 1-下载完成
Progress float64
}
type SourceSDP struct {
Source string `json:"source"` // GetSourceID
SDP
}
type GBOffer struct {
SourceSDP
AnswerSetup string `json:"answer_setup,omitempty"` // 希望应答的连接方式
TransStreamProtocol stream.TransStreamProtocol `json:"trans_stream_protocol,omitempty"`
}
func Source2GBSource(source stream.Source) gb28181.GBSource {
if gbSource, ok := source.(*gb28181.PassiveSource); ok {
return gbSource
} else if gbSource, ok := source.(*gb28181.ActiveSource); ok {
return gbSource
} else if gbSource, ok := source.(*gb28181.PassiveSource); ok {
return gbSource
}
return nil
}
func (api *ApiServer) OnGBSourceCreate(v *SourceSDP, w http.ResponseWriter, r *http.Request) {
log.Sugar.Infof("创建国标源: %v", v)
// 返回收流地址
response := &struct {
SDP
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收流
}
var ssrc string
if InviteTypeDownload == v.SessionName || InviteTypePlayback == v.SessionName {
ssrc = gb28181.GetVodSSRC()
} else {
ssrc = gb28181.GetLiveSSRC()
}
ssrcValue, _ := strconv.Atoi(ssrc)
gbSource, port, err := gb28181.NewGBSource(v.Source, uint32(ssrcValue), tcp, active)
if err != nil {
return
} else if InviteTypeDownload == v.SessionName {
// 开启录制
gbSource.GetTransStreamPublisher().StartRecord()
}
startTime := time.Unix(int64(v.StartTime), 0).Format("2006-01-02T15:04:05")
endTime := time.Unix(int64(v.EndTime), 0).Format("2006-01-02T15:04:05")
gbSource.SetSessionName(v.SessionName)
gbSource.SetStartTime(startTime)
gbSource.SetEndTime(endTime)
gbSource.SetSpeed(v.Speed)
gbSource.SetDuration(v.EndTime - v.StartTime)
response.Addr = net.JoinHostPort(stream.AppConfig.PublicIP, strconv.Itoa(port))
response.Urls = stream.GetStreamPlayUrls(v.Source)
response.SSRC = ssrc
log.Sugar.Infof("创建国标源成功, addr: %s, ssrc: %d", response.Addr, ssrcValue)
httpResponseOK(w, response)
}
func (api *ApiServer) OnGBSourceConnect(v *SourceSDP, 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 stream.SourceType28181 != source.GetType() {
err = fmt.Errorf("%s 源不是28181类型", v.Source)
} else if activeSource, ok := source.(*gb28181.ActiveSource); ok {
activeSource.SetFileSize(v.FileSize)
// 主动连接取流
var addr *net.TCPAddr
addr, err = net.ResolveTCPAddr("tcp", v.Addr)
if err != nil {
return
}
if err = activeSource.Connect(addr); err == nil {
httpResponseOK(w, nil)
}
} else if passiveSource, ok := source.(*gb28181.PassiveSource); ok {
passiveSource.SetFileSize(v.FileSize)
} else if udpSource, ok := source.(*gb28181.UDPSource); ok {
udpSource.SetFileSize(v.FileSize)
}
}
func (api *ApiServer) OnGBOfferCreate(v *SourceSDP, w http.ResponseWriter, r *http.Request) {
// 预览下级设备
if v.SessionName == "" || v.SessionName == InviteTypePlay ||
v.SessionName == InviteTypePlayback ||
v.SessionName == InviteTypeDownload {
api.OnGBSourceCreate(v, w, r)
} else {
// 向上级转发广播和对讲, 或者是向设备发送invite talk
}
}
func (api *ApiServer) AddForwardSink(protocol stream.TransStreamProtocol, transport stream.TransportType, sourceId string, remoteAddr string, ssrc, sessionName string, w http.ResponseWriter, r *http.Request) {
// 解析或生成应答的ssrc
var ssrcOffer int
var ssrcAnswer string
if ssrc != "" {
var err error
ssrcOffer, err = strconv.Atoi(ssrc)
if err != nil {
log.Sugar.Errorf("解析ssrc失败 err: %s ssrc: %s", err.Error(), ssrc)
} else {
ssrcAnswer = ssrc
}
}
if ssrcAnswer == "" {
if "download" != sessionName && "playback" != sessionName {
ssrcAnswer = gb28181.GetLiveSSRC()
} else {
ssrcAnswer = gb28181.GetVodSSRC()
}
var err error
ssrcOffer, err = strconv.Atoi(ssrcAnswer)
// 严重错误, 直接panic
if err != nil {
panic(err)
}
}
var port int
sink, port, err := stream.ForwardStream(protocol, transport, sourceId, r.URL.Query(), remoteAddr, gb28181.TransportManger, uint32(ssrcOffer))
if err != nil {
log.Sugar.Errorf("创建转发sink失败 err: %s", err.Error())
httpResponseError(w, err.Error())
return
}
log.Sugar.Infof("创建转发sink成功, sink: %s port: %d transport: %s ssrc: %s", sink.GetID(), port, transport, ssrcAnswer)
response := struct {
Sink string `json:"sink"` // sink id
SDP
}{Sink: stream.SinkID2String(sink.GetID()), SDP: SDP{Addr: net.JoinHostPort(stream.AppConfig.PublicIP, strconv.Itoa(port)), SSRC: ssrcAnswer}}
httpResponseOK(w, &response)
}
func (api *ApiServer) OnSinkAdd(v *GBOffer, w http.ResponseWriter, r *http.Request) {
log.Sugar.Infof("添加sink: %v", *v)
if stream.TransStreamGBCascaded != v.TransStreamProtocol && stream.TransStreamGBTalk != v.TransStreamProtocol && stream.TransStreamGBGateway != v.TransStreamProtocol {
httpResponseError(w, "不支持的协议")
return
}
setup := gb28181.SetupTypeFromString(v.Setup)
if v.AnswerSetup != "" {
setup = gb28181.SetupTypeFromString(v.AnswerSetup)
}
api.AddForwardSink(v.TransStreamProtocol, setup.TransportType(), v.Source, v.Addr, v.SSRC, v.SessionName, w, r)
}
// OnGBTalk 国标广播/对讲流程:
// 1. 浏览器使用WS携带source_id访问/api/v1/gb28181/talk, 如果source_id冲突, 直接断开ws连接
// 2. WS链接建立后, 调用gb-cms接口/api/v1/broadcast/invite, 向设备发送广播请求
func (api *ApiServer) OnGBTalk(w http.ResponseWriter, r *http.Request) {
conn, err := api.upgrader.Upgrade(w, r, nil)
if err != nil {
log.Sugar.Errorf("升级为websocket失败 err: %s", err.Error())
conn.Close()
return
}
// 获取id
id := r.FormValue("source")
talkSource := gb28181.NewTalkSource(id, conn)
talkSource.Init()
talkSource.SetUrlValues(r.Form)
_, err = stream.PreparePublishSource(talkSource, true)
if err != nil {
log.Sugar.Errorf("对讲失败, err: %s source: %s", err, talkSource)
conn.Close()
return
}
log.Sugar.Infof("ws对讲连接成功, source: %s", talkSource)
stream.LoopEvent(talkSource)
data := stream.UDPReceiveBufferPool.Get().([]byte)
for {
_, bytes, err := conn.ReadMessage()
length := len(bytes)
if err != nil {
log.Sugar.Errorf("读取对讲音频包失败, source: %s err: %s", id, err.Error())
break
} else if length < 1 {
continue
}
for i := 0; i < length; {
n := bufio.MinInt(stream.UDPReceiveBufferSize, length-i)
copy(data, bytes[:n])
_, _ = talkSource.PublishSource.Input(data[:n])
i += n
}
}
talkSource.Close()
}
// OnLiveGBSTalk liveGBS前端对讲
func (api *ApiServer) OnLiveGBSTalk(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
device := vars["device"]
channel := vars["channel"]
_ = r.URL.Query().Get("format")
conn, err := api.upgrader.Upgrade(w, r, nil)
if err != nil {
log.Sugar.Errorf("升级为websocket失败 err: %s", err.Error())
conn.Close()
return
}
id := device + "/" + channel + ".broadcast"
talkSource := gb28181.NewTalkSource(id, conn)
talkSource.Init()
talkSource.SetUrlValues(r.Form)
_, err = stream.PreparePublishSource(talkSource, true)
if err != nil {
log.Sugar.Errorf("对讲失败, err: %s source: %s", err, talkSource)
conn.Close()
return
}
log.Sugar.Infof("ws对讲连接成功, source: %s", talkSource)
stream.LoopEvent(talkSource)
data := stream.UDPReceiveBufferPool.Get().([]byte)
pcm := make([]byte, 32000)
g711aPacket := make([]byte, stream.UDPReceiveBufferSize/2)
for {
_, bytes, err := conn.ReadMessage()
length := len(bytes)
if err != nil {
log.Sugar.Errorf("读取对讲音频包失败, source: %s err: %s", id, err.Error())
break
} else if length < 1 {
continue
}
// 扩容
if int(float64(len(bytes))*1.4) > len(pcm) {
pcm = make([]byte, len(bytes)*2)
}
// base64解密
var pcmN int
pcmN, err = base64.StdEncoding.Decode(pcm, bytes)
if err != nil {
log.Sugar.Errorf("base64解密失败, source: %s err: %s", id, err.Error())
continue
}
for i := 0; i < pcmN; {
// 控制每包大小
n := bufio.MinInt(stream.UDPReceiveBufferSize, length-i)
copy(data, pcm[:n])
// 编码成G711A
audio_transcoder.EncodeAlawToBuffer(data, g711aPacket)
_, _ = talkSource.PublishSource.Input(g711aPacket[:n/2])
i += n
}
}
talkSource.Close()
}
func (api *ApiServer) OnGBSpeedSet(v *SourceSDP, w http.ResponseWriter, r *http.Request) {
source := stream.SourceManager.Find(v.Source)
if source == nil {
w.WriteHeader(http.StatusBadRequest)
httpResponseError(w, "stream not found")
} else if stream.SourceType28181 != source.GetType() {
w.WriteHeader(http.StatusBadRequest)
httpResponseError(w, "stream type not support")
} else if gbSource := Source2GBSource(source); gbSource != nil {
gbSource.SetSpeed(v.Speed)
}
}