-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
executable file
·252 lines (205 loc) · 8.5 KB
/
main.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
//Audiodevice by http://whoshacks.blogspot.com/2009/01/change-audio-devices-via-shell-script.html
//Shell and filesystem dependencies
require('shelljs/global');
var path = require('path');
var _ = require('lodash');
//Electron dependencies
var menubar = require('menubar');
var Menu = require('menu');
var MenuItem = require('menu-item');
var dialog = require('dialog');
var mb = menubar({dir: __dirname, icon: 'not-castingTemplate.png'});
/* Internals */
var MenuFactory = require('./lib/native/MenuFactory');
var NotificationService = require('./lib/native/NotificationService');
var DeviceLookupService = require('./lib/device/utils/DeviceLookupService');
var DeviceMatcher = require('./lib/device/utils/DeviceMatcher');
var LocalSourceSwitcher = require('./lib/device/utils/LocalSourceSwitcher');
var UpnpMediaClientUtils = require('./lib/device/utils/UpnpMediaClientUtils');
var LocalSoundStreamer = require('./lib/sound/LocalSoundStreamerExec');
/* Various Device controllers */
var JongoSpeaker = require('./lib/device/controls/JongoSpeaker');
var ChromeCast = require('./lib/device/controls/ChromeCast');
var logger = require('./lib/common/logger');
//Menubar construction
mb.on('ready', function ready() {
//Menu startup message
var menu = new Menu();
var deviceListMenu = new Menu();
// Initial scanning for devices menu added
menu.append(MenuFactory.scanningForDevices());
// Clicking this option starts casting audio to Cast
menu.append(MenuFactory.castToDeviceMenu(deviceListMenu));
//Refresh
deviceListMenu.append(new MenuItem({
label: 'Refresh Devices...',
click: function () {
logger.debug('TODO - Refresh Items');
}
}));
deviceListMenu.append(MenuFactory.separator());
var devicesAdded = [];
var streamingAddress;
LocalSoundStreamer.startStream(function (err, streamUrl) {
if (err) {
logger.info('Streaming process died', err);
dialog.showMessageBox({
title: 'Error',
message: 'Streaming has crashed, you may need to restart the application!',
detail: err.toString(),
buttons: ["OK"]
});
} else {
streamingAddress = streamUrl;
}
});
DeviceLookupService.lookUpDevices(function onDevice(device) {
// Disable the 'Scanning for Devices...'
menu.items[0].enabled = false;
switch (device.type) {
case DeviceMatcher.TYPES.CHROMECAST:
if (DeviceMatcher.isChromecast(device) || DeviceMatcher.isChromecastAudio(device)) {
devicesAdded.push(device);
deviceListMenu.append(MenuFactory.chromeCastItem(device, function onClicked() {
logger.info('Attempting to play to Chromecast', device.name);
// Sets OSX selected input and output audio devices to Soundflower
LocalSourceSwitcher.switchSource({
output: 'Soundflower (2ch)',
input: 'Soundflower (2ch)'
});
if (!device.controls) {
device.controls = new ChromeCast(device);
}
device.controls.play(streamingAddress, onStreamingUpdateUI.bind({device: device}));
}));
}
break;
case DeviceMatcher.TYPES.UPNP:
if (DeviceMatcher.isSonos(device)) {
devicesAdded.push(device);
deviceListMenu.append(MenuFactory.sonosDeviceItem(device, function onClicked() {
logger.debug('TODO Sonos');
// TODO on click integrate with sonos
}));
}
else if (DeviceMatcher.isJongo(device)) {
devicesAdded.push(device);
deviceListMenu.append(MenuFactory.jongoDeviceItem(device, function onClicked() {
logger.info('Attempting to play to Jongo device', device.name);
// Sets OSX selected input and output audio devices to Soundflower
LocalSourceSwitcher.switchSource({
output: 'Soundflower (2ch)',
input: 'Soundflower (2ch)'
});
if (!device.controls) {
device.controls = new JongoSpeaker(device);
}
device.controls.play(streamingAddress, onStreamingUpdateUI.bind({device: device}));
}));
}
break;
default:
logger.error('Unknown recognised device found', logger.level === 'verbose' ? device : device.name);
}
// Reset the menu items
mb.tray.setContextMenu(menu);
});
// Stream Options
var streamMenu = new Menu();
streamMenu.append(new MenuItem({
label: 'OSX Output (default)',
click: function () {
LocalSourceSwitcher.switchSource({
output: 'Soundflower (2ch)',
input: 'Soundflower (2ch)'
});
NotificationService.notify({
title: 'Audio Source Switched',
message: 'OSX Audio via Soundflower'
});
}
}));
streamMenu.append(new MenuItem({
label: 'Internal Microphone',
click: function () {
LocalSourceSwitcher.switchSource({
output: 'Soundflower (2ch)',
input: 'Internal Microphone'
});
NotificationService.notify({
title: 'Audio Source Switched',
message: 'Internal Microphone via Soundflower'
});
}
}));
streamMenu.append(MenuFactory.separator());
streamMenu.append(MenuFactory.aboutStreamFeature());
// Streaming Menu
menu.append(MenuFactory.separator());
menu.append(MenuFactory.steamMenu(streamMenu));
menu.append(MenuFactory.separator());
//Clicking this option stops casting audio to Chromecast
menu.append(new MenuItem({
label: 'Stop casting',
enabled: true, // default disabled as not initially playing
click: function () {
// Attempt to stop all controls
attemptToStopAllDevices();
// Clean up playing speaker icon
deviceListMenu.items.forEach(MenuFactory.removeSpeaker);
// Re-Enable all devices until further notice
for (var j = 0; j < deviceListMenu.items.length; j++) {
deviceListMenu.items[j].enabled = true;
}
// Disable 'Stop Casting' item
menu.items[2].enabled = false;
// Switch tray icon
mb.tray.setImage(path.join(__dirname, 'not-castingTemplate.png'));
LocalSourceSwitcher.resetOriginSource();
}
}));
var disableAllItems = function (item) {
item.enabled = false
};
var attemptToStopAllDevices = function () {
devicesAdded.forEach(function (device) {
if (_.has(device, 'controls') && _.isFunction(device.controls.stop)) {
device.controls.stop(function (err, result) {
// do something...
});
}
});
};
var setSpeakIcon = function (item) {
if (item.label === this.device.name) {
MenuFactory.setSpeaker(item);
} else {
MenuFactory.removeSpeaker(item);
}
};
var onQuitHandler = function () {
mb.tray.setImage(path.join(__dirname, 'not-castingTemplate.png'));
attemptToStopAllDevices();
LocalSoundStreamer.stopStream();
LocalSourceSwitcher.resetOriginSource();
mb.app.quit();
};
var onStreamingUpdateUI = function () {
//Disables all devices until further stop
deviceListMenu.items.forEach(disableAllItems);
// set speak icon when playing
deviceListMenu.items.forEach(setSpeakIcon.bind({device: this.device}));
// Enable 'Stop Casting' item
menu.items[2].enabled = true;
// Changes tray icon to "Casting"
mb.tray.setImage(path.join(__dirname, 'castingTemplate.png'));
};
// About
menu.append(MenuFactory.about());
// Quit
menu.append(MenuFactory.quit(onQuitHandler));
// CMD + C death
mb.app.on('quit', onQuitHandler);
// Set the menu items
mb.tray.setContextMenu(menu);
});