mirror of https://bitbucket.org/ausocean/av.git
Simplify createOrUpdate method
Every time we want to create or update, let's just start a new revid pipeline. Now that our frequency of requests to vidforward to create or update are much lower (because of the new state machine) we can deal with the overhead of creating a new pipeline and reduce the complexity of the code.
This commit is contained in:
parent
66a1687316
commit
a75f83b4c5
|
@ -38,7 +38,6 @@ import (
|
|||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
@ -138,7 +137,7 @@ func terminationCallback(m *broadcastManager) func() {
|
|||
return
|
||||
}
|
||||
m.log.Info("successfully saved broadcast manager state on termination signal")
|
||||
logTrace(m.log.Debug,m.log.Warning)
|
||||
logTrace(m.log.Debug, m.log.Warning)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -443,55 +442,35 @@ func (m *broadcastManager) createOrUpdate(broadcast Broadcast) error {
|
|||
|
||||
// Try to get any old broadcasts for the provided MAC.
|
||||
maybeOld, ok := m.broadcasts[broadcast.mac]
|
||||
|
||||
// If there's no old broadcast, we need to create a new revid pipeline.
|
||||
if !ok {
|
||||
maybeOld = &broadcast
|
||||
if ok {
|
||||
if maybeOld.rv != nil {
|
||||
m.log.Debug("stopping old revid pipeline", "mac", broadcast.mac)
|
||||
closeDone := make(chan struct{})
|
||||
go func() { maybeOld.rv.Stop(); close(closeDone) }()
|
||||
select {
|
||||
case <-closeDone:
|
||||
m.log.Debug("stopped old revid pipeline", "mac", broadcast.mac)
|
||||
case <-time.After(5 * time.Second):
|
||||
m.log.Warning("could not stop old revid pipeline, looks like we'll end up with some leaked memory then :(", "mac", broadcast.mac)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m.log.Debug("attempting to start init or start pipeline")
|
||||
var err error
|
||||
maybeOld.rv, err = m.initOrStartPipeline(maybeOld.rv, broadcast.urls)
|
||||
maybeOld.rv, err = newRevid(m.log, broadcast.urls)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not get revid pipeline: %w", err)
|
||||
return fmt.Errorf("could not create new revid: %w", err)
|
||||
}
|
||||
m.log.Debug("finished attempting to init or start pipeline")
|
||||
maybeOld.rv.Start()
|
||||
|
||||
// If the URLS have changed, we need to update the revid pipeline.
|
||||
// We won't enter this if we've just created a new revid pipeline.
|
||||
if !reflect.DeepEqual(maybeOld.urls, broadcast.urls) {
|
||||
m.log.Debug("RTMP URLs have changed, updating pipeline config", "mac", broadcast.mac, "old", maybeOld.urls, "new", broadcast.urls)
|
||||
urls := strings.Join(broadcast.urls, ",")
|
||||
err := maybeOld.rv.Update(map[string]string{"RTMPURL": urls})
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not update revid: %w", err)
|
||||
}
|
||||
m.log.Debug("finished updating pipeline config")
|
||||
}
|
||||
|
||||
// If the status has changed, we need to update accordingly.
|
||||
// i.e. if the status is slate, we need to start writing the slate image.
|
||||
// We won't enter this if we've just created a new revid pipeline.
|
||||
if maybeOld.status != broadcast.status {
|
||||
m.log.Debug("status has changed, starting or stopping slate", "mac", broadcast.mac, "old", maybeOld.status, "new", broadcast.status)
|
||||
switch broadcast.status {
|
||||
case statusActive, statusPlay, statusCreate:
|
||||
m.log.Info("updating configuration for mac", "mac", broadcast.mac)
|
||||
signal, ok := m.slateExitSignals[broadcast.mac]
|
||||
if ok {
|
||||
close(signal)
|
||||
delete(m.slateExitSignals, broadcast.mac)
|
||||
}
|
||||
case statusSlate:
|
||||
m.log.Info("slate request")
|
||||
// If there's a signal channel it means that we're already writing the slate
|
||||
// image and theres nothing to do, so return.
|
||||
_, ok := m.slateExitSignals[broadcast.mac]
|
||||
if ok {
|
||||
m.log.Warning("already writing slate")
|
||||
return nil
|
||||
}
|
||||
|
||||
if broadcast.status == statusSlate {
|
||||
// First create a signal that can be used to stop the slate writing routine.
|
||||
// This will be provided to the writeSlate routine below.
|
||||
signalCh := make(chan struct{})
|
||||
|
@ -501,9 +480,6 @@ func (m *broadcastManager) createOrUpdate(broadcast Broadcast) error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("could not write slate and check for errors: %w", err)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("unknown status string: %s", broadcast.status)
|
||||
}
|
||||
}
|
||||
|
||||
// We need to give the revid pipeline to the new broadcast record.
|
||||
|
|
Loading…
Reference in New Issue