forked from grunjol/dlnacasts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
242 lines (201 loc) · 6.36 KB
/
index.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
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
var MediaRenderer = require('upnp-mediarenderer-client')
var debug = require('debug')('dlnacasts')
var events = require('events')
var get = require('simple-get')
var mime = require('mime')
var parallel = require('run-parallel')
var parseString = require('xml2js').parseString
var SSDP
try {
SSDP = require('node-ssdp').Client
} catch (err) {
SSDP = null
}
var thunky = require('thunky')
var noop = function () {}
module.exports = function () {
var that = new events.EventEmitter()
var casts = {}
var ssdp = SSDP ? new SSDP() : null
that.players = []
var emit = function (cst) {
if (!cst || !cst.host || cst.emitted) return
cst.emitted = true
var player = new events.EventEmitter()
var connect = thunky(function reconnect (cb) {
var client = new MediaRenderer(player.xml)
client.on('error', function (err) {
player.emit('error', err)
})
client.on('status', function (status) {
if (status.TransportState === 'PLAYING') player._status.playerState = 'PLAYING'
if (status.TransportState === 'PAUSED_PLAYBACK') player._status.playerState = 'PAUSED'
player.emit('status', player._status)
})
client.on('loading', function (err) {
player.emit('loading', err)
})
client.on('close', function () {
connect = thunky(reconnect)
})
player.client = client
cb(null, player.client)
})
var parseTime = function (time) {
if (!time || time.indexOf(':') === -1) return 0
var parts = time.split(':').map(Number)
return parts[0] * 3600 + parts[1] * 60 + parts[2]
}
player.name = cst.name
player.host = cst.host
player.xml = cst.xml
player.udn = cst.udn
player._status = {}
player.MAX_VOLUME = 100
player.play = function (url, opts, cb) {
if (typeof opts === 'function') return player.play(url, null, opts)
if (!opts) opts = {}
if (!url) return player.resume(cb)
if (!cb) cb = noop
player.subtitles = opts.subtitles
connect(function (err, p) {
if (err) return cb(err)
var media = {
autoplay: opts.autoPlay !== false,
contentType: opts.type || mime.lookup(url, 'video/mp4'),
metadata: opts.metadata || {
title: opts.title || '',
type: 'video', // can be 'video', 'audio' or 'image'
subtitlesUrl: player.subtitles && player.subtitles.length ? player.subtitles[0] : null
}
}
var callback = cb
if (opts.seek) {
callback = function () {
player.seek(opts.seek, cb)
}
}
p.load(url, media, callback)
})
}
player.resume = function (cb) {
if (!cb) cb = noop
player.client.play(cb)
}
player.pause = function (cb) {
if (!cb) cb = noop
player.client.pause(cb)
}
player.stop = function (cb) {
if (!cb) cb = noop
player.client.stop(cb)
}
player.status = function (cb) {
if (!cb) cb = noop
parallel({
currentTime: function (acb) {
var params = {
InstanceID: player.client.instanceId
}
player.client.callAction('AVTransport', 'GetPositionInfo', params, function (err, res) {
if (err) return
var position = parseTime(res.AbsTime) | parseTime(res.RelTime)
acb(null, position)
})
},
volume: function (acb) {
player._volume(acb)
}
},
function (err, results) {
debug('%o', results)
player._status.currentTime = results.currentTime
player._status.volume = {level: results.volume / (player.MAX_VOLUME)}
return cb(err, player._status)
})
}
player._volume = function (cb) {
var params = {
InstanceID: player.client.instanceId,
Channel: 'Master'
}
player.client.callAction('RenderingControl', 'GetVolume', params, function (err, res) {
if (err) return
var volume = res.CurrentVolume ? parseInt(res.CurrentVolume) : 0
cb(null, volume)
})
}
player.volume = function (vol, cb) {
if (!cb) cb = noop
var params = {
InstanceID: player.client.instanceId,
Channel: 'Master',
DesiredVolume: (player.MAX_VOLUME * vol) | 0
}
player.client.callAction('RenderingControl', 'SetVolume', params, cb)
}
player.request = function (target, action, data, cb) {
if (!cb) cb = noop
player.client.callAction(target, action, data, cb)
}
player.seek = function (time, cb) {
if (!cb) cb = noop
player.client.seek(time, cb)
}
player._detectVolume = function (cb) {
if (!cb) cb = noop
player._volume(function (err, currentVolume) {
if (err) cb(err)
player.volume(player.MAX_VOLUME, function (err) {
if (err) cb(err)
player._volume(function (err, maxVolume) {
if (err) cb(err)
player.MAX_VOLUME = maxVolume
player.volume(currentVolume, function (err) {
cb(err, maxVolume)
})
})
})
})
}
that.players.push(player)
that.emit('update', player)
}
if (ssdp) {
ssdp.on('response', function (headers, statusCode, info) {
if (!headers.LOCATION) return
get.concat(headers.LOCATION, function (err, res, body) {
if (err) return
parseString(body.toString(), {explicitArray: false, explicitRoot: false},
function (err, service) {
if (err) return
if (!service.device) return
debug('device %j', service.device)
var name = service.device.friendlyName
if (!name) return
var host = info.address
var xml = headers.LOCATION
var udn = service.device.UDN
if (!casts[udn]) {
casts[udn] = {name: name, host: host, xml: xml, udn: udn}
return emit(casts[udn])
}
if (casts[udn] && !casts[udn].host) {
casts[udn].name = name
casts[udn].host = host
casts[udn].xml = xml
emit(casts[udn])
}
})
})
})
}
that.update = function () {
debug('querying ssdp')
if (ssdp) ssdp.search('urn:schemas-upnp-org:device:MediaRenderer:1')
}
that.destroy = function () {
}
that.update()
return that
}