forked from ARGUS-TV/ARGUS-TV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTvHome.cs
1278 lines (1139 loc) · 51.4 KB
/
TvHome.cs
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
#region Copyright (C) 2005-2013 Team MediaPortal
/*
* Copyright (C) 2005-2013 Team MediaPortal
* http://www.team-mediaportal.com
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Make; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#endregion
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MediaPortal.Dialogs;
using MediaPortal.GUI.Library;
using MediaPortal.Player;
using MediaPortal.Profile;
using MediaPortal.Util;
using ArgusTV.DataContracts;
using ArgusTV.ServiceProxy;
namespace ArgusTV.UI.MediaPortal
{
public class TvHome : HomeBase, ISetupForm, IShowPlugin, IPluginReceiver
{
public TvHome()
: base(ChannelType.Television) { }
private const int _keepAliveIntervalSeconds = 10;
private const string _settingSection = "argustv";
internal static class SettingName
{
public const string Server = "server";
public const string TcpPort = "tcpPort";
public const string MacAddresses = "macAddresses";
public const string IPAddress = "ipAddress";
public const string UseWakeOnLan = "useWakeOnLan";
public const string WakeOnLanTimeoutSeconds = "wakeOnLanTimeoutSeconds";
public const string NoClientStandbyWhenNotHome = "noClientStandbyWhenNotHome";
public const string AutoStreamingMode = "autoStreamingMode";
public const string AvoidRtspForLiveTv = "avoidRtspForLiveTv";
public const string PlayRecordingsOverRtsp = "playRecordingsOverRtsp";
public const string IsSingleSeat = "isSingleSeat";
public const string DisableRadio = "disableRadio";
}
private static int _preNotifyConfig;
private static int _notifyTVTimeout;
private static bool _playNotifyBeep;
private static bool _enableRecNotification;
private static bool _autoTurnOnTv;
private static bool _settingsLoaded;
private static bool _started;
private static bool _showlastactivemodule;
private static bool _lastActivemoduleWasFullscreen;
// Create the crop manager once to enable it.
private TvCropManager _tvCropManager = new TvCropManager();
private NotifyManager _notifyManager = new NotifyManager();
#region ISetupForm Members
public string Author()
{
return "http://www.argus-tv.com/ (dot-i)";
}
public bool CanEnable()
{
return true;
}
public bool DefaultEnabled()
{
return true;
}
public string Description()
{
return "ARGUS TV plugin";
}
public bool GetHome(out string strButtonText, out string strButtonImage, out string strButtonImageFocus, out string strPictureImage)
{
strButtonText = Utility.GetLocalizedText(TextId.MyTv);
strButtonImage = String.Empty;
strButtonImageFocus = String.Empty;
strPictureImage = "hover_my tv.png";
return true;
}
public int GetWindowId()
{
return WindowId.TvHome;
}
public bool HasSetup()
{
return true;
}
public string PluginName()
{
return "ARGUS TV";
}
public void ShowPlugin()
{
using (Settings xmlwriter = new MPSettings())
{
SetupForm setupForm = new SetupForm();
setupForm.ServerSettings = PluginMain.LoadServerSettings();
setupForm.NoClientStandbyWhenNotHome = PluginMain.NoClientStandbyWhenNotHome;
setupForm.PreferRtspForLiveTv = PluginMain.PreferRtspForLiveTv;
setupForm.PlayRecordingsOverRtsp = PluginMain.PlayRecordingsOverRtsp;
setupForm.AutoStreamingMode = PluginMain.AutoStreamingMode;
setupForm.DisableRadio = PluginMain.DisableRadio;
if (setupForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
xmlwriter.SetValue(_settingSection, SettingName.Server, setupForm.ServerSettings.ServerName);
xmlwriter.SetValue(_settingSection, SettingName.TcpPort, setupForm.ServerSettings.Port);
xmlwriter.SetValue(_settingSection, SettingName.MacAddresses, setupForm.ServerSettings.WakeOnLan.MacAddresses);
xmlwriter.SetValue(_settingSection, SettingName.IPAddress, setupForm.ServerSettings.WakeOnLan.IPAddress);
xmlwriter.SetValueAsBool(_settingSection, SettingName.UseWakeOnLan, setupForm.ServerSettings.WakeOnLan.Enabled);
xmlwriter.SetValue(_settingSection, SettingName.WakeOnLanTimeoutSeconds, setupForm.ServerSettings.WakeOnLan.TimeoutSeconds);
xmlwriter.SetValueAsBool(_settingSection, SettingName.NoClientStandbyWhenNotHome, setupForm.NoClientStandbyWhenNotHome);
xmlwriter.SetValueAsBool(_settingSection, SettingName.AvoidRtspForLiveTv, !setupForm.PreferRtspForLiveTv);
xmlwriter.SetValueAsBool(_settingSection, SettingName.PlayRecordingsOverRtsp, setupForm.PlayRecordingsOverRtsp);
xmlwriter.SetValueAsBool(_settingSection, SettingName.AutoStreamingMode, setupForm.AutoStreamingMode);
xmlwriter.SetValueAsBool(_settingSection, SettingName.IsSingleSeat, setupForm.IsSingleSeat);
xmlwriter.SetValueAsBool(_settingSection, SettingName.DisableRadio, setupForm.DisableRadio);
PluginMain.ClearCachedBooleanSettings();
}
}
}
#endregion
#region Keep Alive thread
private ManualResetEvent _keepAliveThreadStopEvent;
private Thread _keepAliveThread;
private DateTime _keepAliveServerTime = DateTime.MinValue;
private void StartKeepAliveThread()
{
_keepAliveServerTime = DateTime.MinValue;
_keepAliveThreadStopEvent = new ManualResetEvent(false);
ThreadStart threadStart = new ThreadStart(KeepAliveThreadMain);
_keepAliveThread = new Thread(threadStart);
_keepAliveThread.Start();
}
private void StopKeepAliveThread()
{
if (_keepAliveThread != null)
{
_keepAliveThreadStopEvent.Set();
_keepAliveThread.Join();
_keepAliveThreadStopEvent.Close();
_keepAliveThreadStopEvent = null;
_keepAliveThread = null;
}
}
/// <summary>
/// To keep the stream, server and this system alive when needed.
/// - System and server are kept alive when there is playing something, when a slideshow is active
/// or when you aren't on the home screen (if configured).
/// - So no need for the powerscheduler plugin.
/// </summary>
private int DoKeepAlive(int param1, int param2, object data)
{
if (DateTime.Now >= _keepAliveServerTime)
{
_keepAliveServerTime = DateTime.Now.AddSeconds(110);
bool systemNeeded = false;
bool displayNeeded = false;
if (!g_Player.Playing)
{
int activeWindow = GUIWindowManager.ActiveWindow;
if (PluginMain.NoClientStandbyWhenNotHome)
{
if (activeWindow != (int)GUIWindow.Window.WINDOW_HOME
&& activeWindow != (int)GUIWindow.Window.WINDOW_SECOND_HOME)
{
systemNeeded = true;
}
}
if (activeWindow == (int)GUIWindow.Window.WINDOW_SLIDESHOW)
{
systemNeeded = true;
displayNeeded = true;
}
}
else
{
displayNeeded = true;
systemNeeded = true;
}
if (systemNeeded)
{
try
{
//tell the system we need it
SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED);
if (PluginMain.IsConnected())
{
// Tell the server we need it.
Proxies.CoreService.KeepServerAlive();
}
if (displayNeeded)
{
SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED);
}
}
catch { Log.Error("TvHome: DoKeepAlive error"); }
}
}
try
{
if (PluginMain.IsConnected())
{
PluginMain.Navigator.SendLiveStreamKeepAlive();
}
}
catch (Exception ex)
{
Log.Error("DoKeepAlive: failed to keep live stream alive: {0}", ex.ToString());
}
return 0;
}
private void KeepAliveThreadMain()
{
do { GUIWindowManager.SendThreadCallbackAndWait(DoKeepAlive, 0, 0, null); }
while (!_keepAliveThreadStopEvent.WaitOne(_keepAliveIntervalSeconds * 1000, false));
}
#endregion
#region Cache Channels thread
/// <summary>
/// After standby, reboot,... miniguide takes long to load.
/// Miniguide data gets cached on the first access,
/// so this thread access the miniguide data on mp startup, resume,
/// so miniguide will be faster on the firts opening.
/// It's better to do this on the server side, but for now we do it in this way.
/// </summary>
///
private Thread _CacheMiniGuide;
private void StartCacheMiniGuideChannelsThread()
{
if (_CacheMiniGuide != null
&& _CacheMiniGuide.IsAlive)
{
return;
}
_CacheMiniGuide = new Thread(CacheMiniGuide);
_CacheMiniGuide.Name = "CacheChannels";
_CacheMiniGuide.Priority = ThreadPriority.Lowest;
_CacheMiniGuide.Start();
}
private void StopCacheMiniGuideChannelsThread()
{
if (_CacheMiniGuide != null
&& _CacheMiniGuide.IsAlive)
{
_CacheMiniGuide.Abort();
}
}
private object chacheLock = new object();
private void CacheMiniGuide()
{
lock (chacheLock)
{
System.Threading.Thread.Sleep(8000);
while (!PluginMain.IsConnected())
{
Thread.Sleep(2000);
}
Log.Debug("CacheChannelsThread: started");
List<CurrentAndNextProgram> _currentAndNextPrograms = new List<CurrentAndNextProgram>();
try
{
ChannelGroup currgroup = PluginMain.Navigator.CurrentGroup;
if (currgroup != null)
{
_currentAndNextPrograms = new List<CurrentAndNextProgram>(Proxies.SchedulerService.GetCurrentAndNextForGroup(PluginMain.Navigator.CurrentGroup.ChannelGroupId, true, null).Result);
}
List<ChannelGroup> groups = PluginMain.Navigator.GetGroups(ChannelType.Television);
foreach (ChannelGroup group in groups)
{
_currentAndNextPrograms = new List<CurrentAndNextProgram>(Proxies.SchedulerService.GetCurrentAndNextForGroup(group.ChannelGroupId, true, null).Result);
}
}
catch { Log.Error("CacheChannelsThread: error"); }
_currentAndNextPrograms.Clear();
_currentAndNextPrograms = null;
Log.Debug("CacheChannelsThread: ended");
}
}
#endregion
#region overrides
public override bool Init()
{
g_Player.PlayBackStopped += new global::MediaPortal.Player.g_Player.StoppedHandler(OnPlayBackStopped);
g_Player.PlayBackEnded += new global::MediaPortal.Player.g_Player.EndedHandler(OnPlayBackEnded);
g_Player.PlayBackStarted += new global::MediaPortal.Player.g_Player.StartedHandler(OnPlayBackStarted);
g_Player.AudioTracksReady += new g_Player.AudioTracksReadyHandler(OnAudioTracksReady);
GUIWindowManager.Receivers += new SendMessageHandler(OnGlobalMessage);
Application.ApplicationExit += new EventHandler(OnApplicationExit);
NetworkChange.NetworkAvailabilityChanged += OnNetworkStateChanged;
PluginMain.NetworkAvailable = NetworkInterface.GetIsNetworkAvailable();
bool result = Load(GUIGraphicsContext.Skin + @"\ARGUS_Home.xml");
if (result)
{
LoadSettings();
base.Init();
OnStart(false);
}
return result;
}
public override void DeInit()
{
g_Player.PlayBackStopped -= new global::MediaPortal.Player.g_Player.StoppedHandler(OnPlayBackStopped);
g_Player.PlayBackEnded -= new global::MediaPortal.Player.g_Player.EndedHandler(OnPlayBackEnded);
g_Player.PlayBackStarted -= new global::MediaPortal.Player.g_Player.StartedHandler(OnPlayBackStarted);
g_Player.AudioTracksReady -= new g_Player.AudioTracksReadyHandler(OnAudioTracksReady);
GUIWindowManager.Receivers -= new SendMessageHandler(OnGlobalMessage);
Application.ApplicationExit -= new EventHandler(OnApplicationExit);
NetworkChange.NetworkAvailabilityChanged -= OnNetworkStateChanged;
OnStop(false);
base.DeInit();
}
public override void OnAdded()
{
Log.Debug("TvHome:OnAdded");
g_Player.ShowFullScreenWindowTV = ShowFullScreenWindowTVHandler;
}
private static bool ShowFullScreenWindowTVHandler()
{
SetRecordingChaptersAndJumpPoints();
if ((g_Player.IsTV && PluginMain.Navigator.IsLiveStreamOn) || g_Player.IsTVRecording)
{
// watching TV
if (GUIWindowManager.ActiveWindow == (int)Window.WINDOW_TVFULLSCREEN)
{
return true;
}
Log.Info("TVHome: ShowFullScreenWindow switching to fullscreen tv");
GUIWindowManager.ActivateWindow((int)Window.WINDOW_TVFULLSCREEN);
GUIGraphicsContext.IsFullScreenVideo = true;
return true;
}
return g_Player.ShowFullScreenWindowTVDefault();
}
internal static void SetRecordingChaptersAndJumpPoints()
{
//push chapter and jumppoint information into the gui property manager
if (g_Player.IsTVRecording && g_Player.HasChapters)
{
double[] chapters = g_Player.Chapters;
double[] jumppoints = g_Player.JumpPoints;
string strChapters = string.Empty;
string strJumpPoints = string.Empty;
double duration = g_Player.Duration;
if (chapters != null)
{
foreach (double chapter in chapters)
{
double chapterPercent = chapter / duration * 100.0d;
strChapters += String.Format("{0:0.00}", chapterPercent) + " ";
}
}
if (jumppoints != null)
{
foreach (double jump in jumppoints)
{
double jumpPercent = jump / duration * 100.0d;
strJumpPoints += String.Format("{0:0.00}", jumpPercent) + " ";
}
}
GUIPropertyManager.SetProperty("#TV.Record.chapters", strChapters);
GUIPropertyManager.SetProperty("#TV.Record.jumppoints", strJumpPoints);
Log.Debug("TVHome.ShowFullScreenWindowTVHandler - setting chapters: " + strChapters);
Log.Debug("TVHome.ShowFUllScreenWindowTVHandler - setting jumppoints: " + strJumpPoints);
}
else
{
GUIPropertyManager.SetProperty("#TV.Record.chapters", string.Empty);
GUIPropertyManager.SetProperty("#TV.Record.jumppoints", string.Empty);
}
}
protected override void OnWindowLoaded()
{
base.OnWindowLoaded();
_tvGuideButton.Label = Utility.GetLocalizedText(TextId.TvGuide);
_recordingsButton.Label = Utility.GetLocalizedText(TextId.RecordedTv);
_teletextButton.IsEnabled = false;
}
protected override void OnPageDestroy(int new_windowId)
{
SaveSettings();
base.OnPageDestroy(new_windowId);
}
public override int GetID
{
get { return WindowId.TvHome; }
set { }
}
#endregion
#region public methods
public static void UpdateProgressPercentageBar()
{
UpdateProgressPercentageBar(false);
}
public static void UpdateProgressPercentageBar(bool ForceUpdate)
{
DoUpdateProgressPercentageBar(ChannelType.Television, ForceUpdate);
}
#endregion
#region private methods
private void OnStop(bool suspend)
{
if (_started)
{
try
{
SaveSettings();
StopEventListenerTask();
StopKeepAliveThread();
StopCacheMiniGuideChannelsThread();
_notifyManager.stop();
g_Player.Stop();
if (PluginMain.Navigator != null)
{
PluginMain.Navigator.AsyncStopLiveStream();
if (!suspend)
{
PluginMain.Navigator = null;
}
}
}
catch (Exception ex)
{
Log.Error("TvHome: OnStop error -{0}", ex.Message);
}
_started = false;
}
}
private void OnStart(bool resume)
{
if (!_started)
{
try
{
using (Settings xmlreader = new MPSettings())
{
_lastActivemoduleWasFullscreen = xmlreader.GetValueAsBool("general", "lastactivemoduleTvfullscreen", false);
}
PluginMain.EnsureConnection(true, false);
_notifyManager.start();
StartKeepAliveThread();
StartCacheMiniGuideChannelsThread();
EnsureEventListenerTaskStarted();
if (PluginMain.IsConnected())
{
//TODO
if (_showlastactivemodule && _lastActivemoduleWasFullscreen)
{
Channel previousChannel = PluginMain.Navigator.GetPreviousChannel(ChannelType.Television);
if (previousChannel != null)
{
//PluginMain.Navigator.ZapToChannel(previousChannel, false);
}
}
}
}
catch (Exception ex)
{
Log.Error("TvHome: OnStart error -{0}", ex.Message);
}
_started = true;
}
}
#endregion
#region global/mediaportal events
private void OnApplicationExit(object sender, EventArgs e)
{
OnStop(false);
}
private void OnNetworkStateChanged(object sender, NetworkAvailabilityEventArgs e)
{
//string heading;
//string message;
if (e.IsAvailable)
{
//Log.Info("TvHome: Network connected!");
//heading = Utility.GetLocalizedText(TextId.Information);
//message = "Network connected!";
PluginMain.NetworkAvailable = true;
}
else
{
//Log.Error("TvHome: Network disconnected!");
//heading = Utility.GetLocalizedText(TextId.Error);
//message = "Network disconnected!";
PluginMain.NetworkAvailable = false;
//if (!PluginMain.IsSingleSeat)
//{
/*if (g_Player.Playing && (g_Player.IsRadio || g_Player.IsTV || g_Player.IsTVRecording))
{
//g_Player.Stop();
GUIMessage msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_RECORDER_VIEW_CHANNEL, 0, 0, 0, 0, 0, null);
msg.Param1 = 1111;
//msg.Object = recording;
GUIGraphicsContext.SendMessage(msg);
msg = null;
}*/
/*using (Settings xmlreader = new MPSettings())
{
bool _startWithBasicHome = xmlreader.GetValueAsBool("gui", "startbasichome", false);
if (_startWithBasicHome && System.IO.File.Exists(GUIGraphicsContext.Skin + @"\basichome.xml"))
{
GUIWindowManager.ActivateWindow((int)GUIWindow.Window.WINDOW_SECOND_HOME);
}
else
{
GUIWindowManager.ActivateWindow((int)GUIWindow.Window.WINDOW_HOME);
}
}*/
//}
/*GUIDialogNotify pDlgNotify = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
if (pDlgNotify != null)
{
pDlgNotify.Reset();
pDlgNotify.ClearAll();
pDlgNotify.SetHeading(heading);
pDlgNotify.SetText(message);
pDlgNotify.TimeOut = 5;
//Utils.PlaySound("notify.wav", false, true);
pDlgNotify.DoModal(GUIWindowManager.ActiveWindow);
}*/
}
/*GUIDialogNotify pDlgNotify = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
if (pDlgNotify != null)
{
pDlgNotify.Reset();
pDlgNotify.ClearAll();
pDlgNotify.SetHeading(heading);
pDlgNotify.SetText(message);
pDlgNotify.TimeOut = 5;
Utils.PlaySound("notify.wav", false, true);
pDlgNotify.DoModal(GUIWindowManager.ActiveWindow);
}*/
}
public static void OnGlobalMessage(GUIMessage message)
{
switch (message.Message)
{
/// <summary>
/// We need to stop the player if our livestream ends unexpectedly.
/// If the stream stopped for a recording, we show it in a message.
/// Without this mediaportal can hang,crash (c++ error in tsreader).
/// </summary>
case GUIMessage.MessageType.GUI_MSG_STOP_SERVER_TIMESHIFTING:
{
Log.Debug("TvHome: GUI_MSG_STOP_SERVER_TIMESHIFTING, param1 = {0}", message.Param1);
if (PluginMain.Navigator.IsLiveStreamOn)
{
if (message.Param1 == 4321)//fired by eventlistener
{
LiveStream liveStream = message.Object as LiveStream;
LiveStream navigatorLiveStream = PluginMain.Navigator.LiveStream;
Channel channel = PluginMain.Navigator.CurrentChannel;
if (liveStream != null && channel != null
&& navigatorLiveStream.TimeshiftFile == liveStream.TimeshiftFile
&& liveStream.StreamStartedTime == navigatorLiveStream.StreamStartedTime)
{
if (g_Player.Playing && (g_Player.IsTV || g_Player.IsRadio))
{
g_Player.Stop();
Log.Info("TvHome: our live stream seems to be aborted, stop the playback now");
}
string text = GUILocalizeStrings.Get(1516);
if (message.Label == LiveStreamAbortReason.RecordingStartedOnCard.ToString())
{
text = GUILocalizeStrings.Get(1513);
}
text = text.Replace("\\r", " ");
string heading = string.Empty;
if (channel.ChannelType == ChannelType.Television)
heading = GUILocalizeStrings.Get(605) + " - " + channel.DisplayName; //my tv
else
heading = GUILocalizeStrings.Get(665) + " - " + channel.DisplayName; //my radio
string tvlogo = Utility.GetLogoImage(channel);
GUIDialogNotify pDlgNotify = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
if (pDlgNotify != null)
{
pDlgNotify.Reset();
pDlgNotify.ClearAll();
pDlgNotify.SetHeading(heading);
if (!string.IsNullOrEmpty(text))
{
pDlgNotify.SetText(text);
}
pDlgNotify.SetImage(tvlogo);
pDlgNotify.TimeOut = 5;
Utils.PlaySound("notify.wav", false, true);
pDlgNotify.DoModal(GUIWindowManager.ActiveWindow);
}
}
}
else//fired by mp player
{
PluginMain.Navigator.AsyncStopLiveStream();
}
}
}
break;
case GUIMessage.MessageType.GUI_MSG_NOTIFY_REC:
{
if (_enableRecNotification)
{
Log.Debug("TvHome: GUI_MSG_NOTIFY_REC");
string head = string.Empty;
string logo = string.Empty;
Recording recording = message.Object as Recording;
if (message.Param1 == 1)
head = GUILocalizeStrings.Get(1446);
else
head = GUILocalizeStrings.Get(1447);
Channel chan = Proxies.SchedulerService.GetChannelById(recording.ChannelId).Result;
logo = Utility.GetLogoImage(chan);
string _text = String.Format("{0} {1}-{2}",
recording.Title,
recording.StartTime.ToString("t", CultureInfo.CurrentCulture.DateTimeFormat),
recording.StopTime.ToString("t", CultureInfo.CurrentCulture.DateTimeFormat));
GUIDialogNotify DlgNotify = (GUIDialogNotify)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_NOTIFY);
if (DlgNotify != null)
{
DlgNotify.Reset();
DlgNotify.ClearAll();
DlgNotify.SetHeading(head);
if (!string.IsNullOrEmpty(_text))
{
DlgNotify.SetText(_text);
}
DlgNotify.SetImage(logo);
DlgNotify.TimeOut = 5;
if (_playNotifyBeep)
{
Utils.PlaySound("notify.wav", false, true);
}
DlgNotify.DoModal(GUIWindowManager.ActiveWindow);
}
}
}
break;
case GUIMessage.MessageType.GUI_MSG_NOTIFY_TV_PROGRAM:
{
Log.Debug("TvHome: GUI_MSG_NOTIFY_TV_PROGRAM");
TVNotifyYesNoDialog tvNotifyDlg = (TVNotifyYesNoDialog)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_TVNOTIFYYESNO);
UpcomingProgram prog = message.Object as UpcomingProgram;
if (tvNotifyDlg == null || prog == null)
{
return;
}
tvNotifyDlg.Reset();
if (prog.StartTime > DateTime.Now)
{
int minUntilStart = (prog.StartTime - DateTime.Now).Minutes;
if (minUntilStart > 1)
{
tvNotifyDlg.SetHeading(String.Format(GUILocalizeStrings.Get(1018), minUntilStart));
}
else
{
tvNotifyDlg.SetHeading(1019); // Program is about to begin
}
}
else
{
tvNotifyDlg.SetHeading(String.Format(GUILocalizeStrings.Get(1206), (DateTime.Now - prog.StartTime).Minutes.ToString()));
}
string description = GUILocalizeStrings.Get(736);
try
{
if (prog.GuideProgramId.HasValue)
{
GuideProgram Program = Proxies.GuideService.GetProgramById(prog.GuideProgramId.Value).Result;
description = Program.CreateCombinedDescription(false);
}
}
catch { }
tvNotifyDlg.SetLine(1, prog.Title);
tvNotifyDlg.SetLine(2, description);
tvNotifyDlg.SetLine(4, String.Format(GUILocalizeStrings.Get(1207), prog.Channel.DisplayName));
string strLogo = Utility.GetLogoImage(prog.Channel);
tvNotifyDlg.SetImage(strLogo);
tvNotifyDlg.TimeOut = _notifyTVTimeout;
if (_playNotifyBeep)
{
Utils.PlaySound("notify.wav", false, true);
}
tvNotifyDlg.SetDefaultToYes(false);
tvNotifyDlg.DoModal(GUIWindowManager.ActiveWindow);
if (tvNotifyDlg.IsConfirmed)
{
try
{
if (prog.Channel.ChannelType == ChannelType.Television)
{
if (g_Player.Playing && g_Player.IsTV && PluginMain.Navigator.IsLiveStreamOn)
GUIWindowManager.ActivateWindow((int)GUIWindow.Window.WINDOW_TVFULLSCREEN);
else
GUIWindowManager.ActivateWindow((int)GUIWindow.Window.WINDOW_TV);
PluginMain.Navigator.ZapToChannel(prog.Channel, false);
if (PluginMain.Navigator.CheckChannelChange())
{
TvHome.UpdateProgressPercentageBar(true);
if (!PluginMain.Navigator.LastChannelChangeFailed)
{
g_Player.ShowFullScreenWindow();
}
}
}
else
{
PluginMain.Navigator.ZapToChannel(prog.Channel, false);
GUIWindowManager.ActivateWindow((int)GUIWindow.Window.WINDOW_RADIO);
}
}
catch (Exception e)
{
Log.Error("TVHome: TVNotification: Error on starting channel {0} after notification: {1} {2} {3}", prog.Channel.DisplayName, e.Message, e.Source, e.StackTrace);
}
}
}
break;
//this (GUI_MSG_RECORDER_VIEW_CHANNEL) event is used to let other plugins play a recording,
//lastMediaHandler does this (with param1 = 5577 for indentification).
case GUIMessage.MessageType.GUI_MSG_RECORDER_VIEW_CHANNEL:
{
if (message.Param1 == 5577)
{
try
{
Recording rec = message.Object as Recording;
RecordedBase.PlayRecording(rec, message.Param2);
}
catch { Log.Error("TVHome: GUI_MSG_RECORDER_VIEW_CHANNEL error"); }
}
}
break;
}
}
#endregion
#region Playback Events
private void OnPlayBackStopped(global::MediaPortal.Player.g_Player.MediaType type, int stoptime, string filename)
{
Log.Debug("TvHome: OnPlayBackStopped");
GUIGraphicsContext.RenderBlackImage = false;
if (type == g_Player.MediaType.TV || type == g_Player.MediaType.Radio)
{
PluginMain.Navigator.AsyncStopLiveStream();
}
}
private void OnPlayBackEnded(global::MediaPortal.Player.g_Player.MediaType type, string filename)
{
Log.Debug("TvHome: OnPlayBackEnded");
GUIGraphicsContext.RenderBlackImage = false;
if (type == g_Player.MediaType.TV || type == g_Player.MediaType.Radio)
{
if (PluginMain.Navigator.IsLiveStreamOn)
{
g_Player.Stop();
PluginMain.Navigator.StopLiveStream();
}
}
}
private void OnPlayBackStarted(global::MediaPortal.Player.g_Player.MediaType type, string filename)
{
Log.Debug("TvHome: OnPlayBackStarted");
if (PluginMain.Navigator.IsLiveStreamOn)
{
if (type == g_Player.MediaType.TV || type == g_Player.MediaType.Radio)
{
// set audio track based on user prefs.
g_Player.CurrentAudioStream = HomeBase.GetPreferedAudioStreamIndex();
}
else
{
// Playback of another type has started while live TV was still on, so let's end the live TV stream.
PluginMain.Navigator.StopLiveStream();
}
}
}
private void OnAudioTracksReady()
{
g_Player.CurrentAudioStream = HomeBase.GetPreferedAudioStreamIndex();
}
#endregion
#region Serialisation
public static void SettingChanged()
{
_settingsLoaded = false;
LoadSettings();
}
private static void LoadSettings()
{
if (!_settingsLoaded)
{
Log.Debug("Tvhome: LoadSettings()");
using (Settings xmlreader = new MPSettings())
{
if (PluginMain.DisableRadio)
{
xmlreader.SetValueAsBool("pluginswindows", "ArgusTV.UI.MediaPortal.RadioHome", false);
xmlreader.SetValueAsBool("home", "ARGUS Radio", false);
xmlreader.SetValueAsBool("myplugins", "ARGUS Radio", false);
}
string strValue = xmlreader.GetValueAsString("mytv", "defaultar", "Normal");
_preNotifyConfig = xmlreader.GetValueAsInt("mytv", "notifyTVBefore", 300);
_notifyTVTimeout = xmlreader.GetValueAsInt("mytv", "notifyTVTimeout", 15);
_playNotifyBeep = xmlreader.GetValueAsBool("mytv", "notifybeep", true);
_enableRecNotification = xmlreader.GetValueAsBool("mytv", "enableRecNotifier", false);
_autoTurnOnTv = xmlreader.GetValueAsBool("mytv", "autoturnontv", false);
_showlastactivemodule = xmlreader.GetValueAsBool("general", "showlastactivemodule", false);
GUIGraphicsContext.ARType = global::MediaPortal.Util.Utils.GetAspectRatio(strValue);
_settingsLoaded = true;
}
}
}
private void SaveSettings()
{
if (PluginMain.IsConnected() && PluginMain.Navigator != null)
{
PluginMain.Navigator.SaveSettings();
using (Settings xmlreader = new MPSettings())
{
//xmlreader.SetValueAsBool("general", "lastactivemoduleTvfullscreen", (g_Player.IsTV && GUIGraphicsContext.IsFullScreenVideo));
}
}
}
protected override bool AutoTurnOnStream
{
get { return _autoTurnOnTv; }
}
public static bool ShowChannelStateIcons
{
get { return _showChannelStateIcons; }
}
public static bool SettingsLoaded
{
get { return _settingsLoaded; }
}
#endregion
#region P/Invoke
[FlagsAttribute]
private enum EXECUTION_STATE : uint
{
ES_SYSTEM_REQUIRED = 0x00000001,
ES_DISPLAY_REQUIRED = 0x00000002,
ES_AWAYMODE_REQUIRED = 0x00000040,
ES_CONTINUOUS = 0x80000000
}
[System.Runtime.InteropServices.DllImport("Kernel32.DLL", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
private extern static EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE state);
#endregion
#region IPlugin Members
private const int WM_POWERBROADCAST = 0x0218;
private const int WM_QUERYENDSESSION = 0x0011;
private const int PBT_APMQUERYSUSPEND = 0x0000;
private const int PBT_APMQUERYSTANDBY = 0x0001;
private const int PBT_APMQUERYSUSPENDFAILED = 0x0002;
private const int PBT_APMQUERYSTANDBYFAILED = 0x0003;
private const int PBT_APMSUSPEND = 0x0004;
private const int PBT_APMSTANDBY = 0x0005;
private const int PBT_APMRESUMECRITICAL = 0x0006;
private const int PBT_APMRESUMESUSPEND = 0x0007;
private const int PBT_APMRESUMESTANDBY = 0x0008;
private const int PBT_APMRESUMEAUTOMATIC = 0x0012;
public bool WndProc(ref System.Windows.Forms.Message msg)
{
if (msg.Msg == WM_POWERBROADCAST)
{