2016-10-29 15:22:20 -07:00
|
|
|
|
using MediaBrowser.Controller.Dlna;
|
|
|
|
|
using MediaBrowser.Controller.Drawing;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Session;
|
2016-10-29 15:34:54 -07:00
|
|
|
|
using Emby.Dlna.Didl;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
using MediaBrowser.Model.Dlna;
|
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Session;
|
|
|
|
|
using MediaBrowser.Model.System;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
2017-09-24 22:06:15 -07:00
|
|
|
|
using System.Linq;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
|
|
|
|
using MediaBrowser.Model.Events;
|
|
|
|
|
using MediaBrowser.Model.Globalization;
|
2017-08-19 12:43:35 -07:00
|
|
|
|
using MediaBrowser.Model.Extensions;
|
2018-09-12 10:26:21 -07:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using MediaBrowser.Model.Services;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
2016-10-29 15:34:54 -07:00
|
|
|
|
namespace Emby.Dlna.PlayTo
|
2016-10-29 15:22:20 -07:00
|
|
|
|
{
|
|
|
|
|
public class PlayToController : ISessionController, IDisposable
|
|
|
|
|
{
|
|
|
|
|
private Device _device;
|
|
|
|
|
private readonly SessionInfo _session;
|
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IDlnaManager _dlnaManager;
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
private readonly IImageProcessor _imageProcessor;
|
|
|
|
|
private readonly IUserDataManager _userDataManager;
|
|
|
|
|
private readonly ILocalizationManager _localization;
|
|
|
|
|
private readonly IMediaSourceManager _mediaSourceManager;
|
|
|
|
|
private readonly IConfigurationManager _config;
|
|
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
|
|
|
|
|
|
|
|
|
private readonly IDeviceDiscovery _deviceDiscovery;
|
|
|
|
|
private readonly string _serverAddress;
|
|
|
|
|
private readonly string _accessToken;
|
|
|
|
|
private readonly DateTime _creationTime;
|
|
|
|
|
|
|
|
|
|
public bool IsSessionActive
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-09-11 11:50:00 -07:00
|
|
|
|
return !_disposed && _device != null;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SupportsMediaControl
|
|
|
|
|
{
|
|
|
|
|
get { return IsSessionActive; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PlayToController(SessionInfo session, ISessionManager sessionManager, ILibraryManager libraryManager, ILogger logger, IDlnaManager dlnaManager, IUserManager userManager, IImageProcessor imageProcessor, string serverAddress, string accessToken, IDeviceDiscovery deviceDiscovery, IUserDataManager userDataManager, ILocalizationManager localization, IMediaSourceManager mediaSourceManager, IConfigurationManager config, IMediaEncoder mediaEncoder)
|
|
|
|
|
{
|
|
|
|
|
_session = session;
|
|
|
|
|
_sessionManager = sessionManager;
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_dlnaManager = dlnaManager;
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_imageProcessor = imageProcessor;
|
|
|
|
|
_serverAddress = serverAddress;
|
|
|
|
|
_deviceDiscovery = deviceDiscovery;
|
|
|
|
|
_userDataManager = userDataManager;
|
|
|
|
|
_localization = localization;
|
|
|
|
|
_mediaSourceManager = mediaSourceManager;
|
|
|
|
|
_config = config;
|
|
|
|
|
_mediaEncoder = mediaEncoder;
|
|
|
|
|
_accessToken = accessToken;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_creationTime = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Init(Device device)
|
|
|
|
|
{
|
|
|
|
|
_device = device;
|
|
|
|
|
_device.OnDeviceUnavailable = OnDeviceUnavailable;
|
|
|
|
|
_device.PlaybackStart += _device_PlaybackStart;
|
|
|
|
|
_device.PlaybackProgress += _device_PlaybackProgress;
|
|
|
|
|
_device.PlaybackStopped += _device_PlaybackStopped;
|
|
|
|
|
_device.MediaChanged += _device_MediaChanged;
|
|
|
|
|
|
|
|
|
|
_device.Start();
|
|
|
|
|
|
|
|
|
|
_deviceDiscovery.DeviceLeft += _deviceDiscovery_DeviceLeft;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDeviceUnavailable()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_sessionManager.ReportSessionEnded(_session.Id);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// Could throw if the session is already gone
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _deviceDiscovery_DeviceLeft(object sender, GenericEventArgs<UpnpDeviceInfo> e)
|
|
|
|
|
{
|
|
|
|
|
var info = e.Argument;
|
|
|
|
|
|
|
|
|
|
string nts;
|
|
|
|
|
info.Headers.TryGetValue("NTS", out nts);
|
|
|
|
|
|
|
|
|
|
string usn;
|
|
|
|
|
if (!info.Headers.TryGetValue("USN", out usn)) usn = String.Empty;
|
|
|
|
|
|
|
|
|
|
string nt;
|
|
|
|
|
if (!info.Headers.TryGetValue("NT", out nt)) nt = String.Empty;
|
|
|
|
|
|
|
|
|
|
if (usn.IndexOf(_device.Properties.UUID, StringComparison.OrdinalIgnoreCase) != -1 &&
|
|
|
|
|
!_disposed)
|
|
|
|
|
{
|
|
|
|
|
if (usn.IndexOf("MediaRenderer:", StringComparison.OrdinalIgnoreCase) != -1 ||
|
|
|
|
|
nt.IndexOf("MediaRenderer:", StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
|
|
{
|
|
|
|
|
OnDeviceUnavailable();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async void _device_MediaChanged(object sender, MediaChangedEventArgs e)
|
|
|
|
|
{
|
2017-01-24 12:54:18 -07:00
|
|
|
|
if (_disposed)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 15:22:20 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var streamInfo = StreamParams.ParseFromUrl(e.OldMediaInfo.Url, _libraryManager, _mediaSourceManager);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
if (streamInfo.Item != null)
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var positionTicks = GetProgressPositionTicks(e.OldMediaInfo, streamInfo);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
ReportPlaybackStopped(e.OldMediaInfo, streamInfo, positionTicks);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
streamInfo = StreamParams.ParseFromUrl(e.NewMediaInfo.Url, _libraryManager, _mediaSourceManager);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
if (streamInfo.Item == null) return;
|
|
|
|
|
|
|
|
|
|
var newItemProgress = GetProgressInfo(e.NewMediaInfo, streamInfo);
|
|
|
|
|
|
|
|
|
|
await _sessionManager.OnPlaybackStart(newItemProgress).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error reporting progress", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async void _device_PlaybackStopped(object sender, PlaybackStoppedEventArgs e)
|
|
|
|
|
{
|
2017-01-24 12:54:18 -07:00
|
|
|
|
if (_disposed)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 15:22:20 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var streamInfo = StreamParams.ParseFromUrl(e.MediaInfo.Url, _libraryManager, _mediaSourceManager);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
if (streamInfo.Item == null) return;
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var positionTicks = GetProgressPositionTicks(e.MediaInfo, streamInfo);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
ReportPlaybackStopped(e.MediaInfo, streamInfo, positionTicks);
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var mediaSource = await streamInfo.GetMediaSource(CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var duration = mediaSource == null ?
|
2016-10-29 15:22:20 -07:00
|
|
|
|
(_device.Duration == null ? (long?)null : _device.Duration.Value.Ticks) :
|
2018-09-12 10:26:21 -07:00
|
|
|
|
mediaSource.RunTimeTicks;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
var playedToCompletion = (positionTicks.HasValue && positionTicks.Value == 0);
|
|
|
|
|
|
|
|
|
|
if (!playedToCompletion && duration.HasValue && positionTicks.HasValue)
|
|
|
|
|
{
|
|
|
|
|
double percent = positionTicks.Value;
|
|
|
|
|
percent /= duration.Value;
|
|
|
|
|
|
|
|
|
|
playedToCompletion = Math.Abs(1 - percent) <= .1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (playedToCompletion)
|
|
|
|
|
{
|
|
|
|
|
await SetPlaylistIndex(_currentPlaylistIndex + 1).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Playlist.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error reporting playback stopped", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void ReportPlaybackStopped(uBaseObject mediaInfo, StreamParams streamInfo, long? positionTicks)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _sessionManager.OnPlaybackStopped(new PlaybackStopInfo
|
|
|
|
|
{
|
|
|
|
|
ItemId = streamInfo.ItemId,
|
|
|
|
|
SessionId = _session.Id,
|
|
|
|
|
PositionTicks = positionTicks,
|
|
|
|
|
MediaSourceId = streamInfo.MediaSourceId
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error reporting progress", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async void _device_PlaybackStart(object sender, PlaybackStartEventArgs e)
|
|
|
|
|
{
|
2017-01-24 12:54:18 -07:00
|
|
|
|
if (_disposed)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 15:22:20 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var info = StreamParams.ParseFromUrl(e.MediaInfo.Url, _libraryManager, _mediaSourceManager);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
if (info.Item != null)
|
|
|
|
|
{
|
|
|
|
|
var progress = GetProgressInfo(e.MediaInfo, info);
|
|
|
|
|
|
|
|
|
|
await _sessionManager.OnPlaybackStart(progress).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error reporting progress", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async void _device_PlaybackProgress(object sender, PlaybackProgressEventArgs e)
|
|
|
|
|
{
|
2017-01-24 12:54:18 -07:00
|
|
|
|
if (_disposed)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 15:22:20 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var mediaUrl = e.MediaInfo.Url;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(mediaUrl))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var info = StreamParams.ParseFromUrl(mediaUrl, _libraryManager, _mediaSourceManager);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
if (info.Item != null)
|
|
|
|
|
{
|
|
|
|
|
var progress = GetProgressInfo(e.MediaInfo, info);
|
|
|
|
|
|
|
|
|
|
await _sessionManager.OnPlaybackProgress(progress).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error reporting progress", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
private long? GetProgressPositionTicks(uBaseObject mediaInfo, StreamParams info)
|
2016-10-29 15:22:20 -07:00
|
|
|
|
{
|
|
|
|
|
var ticks = _device.Position.Ticks;
|
|
|
|
|
|
|
|
|
|
if (!EnableClientSideSeek(info))
|
|
|
|
|
{
|
|
|
|
|
ticks += info.StartPositionTicks;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return ticks;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PlaybackStartInfo GetProgressInfo(uBaseObject mediaInfo, StreamParams info)
|
|
|
|
|
{
|
2016-10-29 15:22:20 -07:00
|
|
|
|
return new PlaybackStartInfo
|
|
|
|
|
{
|
|
|
|
|
ItemId = info.ItemId,
|
|
|
|
|
SessionId = _session.Id,
|
2018-09-12 10:26:21 -07:00
|
|
|
|
PositionTicks = GetProgressPositionTicks(mediaInfo, info),
|
2016-10-29 15:22:20 -07:00
|
|
|
|
IsMuted = _device.IsMuted,
|
|
|
|
|
IsPaused = _device.IsPaused,
|
|
|
|
|
MediaSourceId = info.MediaSourceId,
|
|
|
|
|
AudioStreamIndex = info.AudioStreamIndex,
|
|
|
|
|
SubtitleStreamIndex = info.SubtitleStreamIndex,
|
|
|
|
|
VolumeLevel = _device.Volume,
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
// TODO
|
|
|
|
|
CanSeek = true,
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
2017-04-17 13:33:07 -07:00
|
|
|
|
PlayMethod = info.IsDirectStream ? PlayMethod.DirectStream : PlayMethod.Transcode
|
2016-10-29 15:22:20 -07:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region SendCommands
|
|
|
|
|
|
|
|
|
|
public async Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("{0} - Received PlayRequest: {1}", this._session.DeviceName, command.PlayCommand);
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var user = command.ControllingUserId.Equals(Guid.Empty) ? null : _userManager.GetUserById(command.ControllingUserId);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
var items = new List<BaseItem>();
|
2018-09-12 10:26:21 -07:00
|
|
|
|
foreach (var id in command.ItemIds)
|
2016-10-29 15:22:20 -07:00
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
AddItemFromId(id, items);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-21 15:14:56 -07:00
|
|
|
|
var startIndex = command.StartIndex ?? 0;
|
|
|
|
|
if (startIndex > 0)
|
|
|
|
|
{
|
|
|
|
|
items = items.Skip(startIndex).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 15:22:20 -07:00
|
|
|
|
var playlist = new List<PlaylistItem>();
|
|
|
|
|
var isFirst = true;
|
|
|
|
|
|
|
|
|
|
foreach (var item in items)
|
|
|
|
|
{
|
|
|
|
|
if (isFirst && command.StartPositionTicks.HasValue)
|
|
|
|
|
{
|
2017-11-09 13:58:09 -07:00
|
|
|
|
playlist.Add(CreatePlaylistItem(item, user, command.StartPositionTicks.Value, command.MediaSourceId, command.AudioStreamIndex, command.SubtitleStreamIndex));
|
2016-10-29 15:22:20 -07:00
|
|
|
|
isFirst = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
playlist.Add(CreatePlaylistItem(item, user, 0, null, null, null));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Debug("{0} - Playlist created", _session.DeviceName);
|
|
|
|
|
|
|
|
|
|
if (command.PlayCommand == PlayCommand.PlayLast)
|
|
|
|
|
{
|
|
|
|
|
Playlist.AddRange(playlist);
|
|
|
|
|
}
|
|
|
|
|
if (command.PlayCommand == PlayCommand.PlayNext)
|
|
|
|
|
{
|
|
|
|
|
Playlist.AddRange(playlist);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
if (!command.ControllingUserId.Equals(Guid.Empty))
|
2016-10-29 15:22:20 -07:00
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
_sessionManager.LogSessionActivity(_session.Client, _session.ApplicationVersion, _session.DeviceId,
|
|
|
|
|
_session.DeviceName, _session.RemoteEndPoint, user);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await PlayItems(playlist).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
private Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
|
2016-10-29 15:22:20 -07:00
|
|
|
|
{
|
|
|
|
|
switch (command.Command)
|
|
|
|
|
{
|
|
|
|
|
case PlaystateCommand.Stop:
|
|
|
|
|
Playlist.Clear();
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return _device.SetStop(CancellationToken.None);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
case PlaystateCommand.Pause:
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return _device.SetPause(CancellationToken.None);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
case PlaystateCommand.Unpause:
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return _device.SetPlay(CancellationToken.None);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
2017-08-27 17:33:05 -07:00
|
|
|
|
case PlaystateCommand.PlayPause:
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return _device.IsPaused ? _device.SetPlay(CancellationToken.None) : _device.SetPause(CancellationToken.None);
|
2017-08-27 17:33:05 -07:00
|
|
|
|
|
2016-10-29 15:22:20 -07:00
|
|
|
|
case PlaystateCommand.Seek:
|
|
|
|
|
{
|
|
|
|
|
return Seek(command.SeekPositionTicks ?? 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case PlaystateCommand.NextTrack:
|
|
|
|
|
return SetPlaylistIndex(_currentPlaylistIndex + 1);
|
|
|
|
|
|
|
|
|
|
case PlaystateCommand.PreviousTrack:
|
|
|
|
|
return SetPlaylistIndex(_currentPlaylistIndex - 1);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return Task.CompletedTask;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task Seek(long newPosition)
|
|
|
|
|
{
|
|
|
|
|
var media = _device.CurrentMediaInfo;
|
|
|
|
|
|
|
|
|
|
if (media != null)
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var info = StreamParams.ParseFromUrl(media.Url, _libraryManager, _mediaSourceManager);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
if (info.Item != null && !EnableClientSideSeek(info))
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var user = !_session.UserId.Equals(Guid.Empty) ? _userManager.GetUserById(_session.UserId) : null;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
var newItem = CreatePlaylistItem(info.Item, user, newPosition, info.MediaSourceId, info.AudioStreamIndex, info.SubtitleStreamIndex);
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
await _device.SetAvTransport(newItem.StreamUrl, GetDlnaHeaders(newItem), newItem.Didl, CancellationToken.None).ConfigureAwait(false);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
|
await SeekAfterTransportChange(newPosition, CancellationToken.None).ConfigureAwait(false);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool EnableClientSideSeek(StreamParams info)
|
|
|
|
|
{
|
|
|
|
|
return info.IsDirectStream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool EnableClientSideSeek(StreamInfo info)
|
|
|
|
|
{
|
|
|
|
|
return info.IsDirectStream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Playlist
|
|
|
|
|
|
|
|
|
|
private int _currentPlaylistIndex;
|
|
|
|
|
private readonly List<PlaylistItem> _playlist = new List<PlaylistItem>();
|
|
|
|
|
private List<PlaylistItem> Playlist
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _playlist;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddItemFromId(Guid id, List<BaseItem> list)
|
|
|
|
|
{
|
|
|
|
|
var item = _libraryManager.GetItemById(id);
|
|
|
|
|
if (item.MediaType == MediaType.Audio || item.MediaType == MediaType.Video)
|
|
|
|
|
{
|
|
|
|
|
list.Add(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PlaylistItem CreatePlaylistItem(BaseItem item, User user, long startPostionTicks, string mediaSourceId, int? audioStreamIndex, int? subtitleStreamIndex)
|
|
|
|
|
{
|
|
|
|
|
var deviceInfo = _device.Properties;
|
|
|
|
|
|
|
|
|
|
var profile = _dlnaManager.GetProfile(deviceInfo.ToDeviceIdentification()) ??
|
|
|
|
|
_dlnaManager.GetDefaultProfile();
|
|
|
|
|
|
|
|
|
|
var hasMediaSources = item as IHasMediaSources;
|
|
|
|
|
var mediaSources = hasMediaSources != null
|
2018-09-12 10:26:21 -07:00
|
|
|
|
? (_mediaSourceManager.GetStaticMediaSources(item, true, user))
|
2016-10-29 15:22:20 -07:00
|
|
|
|
: new List<MediaSourceInfo>();
|
|
|
|
|
|
|
|
|
|
var playlistItem = GetPlaylistItem(item, mediaSources, profile, _session.DeviceId, mediaSourceId, audioStreamIndex, subtitleStreamIndex);
|
|
|
|
|
playlistItem.StreamInfo.StartPositionTicks = startPostionTicks;
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
playlistItem.StreamUrl = DidlBuilder.NormalizeDlnaMediaUrl(playlistItem.StreamInfo.ToUrl(_serverAddress, _accessToken));
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
var itemXml = new DidlBuilder(profile, user, _imageProcessor, _serverAddress, _accessToken, _userDataManager, _localization, _mediaSourceManager, _logger, _libraryManager, _mediaEncoder)
|
2016-12-26 10:37:49 -07:00
|
|
|
|
.GetItemDidl(_config.GetDlnaConfiguration(), item, user, null, _session.DeviceId, new Filter(), playlistItem.StreamInfo);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
playlistItem.Didl = itemXml;
|
|
|
|
|
|
|
|
|
|
return playlistItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetDlnaHeaders(PlaylistItem item)
|
|
|
|
|
{
|
|
|
|
|
var profile = item.Profile;
|
|
|
|
|
var streamInfo = item.StreamInfo;
|
|
|
|
|
|
|
|
|
|
if (streamInfo.MediaType == DlnaProfileType.Audio)
|
|
|
|
|
{
|
|
|
|
|
return new ContentFeatureBuilder(profile)
|
|
|
|
|
.BuildAudioHeader(streamInfo.Container,
|
2017-09-24 22:06:15 -07:00
|
|
|
|
streamInfo.TargetAudioCodec.FirstOrDefault(),
|
2016-10-29 15:22:20 -07:00
|
|
|
|
streamInfo.TargetAudioBitrate,
|
|
|
|
|
streamInfo.TargetAudioSampleRate,
|
|
|
|
|
streamInfo.TargetAudioChannels,
|
2017-06-26 08:10:52 -07:00
|
|
|
|
streamInfo.TargetAudioBitDepth,
|
2016-10-29 15:22:20 -07:00
|
|
|
|
streamInfo.IsDirectStream,
|
2018-09-12 10:26:21 -07:00
|
|
|
|
streamInfo.RunTimeTicks ?? 0,
|
2016-10-29 15:22:20 -07:00
|
|
|
|
streamInfo.TranscodeSeekInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (streamInfo.MediaType == DlnaProfileType.Video)
|
|
|
|
|
{
|
|
|
|
|
var list = new ContentFeatureBuilder(profile)
|
|
|
|
|
.BuildVideoHeader(streamInfo.Container,
|
2017-09-24 22:06:15 -07:00
|
|
|
|
streamInfo.TargetVideoCodec.FirstOrDefault(),
|
|
|
|
|
streamInfo.TargetAudioCodec.FirstOrDefault(),
|
2016-10-29 15:22:20 -07:00
|
|
|
|
streamInfo.TargetWidth,
|
|
|
|
|
streamInfo.TargetHeight,
|
|
|
|
|
streamInfo.TargetVideoBitDepth,
|
|
|
|
|
streamInfo.TargetVideoBitrate,
|
|
|
|
|
streamInfo.TargetTimestamp,
|
|
|
|
|
streamInfo.IsDirectStream,
|
2018-09-12 10:26:21 -07:00
|
|
|
|
streamInfo.RunTimeTicks ?? 0,
|
2016-10-29 15:22:20 -07:00
|
|
|
|
streamInfo.TargetVideoProfile,
|
|
|
|
|
streamInfo.TargetVideoLevel,
|
2018-09-12 10:26:21 -07:00
|
|
|
|
streamInfo.TargetFramerate ?? 0,
|
2016-10-29 15:22:20 -07:00
|
|
|
|
streamInfo.TargetPacketLength,
|
|
|
|
|
streamInfo.TranscodeSeekInfo,
|
|
|
|
|
streamInfo.IsTargetAnamorphic,
|
2017-05-29 05:35:59 -07:00
|
|
|
|
streamInfo.IsTargetInterlaced,
|
2016-10-29 15:22:20 -07:00
|
|
|
|
streamInfo.TargetRefFrames,
|
|
|
|
|
streamInfo.TargetVideoStreamCount,
|
|
|
|
|
streamInfo.TargetAudioStreamCount,
|
|
|
|
|
streamInfo.TargetVideoCodecTag,
|
2016-12-14 13:58:55 -07:00
|
|
|
|
streamInfo.IsTargetAVC);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
2017-08-24 12:52:19 -07:00
|
|
|
|
return list.Count == 0 ? null : list[0];
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ILogger GetStreamBuilderLogger()
|
|
|
|
|
{
|
|
|
|
|
if (_config.GetDlnaConfiguration().EnableDebugLog)
|
|
|
|
|
{
|
|
|
|
|
return _logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new NullLogger();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PlaylistItem GetPlaylistItem(BaseItem item, List<MediaSourceInfo> mediaSources, DeviceProfile profile, string deviceId, string mediaSourceId, int? audioStreamIndex, int? subtitleStreamIndex)
|
|
|
|
|
{
|
|
|
|
|
if (string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return new PlaylistItem
|
|
|
|
|
{
|
|
|
|
|
StreamInfo = new StreamBuilder(_mediaEncoder, GetStreamBuilderLogger()).BuildVideoItem(new VideoOptions
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
ItemId = item.Id,
|
|
|
|
|
MediaSources = mediaSources.ToArray(),
|
2016-10-29 15:22:20 -07:00
|
|
|
|
Profile = profile,
|
|
|
|
|
DeviceId = deviceId,
|
|
|
|
|
MaxBitrate = profile.MaxStreamingBitrate,
|
|
|
|
|
MediaSourceId = mediaSourceId,
|
|
|
|
|
AudioStreamIndex = audioStreamIndex,
|
|
|
|
|
SubtitleStreamIndex = subtitleStreamIndex
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
Profile = profile
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return new PlaylistItem
|
|
|
|
|
{
|
|
|
|
|
StreamInfo = new StreamBuilder(_mediaEncoder, GetStreamBuilderLogger()).BuildAudioItem(new AudioOptions
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
ItemId = item.Id,
|
2018-12-28 08:48:26 -07:00
|
|
|
|
MediaSources = mediaSources.ToArray(),
|
2016-10-29 15:22:20 -07:00
|
|
|
|
Profile = profile,
|
|
|
|
|
DeviceId = deviceId,
|
|
|
|
|
MaxBitrate = profile.MaxStreamingBitrate,
|
|
|
|
|
MediaSourceId = mediaSourceId
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
Profile = profile
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.Equals(item.MediaType, MediaType.Photo, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return new PlaylistItemFactory().Create((Photo)item, profile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException("Unrecognized item type.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
|
_logger.Debug("{0} - Playing {1} items", _session.DeviceName, Playlist.Count);
|
|
|
|
|
|
|
|
|
|
await SetPlaylistIndex(0).ConfigureAwait(false);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SetPlaylistIndex(int index)
|
|
|
|
|
{
|
|
|
|
|
if (index < 0 || index >= Playlist.Count)
|
|
|
|
|
{
|
|
|
|
|
Playlist.Clear();
|
2018-09-12 10:26:21 -07:00
|
|
|
|
await _device.SetStop(CancellationToken.None);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_currentPlaylistIndex = index;
|
|
|
|
|
var currentitem = Playlist[index];
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
await _device.SetAvTransport(currentitem.StreamUrl, GetDlnaHeaders(currentitem), currentitem.Didl, CancellationToken.None);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
var streamInfo = currentitem.StreamInfo;
|
|
|
|
|
if (streamInfo.StartPositionTicks > 0 && EnableClientSideSeek(streamInfo))
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
await SeekAfterTransportChange(streamInfo.StartPositionTicks, CancellationToken.None).ConfigureAwait(false);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
private bool _disposed;
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (!_disposed)
|
|
|
|
|
{
|
|
|
|
|
_disposed = true;
|
|
|
|
|
|
|
|
|
|
_device.PlaybackStart -= _device_PlaybackStart;
|
|
|
|
|
_device.PlaybackProgress -= _device_PlaybackProgress;
|
|
|
|
|
_device.PlaybackStopped -= _device_PlaybackStopped;
|
|
|
|
|
_device.MediaChanged -= _device_MediaChanged;
|
|
|
|
|
//_deviceDiscovery.DeviceLeft -= _deviceDiscovery_DeviceLeft;
|
|
|
|
|
_device.OnDeviceUnavailable = null;
|
|
|
|
|
|
|
|
|
|
_device.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
private Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
|
2016-10-29 15:22:20 -07:00
|
|
|
|
{
|
|
|
|
|
GeneralCommandType commandType;
|
|
|
|
|
|
|
|
|
|
if (Enum.TryParse(command.Name, true, out commandType))
|
|
|
|
|
{
|
|
|
|
|
switch (commandType)
|
|
|
|
|
{
|
|
|
|
|
case GeneralCommandType.VolumeDown:
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return _device.VolumeDown(cancellationToken);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
case GeneralCommandType.VolumeUp:
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return _device.VolumeUp(cancellationToken);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
case GeneralCommandType.Mute:
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return _device.Mute(cancellationToken);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
case GeneralCommandType.Unmute:
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return _device.Unmute(cancellationToken);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
case GeneralCommandType.ToggleMute:
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return _device.ToggleMute(cancellationToken);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
case GeneralCommandType.SetAudioStreamIndex:
|
|
|
|
|
{
|
|
|
|
|
string arg;
|
|
|
|
|
|
|
|
|
|
if (command.Arguments.TryGetValue("Index", out arg))
|
|
|
|
|
{
|
|
|
|
|
int val;
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
if (int.TryParse(arg, NumberStyles.Integer, _usCulture, out val))
|
2016-10-29 15:22:20 -07:00
|
|
|
|
{
|
|
|
|
|
return SetAudioStreamIndex(val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException("Unsupported SetAudioStreamIndex value supplied.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException("SetAudioStreamIndex argument cannot be null");
|
|
|
|
|
}
|
|
|
|
|
case GeneralCommandType.SetSubtitleStreamIndex:
|
|
|
|
|
{
|
|
|
|
|
string arg;
|
|
|
|
|
|
|
|
|
|
if (command.Arguments.TryGetValue("Index", out arg))
|
|
|
|
|
{
|
|
|
|
|
int val;
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
if (int.TryParse(arg, NumberStyles.Integer, _usCulture, out val))
|
2016-10-29 15:22:20 -07:00
|
|
|
|
{
|
|
|
|
|
return SetSubtitleStreamIndex(val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException("Unsupported SetSubtitleStreamIndex value supplied.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException("SetSubtitleStreamIndex argument cannot be null");
|
|
|
|
|
}
|
|
|
|
|
case GeneralCommandType.SetVolume:
|
|
|
|
|
{
|
|
|
|
|
string arg;
|
|
|
|
|
|
|
|
|
|
if (command.Arguments.TryGetValue("Volume", out arg))
|
|
|
|
|
{
|
|
|
|
|
int volume;
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
if (int.TryParse(arg, NumberStyles.Integer, _usCulture, out volume))
|
2016-10-29 15:22:20 -07:00
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return _device.SetVolume(volume, cancellationToken);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException("Unsupported volume value supplied.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException("Volume argument cannot be null");
|
|
|
|
|
}
|
|
|
|
|
default:
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return Task.CompletedTask;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return Task.CompletedTask;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SetAudioStreamIndex(int? newIndex)
|
|
|
|
|
{
|
|
|
|
|
var media = _device.CurrentMediaInfo;
|
|
|
|
|
|
|
|
|
|
if (media != null)
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var info = StreamParams.ParseFromUrl(media.Url, _libraryManager, _mediaSourceManager);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
if (info.Item != null)
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var newPosition = GetProgressPositionTicks(media, info) ?? 0;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var user = !_session.UserId.Equals(Guid.Empty) ? _userManager.GetUserById(_session.UserId) : null;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
var newItem = CreatePlaylistItem(info.Item, user, newPosition, info.MediaSourceId, newIndex, info.SubtitleStreamIndex);
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
await _device.SetAvTransport(newItem.StreamUrl, GetDlnaHeaders(newItem), newItem.Didl, CancellationToken.None).ConfigureAwait(false);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
if (EnableClientSideSeek(newItem.StreamInfo))
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
await SeekAfterTransportChange(newPosition, CancellationToken.None).ConfigureAwait(false);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SetSubtitleStreamIndex(int? newIndex)
|
|
|
|
|
{
|
|
|
|
|
var media = _device.CurrentMediaInfo;
|
|
|
|
|
|
|
|
|
|
if (media != null)
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var info = StreamParams.ParseFromUrl(media.Url, _libraryManager, _mediaSourceManager);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
if (info.Item != null)
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var newPosition = GetProgressPositionTicks(media, info) ?? 0;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var user = !_session.UserId.Equals(Guid.Empty) ? _userManager.GetUserById(_session.UserId) : null;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
var newItem = CreatePlaylistItem(info.Item, user, newPosition, info.MediaSourceId, info.AudioStreamIndex, newIndex);
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
await _device.SetAvTransport(newItem.StreamUrl, GetDlnaHeaders(newItem), newItem.Didl, CancellationToken.None).ConfigureAwait(false);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
if (EnableClientSideSeek(newItem.StreamInfo) && newPosition > 0)
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
await SeekAfterTransportChange(newPosition, CancellationToken.None).ConfigureAwait(false);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
private async Task SeekAfterTransportChange(long positionTicks, CancellationToken cancellationToken)
|
2016-10-29 15:22:20 -07:00
|
|
|
|
{
|
|
|
|
|
const int maxWait = 15000000;
|
|
|
|
|
const int interval = 500;
|
|
|
|
|
var currentWait = 0;
|
|
|
|
|
while (_device.TransportState != TRANSPORTSTATE.PLAYING && currentWait < maxWait)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(interval).ConfigureAwait(false);
|
|
|
|
|
currentWait += interval;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
await _device.Seek(TimeSpan.FromTicks(positionTicks), cancellationToken).ConfigureAwait(false);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class StreamParams
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
public Guid ItemId { get; set; }
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
|
|
|
|
public bool IsDirectStream { get; set; }
|
|
|
|
|
|
|
|
|
|
public long StartPositionTicks { get; set; }
|
|
|
|
|
|
|
|
|
|
public int? AudioStreamIndex { get; set; }
|
|
|
|
|
|
|
|
|
|
public int? SubtitleStreamIndex { get; set; }
|
|
|
|
|
|
|
|
|
|
public string DeviceProfileId { get; set; }
|
|
|
|
|
public string DeviceId { get; set; }
|
|
|
|
|
|
|
|
|
|
public string MediaSourceId { get; set; }
|
|
|
|
|
public string LiveStreamId { get; set; }
|
|
|
|
|
|
|
|
|
|
public BaseItem Item { get; set; }
|
2018-09-12 10:26:21 -07:00
|
|
|
|
private MediaSourceInfo MediaSource;
|
|
|
|
|
|
|
|
|
|
private IMediaSourceManager _mediaSourceManager;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
public async Task<MediaSourceInfo> GetMediaSource(CancellationToken cancellationToken)
|
2016-10-29 15:22:20 -07:00
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
if (MediaSource != null)
|
|
|
|
|
{
|
|
|
|
|
return MediaSource;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hasMediaSources = Item as IHasMediaSources;
|
|
|
|
|
|
|
|
|
|
if (hasMediaSources == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MediaSource = await _mediaSourceManager.GetMediaSource(Item, MediaSourceId, LiveStreamId, false, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return MediaSource;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Guid GetItemId(string url)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(url))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("url");
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 15:22:20 -07:00
|
|
|
|
var parts = url.Split('/');
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < parts.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var part = parts[i];
|
|
|
|
|
|
|
|
|
|
if (string.Equals(part, "audio", StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
|
string.Equals(part, "videos", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
if (parts.Length > i + 1)
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return Guid.Parse(parts[i + 1]);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return Guid.Empty;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
public static StreamParams ParseFromUrl(string url, ILibraryManager libraryManager, IMediaSourceManager mediaSourceManager)
|
2016-10-29 15:22:20 -07:00
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
if (string.IsNullOrEmpty(url))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("url");
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 15:22:20 -07:00
|
|
|
|
var request = new StreamParams
|
|
|
|
|
{
|
|
|
|
|
ItemId = GetItemId(url)
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
if (request.ItemId.Equals(Guid.Empty))
|
2016-10-29 15:22:20 -07:00
|
|
|
|
{
|
|
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var index = url.IndexOf('?');
|
2016-10-29 15:22:20 -07:00
|
|
|
|
if (index == -1) return request;
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
var query = url.Substring(index + 1);
|
|
|
|
|
QueryParamCollection values = MyHttpUtility.ParseQueryString(query);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
request.DeviceProfileId = values.Get("DeviceProfileId");
|
|
|
|
|
request.DeviceId = values.Get("DeviceId");
|
|
|
|
|
request.MediaSourceId = values.Get("MediaSourceId");
|
|
|
|
|
request.LiveStreamId = values.Get("LiveStreamId");
|
|
|
|
|
request.IsDirectStream = string.Equals("true", values.Get("Static"), StringComparison.OrdinalIgnoreCase);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
request.AudioStreamIndex = GetIntValue(values, "AudioStreamIndex");
|
|
|
|
|
request.SubtitleStreamIndex = GetIntValue(values, "SubtitleStreamIndex");
|
|
|
|
|
request.StartPositionTicks = GetLongValue(values, "StartPositionTicks");
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
request.Item = libraryManager.GetItemById(request.ItemId);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
request._mediaSourceManager = mediaSourceManager;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
private static int? GetIntValue(QueryParamCollection values, string name)
|
|
|
|
|
{
|
|
|
|
|
var value = values.Get(name);
|
2016-10-29 15:22:20 -07:00
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
int result;
|
|
|
|
|
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result))
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static long GetLongValue(QueryParamCollection values, string name)
|
|
|
|
|
{
|
|
|
|
|
var value = values.Get(name);
|
|
|
|
|
|
|
|
|
|
long result;
|
|
|
|
|
if (long.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result))
|
|
|
|
|
{
|
|
|
|
|
return result;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
|
|
|
|
|
|
return 0;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
public Task SendMessage<T>(string name, string messageId, T data, ISessionController[] allControllers, CancellationToken cancellationToken)
|
2016-10-29 15:22:20 -07:00
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
if (_disposed)
|
|
|
|
|
{
|
|
|
|
|
throw new ObjectDisposedException(GetType().Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_device == null)
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.Equals(name, "Play", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return SendPlayCommand(data as PlayRequest, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
if (string.Equals(name, "PlayState", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return SendPlaystateCommand(data as PlaystateRequest, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
if (string.Equals(name, "GeneralCommand", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return SendGeneralCommand(data as GeneralCommand, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 15:22:20 -07:00
|
|
|
|
// Not supported or needed right now
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return Task.CompletedTask;
|
2016-10-29 15:22:20 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|