添加自动拉流功能

This commit is contained in:
dexter
2021-12-26 15:59:08 +08:00
parent 9d98f5b8dd
commit 7b96cf8d8e
2 changed files with 58 additions and 1 deletions

View File

@@ -18,8 +18,9 @@ var config struct {
ListenAddrTLS string
CertFile string
KeyFile string
AutoPullList map[string]string
}
var streamPathReg = regexp.MustCompile("/(hdl/)?((.+)(\\.flv)|(.+))")
var streamPathReg = regexp.MustCompile(`/(hdl/)?((.+)(\.flv)|(.+))`)
func init() {
InstallPlugin(&PluginConfig{
@@ -37,6 +38,11 @@ func run() {
utils.Print(Green("HDL start reuse gateway port"))
http.HandleFunc("/hdl/", HDLHandler)
}
for streamPath, url := range config.AutoPullList {
if err := PullStream(streamPath, url); err != nil {
utils.Println(err)
}
}
}
func HDLHandler(w http.ResponseWriter, r *http.Request) {

51
pull.go Normal file
View File

@@ -0,0 +1,51 @@
package hdl
import (
"errors"
"io"
"net/http"
"time"
. "github.com/Monibuca/engine/v3"
"github.com/Monibuca/utils/v3/codec"
)
func PullStream(streamPath, url string) error {
if res, err := http.Get(url); err == nil {
stream := Stream{
Type: "HDL Pull",
StreamPath: streamPath,
}
if stream.Publish() {
defer stream.Close()
head := make([]byte, len(codec.FLVHeader))
io.ReadFull(res.Body, head)
var lastTime uint32
at := stream.NewAudioTrack(0)
vt := stream.NewVideoTrack(0)
for {
if t, timestamp, payload, err := codec.ReadFLVTag(res.Body); err == nil {
switch t {
case codec.FLV_TAG_TYPE_AUDIO:
at.PushByteStream(timestamp, payload)
case codec.FLV_TAG_TYPE_VIDEO:
if timestamp != 0 {
if lastTime == 0 {
lastTime = timestamp
}
}
vt.PushByteStream(timestamp, payload)
time.Sleep(time.Duration(timestamp-lastTime) * time.Millisecond)
lastTime = timestamp
}
} else {
return err
}
}
} else {
return errors.New("Bad Name")
}
} else {
return err
}
}