You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Server.Impleme.../Sync/SyncedMediaSourceProvider.cs

155 lines
6.0 KiB

using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller;
10 years ago
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Sync;
using MediaBrowser.Model.Dto;
10 years ago
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Sync;
10 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Sync
{
public class SyncedMediaSourceProvider : IMediaSourceProvider
{
10 years ago
private readonly SyncManager _syncManager;
private readonly IServerApplicationHost _appHost;
10 years ago
private readonly ILogger _logger;
10 years ago
public SyncedMediaSourceProvider(ISyncManager syncManager, IServerApplicationHost appHost, ILogger logger)
{
10 years ago
_appHost = appHost;
10 years ago
_logger = logger;
10 years ago
_syncManager = (SyncManager)syncManager;
}
public async Task<IEnumerable<MediaSourceInfo>> GetMediaSources(IHasMediaSources item, CancellationToken cancellationToken)
{
var jobItemResult = _syncManager.GetJobItems(new SyncJobItemQuery
{
AddMetadata = false,
10 years ago
Statuses = new[] { SyncJobItemStatus.Synced },
ItemId = item.Id.ToString("N")
});
10 years ago
var list = new List<MediaSourceInfo>();
10 years ago
if (jobItemResult.Items.Length > 0)
{
var targets = _syncManager.ServerSyncProviders
.SelectMany(i => i.GetAllSyncTargets().Select(t => new Tuple<IServerSyncProvider, SyncTarget>(i, t)))
.ToList();
var serverId = _appHost.SystemId;
10 years ago
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;
10 years ago
var syncProvider = targetTuple.Item1;
10 years ago
var dataProvider = _syncManager.GetDataProvider(targetTuple.Item1, syncTarget);
var localItems = await dataProvider.GetItems(syncTarget, serverId, item.Id.ToString("N")).ConfigureAwait(false);
10 years ago
foreach (var localItem in localItems)
{
10 years ago
foreach (var mediaSource in localItem.Item.MediaSources)
{
AddMediaSource(list, localItem, mediaSource, syncProvider, syncTarget);
10 years ago
}
}
10 years ago
}
}
}
return list;
}
10 years ago
private void AddMediaSource(List<MediaSourceInfo> list,
10 years ago
LocalItem item,
MediaSourceInfo mediaSource,
IServerSyncProvider provider,
SyncTarget target)
10 years ago
{
SetStaticMediaSourceInfo(item, mediaSource);
10 years ago
var requiresDynamicAccess = provider as IHasDynamicAccess;
10 years ago
if (requiresDynamicAccess != null)
10 years ago
{
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);
mediaSource.OpenToken = string.Join(StreamIdDelimeterString, keyList.ToArray());
10 years ago
}
10 years ago
list.Add(mediaSource);
}
10 years ago
// Do not use a pipe here because Roku http requests to the server will fail, without any explicit error message.
private const string StreamIdDelimeterString = "_";
public async Task<MediaSourceInfo> OpenMediaSource(string openToken, CancellationToken cancellationToken)
{
var openKeys = openToken.Split(new[] { StreamIdDelimeterString[0] }, 3);
10 years ago
var provider = _syncManager.ServerSyncProviders
.FirstOrDefault(i => string.Equals(openKeys[0], i.GetType().FullName.GetMD5().ToString("N"), StringComparison.OrdinalIgnoreCase));
10 years ago
var target = provider.GetAllSyncTargets()
.FirstOrDefault(i => string.Equals(openKeys[1], i.Id.GetMD5().ToString("N"), StringComparison.OrdinalIgnoreCase));
10 years ago
var dataProvider = _syncManager.GetDataProvider(provider, target);
var localItem = await dataProvider.Get(target, openKeys[2]).ConfigureAwait(false);
10 years ago
var fileId = localItem.FileId;
if (string.IsNullOrWhiteSpace(fileId))
{
}
var requiresDynamicAccess = (IHasDynamicAccess)provider;
var dynamicInfo = await requiresDynamicAccess.GetSyncedFileInfo(fileId, target, cancellationToken).ConfigureAwait(false);
var mediaSource = localItem.Item.MediaSources.First();
10 years ago
mediaSource.LiveStreamId = Guid.NewGuid().ToString();
SetStaticMediaSourceInfo(localItem, mediaSource);
foreach (var stream in mediaSource.MediaStreams)
10 years ago
{
10 years ago
if (!string.IsNullOrWhiteSpace(stream.ExternalId))
{
var dynamicStreamInfo = await requiresDynamicAccess.GetSyncedFileInfo(stream.ExternalId, target, cancellationToken).ConfigureAwait(false);
stream.Path = dynamicStreamInfo.Path;
}
10 years ago
}
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;
}
public Task CloseMediaSource(string liveStreamId, CancellationToken cancellationToken)
{
throw new NotImplementedException();
10 years ago
}
}
}