forked from blackjackshellac/kitchenTimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_manager.js
More file actions
411 lines (366 loc) · 13.6 KB
/
Copy pathaudio_manager.js
File metadata and controls
411 lines (366 loc) · 13.6 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* taskTimer AudioManager
*
* Shared GStreamer-based audio playback for alarm sounds. This module is
* designed to work both in the GNOME Shell extension and in the standalone
* GTK application by avoiding direct dependence on Shell-only globals and
* by locating the extension directory similarly to settings.js.
*/
imports.gi.versions.Gst = '1.0';
const { GLib, Gst } = imports.gi;
let Me = null;
try {
const ExtensionUtils = imports.misc.extensionUtils;
Me = ExtensionUtils.getCurrentExtension();
} catch (e) {
// Standalone gjs fallback: assume we're running from the project root
// and that the extension code lives in a taskTimer@CryptoD subdirectory.
Me = {
path: GLib.build_filenamev([GLib.get_current_dir(), 'taskTimer@CryptoD']),
imports: imports['taskTimer@CryptoD'],
};
}
const Logger = Me.imports.logger.Logger;
/**
* Small helper that manages one GStreamer playbin instance with loop
* semantics suitable for timer alarms. AudioManager maintains a collection
* of these players keyed by an arbitrary id (typically timer.id).
*/
class AudioPlayer {
constructor(params) {
this._id = params.id;
this._logger = params.logger;
this._settings = params.settings;
this._soundFile = params.soundFile;
this._loopsTarget = params.loops; // 0 => infinite
this._player = null;
this._bus = null;
this._uri = null;
this._loopsDone = 0;
this._isPlaying = false;
this._destroyed = false;
/** Set when GStreamer reports ERROR (e.g. no audio sink); avoids tight retry loops. */
this._fatalPlaybackError = false;
}
/**
* Resolve a candidate basename inside an AppImage bundle when running
* under $APPDIR. This prefers paths under $APPDIR/taskTimer@CryptoD
* and falls back to $APPDIR itself.
*/
_resolveInAppDir(basename) {
const appDir = GLib.getenv('APPDIR');
if (!appDir || !basename || basename.length === 0) {
return null;
}
const candidates = [
GLib.build_filenamev([appDir, 'taskTimer@CryptoD', basename]),
GLib.build_filenamev([appDir, basename]),
];
for (let i = 0; i < candidates.length; i++) {
const candidate = candidates[i];
if (GLib.file_test(candidate, GLib.FileTest.EXISTS)) {
return candidate;
}
}
return null;
}
/**
* Build a file:// URI for the configured sound file.
*
* Resolution order:
* 1. User-selected absolute path, if it exists.
* 2. If running as an AppImage ($APPDIR), look for a bundled file
* under $APPDIR/taskTimer@CryptoD/<basename> or $APPDIR/<basename>.
* 3. Fallback to a file in the extension directory (Me.path) using
* the default sound-file setting as the basename when available.
*/
_buildUri() {
let path = this._soundFile;
if (!path || path.length === 0) {
return null;
}
// 1. If user configured an absolute path and it exists, prefer it.
if (GLib.path_is_absolute(path) && GLib.file_test(path, GLib.FileTest.EXISTS)) {
return 'file://' + path;
}
// 2. Determine a reasonable basename, optionally using the default.
let base = GLib.path_get_basename(path);
// Prefer the Settings wrapper value (works under JSON provider) rather
// than Gio.Settings default_value (not available in standalone).
try {
const configured = this._settings && this._settings.sound_file ? String(this._settings.sound_file) : '';
if (configured) {
const defBase = GLib.path_get_basename(configured);
if (defBase && (!base || base === path)) {
base = defBase;
}
}
} catch (_e) {
// ignore; we'll fall back to the original basename
}
// 2a. If running inside an AppImage, try bundled locations first.
const appDirPath = this._resolveInAppDir(base);
if (appDirPath) {
return 'file://' + appDirPath;
}
// 3. Fallback to the extension directory (extension or unpacked tree).
const tryBasenames = [];
tryBasenames.push(base);
// Backward-compatible mapping for older default name.
if (base === 'tasktimer-default.ogg') {
tryBasenames.push('kitchen_timer.ogg');
}
for (let i = 0; i < tryBasenames.length; i++) {
const extPath = GLib.build_filenamev([Me.path, tryBasenames[i]]);
if (GLib.file_test(extPath, GLib.FileTest.EXISTS)) {
return 'file://' + extPath;
}
}
this._logger && this._logger.error('AudioManager: could not resolve sound file for %s (base=%s)', this._id, base);
return null;
}
_ensurePlayer() {
if (this._player) {
return;
}
this._uri = this._buildUri();
if (!this._uri) {
this._logger && this._logger.error('AudioManager: no valid sound URI; skipping playback');
return;
}
this._logger && this._logger.debug('AudioManager: init player id=%s uri=%s', this._id, this._uri);
Gst.init(null);
this._player = Gst.ElementFactory.make('playbin', 'tasktimer-player-' + this._id);
if (!this._player) {
this._logger && this._logger.error('AudioManager: failed to create playbin element');
return;
}
this._bus = this._player.get_bus();
if (this._bus) {
this._bus.add_signal_watch();
this._bus.connect('message', (bus, message) => {
if (!message || !this._player) {
return;
}
const type = message.type;
if (type === Gst.MessageType.ERROR) {
try {
const [err, dbg] = message.parse_error();
const em = err && err.message ? err.message : String(err);
this._logger && this._logger.error(
'AudioManager: GStreamer error for %s (no output device, missing codec, or broken pipeline?): %s — %s',
this._id, em, dbg || '');
} catch (_parseE) {
this._logger && this._logger.error('AudioManager: GStreamer ERROR for %s (see Gst debug for details)', this._id);
}
try {
this._player.set_state(Gst.State.READY);
} catch (_st) {}
this._isPlaying = false;
this._fatalPlaybackError = true;
return;
}
if (type === Gst.MessageType.EOS) {
// IMPORTANT: to reuse the player, set state to READY
this._player.set_state(Gst.State.READY);
this._isPlaying = false;
if (this._destroyed) {
this._logger && this._logger.debug('AudioManager: player %s destroyed, stopping playback', this._id);
return;
}
if (this._loopsTarget === 0 || this._loopsDone < this._loopsTarget) {
this._startPlayback();
} else {
this._logger && this._logger.debug('AudioManager: player %s reached loop limit', this._id);
}
}
});
}
}
_startPlayback() {
if (!this._player) {
return false;
}
if (this._fatalPlaybackError) {
return false;
}
if (this._isPlaying) {
return true;
}
if (this._loopsTarget > 0 && this._loopsDone >= this._loopsTarget) {
return false;
}
try {
this._player.set_property('uri', this._uri);
this._player.set_state(Gst.State.PLAYING);
this._loopsDone++;
this._isPlaying = true;
return true;
} catch (e) {
this._logger && this._logger.error('AudioManager: error playing sound for %s: %s', this._id, e.message);
this._isPlaying = false;
return false;
}
}
/**
* Begin or resume looping playback for this player.
*/
play() {
this._destroyed = false;
this._fatalPlaybackError = false;
this._loopsDone = 0;
this._ensurePlayer();
return this._startPlayback();
}
/**
* Stop playback and mark the player as destroyed so the bus handler
* does not restart it.
*/
stop() {
this._destroyed = true;
if (this._player) {
try {
// For normal stops we set READY so we can quickly replay.
this._player.set_state(Gst.State.READY);
} catch (e) {
this._logger && this._logger.warn('AudioManager: failed to stop player %s: %s', this._id, e.message);
}
}
this._isPlaying = false;
}
/**
* Fully tear down the player for application shutdown. This sets the
* element to NULL so GStreamer can clean up sinks/tees without emitting
* criticals on disposal.
*/
destroy() {
this._destroyed = true;
this._isPlaying = false;
if (this._bus) {
try {
if (typeof this._bus.remove_signal_watch === 'function') {
this._bus.remove_signal_watch();
}
} catch (_e) {}
this._bus = null;
}
if (this._player) {
try {
this._player.set_state(Gst.State.NULL);
} catch (e) {
this._logger && this._logger.warn('AudioManager: failed to destroy player %s: %s', this._id, e.message);
}
this._player = null;
}
}
}
/**
* AudioManager orchestrates AudioPlayer instances keyed by id (usually a
* timer id). Call playTimerAlarm() when a timer expires and stopTimerAlarm()
* when the timer is acknowledged/dismissed.
*/
var AudioManager = class AudioManager {
/**
* @param {Object} params
* - settings: Settings instance (used for defaults like sound-file)
* - logger: optional Logger instance; a local one is created if omitted
*/
constructor(params = {}) {
this._settings = params.settings || null;
this.logger = params.logger || new Logger('kt audio', this._settings);
this._players = new Map(); // id -> AudioPlayer
}
/**
* Compute loop count for a timer based on the persist_alarm flag and
* global sound_loops setting. When sound_loops is 0 and notifications
* are disabled, we fall back to 2 loops to avoid infinite playback
* with no visible notification.
*
* @param {Object} timer - Timer/TimerCore instance with persist_alarm
* @returns {number} loops (0 for infinite)
*/
_loopsForTimer(timer) {
if (!this._settings) {
return 0;
}
let loops = timer && timer.persist_alarm ? 0 : this._settings.sound_loops;
if (this._settings.notification === false && loops === 0) {
loops = 2;
}
return loops;
}
/**
* Play an alarm sound for the given timer, honoring configuration
* like sound_loops and persist_alarm.
*
* @param {Object} timer - Timer/TimerCore with id, persist_alarm
* @param {Object} [options]
* - soundFile: override path; defaults to settings.sound_file
*/
playTimerAlarm(timer, options = {}) {
if (!timer || !timer.id) {
return;
}
if (!this._settings || !this._settings.play_sound) {
return;
}
const id = String(timer.id);
const soundFile = options.soundFile || (this._settings && this._settings.sound_file);
if (!soundFile) {
this.logger.warn('AudioManager: no sound file configured; skipping playback for %s', id);
return;
}
const loops = this._loopsForTimer(timer);
let player = this._players.get(id);
if (!player) {
player = new AudioPlayer({
id,
logger: this.logger,
settings: this._settings,
soundFile,
loops,
});
this._players.set(id, player);
}
player._soundFile = soundFile;
player._loopsTarget = loops;
player.play();
}
/**
* Stop any active alarm playback for the given timer.
*
* @param {Object|string} timerOrId - timer object or plain id string
*/
stopTimerAlarm(timerOrId) {
const id = typeof timerOrId === 'string'
? timerOrId
: (timerOrId && timerOrId.id ? String(timerOrId.id) : null);
if (!id) {
return;
}
const player = this._players.get(id);
if (!player) {
return;
}
player.stop();
}
/**
* Stop and release all players. Call from application shutdown to avoid
* GStreamer criticals about disposing non-NULL elements.
*/
shutdown() {
try {
for (const [_id, player] of this._players.entries()) {
try {
if (player && typeof player.destroy === 'function') {
player.destroy();
} else if (player && typeof player.stop === 'function') {
player.stop();
}
} catch (_e) {}
}
} finally {
this._players.clear();
}
}
};