/
TVServerConnection.cs
357 lines (296 loc) · 12 KB
/
TVServerConnection.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
using System;
using System.Collections.Generic;
using System.Text;
using MPTvClient;
using TvControl;
using System.Net;
using TvLibrary.Log;
using TvDatabase;
namespace TVServerXBMC
{
class TVServerConnection
{
private static TVServerController serverIntf;
// info about current timeshift
private static string timeshiftUrl = "";
private static Dictionary<String,int> timeshiftChannel;
// create our objects and connect!
public static bool Init(IController controller)
{
timeshiftUrl = null;
timeshiftChannel = new Dictionary<String,int>();
serverIntf = new TVServerController(controller);
return serverIntf.Setup();
}
// Get a list of <string> tvgroups (channel tvgroups)
public static List<string> GetTVGroups()
{
List<string> groups = serverIntf.GetGroupNames();
if (groups == null)
{
disconnect();
return null;
}
return groups;
}
// Get a list of <string> radiogroups (channel radiogroups)
public static List<string> GetRadioGroups()
{
List<string> groups = serverIntf.GetRadioGroupNames();
if (groups == null)
{
disconnect();
return null;
}
return groups;
}
public static string getBackendName()
{
return serverIntf.GetBackendName();
}
public static int getChannelCount(String group)
{
return serverIntf.GetChannelCount(group);
}
public static int getRecordingCount()
{
return serverIntf.GetRecordingCount();
}
// get a list of all channels
public static List<ChannelInfo> getChannels(String group)
{
List<ChannelInfo> refChannelInfos = serverIntf.GetChannelInfosForGroup(group);
if (refChannelInfos == null)
disconnect();
return refChannelInfos;
}
public static List<string> GetTVChannels(String group)
{
List<string> result = serverIntf.GetTVChannels(group);
return result;
}
public static List<string> GetRadioChannels(String group)
{
List<string> result = serverIntf.GetRadioChannels(group);
return result;
}
public static List<ChannelInfo> getRadioChannels(String group)
{
List<ChannelInfo> refChannelInfos = serverIntf.getRadioChannels(group);
if (refChannelInfos == null)
disconnect();
return refChannelInfos;
}
public static List<RecordingInfo> getRecordings()
{
List<RecordingInfo> recordings = serverIntf.getRecordings();
if (recordings == null)
disconnect();
return recordings;
}
public static List<String> GetRecordings(bool resolveHostnames, ref string OriginalURL)
{
List<String> recordings = serverIntf.GetRecordings(resolveHostnames, ref OriginalURL);
return recordings;
}
public static String getRecordingURL(int recId)
{
string OriginalURL = "";
return serverIntf.GetRecordingURL(recId, null, true, ref OriginalURL);
}
// get a list of recorded shows
public static List<RecordedInfo> getRecordedTV()
{
List<RecordedInfo> recorded = serverIntf.GetRecordedTV();
if (recorded == null)
disconnect();
return recorded;
}
public static bool IsRecording()
{
return serverIntf.IsRecording();
}
public static bool StopRecording(int schedId)
{
return serverIntf.StopRecording(schedId);
}
public static string getRecordingInfo(int recordingId, bool withRTSPurl)
{
return serverIntf.GetRecordingInfo(recordingId, withRTSPurl);
}
// get the amount of schedules recordings
public static int getScheduleCount()
{
return serverIntf.GetScheduleCount();
}
// get a list of scheduled recordings
public static List<ScheduleInfo> getSchedules()
{
List<ScheduleInfo> schedules = serverIntf.getSchedules();
return schedules;
}
public static List<String> GetSchedules()
{
return serverIntf.GetSchedules();
}
public static String GetScheduleInfo(int index)
{
return serverIntf.GetScheduleInfo(index);
}
public static bool DeleteSchedule(int schedId)
{
return serverIntf.DeleteSchedule(schedId); // no return value
}
/**
* \brief timeshift a channel and get the stream url
* \param OriginalURL is the URL given to us by the TVServer
* \return value is the URL with resolved hostnames,
*/
public static String playChannel(int chanId, bool resolveHostnames, ref string OriginalURL, ref TvControl.User me, ref string timeShiftFileName)
{
string rtspURL = "";
string remoteserver = "";
//serverIntf.StopTimeShifting(ref me);
timeshiftChannel.Remove(me.Name);
TvResult result = serverIntf.StartTimeShifting(chanId, ref rtspURL, ref remoteserver, ref me, ref timeShiftFileName);
if (result != TvResult.Succeeded)
{
rtspURL = "[ERROR]: TVServer answer: " + result.ToString() + "|" + (int) result;
}
else
{
// we resolve any host names, as xbox can't do NETBIOS resolution..
try
{
//Workaround for MP TVserver bug when using default port for rtsp => returns rtsp://ip:0
rtspURL = rtspURL.Replace(":554", "");
rtspURL = rtspURL.Replace(":0", "");
OriginalURL = rtspURL;
if (resolveHostnames)
{
Uri u = new Uri(rtspURL);
IPAddress ipaddr = null;
try
{
ipaddr = IPAddress.Parse(u.DnsSafeHost); //Fails on a hostname
}
catch { }
if ((ipaddr == null) || (ipaddr.IsIPv6LinkLocal || ipaddr.IsIPv6Multicast || ipaddr.IsIPv6SiteLocal))
{
try
{
IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(u.DnsSafeHost);
string newHost = "";
foreach (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)
{
Log.Debug("TVServerXBMC: No IPv4 adress found for '" + u.DnsSafeHost + "' failed. Returning original URL.");
Console.WriteLine("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");
}
// update globals
timeshiftUrl = rtspURL;
timeshiftChannel.Add(me.Name, chanId);
}
Log.Debug("TVServerXBMC: PlayChannel " + chanId.ToString() + " => URL=" + rtspURL);
//Console.WriteLine("PlayChannel result : " + rtspURL);
return rtspURL;
}
public static List<Program> getEpg(int chanId, DateTime startTime, DateTime endTime)
{
List<Program> epgs = serverIntf.GetEPGForChannel(chanId.ToString(), startTime, endTime);
return epgs;
}
public static bool StopTimeshift(ref TvControl.User me)
{
bool result = serverIntf.StopTimeShifting(ref me);
timeshiftUrl = null;
return result;
}
public static string getTimeshiftUrl(ref TvControl.User me)
{
return serverIntf.GetTimeshiftUrl(ref me);
}
public static ChannelInfo getTimeshiftInfo(ref TvControl.User me)
{
int channelnr = 0;
if (timeshiftChannel.TryGetValue(me.Name, out channelnr) )
{
return serverIntf.GetChannelInfo(channelnr);
} else if (me.IdChannel > 0)
{
return serverIntf.GetChannelInfo(me.IdChannel);
}
return null;
}
public static bool DeleteRecordedTV(int recId)
{
bool result = true;
serverIntf.DeleteRecording(recId); // no return value
// bool result = IController.DeleteRecording(recId);
return result;
}
public static bool UpdateRecording(int recId, String recName)
{
return serverIntf.UpdateRecording(recId, recName);
}
public static bool AddSchedule(int channelId, String programName, DateTime startTime, DateTime endTime, int scheduleType, Int32 priority, Int32 keepmethod, DateTime keepdate, Int32 preRecordInterval, Int32 postRecordInterval)
{
return serverIntf.AddSchedule(channelId, programName, startTime, endTime, scheduleType, priority, keepmethod, keepdate, preRecordInterval, postRecordInterval);
}
public static bool UpdateSchedule(int scheduleindex, int channelId, int active, String programName, DateTime startTime, DateTime endTime, int scheduleType, Int32 priority, Int32 keepmethod, DateTime keepdate, Int32 preRecordInterval, Int32 postRecordInterval)
{
return serverIntf.UpdateSchedule(scheduleindex, channelId, active, programName, startTime, endTime, scheduleType, priority, keepmethod, keepdate, preRecordInterval, postRecordInterval);
}
public static void disconnect()
{
serverIntf.ResetConnection();
}
public static void CloseAll()
{
if (!String.IsNullOrEmpty(timeshiftUrl))
{
serverIntf.StopTimeShifting();
}
}
public static string GetVersion()
{
return serverIntf.GetVersion();
}
public static TvControl.User RequestUser(string username)
{
return serverIntf.RequestUser(username);
}
public static string GetRecordingDriveSpace()
{
return serverIntf.GetRecordingDriveSpace();
}
public static TVServerController GetServerInterface()
{
return serverIntf;
}
}
}