Skip to content

Commit

Permalink
feat: support set_options command
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Feb 9, 2024
1 parent 84342c8 commit 4b81804
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 64 deletions.
86 changes: 42 additions & 44 deletions cmd/daemon/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,61 +179,59 @@ func (p *AppPlayer) loadCurrentTrack(paused bool) error {
return nil
}

func (p *AppPlayer) setRepeatingContext(val bool) {
if val == p.state.player.Options.RepeatingContext {
return
func (p *AppPlayer) setOptions(repeatingContext *bool, repeatingTrack *bool, shufflingContext *bool) {
var requiresUpdate bool
if repeatingContext != nil && *repeatingContext != p.state.player.Options.RepeatingContext {
p.state.player.Options.RepeatingContext = *repeatingContext

p.app.server.Emit(&ApiEvent{
Type: ApiEventTypeRepeatContext,
Data: ApiEventDataRepeatContext{
Value: *repeatingContext,
},
})

requiresUpdate = true
}

p.state.player.Options.RepeatingContext = val
p.updateState()
if repeatingTrack != nil && *repeatingTrack != p.state.player.Options.RepeatingTrack {
p.state.player.Options.RepeatingTrack = *repeatingTrack

p.app.server.Emit(&ApiEvent{
Type: ApiEventTypeRepeatContext,
Data: ApiEventDataRepeatContext{
Value: val,
},
})
}
p.app.server.Emit(&ApiEvent{
Type: ApiEventTypeRepeatTrack,
Data: ApiEventDataRepeatTrack{
Value: *repeatingTrack,
},
})

func (p *AppPlayer) setRepeatingTrack(val bool) {
if val == p.state.player.Options.RepeatingTrack {
return
requiresUpdate = true
}

p.state.player.Options.RepeatingTrack = val
p.updateState()
if shufflingContext != nil && *shufflingContext != p.state.player.Options.ShufflingContext {
if err := p.state.tracks.ToggleShuffle(*shufflingContext); err != nil {
log.WithError(err).Errorf("failed toggling shuffle context (value: %t)", *shufflingContext)
return
}

p.app.server.Emit(&ApiEvent{
Type: ApiEventTypeRepeatTrack,
Data: ApiEventDataRepeatTrack{
Value: val,
},
})
}
p.state.player.Options.ShufflingContext = *shufflingContext
p.state.player.Track = p.state.tracks.CurrentTrack()
p.state.player.PrevTracks = p.state.tracks.PrevTracks()
p.state.player.NextTracks = p.state.tracks.NextTracks()
p.state.player.Index = p.state.tracks.Index()

func (p *AppPlayer) setShufflingContext(val bool) {
if val == p.state.player.Options.ShufflingContext {
return
}
p.app.server.Emit(&ApiEvent{
Type: ApiEventTypeShuffleContext,
Data: ApiEventDataShuffleContext{
Value: *shufflingContext,
},
})

if err := p.state.tracks.ToggleShuffle(val); err != nil {
log.WithError(err).Errorf("failed toggling shuffle context (value: %t)", val)
return
requiresUpdate = true
}

p.state.player.Options.ShufflingContext = val
p.state.player.Track = p.state.tracks.CurrentTrack()
p.state.player.PrevTracks = p.state.tracks.PrevTracks()
p.state.player.NextTracks = p.state.tracks.NextTracks()
p.state.player.Index = p.state.tracks.Index()
p.updateState()

p.app.server.Emit(&ApiEvent{
Type: ApiEventTypeShuffleContext,
Data: ApiEventDataShuffleContext{
Value: val,
},
})
if requiresUpdate {
p.updateState()
}
}

func (p *AppPlayer) addToQueue(track *connectpb.ContextTrack) {
Expand Down
21 changes: 15 additions & 6 deletions cmd/daemon/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,19 @@ func (p *AppPlayer) handlePlayerCommand(req dealer.RequestPayload) error {
p.updateState()
return nil
case "set_repeating_context":
p.setRepeatingContext(req.Command.Value.(bool))
val := req.Command.Value.(bool)
p.setOptions(&val, nil, nil)
return nil
case "set_repeating_track":
p.setRepeatingTrack(req.Command.Value.(bool))
val := req.Command.Value.(bool)
p.setOptions(nil, &val, nil)
return nil
case "set_shuffling_context":
p.setShufflingContext(req.Command.Value.(bool))
val := req.Command.Value.(bool)
p.setOptions(nil, nil, &val)
return nil
case "set_options":
p.setOptions(req.Command.RepeatingContext, req.Command.RepeatingTrack, req.Command.ShufflingContext)
return nil
case "set_queue":
p.setQueue(req.Command.PrevTracks, req.Command.NextTracks)
Expand Down Expand Up @@ -355,13 +361,16 @@ func (p *AppPlayer) handleApiRequest(req ApiRequest) (any, error) {
p.updateVolume(vol * player.MaxStateVolume / *p.app.cfg.VolumeSteps)
return nil, nil
case ApiRequestTypeSetRepeatingContext:
p.setRepeatingContext(req.Data.(bool))
val := req.Data.(bool)
p.setOptions(&val, nil, nil)
return nil, nil
case ApiRequestTypeSetRepeatingTrack:
p.setRepeatingTrack(req.Data.(bool))
val := req.Data.(bool)
p.setOptions(nil, &val, nil)
return nil, nil
case ApiRequestTypeSetShufflingContext:
p.setShufflingContext(req.Data.(bool))
val := req.Data.(bool)
p.setOptions(nil, nil, &val)
return nil, nil
case ApiRequestTypeAddToQueue:
p.addToQueue(&connectpb.ContextTrack{Uri: req.Data.(string)})
Expand Down
4 changes: 2 additions & 2 deletions cmd/daemon/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ func (p *AppPlayer) initState() {
SupportsPlaylistV2: true,
IsControllable: true,
SupportsExternalEpisodes: false, // TODO: support external episodes
SupportsSetBackendMetadata: false,
SupportsSetBackendMetadata: true,
SupportsTransferCommand: true,
SupportsCommandRequest: true,
IsVoiceEnabled: false,
NeedsFullPlayerState: false,
SupportsGzipPushes: true,
SupportsSetOptionsCommand: false,
SupportsSetOptionsCommand: true,
SupportsHifi: nil, // TODO: nice to have?
ConnectCapabilities: "",
},
Expand Down
27 changes: 15 additions & 12 deletions dealer/recv.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,21 @@ type RequestPayload struct {
TargetAliasId string `json:"target_alias_id"`
SentByDeviceId string `json:"sent_by_device_id"`
Command struct {
Endpoint string `json:"endpoint"`
SessionId string `json:"session_id"`
Data []byte `json:"data"`
Value interface{} `json:"value"`
Position int64 `json:"position"`
Relative string `json:"relative"`
Context *connectpb.Context `json:"context"`
PlayOrigin *connectpb.PlayOrigin `json:"play_origin"`
Track *connectpb.ContextTrack `json:"track"`
PrevTracks []*connectpb.ContextTrack `json:"prev_tracks"`
NextTracks []*connectpb.ContextTrack `json:"next_tracks"`
LoggingParams struct {
Endpoint string `json:"endpoint"`
SessionId string `json:"session_id"`
Data []byte `json:"data"`
Value interface{} `json:"value"`
Position int64 `json:"position"`
Relative string `json:"relative"`
Context *connectpb.Context `json:"context"`
PlayOrigin *connectpb.PlayOrigin `json:"play_origin"`
Track *connectpb.ContextTrack `json:"track"`
PrevTracks []*connectpb.ContextTrack `json:"prev_tracks"`
NextTracks []*connectpb.ContextTrack `json:"next_tracks"`
RepeatingTrack *bool `json:"repeating_track"`
RepeatingContext *bool `json:"repeating_context"`
ShufflingContext *bool `json:"shuffling_context"`
LoggingParams struct {
CommandInitiatedTime int64 `json:"command_initiated_time"`
PageInstanceIds []string `json:"page_instance_ids"`
InteractionIds []string `json:"interaction_ids"`
Expand Down

0 comments on commit 4b81804

Please sign in to comment.