Skip to content

Commit

Permalink
Updated chromecast
Browse files Browse the repository at this point in the history
  • Loading branch information
djthorpe committed Apr 3, 2021
1 parent 0fb7fef commit 12326aa
Show file tree
Hide file tree
Showing 17 changed files with 928 additions and 117 deletions.
87 changes: 87 additions & 0 deletions cmd/rpc/chromecast.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"strconv"
"time"

// Modules
Expand Down Expand Up @@ -52,6 +53,75 @@ func (this *Chromecast) Define(cfg gopi.Config) {
}
return table.Write(os.Stdout, table.OptHeader(), table.OptAscii(80, data.BorderLines))
})

cfg.Command("cast connect", "Connect to chromecast", func(ctx context.Context) error {
stub := this.GetStub(ctx)
args := this.GetArgs(ctx)
if len(args) != 1 {
return gopi.ErrBadParameter
}
if _, err := stub.Connect(ctx, args[0]); err != nil {
return err
} else {
return nil
}
})

cfg.Command("cast disconnect", "Disconnect from a chromecast", func(ctx context.Context) error {
stub := this.GetStub(ctx)
args := this.GetArgs(ctx)
if len(args) != 1 {
return gopi.ErrBadParameter
}
return stub.Disconnect(ctx, args[0])
})

cfg.Command("cast vol", "Set volume", func(ctx context.Context) error {
stub := this.GetStub(ctx)
args := this.GetArgs(ctx)
if len(args) != 2 {
return gopi.ErrBadParameter
} else if vol, err := strconv.ParseFloat(args[1], 32); err != nil {
return err
} else if _, err := stub.SetVolume(ctx, args[0], float32(vol)); err != nil {
return err
}
return nil
})

cfg.Command("cast mute", "Mute volume", func(ctx context.Context) error {
stub := this.GetStub(ctx)
args := this.GetArgs(ctx)
if len(args) != 1 {
return gopi.ErrBadParameter
} else if _, err := stub.SetMuted(ctx, args[0], true); err != nil {
return err
}
return nil
})

cfg.Command("cast unmute", "Unmute volume", func(ctx context.Context) error {
stub := this.GetStub(ctx)
args := this.GetArgs(ctx)
if len(args) != 1 {
return gopi.ErrBadParameter
} else if _, err := stub.SetMuted(ctx, args[0], false); err != nil {
return err
}
return nil
})

cfg.Command("cast app", "Launch application", func(ctx context.Context) error {
stub := this.GetStub(ctx)
args := this.GetArgs(ctx)
if len(args) != 2 {
return gopi.ErrBadParameter
} else if _, err := stub.LaunchAppWithId(ctx, args[0], toAppId(args[1])); err != nil {
return err
}
return nil
})

}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -60,3 +130,20 @@ func (this *Chromecast) Define(cfg gopi.Config) {
func (this *Chromecast) GetStub(ctx context.Context) gopi.CastStub {
return ctx.Value(KeyStub).(gopi.CastStub)
}

func (this *Chromecast) GetArgs(ctx context.Context) []string {
return ctx.Value(KeyArgs).([]string)
}

func toAppId(name string) string {
switch name {
case "backdrop":
return gopi.CAST_APPID_BACKDROP
case "mutablemedia":
return gopi.CAST_APPID_MUTABLEMEDIA
case "default":
return gopi.CAST_APPID_DEFAULT
default:
return name
}
}
98 changes: 45 additions & 53 deletions dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"image"
"net"
"net/url"
"strings"
"time"
)
Expand Down Expand Up @@ -270,26 +271,31 @@ type CastManager interface {
// Return list of discovered Google Chromecast Devices
Devices(context.Context) ([]Cast, error)

// Return a chromecast by id or name, returns nil if not found
Get(string) Cast

// Connect to the control channel for a device
Connect(Cast) error

// Disconnect from the device
Disconnect(Cast) error

/*
// SetVolume sets the volume for a device, the value is between 0.0 and 1.0
SetVolume(context.Context, Cast, float32) error

// LaunchAppWithId launches application with Id on a cast device.
LaunchAppWithId(Cast, string) error
// SetMuted sets the volume as muted or unmuted. Does not affect the
// volume level
SetMuted(context.Context, Cast, bool) error

// SetVolume sets the volume for a device, the value is between 0.0
// and 1.0
SetVolume(Cast, float32) error
// LaunchAppWithId launches application with Id on a cast device.
LaunchAppWithId(context.Context, Cast, string) error

// SetMuted sets the volume as muted or unmuted. Does not affect the
// volume level
SetMuted(Cast, bool) error
// LoadMedia loads a video, audio, webpage or image onto the Chromecast,
// assuming an application has already been loaded. Autoplay parameter
// starts media playback immediately
LoadMedia(context.Context, Cast, *url.URL, bool) error

/*
// SetPlay sets media playback state to either PLAY or STOP
SetPlay(Cast, bool) error
Expand All @@ -305,19 +311,7 @@ type CastManager interface {
// Send stop signal, terminating any playing media
Stop(Cast) error
// Stream URL to Chromecast supports HTTP and HTTPS protocols,
// and the stream can be automatically started if the third
// argument is set to true. Requires application load before
// calling, to set the transport, or else returns an OutOfOrder
// error
LoadURL(Cast, *url.URL, bool) error
// Returns current volume state (level and muted)
Volume(Cast) (float32, bool, error)
// Returns app state
App(Cast) (CastApp, error)*/
*/
}

