@edenware/dlnacasts 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 grunjol et al.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,143 @@
1
+ # @edenware/dlnacasts
2
+
3
+ Query your local network for DLNA media renderers and have them play media
4
+
5
+ Note: @edenware/dlnacasts is a fork of rslavin abandonned dlnacasts2 (itself based on grunjol's dlnacasts). It has been updated for security reasons, including some hotfixes from contributors (see commit list). API (and code) based on mafintosh/chromecasts for DLNA.
6
+
7
+ ## Updating from 0.x.x to 1.x.x
8
+ Breaking changes:
9
+ - `const list = dlnacasts()` will no longer trigger a `list.update()`
10
+ - `player.on('status', status)` has changed, see below
11
+ - volume API changed, it is now `player.getVolume` and `player.setVolume`
12
+
13
+ ## Usage
14
+ ```
15
+ npm install @edenware/dlnacasts
16
+ ```
17
+ then in your JS files:
18
+ ``` js
19
+ const dlnacasts = require('@edenware/dlnacasts')()
20
+
21
+ dlnacasts.on('update', function (player) {
22
+ console.log('all players: ', dlnacasts.players)
23
+ player.play('http://example.com/my-video.mp4', {title: 'my video', type: 'video/mp4'})
24
+ })
25
+ ```
26
+
27
+ ## Module Compatibility
28
+
29
+ This package supports both CommonJS and ES Module systems:
30
+
31
+ **CommonJS (require):**
32
+ ```javascript
33
+ const dlnacasts = require('@edenware/dlnacasts')()
34
+ ```
35
+
36
+ **ES Modules (import):**
37
+ ```javascript
38
+ import dlnacasts from '@edenware/dlnacasts'
39
+ const list = dlnacasts()
40
+ ```
41
+
42
+ ## API
43
+
44
+ #### `const list = dlnacasts()`
45
+
46
+ Creates a dlna list.
47
+
48
+ #### `list.update()`
49
+
50
+ Updates the player list by querying the local network for DLNA renderer instances.
51
+
52
+ #### `list.on('update', player)`
53
+
54
+ Emitted when a new player is found on the local network
55
+
56
+ #### `player.play(url, [opts], cb)`
57
+
58
+ Make the player play a url. Options include:
59
+
60
+ ``` js
61
+ {
62
+ title: 'My movie',
63
+ type: 'video/mp4',
64
+ dlnaFeatures: 'DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01100000000000000000000000000000', // this enables seeking in some dlna devices like LG WebOS
65
+ seek: seconds, // start by seeking to this offset
66
+ subtitles: ['http://example.com/sub.vtt'], // subtitle track 1,
67
+ autoSubtitles: true // enable first track if you provide subs
68
+ }
69
+ ```
70
+
71
+ #### `player.subtitles(track, [cb])`
72
+
73
+ Enable subtitle track. Use `player.subtitles(false)` to disable subtitles
74
+
75
+ #### `player.pause([cb])`
76
+
77
+ Make the player pause playback
78
+
79
+ #### `player.resume([cb])`
80
+
81
+ Resume playback
82
+
83
+ #### `player.stop([cb])`
84
+
85
+ Stop the playback
86
+
87
+ #### `player.seek(seconds, [cb])`
88
+
89
+ Seek the video
90
+
91
+ ### `player.status([cb])`
92
+
93
+ Get a status of what's playing on the renderer. Similar to `player.on('status', cb)` event but manually triggered
94
+
95
+ ### `player.getVolume([cb])`
96
+
97
+ Get the volume of the renderer
98
+
99
+ ### `player.setVolume(<volume>, [cb])`
100
+
101
+ Set the volume on the renderer
102
+
103
+ #### `player.on('status', status)`
104
+
105
+ Emitted when a status object is received.
106
+
107
+ status Object()
108
+ ```js
109
+ {
110
+ currentTime: 122, // time in seconds (122 = 00:02:02)
111
+ playerState: "PAUSED_PLAYBACK", // player State: see below
112
+ volume: {
113
+ level: 0.1 // 0.1 corresponds to 10 on a scale of 100
114
+ }
115
+ }
116
+ ```
117
+
118
+ ## Security Notice
119
+
120
+ ⚠️ **Known Security Vulnerability**
121
+
122
+ This package depends on `@edenware/ssdp` which uses the `ip` package that has a known high-severity vulnerability (GHSA-2p57-rm9w-gvfp) related to SSRF improper categorization in `isPublic`.
123
+
124
+ - **Impact**: Potential Server-Side Request Forgery (SSRF) attacks
125
+ - **Status**: No fix available upstream
126
+ - **Recommendation**: Use this package only in trusted network environments
127
+
128
+ The vulnerability affects the IP address validation functionality used for SSDP network discovery. If you're concerned about security, consider using this package only in controlled environments or implement additional network security measures.
129
+
130
+ `status.playerState` could be one of :
131
+ ```js
132
+ [
133
+ 'PLAYING', // player is playing a video (player.pause() to pause)
134
+ 'STOPPED', // player was quit by user
135
+ 'PAUSED_PLAYBACK', // player was paused (player.play() to continue)
136
+ 'NO_MEDIA_PRESENT', // usually after a 'STOPPED'
137
+ 'TRANSITIONNING' // DLNA renderer is loading something
138
+ ]
139
+ ```
140
+
141
+ ## License
142
+
143
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,393 @@
1
+ const MediaRenderer = require('upnp-mediarenderer-client')
2
+ const events = require('events')
3
+ const parallel = require('run-parallel')
4
+ const parseString = require('xml2js').parseString
5
+ const SSDP = require('@edenware/ssdp').Client
6
+ const { isIP } = require('node:net')
7
+ const http = require('node:http')
8
+ const os = require('os')
9
+ const agent = new http.Agent({ keepAlive: true, maxSockets: 5 })
10
+
11
+ const SERVICE_TYPE = 'urn:schemas-upnp-org:device:MediaRenderer:1';
12
+ const thunky = require('thunky')
13
+
14
+ const noop = () => {}
15
+
16
+ module.exports = (options = {}) => {
17
+ const log = options.log ? console.debug : () => {}
18
+
19
+ // Find local IP
20
+ const interfaces = os.networkInterfaces()
21
+ let localIP = '192.168.1.8' // fallback
22
+ for (const name of Object.keys(interfaces)) {
23
+ for (const iface of interfaces[name]) {
24
+ if (iface.family === 'IPv4' && !iface.internal) {
25
+ localIP = iface.address
26
+ break
27
+ }
28
+ }
29
+ if (localIP !== '192.168.1.8') break
30
+ }
31
+ log('Local IP:', localIP)
32
+
33
+ const that = new events.EventEmitter()
34
+ const casts = {}
35
+ const ssdp = SSDP ? new SSDP({
36
+ unicastHost: localIP,
37
+ log: true
38
+ }) : null
39
+
40
+ log('[DLNACASTS] SSDP client created:', !!ssdp)
41
+
42
+ that.players = []
43
+
44
+ const emit = (cst) => {
45
+ if (!cst || !cst.host || cst.emitted) return
46
+ cst.emitted = true
47
+
48
+ const player = new events.EventEmitter()
49
+ let getStatus = undefined
50
+ let stopped = false // Flag para rastrear se stop foi chamado
51
+ let playFailed = false // Flag para rastrear se play falhou
52
+
53
+ const connect = thunky(function reconnect (cb) {
54
+ const client = new MediaRenderer(player.xml)
55
+
56
+ client.on('error', (err) => {
57
+ log('[DLNACASTS] Client error, clearing player.client:', err)
58
+ try { clearInterval(getStatus) } catch(e) {}
59
+ player.client = undefined
60
+ player.emit('error', err)
61
+ })
62
+
63
+ client.on('loading', (err) => {
64
+ player.emit('loading', err)
65
+ })
66
+
67
+ client.on('close', () => {
68
+ log('[DLNACASTS] Client closed, clearing player.client')
69
+ try { clearInterval(getStatus) } catch(e) {}
70
+ player.client = undefined
71
+ connect = thunky(reconnect) // Reset thunky para permitir nova reconexão
72
+ })
73
+
74
+ player.client = client
75
+ cb(null, player.client)
76
+ })
77
+
78
+ const parseTime = (time) => {
79
+ if (!time || time.indexOf(':') === -1) return 0
80
+ const parts = time.split(':').map(Number)
81
+ return parts[0] * 3600 + parts[1] * 60 + parts[2]
82
+ }
83
+
84
+ player.name = cst.name
85
+ player.host = cst.host
86
+ player.xml = cst.xml
87
+ player._status = {}
88
+ player.MAX_VOLUME = 100
89
+
90
+ player.connect = connect
91
+
92
+ player.close = (cb = noop) => {
93
+ log('[DLNACASTS] Closing player, clearing player.client')
94
+ try { clearInterval(getStatus) } catch(e) {}
95
+ if (player.client) {
96
+ for (let e of ["error", "status", "loading", "close"]) {
97
+ player.client.removeAllListeners(e)
98
+ }
99
+ player.client = undefined
100
+ }
101
+ stopped = true // Marcar que stop foi chamado
102
+ cb()
103
+ }
104
+
105
+ player.play = (url, opts, cb = noop) => {
106
+ if (typeof opts === 'function') return player.play(url, null, opts)
107
+ if (!opts) opts = {}
108
+ if (!url) return player.resume(cb)
109
+
110
+ stopped = false // Resetar stopped ao chamar play
111
+ playFailed = false // Resetar playFailed ao chamar play
112
+ player.subtitles = opts.subtitles
113
+
114
+ connect((err, p) => {
115
+ if (err) {
116
+ log('[DLNACASTS] Connect failed in play:', err)
117
+ playFailed = true // Marcar que play falhou
118
+ return cb(err)
119
+ }
120
+
121
+ try { clearInterval(getStatus) } catch(e) {}
122
+
123
+ const media = {
124
+ autoplay: opts.autoPlay !== false,
125
+ contentType: opts.type || 'video/mp4',
126
+ metadata: opts.metadata || {
127
+ title: opts.title || '',
128
+ type: 'video', // can be 'video', 'audio' or 'image'
129
+ subtitlesUrl: player.subtitles && player.subtitles.length ? player.subtitles[0] : null
130
+ }
131
+ }
132
+ if (opts.dlnaFeatures) {
133
+ media.dlnaFeatures = opts.dlnaFeatures || 'DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000'
134
+ //media.dlnaFeatures = opts.dlnaFeatures; // for LG WebOS 'DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01100000000000000000000000000000' allows seeking
135
+ }
136
+
137
+ let callback = cb
138
+ if (opts.seek) {
139
+ callback = (err) => {
140
+ if (err) {
141
+ playFailed = true // Marcar que play falhou
142
+ return cb(err)
143
+ }
144
+ player.seek(opts.seek, cb)
145
+ }
146
+ }
147
+
148
+ getStatus = setInterval(() => {
149
+ if (stopped || playFailed) {
150
+ log('[DLNACASTS] Skipping getStatus: stopped=%s, playFailed=%s', stopped, playFailed)
151
+ return
152
+ }
153
+ if (!player.client) {
154
+ log('[DLNACASTS] player.client is undefined in getStatus, attempting to reconnect')
155
+ connect((err) => {
156
+ if (err) {
157
+ log('[DLNACASTS] Reconnect failed in getStatus:', err)
158
+ playFailed = true // Marcar falha se reconexão falhar
159
+ return
160
+ }
161
+ log('[DLNACASTS] Reconnected successfully in getStatus')
162
+ player.client.callAction('AVTransport', 'GetTransportInfo', {
163
+ InstanceID: player.client.instanceId
164
+ }, (err, res) => {
165
+ if (err) return
166
+ const newStatus = res.CurrentTransportState
167
+ if (newStatus !== player._status.playerState) {
168
+ player._status.playerState = newStatus
169
+ player.status((err, status) => {
170
+ if (err) return
171
+ player.emit('status', status)
172
+ })
173
+ }
174
+ })
175
+ })
176
+ return
177
+ }
178
+ player.client.callAction('AVTransport', 'GetTransportInfo', {
179
+ InstanceID: player.client.instanceId
180
+ }, (err, res) => {
181
+ if (err) return
182
+ const newStatus = res.CurrentTransportState
183
+ if (newStatus !== player._status.playerState) {
184
+ player._status.playerState = newStatus
185
+ player.status((err, status) => {
186
+ if (err) return
187
+ player.emit('status', status)
188
+ })
189
+ }
190
+ })
191
+ }, 1000)
192
+
193
+ p.load(url, media, callback)
194
+ })
195
+ }
196
+
197
+ player.resume = (cb = noop) => {
198
+ player.client.play(cb)
199
+ }
200
+
201
+ player.pause = (cb = noop) => {
202
+ player.client.pause(cb)
203
+ }
204
+
205
+ player.stop = (cb = noop) => {
206
+ try { clearInterval(getStatus) } catch(e) {}
207
+ stopped = true // Marcar que stop foi chamado
208
+ player.client.stop(cb)
209
+ }
210
+
211
+ player.status = (cb = noop) => {
212
+ if (stopped || playFailed) {
213
+ log('[DLNACASTS] Skipping status: stopped=%s, playFailed=%s', stopped, playFailed)
214
+ return cb(null, player._status)
215
+ }
216
+ if (!player.client) {
217
+ log('[DLNACASTS] player.client is undefined in status, attempting to reconnect')
218
+ connect((err) => {
219
+ if (err) {
220
+ log('[DLNACASTS] Reconnect failed in status:', err)
221
+ playFailed = true // Marcar falha se reconexão falhar
222
+ return cb(err)
223
+ }
224
+ log('[DLNACASTS] Reconnected successfully in status')
225
+ parallel({
226
+ currentTime: (acb) => {
227
+ player.client.callAction('AVTransport', 'GetPositionInfo', {
228
+ InstanceID: player.client.instanceId
229
+ }, (err, res) => {
230
+ if (err) return acb()
231
+ acb(null, parseTime(res.AbsTime) | parseTime(res.RelTime))
232
+ })
233
+ },
234
+ volume: (acb) => {
235
+ player.getVolume(acb)
236
+ }
237
+ }, (err, results) => {
238
+ log('dlnacasts player.status results: %o', results)
239
+ player._status.currentTime = results.currentTime
240
+ player._status.volume = { level: results.volume / player.MAX_VOLUME }
241
+ return cb(err, player._status)
242
+ })
243
+ })
244
+ return
245
+ }
246
+
247
+ parallel({
248
+ currentTime: (acb) => {
249
+ player.client.callAction('AVTransport', 'GetPositionInfo', {
250
+ InstanceID: player.client.instanceId
251
+ }, (err, res) => {
252
+ if (err) return acb()
253
+ acb(null, parseTime(res.AbsTime) | parseTime(res.RelTime))
254
+ })
255
+ },
256
+ volume: (acb) => {
257
+ player.getVolume(acb)
258
+ }
259
+ }, (err, results) => {
260
+ log('dlnacasts player.status results: %o', results)
261
+ player._status.currentTime = results.currentTime
262
+ player._status.volume = { level: results.volume / player.MAX_VOLUME }
263
+ return cb(err, player._status)
264
+ })
265
+ }
266
+
267
+ player.getVolume = (cb) => {
268
+ player.client.callAction('RenderingControl', 'GetVolume', {
269
+ InstanceID: player.client.instanceId,
270
+ Channel: 'Master'
271
+ }, (err, res) => {
272
+ if (err) return cb()
273
+ cb(null, res.CurrentVolume ? parseInt(res.CurrentVolume) : 0)
274
+ })
275
+ }
276
+
277
+ player.setVolume = (vol, cb = noop) => {
278
+ player.client.callAction('RenderingControl', 'SetVolume', {
279
+ InstanceID: player.client.instanceId,
280
+ Channel: 'Master',
281
+ DesiredVolume: vol
282
+ }, cb)
283
+ }
284
+
285
+ player.request = (target, action, data, cb = noop) => {
286
+ if (data.InstanceID === null) {
287
+ data.InstanceID = player.client.instanceId
288
+ }
289
+ player.client.callAction(target, action, data, cb)
290
+ }
291
+
292
+ player.seek = (time, cb = noop) => {
293
+ player.client.seek(time, cb)
294
+ }
295
+
296
+ that.players.push(player)
297
+ that.emit('update', player)
298
+ }
299
+
300
+ if (ssdp) {
301
+ // Response handler moved to update()
302
+ }
303
+
304
+ that.validate = (name, host, xml) => {
305
+ if (!casts[name]) {
306
+ http.get(xml, { agent }, res => {
307
+ const {statusCode} = res
308
+ if (statusCode == 200) {
309
+ if (!casts[name]) {
310
+ casts[name] = {name, host, xml}
311
+ emit(casts[name])
312
+ } else if (isIP(casts[name].host) != 4 && isIP(host) == 4) {
313
+ casts[name].host = host
314
+ casts[name].xml = xml
315
+ emit(casts[name])
316
+ }
317
+ }
318
+ res.resume()
319
+ }).on('error', e => {})
320
+ }
321
+ }
322
+
323
+ that.update = () => {
324
+ log('[DLNACASTS] querying ssdp')
325
+ if (ssdp) {
326
+ const tasks = []
327
+ const responseHandler = (headers, statusCode, info) => {
328
+ if (!headers.LOCATION) return
329
+ if (headers.ST !== SERVICE_TYPE) return
330
+
331
+ tasks.push((cb) => {
332
+ http.get(headers.LOCATION, { agent }, (res) => {
333
+ if (res.statusCode !== 200) return cb()
334
+ let body = ''
335
+ res.on('data', (chunk) => { body += chunk })
336
+ res.on('end', () => {
337
+ parseString(body, {explicitArray: false, explicitRoot: false},
338
+ (err, service) => {
339
+ if (err) return cb()
340
+ if (!service.device) return cb()
341
+
342
+ log('[DLNACASTS] ssdp device:', service.device)
343
+
344
+ const name = service.device.friendlyName
345
+
346
+ if (!name) return cb()
347
+
348
+ const host = info.address
349
+ const xml = headers.LOCATION
350
+
351
+ if (!casts[name]) {
352
+ casts[name] = {name: name, host: host, xml: xml}
353
+ emit(casts[name])
354
+ } else if (casts[name] && !casts[name].host) {
355
+ casts[name].host = host
356
+ casts[name].xml = xml
357
+ emit(casts[name])
358
+ }
359
+ cb()
360
+ })
361
+ })
362
+ }).on('error', () => cb())
363
+ })
364
+ }
365
+ ssdp.on('response', responseHandler)
366
+ ssdp.search(SERVICE_TYPE)
367
+ log('[DLNACASTS] SSDP search started for:', SERVICE_TYPE)
368
+ setTimeout(() => {
369
+ ssdp.removeListener('response', responseHandler)
370
+ parallel(tasks, () => {})
371
+ }, 10000)
372
+ }
373
+ }
374
+
375
+ that.on('removeListener', () => {
376
+ if (ssdp && that.listenerCount('update') === 0) {
377
+ ssdp.stop()
378
+ }
379
+ })
380
+
381
+ that.destroy = () => {
382
+ log('[DLNACASTS] destroying ssdp...')
383
+ if (ssdp) {
384
+ ssdp.stop()
385
+ }
386
+ }
387
+
388
+ that.close = () => {
389
+ that.removeAllListeners('update')
390
+ }
391
+
392
+ return that
393
+ }