forked from mafintosh/chromecasts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
230 lines (189 loc) · 5.44 KB
/
index.js
File metadata and controls
230 lines (189 loc) · 5.44 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
var events = require('events')
var mdns = require('multicast-dns')
var thunky = require('thunky')
var castv2 = require('castv2-client')
var noop = function () {}
var toMap = function (url) {
return typeof url === 'string' ? {url: url} : url
}
var toSubtitles = function (url, i) {
if (typeof url !== 'string') return url
return {
trackId: i + 1,
type: 'TEXT',
trackContentId: url,
trackContentType: 'text/vtt',
name: 'English',
language: 'en-US',
subtype: 'SUBTITLES'
}
}
module.exports = function () {
var dns = mdns()
var that = new events.EventEmitter()
var casts = {}
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 castv2.Client()
client.on('error', function (err) {
player.emit('error', err)
})
client.on('close', function () {
connect = thunky(reconnect)
})
client.connect(player.host, function (err) {
if (err) return cb(err)
player.emit('connect')
client.launch(castv2.DefaultMediaReceiver, function (err, p) {
if (err) return cb(err)
player.emit('ready')
p.on('close', function () {
connect = thunky(reconnect)
})
p.on('status', function (status) {
player.emit('status', status)
})
cb(null, p)
})
})
})
var connectClient = thunky(function reconnectClient (cb) {
var client = new castv2.Client()
client.on('error', function (err) {
connectClient = thunky(reconnectClient)
})
client.on('close', function () {
connectClient = thunky(reconnectClient)
})
client.connect(player.host, function (err) {
if (err) return cb(err)
cb(null, client)
})
})
player.name = cst.name
player.host = cst.host
player.client = function (cb) {
connectClient(cb)
}
player.chromecastStatus = function (cb) {
connectClient(function (err, client) {
if (err) return cb(err)
client.getStatus(cb)
})
}
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
connect(function (err, p) {
if (err) return cb(err)
var media = {
contentId: url,
contentType: opts.type || 'video/mp4',
streamType: opts.streamType || 'BUFFERED',
tracks: [].concat(opts.subtitles || []).map(toSubtitles),
metadata: opts.metadata || {
type: 0,
metadataType: 0,
title: opts.title || '',
images: [].concat(opts.images || []).map(toMap)
}
}
var autoSubtitles = opts.autoSubtitles
if (autoSubtitles === false) autoSubtitles = 0
if (autoSubtitles === true) autoSubtitles = 1
var playerOptions = {
autoplay: opts.autoPlay !== false,
currentTime: opts.seek,
activeTrackIds: opts.subtitles && (autoSubtitles === 0 ? [] : [autoSubtitles || 1])
}
p.load(media, playerOptions, cb)
})
}
player.resume = function (cb) {
if (!cb) cb = noop
connect(function (err, p) {
if (err) return cb(err)
p.play()
})
}
player.pause = function (cb) {
if (!cb) cb = noop
connect(function (err, p) {
if (err) return cb(err)
p.pause(cb)
})
}
player.stop = function (cb) {
if (!cb) cb = noop
connect(function (err, p) {
if (err) return cb(err)
p.stop(cb)
})
}
player.status = function (cb) {
connect(function (err, p) {
if (err) return cb(err)
p.getStatus(cb)
})
}
player.subtitles = function(id, cb) {
if (!cb) cb = noop
player.request({
type: 'EDIT_TRACKS_INFO',
activeTrackIds: id ? [id === true ? 1 : id] : []
}, cb)
}
player.request = function (data, cb) {
if (!cb) cb = noop
connect(function (err, p) {
if (err) return cb(err)
p.media.sessionRequest(data, cb)
})
}
player.seek = function (time, cb) {
if (!cb) cb = noop
connect(function (err, p) {
if (err) return cb(err)
p.seek(time, cb)
})
}
that.players.push(player)
that.emit('update', player)
}
dns.on('response', function (response) {
response.answers.forEach(function (a) {
if (a.type === 'PTR' || a.name === '_googlecast._tcp.local') {
var name = a.data.replace('._googlecast._tcp.local', '')
if (!casts[name]) casts[name] = {name: name, host: null}
}
})
var onanswer = function (a) {
var name = a.name.replace('.local', '')
if (a.type === 'A' && casts[name] && !casts[name].host) {
casts[name].host = a.data
emit(casts[name])
}
}
response.additionals.forEach(onanswer)
response.answers.forEach(onanswer)
})
dns.on('query', function (q) {
dns.response({
answers: [{type: 'PTR', name: '_googlecast._tcp.local', data: 'john.g'}]
})
})
that.update = function () {
dns.query('_googlecast._tcp.local', 'PTR')
}
that.destroy = function () {
dns.destroy()
}
that.update()
return that
}