jellyfin/MediaBrowser.Server.Implementations/Sync/SyncedMediaSourceProvider.cs

156 lines
6.1 KiB
C#
Raw Normal View History

2015-03-28 13:22:27 -07:00
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller;
2015-03-07 22:37:48 -07:00
using MediaBrowser.Controller.Entities;
2015-03-07 15:43:53 -07:00
using MediaBrowser.Controller.Library;
2015-03-07 16:39:24 -07:00
using MediaBrowser.Controller.Sync;
2015-03-07 15:43:53 -07:00
using MediaBrowser.Model.Dto;
2015-03-27 19:19:20 -07:00
using MediaBrowser.Model.Logging;
2015-03-07 16:39:24 -07:00
using MediaBrowser.Model.Sync;
2015-03-07 22:37:48 -07:00
using System;
2015-03-07 15:43:53 -07:00
using System.Collections.Generic;
2015-03-07 16:39:24 -07:00
using System.Linq;
2015-03-07 15:43:53 -07:00
using System.Threading;
using System.Threading.Tasks;
2016-02-05 08:31:11 -07:00
using MediaBrowser.Model.MediaInfo;
2015-03-07 15:43:53 -07:00
namespace MediaBrowser.Server.Implementations.Sync
{
public class SyncedMediaSourceProvider : IMediaSourceProvider
{
2015-03-07 22:37:48 -07:00
private readonly SyncManager _syncManager;
private readonly IServerApplicationHost _appHost;
2015-03-27 19:19:20 -07:00
private readonly ILogger _logger;
2015-03-07 16:39:24 -07:00
2015-03-27 19:19:20 -07:00
public SyncedMediaSourceProvider(ISyncManager syncManager, IServerApplicationHost appHost, ILogger logger)
2015-03-07 16:39:24 -07:00
{
2015-03-07 22:37:48 -07:00
_appHost = appHost;
2015-03-27 19:19:20 -07:00
_logger = logger;
2015-03-07 22:37:48 -07:00
_syncManager = (SyncManager)syncManager;
2015-03-07 16:39:24 -07:00
}
2015-03-07 15:43:53 -07:00
public async Task<IEnumerable<MediaSourceInfo>> GetMediaSources(IHasMediaSources item, CancellationToken cancellationToken)
{
2015-03-07 16:39:24 -07:00
var jobItemResult = _syncManager.GetJobItems(new SyncJobItemQuery
{
AddMetadata = false,
2015-03-27 19:19:20 -07:00
Statuses = new[] { SyncJobItemStatus.Synced },
2015-03-07 16:39:24 -07:00
ItemId = item.Id.ToString("N")
});
2015-03-07 22:37:48 -07:00
var list = new List<MediaSourceInfo>();
2015-03-07 16:39:24 -07:00
2015-03-07 22:37:48 -07:00
if (jobItemResult.Items.Length > 0)
{
var targets = _syncManager.ServerSyncProviders
.SelectMany(i => i.GetAllSyncTargets().Select(t => new Tuple<IServerSyncProvider, SyncTarget>(i, t)))
.ToList();
2015-03-10 11:10:38 -07:00
var serverId = _appHost.SystemId;
2015-03-07 22:37:48 -07:00
foreach (var jobItem in jobItemResult.Items)
{
var targetTuple = targets.FirstOrDefault(i => string.Equals(i.Item2.Id, jobItem.TargetId, StringComparison.OrdinalIgnoreCase));
if (targetTuple != null)
{
var syncTarget = targetTuple.Item2;
2015-03-27 19:19:20 -07:00
var syncProvider = targetTuple.Item1;
2015-03-07 22:37:48 -07:00
var dataProvider = _syncManager.GetDataProvider(targetTuple.Item1, syncTarget);
2015-04-03 18:24:49 -07:00
var localItems = await dataProvider.GetItems(syncTarget, serverId, item.Id.ToString("N")).ConfigureAwait(false);
2015-03-07 22:37:48 -07:00
foreach (var localItem in localItems)
{
2015-03-27 19:19:20 -07:00
foreach (var mediaSource in localItem.Item.MediaSources)
{
2015-03-28 13:22:27 -07:00
AddMediaSource(list, localItem, mediaSource, syncProvider, syncTarget);
2015-03-27 19:19:20 -07:00
}
}
2015-03-07 22:37:48 -07:00
}
}
}
return list;
2015-03-07 15:43:53 -07:00
}
2015-03-27 19:19:20 -07:00
2015-03-28 13:22:27 -07:00
private void AddMediaSource(List<MediaSourceInfo> list,
2015-03-27 19:19:20 -07:00
LocalItem item,
MediaSourceInfo mediaSource,
IServerSyncProvider provider,
2015-03-28 13:22:27 -07:00
SyncTarget target)
2015-03-27 19:19:20 -07:00
{
2015-03-28 13:22:27 -07:00
SetStaticMediaSourceInfo(item, mediaSource);
2015-03-27 22:07:29 -07:00
var requiresDynamicAccess = provider as IHasDynamicAccess;
2015-03-27 19:19:20 -07:00
2015-03-28 13:22:27 -07:00
if (requiresDynamicAccess != null)
2015-03-27 19:19:20 -07:00
{
2015-03-28 13:22:27 -07:00
mediaSource.RequiresOpening = true;
var keyList = new List<string>();
keyList.Add(provider.GetType().FullName.GetMD5().ToString("N"));
keyList.Add(target.Id.GetMD5().ToString("N"));
keyList.Add(item.Id);
2015-07-18 11:07:03 -07:00
mediaSource.OpenToken = string.Join(StreamIdDelimeterString, keyList.ToArray());
2015-03-27 19:19:20 -07:00
}
2015-03-29 15:38:32 -07:00
list.Add(mediaSource);
2015-03-28 13:22:27 -07:00
}
2015-03-27 19:19:20 -07:00
2015-07-18 11:07:03 -07:00
// Do not use a pipe here because Roku http requests to the server will fail, without any explicit error message.
2015-08-17 11:04:58 -07:00
private const string StreamIdDelimeterString = "_";
2015-07-18 11:07:03 -07:00
2015-03-28 21:56:39 -07:00
public async Task<MediaSourceInfo> OpenMediaSource(string openToken, CancellationToken cancellationToken)
2015-03-28 13:22:27 -07:00
{
2015-08-17 11:04:58 -07:00
var openKeys = openToken.Split(new[] { StreamIdDelimeterString[0] }, 3);
2015-03-27 19:19:20 -07:00
2015-03-28 13:22:27 -07:00
var provider = _syncManager.ServerSyncProviders
.FirstOrDefault(i => string.Equals(openKeys[0], i.GetType().FullName.GetMD5().ToString("N"), StringComparison.OrdinalIgnoreCase));
2015-03-27 19:19:20 -07:00
2015-03-28 13:22:27 -07:00
var target = provider.GetAllSyncTargets()
.FirstOrDefault(i => string.Equals(openKeys[1], i.Id.GetMD5().ToString("N"), StringComparison.OrdinalIgnoreCase));
2015-03-27 19:19:20 -07:00
2015-03-28 13:22:27 -07:00
var dataProvider = _syncManager.GetDataProvider(provider, target);
var localItem = await dataProvider.Get(target, openKeys[2]).ConfigureAwait(false);
2015-03-27 19:19:20 -07:00
2015-04-12 22:12:02 -07:00
var fileId = localItem.FileId;
if (string.IsNullOrWhiteSpace(fileId))
{
}
2015-03-28 13:22:27 -07:00
var requiresDynamicAccess = (IHasDynamicAccess)provider;
2015-04-12 22:12:02 -07:00
var dynamicInfo = await requiresDynamicAccess.GetSyncedFileInfo(fileId, target, cancellationToken).ConfigureAwait(false);
2015-03-28 13:22:27 -07:00
var mediaSource = localItem.Item.MediaSources.First();
2015-03-29 15:38:32 -07:00
mediaSource.LiveStreamId = Guid.NewGuid().ToString();
2015-03-28 13:22:27 -07:00
SetStaticMediaSourceInfo(localItem, mediaSource);
foreach (var stream in mediaSource.MediaStreams)
2015-03-27 19:19:20 -07:00
{
2015-03-29 15:38:32 -07:00
if (!string.IsNullOrWhiteSpace(stream.ExternalId))
{
var dynamicStreamInfo = await requiresDynamicAccess.GetSyncedFileInfo(stream.ExternalId, target, cancellationToken).ConfigureAwait(false);
stream.Path = dynamicStreamInfo.Path;
}
2015-03-27 19:19:20 -07:00
}
2015-03-28 13:22:27 -07:00
mediaSource.Path = dynamicInfo.Path;
mediaSource.Protocol = dynamicInfo.Protocol;
mediaSource.RequiredHttpHeaders = dynamicInfo.RequiredHttpHeaders;
return mediaSource;
}
private void SetStaticMediaSourceInfo(LocalItem item, MediaSourceInfo mediaSource)
{
mediaSource.Id = item.Id;
mediaSource.SupportsTranscoding = false;
}
2015-03-28 21:56:39 -07:00
public Task CloseMediaSource(string liveStreamId, CancellationToken cancellationToken)
2015-03-28 13:22:27 -07:00
{
throw new NotImplementedException();
2015-03-27 19:19:20 -07:00
}
2015-03-07 15:43:53 -07:00
}
}