2014-04-16 02:17:48 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-03-17 04:25:11 +00:00
|
|
|
|
using MediaBrowser.Controller;
|
2014-03-13 19:08:02 +00:00
|
|
|
|
using MediaBrowser.Controller.Dlna;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-03-22 18:25:03 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Persistence;
|
|
|
|
|
using MediaBrowser.Controller.Session;
|
2014-04-01 22:23:07 +00:00
|
|
|
|
using MediaBrowser.Model.Dlna;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Session;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-04-16 02:17:48 +00:00
|
|
|
|
using System.Globalization;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Dlna.PlayTo
|
|
|
|
|
{
|
|
|
|
|
public class PlayToController : ISessionController, IDisposable
|
|
|
|
|
{
|
|
|
|
|
private Device _device;
|
2014-03-26 15:06:48 +00:00
|
|
|
|
private BaseItem _currentItem;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
private readonly SessionInfo _session;
|
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
private readonly IItemRepository _itemRepository;
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly INetworkManager _networkManager;
|
|
|
|
|
private readonly ILogger _logger;
|
2014-03-13 19:08:02 +00:00
|
|
|
|
private readonly IDlnaManager _dlnaManager;
|
2014-03-16 04:23:58 +00:00
|
|
|
|
private readonly IUserManager _userManager;
|
2014-03-17 04:25:11 +00:00
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
2014-03-26 15:06:48 +00:00
|
|
|
|
private bool _playbackStarted;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-03-21 03:31:40 +00:00
|
|
|
|
private const int UpdateTimerIntervalMs = 1000;
|
2014-03-17 04:25:11 +00:00
|
|
|
|
|
2014-03-10 10:10:16 +00:00
|
|
|
|
public bool SupportsMediaRemoteControl
|
|
|
|
|
{
|
|
|
|
|
get { return true; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsSessionActive
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_device == null || _device.UpdateTime == default(DateTime))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return DateTime.UtcNow <= _device.UpdateTime.AddSeconds(30);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-17 04:25:11 +00:00
|
|
|
|
public PlayToController(SessionInfo session, ISessionManager sessionManager, IItemRepository itemRepository, ILibraryManager libraryManager, ILogger logger, INetworkManager networkManager, IDlnaManager dlnaManager, IUserManager userManager, IServerApplicationHost appHost)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
_session = session;
|
|
|
|
|
_itemRepository = itemRepository;
|
|
|
|
|
_sessionManager = sessionManager;
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_networkManager = networkManager;
|
2014-03-13 21:00:17 +00:00
|
|
|
|
_dlnaManager = dlnaManager;
|
2014-03-16 04:23:58 +00:00
|
|
|
|
_userManager = userManager;
|
2014-03-17 04:25:11 +00:00
|
|
|
|
_appHost = appHost;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-13 19:08:02 +00:00
|
|
|
|
public void Init(Device device)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
_device = device;
|
|
|
|
|
_device.PlaybackChanged += Device_PlaybackChanged;
|
|
|
|
|
_device.CurrentIdChanged += Device_CurrentIdChanged;
|
|
|
|
|
_device.Start();
|
|
|
|
|
|
2014-03-22 03:35:03 +00:00
|
|
|
|
_updateTimer = new Timer(updateTimer_Elapsed, null, UpdateTimerIntervalMs, UpdateTimerIntervalMs);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Device EventHandlers & Update Timer
|
|
|
|
|
|
2014-03-22 03:35:03 +00:00
|
|
|
|
Timer _updateTimer;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
async void Device_PlaybackChanged(object sender, TransportStateEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (_currentItem == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (e.Stopped == false)
|
|
|
|
|
await ReportProgress().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
else if (e.Stopped && _playbackStarted)
|
|
|
|
|
{
|
|
|
|
|
_playbackStarted = false;
|
|
|
|
|
|
2014-04-16 02:17:48 +00:00
|
|
|
|
await _sessionManager.OnPlaybackStopped(new PlaybackStopInfo
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-16 02:17:48 +00:00
|
|
|
|
ItemId = _currentItem.Id.ToString("N"),
|
2014-03-10 10:10:16 +00:00
|
|
|
|
SessionId = _session.Id,
|
|
|
|
|
PositionTicks = _device.Position.Ticks
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
await SetNext().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async void Device_CurrentIdChanged(object sender, CurrentIdEventArgs e)
|
|
|
|
|
{
|
2014-03-26 15:06:48 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(e.Id))
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-03-26 15:06:48 +00:00
|
|
|
|
Guid guid;
|
|
|
|
|
|
|
|
|
|
if (Guid.TryParse(e.Id, out guid))
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-03-26 15:06:48 +00:00
|
|
|
|
if (_currentItem != null && _currentItem.Id == guid)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-03-26 15:06:48 +00:00
|
|
|
|
var item = _libraryManager.GetItemById(guid);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-03-26 15:06:48 +00:00
|
|
|
|
if (item != null)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("{0} - CurrentId {1}", _session.DeviceName, item.Id);
|
|
|
|
|
_currentItem = item;
|
|
|
|
|
_playbackStarted = false;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-03-26 15:06:48 +00:00
|
|
|
|
await ReportProgress().ConfigureAwait(false);
|
|
|
|
|
}
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the Elapsed event of the updateTimer control.
|
|
|
|
|
/// </summary>
|
2014-03-17 04:25:11 +00:00
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
|
private async void updateTimer_Elapsed(object state)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
if (_disposed)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-03-17 04:25:11 +00:00
|
|
|
|
if (IsSessionActive)
|
2014-03-10 10:16:03 +00:00
|
|
|
|
{
|
2014-03-17 04:25:11 +00:00
|
|
|
|
await ReportProgress().ConfigureAwait(false);
|
2014-03-10 10:16:03 +00:00
|
|
|
|
}
|
2014-03-17 04:25:11 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_updateTimer.Change(Timeout.Infinite, Timeout.Infinite);
|
2014-03-10 10:16:03 +00:00
|
|
|
|
|
2014-03-25 05:25:03 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Session is inactive, mark it for Disposal and don't start the elapsed timer.
|
|
|
|
|
await _sessionManager.ReportSessionEnded(_session.Id);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error in ReportSessionEnded", ex);
|
|
|
|
|
}
|
2014-03-17 04:25:11 +00:00
|
|
|
|
}
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reports the playback progress.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private async Task ReportProgress()
|
|
|
|
|
{
|
|
|
|
|
if (_currentItem == null || _device.IsStopped)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-04-06 18:34:47 +00:00
|
|
|
|
var playlistItem = Playlist.FirstOrDefault(p => p.PlayState == 1);
|
2014-04-16 02:17:48 +00:00
|
|
|
|
|
2014-04-06 18:34:47 +00:00
|
|
|
|
if (playlistItem != null)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-06 18:34:47 +00:00
|
|
|
|
if (!_playbackStarted)
|
2014-03-25 05:25:03 +00:00
|
|
|
|
{
|
2014-04-16 02:17:48 +00:00
|
|
|
|
await _sessionManager.OnPlaybackStart(new PlaybackStartInfo
|
2014-04-06 18:34:47 +00:00
|
|
|
|
{
|
2014-04-16 02:17:48 +00:00
|
|
|
|
ItemId = _currentItem.Id.ToString("N"),
|
2014-04-06 18:34:47 +00:00
|
|
|
|
SessionId = _session.Id,
|
|
|
|
|
CanSeek = true,
|
|
|
|
|
QueueableMediaTypes = new List<string> { _currentItem.MediaType },
|
|
|
|
|
MediaSourceId = playlistItem.MediaSourceId,
|
|
|
|
|
AudioStreamIndex = playlistItem.AudioStreamIndex,
|
2014-04-16 02:17:48 +00:00
|
|
|
|
SubtitleStreamIndex = playlistItem.SubtitleStreamIndex,
|
|
|
|
|
IsMuted = _device.IsMuted,
|
|
|
|
|
IsPaused = _device.IsPaused,
|
|
|
|
|
VolumeLevel = _device.Volume
|
2014-03-25 05:25:03 +00:00
|
|
|
|
|
2014-04-06 18:34:47 +00:00
|
|
|
|
}).ConfigureAwait(false);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-06 18:34:47 +00:00
|
|
|
|
_playbackStarted = true;
|
|
|
|
|
}
|
2014-04-06 17:53:23 +00:00
|
|
|
|
|
2014-04-06 18:34:47 +00:00
|
|
|
|
if ((_device.IsPlaying || _device.IsPaused))
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-06 17:53:23 +00:00
|
|
|
|
var ticks = _device.Position.Ticks;
|
|
|
|
|
|
|
|
|
|
if (playlistItem.Transcode)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-06 17:53:23 +00:00
|
|
|
|
ticks += playlistItem.StartPositionTicks;
|
|
|
|
|
}
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-04-16 02:17:48 +00:00
|
|
|
|
await _sessionManager.OnPlaybackProgress(new PlaybackProgressInfo
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
2014-04-16 02:17:48 +00:00
|
|
|
|
ItemId = _currentItem.Id.ToString("N"),
|
2014-03-10 10:10:16 +00:00
|
|
|
|
SessionId = _session.Id,
|
2014-04-06 17:53:23 +00:00
|
|
|
|
PositionTicks = ticks,
|
2014-03-10 10:10:16 +00:00
|
|
|
|
IsMuted = _device.IsMuted,
|
2014-04-06 17:53:23 +00:00
|
|
|
|
IsPaused = _device.IsPaused,
|
|
|
|
|
MediaSourceId = playlistItem.MediaSourceId,
|
|
|
|
|
AudioStreamIndex = playlistItem.AudioStreamIndex,
|
2014-04-15 03:54:52 +00:00
|
|
|
|
SubtitleStreamIndex = playlistItem.SubtitleStreamIndex,
|
2014-04-16 02:17:48 +00:00
|
|
|
|
VolumeLevel = _device.Volume,
|
|
|
|
|
CanSeek = true
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region SendCommands
|
|
|
|
|
|
2014-03-16 04:23:58 +00:00
|
|
|
|
public async Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
|
2014-03-10 10:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
_logger.Debug("{0} - Received PlayRequest: {1}", this._session.DeviceName, command.PlayCommand);
|
|
|
|
|
|
|
|
|
|
var items = new List<BaseItem>();
|
|
|
|
|
foreach (string id in command.ItemIds)
|
|
|
|
|
{
|
|
|
|
|
AddItemFromId(Guid.Parse(id), items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var playlist = new List<PlaylistItem>();
|
|
|
|
|
var isFirst = true;
|
|
|
|
|
|
|
|
|
|
var serverAddress = GetServerAddress();
|
|
|
|
|
|
|
|
|
|
foreach (var item in items)
|
|
|
|
|
{
|
|
|
|
|
if (isFirst && command.StartPositionTicks.HasValue)
|
|
|
|
|
{
|
|
|
|
|
playlist.Add(CreatePlaylistItem(item, command.StartPositionTicks.Value, serverAddress));
|
|
|
|
|
isFirst = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
playlist.Add(CreatePlaylistItem(item, 0, serverAddress));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Debug("{0} - Playlist created", _session.DeviceName);
|
|
|
|
|
|
|
|
|
|
if (command.PlayCommand == PlayCommand.PlayLast)
|
|
|
|
|
{
|
|
|
|
|
AddItemsToPlaylist(playlist);
|
|
|
|
|
}
|
|
|
|
|
if (command.PlayCommand == PlayCommand.PlayNext)
|
|
|
|
|
{
|
|
|
|
|
AddItemsToPlaylist(playlist);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Debug("{0} - Playing {1} items", _session.DeviceName, playlist.Count);
|
2014-03-16 04:23:58 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(command.ControllingUserId))
|
|
|
|
|
{
|
|
|
|
|
var userId = new Guid(command.ControllingUserId);
|
|
|
|
|
|
|
|
|
|
var user = _userManager.GetUserById(userId);
|
|
|
|
|
|
|
|
|
|
await _sessionManager.LogSessionActivity(_session.Client, _session.ApplicationVersion, _session.DeviceId,
|
|
|
|
|
_session.DeviceName, _session.RemoteEndPoint, user).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await PlayItems(playlist).ConfigureAwait(false);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
switch (command.Command)
|
|
|
|
|
{
|
|
|
|
|
case PlaystateCommand.Stop:
|
|
|
|
|
Playlist.Clear();
|
|
|
|
|
return _device.SetStop();
|
|
|
|
|
|
|
|
|
|
case PlaystateCommand.Pause:
|
|
|
|
|
return _device.SetPause();
|
|
|
|
|
|
|
|
|
|
case PlaystateCommand.Unpause:
|
|
|
|
|
return _device.SetPlay();
|
|
|
|
|
|
|
|
|
|
case PlaystateCommand.Seek:
|
2014-04-06 17:53:23 +00:00
|
|
|
|
//var playlistItem = Playlist.FirstOrDefault(p => p.PlayState == 1);
|
|
|
|
|
//if (playlistItem != null && playlistItem.Transcode && _currentItem != null)
|
|
|
|
|
//{
|
|
|
|
|
// var newItem = CreatePlaylistItem(_currentItem, command.SeekPositionTicks ?? 0, GetServerAddress());
|
|
|
|
|
// playlistItem.StartPositionTicks = newItem.StartPositionTicks;
|
|
|
|
|
// playlistItem.StreamUrl = newItem.StreamUrl;
|
|
|
|
|
// playlistItem.Didl = newItem.Didl;
|
|
|
|
|
// return _device.SetAvTransport(playlistItem.StreamUrl, GetDlnaHeaders(playlistItem), playlistItem.Didl);
|
|
|
|
|
|
|
|
|
|
//}
|
2014-03-10 10:10:16 +00:00
|
|
|
|
return _device.Seek(TimeSpan.FromTicks(command.SeekPositionTicks ?? 0));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case PlaystateCommand.NextTrack:
|
|
|
|
|
_currentItem = null;
|
|
|
|
|
return SetNext();
|
|
|
|
|
|
|
|
|
|
case PlaystateCommand.PreviousTrack:
|
|
|
|
|
_currentItem = null;
|
|
|
|
|
return SetPrevious();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendRestartRequiredNotification(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendServerRestartNotification(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 17:53:23 +00:00
|
|
|
|
public Task SendSessionEndedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-12 17:27:53 +00:00
|
|
|
|
public Task SendPlaybackStartNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendPlaybackStoppedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-10 10:10:16 +00:00
|
|
|
|
public Task SendServerShutdownNotification(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendMessageCommand(MessageCommand command, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Playlist
|
|
|
|
|
|
|
|
|
|
private List<PlaylistItem> _playlist = new List<PlaylistItem>();
|
|
|
|
|
|
|
|
|
|
private List<PlaylistItem> Playlist
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _playlist;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_playlist = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddItemFromId(Guid id, List<BaseItem> list)
|
|
|
|
|
{
|
|
|
|
|
var item = _libraryManager.GetItemById(id);
|
|
|
|
|
if (item.IsFolder)
|
|
|
|
|
{
|
|
|
|
|
foreach (var childId in _itemRepository.GetChildren(item.Id))
|
|
|
|
|
{
|
|
|
|
|
AddItemFromId(childId, list);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (item.MediaType == MediaType.Audio || item.MediaType == MediaType.Video)
|
|
|
|
|
{
|
|
|
|
|
list.Add(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetServerAddress()
|
|
|
|
|
{
|
|
|
|
|
return string.Format("{0}://{1}:{2}/mediabrowser",
|
|
|
|
|
|
|
|
|
|
"http",
|
|
|
|
|
_networkManager.GetLocalIpAddresses().FirstOrDefault() ?? "localhost",
|
2014-03-17 04:25:11 +00:00
|
|
|
|
_appHost.HttpServerPort
|
2014-03-10 10:10:16 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PlaylistItem CreatePlaylistItem(BaseItem item, long startPostionTicks, string serverAddress)
|
|
|
|
|
{
|
2014-03-23 05:10:33 +00:00
|
|
|
|
var streams = _itemRepository.GetMediaStreams(new MediaStreamQuery
|
|
|
|
|
{
|
|
|
|
|
ItemId = item.Id
|
|
|
|
|
|
|
|
|
|
}).ToList();
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-03-13 19:08:02 +00:00
|
|
|
|
var deviceInfo = _device.Properties;
|
|
|
|
|
|
2014-03-26 15:17:36 +00:00
|
|
|
|
var profile = _dlnaManager.GetProfile(deviceInfo.ToDeviceIdentification()) ??
|
|
|
|
|
_dlnaManager.GetDefaultProfile();
|
2014-03-23 16:42:02 +00:00
|
|
|
|
|
|
|
|
|
var playlistItem = GetPlaylistItem(item, streams, profile);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
playlistItem.StartPositionTicks = startPostionTicks;
|
2014-03-26 15:06:48 +00:00
|
|
|
|
playlistItem.DeviceProfileId = profile.Id;
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
2014-03-22 18:25:03 +00:00
|
|
|
|
if (playlistItem.MediaType == DlnaProfileType.Audio)
|
|
|
|
|
{
|
2014-03-23 05:10:33 +00:00
|
|
|
|
playlistItem.StreamUrl = StreamHelper.GetAudioUrl(deviceInfo, playlistItem, streams, serverAddress);
|
2014-03-22 18:25:03 +00:00
|
|
|
|
}
|
2014-03-10 10:10:16 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
playlistItem.StreamUrl = StreamHelper.GetVideoUrl(_device.Properties, playlistItem, streams, serverAddress);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-28 16:53:45 +00:00
|
|
|
|
playlistItem.Didl = DidlBuilder.Build(item, _session.UserId.ToString(), serverAddress, playlistItem.StreamUrl, streams, profile.EnableAlbumArtInDidl);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
|
|
|
|
|
return playlistItem;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-23 05:10:33 +00:00
|
|
|
|
private string GetDlnaHeaders(PlaylistItem item)
|
|
|
|
|
{
|
|
|
|
|
var orgOp = item.Transcode ? ";DLNA.ORG_OP=00" : ";DLNA.ORG_OP=01";
|
|
|
|
|
|
|
|
|
|
var orgCi = item.Transcode ? ";DLNA.ORG_CI=0" : ";DLNA.ORG_CI=1";
|
|
|
|
|
|
|
|
|
|
const string dlnaflags = ";DLNA.ORG_FLAGS=01500000000000000000000000000000";
|
|
|
|
|
|
|
|
|
|
string contentFeatures;
|
|
|
|
|
|
|
|
|
|
var container = item.Container.TrimStart('.');
|
|
|
|
|
|
|
|
|
|
if (string.Equals(container, "mp3", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
contentFeatures = "DLNA.ORG_PN=MP3";
|
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(container, "wma", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
contentFeatures = "DLNA.ORG_PN=WMABASE";
|
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(container, "wmw", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
contentFeatures = "DLNA.ORG_PN=WMVMED_BASE";
|
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(container, "asf", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
contentFeatures = "DLNA.ORG_PN=WMVMED_BASE";
|
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(container, "avi", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
contentFeatures = "DLNA.ORG_PN=AVI";
|
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(container, "mkv", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
contentFeatures = "DLNA.ORG_PN=MATROSKA";
|
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(container, "mp4", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
contentFeatures = "DLNA.ORG_PN=AVC_MP4_MP_HD_720p_AAC";
|
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(container, "mpeg", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
contentFeatures = "DLNA.ORG_PN=MPEG_PS_PAL";
|
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(container, "ts", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
contentFeatures = "DLNA.ORG_PN=MPEG_PS_PAL";
|
|
|
|
|
}
|
|
|
|
|
else if (item.MediaType == DlnaProfileType.Video)
|
|
|
|
|
{
|
|
|
|
|
// Default to AVI for video
|
|
|
|
|
contentFeatures = "DLNA.ORG_PN=AVI";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Default to MP3 for audio
|
|
|
|
|
contentFeatures = "DLNA.ORG_PN=MP3";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (contentFeatures + orgOp + orgCi + dlnaflags).Trim(';');
|
|
|
|
|
}
|
2014-04-06 18:34:47 +00:00
|
|
|
|
|
2014-03-23 05:10:33 +00:00
|
|
|
|
private PlaylistItem GetPlaylistItem(BaseItem item, List<MediaStream> mediaStreams, DeviceProfile profile)
|
2014-03-22 18:25:03 +00:00
|
|
|
|
{
|
|
|
|
|
var video = item as Video;
|
|
|
|
|
|
|
|
|
|
if (video != null)
|
|
|
|
|
{
|
2014-03-23 05:10:33 +00:00
|
|
|
|
return new PlaylistItemFactory().Create(video, mediaStreams, profile);
|
2014-03-22 18:25:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var audio = item as Audio;
|
|
|
|
|
|
|
|
|
|
if (audio != null)
|
|
|
|
|
{
|
2014-03-23 05:10:33 +00:00
|
|
|
|
return new PlaylistItemFactory().Create(audio, mediaStreams, profile);
|
2014-03-22 18:25:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var photo = item as Photo;
|
|
|
|
|
|
|
|
|
|
if (photo != null)
|
|
|
|
|
{
|
2014-03-23 05:10:33 +00:00
|
|
|
|
return new PlaylistItemFactory().Create(photo, profile);
|
2014-03-22 18:25:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException("Unrecognized item type.");
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-10 10:10:16 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Plays the items.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="items">The items.</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private async Task<bool> PlayItems(IEnumerable<PlaylistItem> items)
|
|
|
|
|
{
|
|
|
|
|
Playlist.Clear();
|
|
|
|
|
Playlist.AddRange(items);
|
|
|
|
|
await SetNext();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds the items to playlist.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="items">The items.</param>
|
|
|
|
|
private void AddItemsToPlaylist(IEnumerable<PlaylistItem> items)
|
|
|
|
|
{
|
|
|
|
|
Playlist.AddRange(items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<bool> SetNext()
|
|
|
|
|
{
|
|
|
|
|
if (!Playlist.Any() || Playlist.All(i => i.PlayState != 0))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
var currentitem = Playlist.FirstOrDefault(i => i.PlayState == 1);
|
|
|
|
|
|
|
|
|
|
if (currentitem != null)
|
|
|
|
|
{
|
|
|
|
|
currentitem.PlayState = 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var nextTrack = Playlist.FirstOrDefault(i => i.PlayState == 0);
|
|
|
|
|
if (nextTrack == null)
|
|
|
|
|
{
|
|
|
|
|
await _device.SetStop();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-03-23 05:10:33 +00:00
|
|
|
|
|
2014-03-10 10:10:16 +00:00
|
|
|
|
nextTrack.PlayState = 1;
|
2014-03-23 05:10:33 +00:00
|
|
|
|
|
|
|
|
|
var dlnaheaders = GetDlnaHeaders(nextTrack);
|
|
|
|
|
|
|
|
|
|
_logger.Debug("{0} - SetAvTransport Uri: {1} DlnaHeaders: {2}", _device.Properties.Name, nextTrack.StreamUrl, dlnaheaders);
|
|
|
|
|
|
|
|
|
|
await _device.SetAvTransport(nextTrack.StreamUrl, dlnaheaders, nextTrack.Didl);
|
2014-04-06 18:34:47 +00:00
|
|
|
|
|
2014-03-10 10:10:16 +00:00
|
|
|
|
if (nextTrack.StartPositionTicks > 0 && !nextTrack.Transcode)
|
|
|
|
|
await _device.Seek(TimeSpan.FromTicks(nextTrack.StartPositionTicks));
|
2014-04-06 18:34:47 +00:00
|
|
|
|
|
2014-03-10 10:10:16 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<bool> SetPrevious()
|
|
|
|
|
{
|
|
|
|
|
if (!Playlist.Any() || Playlist.All(i => i.PlayState != 2))
|
|
|
|
|
return Task.FromResult(false);
|
|
|
|
|
|
|
|
|
|
var currentitem = Playlist.FirstOrDefault(i => i.PlayState == 1);
|
|
|
|
|
|
|
|
|
|
var prevTrack = Playlist.LastOrDefault(i => i.PlayState == 2);
|
|
|
|
|
|
|
|
|
|
if (currentitem != null)
|
|
|
|
|
{
|
|
|
|
|
currentitem.PlayState = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prevTrack == null)
|
|
|
|
|
return Task.FromResult(false);
|
|
|
|
|
|
|
|
|
|
prevTrack.PlayState = 1;
|
2014-03-23 05:10:33 +00:00
|
|
|
|
return _device.SetAvTransport(prevTrack.StreamUrl, GetDlnaHeaders(prevTrack), prevTrack.Didl);
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
private bool _disposed;
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (!_disposed)
|
|
|
|
|
{
|
|
|
|
|
_disposed = true;
|
2014-03-17 04:25:11 +00:00
|
|
|
|
_updateTimer.Dispose();
|
2014-03-10 10:10:16 +00:00
|
|
|
|
_device.Dispose();
|
2014-03-15 16:11:10 +00:00
|
|
|
|
_logger.Log(LogSeverity.Debug, "Controller disposed");
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-28 19:58:18 +00:00
|
|
|
|
|
2014-04-15 03:54:52 +00:00
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
2014-04-16 02:17:48 +00:00
|
|
|
|
|
2014-03-31 21:04:22 +00:00
|
|
|
|
public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
|
2014-03-28 19:58:18 +00:00
|
|
|
|
{
|
2014-03-31 21:04:22 +00:00
|
|
|
|
GeneralCommandType commandType;
|
|
|
|
|
|
2014-04-14 06:42:51 +00:00
|
|
|
|
if (Enum.TryParse(command.Name, true, out commandType))
|
2014-03-31 21:04:22 +00:00
|
|
|
|
{
|
|
|
|
|
switch (commandType)
|
|
|
|
|
{
|
|
|
|
|
case GeneralCommandType.VolumeDown:
|
|
|
|
|
return _device.VolumeDown();
|
|
|
|
|
case GeneralCommandType.VolumeUp:
|
|
|
|
|
return _device.VolumeUp();
|
|
|
|
|
case GeneralCommandType.Mute:
|
|
|
|
|
return _device.VolumeDown(true);
|
|
|
|
|
case GeneralCommandType.Unmute:
|
|
|
|
|
return _device.VolumeUp(true);
|
|
|
|
|
case GeneralCommandType.ToggleMute:
|
|
|
|
|
return _device.ToggleMute();
|
2014-04-15 03:54:52 +00:00
|
|
|
|
case GeneralCommandType.SetVolume:
|
|
|
|
|
{
|
2014-04-16 02:17:48 +00:00
|
|
|
|
string volumeArg;
|
2014-04-15 03:54:52 +00:00
|
|
|
|
|
2014-04-16 02:17:48 +00:00
|
|
|
|
if (command.Arguments.TryGetValue("Volume", out volumeArg))
|
2014-04-15 03:54:52 +00:00
|
|
|
|
{
|
2014-04-16 02:17:48 +00:00
|
|
|
|
int volume;
|
|
|
|
|
|
|
|
|
|
if (int.TryParse(volumeArg, NumberStyles.Any, _usCulture, out volume))
|
|
|
|
|
{
|
|
|
|
|
return _device.SetVolume(volume);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException("Unsupported volume value supplied.");
|
2014-04-15 03:54:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-16 02:17:48 +00:00
|
|
|
|
throw new ArgumentException("Volume argument cannot be null");
|
2014-04-15 03:54:52 +00:00
|
|
|
|
}
|
2014-03-31 21:04:22 +00:00
|
|
|
|
default:
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(true);
|
2014-03-28 19:58:18 +00:00
|
|
|
|
}
|
2014-03-10 10:10:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|