-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.go
336 lines (251 loc) · 7.5 KB
/
player.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main
import (
"encoding/xml"
"fmt"
"math"
"regexp"
"time"
"github.com/ninjasphere/go-ninja/api"
"github.com/ninjasphere/go-ninja/channels"
"github.com/ninjasphere/go-ninja/devices"
"github.com/ninjasphere/go-ninja/logger"
"github.com/ninjasphere/go-ninja/model"
"github.com/ninjasphere/go-sonos"
"github.com/ninjasphere/go-sonos/didl"
"github.com/ninjasphere/go-sonos/upnp"
)
const (
defaultInstanceID = 0
defaultSpeed = "1"
)
type sonosZonePlayer struct {
*sonos.Sonos
log *logger.Logger
player *devices.MediaPlayerDevice
lastSeen time.Time
}
func (sp *sonosZonePlayer) UpdateLastSeen() {
sp.lastSeen = time.Now()
}
func (sp *sonosZonePlayer) applyPlayPause(playing bool) error {
sp.log.Infof("applyPlayPause called, playing: %t", playing)
if playing {
err := sp.Play(defaultInstanceID, defaultSpeed)
if err != nil {
return err
}
return sp.player.UpdateControlState(channels.MediaControlEventPlaying)
}
err := sp.Pause(defaultInstanceID)
if err != nil {
return err
}
return sp.player.UpdateControlState(channels.MediaControlEventPaused)
}
func (sp *sonosZonePlayer) applyStop() error {
sp.log.Infof("applyStop called")
err := sp.Stop(defaultInstanceID)
if err != nil {
return err
}
return sp.player.UpdateControlState(channels.MediaControlEventStopped)
}
func (sp *sonosZonePlayer) applyPlaylistJump(delta int) error {
sp.log.Infof("applyPlaylistJump called, delta : %d", delta)
if delta < 0 {
return sp.Previous(defaultInstanceID)
}
return sp.Next(defaultInstanceID)
}
func (sp *sonosZonePlayer) applyVolume(volume *channels.VolumeState) error {
sp.log.Infof("applyVolume called, volume %v", volume)
if volume.Level != nil {
vol := uint16(*volume.Level * 100)
if err := sp.SetVolume(defaultInstanceID, upnp.Channel_Master, vol); err != nil {
return err
}
}
if volume.Muted != nil {
if err := sp.SetMute(defaultInstanceID, upnp.Channel_Master, *volume.Muted); err != nil {
return err
}
}
return sp.player.UpdateVolumeState(volume)
}
func (sp *sonosZonePlayer) applyPlayURL(url string, queue bool) error {
return fmt.Errorf("Playing a URL has not been implemented yet.")
}
func (sp *sonosZonePlayer) bindMethods() error {
sp.player.ApplyPlayPause = sp.applyPlayPause
sp.player.ApplyStop = sp.applyStop
sp.player.ApplyPlaylistJump = sp.applyPlaylistJump
sp.player.ApplyVolume = sp.applyVolume
sp.player.ApplyPlayURL = sp.applyPlayURL
err := sp.player.EnableControlChannel([]string{
"playing",
"paused",
"stopped",
"idle",
})
if err != nil {
return err
}
err = sp.player.EnableVolumeChannel(true)
if err != nil {
return err
}
err = sp.player.EnableMediaChannel()
if err != nil {
return err
}
return nil
}
var timeDuration = regexp.MustCompile("([0-9]{1,2})\\:([0-9]{2})\\:([0-9]{2})")
func parseDuration(t string) (*time.Duration, error) {
found := timeDuration.FindAllStringSubmatch(t, -1)
if found == nil || len(found) == 0 {
return nil, fmt.Errorf("Failed to parse duration from '%s'", t)
}
duration, err := time.ParseDuration(fmt.Sprintf("%sh%sm%ss", found[0][1], found[0][2], found[0][3]))
if found == nil || len(found) == 0 {
return nil, fmt.Errorf("Failed to parse duration from '%s': %s", t, err)
}
return &duration, nil
}
func (sp *sonosZonePlayer) updateMedia() error {
t := sp.log
positionInfo, err := sp.GetPositionInfo(0)
if err != nil {
return fmt.Errorf("Failed getting position info: %s", err)
}
if positionInfo.TrackMetaData == "" {
t.Infof("No track!")
err = sp.player.UpdateMusicMediaState(nil, nil)
return err
}
track := &channels.MusicTrackMediaItem{
ID: &positionInfo.TrackURI,
}
var positionMs int
if positionInfo.TrackDuration != "NOT_IMPLEMENTED" {
duration, err := parseDuration(positionInfo.TrackDuration)
if err != nil {
return fmt.Errorf("Failed parsing duration: %s", err)
}
durationMs := int(*duration / time.Millisecond)
track.Duration = &durationMs
}
if positionInfo.RelTime != "NOT_IMPLEMENTED" {
position, err := parseDuration(positionInfo.RelTime)
if err != nil {
return fmt.Errorf("Failed parsing position: %s", err)
}
positionMs = int(*position / time.Millisecond)
}
var trackMetadata didl.Lite
if positionInfo.TrackMetaData != "NOT_IMPLEMENTED" {
err = xml.Unmarshal([]byte(positionInfo.TrackMetaData), &trackMetadata)
if err != nil {
return fmt.Errorf("Failed unmarshalling metadata(%s): %s", positionInfo.TrackMetaData, err)
}
if len(trackMetadata.Item) > 0 {
item := trackMetadata.Item[0]
if len(item.Title) > 0 {
track.Title = &item.Title[0].Value
}
if len(item.Album) > 0 {
track.Album = &channels.MediaItemAlbum{
Name: item.Album[0].Value,
}
}
if len(item.Creator) > 0 {
track.Artists = &[]channels.MediaItemArtist{
channels.MediaItemArtist{
Name: item.Creator[0].Value,
},
}
}
}
}
err = sp.player.UpdateMusicMediaState(track, &positionMs)
if err != nil {
return fmt.Errorf("Failed sending media state: %s", err)
}
return nil
}
func (sp *sonosZonePlayer) updateState() error {
sp.log.Infof("updateMedia")
if err := sp.updateMedia(); err != nil {
return fmt.Errorf("Failed to update media state: %s", err)
}
muted, err := sp.GetMute(defaultInstanceID, upnp.Channel_Master)
if err != nil {
return fmt.Errorf("Failed to get mute: %s", err)
}
vol, err := sp.GetVolume(defaultInstanceID, upnp.Channel_Master)
if err != nil {
return fmt.Errorf("Failed to get volume: %s", err)
}
var volume float64
if vol > 0 {
volume = float64(vol) / 100
} else {
volume = float64(0)
}
volume = math.Min(1, volume)
sp.log.Infof("UpdateVolumeState %d %f", vol, volume)
if err := sp.player.UpdateVolumeState(&channels.VolumeState{
Level: &volume,
Muted: &muted,
}); err != nil {
return err
}
transportInfo, err := sp.GetTransportInfo(defaultInstanceID)
if err != nil {
return err
}
switch transportInfo.CurrentTransportState {
case upnp.State_PLAYING:
sp.log.Infof("UpdateControlState PLAYING")
return sp.player.UpdateControlState(channels.MediaControlEventPlaying)
case upnp.State_STOPPED:
sp.log.Infof("UpdateControlState STOPPED")
return sp.player.UpdateControlState(channels.MediaControlEventStopped)
case upnp.State_PAUSED_PLAYBACK:
sp.log.Infof("UpdateControlState PAUSED")
return sp.player.UpdateControlState(channels.MediaControlEventPaused)
}
return nil
}
func NewPlayer(driver *sonosDriver, conn *ninja.Connection, zoneInfo *sonosZoneInfo) (*sonosZonePlayer, error) {
id := zoneInfo.attributes.CurrentZoneGroupID
name := zoneInfo.attributes.CurrentZoneGroupName
nlog.Infof("Making media player with ID: %s Label: %s")
player, err := devices.CreateMediaPlayerDevice(driver, &model.Device{
NaturalID: id,
NaturalIDType: "sonos",
Name: &name,
Signatures: &map[string]string{
"ninja:manufacturer": "Sonos",
"ninja:productName": "Sonos Player",
"ninja:productType": "MediaPlayer",
"ninja:thingType": "mediaplayer",
},
}, conn)
if err != nil {
nlog.FatalError(err, "Failed to create media player device")
}
// At the moment I am using the first player in that zone
// i need to know how this looks in a multi player situation.
sonosUnit := zoneInfo.players[0]
sp := &sonosZonePlayer{sonosUnit, logger.GetLogger("sonosZonePlayer"), player, time.Now()}
err = sp.bindMethods()
if err != nil {
sp.log.FatalError(err, "Failed to bind channels to sonos device")
}
err = sp.updateState()
if err != nil {
sp.log.FatalError(err, "Failed to get initial device state")
}
return sp, nil
}