|
@@ -1,9 +1,7 @@
|
|
|
package daemon
|
|
|
|
|
|
import (
|
|
|
- "bytes"
|
|
|
"errors"
|
|
|
- "fmt"
|
|
|
"git.beejay.kim/WatchDog/ward/internal/download"
|
|
|
"git.beejay.kim/WatchDog/ward/model"
|
|
|
"git.beejay.kim/WatchDog/ward/platform"
|
|
@@ -11,35 +9,32 @@ import (
|
|
|
"github.com/rs/zerolog/log"
|
|
|
"github.com/urfave/cli/v2"
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
- "io"
|
|
|
- "os"
|
|
|
- "path/filepath"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
var action = func(ctx *cli.Context) error {
|
|
|
var (
|
|
|
- cfg = config.Get[Configuration](ctx)
|
|
|
- p platform.Platform
|
|
|
-
|
|
|
+ cfg = config.Get[Configuration](ctx)
|
|
|
+ p platform.Platform
|
|
|
downloader *download.HLS
|
|
|
- fOut *os.File
|
|
|
|
|
|
- exit = make(chan error)
|
|
|
chMessage = make(chan proto.Message, 256)
|
|
|
chStream chan []byte
|
|
|
err error
|
|
|
)
|
|
|
|
|
|
defer func() {
|
|
|
- if fOut != nil {
|
|
|
- _ = fOut.Close()
|
|
|
- }
|
|
|
+ for i := range players {
|
|
|
+ if players[i] == nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
|
|
|
- if chMessage != nil {
|
|
|
- close(chMessage)
|
|
|
+ _ = players[i].Close()
|
|
|
}
|
|
|
|
|
|
+ close(chMessage)
|
|
|
+ close(ctrlSignal)
|
|
|
+
|
|
|
if chStream != nil {
|
|
|
close(chStream)
|
|
|
}
|
|
@@ -53,26 +48,28 @@ var action = func(ctx *cli.Context) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- if fOut, err = os.OpenFile(
|
|
|
- filepath.Join(path, fmt.Sprintf("%s_%s.TS", bid, time.Now().String())),
|
|
|
- os.O_APPEND|os.O_WRONLY|os.O_CREATE,
|
|
|
- 0666); err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
go func() {
|
|
|
if err = p.Connect(chMessage); err != nil {
|
|
|
exit <- err
|
|
|
}
|
|
|
}()
|
|
|
|
|
|
- // usually HLS stream segments have 2s length;
|
|
|
- //
|
|
|
- if downloader, err = download.NewHLS(p.HLSStream, time.Second*2); err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
+ if len(players) > 0 {
|
|
|
+ if downloader, err = download.NewHLS(p.HLSStream, download.HLSOptions{
|
|
|
+ Timeout: time.Second * 10,
|
|
|
+ MaxAttempts: 9,
|
|
|
+ }); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
|
|
|
- chStream = downloader.Start()
|
|
|
+ for i := range players {
|
|
|
+ if err = players[i].Start(exit); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ chStream = downloader.Start()
|
|
|
+ }
|
|
|
|
|
|
for {
|
|
|
select {
|
|
@@ -80,17 +77,13 @@ var action = func(ctx *cli.Context) error {
|
|
|
return err
|
|
|
case buff := <-chStream:
|
|
|
if buff == nil {
|
|
|
- go func() {
|
|
|
- <-time.After(time.Second * 5)
|
|
|
- chStream = downloader.Start()
|
|
|
- }()
|
|
|
-
|
|
|
- log.Info().Msg("stream has been closed; we arranged next retry attempt")
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
- if _, err = io.Copy(fOut, bytes.NewBuffer(buff)); err != nil {
|
|
|
- return err
|
|
|
+ for i := range players {
|
|
|
+ if err = players[i].Write(buff); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
}
|
|
|
case m := <-chMessage:
|
|
|
switch t := m.(type) {
|