forked from AndreasAbdi/gochromecast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Device.go
executable file
·98 lines (81 loc) · 3.23 KB
/
Device.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package cast
import (
"net"
"time"
"github.com/barakmich/gochromecast/configs"
"github.com/barakmich/gochromecast/controllers"
"github.com/barakmich/gochromecast/controllers/media"
"github.com/barakmich/gochromecast/controllers/receiver"
"github.com/barakmich/gochromecast/primitives"
)
const defaultTimeout = time.Second * 10
//Device Object to run basic chromecast commands
type Device struct {
client *primitives.Client
heartbeatController *controllers.HeartbeatController
connectionController *controllers.ConnectionController
ReceiverController *controllers.ReceiverController
MediaController *controllers.MediaController
YoutubeController *controllers.YoutubeController
}
//NewDevice is constructor for Device struct
func NewDevice(host net.IP, port int) (Device, error) {
var device Device
client, err := primitives.NewClient(host, port)
if err != nil {
return device, err
}
device.client = client
device.heartbeatController = controllers.NewHeartbeatController(client, defaultChromecastSenderID, defaultChromecastReceiverID)
device.heartbeatController.Start()
device.connectionController = controllers.NewConnectionController(client, defaultChromecastSenderID, defaultChromecastReceiverID)
device.connectionController.Connect()
device.ReceiverController = controllers.NewReceiverController(client, defaultChromecastSenderID, defaultChromecastReceiverID)
device.MediaController = controllers.NewMediaController(client, defaultChromecastSenderID, device.ReceiverController)
device.YoutubeController = controllers.NewYoutubeController(client, defaultChromecastSenderID, device.ReceiverController)
return device, nil
}
//Play just plays.
func (device *Device) Play() {
device.MediaController.Play(defaultTimeout)
}
//PlayMedia plays a video via the media controller.
func (device *Device) PlayMedia(URL string, MIMEType string, streamType string) {
appID := configs.MediaReceiverAppID
device.ReceiverController.LaunchApplication(&appID, defaultTimeout, false)
device.MediaController.Load(URL, MIMEType, streamType, defaultTimeout)
}
//QuitApplication that is currently running on the device
func (device *Device) QuitApplication(timeout time.Duration) {
status, err := device.ReceiverController.GetStatus(timeout)
if err != nil {
return
}
for _, appSessions := range status.Applications {
session := appSessions.SessionID
device.ReceiverController.StopApplication(session, timeout)
}
}
//PlayYoutubeVideo launches the youtube app and tries to play the video based on its id.
func (device *Device) PlayYoutubeVideo(videoID string) {
appID := configs.YoutubeAppID
device.ReceiverController.LaunchApplication(&appID, defaultTimeout, false)
device.YoutubeController.PlayVideo(videoID, "")
}
//GetMediaStatus of current media controller
func (device *Device) GetMediaStatus(timeout time.Duration) []*media.MediaStatus {
response, err := device.MediaController.GetStatus(time.Second * 5)
if err != nil {
emptyStatus := make([]*media.MediaStatus, 0)
return emptyStatus
}
return response
}
//GetStatus of the device.
func (device *Device) GetStatus(timeout time.Duration) *receiver.Status {
response, err := device.ReceiverController.GetStatus(time.Second * 5)
if err != nil {
return nil
}
return response
}