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

213 lines
7.6 KiB
C#
Raw Normal View History

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