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

218 lines
8.1 KiB
C#
Raw Normal View History

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