mirror of
https://github.com/Monibuca/plugin-hdl.git
synced 2025-10-05 16:56:55 +08:00
优化代码
This commit is contained in:
30
main.go
30
main.go
@@ -27,7 +27,7 @@ var streamPathReg = regexp.MustCompile(`/(hdl/)?((.+)(\.flv)|(.+))`)
|
|||||||
|
|
||||||
func (c *HDLConfig) Update(override config.Config) {
|
func (c *HDLConfig) Update(override config.Config) {
|
||||||
if c.ListenAddr != "" || c.ListenAddrTLS != "" {
|
if c.ListenAddr != "" || c.ListenAddrTLS != "" {
|
||||||
plugin.Infoln(Green("HDL Listen at "), BrightBlue(c.ListenAddr), BrightBlue(c.ListenAddrTLS))
|
plugin.Info(Green("HDL Listen at "), BrightBlue(c.ListenAddr), BrightBlue(c.ListenAddrTLS))
|
||||||
c.Listen(plugin, c)
|
c.Listen(plugin, c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -39,7 +39,7 @@ func (c *HDLConfig) API_Pull(rw http.ResponseWriter, r *http.Request) {
|
|||||||
c.AddPull(streamPath, targetURL)
|
c.AddPull(streamPath, targetURL)
|
||||||
plugin.Modified["pull"] = c.Pull
|
plugin.Modified["pull"] = c.Pull
|
||||||
if err := plugin.Save(); err != nil {
|
if err := plugin.Save(); err != nil {
|
||||||
plugin.Errorln(err)
|
plugin.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,14 +66,16 @@ func (*HDLConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
sub := Subscriber{ID: r.RemoteAddr, Type: "FLV"}
|
sub := Subscriber{ID: r.RemoteAddr, Type: "FLV"}
|
||||||
if sub.Subscribe(stringPath, Config.Subscribe) {
|
if sub.Subscribe(stringPath, Config.Subscribe) {
|
||||||
vt, at := sub.WaitVideoTrack(), sub.WaitAudioTrack()
|
vt, at := sub.WaitVideoTrack(), sub.WaitAudioTrack()
|
||||||
|
hasVideo := vt != nil
|
||||||
|
hasAudio := at != nil
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
if _, err := amf.WriteString(&buffer, "onMetaData"); err != nil {
|
if _, err := amf.WriteString(&buffer, "onMetaData"); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
metaData := amf.Object{
|
metaData := amf.Object{
|
||||||
"MetaDataCreator": "m7s" + Engine.Version,
|
"MetaDataCreator": "m7s" + Engine.Version,
|
||||||
"hasVideo": vt != nil,
|
"hasVideo": hasVideo,
|
||||||
"hasAudio": at != nil,
|
"hasAudio": hasAudio,
|
||||||
"hasMatadata": true,
|
"hasMatadata": true,
|
||||||
"canSeekToEnd": false,
|
"canSeekToEnd": false,
|
||||||
"duration": 0,
|
"duration": 0,
|
||||||
@@ -86,37 +88,39 @@ func (*HDLConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var flags byte
|
var flags byte
|
||||||
if at != nil {
|
if hasAudio {
|
||||||
flags |= (1 << 2)
|
flags |= (1 << 2)
|
||||||
}
|
}
|
||||||
if vt != nil {
|
if hasVideo {
|
||||||
flags |= 1
|
flags |= 1
|
||||||
}
|
}
|
||||||
w.Write([]byte{'F', 'L', 'V', 0x01, flags, 0, 0, 0, 9, 0, 0, 0, 0})
|
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 hasVideo {
|
||||||
if vt != nil {
|
|
||||||
metaData["videocodecid"] = int(vt.CodecID)
|
metaData["videocodecid"] = int(vt.CodecID)
|
||||||
metaData["width"] = vt.SPSInfo.Width
|
metaData["width"] = vt.SPSInfo.Width
|
||||||
metaData["height"] = vt.SPSInfo.Height
|
metaData["height"] = vt.SPSInfo.Height
|
||||||
vt.DecoderConfiguration.FLV.WriteTo(w)
|
|
||||||
sub.OnVideo = func(frame *VideoFrame) error {
|
sub.OnVideo = func(frame *VideoFrame) error {
|
||||||
frame.FLV.WriteTo(w)
|
frame.FLV.WriteTo(w)
|
||||||
return r.Context().Err()
|
return r.Context().Err()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if at != nil {
|
if hasVideo {
|
||||||
metaData["audiocodecid"] = int(at.CodecID)
|
metaData["audiocodecid"] = int(at.CodecID)
|
||||||
metaData["audiosamplerate"] = at.SampleRate
|
metaData["audiosamplerate"] = at.SampleRate
|
||||||
metaData["audiosamplesize"] = at.SampleSize
|
metaData["audiosamplesize"] = at.SampleSize
|
||||||
metaData["stereo"] = at.Channels == 2
|
metaData["stereo"] = at.Channels == 2
|
||||||
if at.CodecID == 10 {
|
|
||||||
at.DecoderConfiguration.FLV.WriteTo(w)
|
|
||||||
}
|
|
||||||
sub.OnAudio = func(frame *AudioFrame) error {
|
sub.OnAudio = func(frame *AudioFrame) error {
|
||||||
frame.FLV.WriteTo(w)
|
frame.FLV.WriteTo(w)
|
||||||
return r.Context().Err()
|
return r.Context().Err()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
codec.WriteFLVTag(w, codec.FLV_TAG_TYPE_SCRIPT, 0, net.Buffers{buffer.Bytes()})
|
||||||
|
if hasVideo {
|
||||||
|
vt.DecoderConfiguration.FLV.WriteTo(w)
|
||||||
|
}
|
||||||
|
if hasAudio && at.CodecID == codec.CodecID_AAC {
|
||||||
|
at.DecoderConfiguration.FLV.WriteTo(w)
|
||||||
|
}
|
||||||
sub.Play(at, vt)
|
sub.Play(at, vt)
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
|
18
pull.go
18
pull.go
@@ -66,10 +66,9 @@ type HDLPuller struct {
|
|||||||
at *track.UnknowAudio
|
at *track.UnknowAudio
|
||||||
vt *track.UnknowVideo
|
vt *track.UnknowVideo
|
||||||
}
|
}
|
||||||
type FLVFile HDLPuller
|
// 用于发布FLV文件
|
||||||
|
type FLVFile struct {
|
||||||
func (puller *FLVFile) pull() {
|
HDLPuller
|
||||||
(*HDLPuller)(puller).pull()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (puller *FLVFile) Pull(count int) {
|
func (puller *FLVFile) Pull(count int) {
|
||||||
@@ -95,14 +94,17 @@ func (puller *HDLPuller) Pull(count int) {
|
|||||||
if res, err := http.Get(puller.RemoteURL); err == nil {
|
if res, err := http.Get(puller.RemoteURL); err == nil {
|
||||||
puller.Reader = res.Body
|
puller.Reader = res.Body
|
||||||
puller.Closer = res.Body
|
puller.Closer = res.Body
|
||||||
|
} else {
|
||||||
|
puller.Error(err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
puller.pull()
|
puller.pull()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *HDLConfig) PullStream(streamPath string, puller Puller) bool {
|
func (config *HDLConfig) PullStream(streamPath string, puller Puller) bool {
|
||||||
if strings.HasPrefix(puller.RemoteURL, "http") {
|
var puber IPublisher = &HDLPuller{Puller: puller}
|
||||||
return puller.Publish(streamPath, &HDLPuller{Puller: puller}, Config.Publish)
|
if !strings.HasPrefix(puller.RemoteURL, "http") {
|
||||||
} else {
|
puber = &FLVFile{HDLPuller: *puber.(*HDLPuller)}
|
||||||
return puller.Publish(streamPath, &FLVFile{Puller: puller}, Config.Publish)
|
|
||||||
}
|
}
|
||||||
|
return puber.Publish(streamPath, puber, Config.Publish)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user