forked from xLightsSequencer/xLights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduleManager.cpp
5969 lines (5315 loc) · 205 KB
/
ScheduleManager.cpp
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
/***************************************************************
* This source files comes from the xLights project
* https://www.xlights.org
* https://github.com/smeighan/xLights
* See the github commit history for a record of contributing
* developers.
* Copyright claimed based on commit dates recorded in Github
* License: https://github.com/smeighan/xLights/blob/master/License.txt
**************************************************************/
#include <wx/xml/xml.h>
#include <wx/msgdlg.h>
#include <wx/log.h>
#include <wx/dir.h>
#include <wx/file.h>
#include <wx/config.h>
#include <wx/sckaddr.h>
#include <wx/socket.h>
#include <wx/filename.h>
#include <wx/mimetype.h>
#include "ScheduleManager.h"
#include "ScheduleOptions.h"
#include "PlayList/PlayList.h"
#include "../xLights/outputs/OutputManager.h"
#include "../xLights/outputs/Output.h"
#include "../xLights/outputs/ControllerEthernet.h"
#include "PlayList/PlayListStep.h"
#include "RunningSchedule.h"
#include "../xLights/xLightsVersion.h"
#include "../xLights/AudioManager.h"
#include "xScheduleMain.h"
#include "xScheduleApp.h"
#include "UserButton.h"
#include "OutputProcess.h"
#include "PlayList/PlayListItemAudio.h"
#include "PlayList/PlayListItemFSEQ.h"
#include "PlayList/PlayListItemFSEQVideo.h"
#include <wx/stdpaths.h>
#include "PlayList/PlayListItemVideo.h"
#include "Xyzzy.h"
#include "PlayList/PlayListItemText.h"
#include "../xLights/outputs/IPOutput.h"
#include "../xLights/UtilFunctions.h"
#include "Pinger.h"
#include "events/ListenerManager.h"
#include "wxJSON/jsonreader.h"
#include "../xLights/VideoReader.h"
#include "../xLights/outputs/Controller.h"
#include "OutputProcessExcludeDim.h"
#include "../xLights/Parallel.h"
#include <memory>
#include <log4cpp/Category.hh>
ScheduleManager::ScheduleManager(xScheduleFrame* frame, const std::string& showDir)
{
static log4cpp::Category &logger_base = log4cpp::Category::getInstance(std::string("log_base"));
logger_base.info("Loading schedule from %s.", (const char *)showDir.c_str());
// prime fix file with our show directory for any filename fixups
SetFixFileShowDir(showDir);
_syncManager = std::make_unique<SyncManager>(this);
_testMode = false;
_mainThread = wxThread::GetCurrentId();
_listenerManager = nullptr;
_pinger = nullptr;
_webRequestToggle = false;
_backgroundPlayList = nullptr;
_queuedSongs = new PlayList();
_queuedSongs->SetName("Song Queue");
_fppSyncMaster = nullptr;
_midiMaster = nullptr;
_artNetSyncMaster = nullptr;
_fppSyncMasterUnicast = nullptr;
_manualOTL = -1;
_immediatePlay = nullptr;
_scheduleOptions = nullptr;
_showDir = showDir;
_startTime = wxGetUTCTimeMillis().GetLo();
_outputManager = nullptr;
_buffer = nullptr;
_brightness = 100;
_lastBrightness = 100;
_xyzzy = nullptr;
_timerAdjustment = 0;
_lastXyzzyCommand = wxDateTime::Now();
_outputManager = new OutputManager();
_mode = (int)SYNCMODE::STANDALONE;
_remoteMode = REMOTEMODE::DISABLED;
wxConfigBase* config = wxConfigBase::Get();
if (config != nullptr)
{
_mode = config->ReadLong(_("SyncMode"), (int)SYNCMODE::STANDALONE);
_remoteMode = (REMOTEMODE)config->ReadLong(_("RemoteMode"), (int)REMOTEMODE::DISABLED);
}
wxLogNull logNo; //kludge: avoid "error 0" message from wxWidgets after new file is written
_lastSavedChangeCount = 0;
_changeCount = 0;
wxXmlDocument doc;
doc.Load(showDir + "/" + GetScheduleFile());
std::string backgroundPlayList = "";
if (doc.IsOk())
{
for (wxXmlNode* n = doc.GetRoot()->GetChildren(); n != nullptr; n = n->GetNext())
{
if (n->GetName() == "PlayList")
{
_playLists.push_back(new PlayList(_outputManager, n));
}
else if (n->GetName() == "Options")
{
_scheduleOptions = new ScheduleOptions(_outputManager, n, GetCommandManager());
_outputManager->SetParallelTransmission(_scheduleOptions->IsParallelTransmission());
OutputManager::SetRetryOpen(_scheduleOptions->IsRetryOpen());
_outputManager->SetSyncEnabled(_scheduleOptions->IsSync());
Schedule::SetCity(_scheduleOptions->GetCity());
}
else if (n->GetName() == "OutputProcesses")
{
for (wxXmlNode* n1 = n->GetChildren(); n1 != nullptr; n1 = n1->GetNext())
{
OutputProcess* op = OutputProcess::CreateFromXml(_outputManager, n1);
if (op != nullptr)
{
_outputProcessing.push_back(op);
}
}
}
else if (n->GetName() == "Background")
{
backgroundPlayList = n->GetAttribute("PlayList", "");
}
}
}
else
{
logger_base.error("Problem loading xml file %s.", (const char *)(showDir + "/" + GetScheduleFile()).c_str());
}
if (backgroundPlayList != "" && GetPlayList(backgroundPlayList) != nullptr)
{
_backgroundPlayList = new PlayList(*GetPlayList(backgroundPlayList));
logger_base.debug("Background playlist loaded. %s.", (const char *)_backgroundPlayList->GetNameNoTime().c_str());
}
if (_scheduleOptions == nullptr)
{
_scheduleOptions = new ScheduleOptions();
Schedule::SetCity(_scheduleOptions->GetCity());
_outputManager->SetParallelTransmission(_scheduleOptions->IsParallelTransmission());
_outputManager->SetSyncEnabled(_scheduleOptions->IsSync());
OutputManager::SetRetryOpen(_scheduleOptions->IsRetryOpen());
}
VideoReader::SetHardwareAcceleratedVideo(_scheduleOptions->IsHardwareAcceleratedVideo());
if (VideoReader::IsHardwareAcceleratedVideo())
{
logger_base.debug("Hardware accelerated video enabled.");
}
else
{
logger_base.debug("Hardware accelerated video disabled.");
}
_outputManager->Load(_showDir, _scheduleOptions->IsSync());
logger_base.info("Loaded outputs from %s.", (const char *)(_showDir + "/" + _outputManager->GetNetworksFileName()).c_str());
SetForceLocalIP(GetForceLocalIP());
if (_scheduleOptions->IsSendOffWhenNotRunning())
{
if (!_outputManager->IsOutputting())
{
if (_outputManager->IsOutputOpenInAnotherProcess())
{
logger_base.warn("Warning: Lights output is already open in another process. This will cause issues.", "WARNING", 4 | wxCENTRE, frame);
}
DisableRemoteOutputs();
_outputManager->StartOutput();
#ifdef __WXMSW__
::SetPriorityClass(::GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
#endif
}
ManageBackground();
logger_base.info("Started outputting to lights ... even though nothing is running.");
StartVirtualMatrices();
}
_listenerManager = new ListenerManager(this);
_syncManager->Start(_mode, _remoteMode, GetForceLocalIP());
// This is out frame data buffer ... it cannot be resized
logger_base.info("Allocated frame buffer of %ld bytes", _outputManager->GetTotalChannels());
_buffer = (uint8_t*)malloc(_outputManager->GetTotalChannels());
memset(_buffer, 0x00, _outputManager->GetTotalChannels());
#ifdef __WXMSW__
unsigned long state = ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED;
if (_scheduleOptions->IsKeepScreenOn())
{
state |= ES_DISPLAY_REQUIRED;
}
::SetThreadExecutionState(state);
#endif
}
void ScheduleManager::AddPlayList(PlayList* playlist)
{
_playLists.push_back(playlist);
_changeCount++;
}
std::list<PlayList*> ScheduleManager::GetPlayLists()
{
return _playLists;
}
int ScheduleManager::GetPPS() const
{
if (_outputManager != nullptr)
{
return _outputManager->GetPacketsPerSecond();
}
return 0;
}
void ScheduleManager::StartListeners()
{
_listenerManager->StartListeners(GetForceLocalIP());
}
int ScheduleManager::Sync(const std::string& filename, long ms)
{
wxCommandEvent event(EVT_SYNC);
event.SetString(filename);
event.SetInt(ms);
wxPostEvent(wxGetApp().GetTopWindow(), event);
return 50; // this is a problem
}
int ScheduleManager::DoSync(const std::string& filename, long ms)
{
static log4cpp::Category &logger_sync = log4cpp::Category::getInstance(std::string("log_sync"));
//logger_base.debug("DoSync Enter");
PlayList* pl = GetRunningPlayList();
PlayListStep* pls = nullptr;
// adjust the time we received by the desired latency
ms += GetOptions()->GetRemoteLatency();
if (filename != "" && pl != nullptr && pl->GetRunningStep() != nullptr && pl->GetRunningStep()->GetNameNoTime() == filename)
{
// right step is running
pls = pl->GetRunningStep();
}
else if (filename == "" && pl != nullptr)
{
// dont touch the playlist but need to work out which step should be playing
long stepMS = 0;
PlayListStep* shouldberunning = pl->GetStepAtTime(ms, stepMS);
ms = stepMS;
if (shouldberunning != nullptr)
{
if (shouldberunning != pl->GetRunningStep())
{
logger_sync.debug("Remote sync with no filename ... wrong step was running '%s' switching to '%s'.", (const char *)pl->GetRunningStep()->GetNameNoTime().c_str(), (const char *)shouldberunning->GetNameNoTime().c_str());
pl->JumpToStep(shouldberunning->GetNameNoTime());
wxCommandEvent event2(EVT_SCHEDULECHANGED);
wxPostEvent(wxGetApp().GetTopWindow(), event2);
}
if (pl->GetRunningStep() != nullptr)
{
pl->GetRunningStep()->SetSyncPosition(ms, GetOptions()->GetRemoteAcceptableJitter(), true);
}
}
else
{
if (ms == 0xFFFFFFFE)
{
pl->Suspend(true);
}
else if (ms == 0xFFFFFFFD)
{
pl->Suspend(false);
}
else
{
logger_sync.debug("Remote sync with no filename ... playlist was not sufficiently long for received sync position %ld.", ms);
pl->Stop();
}
}
}
else if (filename == "" && pl == nullptr)
{
if (_playLists.size() > 0)
{
// we only access the first playlist for timecode
pl = new PlayList(*_playLists.front());
long stepMS = 0;
PlayListStep* shouldberunning = pl->GetStepAtTime(ms, stepMS);
ms = stepMS;
if (shouldberunning == nullptr)
{
logger_sync.debug("Remote sync with no filename ... playlist was not sufficiently long for received sync position %ld.", ms);
delete pl;
pl = nullptr;
}
else
{
logger_sync.debug("Remote sync with no filename ... starting playlist '%s' step '%s'.", (const char *)pl->GetNameNoTime().c_str(), (const char *)shouldberunning->GetNameNoTime().c_str());
pl->Start(false, false, false);
pl->JumpToStep(shouldberunning->GetNameNoTime());
if (pl->GetRunningStep() != nullptr)
{
pl->GetRunningStep()->SetSyncPosition(ms, GetOptions()->GetRemoteAcceptableJitter(), true);
}
_immediatePlay = pl;
wxCommandEvent event2(EVT_SCHEDULECHANGED);
wxPostEvent(wxGetApp().GetTopWindow(), event2);
}
}
else
{
logger_sync.warn("Remote sync with no filename ... No playlist found to run.");
}
}
else
{
if (pl != nullptr)
{
StopPlayList(pl, false);
pl = nullptr;
}
// need to start the playlist step
if (filename != "")
{
StartStep(filename);
}
pl = GetRunningPlayList();
if (pl != nullptr) pls = pl->GetRunningStep();
wxCommandEvent event2(EVT_SCHEDULECHANGED);
wxPostEvent(wxGetApp().GetTopWindow(), event2);
}
if (pls != nullptr)
{
if (ms == 0xFFFFFFFF)
{
if (pls->GetNameNoTime() == filename)
{
wxCommandEvent event(EVT_STOP);
event.SetInt(pl->GetId());
if (!GetOptions()->IsRemoteAllOff())
{
event.SetString("sustain");
}
wxPostEvent(wxGetApp().GetTopWindow(), event);
}
}
else if (ms == 0xFFFFFFFE)
{
// pause
if (pls->GetNameNoTime() == filename)
{
pl->Suspend(true);
}
}
else if (ms == 0xFFFFFFFD)
{
// unpause
if (pls->GetNameNoTime() == filename)
{
pl->Suspend(false);
}
}
else
{
pls->SetSyncPosition((size_t)ms, GetOptions()->GetRemoteAcceptableJitter(), true);
}
}
if (pls != nullptr)
{
_listenerManager->SetFrameMS(pls->GetFrameMS());
//logger_base.debug("DoSync Leave");
return pls->GetFrameMS();
}
if (pl != nullptr)
{
_listenerManager->SetFrameMS(pl->GetFrameMS());
//logger_base.debug("DoSync Leave");
return pl->GetFrameMS();
}
_listenerManager->SetFrameMS(50);
//logger_base.debug("DoSync Leave");
return 50;
}
ScheduleManager::~ScheduleManager()
{
static log4cpp::Category &logger_base = log4cpp::Category::getInstance(std::string("log_base"));
AllOff();
_outputManager->StopOutput();
#ifdef __WXMSW__
::SetPriorityClass(::GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
#endif
StopVirtualMatrices();
ManageBackground();
logger_base.info("Stopped outputting to lights.");
if (IsDirty())
{
if (wxMessageBox("Unsaved changes to the schedule. Save now?", "Unsaved changes", wxYES_NO) == wxYES)
{
Save();
}
}
_syncManager->Stop(GetForceLocalIP());
if (_listenerManager != nullptr) {
_listenerManager->Stop();
delete _listenerManager;
}
while (_overlayData.size() > 0)
{
auto todelete = _overlayData.front();
_overlayData.remove(todelete);
delete todelete;
}
if (_xyzzy != nullptr)
{
wxString res;
_xyzzy->Close(res, "");
// clear the screen
_xyzzy->DrawBlack(_buffer, _outputManager->GetTotalChannels());
delete _xyzzy;
_xyzzy = nullptr;
}
if (_backgroundPlayList != nullptr)
{
logger_base.debug("Background playlist stopped and deleted. %s.", (const char *)_backgroundPlayList->GetNameNoTime().c_str());
_backgroundPlayList->Stop();
delete _backgroundPlayList;
_backgroundPlayList = nullptr;
}
while (_eventPlayLists.size() > 0)
{
_eventPlayLists.front()->Stop();
delete _eventPlayLists.front();
_eventPlayLists.pop_front();
}
while (_outputProcessing.size() > 0)
{
auto toremove = _outputProcessing.front();
_outputProcessing.remove(toremove);
delete toremove;
}
while (_playLists.size() > 0)
{
auto toremove = _playLists.front();
_playLists.remove(toremove);
delete toremove;
}
if (_immediatePlay != nullptr)
{
delete _immediatePlay;
_immediatePlay = nullptr;
}
if (_queuedSongs != nullptr)
{
delete _queuedSongs;
_queuedSongs = nullptr;
}
while (_activeSchedules.size() > 0)
{
auto toremove = _activeSchedules.front();
_activeSchedules.remove(toremove);
delete toremove;
}
delete _scheduleOptions;
delete _outputManager;
_syncManager = nullptr;
if (_buffer != nullptr)
{
free(_buffer);
}
#ifdef __WXMSW__
::SetThreadExecutionState(ES_CONTINUOUS);
#endif
logger_base.info("Closed schedule.");
}
std::list<PlayListItem*> ScheduleManager::GetPlayListIps() const
{
std::list<PlayListItem*> res;
for (const auto& it : _playLists) {
for (const auto& it2 : it->GetSteps()) {
for (const auto& it3 : it2->GetItems()) {
if (it3->HasIP()) {
res.push_back(it3);
}
}
}
}
return res;
}
bool ScheduleManager::GetWebRequestToggle()
{
bool rc = _webRequestToggle;
_webRequestToggle = false;
return rc;
}
bool ScheduleManager::IsDirty()
{
bool res = _lastSavedChangeCount != _changeCount;
auto it = _playLists.begin();
while (!res && it != _playLists.end()) {
res = res || (*it)->IsDirty();
++it;
}
res = res || _scheduleOptions->IsDirty();
for (const auto& it2 : _outputProcessing) {
res = res || it2->IsDirty();
}
return res;
}
void ScheduleManager::SetDirty()
{
_changeCount++;
}
void ScheduleManager::Save()
{
static log4cpp::Category& logger_base = log4cpp::Category::getInstance(std::string("log_base"));
wxXmlDocument doc;
wxXmlNode* root = new wxXmlNode(nullptr, wxXML_ELEMENT_NODE, "xSchedule");
doc.SetRoot(root);
root->AddChild(_scheduleOptions->Save());
for (const auto& it : _playLists) {
root->AddChild(it->Save());
}
if (_outputProcessing.size() != 0) {
wxXmlNode* op = new wxXmlNode(nullptr, wxXML_ELEMENT_NODE, "OutputProcesses");
for (const auto& it : _outputProcessing) {
op->AddChild(it->Save());
}
root->AddChild(op);
}
if (_backgroundPlayList != nullptr) {
wxXmlNode* background = new wxXmlNode(nullptr, wxXML_ELEMENT_NODE, "Background");
background->AddAttribute("PlayList", _backgroundPlayList->GetNameNoTime());
root->AddChild(background);
}
doc.Save(_showDir + "/" + GetScheduleFile());
ClearDirty();
logger_base.info("Saved Schedule to %s.", (const char*)(_showDir + "/" + GetScheduleFile()).c_str());
}
void ScheduleManager::ClearDirty()
{
_lastSavedChangeCount = _changeCount;
for (const auto& it : _playLists) {
it->ClearDirty();
}
_scheduleOptions->ClearDirty();
for (const auto& it : _outputProcessing) {
it->ClearDirty();
}
}
void ScheduleManager::RemovePlayList(PlayList* playlist)
{
static log4cpp::Category &logger_base = log4cpp::Category::getInstance(std::string("log_base"));
logger_base.info("Deleting playlist %s.", (const char*)playlist->GetNameNoTime().c_str());
_playLists.remove(playlist);
_changeCount++;
}
PlayList* ScheduleManager::GetRunningPlayList(int id) const
{
if (_immediatePlay != nullptr && _immediatePlay->IsRunning() && _immediatePlay->GetId() == id)
{
return _immediatePlay;
}
for (auto it : _activeSchedules)
{
if (it->GetPlayList()->GetId() == id)
{
return it->GetPlayList();
}
}
return nullptr;
}
PlayList* ScheduleManager::GetRunningPlayList() const
{
// find the highest priority running playlist
PlayList* running = nullptr;
if (_immediatePlay != nullptr && _immediatePlay->IsRunning())
{
running = _immediatePlay;
}
else if ((GetRunningSchedule() == nullptr || GetRunningSchedule()->GetSchedule()->GetPriority() <= Schedule::GetMaxSchedulePriorityBelowQueued()) &&
_queuedSongs->GetStepCount() > 0 && _queuedSongs->IsRunning())
{
running = _queuedSongs;
}
else
{
if (GetRunningSchedule() != nullptr)
{
running = GetRunningSchedule()->GetPlayList();
}
}
return running;
}
void ScheduleManager::StopAll(bool sustain)
{
static log4cpp::Category &logger_base = log4cpp::Category::getInstance(std::string("log_base"));
logger_base.info("Stopping all playlists.");
_syncManager->SendStop();
if (_immediatePlay != nullptr)
{
_immediatePlay->Stop();
delete _immediatePlay;
_immediatePlay = nullptr;
}
if (_queuedSongs->IsRunning())
{
_queuedSongs->Stop();
_queuedSongs->RemoveAllSteps();
}
for (auto it = _activeSchedules.begin(); it != _activeSchedules.end(); ++it)
{
(*it)->Stop();
}
while (_eventPlayLists.size() > 0)
{
_eventPlayLists.front()->Stop();
delete _eventPlayLists.front();
_eventPlayLists.pop_front();
}
if (!sustain)
{
if (!IsSlave() || GetOptions()->IsRemoteAllOff())
{
AllOff();
}
}
}
void ScheduleManager::AllOff()
{
static log4cpp::Category &logger_base = log4cpp::Category::getInstance(std::string("log_base"));
logger_base.debug("Turning all the lights off.");
memset(_buffer, 0x00, _outputManager->GetTotalChannels()); // clear out any prior frame data
_outputManager->StartFrame(0);
if ((_backgroundPlayList != nullptr || _eventPlayLists.size() > 0) && _scheduleOptions->IsSendBackgroundWhenNotRunning())
{
if (_backgroundPlayList != nullptr)
{
logger_base.debug(" ... except the background lights.");
if (!_backgroundPlayList->IsRunning())
{
_backgroundPlayList->Start(true);
logger_base.debug("Background playlist started. %s.", (const char *)_backgroundPlayList->GetNameNoTime().c_str());
}
_backgroundPlayList->Frame(_buffer, _outputManager->GetTotalChannels(), true);
}
if (_eventPlayLists.size() > 0)
{
logger_base.debug(" ... except the event lights.");
auto it = _eventPlayLists.begin();
while (it != _eventPlayLists.end())
{
if ((*it)->Frame(_buffer, _outputManager->GetTotalChannels(), true))
{
auto temp = it;
++it;
(*temp)->Stop();
delete *temp;
_eventPlayLists.remove(*temp);
}
else
{
++it;
}
}
}
// apply any overlay data
for (auto it = _overlayData.begin(); it != _overlayData.end(); ++it)
{
(*it)->Set(_buffer, _outputManager->GetTotalChannels());
}
}
// apply any output processing
for (const auto& it : _outputProcessing)
{
it->Frame(_buffer, _outputManager->GetTotalChannels(), _outputProcessing);
}
//if (_brightness < 100)
//{
// ApplyBrightness();
//}
for (const auto& it : *GetOptions()->GetVirtualMatrices())
{
it->Frame(_buffer, _outputManager->GetTotalChannels());
}
_outputManager->SetManyChannels(0, _buffer, _outputManager->GetTotalChannels());
_outputManager->EndFrame();
}
void ScheduleManager::ApplyBrightness()
{
if (_brightness == 100) return;
if (_brightness != _lastBrightness) {
_lastBrightness = _brightness;
CreateBrightnessArray();
}
auto totalChannels = _outputManager->GetTotalChannels();
auto ed = OutputProcess::GetExcludeDim(_outputProcessing, 1, totalChannels);
if (ed.size() == 0) { // handle the simple case fast
//uint8_t* pb = _buffer;
//for (size_t i = 0; i < totalChannels; ++i) {
// *pb = _brightnessArray[*pb];
// pb++;
//}
parallel_for(0, totalChannels, [this](int i) {
_buffer[i] = _brightnessArray[_buffer[i]];
});
}
else {
auto exclude = ed.begin();
uint8_t* pb = _buffer;
for (size_t i = 0; i < totalChannels; ++i) {
while (exclude != ed.end() && i > (*exclude)->GetLastExcludeChannel() - 1) ++exclude;
bool ex = (exclude != ed.end() && i >= (*exclude)->GetFirstExcludeChannel() - 1);
if (!ex) {
*pb = _brightnessArray[*pb];
}
pb++;
}
}
}
int ScheduleManager::Frame(bool outputframe, xScheduleFrame* frame)
{
static bool reentry = false;
static int oldrate = 50;
if (reentry) return oldrate;
reentry = true;
static log4cpp::Category &logger_base = log4cpp::Category::getInstance(std::string("log_base"));
static log4cpp::Category &logger_frame = log4cpp::Category::getInstance(std::string("log_frame"));
wxStopWatch sw;
int rate = 0;
long totalChannels = _outputManager->GetTotalChannels();
// timeout xyzzy if no api calls for 15 seconds
if (_xyzzy != nullptr && (wxDateTime::Now() - _lastXyzzyCommand).GetSeconds() > 15)
{
logger_base.info("Stopping xyzzy due to timeout.");
wxString msg;
DoXyzzy("close", "", msg, "");
}
if (IsTest())
{
long msec = wxGetUTCTimeMillis().GetLo() - _startTime;
if (outputframe)
{
memset(_buffer, 0x00, totalChannels); // clear out any prior frame data
_outputManager->StartFrame(msec);
TestFrame(_buffer, totalChannels, msec);
}
// apply any output processing
for (const auto& it : _outputProcessing)
{
it->Frame(_buffer, totalChannels, _outputProcessing);
}
if (outputframe && _brightness < 100)
{
ApplyBrightness();
}
for (const auto& it : *GetOptions()->GetVirtualMatrices())
{
it->Frame(_buffer, totalChannels);
}
if (outputframe)
{
_outputManager->SetManyChannels(0, _buffer, totalChannels);
_outputManager->EndFrame();
}
}
else
{
PlayList* running = GetRunningPlayList();
if (running != nullptr || _xyzzy != nullptr)
{
rate = 50;
std::string fseq = "";
std::string media = "";
if (running != nullptr)
{
fseq = running->GetActiveSyncItemFSEQ();
media = running->GetActiveSyncItemMedia();
rate = running->GetFrameMS();
}
long msec = wxGetUTCTimeMillis().GetLo() - _startTime;
if (outputframe)
{
memset(_buffer, 0x00, totalChannels); // clear out any prior frame data
_outputManager->StartFrame(msec);
}
bool done = false;
if (running != nullptr)
{
logger_frame.debug("Frame: About to run step frame %ldms", sw.Time());
done = running->Frame(_buffer, totalChannels, outputframe);
logger_frame.debug("Frame: step frame done %ldms", sw.Time());
if (running->GetRunningStep() != nullptr)
{
size_t fms;
std::string tsn = "";
auto ts = running->GetRunningStep()->GetTimeSource(fms);
if (ts != nullptr)
{
tsn = running->GetRunningStep()->GetTimeSource(fms)->GetNameNoTime();
}
_syncManager->SendSync(rate ,
running->GetRunningStep()->GetLengthMS(),
running->GetRunningStep()->GetPosition(),
running->GetPosition(),
fseq,
media,
running->GetRunningStep()->GetNameNoTime(),
tsn,
running->GetRunningStepIndex());
}
// for queued songs we must remove the queued song when it finishes
if (running == _queuedSongs)
{
if (_queuedSongs->GetRunningStep() != _queuedSongs->GetSteps().front())
{
_queuedSongs->RemoveStep(_queuedSongs->GetSteps().front());
wxCommandEvent event(EVT_DOCHECKSCHEDULE);
wxPostEvent(wxGetApp().GetTopWindow(), event);
wxCommandEvent event2(EVT_SCHEDULECHANGED);
wxPostEvent(wxGetApp().GetTopWindow(), event2);
}
}
}
if (_backgroundPlayList != nullptr)
{
if (!_backgroundPlayList->IsRunning())
{
_backgroundPlayList->Start(true);
logger_base.debug("Background playlist restarted. %s.", (const char *)_backgroundPlayList->GetNameNoTime().c_str());
}
_backgroundPlayList->Frame(_buffer, totalChannels, outputframe);
}
if (_eventPlayLists.size() > 0)
{
auto it = _eventPlayLists.begin();
while (it != _eventPlayLists.end())
{
if ((*it)->Frame(_buffer, _outputManager->GetTotalChannels(), outputframe))
{
auto temp = it;
++it;
(*temp)->Stop();
delete *temp;
_eventPlayLists.remove(*temp);
}
else
{
++it;
}
}
}
if (_xyzzy != nullptr)
{
_xyzzy->Frame(_buffer, totalChannels, outputframe);
}
if (outputframe)
{
// apply any overlay data
for (auto it = _overlayData.begin(); it != _overlayData.end(); ++it)
{
(*it)->Set(_buffer, totalChannels);
}
frame->ManipulateBuffer(_buffer, totalChannels);
logger_frame.debug("Frame: Overlay data done %ldms", sw.Time());
// apply any output processing
for (auto it = _outputProcessing.begin(); it != _outputProcessing.end(); ++it)
{
(*it)->Frame(_buffer, totalChannels, _outputProcessing);
}
logger_frame.debug("Frame: Output processing done %ldms", sw.Time());
if (outputframe && _brightness < 100)
{
ApplyBrightness();
}