-
Notifications
You must be signed in to change notification settings - Fork 14
/
config.go
172 lines (153 loc) · 3.39 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
package main
import (
"crypto/rand"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"sync"
"time"
"github.com/deepch/vdk/av"
)
//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:"-"`
Codecs []av.CodecData
Cl map[string]viewer
}
type viewer struct {
c chan *[]byte
}
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)
tmp.Streams[i] = v
}
return &tmp
}
func (element *ConfigST) cast(uuid string, pck []byte) {
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 *[]byte) {
element.mutex.Lock()
defer element.mutex.Unlock()
cuuid := pseudoUUID()
ch := make(chan *[]byte, 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
}