initial commit
This commit is contained in:
commit
6db0ec3a77
6 changed files with 279 additions and 0 deletions
77
store/store.go
Normal file
77
store/store.go
Normal file
|
@ -0,0 +1,77 @@
|
|||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"ffmpeg-playout/playout"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
Playouts map[uuid.UUID]*playout.Job
|
||||
DefaultDuration time.Duration
|
||||
Outputs []string
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func NewStore(o []string, defaultDuration string) (*Store, error) {
|
||||
playouts := make(map[uuid.UUID]*playout.Job)
|
||||
|
||||
var d time.Duration
|
||||
var err error
|
||||
if d, err = time.ParseDuration(defaultDuration); err != nil {
|
||||
log.Fatal("Failed to set Default Duration: ", err)
|
||||
}
|
||||
|
||||
store := &Store{Playouts: playouts, DefaultDuration: d, Outputs: o}
|
||||
|
||||
return store, nil
|
||||
}
|
||||
|
||||
func (s *Store) AddPlayout(p *playout.Job) (uuid.UUID, error) {
|
||||
outputs := s.Outputs
|
||||
for _, value := range s.Playouts {
|
||||
if len(outputs) != 0 &&
|
||||
(value.StartAt.After(p.StartAt) && value.StartAt.Before(p.StopAt) ||
|
||||
value.StopAt.After(p.StartAt) && value.StopAt.Before(p.StopAt) ||
|
||||
p.StartAt.After(value.StartAt) && p.StartAt.Before(value.StopAt) ||
|
||||
p.StopAt.After(value.StartAt) && p.StopAt.Before(value.StopAt)) {
|
||||
inSlice, index := stringInSlice(value.Output, outputs)
|
||||
if inSlice {
|
||||
outputs = remove(outputs, index)
|
||||
}
|
||||
}
|
||||
}
|
||||
var output string
|
||||
if len(outputs) != 0 {
|
||||
output = outputs[0]
|
||||
} else {
|
||||
return uuid.Nil, errors.New("no output available")
|
||||
}
|
||||
uid, err := uuid.NewUUID()
|
||||
if err != nil {
|
||||
return uuid.Nil, fmt.Errorf("couldn't generate uuid: %s", err.Error())
|
||||
}
|
||||
p.Output = output
|
||||
s.Playouts[uid] = p
|
||||
return uid, nil
|
||||
}
|
||||
|
||||
//https://stackoverflow.com/a/15323988/10997297
|
||||
func stringInSlice(a string, list []string) (bool, int) {
|
||||
for index, b := range list {
|
||||
if b == a {
|
||||
return true, index
|
||||
}
|
||||
}
|
||||
return false, 0
|
||||
}
|
||||
|
||||
func remove(s []string, i int) []string {
|
||||
s[i] = s[len(s)-1]
|
||||
// We do not need to put s[i] at the end, as it will be discarded anyway
|
||||
return s[:len(s)-1]
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue