-
Notifications
You must be signed in to change notification settings - Fork 9
/
configStream.go
255 lines (230 loc) · 5.66 KB
/
configStream.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"sync"
"time"
"github.com/deepch/vdk/av"
)
const (
FPSModeFixed = iota
FPSModeSDP
FPSModeSPS
FPSModeProbe
FPSModePTS
)
//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 {
HTTPName string `json:"http_server_name"`
HTTPPort string `json:"http_port"`
HTTPSPort string `json:"https_port"`
}
//StreamST struct
type StreamST struct {
URL string `json:"url"`
Status bool `json:"status"`
OnDemand bool `json:"on_demand"`
FPSMode string `json:"fps_mode"`
FPSProbeTime int `json:"fps_probe_time"`
FPS int `json:"fps"`
HlsSegmentMinDuration int `json:"hls_segment_min_duration"`
HlsSegmentMaxSegments int `json:"hls_segment_max_segments"`
RunLock bool `json:"-"`
HlsMuxer *MuxerHLS `json:"-"`
Codecs []av.CodecData
}
//loadConfig func
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)
}
return &tmp
}
//RunIFNotRun if not run
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)
}
}
}
//RunUnlock lock run stream
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
}
}
}
//FPSMode func
func (element *ConfigST) FPSMode(uuid string) int {
element.mutex.RLock()
defer element.mutex.RUnlock()
if tmp, ok := element.Streams[uuid]; ok {
switch tmp.FPSMode {
case "fixed":
return FPSModeFixed
case "sdp":
return FPSModeSDP
case "sps":
return FPSModeSPS
case "probe":
return FPSModeProbe
case "pts":
return FPSModePTS
default:
return FPSModeProbe
}
}
return FPSModeProbe
}
//ext check stream exists
func (element *ConfigST) ext(uuid string) bool {
element.mutex.Lock()
defer element.mutex.Unlock()
_, ok := element.Streams[uuid]
return ok
}
//coAd add codec to stream
func (element *ConfigST) coAd(uuid string, codecs []av.CodecData) {
element.mutex.Lock()
defer element.mutex.Unlock()
t := element.Streams[uuid]
t.Codecs = codecs
element.Streams[uuid] = t
}
//HttpName func
func (element *ConfigST) HttpName() string {
element.mutex.Lock()
defer element.mutex.Unlock()
return element.Server.HTTPName
}
//HttpPort func
func (element *ConfigST) HttpPort() string {
element.mutex.Lock()
defer element.mutex.Unlock()
return element.Server.HTTPPort
}
//HttpsPort func
func (element *ConfigST) HttpsPort() string {
element.mutex.Lock()
defer element.mutex.Unlock()
return element.Server.HTTPSPort
}
//coGe get stream codec
func (element *ConfigST) coGe(uuid string) []av.CodecData {
for i := 0; i < 100; i++ {
element.mutex.RLock()
tmp, ok := element.Streams[uuid]
element.mutex.RUnlock()
if !ok {
return nil
}
if tmp.Codecs != nil {
return tmp.Codecs
}
//TODO add ctx wait codec data
time.Sleep(50 * time.Millisecond)
}
return nil
}
//list return all stream list
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
}
//NewHLSMuxer new muxer init
func (element *ConfigST) NewHLSMuxer(uuid string) {
element.mutex.Lock()
defer element.mutex.Unlock()
if tmp, ok := element.Streams[uuid]; ok {
tmp.HlsMuxer = NewHLSMuxer(uuid)
element.Streams[uuid] = tmp
}
}
//HlsMuxerSetFPS write packet
func (element *ConfigST) HlsMuxerSetFPS(uuid string, fps int) {
element.mutex.Lock()
defer element.mutex.Unlock()
if tmp, ok := element.Streams[uuid]; ok {
tmp.HlsMuxer.SetFPS(fps)
}
}
//HlsMuxerWritePacket write packet
func (element *ConfigST) HlsMuxerWritePacket(uuid string, packet *av.Packet) {
element.mutex.Lock()
defer element.mutex.Unlock()
if tmp, ok := element.Streams[uuid]; ok {
tmp.HlsMuxer.WritePacket(packet)
}
}
//HLSMuxerClose close muxer
func (element *ConfigST) HLSMuxerClose(uuid string) {
element.mutex.Lock()
defer element.mutex.Unlock()
if tmp, ok := element.Streams[uuid]; ok {
tmp.HlsMuxer.Close()
}
}
//HLSMuxerM3U8 get m3u8 list
func (element *ConfigST) HLSMuxerM3U8(uuid string, msn, part int) (string, error) {
element.mutex.Lock()
tmp, ok := element.Streams[uuid]
element.mutex.Unlock()
if ok {
index, err := tmp.HlsMuxer.GetIndexM3u8(msn, part)
return index, err
}
return "", ErrorStreamNotFound
}
//HLSMuxerSegment get segment
func (element *ConfigST) HLSMuxerSegment(uuid string, segment int) ([]*av.Packet, error) {
element.mutex.Lock()
defer element.mutex.Unlock()
if tmp, ok := element.Streams[uuid]; ok {
return tmp.HlsMuxer.GetSegment(segment)
}
return nil, ErrorStreamSegmentNotFound
}
//HLSMuxerFragment get fragment
func (element *ConfigST) HLSMuxerFragment(uuid string, segment, fragment int) ([]*av.Packet, error) {
element.mutex.Lock()
tmp, ok := element.Streams[uuid]
element.mutex.Unlock()
if ok {
packet, err := tmp.HlsMuxer.GetFragment(segment, fragment)
return packet, err
}
return nil, ErrorStreamFragmentNotFound
}