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

159 lines
6.1 KiB
C#
Raw Normal View History

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