diff --git a/MediaBrowser.Controller/IServerApplicationHost.cs b/MediaBrowser.Controller/IServerApplicationHost.cs index 5554ced37..4b2ca497b 100644 --- a/MediaBrowser.Controller/IServerApplicationHost.cs +++ b/MediaBrowser.Controller/IServerApplicationHost.cs @@ -25,5 +25,11 @@ namespace MediaBrowser.Controller /// /// true if [supports automatic run at startup]; otherwise, false. bool SupportsAutoRunAtStartup { get; } + + /// + /// Gets the HTTP server port. + /// + /// The HTTP server port. + int HttpServerPort { get; } } } diff --git a/MediaBrowser.Dlna/PlayTo/DlnaController.cs b/MediaBrowser.Dlna/PlayTo/DlnaController.cs index 2d500db89..b3f1accc7 100644 --- a/MediaBrowser.Dlna/PlayTo/DlnaController.cs +++ b/MediaBrowser.Dlna/PlayTo/DlnaController.cs @@ -1,4 +1,5 @@ using MediaBrowser.Common.Net; +using MediaBrowser.Controller; using MediaBrowser.Controller.Dlna; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; @@ -13,7 +14,6 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Timers; -using Timer = System.Timers.Timer; namespace MediaBrowser.Dlna.PlayTo { @@ -29,8 +29,11 @@ namespace MediaBrowser.Dlna.PlayTo private readonly ILogger _logger; private readonly IDlnaManager _dlnaManager; private readonly IUserManager _userManager; + private readonly IServerApplicationHost _appHost; private bool _playbackStarted = false; + private int UpdateTimerIntervalMs = 1000; + public bool SupportsMediaRemoteControl { get { return true; } @@ -47,7 +50,7 @@ namespace MediaBrowser.Dlna.PlayTo } } - public PlayToController(SessionInfo session, ISessionManager sessionManager, IItemRepository itemRepository, ILibraryManager libraryManager, ILogger logger, INetworkManager networkManager, IDlnaManager dlnaManager, IUserManager userManager) + public PlayToController(SessionInfo session, ISessionManager sessionManager, IItemRepository itemRepository, ILibraryManager libraryManager, ILogger logger, INetworkManager networkManager, IDlnaManager dlnaManager, IUserManager userManager, IServerApplicationHost appHost) { _session = session; _itemRepository = itemRepository; @@ -56,6 +59,7 @@ namespace MediaBrowser.Dlna.PlayTo _networkManager = networkManager; _dlnaManager = dlnaManager; _userManager = userManager; + _appHost = appHost; _logger = logger; } @@ -66,14 +70,12 @@ namespace MediaBrowser.Dlna.PlayTo _device.CurrentIdChanged += Device_CurrentIdChanged; _device.Start(); - _updateTimer = new Timer(1000); - _updateTimer.Elapsed += updateTimer_Elapsed; - _updateTimer.Start(); + _updateTimer = new System.Threading.Timer(updateTimer_Elapsed, null, UpdateTimerIntervalMs, UpdateTimerIntervalMs); } #region Device EventHandlers & Update Timer - Timer _updateTimer; + System.Threading.Timer _updateTimer; async void Device_PlaybackChanged(object sender, TransportStateEventArgs e) { @@ -124,27 +126,23 @@ namespace MediaBrowser.Dlna.PlayTo /// /// Handles the Elapsed event of the updateTimer control. /// - /// The source of the event. - /// The instance containing the event data. - async void updateTimer_Elapsed(object sender, ElapsedEventArgs e) + /// The state. + private async void updateTimer_Elapsed(object state) { if (_disposed) return; - ((Timer)sender).Stop(); - - - if (!IsSessionActive) + if (IsSessionActive) { - //Session is inactive, mark it for Disposal and don't start the elapsed timer. - await _sessionManager.ReportSessionEnded(this._session.Id); - return; + await ReportProgress().ConfigureAwait(false); } + else + { + _updateTimer.Change(Timeout.Infinite, Timeout.Infinite); - await ReportProgress().ConfigureAwait(false); - - if (!_disposed && IsSessionActive) - ((Timer)sender).Start(); + //Session is inactive, mark it for Disposal and don't start the elapsed timer. + await _sessionManager.ReportSessionEnded(_session.Id); + } } /// @@ -387,7 +385,7 @@ namespace MediaBrowser.Dlna.PlayTo "http", _networkManager.GetLocalIpAddresses().FirstOrDefault() ?? "localhost", - "8096" + _appHost.HttpServerPort ); } @@ -493,8 +491,8 @@ namespace MediaBrowser.Dlna.PlayTo { if (!_disposed) { - _updateTimer.Stop(); _disposed = true; + _updateTimer.Dispose(); _device.Dispose(); _logger.Log(LogSeverity.Debug, "Controller disposed"); } diff --git a/MediaBrowser.Dlna/PlayTo/PlayToManager.cs b/MediaBrowser.Dlna/PlayTo/PlayToManager.cs index 0eab41b1b..2fc03f2f9 100644 --- a/MediaBrowser.Dlna/PlayTo/PlayToManager.cs +++ b/MediaBrowser.Dlna/PlayTo/PlayToManager.cs @@ -1,4 +1,5 @@ using MediaBrowser.Common.Net; +using MediaBrowser.Controller; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Dlna; using MediaBrowser.Controller.Library; @@ -32,8 +33,9 @@ namespace MediaBrowser.Dlna.PlayTo private readonly IUserManager _userManager; private readonly IDlnaManager _dlnaManager; private readonly IServerConfigurationManager _config; + private readonly IServerApplicationHost _appHost; - public PlayToManager(ILogger logger, IServerConfigurationManager config, ISessionManager sessionManager, IHttpClient httpClient, IItemRepository itemRepository, ILibraryManager libraryManager, INetworkManager networkManager, IUserManager userManager, IDlnaManager dlnaManager) + public PlayToManager(ILogger logger, IServerConfigurationManager config, ISessionManager sessionManager, IHttpClient httpClient, IItemRepository itemRepository, ILibraryManager libraryManager, INetworkManager networkManager, IUserManager userManager, IDlnaManager dlnaManager, IServerApplicationHost appHost) { _locations = new ConcurrentDictionary(); _tokenSource = new CancellationTokenSource(); @@ -46,6 +48,7 @@ namespace MediaBrowser.Dlna.PlayTo _networkManager = networkManager; _userManager = userManager; _dlnaManager = dlnaManager; + _appHost = appHost; _config = config; } @@ -227,7 +230,7 @@ namespace MediaBrowser.Dlna.PlayTo if (controller == null) { - sessionInfo.SessionController = controller = new PlayToController(sessionInfo, _sessionManager, _itemRepository, _libraryManager, _logger, _networkManager, _dlnaManager, _userManager); + sessionInfo.SessionController = controller = new PlayToController(sessionInfo, _sessionManager, _itemRepository, _libraryManager, _logger, _networkManager, _dlnaManager, _userManager, _appHost); } controller.Init(device); diff --git a/MediaBrowser.Dlna/PlayTo/PlayToServerEntryPoint.cs b/MediaBrowser.Dlna/PlayTo/PlayToServerEntryPoint.cs index a7afeab3c..91b03bc23 100644 --- a/MediaBrowser.Dlna/PlayTo/PlayToServerEntryPoint.cs +++ b/MediaBrowser.Dlna/PlayTo/PlayToServerEntryPoint.cs @@ -1,4 +1,5 @@ using MediaBrowser.Common.Net; +using MediaBrowser.Controller; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Dlna; using MediaBrowser.Controller.Library; @@ -22,8 +23,9 @@ namespace MediaBrowser.Dlna.PlayTo private readonly INetworkManager _networkManager; private readonly IUserManager _userManager; private readonly IDlnaManager _dlnaManager; + private readonly IServerApplicationHost _appHost; - public PlayToServerEntryPoint(ILogManager logManager, IServerConfigurationManager config, ISessionManager sessionManager, IHttpClient httpClient, IItemRepository itemRepo, ILibraryManager libraryManager, INetworkManager networkManager, IUserManager userManager, IDlnaManager dlnaManager) + public PlayToServerEntryPoint(ILogManager logManager, IServerConfigurationManager config, ISessionManager sessionManager, IHttpClient httpClient, IItemRepository itemRepo, ILibraryManager libraryManager, INetworkManager networkManager, IUserManager userManager, IDlnaManager dlnaManager, IServerApplicationHost appHost) { _config = config; _sessionManager = sessionManager; @@ -33,6 +35,7 @@ namespace MediaBrowser.Dlna.PlayTo _networkManager = networkManager; _userManager = userManager; _dlnaManager = dlnaManager; + _appHost = appHost; _logger = logManager.GetLogger("PlayTo"); } @@ -69,7 +72,7 @@ namespace MediaBrowser.Dlna.PlayTo { try { - _manager = new PlayToManager(_logger, _config, _sessionManager, _httpClient, _itemRepo, _libraryManager, _networkManager, _userManager, _dlnaManager); + _manager = new PlayToManager(_logger, _config, _sessionManager, _httpClient, _itemRepo, _libraryManager, _networkManager, _userManager, _dlnaManager, _appHost); _manager.Start(); } catch (Exception ex) diff --git a/MediaBrowser.ServerApplication/ApplicationHost.cs b/MediaBrowser.ServerApplication/ApplicationHost.cs index f55f18cc3..43aeb0ab0 100644 --- a/MediaBrowser.ServerApplication/ApplicationHost.cs +++ b/MediaBrowser.ServerApplication/ApplicationHost.cs @@ -861,7 +861,7 @@ namespace MediaBrowser.ServerApplication ItemsByNamePath = ApplicationPaths.ItemsByNamePath, CachePath = ApplicationPaths.CachePath, MacAddress = GetMacAddress(), - HttpServerPortNumber = ServerConfigurationManager.Configuration.HttpServerPortNumber, + HttpServerPortNumber = HttpServerPort, OperatingSystem = Environment.OSVersion.ToString(), CanSelfRestart = CanSelfRestart, CanSelfUpdate = CanSelfUpdate, @@ -874,6 +874,11 @@ namespace MediaBrowser.ServerApplication }; } + public int HttpServerPort + { + get { return ServerConfigurationManager.Configuration.HttpServerPortNumber; } + } + private readonly CultureInfo _usCulture = new CultureInfo("en-US"); private string GetWanAddress() {