mirror of
https://github.com/Monibuca/plugin-hdl.git
synced 2025-10-27 18:41:13 +08:00
first commit
This commit is contained in:
180
main.go
180
main.go
@@ -2,101 +2,96 @@ package hdl
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"net"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
. "github.com/Monibuca/engine/v3"
|
||||
"github.com/Monibuca/utils/v3"
|
||||
"github.com/Monibuca/utils/v3/codec"
|
||||
. "github.com/Monibuca/engine/v4"
|
||||
"github.com/Monibuca/engine/v4/codec"
|
||||
"github.com/Monibuca/engine/v4/util"
|
||||
. "github.com/logrusorgru/aurora"
|
||||
amf "github.com/zhangpeihao/goamf"
|
||||
)
|
||||
|
||||
var config struct {
|
||||
ListenAddr string
|
||||
ListenAddrTLS string
|
||||
CertFile string
|
||||
KeyFile string
|
||||
Reconnect bool
|
||||
AutoPullList map[string]string
|
||||
type HDLConfig struct {
|
||||
HTTPConfig
|
||||
PublishConfig
|
||||
SubscribeConfig
|
||||
PullConfig
|
||||
context.Context
|
||||
context.CancelFunc
|
||||
}
|
||||
|
||||
var streamPathReg = regexp.MustCompile(`/(hdl/)?((.+)(\.flv)|(.+))`)
|
||||
var pconfig = PluginConfig{
|
||||
Name: "HDL",
|
||||
Config: &config,
|
||||
var config = &HDLConfig{
|
||||
PublishConfig: DefaultPublishConfig,
|
||||
SubscribeConfig: DefaultSubscribeConfig,
|
||||
}
|
||||
|
||||
func (config *HDLConfig) Update(override Config) {
|
||||
override.Unmarshal(config)
|
||||
needListen := false
|
||||
if config.CancelFunc == nil {
|
||||
needListen = config.ListenAddr != "" || config.ListenAddrTLS != ""
|
||||
if config.PullOnStart {
|
||||
for streamPath, url := range config.AutoPullList {
|
||||
if err := PullStream(streamPath, url); err != nil {
|
||||
util.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if override.Has("ListenAddr") || override.Has("ListenAddrTLS") {
|
||||
config.CancelFunc()
|
||||
needListen = config.ListenAddr != "" || config.ListenAddrTLS != ""
|
||||
}
|
||||
}
|
||||
config.Context, config.CancelFunc = context.WithCancel(Ctx)
|
||||
if needListen {
|
||||
util.Print(Green("HDL Listen at "), BrightBlue(config.ListenAddr), BrightBlue(config.ListenAddrTLS))
|
||||
config.Listen(config)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
pconfig.Install(run)
|
||||
if plugin := InstallPlugin(config); plugin != nil {
|
||||
plugin.HandleApi("/list", util.GetJsonHandler(getHDList, time.Second))
|
||||
plugin.HandleFunc("/pull", func(rw http.ResponseWriter, r *http.Request) {
|
||||
util.CORS(rw, r)
|
||||
targetURL := r.URL.Query().Get("target")
|
||||
streamPath := r.URL.Query().Get("streamPath")
|
||||
save := r.URL.Query().Get("save")
|
||||
if err := PullStream(streamPath, targetURL); err == nil {
|
||||
if save == "1" {
|
||||
if config.AutoPullList == nil {
|
||||
config.AutoPullList = make(map[string]string)
|
||||
}
|
||||
config.AutoPullList[streamPath] = targetURL
|
||||
if err = plugin.Save(); err != nil {
|
||||
util.Println(err)
|
||||
}
|
||||
}
|
||||
rw.WriteHeader(200)
|
||||
} else {
|
||||
rw.WriteHeader(500)
|
||||
}
|
||||
})
|
||||
plugin.HandleFunc("/", config.ServeHTTP)
|
||||
}
|
||||
}
|
||||
func getHDList() (info []*Stream) {
|
||||
for _, s := range Streams.ToList() {
|
||||
if _, ok := s.ExtraProp.(*HDLPuller); ok {
|
||||
if _, ok := s.Publisher.(*HDLPuller); ok {
|
||||
info = append(info, s)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
func run() {
|
||||
http.HandleFunc("/api/hdl/list", func(rw http.ResponseWriter, r *http.Request) {
|
||||
utils.CORS(rw, r)
|
||||
if r.URL.Query().Get("json") != "" {
|
||||
if jsonData, err := json.Marshal(getHDList()); err == nil {
|
||||
rw.Write(jsonData)
|
||||
} else {
|
||||
rw.WriteHeader(500)
|
||||
}
|
||||
return
|
||||
}
|
||||
sse := utils.NewSSE(rw, r.Context())
|
||||
var err error
|
||||
for tick := time.NewTicker(time.Second); err == nil; <-tick.C {
|
||||
err = sse.WriteJSON(getHDList())
|
||||
}
|
||||
})
|
||||
http.HandleFunc("/api/hdl/pull", func(rw http.ResponseWriter, r *http.Request) {
|
||||
utils.CORS(rw, r)
|
||||
targetURL := r.URL.Query().Get("target")
|
||||
streamPath := r.URL.Query().Get("streamPath")
|
||||
save := r.URL.Query().Get("save")
|
||||
if err := PullStream(streamPath, targetURL); err == nil {
|
||||
if save == "1" {
|
||||
if config.AutoPullList == nil {
|
||||
config.AutoPullList = make(map[string]string)
|
||||
}
|
||||
config.AutoPullList[streamPath] = targetURL
|
||||
if err = pconfig.Save(); err != nil {
|
||||
utils.Println(err)
|
||||
}
|
||||
}
|
||||
rw.WriteHeader(200)
|
||||
} else {
|
||||
rw.WriteHeader(500)
|
||||
}
|
||||
})
|
||||
if config.ListenAddr != "" || config.ListenAddrTLS != "" {
|
||||
utils.Print(Green("HDL start at "), BrightBlue(config.ListenAddr), BrightBlue(config.ListenAddrTLS))
|
||||
utils.ListenAddrs(config.ListenAddr, config.ListenAddrTLS, config.CertFile, config.KeyFile, http.HandlerFunc(HDLHandler))
|
||||
} else {
|
||||
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) {
|
||||
// if err := AuthHooks.Trigger(sign); err != nil {
|
||||
// w.WriteHeader(403)
|
||||
// return
|
||||
// }
|
||||
utils.CORS(w, r)
|
||||
func (config *HDLConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
parts := streamPathReg.FindStringSubmatch(r.RequestURI)
|
||||
if len(parts) == 0 {
|
||||
w.WriteHeader(404)
|
||||
@@ -108,9 +103,8 @@ func HDLHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
w.Header().Set("Transfer-Encoding", "chunked")
|
||||
w.Header().Set("Content-Type", "video/x-flv")
|
||||
w.Write(codec.FLVHeader)
|
||||
sub := Subscriber{ID: r.RemoteAddr, Type: "FLV", Ctx2: r.Context()}
|
||||
if err := sub.Subscribe(stringPath); err == nil {
|
||||
sub := Subscriber{ID: r.RemoteAddr, Type: "FLV"}
|
||||
if sub.Subscribe(stringPath, config.SubscribeConfig) {
|
||||
vt, at := sub.WaitVideoTrack(), sub.WaitAudioTrack()
|
||||
var buffer bytes.Buffer
|
||||
if _, err := amf.WriteString(&buffer, "onMetaData"); err != nil {
|
||||
@@ -128,32 +122,44 @@ func HDLHandler(w http.ResponseWriter, r *http.Request) {
|
||||
"videodatarate": 0,
|
||||
"filesize": 0,
|
||||
}
|
||||
if _, err := WriteEcmaArray(&buffer, metaData); err != nil {
|
||||
return
|
||||
}
|
||||
var flags byte
|
||||
if at != nil {
|
||||
flags |= (1 << 2)
|
||||
}
|
||||
if vt != nil {
|
||||
flags |= 1
|
||||
}
|
||||
w.Write([]byte{'F', 'L', 'V', 0x01, flags, 0, 0, 0, 9, 0, 0, 0, 0})
|
||||
codec.WriteFLVTag(w, codec.FLV_TAG_TYPE_SCRIPT, 0, net.Buffers{buffer.Bytes()})
|
||||
if vt != nil {
|
||||
metaData["videocodecid"] = int(vt.CodecID)
|
||||
metaData["width"] = vt.SPSInfo.Width
|
||||
metaData["height"] = vt.SPSInfo.Height
|
||||
codec.WriteFLVTag(w, codec.FLV_TAG_TYPE_VIDEO, 0, vt.ExtraData.Payload)
|
||||
sub.OnVideo = func(ts uint32, pack *VideoPack) {
|
||||
codec.WriteFLVTag(w, codec.FLV_TAG_TYPE_VIDEO, ts, pack.Payload)
|
||||
vt.DecoderConfiguration.FLV.WriteTo(w)
|
||||
sub.OnVideo = func(frame *VideoFrame) error {
|
||||
frame.FLV.WriteTo(w)
|
||||
return r.Context().Err()
|
||||
}
|
||||
}
|
||||
if at != nil {
|
||||
metaData["audiocodecid"] = int(at.CodecID)
|
||||
metaData["audiosamplerate"] = at.SoundRate
|
||||
metaData["audiosamplesize"] = int(at.SoundSize)
|
||||
metaData["audiosamplerate"] = at.SampleRate
|
||||
metaData["audiosamplesize"] = at.SampleSize
|
||||
metaData["stereo"] = at.Channels == 2
|
||||
if at.CodecID == 10 {
|
||||
codec.WriteFLVTag(w, codec.FLV_TAG_TYPE_AUDIO, 0, at.ExtraData)
|
||||
at.DecoderConfiguration.FLV.WriteTo(w)
|
||||
}
|
||||
sub.OnAudio = func(ts uint32, pack *AudioPack) {
|
||||
codec.WriteFLVTag(w, codec.FLV_TAG_TYPE_AUDIO, ts, pack.Payload)
|
||||
sub.OnAudio = func(frame *AudioFrame) error {
|
||||
frame.FLV.WriteTo(w)
|
||||
return r.Context().Err()
|
||||
}
|
||||
}
|
||||
if _, err := WriteEcmaArray(&buffer, metaData); err != nil {
|
||||
return
|
||||
}
|
||||
codec.WriteFLVTag(w, codec.FLV_TAG_TYPE_SCRIPT, 0, buffer.Bytes())
|
||||
sub.Play(at, vt)
|
||||
} else {
|
||||
w.WriteHeader(500)
|
||||
}
|
||||
}
|
||||
func WriteEcmaArray(w amf.Writer, o amf.Object) (n int, err error) {
|
||||
|
||||
Reference in New Issue
Block a user