Refactoring #3
+23
-22
@@ -342,30 +342,31 @@ func main() {
|
|||||||
videoActive = videoStr != ""
|
videoActive = videoStr != ""
|
||||||
audioActive = audioStr != ""
|
audioActive = audioStr != ""
|
||||||
|
|
||||||
if videoStr == "" {
|
videoConfig := playback.FeedConfig{}
|
||||||
enqueueCommand(playback.SessionCommand{Kind: playback.CommandRemoveVideo})
|
if videoActive {
|
||||||
} else {
|
videoConfig = playback.FeedConfig{
|
||||||
enqueueCommand(playback.SessionCommand{
|
Domain: videoDomainStr,
|
||||||
Kind: playback.CommandSetVideo,
|
UUID: videoStr,
|
||||||
Config: playback.FeedConfig{
|
Active: true,
|
||||||
Domain: videoDomainStr,
|
}
|
||||||
UUID: videoStr,
|
|
||||||
Active: true,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
if audioStr == "" {
|
audioConfig := playback.FeedConfig{}
|
||||||
enqueueCommand(playback.SessionCommand{Kind: playback.CommandRemoveAudio})
|
if audioActive {
|
||||||
} else {
|
audioConfig = playback.FeedConfig{
|
||||||
enqueueCommand(playback.SessionCommand{
|
Domain: audioDomainStr,
|
||||||
Kind: playback.CommandSetAudio,
|
UUID: audioStr,
|
||||||
Config: playback.FeedConfig{
|
Active: true,
|
||||||
Domain: audioDomainStr,
|
}
|
||||||
UUID: audioStr,
|
|
||||||
Active: true,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enqueueCommand(playback.SessionCommand{
|
||||||
|
Kind: playback.CommandSetSession,
|
||||||
|
Session: playback.SessionConfig{
|
||||||
|
Video: videoConfig,
|
||||||
|
Audio: audioConfig,
|
||||||
|
SyncRequested: syncRequested,
|
||||||
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
playbackDone := make(chan error, 1)
|
playbackDone := make(chan error, 1)
|
||||||
|
|||||||
@@ -8,12 +8,19 @@ import (
|
|||||||
"mxl-player/internal/playback"
|
"mxl-player/internal/playback"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type playerAudioSink interface {
|
||||||
|
playback.AudioSink
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ playerAudioSink = (*output.SDLAudioSink)(nil)
|
||||||
|
|
||||||
type playerPlayback struct {
|
type playerPlayback struct {
|
||||||
Controller *playback.SessionController
|
Controller *playback.SessionController
|
||||||
Commands chan playback.SessionCommand
|
Commands chan playback.SessionCommand
|
||||||
Video *playback.VideoBridge
|
Video *playback.VideoBridge
|
||||||
Status *playback.StatusStore
|
Status *playback.StatusStore
|
||||||
Audio *output.SDLAudioSink
|
Audio playerAudioSink
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPlayerPlayback(
|
func newPlayerPlayback(
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ Size=200,200
|
|||||||
Collapsed=0
|
Collapsed=0
|
||||||
|
|
||||||
[Window][Connection]
|
[Window][Connection]
|
||||||
Pos=322,387
|
Pos=322,130
|
||||||
Size=618,275
|
Size=661,444
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
|
|
||||||
|
|||||||
@@ -20,11 +20,13 @@ const (
|
|||||||
CommandRemoveAudio
|
CommandRemoveAudio
|
||||||
CommandEnableSync
|
CommandEnableSync
|
||||||
CommandDisableSync
|
CommandDisableSync
|
||||||
|
CommandSetSession
|
||||||
)
|
)
|
||||||
|
|
||||||
type SessionCommand struct {
|
type SessionCommand struct {
|
||||||
Kind SessionCommandKind
|
Kind SessionCommandKind
|
||||||
Config FeedConfig // Used only by SetVideo and SetAudio.
|
Config FeedConfig // Used only by SetVideo and SetAudio.
|
||||||
|
Session SessionConfig // Used only by SetSession.
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -99,6 +101,12 @@ func ApplySessionCommand(
|
|||||||
case CommandDisableSync:
|
case CommandDisableSync:
|
||||||
next.SyncRequested = false
|
next.SyncRequested = false
|
||||||
|
|
||||||
|
case CommandSetSession:
|
||||||
|
next = command.Session
|
||||||
|
// Retry policy belongs to the running controller configuration, not to
|
||||||
|
// GUI or playlist session selections.
|
||||||
|
next.Retry = current.Retry
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return current, ErrUnknownSessionCommand
|
return current, ErrUnknownSessionCommand
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,6 +114,27 @@ func TestApplySessionCommand(t *testing.T) {
|
|||||||
command: SessionCommand{Kind: CommandDisableSync},
|
command: SessionCommand{Kind: CommandDisableSync},
|
||||||
want: func() SessionConfig { c := base; c.SyncRequested = false; return c }(),
|
want: func() SessionConfig { c := base; c.SyncRequested = false; return c }(),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "set complete session atomically and preserve retry",
|
||||||
|
current: base,
|
||||||
|
command: SessionCommand{
|
||||||
|
Kind: CommandSetSession,
|
||||||
|
Session: SessionConfig{
|
||||||
|
Video: newVideo,
|
||||||
|
Audio: newAudio,
|
||||||
|
SyncRequested: false,
|
||||||
|
Retry: RetryPolicy{
|
||||||
|
MaxAttempts: 99,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: SessionConfig{
|
||||||
|
Video: newVideo,
|
||||||
|
Audio: newAudio,
|
||||||
|
SyncRequested: false,
|
||||||
|
Retry: base.Retry,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@@ -157,6 +178,17 @@ func TestApplySessionCommandFailurePreservesState(t *testing.T) {
|
|||||||
ErrAudioNotConfigured,
|
ErrAudioNotConfigured,
|
||||||
},
|
},
|
||||||
{"unknown command", base, SessionCommand{Kind: 255}, ErrUnknownSessionCommand},
|
{"unknown command", base, SessionCommand{Kind: 255}, ErrUnknownSessionCommand},
|
||||||
|
{
|
||||||
|
"invalid complete session",
|
||||||
|
base,
|
||||||
|
SessionCommand{
|
||||||
|
Kind: CommandSetSession,
|
||||||
|
Session: SessionConfig{
|
||||||
|
Video: FeedConfig{UUID: "video", Active: true},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ErrFeedDomainRequired,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|||||||
@@ -240,6 +240,39 @@ func TestSessionControllerUpdatesOnlyChangedIndependentSlot(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSessionControllerReplacesSynchronizedPairWithOneCommand(t *testing.T) {
|
||||||
|
events := make(chan controllerEvent, 32)
|
||||||
|
controller := newRecordingController(t, events)
|
||||||
|
initial := validCommandSession()
|
||||||
|
commands := make(chan SessionCommand)
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() { done <- controller.Run(context.Background(), initial, commands) }()
|
||||||
|
start := receiveControllerEvent(t, events)
|
||||||
|
if start.unit != UnitSync || start.action != "start" {
|
||||||
|
t.Fatalf("initial event = %+v, want sync start", start)
|
||||||
|
}
|
||||||
|
|
||||||
|
next := initial
|
||||||
|
next.Video = FeedConfig{Domain: "/next", UUID: "next-video", Active: true}
|
||||||
|
next.Audio = FeedConfig{Domain: "/next", UUID: "next-audio", Active: true}
|
||||||
|
commands <- SessionCommand{Kind: CommandSetSession, Session: next}
|
||||||
|
event := receiveControllerEvent(t, events)
|
||||||
|
wantPair := SyncPairConfig{Video: next.Video, Audio: next.Audio}
|
||||||
|
if event.unit != UnitSync || event.action != "command" || event.pair != wantPair {
|
||||||
|
t.Fatalf("replacement event = %+v, want one sync command for %#v", event, wantPair)
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case event := <-events:
|
||||||
|
t.Fatalf("atomic replacement emitted an extra event: %+v", event)
|
||||||
|
case <-time.After(20 * time.Millisecond):
|
||||||
|
}
|
||||||
|
|
||||||
|
close(commands)
|
||||||
|
if err := <-done; err != nil {
|
||||||
|
t.Fatalf("Run() error = %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSessionControllerStopsIndependentSlotsBeforeStartingSync(t *testing.T) {
|
func TestSessionControllerStopsIndependentSlotsBeforeStartingSync(t *testing.T) {
|
||||||
events := make(chan controllerEvent, 32)
|
events := make(chan controllerEvent, 32)
|
||||||
controller := newRecordingController(t, events)
|
controller := newRecordingController(t, events)
|
||||||
|
|||||||
Reference in New Issue
Block a user