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