mirror of
https://github.com/Monibuca/plugin-ts.git
synced 2025-10-26 17:01:14 +08:00
添加了UI界面
This commit is contained in:
106
main.go
106
main.go
@@ -2,7 +2,13 @@ package tsplugin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
. "github.com/Monibuca/engine"
|
||||
@@ -11,17 +17,45 @@ import (
|
||||
"github.com/Monibuca/engine/util"
|
||||
)
|
||||
|
||||
func init() {
|
||||
var config = struct {
|
||||
BufferLength int
|
||||
Path string
|
||||
AutoPublish bool
|
||||
}{2048, "ts", true}
|
||||
|
||||
func init() {
|
||||
_, currentFilePath, _, _ := runtime.Caller(0)
|
||||
InstallPlugin(&PluginConfig{
|
||||
Name: "TS",
|
||||
Type: PLUGIN_PUBLISHER,
|
||||
Version: "1.0.1",
|
||||
UI: filepath.Join(filepath.Dir(currentFilePath), "dashboard", "ui", "plugin-ts.min.js"),
|
||||
Config: &config,
|
||||
Run: func() {
|
||||
if config.AutoPublish {
|
||||
OnSubscribeHooks.AddHook(func(s *OutputStream) {
|
||||
if s.Publisher == nil {
|
||||
new(TS).PublishDir(s.StreamPath)
|
||||
}
|
||||
})
|
||||
}
|
||||
http.HandleFunc("/ts/list", listTsDir)
|
||||
http.HandleFunc("/ts/publish", publishTsDir)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type TSDir struct {
|
||||
StreamPath string
|
||||
TsCount int
|
||||
TotalSize int64
|
||||
}
|
||||
type TS struct {
|
||||
InputStream
|
||||
*mpegts.MpegTsStream
|
||||
TSInfo
|
||||
//TsChan chan io.Reader
|
||||
lastDts uint64
|
||||
parent Publisher
|
||||
}
|
||||
type TSInfo struct {
|
||||
TotalPesCount int
|
||||
@@ -165,19 +199,69 @@ func (ts *TS) run() {
|
||||
}
|
||||
}
|
||||
}
|
||||
func (ts *TS) OnClosed() {
|
||||
if ts.parent != nil {
|
||||
ts.parent.OnClosed()
|
||||
}
|
||||
}
|
||||
func (ts *TS) Publish(streamPath string, publisher Publisher) (result bool) {
|
||||
if publisher != ts {
|
||||
ts.parent = publisher
|
||||
}
|
||||
if result = ts.InputStream.Publish(streamPath, publisher); result {
|
||||
ts.TSInfo.RoomInfo = &ts.Room.RoomInfo
|
||||
ts.MpegTsStream = mpegts.NewMpegTsStream(2048)
|
||||
ts.MpegTsStream = mpegts.NewMpegTsStream(config.BufferLength)
|
||||
go ts.run()
|
||||
}
|
||||
return
|
||||
}
|
||||
func (ts *TS) PublishDir(streamPath string) {
|
||||
dirPath := filepath.Join(config.Path, streamPath)
|
||||
files, err := ioutil.ReadDir(dirPath)
|
||||
if err != nil || len(files) == 0 {
|
||||
return
|
||||
}
|
||||
if ts.InputStream.Publish(streamPath, ts) {
|
||||
ts.TSInfo.RoomInfo = &ts.Room.RoomInfo
|
||||
ts.MpegTsStream = mpegts.NewMpegTsStream(0)
|
||||
go ts.run()
|
||||
for _, file := range files {
|
||||
fullPath := filepath.Join(dirPath, file.Name())
|
||||
if filepath.Ext(fullPath) == ".ts" {
|
||||
if data, err := os.Open(fullPath); err == nil {
|
||||
ts.Feed(data)
|
||||
data.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
ts.Close()
|
||||
}
|
||||
}
|
||||
func publishTsDir(w http.ResponseWriter, r *http.Request) {
|
||||
streamPath := r.URL.Query().Get("streamPath")
|
||||
go new(TS).PublishDir(streamPath)
|
||||
}
|
||||
func readTsDir(currentDir string) []*TSDir {
|
||||
var list []*TSDir
|
||||
abDir := filepath.Join(config.Path, currentDir)
|
||||
if items, err := ioutil.ReadDir(abDir); err == nil {
|
||||
tscount := 0
|
||||
var totalSize int64
|
||||
for _, file := range items {
|
||||
if file.IsDir() {
|
||||
list = append(list, readTsDir(filepath.Join(currentDir, file.Name()))...)
|
||||
} else if filepath.Ext(filepath.Join(abDir, file.Name())) == ".ts" {
|
||||
tscount++
|
||||
totalSize = totalSize + file.Size()
|
||||
}
|
||||
}
|
||||
if tscount > 0 {
|
||||
info := TSDir{
|
||||
currentDir, tscount, totalSize,
|
||||
}
|
||||
list = append(list, &info)
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
func listTsDir(w http.ResponseWriter, r *http.Request) {
|
||||
var list []*TSDir = readTsDir(".")
|
||||
bytes, err := json.Marshal(list)
|
||||
if err == nil {
|
||||
w.Write(bytes)
|
||||
} else {
|
||||
w.Write([]byte("{\"code\":1}"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user