jellyfin/Emby.Server.Implementations/LiveTv/LiveTvMediaSourceProvider.cs

129 lines
4.6 KiB
C#
Raw Normal View History

#nullable disable
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller;
2015-05-19 00:23:03 +02:00
using MediaBrowser.Controller.Entities;
2015-03-28 21:22:27 +01:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.Dto;
2015-05-19 00:23:03 +02:00
using MediaBrowser.Model.MediaInfo;
using Microsoft.Extensions.Logging;
2015-03-28 21:22:27 +01:00
2016-11-04 00:35:19 +01:00
namespace Emby.Server.Implementations.LiveTv
2015-03-28 21:22:27 +01:00
{
public class LiveTvMediaSourceProvider : IMediaSourceProvider
{
// Do not use a pipe here because Roku http requests to the server will fail, without any explicit error message.
private const char StreamIdDelimiter = '_';
2015-03-28 21:22:27 +01:00
private readonly ILiveTvManager _liveTvManager;
2020-06-06 02:15:56 +02:00
private readonly ILogger<LiveTvMediaSourceProvider> _logger;
2015-03-31 18:24:16 +02:00
private readonly IMediaSourceManager _mediaSourceManager;
2015-05-19 00:23:03 +02:00
private readonly IServerApplicationHost _appHost;
2015-03-28 21:22:27 +01:00
public LiveTvMediaSourceProvider(ILiveTvManager liveTvManager, ILogger<LiveTvMediaSourceProvider> logger, IMediaSourceManager mediaSourceManager, IServerApplicationHost appHost)
2015-03-28 21:22:27 +01:00
{
_liveTvManager = liveTvManager;
_logger = logger;
2015-03-31 18:24:16 +02:00
_mediaSourceManager = mediaSourceManager;
2015-05-19 00:23:03 +02:00
_appHost = appHost;
2015-03-28 21:22:27 +01:00
}
2018-09-12 19:26:21 +02:00
public Task<IEnumerable<MediaSourceInfo>> GetMediaSources(BaseItem item, CancellationToken cancellationToken)
2015-03-28 21:22:27 +01:00
{
if (item.SourceType == SourceType.LiveTV)
2015-03-28 21:22:27 +01:00
{
2017-08-23 21:45:52 +02:00
var activeRecordingInfo = _liveTvManager.GetActiveRecordingInfo(item.Path);
if (string.IsNullOrEmpty(item.Path) || activeRecordingInfo != null)
2015-03-28 21:22:27 +01:00
{
2017-08-23 21:45:52 +02:00
return GetMediaSourcesInternal(item, activeRecordingInfo, cancellationToken);
2015-03-28 21:22:27 +01:00
}
}
return Task.FromResult(Enumerable.Empty<MediaSourceInfo>());
2015-03-28 21:22:27 +01:00
}
2018-09-12 19:26:21 +02:00
private async Task<IEnumerable<MediaSourceInfo>> GetMediaSourcesInternal(BaseItem item, ActiveRecordingInfo activeRecordingInfo, CancellationToken cancellationToken)
2015-03-28 21:22:27 +01:00
{
2015-03-29 06:56:39 +02:00
IEnumerable<MediaSourceInfo> sources;
2015-03-28 21:22:27 +01:00
2015-08-02 21:08:55 +02:00
var forceRequireOpening = false;
2015-03-29 06:56:39 +02:00
try
{
2018-09-12 19:26:21 +02:00
if (activeRecordingInfo != null)
2015-03-29 06:56:39 +02:00
{
2018-09-12 19:26:21 +02:00
sources = await EmbyTV.EmbyTV.Current.GetRecordingStreamMediaSources(activeRecordingInfo, cancellationToken)
2017-08-23 21:45:52 +02:00
.ConfigureAwait(false);
2015-03-29 06:56:39 +02:00
}
else
{
2018-09-12 19:26:21 +02:00
sources = await _liveTvManager.GetChannelMediaSources(item, cancellationToken)
.ConfigureAwait(false);
2015-03-29 06:56:39 +02:00
}
}
catch (NotImplementedException)
{
2018-09-12 19:26:21 +02:00
sources = _mediaSourceManager.GetStaticMediaSources(item, false);
2015-08-02 21:08:55 +02:00
forceRequireOpening = true;
2015-03-29 06:56:39 +02:00
}
2015-03-28 21:22:27 +01:00
2015-03-29 06:56:39 +02:00
var list = sources.ToList();
foreach (var source in list)
2015-03-28 21:22:27 +01:00
{
source.Type = MediaSourceType.Default;
source.BufferMs ??= 1500;
2015-03-28 21:22:27 +01:00
2015-08-02 21:08:55 +02:00
if (source.RequiresOpening || forceRequireOpening)
2015-07-24 01:40:54 +02:00
{
source.RequiresOpening = true;
}
2015-08-02 21:08:55 +02:00
if (source.RequiresOpening)
2015-07-24 01:40:54 +02:00
{
var openKeys = new List<string>
{
item.GetType().Name,
item.Id.ToString("N", CultureInfo.InvariantCulture),
source.Id ?? string.Empty
};
2020-02-19 21:04:28 +01:00
source.OpenToken = string.Join(StreamIdDelimiter, openKeys);
}
2015-05-19 00:23:03 +02:00
// Dummy this up so that direct play checks can still run
if (string.IsNullOrEmpty(source.Path) && source.Protocol == MediaProtocol.Http)
{
source.Path = _appHost.GetApiUrlForLocalAccess();
2015-05-19 00:23:03 +02:00
}
2015-03-28 21:22:27 +01:00
}
_logger.LogDebug("MediaSources: {@MediaSources}", list);
2015-03-29 06:56:39 +02:00
return list;
2015-03-28 21:22:27 +01:00
}
2020-02-19 21:04:28 +01:00
/// <inheritdoc />
2018-09-12 19:26:21 +02:00
public async Task<ILiveStream> OpenMediaSource(string openToken, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken)
2015-03-28 21:22:27 +01:00
{
var keys = openToken.Split(StreamIdDelimiter, 3);
2015-08-24 14:54:10 +02:00
var mediaSourceId = keys.Length >= 3 ? keys[2] : null;
2015-04-04 21:35:29 +02:00
2018-09-12 19:26:21 +02:00
var info = await _liveTvManager.GetChannelStream(keys[1], mediaSourceId, currentLiveStreams, cancellationToken).ConfigureAwait(false);
var liveStream = info.Item2;
2015-04-19 21:17:17 +02:00
2018-09-12 19:26:21 +02:00
return liveStream;
2015-03-28 21:22:27 +01:00
}
}
}