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

212 lines
7.8 KiB
C#
Raw Normal View History

2015-05-19 00:23:03 +02:00
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
2015-03-28 21:22:27 +01:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
2015-04-04 21:35:29 +02:00
using MediaBrowser.Controller.MediaEncoding;
2015-03-28 21:22:27 +01:00
using MediaBrowser.Model.Dto;
2015-03-29 06:56:39 +02:00
using MediaBrowser.Model.Logging;
2015-05-19 00:23:03 +02:00
using MediaBrowser.Model.MediaInfo;
2015-03-29 06:56:39 +02:00
using MediaBrowser.Model.Serialization;
2015-03-28 21:22:27 +01:00
using System;
using System.Collections.Generic;
2016-10-07 17:08:13 +02:00
using System.Globalization;
2015-03-28 21:22:27 +01:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2016-10-06 20:55:01 +02:00
using MediaBrowser.Model.Dlna;
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
{
private readonly ILiveTvManager _liveTvManager;
2015-03-29 06:56:39 +02:00
private readonly IJsonSerializer _jsonSerializer;
private readonly ILogger _logger;
2015-03-31 18:24:16 +02:00
private readonly IMediaSourceManager _mediaSourceManager;
2015-04-04 21:35:29 +02:00
private readonly IMediaEncoder _mediaEncoder;
2015-05-19 00:23:03 +02:00
private readonly IServerApplicationHost _appHost;
2015-03-28 21:22:27 +01:00
2015-05-19 00:23:03 +02:00
public LiveTvMediaSourceProvider(ILiveTvManager liveTvManager, IJsonSerializer jsonSerializer, ILogManager logManager, IMediaSourceManager mediaSourceManager, IMediaEncoder mediaEncoder, IServerApplicationHost appHost)
2015-03-28 21:22:27 +01:00
{
_liveTvManager = liveTvManager;
2015-03-29 06:56:39 +02:00
_jsonSerializer = jsonSerializer;
2015-03-31 18:24:16 +02:00
_mediaSourceManager = mediaSourceManager;
2015-04-04 21:35:29 +02:00
_mediaEncoder = mediaEncoder;
2015-05-19 00:23:03 +02:00
_appHost = appHost;
2015-03-29 06:56:39 +02:00
_logger = logManager.GetLogger(GetType().Name);
2015-03-28 21:22:27 +01:00
}
public Task<IEnumerable<MediaSourceInfo>> GetMediaSources(IHasMediaSources item, CancellationToken cancellationToken)
{
2016-03-19 22:17:08 +01:00
var baseItem = (BaseItem)item;
2015-03-28 21:22:27 +01:00
2016-03-19 22:17:08 +01:00
if (baseItem.SourceType == SourceType.LiveTV)
2015-03-28 21:22:27 +01:00
{
2016-03-19 22:17:08 +01:00
if (string.IsNullOrWhiteSpace(baseItem.Path))
2015-03-28 21:22:27 +01:00
{
2016-03-19 22:17:08 +01:00
return GetMediaSourcesInternal(item, cancellationToken);
2015-03-28 21:22:27 +01:00
}
}
return Task.FromResult<IEnumerable<MediaSourceInfo>>(new List<MediaSourceInfo>());
}
2015-07-18 20:07:03 +02:00
// Do not use a pipe here because Roku http requests to the server will fail, without any explicit error message.
private const char StreamIdDelimeter = '_';
2015-07-24 01:40:54 +02:00
private const string StreamIdDelimeterString = "_";
2016-03-19 22:17:08 +01:00
private async Task<IEnumerable<MediaSourceInfo>> GetMediaSourcesInternal(IHasMediaSources item, 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
{
if (item is ILiveTvRecording)
{
2016-10-09 09:18:43 +02:00
sources = await _liveTvManager.GetRecordingMediaSources(item, cancellationToken)
2015-03-29 06:56:39 +02:00
.ConfigureAwait(false);
}
else
{
2016-10-09 09:18:43 +02:00
sources = await _liveTvManager.GetChannelMediaSources(item, cancellationToken)
2015-03-29 06:56:39 +02:00
.ConfigureAwait(false);
}
}
catch (NotImplementedException)
{
var hasMediaSources = (IHasMediaSources)item;
2015-03-31 18:24:16 +02:00
sources = _mediaSourceManager.GetStaticMediaSources(hasMediaSources, false)
2015-03-29 06:56:39 +02:00
.ToList();
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();
2016-06-19 08:18:29 +02:00
var serverUrl = await _appHost.GetLocalApiUrl().ConfigureAwait(false);
2015-03-29 06:56:39 +02:00
foreach (var source in list)
2015-03-28 21:22:27 +01:00
{
source.Type = MediaSourceType.Default;
2015-08-24 14:54:10 +02:00
source.BufferMs = 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>();
openKeys.Add(item.GetType().Name);
openKeys.Add(item.Id.ToString("N"));
openKeys.Add(source.Id ?? string.Empty);
source.OpenToken = string.Join(StreamIdDelimeterString, openKeys.ToArray());
2015-08-24 14:54:10 +02:00
}
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 = serverUrl;
}
2015-03-28 21:22:27 +01:00
}
2015-03-29 06:56:39 +02:00
_logger.Debug("MediaSources: {0}", _jsonSerializer.SerializeToString(list));
return list;
2015-03-28 21:22:27 +01:00
}
2016-10-05 09:15:29 +02:00
public async Task<Tuple<MediaSourceInfo, IDirectStreamProvider>> OpenMediaSource(string openToken, CancellationToken cancellationToken)
2015-03-28 21:22:27 +01:00
{
2016-10-05 09:15:29 +02:00
MediaSourceInfo stream = null;
2015-05-16 04:36:47 +02:00
const bool isAudio = false;
2015-04-04 21:35:29 +02:00
2015-07-18 20:07:03 +02:00
var keys = openToken.Split(new[] { StreamIdDelimeter }, 3);
2015-08-24 14:54:10 +02:00
var mediaSourceId = keys.Length >= 3 ? keys[2] : null;
2016-10-05 09:15:29 +02:00
IDirectStreamProvider directStreamProvider = null;
2015-03-28 21:22:27 +01:00
if (string.Equals(keys[0], typeof(LiveTvChannel).Name, StringComparison.OrdinalIgnoreCase))
{
2016-10-05 09:15:29 +02:00
var info = await _liveTvManager.GetChannelStream(keys[1], mediaSourceId, cancellationToken).ConfigureAwait(false);
stream = info.Item1;
directStreamProvider = info.Item2;
2015-04-04 21:35:29 +02:00
}
else
{
stream = await _liveTvManager.GetRecordingStream(keys[1], cancellationToken).ConfigureAwait(false);
}
try
{
2016-10-20 20:02:42 +02:00
if (!stream.SupportsProbing || stream.MediaStreams.Any(i => i.Index != -1))
2016-10-07 17:08:13 +02:00
{
await AddMediaInfo(stream, isAudio, cancellationToken).ConfigureAwait(false);
}
else
{
2016-10-11 08:46:59 +02:00
await new LiveStreamHelper(_mediaEncoder, _logger).AddMediaInfoWithProbe(stream, isAudio, cancellationToken).ConfigureAwait(false);
2016-10-07 17:08:13 +02:00
}
2015-04-04 21:35:29 +02:00
}
catch (Exception ex)
{
_logger.ErrorException("Error probing live tv stream", ex);
}
2016-10-05 09:15:29 +02:00
return new Tuple<MediaSourceInfo, IDirectStreamProvider>(stream, directStreamProvider);
2015-04-04 21:35:29 +02:00
}
private async Task AddMediaInfo(MediaSourceInfo mediaSource, bool isAudio, CancellationToken cancellationToken)
{
mediaSource.DefaultSubtitleStreamIndex = null;
2015-04-19 21:17:17 +02:00
// Null this out so that it will be treated like a live stream
2016-10-22 21:21:50 +02:00
mediaSource.RunTimeTicks = null;
2015-04-19 21:17:17 +02:00
2016-11-04 00:35:19 +01:00
var audioStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Audio);
2015-04-04 21:35:29 +02:00
if (audioStream == null || audioStream.Index == -1)
{
mediaSource.DefaultAudioStreamIndex = null;
}
else
{
mediaSource.DefaultAudioStreamIndex = audioStream.Index;
2015-03-28 21:22:27 +01:00
}
2016-11-04 00:35:19 +01:00
var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Video);
2015-05-17 05:17:23 +02:00
if (videoStream != null)
2015-04-04 21:35:29 +02:00
{
2015-05-17 05:17:23 +02:00
if (!videoStream.BitRate.HasValue)
2015-04-04 21:35:29 +02:00
{
var width = videoStream.Width ?? 1920;
if (width >= 1900)
{
2015-05-17 05:17:23 +02:00
videoStream.BitRate = 8000000;
2015-04-04 21:35:29 +02:00
}
else if (width >= 1260)
{
2015-05-17 05:17:23 +02:00
videoStream.BitRate = 3000000;
2015-04-04 21:35:29 +02:00
}
else if (width >= 700)
{
2015-05-17 05:17:23 +02:00
videoStream.BitRate = 1000000;
2015-04-04 21:35:29 +02:00
}
}
}
2015-05-19 00:23:03 +02:00
2015-05-17 05:17:23 +02:00
// Try to estimate this
2017-01-21 21:27:07 +01:00
mediaSource.InferTotalBitrate();
2015-03-28 21:22:27 +01:00
}
2016-09-29 14:55:49 +02:00
public Task CloseMediaSource(string liveStreamId)
2015-03-28 21:22:27 +01:00
{
2016-09-29 14:55:49 +02:00
return _liveTvManager.CloseLiveStream(liveStreamId);
2015-03-28 21:22:27 +01:00
}
}
}