update tuner discovery

This commit is contained in:
Luke Pulverenti 2017-03-13 00:49:10 -04:00
parent 0650be4780
commit f05dc08c06
6 changed files with 70 additions and 12 deletions

View File

@ -60,6 +60,8 @@ namespace Emby.Common.Implementations.Net
var state = new AsyncReceiveState(_Socket, receivedFromEndPoint);
state.TaskCompletionSource = tcs;
cancellationToken.Register(() => tcs.TrySetCanceled());
#if NETSTANDARD1_6
_Socket.ReceiveFromAsync(new ArraySegment<Byte>(state.Buffer), SocketFlags.None, state.RemoteEndPoint)
.ContinueWith((task, asyncState) =>
@ -160,7 +162,7 @@ namespace Emby.Common.Implementations.Net
var bytesRead = receiveData();
var ipEndPoint = state.RemoteEndPoint as IPEndPoint;
state.TaskCompletionSource.SetResult(
state.TaskCompletionSource.TrySetResult(
new SocketReceiveResult
{
Buffer = state.Buffer,
@ -172,18 +174,18 @@ namespace Emby.Common.Implementations.Net
}
catch (ObjectDisposedException)
{
state.TaskCompletionSource.SetCanceled();
state.TaskCompletionSource.TrySetCanceled();
}
catch (SocketException se)
{
if (se.SocketErrorCode != SocketError.Interrupted && se.SocketErrorCode != SocketError.OperationAborted && se.SocketErrorCode != SocketError.Shutdown)
state.TaskCompletionSource.SetException(se);
state.TaskCompletionSource.TrySetException(se);
else
state.TaskCompletionSource.SetCanceled();
state.TaskCompletionSource.TrySetCanceled();
}
catch (Exception ex)
{
state.TaskCompletionSource.SetException(ex);
state.TaskCompletionSource.TrySetException(ex);
}
}
@ -206,7 +208,7 @@ namespace Emby.Common.Implementations.Net
var bytesRead = state.Socket.EndReceiveFrom(asyncResult, ref state.RemoteEndPoint);
var ipEndPoint = state.RemoteEndPoint as IPEndPoint;
state.TaskCompletionSource.SetResult(
state.TaskCompletionSource.TrySetResult(
new SocketReceiveResult
{
Buffer = state.Buffer,
@ -218,11 +220,11 @@ namespace Emby.Common.Implementations.Net
}
catch (ObjectDisposedException)
{
state.TaskCompletionSource.SetCanceled();
state.TaskCompletionSource.TrySetCanceled();
}
catch (Exception ex)
{
state.TaskCompletionSource.SetException(ex);
state.TaskCompletionSource.TrySetException(ex);
}
#endif
}

View File

@ -2542,6 +2542,60 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
public ProgramInfo Program { get; set; }
public CancellationTokenSource CancellationTokenSource { get; set; }
}
public async Task ScanForTunerDeviceChanges(CancellationToken cancellationToken)
{
foreach (var host in _liveTvManager.TunerHosts)
{
await ScanForTunerDeviceChanges(host, cancellationToken).ConfigureAwait(false);
}
}
private async Task ScanForTunerDeviceChanges(ITunerHost host, CancellationToken cancellationToken)
{
var discoveredDevices = await DiscoverDevices(host, 2000, cancellationToken).ConfigureAwait(false);
var configuredDevices = GetConfiguration().TunerHosts
.Where(i => string.Equals(i.Type, host.Type, StringComparison.OrdinalIgnoreCase))
.ToList();
foreach (var device in discoveredDevices)
{
var configuredDevice = configuredDevices.FirstOrDefault(i => string.Equals(i.DeviceId, device.DeviceId, StringComparison.OrdinalIgnoreCase));
if (configuredDevice != null)
{
if (!string.Equals(device.Url, configuredDevice.Url, StringComparison.OrdinalIgnoreCase))
{
_logger.Info("Tuner url has changed from {0} to {1}", configuredDevice.Url, device.Url);
configuredDevice.Url = device.Url;
await _liveTvManager.SaveTunerHost(configuredDevice).ConfigureAwait(false);
}
}
}
}
private async Task<List<TunerHostInfo>> DiscoverDevices(ITunerHost host, int discoveryDuationMs, CancellationToken cancellationToken)
{
try
{
var discoveredDevices = await host.DiscoverDevices(discoveryDuationMs, cancellationToken).ConfigureAwait(false);
foreach (var device in discoveredDevices)
{
_logger.Info("Discovered tuner device {0} at {1}", host.Name, device.Url);
}
return discoveredDevices;
}
catch (Exception ex)
{
_logger.ErrorException("Error discovering tuner devices", ex);
return new List<TunerHostInfo>();
}
}
}
public static class ConfigurationExtension
{

View File

@ -1180,6 +1180,8 @@ namespace Emby.Server.Implementations.LiveTv
{
EmbyTV.EmbyTV.Current.CreateRecordingFolders();
await EmbyTV.EmbyTV.Current.ScanForTunerDeviceChanges(cancellationToken).ConfigureAwait(false);
var numComplete = 0;
double progressPerService = _services.Count == 0
? 0

View File

@ -653,9 +653,9 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
public int TunerCount { get; set; }
}
public async Task<List<TunerHostInfo>> DiscoverDevices(int discoveryDurationMs)
public async Task<List<TunerHostInfo>> DiscoverDevices(int discoveryDurationMs, CancellationToken cancellationToken)
{
var cancellationToken = new CancellationTokenSource(discoveryDurationMs).Token;
cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(new CancellationTokenSource(discoveryDurationMs).Token, cancellationToken).Token;
var list = new List<TunerHostInfo>();
// Create udp broadcast discovery message

View File

@ -177,7 +177,7 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts
return Task.FromResult(true);
}
public Task<List<TunerHostInfo>> DiscoverDevices(int discoveryDurationMs)
public Task<List<TunerHostInfo>> DiscoverDevices(int discoveryDurationMs, CancellationToken cancellationToken)
{
return Task.FromResult(new List<TunerHostInfo>());
}

View File

@ -45,7 +45,7 @@ namespace MediaBrowser.Controller.LiveTv
/// <returns>Task&lt;List&lt;MediaSourceInfo&gt;&gt;.</returns>
Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(string channelId, CancellationToken cancellationToken);
Task<List<TunerHostInfo>> DiscoverDevices(int discoveryDurationMs);
Task<List<TunerHostInfo>> DiscoverDevices(int discoveryDurationMs, CancellationToken cancellationToken);
}
public interface IConfigurableTunerHost
{