Skip to content

Commit

Permalink
cmd/load-app: Load chromecast App and Content ID
Browse files Browse the repository at this point in the history
Add a new command to load a specific app on the chromecast and play the
content id. This should be useful for loading media on arbitrary
chromecast apps.

Updates: #90
  • Loading branch information
vishen committed Jan 31, 2021
1 parent ef42fff commit 3f5fae1
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Available Commands:
help Help about any command
httpserver Start the HTTP server
load Load and play media on the chromecast
load-app Load and play content on a chromecast app
ls List devices
mute Mute the chromecast
next Play the next available media
Expand Down
39 changes: 34 additions & 5 deletions application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ var (
const (
// 'CC1AD845' seems to be a predefined app; check link
// https://gist.github.com/jloutsenhizer/8855258
// https://github.com/thibauts/node-castv2
defaultChromecastAppId = "CC1AD845"
defaultChromecastAppID = "CC1AD845"

defaultSender = "sender-0"
defaultRecv = "receiver-0"
Expand Down Expand Up @@ -699,6 +698,32 @@ func (a *Application) Load(filenameOrUrl, contentType string, transcode, detach,
return nil
}

func (a *Application) LoadApp(appID, contentID string) error {
// old list https://gist.github.com/jloutsenhizer/8855258.
// NOTE: This isn't concurrent safe, but it doesn't need to be at the moment!
a.MediaStart()

if err := a.ensureIsAppID(appID); err != nil {
return errors.Wrapf(err, "unable to change chromecast app")
}

// Send the command to the chromecast
a.sendMediaRecv(&cast.LoadMediaCommand{
PayloadHeader: cast.LoadHeader,
CurrentTime: 0,
Autoplay: true,
Media: cast.MediaItem{
ContentId: contentID,
StreamType: "BUFFERED",
},
})

// Wait until we have been notified that the media has finished playing
a.MediaWait()

return nil
}

func (a *Application) QueueLoad(filenames []string, contentType string, transcode bool) error {

mediaItems, err := a.loadAndServeFiles(filenames, contentType, transcode)
Expand Down Expand Up @@ -742,14 +767,18 @@ func (a *Application) QueueLoad(filenames []string, contentType string, transcod
func (a *Application) ensureIsDefaultMediaReceiver() error {
// If the current chromecast application isn't the Default Media Receiver
// we need to change it.
if a.application == nil || a.application.AppId != defaultChromecastAppId {
return a.ensureIsAppID(defaultChromecastAppID)
}

func (a *Application) ensureIsAppID(appID string) error {
if a.application == nil || a.application.AppId != appID {
_, err := a.sendAndWaitDefaultRecv(&cast.LaunchRequest{
PayloadHeader: cast.LaunchHeader,
AppId: defaultChromecastAppId,
AppId: appID,
})

if err != nil {
return errors.Wrap(err, "unable to change to default media receiver")
return errors.Wrapf(err, "unable to change to appID %q", appID)
}
// Update the 'application' and 'media' field on the 'CastApplication'
return a.Update()
Expand Down
71 changes: 71 additions & 0 deletions cmd/load-app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright © 2021 Jonathan Pentecost <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"

"github.com/vishen/go-chromecast/ui"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

// loadAppCmd represents the load command
var loadAppCmd = &cobra.Command{
Use: "load-app <app-id> <content-id>",
Short: "Load and play content on a chromecast app",
Long: `Load and play content on a chromecast app. This requires
the chromecast receiver app to be specified. An older list can be found
here https://gist.github.com/jloutsenhizer/8855258.
`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return fmt.Errorf("requires exactly two arguments")
}
app, err := castApplication(cmd, args)
if err != nil {
fmt.Printf("unable to get cast application: %v\n", err)
return nil
}

// Optionally run a UI when playing this media:
runWithUI, _ := cmd.Flags().GetBool("with-ui")
if runWithUI {
go func() {
if err := app.LoadApp(args[0], args[1]); err != nil {
logrus.WithError(err).Fatal("unable to load media")
}
}()

ccui, err := ui.NewUserInterface(app)
if err != nil {
logrus.WithError(err).Fatal("unable to prepare a new user-interface")
}
return ccui.Run()
}

// Otherwise just run in CLI mode:
if err := app.LoadApp(args[0], args[1]); err != nil {
fmt.Printf("unable to load media: %v\n", err)
return nil
}
return nil
},
}

func init() {
rootCmd.AddCommand(loadAppCmd)
}
2 changes: 1 addition & 1 deletion cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var loadCmd = &cobra.Command{
Use: "load <filename_or_url>",
Short: "Load and play media on the chromecast",
Long: `Load and play media files on the chromecast, this will
start a streaming server locally and serve the media file to the
start a HTTP server locally and will stream the media file to the
chromecast if it is a local file, otherwise it will load the url.
If the media file is an unplayable media type by the chromecast, this
Expand Down
1 change: 1 addition & 0 deletions testdata/helptext.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Available Commands:
help Help about any command
httpserver Start the HTTP server
load Load and play media on the chromecast
load-app Load and play content on a chromecast app
ls List devices
mute Mute the chromecast
next Play the next available media
Expand Down

0 comments on commit 3f5fae1

Please sign in to comment.