-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
142 lines (135 loc) · 3.66 KB
/
app.js
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
var Application = require('@yodaos/application').Application
var flora = require('@yoda/flora')
var _ = require('@yoda/util')._
var logger = require('logger')('cloud-player')
var constant = require('./constant')
var FloraUri = constant.FloraUri
var app = Application({
created: function created () {
this.voices = []
this.agent = new flora.Agent(FloraUri)
this.agent.start()
this.agent.declareMethod(constant.InspectPlayerChannel, (req, res) => {
res.end(0, this.inspectPlayers())
})
},
url: function url (urlObj) {
logger.info('received url', urlObj.href)
var voice
var immediateResume = true
var hint
switch (urlObj.pathname) {
case '/play': {
voice = this.startVoice('player', [
urlObj.query.text, urlObj.query.url,
_.get(urlObj.query, 'transient', '1') === '1', /** defaults to transient */
_.get(urlObj.query, 'sequential', '0') === '1', /** defaults to sequential */
urlObj.query.tag
])
voice.tag = urlObj.query.tag
break
}
case '/pause': {
voice = this.getVoice('player')
if (voice) {
voice.pause()
}
break
}
case '/resume': {
voice = this.getVoice('player')
if (voice) {
voice.resume()
}
break
}
case '/seek': {
var pos = Number(urlObj.query.to)
var by = Number(urlObj.query.by)
hint = urlObj.query.hint
voice = this.getVoice('player')
if (voice == null) {
break
}
if (hint) {
immediateResume = false
this.startVoice('player', [hint, undefined, /** transient */ true])
}
if (!isNaN(pos) || pos >= 0) {
voice.seekTo(pos, immediateResume)
break
}
if (!isNaN(by)) {
voice.seekBy(by, immediateResume)
break
}
break
}
case '/set-speed': {
var speed = Number(urlObj.query.speed)
hint = urlObj.query.hint
if (isNaN(speed) || speed < 0) {
break
}
voice = this.getVoice('player')
if (voice == null) {
break
}
if (hint) {
immediateResume = false
this.startVoice('player', [hint, undefined, /** transient */ true])
}
voice.setSpeed(speed, immediateResume)
break
}
case '/stop': {
voice = this.getVoice('player')
if (voice) {
voice.abandon()
}
break
}
case '/play-tts-stream': {
this.startVoice('tts-stream', [
_.get(urlObj.query, 'pickupOnEnd', '0') === '1',
Number(_.get(urlObj.query, 'pickupDuration', '0')),
_.get(urlObj.query, 'hint')
])
break
}
}
}
})
app.inspectPlayers = function inspectPlayers () {
return this.voices
.filter(it => it.name === 'player')
.map(it => ([
it.tag,
['duration', _.get(it, 'player.duration')],
['position', _.get(it, 'player.position')],
['playing', _.get(it, 'player.playing') ? 1 : 0]
]))
}
app.startVoice = function startVoice (name, args) {
var voiceConstructor = require(`./voice/${name}`)
var voice = voiceConstructor.apply(this, args || [])
voice.name = name
this.voices.push(voice)
return voice
}
app.getVoice = function getVoice (name) {
for (var idx = 0; idx < this.voices.length; ++idx) {
var focus = this.voices[idx]
if (focus.name === name) {
return focus
}
}
}
app.finishVoice = function finishVoice (it) {
var idx = this.voices.indexOf(it)
if (idx < 0) {
return
}
this.voices.splice(idx, 1)
}
module.exports = app