-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathconfig.go
269 lines (243 loc) · 6.54 KB
/
config.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
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
package main
import (
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"sort"
"strconv"
"sync"
"time"
"github.com/deepch/vdk/av"
)
var (
Success = "success"
ErrorStreamNotFound = errors.New("stream not found")
ErrorStreamAlreadyExists = errors.New("stream already exists")
ErrorStreamChannelAlreadyExists = errors.New("stream channel already exists")
ErrorStreamNotHLSSegments = errors.New("stream hls not ts seq found")
ErrorStreamNoVideo = errors.New("stream no video")
ErrorStreamNoClients = errors.New("stream no clients")
ErrorStreamRestart = errors.New("stream restart")
ErrorStreamStopCoreSignal = errors.New("stream stop core signal")
ErrorStreamStopRTSPSignal = errors.New("stream stop rtsp signal")
ErrorStreamChannelNotFound = errors.New("stream channel not found")
ErrorStreamChannelCodecNotFound = errors.New("stream channel codec not ready, possible stream offline")
ErrorStreamsLen0 = errors.New("streams len zero")
)
//Config global
var Config = loadConfig()
//ConfigST struct
type ConfigST struct {
mutex sync.RWMutex
Server ServerST `json:"server"`
Streams map[string]StreamST `json:"streams"`
}
//ServerST struct
type ServerST struct {
HTTPPort string `json:"http_port"`
}
//StreamST struct
type StreamST struct {
URL string `json:"url"`
Status bool `json:"status"`
OnDemand bool `json:"on_demand"`
RunLock bool `json:"-"`
hlsSegmentNumber int `json:"-"`
hlsSegmentBuffer map[int]Segment `json:"-"`
Codecs []av.CodecData
Cl map[string]viewer
}
//Segment HLS cache section
type Segment struct {
dur time.Duration
data []*av.Packet
}
type viewer struct {
c chan av.Packet
}
func (element *ConfigST) RunIFNotRun(uuid string) {
element.mutex.Lock()
defer element.mutex.Unlock()
if tmp, ok := element.Streams[uuid]; ok {
if tmp.OnDemand && !tmp.RunLock {
tmp.RunLock = true
element.Streams[uuid] = tmp
go RTSPWorkerLoop(uuid, tmp.URL, tmp.OnDemand)
}
}
}
func (element *ConfigST) RunUnlock(uuid string) {
element.mutex.Lock()
defer element.mutex.Unlock()
if tmp, ok := element.Streams[uuid]; ok {
if tmp.OnDemand && tmp.RunLock {
tmp.RunLock = false
element.Streams[uuid] = tmp
}
}
}
func (element *ConfigST) HasViewer(uuid string) bool {
element.mutex.Lock()
defer element.mutex.Unlock()
if tmp, ok := element.Streams[uuid]; ok && len(tmp.Cl) > 0 {
return true
}
return false
}
func loadConfig() *ConfigST {
var tmp ConfigST
data, err := ioutil.ReadFile("config.json")
if err != nil {
log.Fatalln(err)
}
err = json.Unmarshal(data, &tmp)
if err != nil {
log.Fatalln(err)
}
for i, v := range tmp.Streams {
v.Cl = make(map[string]viewer)
v.hlsSegmentBuffer = make(map[int]Segment)
tmp.Streams[i] = v
}
return &tmp
}
func (element *ConfigST) cast(uuid string, pck av.Packet) {
element.mutex.Lock()
defer element.mutex.Unlock()
for _, v := range element.Streams[uuid].Cl {
if len(v.c) < cap(v.c) {
v.c <- pck
}
}
}
func (element *ConfigST) ext(suuid string) bool {
element.mutex.Lock()
defer element.mutex.Unlock()
_, ok := element.Streams[suuid]
return ok
}
func (element *ConfigST) coAd(suuid string, codecs []av.CodecData) {
element.mutex.Lock()
defer element.mutex.Unlock()
t := element.Streams[suuid]
t.Codecs = codecs
element.Streams[suuid] = t
}
func (element *ConfigST) coGe(suuid string) []av.CodecData {
for i := 0; i < 100; i++ {
element.mutex.RLock()
tmp, ok := element.Streams[suuid]
element.mutex.RUnlock()
if !ok {
return nil
}
if tmp.Codecs != nil {
return tmp.Codecs
}
time.Sleep(50 * time.Millisecond)
}
return nil
}
func (element *ConfigST) clAd(suuid string) (string, chan av.Packet) {
element.mutex.Lock()
defer element.mutex.Unlock()
cuuid := pseudoUUID()
ch := make(chan av.Packet, 100)
element.Streams[suuid].Cl[cuuid] = viewer{c: ch}
return cuuid, ch
}
func (element *ConfigST) list() (string, []string) {
element.mutex.Lock()
defer element.mutex.Unlock()
var res []string
var fist string
for k := range element.Streams {
if fist == "" {
fist = k
}
res = append(res, k)
}
return fist, res
}
func (element *ConfigST) clDe(suuid, cuuid string) {
element.mutex.Lock()
defer element.mutex.Unlock()
delete(element.Streams[suuid].Cl, cuuid)
}
func pseudoUUID() (uuid string) {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
fmt.Println("Error: ", err)
return
}
uuid = fmt.Sprintf("%X-%X-%X-%X-%X", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return
}
//StreamHLSAdd add hls seq to buffer
func (obj *ConfigST) StreamHLSAdd(uuid string, val []*av.Packet, dur time.Duration) {
obj.mutex.Lock()
defer obj.mutex.Unlock()
if tmp, ok := obj.Streams[uuid]; ok {
tmp.hlsSegmentNumber++
tmp.hlsSegmentBuffer[tmp.hlsSegmentNumber] = Segment{data: val, dur: dur}
if len(tmp.hlsSegmentBuffer) >= 6 {
delete(tmp.hlsSegmentBuffer, tmp.hlsSegmentNumber-6-1)
}
obj.Streams[uuid] = tmp
}
}
//StreamHLSm3u8 get hls m3u8 list
func (obj *ConfigST) StreamHLSm3u8(uuid string) (string, int, error) {
obj.mutex.RLock()
defer obj.mutex.RUnlock()
if tmp, ok := obj.Streams[uuid]; ok {
var out string
//TODO fix it
out += "#EXTM3U\r\n#EXT-X-TARGETDURATION:4\r\n#EXT-X-VERSION:4\r\n#EXT-X-MEDIA-SEQUENCE:" + strconv.Itoa(tmp.hlsSegmentNumber) + "\r\n"
var keys []int
for k := range tmp.hlsSegmentBuffer {
keys = append(keys, k)
}
sort.Ints(keys)
var count int
for _, i := range keys {
count++
out += "#EXTINF:" + strconv.FormatFloat(tmp.hlsSegmentBuffer[i].dur.Seconds(), 'f', 1, 64) + ",\r\nsegment/" + strconv.Itoa(i) + "/file.ts\r\n"
}
return out, count, nil
}
return "", 0, ErrorStreamNotFound
}
//StreamHLSTS send hls segment buffer to clients
func (obj *ConfigST) StreamHLSTS(uuid string, seq int) ([]*av.Packet, error) {
obj.mutex.RLock()
defer obj.mutex.RUnlock()
if tmp, ok := obj.Streams[uuid]; ok {
if buf, ok := tmp.hlsSegmentBuffer[seq]; ok {
return buf.data, nil
}
}
return nil, ErrorStreamNotFound
}
//StreamHLSFlush delete hls cache
func (obj *ConfigST) StreamHLSFlush(uuid string) {
obj.mutex.Lock()
defer obj.mutex.Unlock()
if tmp, ok := obj.Streams[uuid]; ok {
tmp.hlsSegmentBuffer = make(map[int]Segment)
tmp.hlsSegmentNumber = 0
obj.Streams[uuid] = tmp
}
}
//stringToInt convert string to int if err to zero
func stringToInt(val string) int {
i, err := strconv.Atoi(val)
if err != nil {
return 0
}
return i
}