代码拉取完成,页面将自动刷新
package main
import (
"errors"
"log"
"time"
"github.com/deepch/vdk/format/rtspv2"
)
var (
ErrorStreamExitNoVideoOnStream = errors.New("Stream Exit No Video On Stream")
ErrorStreamExitRtspDisconnect = errors.New("Stream Exit Rtsp Disconnect")
ErrorStreamExitNoViewer = errors.New("Stream Exit On Demand No Viewer")
)
func serveStreams() {
for k, v := range Config.Streams {
if !v.OnDemand {
go RTSPWorkerLoop(k, v.URL, v.OnDemand, v.DisableAudio, v.Debug)
}
}
}
func RTSPWorkerLoop(name, url string, OnDemand, DisableAudio, Debug bool) {
defer Config.RunUnlock(name)
for {
log.Println("Stream Try Connect", name)
err := RTSPWorker(name, url, OnDemand, DisableAudio, Debug)
if err != nil {
log.Println(err)
Config.LastError = err
}
if OnDemand && !Config.HasViewer(name) {
log.Println(ErrorStreamExitNoViewer)
return
}
time.Sleep(1 * time.Second)
}
}
func RTSPWorker(name, url string, OnDemand, DisableAudio, Debug bool) error {
keyTest := time.NewTimer(20 * time.Second)
clientTest := time.NewTimer(20 * time.Second)
//add next TimeOut
RTSPClient, err := rtspv2.Dial(rtspv2.RTSPClientOptions{URL: url, DisableAudio: DisableAudio, DialTimeout: 3 * time.Second, ReadWriteTimeout: 3 * time.Second, Debug: Debug})
if err != nil {
return err
}
defer RTSPClient.Close()
if RTSPClient.CodecData != nil {
Config.coAd(name, RTSPClient.CodecData)
}
var AudioOnly bool
if len(RTSPClient.CodecData) == 1 && RTSPClient.CodecData[0].Type().IsAudio() {
AudioOnly = true
}
for {
select {
case <-clientTest.C:
if OnDemand {
if !Config.HasViewer(name) {
return ErrorStreamExitNoViewer
} else {
clientTest.Reset(20 * time.Second)
}
}
case <-keyTest.C:
return ErrorStreamExitNoVideoOnStream
case signals := <-RTSPClient.Signals:
switch signals {
case rtspv2.SignalCodecUpdate:
Config.coAd(name, RTSPClient.CodecData)
case rtspv2.SignalStreamRTPStop:
return ErrorStreamExitRtspDisconnect
}
case packetAV := <-RTSPClient.OutgoingPacketQueue:
if AudioOnly || packetAV.IsKeyFrame {
keyTest.Reset(20 * time.Second)
}
Config.cast(name, *packetAV)
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。