// Cast represents a Google Chromecast device
Expand All @@ -336,6 +330,11 @@ type Cast interface {

// State returns 0 if backdrop, else returns 1
State() uint

// Returns current volume state (level,0->1 and muted)
// and will return 0,false if not known (not connected to
// cast device)
Volume() (float32, bool)
}

// CastApp represents an application running on the Chromecast
Expand Down Expand Up @@ -373,46 +372,39 @@ type CastStub interface {
// Stream emits chromecast events on a channel
Stream(context.Context, chan<- CastEvent) error

/*
// Return Volume
Volume(ctx context.Context, castId string) (float32, bool, error)
// Connect to chromecast
Connect(context.Context, string) (Cast, error)

// SetVolume sets the Chromecast sound volume
SetVolume(ctx context.Context, castId string, value float32) error
// Connect to chromecast
Disconnect(context.Context, string) error

// SetMute mutes and unmutes the sound
SetMute(ctx context.Context, castId string, value bool) error
// SetVolume sets the sound volume
SetVolume(ctx context.Context, key string, level float32) (Cast, error)

// Return App
App(ctx context.Context, castId string) (CastApp, error)
// SetMuted mutes and unmutes the sound
SetMuted(ctx context.Context, key string, value bool) (Cast, error)

// SetApp loads an application into the Chromecast
SetApp(ctx context.Context, castId, appId string) error
// LaunchAppWithId loads an application into the Chromecast
LaunchAppWithId(context.Context, string, string) (Cast, error)

// LoadURL loads a video, audio or image onto the Chromecast,
// assuming an application has already been loaded
LoadURL(ctx context.Context, castId string, url *url.URL) error
/*
// Stop stops currently playing media if a media session is ongoing
// or else resets the Chromecast to the backdrop if no media session
Stop(ctx context.Context, castId string) error
// Stop stops currently playing media if a media session is ongoing
// or else resets the Chromecast to the backdrop if no media session
Stop(ctx context.Context, castId string) error
// Play resumes playback after paused media
Play(ctx context.Context, castId string) error
// Play resumes playback after paused media
Play(ctx context.Context, castId string) error
// Pause the media session
Pause(ctx context.Context, castId string) error
// Pause the media session
Pause(ctx context.Context, castId string) error
// SeekAbs within playing audio or video relative to the start of the
// playing media
SeekAbs(ctx context.Context, castId string, value time.Duration) error
// SeekAbs within playing audio or video relative to the start of the
// playing media
SeekAbs(ctx context.Context, castId string, value time.Duration) error
// SeekRel within playing audio or video relative to the current position
SeekRel(ctx context.Context, castId string, value time.Duration) error
// Stream emits events from Chromecasts, filtered
// by the id of the chromecast until context is cancelled. Where
// the id filter is empty, all connected chromecast events are emitted
// SeekRel within playing audio or video relative to the current position
SeekRel(ctx context.Context, castId string, value time.Duration) error
*/
}

Expand Down
5 changes: 3 additions & 2 deletions gopi.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ type Promise interface {
// Chain a function to a promise
Then(func(context.Context, interface{}) (interface{}, error)) Promise

// Finalally runs the promise in the background and optionally waits for it to complete
Finally(func(interface{}, error), bool)
// Finally runs the promise in the background and optionally waits for it to complete
// then returns any error if not running in background
Finally(func(interface{}, error) error, bool) error
}

/////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion pkg/dev/googlecast/app.go → pkg/dev/chromecast/app.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package googlecast
package chromecast

import (
"strconv"
Expand Down
Loading

0 comments on commit 12326aa

Please sign in to comment.