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;
|
2016-10-07 08:08:13 -07:00
|
|
|
|
using System.Globalization;
|
2015-03-28 13:22:27 -07:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-10-06 11:55:01 -07:00
|
|
|
|
using MediaBrowser.Model.Dlna;
|
2015-03-28 13:22:27 -07:00
|
|
|
|
|
2016-11-03 16:35:19 -07:00
|
|
|
|
namespace Emby.Server.Implementations.LiveTv
|
2015-03-28 13:22:27 -07:00
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
2016-10-09 00:18:43 -07:00
|
|
|
|
sources = await _liveTvManager.GetRecordingMediaSources(item, cancellationToken)
|
2015-03-28 21:56:39 -07:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-10-09 00:18:43 -07:00
|
|
|
|
sources = await _liveTvManager.GetChannelMediaSources(item, cancellationToken)
|
2015-03-28 21:56:39 -07:00
|
|
|
|
.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());
|
2017-03-29 12:16:43 -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
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 00:15:29 -07:00
|
|
|
|
public async Task<Tuple<MediaSourceInfo, IDirectStreamProvider>> OpenMediaSource(string openToken, CancellationToken cancellationToken)
|
2015-03-28 13:22:27 -07:00
|
|
|
|
{
|
2016-10-05 00:15:29 -07:00
|
|
|
|
MediaSourceInfo stream = null;
|
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;
|
2016-10-05 00:15:29 -07:00
|
|
|
|
IDirectStreamProvider directStreamProvider = null;
|
2015-03-28 13:22:27 -07:00
|
|
|
|
|
|
|
|
|
if (string.Equals(keys[0], typeof(LiveTvChannel).Name, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2016-10-05 00:15:29 -07:00
|
|
|
|
var info = await _liveTvManager.GetChannelStream(keys[1], mediaSourceId, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
stream = info.Item1;
|
|
|
|
|
directStreamProvider = info.Item2;
|
2015-04-04 12:35:29 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
stream = await _liveTvManager.GetRecordingStream(keys[1], cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-10-20 11:02:42 -07:00
|
|
|
|
if (!stream.SupportsProbing || stream.MediaStreams.Any(i => i.Index != -1))
|
2016-10-07 08:08:13 -07:00
|
|
|
|
{
|
2017-03-29 12:16:43 -07:00
|
|
|
|
AddMediaInfo(stream, isAudio, cancellationToken);
|
2016-10-07 08:08:13 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-02-17 14:11:13 -07:00
|
|
|
|
await new LiveStreamHelper(_mediaEncoder, _logger).AddMediaInfoWithProbe(stream, isAudio, cancellationToken).ConfigureAwait(false);
|
2016-10-07 08:08:13 -07:00
|
|
|
|
}
|
2015-04-04 12:35:29 -07:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error probing live tv stream", ex);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-07 11:33:15 -07:00
|
|
|
|
_logger.Info("Live stream info: {0}", _jsonSerializer.SerializeToString(stream));
|
2016-10-05 00:15:29 -07:00
|
|
|
|
return new Tuple<MediaSourceInfo, IDirectStreamProvider>(stream, directStreamProvider);
|
2015-04-04 12:35:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-29 12:16:43 -07:00
|
|
|
|
private void AddMediaInfo(MediaSourceInfo mediaSource, bool isAudio, CancellationToken cancellationToken)
|
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
|
2016-10-22 12:21:50 -07:00
|
|
|
|
mediaSource.RunTimeTicks = null;
|
2015-04-19 12:17:17 -07:00
|
|
|
|
|
2016-11-03 16:35:19 -07:00
|
|
|
|
var audioStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Audio);
|
2015-04-04 12:35:29 -07:00
|
|
|
|
|
|
|
|
|
if (audioStream == null || audioStream.Index == -1)
|
|
|
|
|
{
|
|
|
|
|
mediaSource.DefaultAudioStreamIndex = null;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mediaSource.DefaultAudioStreamIndex = audioStream.Index;
|
2015-03-28 13:22:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 16:35:19 -07:00
|
|
|
|
var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Video);
|
2015-05-16 20:17:23 -07:00
|
|
|
|
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;
|
|
|
|
|
|
2017-04-19 09:57:06 -07:00
|
|
|
|
if (width >= 3000)
|
|
|
|
|
{
|
2017-04-20 20:47:17 -07:00
|
|
|
|
videoStream.BitRate = 30000000;
|
2017-04-19 09:57:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (width >= 1900)
|
2015-04-04 12:35:29 -07:00
|
|
|
|
{
|
2017-04-20 20:47:17 -07:00
|
|
|
|
videoStream.BitRate = 20000000;
|
2015-04-04 12:35:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-19 09:57:06 -07:00
|
|
|
|
else if (width >= 1200)
|
2015-04-04 12:35:29 -07:00
|
|
|
|
{
|
2017-04-20 20:47:17 -07:00
|
|
|
|
videoStream.BitRate = 8000000;
|
2015-04-04 12:35:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (width >= 700)
|
|
|
|
|
{
|
2017-04-20 20:47:17 -07:00
|
|
|
|
videoStream.BitRate = 2000000;
|
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
|
2017-01-21 13:27:07 -07:00
|
|
|
|
mediaSource.InferTotalBitrate();
|
2015-03-28 13:22:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-29 05:55:49 -07:00
|
|
|
|
public Task CloseMediaSource(string liveStreamId)
|
2015-03-28 13:22:27 -07:00
|
|
|
|
{
|
2016-09-29 05:55:49 -07:00
|
|
|
|
return _liveTvManager.CloseLiveStream(liveStreamId);
|
2015-03-28 13:22:27 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|