-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
2178 lines (1812 loc) · 85.3 KB
/
api.php
File metadata and controls
2178 lines (1812 loc) · 85.3 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
include_once("/opt/fpp/www/common.php");
$pluginName = "fpp-plugin-BackgroundMusic";
$pluginConfigFile = (isset($settings['configDirectory']) ? $settings['configDirectory'] : '/home/fpp/media/config') . "/plugin." . $pluginName;
function getEndpointsfpppluginBackgroundMusic() {
$result = array();
$ep = array(
'method' => 'GET',
'endpoint' => 'version',
'callback' => 'fppBackgroundMusicVersion');
array_push($result, $ep);
$ep = array(
'method' => 'GET',
'endpoint' => 'status',
'callback' => 'fppBackgroundMusicStatus');
array_push($result, $ep);
$ep = array(
'method' => 'GET',
'endpoint' => 'headerIndicator',
'callback' => 'fppBackgroundMusicHeaderIndicator');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'start-background',
'callback' => 'fppBackgroundMusicStartBackground');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'stop-background',
'callback' => 'fppBackgroundMusicStopBackground');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'pause-background',
'callback' => 'fppBackgroundMusicPauseBackground');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'resume-background',
'callback' => 'fppBackgroundMusicResumeBackground');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'next-track',
'callback' => 'fppBackgroundMusicNextTrack');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'previous-track',
'callback' => 'fppBackgroundMusicPreviousTrack');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'jump-to-track',
'callback' => 'fppBackgroundMusicJumpToTrack');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'start-show',
'callback' => 'fppBackgroundMusicStartShow');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'set-volume',
'callback' => 'fppBackgroundMusicSetVolume');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'save-settings',
'callback' => 'fppBackgroundMusicSaveSettings');
array_push($result, $ep);
$ep = array(
'method' => 'GET',
'endpoint' => 'playlist-details',
'callback' => 'fppBackgroundMusicPlaylistDetails');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'play-announcement',
'callback' => 'fppBackgroundMusicPlayAnnouncement');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'stop-announcement',
'callback' => 'fppBackgroundMusicStopAnnouncement');
array_push($result, $ep);
$ep = array(
'method' => 'GET',
'endpoint' => 'psa-status',
'callback' => 'fppBackgroundMusicPSAStatus');
array_push($result, $ep);
$ep = array(
'method' => 'GET',
'endpoint' => 'check-update',
'callback' => 'fppBackgroundMusicCheckUpdate');
array_push($result, $ep);
$ep = array(
'method' => 'GET',
'endpoint' => 'get-commit-history',
'callback' => 'fppBackgroundMusicGetCommitHistory');
array_push($result, $ep);
$ep = array(
'method' => 'GET',
'endpoint' => 'get-log',
'callback' => 'fppBackgroundMusicGetLog');
array_push($result, $ep);
$ep = array(
'method' => 'GET',
'endpoint' => 'download-log',
'callback' => 'fppBackgroundMusicDownloadLog');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'reorder-playlist',
'callback' => 'fppBackgroundMusicReorderPlaylist');
array_push($result, $ep);
$ep = array(
'method' => 'GET',
'endpoint' => 'tts-status',
'callback' => 'fppBackgroundMusicTTSStatus');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'install-tts',
'callback' => 'fppBackgroundMusicInstallTTS');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'generate-tts',
'callback' => 'fppBackgroundMusicGenerateTTS');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'play-tts',
'callback' => 'fppBackgroundMusicPlayTTS');
array_push($result, $ep);
$ep = array(
'method' => 'GET',
'endpoint' => 'tts-voices',
'callback' => 'fppBackgroundMusicTTSVoices');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'install-voice',
'callback' => 'fppBackgroundMusicInstallVoice');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'set-default-voice',
'callback' => 'fppBackgroundMusicSetDefaultVoice');
array_push($result, $ep);
$ep = array(
'method' => 'POST',
'endpoint' => 'delete-voice',
'callback' => 'fppBackgroundMusicDeleteVoice');
array_push($result, $ep);
$ep = array(
'method' => 'GET',
'endpoint' => 'system-diagnostics',
'callback' => 'fppBackgroundMusicSystemDiagnostics');
array_push($result, $ep);
return $result;
}
// GET /api/plugin/fpp-plugin-BackgroundMusic/version
function fppBackgroundMusicVersion() {
$result = array();
$result['version'] = 'fpp-BackgroundMusic v1.0';
return json($result);
}
// GET /api/plugin/fpp-plugin-BackgroundMusic/status
function fppBackgroundMusicStatus() {
global $settings;
$pluginConfigFile = $settings['configDirectory'] . "/plugin.fpp-plugin-BackgroundMusic";
// Load plugin config with error checking
$pluginSettings = array();
if (file_exists($pluginConfigFile)){
// Check if file is readable
if (!is_readable($pluginConfigFile)) {
error_log("BackgroundMusic Plugin: Config file exists but is not readable: $pluginConfigFile");
// Try to fix permissions
@chmod($pluginConfigFile, 0644);
}
// Try to parse the file
$pluginSettings = @parse_ini_file($pluginConfigFile);
if ($pluginSettings === false) {
error_log("BackgroundMusic Plugin: Failed to parse config file: $pluginConfigFile");
$pluginSettings = array();
}
} else {
error_log("BackgroundMusic Plugin: Config file does not exist: $pluginConfigFile");
}
// Get current brightness
$brightness = getSetting('brightness');
if ($brightness === false || $brightness === '') {
$brightness = 100;
}
// Check running playlists
$status = GetCurrentStatus();
$currentPlaylist = isset($status['current_playlist']['playlist']) ? $status['current_playlist']['playlist'] : '';
$fppStatus = isset($status['status_name']) ? $status['status_name'] : 'unknown';
$currentSequence = isset($status['current_sequence']) ? $status['current_sequence'] : '';
$backgroundMusicPlaylist = isset($pluginSettings['BackgroundMusicPlaylist']) ? $pluginSettings['BackgroundMusicPlaylist'] : '';
$showPlaylist = isset($pluginSettings['ShowPlaylist']) ? $pluginSettings['ShowPlaylist'] : '';
$returnToPreShow = isset($pluginSettings['ReturnToPreShow']) ? $pluginSettings['ReturnToPreShow'] : '1';
$shuffleMusic = isset($pluginSettings['ShuffleMusic']) ? $pluginSettings['ShuffleMusic'] : '0';
$volumeLevel = isset($pluginSettings['VolumeLevel']) ? intval($pluginSettings['VolumeLevel']) : 70;
$backgroundMusicVolume = isset($pluginSettings['BackgroundMusicVolume']) ? intval($pluginSettings['BackgroundMusicVolume']) : $volumeLevel;
$showPlaylistVolume = isset($pluginSettings['ShowPlaylistVolume']) ? intval($pluginSettings['ShowPlaylistVolume']) : 100;
$postShowBackgroundVolume = isset($pluginSettings['PostShowBackgroundVolume']) ? intval($pluginSettings['PostShowBackgroundVolume']) : $backgroundMusicVolume;
// Check if background music player is running (independent of FPP playlists)
$pidFile = '/tmp/background_music_player.pid';
$backgroundMusicRunning = false;
if (file_exists($pidFile)) {
$pid = trim(file_get_contents($pidFile));
// Check if process is actually running
exec("ps -p $pid > /dev/null 2>&1", $output, $returnCode);
$backgroundMusicRunning = ($returnCode === 0);
}
// Get current track information if player is running
$currentTrack = '';
$trackDuration = 0;
$trackElapsed = 0;
$trackProgress = 0;
$playbackState = 'stopped';
$currentTrackNumber = 0;
$totalTracks = 0;
$streamSource = false;
$streamTitle = '';
$streamArtist = '';
if ($backgroundMusicRunning) {
$statusFile = '/tmp/bg_music_status.txt';
if (file_exists($statusFile)) {
// Read status file line by line to handle special characters properly
$statusLines = file($statusFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$statusData = array();
foreach ($statusLines as $line) {
// Split on first = only
$pos = strpos($line, '=');
if ($pos !== false) {
$key = substr($line, 0, $pos);
$value = substr($line, $pos + 1);
$statusData[$key] = $value;
}
}
if ($statusData) {
// Check if this is a stream source
$streamSource = isset($statusData['source']) && $statusData['source'] === 'stream';
if ($streamSource) {
// Get stream metadata (ICY info)
$streamTitle = isset($statusData['stream_title']) ? $statusData['stream_title'] : '';
$streamArtist = isset($statusData['stream_artist']) ? $statusData['stream_artist'] : '';
} else {
// Only populate track info for playlist mode, not for streams
$currentTrack = isset($statusData['filename']) ? $statusData['filename'] : '';
$trackDuration = isset($statusData['duration']) ? intval($statusData['duration']) : 0;
$trackElapsed = isset($statusData['elapsed']) ? intval($statusData['elapsed']) : 0;
$trackProgress = isset($statusData['progress']) ? intval($statusData['progress']) : 0;
$currentTrackNumber = isset($statusData['track_number']) ? intval($statusData['track_number']) : 0;
$totalTracks = isset($statusData['total_tracks']) ? intval($statusData['total_tracks']) : 0;
}
$playbackState = isset($statusData['state']) ? $statusData['state'] : 'playing';
}
}
}
// Check if a show is running
// A show is considered running if:
// 1. The configured show playlist is currently running in FPP, OR
// 2. The background music playlist is running (handled separately)
// NOTE: Other playlists (like lights-only sequences) are allowed to run alongside background music
$showRunning = false;
if ($currentPlaylist !== '' && $currentPlaylist !== $backgroundMusicPlaylist) {
// Only consider it a "show" if it matches the configured show playlist
// This allows lights-only sequences to run without blocking background music
if (!empty($showPlaylist) && $currentPlaylist === $showPlaylist) {
$showRunning = true;
}
}
// Check if fpp-brightness plugin is installed (required for transitions)
$brightnessPluginInstalled = file_exists('/home/fpp/media/plugins/fpp-brightness/libfpp-brightness.so');
// Get current bgmplayer volume from PipeWire stream (query actual volume)
$bgmplayerVolume = $backgroundMusicVolume; // default from config
// Query PipeWire for actual stream volume
// Note: We query all running BackgroundMusic streams and take the first result.
// The player sets bgmplayer.role metadata to distinguish "main" vs "crossfade" streams,
// but querying metadata requires per-stream lookups which is slower.
// Since volume should be consistent across streams, we rely on the most recent stream.
$fppUid = trim(shell_exec('id -u fpp'));
$pwDumpCmd = "sudo -u fpp XDG_RUNTIME_DIR=/run/user/{$fppUid} pw-dump 2>/dev/null";
$jqQuery = '.[] | select(.info.props["media.name"]? // "" | startswith("BackgroundMusic")) | select(.type == "PipeWire:Interface:Node") | select(.info.state? == "running") | .info.params.Props[0].volume';
$streamVolume = trim(shell_exec("{$pwDumpCmd} | jq -r '{$jqQuery}' 2>/dev/null | tail -1"));
if (!empty($streamVolume) && $streamVolume !== 'null' && is_numeric($streamVolume)) {
// Convert from 0.0-1.0 to 0-100
$bgmplayerVolume = intval($streamVolume * 100);
}
$result = array(
'backgroundMusicRunning' => $backgroundMusicRunning,
'showRunning' => $showRunning,
'streamSource' => $streamSource,
'brightness' => intval($brightness),
'brightnessPluginInstalled' => $brightnessPluginInstalled,
'currentPlaylist' => $currentPlaylist,
'fppStatus' => $fppStatus,
'currentSequence' => $currentSequence,
'currentTrack' => $currentTrack,
'trackDuration' => $trackDuration,
'trackElapsed' => $trackElapsed,
'trackProgress' => $trackProgress,
'playbackState' => $playbackState,
'currentTrackNumber' => $currentTrackNumber,
'totalTracks' => $totalTracks,
'streamTitle' => $streamTitle,
'streamArtist' => $streamArtist,
'volume' => $bgmplayerVolume,
'config' => array(
'backgroundMusicSource' => isset($pluginSettings['BackgroundMusicSource']) ? $pluginSettings['BackgroundMusicSource'] : 'playlist',
'backgroundMusicPlaylist' => $backgroundMusicPlaylist,
'backgroundMusicStreamURL' => isset($pluginSettings['BackgroundMusicStreamURL']) ? $pluginSettings['BackgroundMusicStreamURL'] : '',
'showPlaylist' => $showPlaylist,
'fadeTime' => isset($pluginSettings['FadeTime']) ? $pluginSettings['FadeTime'] : 5,
'blackoutTime' => isset($pluginSettings['BlackoutTime']) ? $pluginSettings['BlackoutTime'] : 2,
'returnToPreShow' => $returnToPreShow,
'postShowDelay' => isset($pluginSettings['PostShowDelay']) ? $pluginSettings['PostShowDelay'] : 0,
'shuffleMusic' => $shuffleMusic,
'enableCrossfade' => isset($pluginSettings['EnableCrossfade']) && $pluginSettings['EnableCrossfade'] == '1' ? true : false,
'crossfadeDuration' => isset($pluginSettings['CrossfadeDuration']) ? floatval($pluginSettings['CrossfadeDuration']) : 3,
'volumeLevel' => $volumeLevel,
'backgroundMusicVolume' => $backgroundMusicVolume,
'showPlaylistVolume' => $showPlaylistVolume,
'postShowBackgroundVolume' => $postShowBackgroundVolume,
'PSAAnnouncementVolume' => isset($pluginSettings['PSAAnnouncementVolume']) ? $pluginSettings['PSAAnnouncementVolume'] : '90',
'PSADuckVolume' => isset($pluginSettings['PSADuckVolume']) ? $pluginSettings['PSADuckVolume'] : '30'
)
);
// Dynamically add all PSA button configurations (1-20)
for ($i = 1; $i <= 20; $i++) {
$result['config']['PSAButton' . $i . 'Label'] = isset($pluginSettings['PSAButton' . $i . 'Label']) ? $pluginSettings['PSAButton' . $i . 'Label'] : '';
$result['config']['PSAButton' . $i . 'File'] = isset($pluginSettings['PSAButton' . $i . 'File']) ? $pluginSettings['PSAButton' . $i . 'File'] : '';
}
return json($result);
}
// GET /api/plugin/fpp-plugin-BackgroundMusic/headerIndicator
function fppBackgroundMusicHeaderIndicator() {
global $settings, $pluginConfigFile;
// Get plugin status
$pidFile = '/tmp/background_music_start.pid';
$backgroundMusicRunning = false;
if (file_exists($pidFile)) {
$pid = trim(file_get_contents($pidFile));
exec("ps -p $pid > /dev/null 2>&1", $output, $returnCode);
$backgroundMusicRunning = ($returnCode === 0);
}
// Only return indicator if background music is running
if (!$backgroundMusicRunning) {
return json(null);
}
// Get current track info for tooltip
$currentTrack = '';
$streamSource = false;
$streamTitle = '';
$statusFile = '/tmp/bg_music_status.txt';
if (file_exists($statusFile)) {
$statusLines = file($statusFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$statusData = array();
foreach ($statusLines as $line) {
$pos = strpos($line, '=');
if ($pos !== false) {
$key = substr($line, 0, $pos);
$value = substr($line, $pos + 1);
$statusData[$key] = $value;
}
}
if ($statusData) {
$streamSource = isset($statusData['source']) && $statusData['source'] === 'stream';
if ($streamSource) {
$streamTitle = isset($statusData['stream_title']) ? $statusData['stream_title'] : '';
} else {
$currentTrack = isset($statusData['filename']) ? $statusData['filename'] : '';
}
}
}
// Build tooltip text
$tooltip = 'Background Music Playing';
if ($streamSource && !empty($streamTitle)) {
$tooltip = 'Background Music: ' . $streamTitle;
} elseif (!$streamSource && !empty($currentTrack)) {
$tooltip = 'Background Music: ' . $currentTrack;
}
// Return indicator configuration
$indicator = array(
'visible' => true,
'icon' => 'fa-music',
'color' => '#8b5cf6',
'tooltip' => $tooltip,
'link' => '/plugin.php?_menu=status&plugin=fpp-plugin-BackgroundMusic&page=backgroundmusic.php',
'animate' => 'pulse'
);
return json($indicator);
}
// POST /api/plugin/fpp-plugin-BackgroundMusic/start-background
function fppBackgroundMusicStartBackground() {
global $settings;
$pluginConfigFile = $settings['configDirectory'] . "/plugin.fpp-plugin-BackgroundMusic";
if (file_exists($pluginConfigFile)){
$pluginSettings = parse_ini_file($pluginConfigFile);
} else {
return json(array('status' => 'ERROR', 'message' => 'Plugin not configured'));
}
// Check source type
$source = isset($pluginSettings['BackgroundMusicSource']) ? $pluginSettings['BackgroundMusicSource'] : 'playlist';
if ($source === 'stream') {
$streamURL = isset($pluginSettings['BackgroundMusicStreamURL']) ? $pluginSettings['BackgroundMusicStreamURL'] : '';
if (empty($streamURL)) {
return json(array('status' => 'ERROR', 'message' => 'Stream URL not configured'));
}
} else {
$backgroundMusicPlaylist = isset($pluginSettings['BackgroundMusicPlaylist']) ? $pluginSettings['BackgroundMusicPlaylist'] : '';
if (empty($backgroundMusicPlaylist)) {
return json(array('status' => 'ERROR', 'message' => 'Background music playlist not configured'));
}
}
// Start background music using independent player (not FPP playlist system)
// This allows music to play while FPP scheduler controls the sequence playlist
// Run in background to avoid HTTP timeout during PipeWire initialization
$scriptPath = dirname(__FILE__) . '/scripts/background_music_player.sh';
$logFile = '/tmp/background_music_start.log';
$pidFile = '/tmp/background_music_start.pid';
// Kill any previous stuck startup processes
if (file_exists($pidFile)) {
$oldPid = trim(file_get_contents($pidFile));
if (!empty($oldPid) && is_numeric($oldPid)) {
exec("ps -p " . escapeshellarg($oldPid) . " > /dev/null 2>&1 && kill " . escapeshellarg($oldPid) . " 2>/dev/null");
}
}
// Start in background using wrapper script
// The wrapper handles proper detachment from Apache
$wrapperScript = dirname(__FILE__) . '/scripts/start_background_wrapper.sh';
$cmd = "sudo " . escapeshellarg($wrapperScript);
exec($cmd);
// Give script time to start PipeWire, configure audio, and write PID file
// Script can take 3-4 seconds when starting PipeWire
sleep(5);
file_put_contents('/tmp/api_debug.log', "After sleep, checking PID file: $pidFile\n", FILE_APPEND);
// Check if the process started successfully
if (file_exists($pidFile)) {
$pid = trim(file_get_contents($pidFile));
if (!empty($pid) && is_numeric($pid)) {
exec("ps -p " . escapeshellarg($pid) . " > /dev/null 2>&1", $psOutput, $psReturn);
if ($psReturn === 0) {
return json(array('status' => 'OK', 'message' => 'Background music starting...', 'details' => 'Check /tmp/background_music_start.log for progress'));
}
}
}
// If we get here, something went wrong
$errorLog = file_exists($logFile) ? file_get_contents($logFile) : 'No log file found';
return json(array('status' => 'ERROR', 'message' => 'Failed to start background music', 'details' => $errorLog));
}
// POST /api/plugin/fpp-plugin-BackgroundMusic/stop-background
function fppBackgroundMusicStopBackground() {
// Stop the independent background music player
$scriptPath = dirname(__FILE__) . '/scripts/background_music_player.sh';
$output = array();
$returnCode = 0;
exec("sudo /bin/bash " . escapeshellarg($scriptPath) . " stop 2>&1", $output, $returnCode);
// Also stop any FPP playlist that might be playing background music
// Check if a background music playlist is currently playing
global $settings;
$pluginConfigFile = $settings['configDirectory'] . "/plugin.fpp-plugin-BackgroundMusic";
if (file_exists($pluginConfigFile)) {
$pluginSettings = parse_ini_file($pluginConfigFile);
$bgMusicPlaylist = isset($pluginSettings['BackgroundMusicPlaylist']) ? $pluginSettings['BackgroundMusicPlaylist'] : '';
// Get current FPP status
$fppStatus = @file_get_contents('http://localhost/api/fppd/status');
if ($fppStatus) {
$statusData = json_decode($fppStatus, true);
$currentPlaylist = isset($statusData['current_playlist']['playlist']) ? $statusData['current_playlist']['playlist'] : '';
// If the current FPP playlist matches our background music playlist, stop it
if (!empty($bgMusicPlaylist) && $currentPlaylist === $bgMusicPlaylist) {
// Stop the FPP playlist
@file_get_contents('http://localhost/api/playlists/stop');
}
}
}
return json(array('status' => 'OK', 'message' => 'Background music stopped'));
}
// POST /api/plugin/fpp-plugin-BackgroundMusic/pause-background
function fppBackgroundMusicPauseBackground() {
$scriptPath = dirname(__FILE__) . '/scripts/background_music_player.sh';
$output = array();
$returnCode = 0;
exec("sudo /bin/bash " . escapeshellarg($scriptPath) . " pause 2>&1", $output, $returnCode);
if ($returnCode === 0) {
return json(array('status' => 'OK', 'message' => 'Background music paused'));
} else {
return json(array('status' => 'ERROR', 'message' => 'Failed to pause background music', 'details' => implode("\n", $output)));
}
}
// POST /api/plugin/fpp-plugin-BackgroundMusic/resume-background
function fppBackgroundMusicResumeBackground() {
$scriptPath = dirname(__FILE__) . '/scripts/background_music_player.sh';
$output = array();
$returnCode = 0;
exec("sudo /bin/bash " . escapeshellarg($scriptPath) . " resume 2>&1", $output, $returnCode);
if ($returnCode === 0) {
return json(array('status' => 'OK', 'message' => 'Background music resumed'));
} else {
return json(array('status' => 'ERROR', 'message' => 'Failed to resume background music', 'details' => implode("\n", $output)));
}
}
// POST /api/plugin/fpp-plugin-BackgroundMusic/next-track
function fppBackgroundMusicNextTrack() {
$scriptPath = dirname(__FILE__) . '/scripts/background_music_player.sh';
$output = array();
$returnCode = 0;
exec("sudo /bin/bash " . escapeshellarg($scriptPath) . " next 2>&1", $output, $returnCode);
if ($returnCode === 0) {
return json(array('status' => 'OK', 'message' => 'Skipped to next track'));
} else {
return json(array('status' => 'ERROR', 'message' => 'Failed to skip track', 'details' => implode("\n", $output)));
}
}
// POST /api/plugin/fpp-plugin-BackgroundMusic/previous-track
function fppBackgroundMusicPreviousTrack() {
$scriptPath = dirname(__FILE__) . '/scripts/background_music_player.sh';
$output = array();
$returnCode = 0;
exec("sudo /bin/bash " . escapeshellarg($scriptPath) . " previous 2>&1", $output, $returnCode);
if ($returnCode === 0) {
return json(array('status' => 'OK', 'message' => 'Went to previous track'));
} else {
return json(array('status' => 'ERROR', 'message' => 'Failed to go to previous track', 'details' => implode("\n", $output)));
}
}
// POST /api/plugin/fpp-plugin-BackgroundMusic/jump-to-track
function fppBackgroundMusicJumpToTrack() {
$input = json_decode(file_get_contents('php://input'), true);
$trackNumber = isset($input['trackNumber']) ? intval($input['trackNumber']) : 0;
if ($trackNumber < 1) {
return json(array('status' => 'ERROR', 'message' => 'Invalid track number'));
}
$scriptPath = dirname(__FILE__) . '/scripts/background_music_player.sh';
$output = array();
$returnCode = 0;
exec("sudo /bin/bash " . escapeshellarg($scriptPath) . " jump " . escapeshellarg($trackNumber) . " 2>&1", $output, $returnCode);
if ($returnCode === 0) {
return json(array('status' => 'OK', 'message' => 'Jumped to track ' . $trackNumber));
} else {
return json(array('status' => 'ERROR', 'message' => 'Failed to jump to track', 'details' => implode("\n", $output)));
}
}
// POST /api/plugin/fpp-plugin-BackgroundMusic/start-show
function fppBackgroundMusicStartShow() {
global $settings;
$pluginConfigFile = $settings['configDirectory'] . "/plugin.fpp-plugin-BackgroundMusic";
if (file_exists($pluginConfigFile)){
$pluginSettings = parse_ini_file($pluginConfigFile);
} else {
return json(array('status' => 'ERROR', 'message' => 'Plugin not configured'));
}
// Get POST data - check if a playlist name was passed in the request
$input = json_decode(file_get_contents('php://input'), true);
$showPlaylist = isset($input['playlist']) && !empty($input['playlist']) ? $input['playlist'] : '';
// If no playlist was passed, fall back to configured ShowPlaylist
if (empty($showPlaylist)) {
$showPlaylist = isset($pluginSettings['ShowPlaylist']) ? $pluginSettings['ShowPlaylist'] : '';
}
$fadeTime = isset($pluginSettings['FadeTime']) ? intval($pluginSettings['FadeTime']) : 5;
$blackoutTime = isset($pluginSettings['BlackoutTime']) ? intval($pluginSettings['BlackoutTime']) : 2;
if (empty($showPlaylist)) {
return json(array('status' => 'ERROR', 'message' => 'Show playlist not configured'));
}
// Execute the fade and show transition script in background via wrapper
$wrapperScript = dirname(__FILE__) . '/scripts/start_show_wrapper.sh';
$cmd = sprintf('sudo %s %d %d %s',
escapeshellarg($wrapperScript),
$fadeTime,
$blackoutTime,
escapeshellarg($showPlaylist));
exec($cmd);
return json(array('status' => 'OK', 'message' => 'Show transition started'));
}
// POST /api/plugin/fpp-plugin-BackgroundMusic/set-volume
function fppBackgroundMusicSetVolume() {
global $settings;
$pluginConfigFile = $settings['configDirectory'] . "/plugin.fpp-plugin-BackgroundMusic";
// Get POST data
$input = json_decode(file_get_contents('php://input'), true);
$volume = isset($input['volume']) ? intval($input['volume']) : null;
if ($volume === null || $volume < 0 || $volume > 100) {
return json(array('status' => 'ERROR', 'message' => 'Invalid volume level. Must be between 0 and 100.'));
}
// Update the config file
if (file_exists($pluginConfigFile)){
$pluginSettings = parse_ini_file($pluginConfigFile);
} else {
$pluginSettings = array();
}
$pluginSettings['VolumeLevel'] = $volume;
$pluginSettings['BackgroundMusicVolume'] = $volume; // Update both for consistency
// Write back to config file
$configContent = "";
foreach ($pluginSettings as $key => $value) {
$configContent .= "$key=$value\n";
}
file_put_contents($pluginConfigFile, $configContent);
// Apply volume change immediately to bgmplayer (not system volume)
// This controls the player's internal volume via SIGUSR1/SIGUSR2 signals
$scriptPath = dirname(__FILE__) . '/scripts/set_bgmplayer_volume.sh';
exec("/bin/bash " . escapeshellarg($scriptPath) . " " . escapeshellarg($volume) . " 2>&1", $output, $returnCode);
if ($returnCode === 0) {
return json(array('status' => 'OK', 'message' => 'Volume updated', 'volume' => $volume));
} else {
return json(array('status' => 'WARNING', 'message' => 'Volume saved but player may not be running', 'volume' => $volume));
}
}
function GetCurrentStatus() {
$ch = curl_init('http://localhost/api/fppd/status');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
return json_decode($data, true);
}
// POST /api/plugin/fpp-plugin-BackgroundMusic/save-settings
function fppBackgroundMusicSaveSettings() {
global $settings;
$pluginConfigFile = $settings['configDirectory'] . "/plugin.fpp-plugin-BackgroundMusic";
// Get POST data
$input = json_decode(file_get_contents('php://input'), true);
if (!$input) {
return json(array('status' => 'ERROR', 'message' => 'Invalid input data'));
}
// Write settings in INI format
$configContent = "";
foreach ($input as $key => $value) {
// Quote values that contain spaces or special characters
if (strpos($value, ' ') !== false || strpos($value, '"') !== false || strpos($value, '=') !== false) {
// Escape any existing quotes and wrap in quotes
$value = '"' . str_replace('"', '\\"', $value) . '"';
}
$configContent .= "$key=$value\n";
}
if (file_put_contents($pluginConfigFile, $configContent) !== false) {
return json(array('status' => 'OK', 'message' => 'Settings saved successfully'));
} else {
return json(array('status' => 'ERROR', 'message' => 'Failed to write settings file'));
}
}
// GET /api/plugin/fpp-plugin-BackgroundMusic/playlist-details
function fppBackgroundMusicPlaylistDetails() {
global $settings;
$pluginConfigFile = $settings['configDirectory'] . "/plugin.fpp-plugin-BackgroundMusic";
$result = array();
$result['status'] = 'OK';
$result['tracks'] = array();
$result['playlistName'] = '';
$result['totalDuration'] = 0;
// Read plugin configuration
if (file_exists($pluginConfigFile)) {
$pluginSettings = parse_ini_file($pluginConfigFile);
$playlistName = isset($pluginSettings['BackgroundMusicPlaylist']) ? $pluginSettings['BackgroundMusicPlaylist'] : '';
if (!empty($playlistName)) {
$result['playlistName'] = $playlistName;
// Get playlist file path - FPP playlists are JSON files
$playlistFile = $settings['playlistDirectory'] . '/' . $playlistName . '.json';
if (file_exists($playlistFile)) {
$playlistContent = file_get_contents($playlistFile);
$playlistData = json_decode($playlistContent, true);
if ($playlistData && isset($playlistData['mainPlaylist'])) {
$trackNum = 1;
foreach ($playlistData['mainPlaylist'] as $item) {
// Only include media items that are enabled
if ($item['type'] === 'media' && isset($item['enabled']) && $item['enabled'] == 1) {
$trackInfo = array();
$trackInfo['number'] = $trackNum;
$trackInfo['name'] = isset($item['mediaName']) ? $item['mediaName'] : 'Unknown';
// Duration is already in the playlist JSON
$duration = isset($item['duration']) ? (int)$item['duration'] : 0;
$trackInfo['duration'] = $duration;
$trackInfo['durationFormatted'] = formatDuration($duration);
$result['tracks'][] = $trackInfo;
$result['totalDuration'] += $duration;
$trackNum++;
}
}
$result['totalDurationFormatted'] = formatDuration($result['totalDuration']);
$result['totalTracks'] = count($result['tracks']);
} else {
$result['status'] = 'WARNING';
$result['message'] = 'Invalid playlist format';
}
} else {
$result['status'] = 'WARNING';
$result['message'] = 'Playlist file not found: ' . $playlistFile;
}
} else {
$result['status'] = 'WARNING';
$result['message'] = 'No background music playlist configured';
}
} else {
$result['status'] = 'ERROR';
$result['message'] = 'Plugin not configured';
}
return json($result);
}
function formatDuration($seconds) {
if ($seconds < 60) {
return $seconds . 's';
} else if ($seconds < 3600) {
$minutes = floor($seconds / 60);
$secs = $seconds % 60;
return sprintf('%d:%02d', $minutes, $secs);
} else {
$hours = floor($seconds / 3600);
$minutes = floor(($seconds % 3600) / 60);
$secs = $seconds % 60;
return sprintf('%d:%02d:%02d', $hours, $minutes, $secs);
}
}
// POST /api/plugin/fpp-plugin-BackgroundMusic/play-announcement
function fppBackgroundMusicPlayAnnouncement() {
global $settings;
$pluginConfigFile = $settings['configDirectory'] . "/plugin.fpp-plugin-BackgroundMusic";
// Load plugin config
if (!file_exists($pluginConfigFile)) {
return json(array('status' => 'ERROR', 'message' => 'Plugin not configured'));
}
$pluginSettings = parse_ini_file($pluginConfigFile);
// Get input data
$input = json_decode(file_get_contents('php://input'), true);
$buttonNumber = isset($input['buttonNumber']) ? intval($input['buttonNumber']) : 0;
if ($buttonNumber < 1 || $buttonNumber > 20) {
return json(array('status' => 'ERROR', 'message' => 'Invalid button number (must be 1-20)'));
}
// Get announcement configuration
$announcementFile = isset($pluginSettings['PSAButton' . $buttonNumber . 'File']) ? $pluginSettings['PSAButton' . $buttonNumber . 'File'] : '';
$announcementLabel = isset($pluginSettings['PSAButton' . $buttonNumber . 'Label']) ? $pluginSettings['PSAButton' . $buttonNumber . 'Label'] : 'PSA #' . $buttonNumber;
$announcementVolume = isset($pluginSettings['PSAAnnouncementVolume']) ? intval($pluginSettings['PSAAnnouncementVolume']) : 90;
$duckVolume = isset($pluginSettings['PSADuckVolume']) ? intval($pluginSettings['PSADuckVolume']) : 30;
if (empty($announcementFile)) {
return json(array('status' => 'ERROR', 'message' => 'Announcement button not configured'));
}
if (!file_exists($announcementFile)) {
return json(array('status' => 'ERROR', 'message' => 'Announcement file not found: ' . $announcementFile));
}
// Call the play_announcement script
$scriptPath = dirname(__FILE__) . '/scripts/play_announcement.sh';
$output = array();
$returnCode = 0;
$cmd = "/bin/bash " . escapeshellarg($scriptPath) . " " .
escapeshellarg($announcementFile) . " " .
escapeshellarg($duckVolume) . " " .
escapeshellarg($announcementVolume) . " " .
escapeshellarg($buttonNumber) . " " .
escapeshellarg($announcementLabel) . " 2>&1";
exec($cmd, $output, $returnCode);
if ($returnCode === 0) {
return json(array('status' => 'OK', 'message' => 'Announcement started'));
} else {
return json(array('status' => 'ERROR', 'message' => 'Failed to play announcement', 'details' => implode("\n", $output)));
}
}
// POST /api/plugin/fpp-plugin-BackgroundMusic/stop-announcement
function fppBackgroundMusicStopAnnouncement() {
$pidFile = '/tmp/announcement_player.pid';
if (!file_exists($pidFile)) {
return json(array('status' => 'OK', 'message' => 'No announcement playing'));
}
$pid = trim(file_get_contents($pidFile));
// Kill the announcement process
exec("kill $pid 2>&1", $output, $returnCode);
// Clean up PID file
@unlink($pidFile);
return json(array('status' => 'OK', 'message' => 'Announcement stopped'));
}
// GET /api/plugin/fpp-plugin-BackgroundMusic/psa-status
function fppBackgroundMusicPSAStatus() {
$pidFile = '/tmp/announcement_player.pid';
$statusFile = '/tmp/announcement_status.txt';
$playing = false;
$buttonNumber = 0;
$buttonLabel = '';
$announcementFile = '';
$duration = 0;
$elapsed = 0;
$progress = 0;
$maxDuration = 300; // 5 minutes max for any announcement
if (file_exists($pidFile)) {
$pid = trim(file_get_contents($pidFile));
// Check if process is actually running
exec("ps -p $pid > /dev/null 2>&1", $output, $returnCode);
$playing = ($returnCode === 0);
// If playing, read status information
if ($playing && file_exists($statusFile)) {
$statusData = parse_ini_file($statusFile);
if ($statusData) {
$buttonNumber = isset($statusData['buttonNumber']) ? intval($statusData['buttonNumber']) : 0;
$buttonLabel = isset($statusData['buttonLabel']) ? $statusData['buttonLabel'] : '';
$announcementFile = isset($statusData['announcementFile']) ? $statusData['announcementFile'] : '';
$duration = isset($statusData['duration']) ? intval($statusData['duration']) : 0;
// Calculate elapsed time and progress
if (isset($statusData['startTime'])) {
$startTime = intval($statusData['startTime']);
$elapsed = time() - $startTime;
// Calculate progress percentage
if ($duration > 0) {
$progress = min(100, round(($elapsed / $duration) * 100));
}
// Check if announcement has been running too long (stuck)
if ($elapsed > $maxDuration) {
error_log("BackgroundMusic: PSA stuck for $elapsed seconds, killing process $pid");
// Kill stuck process
exec("kill $pid 2>&1");
$playing = false;
}
}
}
}
// If not playing but PID file exists, clean it up
if (!$playing) {
@unlink($pidFile);
@unlink($statusFile);
}
} else {
// No PID file - ensure status file is also cleaned up
if (file_exists($statusFile)) {
@unlink($statusFile);
}
}
$result = array(
'status' => 'OK',
'playing' => $playing,
'buttonNumber' => $buttonNumber,
'buttonLabel' => $buttonLabel,
'announcementFile' => $announcementFile,
'duration' => $duration,
'elapsed' => $elapsed,
'progress' => $progress
);
return json($result);
}
// GET /api/plugin/fpp-plugin-BackgroundMusic/check-update
function fppBackgroundMusicCheckUpdate() {
$pluginDir = dirname(__FILE__);
$result = array(
'status' => 'OK',
'hasUpdate' => false,