-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcast.js
More file actions
237 lines (206 loc) · 5.53 KB
/
cast.js
File metadata and controls
237 lines (206 loc) · 5.53 KB
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
var dlnacasts = require('./dlna')()
var server = require('./lib/server')
var onExit = require('./lib/onExit')
var readcommand = require('readcommand')
var EventEmitter = require('eventemitter2').EventEmitter2
var parseTorrent = require('parse-torrent')
var debug = require('debug')('bitcast:cast')
var isTorrent = url => {
try {
return url.match(/[.]torrent$/) || parseTorrent(url)
} catch(error) {
return false
}
}
var isHttp = url => url.match(/^https?\:\/\//)
var url = process.argv[2];
function showUsage() {
console.log(`
Usage:
bitcast $url
Param:
$url A URL of either
- http url of video file (mp4)
- a torrent identifier (.torrent file, magnet or infoHash)
`)
}
if (!url) {
console.log('Parameter Error: Please specify url argument')
showUsage()
return
} else {
setImmediate(() => startCast(url))
}
var sigints = 0;
var playerState = null
var currentPlayer = null
var noPlayerFoundMsg = 'No playable devices found network'
var startCast = (url) => {
if (isTorrent(url)) {
var magnet = url
console.log('Starting torrent stream', magnet)
server(magnet, function(err, url, server, client, type) {
if (err) {
console.log('Server error', err)
throw err
}
console.log('Server created at url', url, type)
castWithRetry(url)
.then(() => availableCommandMsg())
onExit(function() {
server.close()
if (!client.destroyed) {
client.destroy()
}
})
})
} else if (isHttp(url)) {
console.log('Starting http stream', url)
castWithRetry(url)
.then(() => availableCommandMsg())
} else {
console.log('Parameter Error: Unknown url argument')
showUsage()
}
}
var getPlayers = () => new Promise(function(resolve) {
if (dlnacasts.players.length) {
resolve(dlnacasts.players)
} else {
dlnacasts.once('update', function (player) {
resolve(dlnacasts.players)
})
}
})
var castWithRetry = (url, retries = 5, interval = 5000) => {
return cast(url).catch(error => {
debug(error)
console.log('Cast failed, retrying...')
return new Promise(resolve => setTimeout(() => {
castWithRetry(url, retries - 1, interval)
.then(status => resolve(status))
}))
})
}
var createError = ({msg, ...props}) => {
const error = new Error(msg)
Object.assign(error, props)
return error
}
function cast(url) {
return new Promise((resolve, reject) => {
getPlayers().then(function(players) {
if (!players.length) {
console.log(noPlayerFoundMsg)
return reject(createError({ msg: noPlayerFoundMsg, name: 'NoDNLAPlayerFoundError' }))
}
console.log('Available players: ', dlnacasts.players.map(player => player.name))
players.some(function(player) {
console.log('casting video to device', player.name, url)
player.play(url, {title: 'Streamcaster Torrent'}, () => resolve())
player.on('status', function(status) {
console.log('Player status', status)
playerState = status
resolve(status)
})
player.on('error', function(error) {
console.log('Player error', error)
reject(error)
})
currentPlayer = player
return player
})
})
})
}
readCommands()
function readCommands() {
const commandEvent = new EventEmitter()
debug('commandEvent', commandEvent)
const allowedCommands = [
'stop',
'start',
'play',
'pause',
'resume',
'volume',
'seek'
]
// run command
commandEvent.onAny((command, value) => {
console.log('Received command', command, value)
try {
currentPlayer[command](value, () => {
console.log('Completed command %s', command, value)
})
} catch(err) {
console.log('Error occurred procesisng command', err)
}
})
readcommand.loop(function(err, args, str, next) {
if (err) return readErrors(err, next)
debug('Received args', args);
if (args.length) {
const msg = args.join(' ')
debug('Player state', playerState)
if (!currentPlayer || !playerState) {
const state = playerState ? playerState.playerState : 'unknown'
console.log('Please wait for player to start. State: %o', playerState)
return next()
}
const parsedCommand = parseCommand(msg)
debug('command', parsedCommand)
if (parsedCommand) {
const {command, value} = parsedCommand
if (allowedCommands.includes(command)) {
debug('emitting command', command, value)
commandEvent.emit(command, value)
} else {
debug('Invalid command', command, value)
invalidCommandMsg()
}
}
} else {
invalidCommandMsg()
}
return next();
});
}
function availableCommandMsg() {
console.log(`
Available commands
seek [time]
play
stop
pause
resume
`)
}
function invalidCommandMsg() {
console.log(`Invalid command received.`)
availableCommandMsg()
}
function parseCommand(msg) {
const matches = msg.match(/^([a-z]+)\s*(.+)?$/i)
if (matches) {
const [, command, value] = matches
debug('parsed command', command, value)
return {command, value}
}
return false
}
function readErrors(err, next) {
if (err && err.code !== 'SIGINT') {
throw err;
} else if (err) {
if (sigints === 1) {
process.exit(0);
} else {
sigints++;
console.log('Press ^C again to exit.');
return next && next();
}
} else {
sigints = 0;
}
}