forked from xat/chromecast-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
260 lines (243 loc) · 7.52 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
var Client = require('castv2-client').Client;
var scanner = require('chromecast-scanner');
var ware = require('ware');
var mutate = require('mutate.js');
var inherits = require('util').inherits;
var ee = require('events').EventEmitter;
var extend = require('xtend');
var debug = require('debug')('chromecast-player');
var Promise = require('promiscuous');
var api = require('./api');
var noop = function() {};
var slice = Array.prototype.slice;
var defaults = {
autoplay: true,
ttl: 10000,
startTime: 0,
streamType: 'BUFFERED',
activeTrackIds: [],
media: {},
cb: noop
};
var apirize = function(fn, ctx) {
return mutate(function(opts) {
opts = opts || {};
if (opts._opts) {
opts = extend(opts, opts._opts);
delete opts._opts;
}
fn.call(ctx, opts);
})
.method(['function'], ['cb'])
.method(['object', 'function'], ['_opts', 'cb'])
.method(['string'], ['path'])
.method(['string', 'function'], ['path', 'cb'])
.method(['string', 'object'], ['path', '_opts'])
.method(['string', 'object', 'function'], ['path', '_opts', 'cb'])
.method(['string', 'string'], ['path', 'type'])
.method(['string', 'string', 'function'], ['path', 'type', 'cb'])
.method(['string', 'string', 'object'], ['path', 'type', '_opts'])
.method(['string', 'string', 'object', 'function'], ['path', 'type', '_opts', 'cb'])
.close();
};
var shutdown = function() {
debug('shutdown');
if (this.client && !this.clientClosed) {
this.client.close();
this.clientClosed = true;
}
if (this.player && !this.playerClosed) {
this.player.close();
this.playerClosed = true;
}
this.inst._setStatus(this, 'closed');
this.emit('closed');
};
var player = function() {
if (!(this instanceof player)) return new player();
ee.call(this);
this.mw = ware();
this.use = this.mw.use.bind(this.mw);
this.launch = apirize(function(opts) {
var that = this;
var ctx = new ee();
ctx.mode = 'launch';
ctx.options = opts;
ctx.api = api;
ctx.shutdown = shutdown;
ctx.inst = this;
this.mw.run(ctx, function(err, ctx) {
ctx.options = extend(defaults, ctx.options);
if (err) return ctx.options.cb(err);
that._setStatus(ctx, 'loading plugins');
that._scan(ctx)
.then(function(ctx) { return that._connect(ctx); })
.then(function(ctx) { return that._launch(ctx); })
.then(function(ctx) { return that._load(ctx); })
.then(function(ctx) { return that._status(ctx); })
.then(function(ctx) { ctx.options.cb(null, ctx.player, ctx); },
function(err) { ctx.options.cb(err); });
});
}, this);
this.attach = apirize(function(opts) {
var that = this;
var ctx = new ee();
ctx.mode = 'attach';
ctx.options = opts;
ctx.api = api;
ctx.shutdown = shutdown;
ctx.inst = this;
that._setStatus(ctx, 'loading plugins');
this.mw.run(ctx, function(err, opts) {
ctx.options = extend(defaults, ctx.options);
if (err) return ctx.options.cb(err);
that._scan(ctx)
.then(function(ctx) { return that._connect(ctx); })
.then(function(ctx) { return that._find(ctx); })
.then(function(ctx) { return that._join(ctx); })
.then(function(ctx) { return that._status(ctx); })
.then(function(ctx) { ctx.options.cb(null, ctx.player, ctx); },
function(err) { ctx.options.cb(err); });
});
}, this);
};
inherits(player, ee);
// find chromecast devices in the network and
// either return the first found or the one
// which matches device.
player.prototype._scan = function(ctx) {
this._setStatus(ctx, 'scanning');
return new Promise(function(resolve, reject) {
if (ctx.options.address) {
ctx.address = ctx.options.address;
return resolve(ctx);
}
scanner({
name: ctx.options.device ? ctx.options.device + '.local' : null,
ttl: ctx.options.ttl,
},
function(err, service) {
if (err) return reject(err);
ctx.address = service.data;
resolve(ctx);
}
);
});
};
// establish a connection to a chromecast device
player.prototype._connect = function(ctx) {
this._setStatus(ctx, 'connecting');
return new Promise(function(resolve, reject) {
var client = new Client();
client.connect(ctx.address, function() {
ctx.client = client;
resolve(ctx);
});
var onError = function(err) {
debug('client error %o', err);
client.removeListener('error', onError);
ctx.shutdown();
};
var onClose = function() {
debug('client onClose');
client.client.removeListener('close', onClose);
client.removeListener('error', onError);
ctx.clientClosed = true;
};
client.client.on('close', onClose);
client.on('error', onError);
});
};
// find running app
player.prototype._find = function(ctx) {
this._setStatus(ctx, 'finding');
return new Promise(function(resolve, reject) {
ctx.client.getSessions(function(err, apps) {
if (err) return reject(err);
if (!apps.length) return reject(new Error('app not found'));
ctx.session = apps[0];
resolve(ctx);
});
});
};
// join an existing chromecast session
player.prototype._join = function(ctx) {
var that = this;
this._setStatus(ctx, 'joining');
return new Promise(function(resolve, reject) {
ctx.client.join(ctx.session, ctx.api,
function(err, p) {
if (err) return reject(err);
if (p.setPlatform) p.setPlatform(ctx.client);
that._setStatus(ctx, 'ready');
ctx.player = p;
var onStatus = function(status) {
that._setStatus(ctx, status.playerState.toLowerCase());
};
var onClosed = function() {
debug('_join player onClosed');
ctx.player.removeListener('status', onStatus);
ctx.player.removeListener('closed', onClosed);
ctx.playerClosed = true;
ctx.shutdown();
};
ctx.player.on('status', onStatus);
ctx.player.on('closed', onClosed);
resolve(ctx);
}
);
});
};
// fetch the current state of the player
player.prototype._status = function(ctx) {
var that = this;
return new Promise(function(resolve, reject) {
ctx.player.updateStatus(function(err) {
if (err) return reject(err);
resolve(ctx);
});
});
};
// launch an application
player.prototype._launch = function(ctx) {
var that = this;
this._setStatus(ctx, 'launching');
return new Promise(function(resolve, reject) {
ctx.client.launch(ctx.api, function(err, p) {
if (err) return reject(err);
if (p.setPlatform) p.setPlatform(ctx.client);
ctx.player = p;
resolve(ctx);
});
});
};
// load a media file
player.prototype._load = function(ctx) {
var that = this;
this._setStatus(ctx, 'loading');
return new Promise(function(resolve, reject) {
ctx.player.load(ctx.options, function(err) {
if (err) return reject(err);
that._setStatus(ctx, 'ready');
var onStatus = function(status) {
that._setStatus(ctx, status.playerState.toLowerCase());
};
var onClosed = function() {
debug('_load player onClosed');
ctx.player.removeListener('status', onStatus);
ctx.player.removeListener('closed', onClosed);
ctx.playerClosed = true;
ctx.shutdown();
};
ctx.player.on('status', onStatus);
ctx.player.on('closed', onClosed);
resolve(ctx);
});
});
};
player.prototype._setStatus = function(ctx, status) {
ctx.status = status;
ctx.emit('status', status);
};
player.api = api;
module.exports = player;