Skip to content

Commit ea07ab0

Browse files
committed
all: cast ip address to use to be specified via flags.
Add an 'addr' command line flag to accept the address of the chromecast device to use. This may be required if the multicast DNS doesn't work because of networking setup. This will allow the user to specify the ip address of the device to use. This does require the user to know the ip address, but that can be found trivially. Fix panic when no connection available. Clean up error messages. Updates: #24
1 parent 3f54e66 commit ea07ab0

File tree

6 files changed

+53
-25
lines changed

6 files changed

+53
-25
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ Idle (Backdrop), volume=1.00 muted=false
4949
$ go-chromecast status -n "Living Room Speaker"
5050
Idle, volume=0.17 muted=false
5151
52+
# Specify a cat device by ip address.
53+
$ go-chromecast status -a 192.168.0.52
54+
Idle, volume=0.17 muted=false
55+
5256
# Specify a cast device uuid.
5357
$ go-chromecast status -u b87d86bed423a6feb8b91a7d2778b55c
5458
Idle (Default Media Receiver), volume=0.17 muted=false
@@ -205,11 +209,13 @@ Available Commands:
205209
watch Watch all events sent from a chromecaset device
206210
207211
Flags:
212+
-a, --addr string Address of the chromecast device
208213
--debug debug logging
209214
-d, --device string chromecast device, ie: 'Chromecast' or 'Google Home Mini'
210215
-n, --device-name string chromecast device name
211216
--disable-cache disable the cache
212217
-h, --help help for go-chromecast
218+
-p, --port string Port of the chromecast device if 'addr' is specified (default "8009")
213219
-u, --uuid string chromecast device uuid
214220
--with-ui run with a UI
215221

application/application.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (a *Application) Start(entry castdns.CastDNSEntry) error {
157157
}
158158

159159
if err := a.conn.Start(entry.GetAddr(), entry.GetPort()); err != nil {
160-
return errors.Wrap(err, "unable to start connection")
160+
return err
161161
}
162162
if err := a.sendDefaultConn(&cast.ConnectHeader); err != nil {
163163
return errors.Wrap(err, "unable to connect to chromecast")

cast/connection.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ func (c *Connection) Send(requestID int, payload Payload, sourceID, destinationI
109109
func (c *Connection) receiveLoop() {
110110
for {
111111
var length uint32
112+
if c.conn == nil {
113+
continue
114+
}
112115
if err := binary.Read(c.conn, binary.BigEndian, &length); err != nil {
113116
c.log("failed to binary read payload: %v", err)
114117
break

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@ func init() {
4545
rootCmd.PersistentFlags().StringP("device", "d", "", "chromecast device, ie: 'Chromecast' or 'Google Home Mini'")
4646
rootCmd.PersistentFlags().StringP("device-name", "n", "", "chromecast device name")
4747
rootCmd.PersistentFlags().StringP("uuid", "u", "", "chromecast device uuid")
48+
rootCmd.PersistentFlags().StringP("addr", "a", "", "Address of the chromecast device")
49+
rootCmd.PersistentFlags().StringP("port", "p", "8009", "Port of the chromecast device if 'addr' is specified")
4850
}

cmd/utils.go

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,33 +48,48 @@ func castApplication(cmd *cobra.Command, args []string) (*application.Applicatio
4848
device, _ := cmd.Flags().GetString("device")
4949
debug, _ := cmd.Flags().GetBool("debug")
5050
disableCache, _ := cmd.Flags().GetBool("disable-cache")
51+
addr, _ := cmd.Flags().GetString("addr")
52+
port, _ := cmd.Flags().GetString("port")
5153

52-
// If a device name or uuid was specified, check the cache for the ip+port
5354
var entry castdns.CastDNSEntry
54-
found := false
55-
if !disableCache && (deviceName != "" || deviceUuid != "") {
56-
entry = findCachedCastDNS(deviceName, deviceUuid)
57-
found = entry.GetAddr() != ""
58-
}
59-
if !found {
60-
var err error
61-
if entry, err = findCastDNS(device, deviceName, deviceUuid); err != nil {
62-
return nil, errors.Wrap(err, "unable to find cast dns entry")
55+
// If no address was specified, attempt to determine the address of any
56+
// local chromecast devices.
57+
if addr == "" {
58+
// If a device name or uuid was specified, check the cache for the ip+port
59+
found := false
60+
if !disableCache && (deviceName != "" || deviceUuid != "") {
61+
entry = findCachedCastDNS(deviceName, deviceUuid)
62+
found = entry.GetAddr() != ""
6363
}
64-
}
65-
if !disableCache {
66-
cachedEntry := CachedDNSEntry{
67-
UUID: entry.GetUUID(),
68-
Name: entry.GetName(),
69-
Addr: entry.GetAddr(),
70-
Port: entry.GetPort(),
64+
if !found {
65+
var err error
66+
if entry, err = findCastDNS(device, deviceName, deviceUuid); err != nil {
67+
return nil, errors.Wrap(err, "unable to find cast dns entry")
68+
}
69+
}
70+
if !disableCache {
71+
cachedEntry := CachedDNSEntry{
72+
UUID: entry.GetUUID(),
73+
Name: entry.GetName(),
74+
Addr: entry.GetAddr(),
75+
Port: entry.GetPort(),
76+
}
77+
cachedEntryJson, _ := json.Marshal(cachedEntry)
78+
cache.Save(getCacheKey(cachedEntry.UUID), cachedEntryJson)
79+
cache.Save(getCacheKey(cachedEntry.Name), cachedEntryJson)
80+
}
81+
if debug {
82+
fmt.Printf("using device name=%s addr=%s port=%d uuid=%s\n", entry.GetName(), entry.GetAddr(), entry.GetPort(), entry.GetUUID())
83+
}
84+
} else {
85+
p, err := strconv.Atoi(port)
86+
if err != nil {
87+
return nil, errors.Wrap(err, "port needs to be a number")
88+
}
89+
entry = CachedDNSEntry{
90+
Addr: addr,
91+
Port: p,
7192
}
72-
cachedEntryJson, _ := json.Marshal(cachedEntry)
73-
cache.Save(getCacheKey(cachedEntry.UUID), cachedEntryJson)
74-
cache.Save(getCacheKey(cachedEntry.Name), cachedEntryJson)
75-
}
76-
if debug {
77-
fmt.Printf("using device name=%s addr=%s port=%d uuid=%s\n", entry.GetName(), entry.GetAddr(), entry.GetPort(), entry.GetUUID())
7893
}
7994
app := application.NewApplication(debug, disableCache)
8095
if err := app.Start(entry); err != nil {

dns/dns.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ func FindCastDNSEntries() []CastEntry {
5858
entriesCh := make(chan *mdns.ServiceEntry, 20)
5959
go func() {
6060
// This will find any and all google products, including chromecast, home mini, etc.
61+
fmt.Println("WERD")
6162
mdns.Query(&mdns.QueryParam{
6263
Service: "_googlecast._tcp",
63-
Domain: "local",
64+
// Domain: "local",
65+
Domain: "192.168.0.1",
6466
Timeout: time.Second * 3,
6567
Entries: entriesCh,
6668
})

0 commit comments

Comments
 (0)