-
Notifications
You must be signed in to change notification settings - Fork 17
/
ServerInterface.cs
1609 lines (1451 loc) · 64.7 KB
/
ServerInterface.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using Gentle.Common;
using Gentle.Framework;
using TvControl;
using TvDatabase;
using TvLibrary.Log;
namespace MPTvClient
{
public class TVServerController
{
IList<TvDatabase.ChannelGroup> tvgroups = null;
IList<TvDatabase.Channel> channels = null;
IList<TvDatabase.GroupMap> tvmappings = null;
IList<TvDatabase.RadioChannelGroup> radiogroups = null;
IList<TvDatabase.RadioGroupMap> radiomappings = null;
IList<TvDatabase.Card> cards = null;
IController controller = null;
IUser me = null;
Dictionary<String, String> isTimeShifting = null;
public static Dictionary<String, TvControl.User> userlist = null;
public Exception lastException = null;
#region Connection functions
public TVServerController(IController controller)
{
this.controller = controller;
this.isTimeShifting = new Dictionary<String,String>();
TVServerController.userlist = new Dictionary<String, TvControl.User>();
}
public bool Setup()
{
string connStr = "";
string provider = "";
try
{
controller.GetDatabaseConnectionString(out connStr, out provider);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Log.Error("TVServerXBMC: An exception occurred while connecting to the database. Did you select the right backend? TVServerXBMC default=MySQL; change your Gentle.conf file if you are using MSSQL.");
Log.Error("TVServerXBMC: The exception: " + ex.ToString());
return false;
}
Console.WriteLine(provider + ":" + connStr);
try
{
Console.WriteLine("Set Gentle framework provider to " + provider);
Console.WriteLine("Set Gentle framework provider string to " + connStr);
Gentle.Framework.ProviderFactory.SetDefaultProvider(provider);
Gentle.Framework.ProviderFactory.SetDefaultProviderConnectionString(connStr);
Console.WriteLine("New TvControl Admin user");
me = new User("TVServerXBMC", true);
}
catch (Exception ex)
{
try
{
Console.WriteLine(ex.ToString());
}
catch {}
lastException = ex;
return false;
}
try
{
Console.WriteLine("Fetch card list");
cards = Card.ListAll();
Console.Write("Fetch TV and radio channel list");
channels = Channel.ListAll();
Console.WriteLine(": " + channels.Count + " channels found");
// TV channel groups information:
Console.Write("Fetch TV groups list");
tvgroups = ChannelGroup.ListAll();
Console.WriteLine(": " + tvgroups.Count + " tvgroups found");
Console.Write("Fetch TV group mappings");
tvmappings = GroupMap.ListAll();
Console.WriteLine(": " + tvmappings.Count + " tvmappings found");
// Radio channel groups information:
Console.Write("Fetch radio groups");
radiogroups = RadioChannelGroup.ListAll();
Console.WriteLine(": " + radiogroups.Count + " radiogroups found");
Console.Write("Fetch radio group mappings");
radiomappings = RadioGroupMap.ListAll();
Console.WriteLine(": " + channels.Count + " radiomappings found");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Log.Error("TVServerXBMC: An exception occurred during Setup(): " + ex.Message);
if (ex.Source == "Gentle.Common")
{
Console.WriteLine("TVServerXBMC has problems connecting to the database. Check your settings (Gentle.config)");
Log.Error("TVServerXBMC has problems connecting to the database. Check your settings");
}
return false;
}
return true;
}
public void ResetConnection()
{
//_isTimeShifting = false;
}
#endregion
#region Control functions
public TvResult StartTimeShifting(int idChannel, ref string rtspURL, ref string remoteserver, ref IUser user, ref string timeshiftfilename, ref Int64 timeShiftBufPos, ref long timeShiftBufNr)
{
VirtualCard vcard;
//int cardId = -1;
TvResult result;
remoteserver = "";
timeShiftBufNr = -1;
timeShiftBufPos = -1;
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
//cardId = user.CardId;
watch.Start();
try
{
Console.WriteLine("Start timeshift for channel " + idChannel + " for user '" + user.Name);
Log.Info("TVServerXBMC: Start timeshift for channel " + idChannel + " for user '" + user.Name);
//result = controller.StartTimeShifting(ref user, idChannel, out vcard, cardId != -1); //This one is faster but we need to know the card id on beforehand
result = controller.StartTimeShifting(ref user, idChannel, out vcard);
}
catch (Exception ex)
{
lastException = ex;
StopTimeShifting(ref user);
return TvResult.UnknownError;
}
if (result == TvResult.Succeeded)
{
rtspURL = vcard.RTSPUrl;
remoteserver = vcard.RemoteServer;
timeshiftfilename = vcard.TimeShiftFileName;
user.CardId = vcard.Id; // keep track of the card the user is timeshifting
user.IdChannel = vcard.IdChannel;
TVServerController.userlist[user.Name].CardId = vcard.Id; //can't pass the list item by reference
TVServerController.userlist[user.Name].IdChannel = vcard.IdChannel; //can't pass the list item by reference
try
{
isTimeShifting.Add(user.Name, rtspURL);
}
catch { }
Console.WriteLine("Timeshift started for channel: '" + vcard.ChannelName + "' on device '" + vcard.Name + "' card id=" + vcard.Id);
Log.Debug("TVServerXBMC: Timeshift started for channel: '" + vcard.ChannelName + "' on device '" + vcard.Name + "'");
Console.WriteLine("TV Server returned '" + rtspURL + "' as timeshift URL and " + timeshiftfilename + " as timeshift file");
Log.Debug("TVServerXBMC: TV Server returned '" + rtspURL + "' as timeshift URL and " + timeshiftfilename + " as timeshift file");
Console.WriteLine("Remote server='" + remoteserver + "'");
Log.Debug("TVServerXBMC: Remote server='" + remoteserver + "'");
//Console.WriteLine("Streaming server IP-address: " + GetRTSPserverIP());
try {
//Fetch video stream information (takes approx 2-5 ms):
TvLibrary.Interfaces.IVideoStream videostream = vcard.GetCurrentVideoStream(TVServerController.userlist[user.Name]);
TvLibrary.Interfaces.VideoStreamType vidtype = videostream.StreamType;
Console.WriteLine("Video stream type=" + vidtype.ToString() + " Pid=" + videostream.Pid.ToString("X") + " PcrPid=" + videostream.PcrPid.ToString("X"));
TvLibrary.Interfaces.IAudioStream audiostream = vcard.AudioStream;
TvLibrary.Interfaces.AudioStreamType audiotype = audiostream.StreamType;
Console.WriteLine("Audio stream type=" + audiotype.ToString() + " Pid=" + audiostream.Pid.ToString("X") + " Language=" + audiostream.Language);
}
catch { }
controller.TimeShiftGetCurrentFilePosition(ref user, ref timeShiftBufPos, ref timeShiftBufNr);
Console.WriteLine("TimeShift file pos=" + timeShiftBufPos.ToString() + " buffer id=" + timeShiftBufNr.ToString());
}
else if ((result == TvResult.NoTuningDetails) || (result== TvResult.UnknownError))
{ //Hmmz, maybe a webstream?
StopTimeShifting(ref user);
Console.WriteLine("TV Server returned: 'NoTuningDetails' or 'UnknownError'.");
Console.WriteLine("Checking the availability of a webstream for this channel (id=" + idChannel.ToString() + ")");
Log.Debug("TVServerXBMC: TV Server returned: 'NoTuningDetails' or 'UnknownError'.");
Log.Debug("TVServerXBMC: Checking the availability of a webstream for this channel (id=" + idChannel.ToString() + ")");
rtspURL = GetWebStreamURL(idChannel);
if (rtspURL.Length > 0)
{
result = TvResult.Succeeded;
Console.WriteLine("Found a webstream: '" + rtspURL + "'");
Log.Debug("TVServerXBMC: Found a webstream: '" + rtspURL + "'");
}
else
{
Console.WriteLine("Unable to find tuning details or a webstream for channel (id=" + idChannel + ")");
Log.Debug("TVServerXBMC: Unable to find tuning details or a webstream for channel (id=" + idChannel + ")");
}
}
else
{
StopTimeShifting(ref user);
Console.WriteLine("Failed. TvResult = " + result.ToString());
Log.Debug("TVServerXBMC: Failed. TvResult = " + result.ToString());
}
watch.Stop();
Console.WriteLine("StartTimeShifting took " + watch.ElapsedMilliseconds.ToString() + " ms");
Log.Debug("TVServerXBMC: StartTimeShifting took " + watch.ElapsedMilliseconds.ToString() + " ms");
return result;
}
public string GetTimeshiftUrl(ref TvControl.IUser me)
{
if (IsTimeShifting(ref me))
{
string url;
if ( isTimeShifting.TryGetValue(me.Name, out url) )
return url;
}
return "";
}
public bool IsTimeShifting(ref IUser user)
{
bool result = controller.IsTimeShifting(ref user);
if (result == false)
{ // Fetch streaming status of all cards and search for this user
List<StreamingStatus> lss = GetStreamingStatus();
foreach (StreamingStatus ss in lss)
{
if((ss.status == "Timeshifting") && (ss.userName == user.Name))
{ //Found one...
user.CardId = ss.cardId;
user.IdChannel = ss.channelId;
isTimeShifting.Add(user.Name, ss.RTSPUrl);
TVServerController.userlist[user.Name].CardId = ss.cardId;
TVServerController.userlist[user.Name].IdChannel = ss.channelId;
return true;
}
}
}
return result;
}
// Stop the timeshift created by User user
public bool StopTimeShifting(ref IUser user)
{
if ( !IsTimeShifting(ref user) )
return false;
bool result = false;
try
{
result = controller.StopTimeShifting(ref user);
isTimeShifting.Remove(user.Name);
}
catch (Exception ex)
{
lastException = ex;
return false;
}
return result;
}
// Stop all active timeshifts created by the TVServerXBMC plugin
public bool StopTimeShifting()
{
Console.WriteLine("TODO: fixme StopTimeShifting() for all TVServerXBMC users");
Log.Debug("TVServerXBMC: TODO: fixme StopTimeShifting()");
return true;
}
public string GetRecordingURL(int idRecording, TvServer server, bool resolveHostnames, ref string OriginalURL)
{
//TvServer server = new TvServer();
if (server == null)
server = new TvServer();
string rtspURL = server.GetStreamUrlForFileName(idRecording);
// we resolve any host names, as xbox can't do NETBIOS resolution..
try
{
// XBMC's ffmpeg rtsp code does not like port numbers in the url
rtspURL = rtspURL.Replace(":554", "");
// Workaround for MP TVserver bug when using default port for rtsp => returns rtsp://ip:0
rtspURL = rtspURL.Replace(":0", "");
OriginalURL = rtspURL;
if (resolveHostnames)
{
Uri u = new Uri(rtspURL);
System.Net.IPAddress ipaddr = null;
try
{
ipaddr = System.Net.IPAddress.Parse(u.DnsSafeHost); //Fails on a hostname
}
catch { }
if ((ipaddr == null) || (ipaddr.IsIPv6LinkLocal || ipaddr.IsIPv6Multicast || ipaddr.IsIPv6SiteLocal))
{
try
{
System.Net.IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(u.DnsSafeHost);
string newHost = "";
foreach (System.Net.IPAddress ipaddr2 in hostEntry.AddressList)
{
// we only want ipv4.. this is just the xbox after all
if (ipaddr2.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6)
{
newHost = ipaddr2.ToString();
break;
}
}
rtspURL = u.AbsoluteUri.Replace(u.Scheme + "://" + u.Host + "/", u.Scheme + "://" + newHost + "/");
if (newHost.Length == 0)
{
Console.WriteLine("No IPv4 adress found for '" + u.DnsSafeHost + "' failed. Returning original URL.");
Log.Debug("TVServerXBMC: No IPv4 adress found for '" + u.DnsSafeHost + "' failed. Returning original URL.");
}
}
catch (Exception ex)
{
Console.WriteLine("IP resolve for '" + u.DnsSafeHost + "' failed.");
Console.WriteLine("Error: " + ex.ToString());
Log.Debug("TVServerXBMC: IP resolve for '" + u.DnsSafeHost + "' failed.");
Log.Debug("TVServerXBMC: Error: " + ex.ToString());
}
}
}
}
catch
{
//Console.WriteLine("IP resolve failed");
return "";
}
return rtspURL;
}
public void DeleteRecording(int idRecording)
{
controller.DeleteRecording(idRecording);
}
public bool DeleteSchedule(int idSchedule)
{
try
{
Schedule sched = Schedule.Retrieve(idSchedule);
sched.Remove();
return true;
}
catch
{
return false;
}
}
public bool IsRecording()
{
return controller.IsAnyCardRecording();
}
public bool StopRecording(int schedId)
{
bool result = false;
try
{
if (controller.IsAnyCardRecording())
{
if (schedId >= 0)
{
controller.StopRecordingSchedule(schedId);
return true;
//return controller.IsRecordingSchedule(schedId,
}
else
{
result = controller.StopRecording(ref me);
}
}
}
catch (Exception ex)
{
lastException = ex;
Console.WriteLine(ex.ToString());
Log.Error("TVServerXBMC: " + ex.ToString());
}
return result;
}
#endregion
#region Info functions
public ReceptionDetails GetReceptionDetails()
{
VirtualCard vcard;
try
{
vcard = new VirtualCard(me, RemoteControl.HostName);
}
catch (Exception ex)
{
lastException = ex;
Console.WriteLine(ex.ToString());
Log.Error("TVServerXBMC: " + ex.ToString());
return null;
}
ReceptionDetails details = new ReceptionDetails();
details.signalLevel = vcard.SignalLevel;
details.signalQuality = vcard.SignalQuality;
return details;
}
public string GetBackendName()
{
return RemoteControl.HostName;
}
public List<StreamingStatus> GetStreamingStatus()
{
List<StreamingStatus> states = new List<StreamingStatus>();
VirtualCard vcard;
try
{
foreach (Card card in cards)
{
IUser user = new User();
user.CardId = card.IdCard;
IUser[] usersForCard = controller.GetUsersForCard(card.IdCard);
if (usersForCard == null)
{
StreamingStatus state = new StreamingStatus();
vcard = new VirtualCard(user, RemoteControl.HostName);
string tmp = "idle";
if (vcard.IsScanning) tmp = "Scanning";
if (vcard.IsGrabbingEpg) tmp = "Grabbing EPG";
state.cardId = card.IdCard;
state.cardName = vcard.Name;
state.cardType = vcard.Type.ToString();
state.status = tmp;
state.channelName = "";
state.userName = "";
states.Add(state);
continue;
}
if (usersForCard.Length == 0)
{
StreamingStatus state = new StreamingStatus();
vcard = new VirtualCard(user, RemoteControl.HostName);
string tmp = "idle";
if (vcard.IsScanning) tmp = "Scanning";
if (vcard.IsGrabbingEpg) tmp = "Grabbing EPG";
state.cardId = card.IdCard;
state.cardName = vcard.Name;
state.cardType = vcard.Type.ToString();
state.status = tmp;
state.channelName = "";
state.userName = "";
states.Add(state);
continue;
}
for (int i = 0; i < usersForCard.Length; ++i)
{
StreamingStatus state = new StreamingStatus();
string tmp = "idle";
vcard = new VirtualCard(usersForCard[i], RemoteControl.HostName);
if (vcard.IsTimeShifting)
tmp = "Timeshifting";
else
if (vcard.IsRecording)
tmp = "Recording";
else
if (vcard.IsScanning)
tmp = "Scanning";
else
if (vcard.IsGrabbingEpg)
tmp = "Grabbing EPG";
state.cardId = card.IdCard;
state.cardName = vcard.Name;
state.cardType = vcard.Type.ToString();
state.status = tmp;
state.channelName = vcard.ChannelName;
state.userName = vcard.User.Name;
state.channelId = vcard.IdChannel;
state.TimeShiftFileName = vcard.TimeShiftFileName;
state.RTSPUrl = vcard.RTSPUrl;
states.Add(state);
}
}
}
catch (Exception ex)
{
lastException = ex;
Console.WriteLine(ex.ToString());
Log.Error("TVServerXBMC: " + ex.ToString());
return null;
}
return states;
}
public List<string> GetGroupNames()
{
if (!RemoteControl.IsConnected)
return null;
List<string> lGroups = new List<string>();
try
{
foreach (ChannelGroup group in tvgroups)
lGroups.Add(group.GroupName);
}
catch (Exception ex)
{
lastException = ex;
Console.WriteLine(ex.ToString());
Log.Error("TVServerXBMC: " + ex.ToString());
return null;
}
return lGroups;
}
public List<string> GetRadioGroupNames()
{
if (!RemoteControl.IsConnected)
return null;
List<string> lGroups = new List<string>();
try
{
foreach (RadioChannelGroup group in radiogroups)
lGroups.Add(group.GroupName);
}
catch (Exception ex)
{
lastException = ex;
Console.WriteLine(ex.ToString());
Log.Error("TVServerXBMC: " + ex.ToString());
return null;
}
return lGroups;
}
public int GetChannelCount(String group)
{
if (group == "" || group == null)
{
return channels.Count;
}
else
{
foreach (ChannelGroup chgroup in tvgroups)
{
if (chgroup.GroupName == group)
{
IList<GroupMap> maps = chgroup.ReferringGroupMap();
return maps.Count;
}
}
}
return 0;
}
public ChannelInfo GetChannelInfo(int chanId)
{
foreach (Channel chan in channels)
{
if (chan.IdChannel == chanId)
{
// this is the channel we want
ChannelInfo channelInfo = new ChannelInfo();
TvDatabase.Program epg = null;
channelInfo.channelID = chan.IdChannel.ToString();
channelInfo.name = chan.DisplayName;
channelInfo.isWebStream = chan.IsWebstream();
channelInfo.epgNow = new ProgrammInfo();
channelInfo.epgNext = new ProgrammInfo();
try
{
epg = chan.CurrentProgram;
if (epg != null)
{
channelInfo.epgNow.timeInfo = epg.StartTime.ToShortTimeString() + "-" + epg.EndTime.ToShortTimeString();
channelInfo.epgNow.description = epg.Title;
}
epg = chan.NextProgram;
if (epg != null)
{
channelInfo.epgNext.timeInfo = epg.StartTime.ToShortTimeString() + "-" + epg.EndTime.ToShortTimeString();
channelInfo.epgNext.description = epg.Title;
}
}
catch { }
return channelInfo;
}
}
return null;
}
public List<ChannelInfo> GetChannelInfosForGroup(string groupName)
{
List<ChannelInfo> refChannelInfos = new List<ChannelInfo>();
try
{
if (tvgroups != null)
{
foreach (ChannelGroup group in tvgroups)
{
if (group.GroupName == groupName || groupName == "")
{
IList<GroupMap> maps = group.ReferringGroupMap();
TvDatabase.Program epg;
foreach (GroupMap map in maps)
{
ChannelInfo channelInfo = new ChannelInfo();
channelInfo.channelID = map.ReferencedChannel().IdChannel.ToString();
channelInfo.name = map.ReferencedChannel().DisplayName;
channelInfo.epgNow = new ProgrammInfo();
try
{
epg = map.ReferencedChannel().CurrentProgram;
if (epg != null)
{
channelInfo.epgNow.timeInfo = epg.StartTime.ToShortTimeString() + "-" + epg.EndTime.ToShortTimeString();
channelInfo.epgNow.description = epg.Title;
}
epg = map.ReferencedChannel().NextProgram;
channelInfo.epgNext = new ProgrammInfo();
if (epg != null)
{
channelInfo.epgNext.timeInfo = epg.StartTime.ToShortTimeString() + "-" + epg.EndTime.ToShortTimeString();
channelInfo.epgNext.description = epg.Title;
}
}
catch (Exception e)
{
Console.WriteLine("Error while obtaining the EPG for channel " + channelInfo.channelID + ": " + e.ToString());
Log.Error("TVServerXBMC: Error while obtaining the EPG for channel " + channelInfo.channelID + ": " + e.ToString());
}
//Get the tuning details
try
{
IList<TuningDetail> listtuningdetails = map.ReferencedChannel().ReferringTuningDetail();
foreach (TuningDetail tuningdetail in listtuningdetails)
{
channelInfo.isScrambled = !(tuningdetail.FreeToAir);
}
}
catch
{
channelInfo.isScrambled = false;
}
refChannelInfos.Add(channelInfo);
}
if (groupName != "")
break;
}
}
}
}
catch (Exception ex)
{
lastException = ex;
Console.WriteLine(ex.ToString());
Log.Error("TVServerXBMC: " + ex.ToString());
return null;
}
return refChannelInfos;
}
public List<string> GetTVChannels(string groupName)
{
try
{
if (tvgroups != null)
{
List<string> tvchannels = new List<string>();
if (groupName.Length == 0)
groupName = "All Channels";
foreach (ChannelGroup group in tvgroups)
{
if (group.GroupName == groupName)
{
IList<GroupMap> maps = group.ReferringGroupMap();
foreach (GroupMap map in maps)
{
Channel chan = map.ReferencedChannel();
if (chan == null) continue;
string tvchannel;
int channelNumber = 0;
bool freetoair = true;
//Determine the channel number given by the provider using this channel's tuning details
IList<TuningDetail> tuningdetails = chan.ReferringTuningDetail();
foreach (TuningDetail tuningdetail in tuningdetails)
{
//For now, just take the first one:
channelNumber = tuningdetail.ChannelNumber;
freetoair = tuningdetail.FreeToAir;
break;
}
//XBMC side:
//uid, number, name, callsign, iconpath, isencrypted,
//isradio, ishidden, isrecording, bouquet, multifeed,
//stream_url;
//[0] = channel uid
//[1] = channel number
//[2] = channel name
tvchannel = chan.IdChannel + "|" + channelNumber + "|" + chan.DisplayName + "|";
//[3] = isencrypted
tvchannel += (freetoair ? "0" : "1");
tvchannels.Add(tvchannel);
}
}
}
return tvchannels;
}
return null;
}
catch (Exception ex)
{
lastException = ex;
Console.WriteLine(ex.ToString());
Log.Error("TVServerXBMC: " + ex.ToString());
return null;
}
}
public List<ChannelInfo> getRadioChannels(string groupName)
{
List<ChannelInfo> radioChannels = new List<ChannelInfo>();
try
{
if (groupName == "")
{
TvDatabase.Program epg;
foreach (Channel chan in channels)
{
if (!chan.IsRadio)
continue;
ChannelInfo channelInfo = new ChannelInfo();
channelInfo.channelID = chan.IdChannel.ToString();
channelInfo.name = chan.DisplayName;
channelInfo.isWebStream = chan.IsWebstream();
epg = chan.CurrentProgram;
channelInfo.epgNow = new ProgrammInfo();
if (epg != null)
{
channelInfo.epgNow.timeInfo = epg.StartTime.ToShortTimeString() + "-" + epg.EndTime.ToShortTimeString();
channelInfo.epgNow.description = epg.Title;
}
epg = chan.NextProgram;
channelInfo.epgNext = new ProgrammInfo();
if (epg != null)
{
channelInfo.epgNext.timeInfo = epg.StartTime.ToShortTimeString() + "-" + epg.EndTime.ToShortTimeString();
channelInfo.epgNext.description = epg.Title;
}
radioChannels.Add(channelInfo);
}
}
else
{
foreach (RadioChannelGroup group in radiogroups)
{
if (group.GroupName == groupName)
{
IList<RadioGroupMap> maps = group.ReferringRadioGroupMap();
TvDatabase.Program epg;
foreach (RadioGroupMap map in maps)
{
ChannelInfo channelInfo = new ChannelInfo();
if (map.ReferencedChannel() == null) continue;
channelInfo.channelID = map.ReferencedChannel().IdChannel.ToString();
channelInfo.name = map.ReferencedChannel().DisplayName;
epg = map.ReferencedChannel().CurrentProgram;
channelInfo.epgNow = new ProgrammInfo();
if (epg != null)
{
channelInfo.epgNow.timeInfo = epg.StartTime.ToShortTimeString() + "-" + epg.EndTime.ToShortTimeString();
channelInfo.epgNow.description = epg.Title;
}
epg = map.ReferencedChannel().NextProgram;
channelInfo.epgNext = new ProgrammInfo();
if (epg != null)
{
channelInfo.epgNext.timeInfo = epg.StartTime.ToShortTimeString() + "-" + epg.EndTime.ToShortTimeString();
channelInfo.epgNext.description = epg.Title;
}
radioChannels.Add(channelInfo);
}
if (groupName != "")
break;
}
}
}
}
catch (Exception ex)
{
lastException = ex;
Console.WriteLine(ex.ToString());
Log.Error("TVServerXBMC: " + ex.ToString());
return null;
}
return radioChannels;
}
public List<string> GetRadioChannels(string groupName)
{
try
{
if (radiogroups != null)
{
List<string> radiochannels = new List<string>();
if (groupName.Length == 0)
groupName = "All Channels";
foreach (RadioChannelGroup group in radiogroups)
{
if (group.GroupName == groupName)
{
IList<RadioGroupMap> maps = group.ReferringRadioGroupMap();
foreach (RadioGroupMap map in maps)
{
Channel chan = map.ReferencedChannel();
if (chan == null) continue;
string radiochannel;
int channelNumber = 0;
bool freetoair = true;
//Determine the channel number given by the provider using this channel's tuning details
IList<TuningDetail> tuningdetails = chan.ReferringTuningDetail();
foreach (TuningDetail tuningdetail in tuningdetails)
{
//For now, just take the first one:
channelNumber = tuningdetail.ChannelNumber;
freetoair = tuningdetail.FreeToAir;
break;
}
//[0] = channel uid
//[1] = channel number
//[2] = channel name
radiochannel = chan.IdChannel + "|" + channelNumber + "|" + chan.DisplayName + "|";
//[3] = isencrypted
radiochannel += (freetoair ? "0" : "1");
//[4] = iswebstream
//[5] = webstream url
if (chan.IsWebstream())
{
radiochannel += "|1|";
radiochannel += GetWebStreamURL(ref chan);
}
else
{
radiochannel += "|0|";
}
radiochannels.Add(radiochannel);
}
}
}
return radiochannels;
}
return null;
}
catch (Exception ex)
{
lastException = ex;
Console.WriteLine(ex.ToString());
Log.Error("TVServerXBMC: " + ex.ToString());
return null;
}
}
public string GetWebStreamURL(int idChannel)
{
Channel chan = Channel.Retrieve(idChannel);
return GetWebStreamURL(ref chan);
}
public string GetWebStreamURL(ref Channel chan)
{
string url = "";
IList<TuningDetail> details = chan.ReferringTuningDetail();
foreach (TuningDetail detail in details)
{
if (detail.ChannelType == 5)
{
url = detail.Url;
break;
}
}
return url;
}
public int GetRecordingCount()
{
IList<TvDatabase.Recording> recordings = Recording.ListAll();
return recordings.Count;
}
public List<RecordingInfo> getRecordings()
{
List<RecordingInfo> recInfos = new List<RecordingInfo>();
try
{
IList<TvDatabase.Recording> recordings = Recording.ListAll();
foreach (Recording rec in recordings)
{
RecordingInfo recInfo = new RecordingInfo();
recInfo.recordingID = rec.IdRecording.ToString();
try
{
recInfo.channel = rec.ReferencedChannel().DisplayName;
}
catch
{ // Occurs for example when a recording is pointing to a channel
// that is deleted in the meantime
recInfo.channel = rec.IdChannel.ToString();
}
recInfo.title = rec.Title;
recInfo.description = rec.Description;
recInfo.genre = rec.Genre;
recInfo.timeInfo = rec.StartTime.ToString("u") + "|" + rec.EndTime.ToString("u");
recInfos.Add(recInfo);
}
}
catch (Exception ex)
{
lastException = ex;
Console.WriteLine(ex.ToString());
Log.Error("TVServerXBMC: " + ex.ToString());
return null;
}
return recInfos;
}
public List<String> GetRecordings(bool resolveHostnames, ref string OriginalURL)
{
List<String> reclist = new List<String>();
try
{
TvServer server = new TvServer();
IList<TvDatabase.Recording> recordings = Recording.ListAll();
foreach (Recording rec in recordings)
{
string recording;
string channelname;
string rtspURL = GetRecordingURL(rec.IdRecording, server, resolveHostnames, ref OriginalURL);//server.GetStreamUrlForFileName(rec.IdRecording);
//XBMC pvr side:
//index, channelname, lifetime, priority, start time, duration