1 Star 0 Fork 0

t6685367 / videostream

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
config.go 3.50 KB
一键复制 编辑 原始数据 按行查看 历史
t6685367 提交于 2023-01-18 07:16 . update config.go.
package videostream
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 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)
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 {
num := 0
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 {
if num < 3 {
time.Sleep(50 * time.Millisecond)
num++
continue
}
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
}
Go
1
https://gitee.com/t6685367/videostream.git
git@gitee.com:t6685367/videostream.git
t6685367
videostream
videostream
372ee6b8a63a

搜索帮助