2019-01-13 14:22:00 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Controller;
|
2015-05-18 18:23:03 -04:00
|
|
|
|
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-05-18 18:23:03 -04:00
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2015-03-29 00:56:39 -04:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2019-01-13 14:22:00 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
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;
|
2018-09-12 13:26:21 -04:00
|
|
|
|
private IApplicationPaths _appPaths;
|
2015-03-28 16:22:27 -04:00
|
|
|
|
|
2018-12-13 08:18:25 -05:00
|
|
|
|
public LiveTvMediaSourceProvider(ILiveTvManager liveTvManager, IApplicationPaths appPaths, IJsonSerializer jsonSerializer, ILoggerFactory loggerFactory, 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;
|
2018-12-13 08:18:25 -05:00
|
|
|
|
_logger = loggerFactory.CreateLogger(GetType().Name);
|
2018-09-12 13:26:21 -04:00
|
|
|
|
_appPaths = appPaths;
|
2015-03-28 16:22:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
|
public Task<IEnumerable<MediaSourceInfo>> GetMediaSources(BaseItem item, CancellationToken cancellationToken)
|
2015-03-28 16:22:27 -04:00
|
|
|
|
{
|
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
|
|
|
|
{
|
2017-08-23 15:45:52 -04:00
|
|
|
|
var activeRecordingInfo = _liveTvManager.GetActiveRecordingInfo(item.Path);
|
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
|
if (string.IsNullOrEmpty(baseItem.Path) || activeRecordingInfo != null)
|
2015-03-28 16:22:27 -04:00
|
|
|
|
{
|
2017-08-23 15:45:52 -04:00
|
|
|
|
return GetMediaSourcesInternal(item, activeRecordingInfo, cancellationToken);
|
2015-03-28 16:22:27 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
|
return Task.FromResult<IEnumerable<MediaSourceInfo>>(Array.Empty<MediaSourceInfo>());
|
2015-03-28 16:22:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
|
private async Task<IEnumerable<MediaSourceInfo>> GetMediaSourcesInternal(BaseItem item, ActiveRecordingInfo activeRecordingInfo, 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
|
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
|
if (activeRecordingInfo != null)
|
2015-03-29 00:56:39 -04:00
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
|
sources = await EmbyTV.EmbyTV.Current.GetRecordingStreamMediaSources(activeRecordingInfo, cancellationToken)
|
2017-08-23 15:45:52 -04:00
|
|
|
|
.ConfigureAwait(false);
|
2015-03-29 00:56:39 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
|
sources = await _liveTvManager.GetChannelMediaSources(item, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
2015-03-29 00:56:39 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (NotImplementedException)
|
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
|
sources = _mediaSourceManager.GetStaticMediaSources(item, false);
|
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();
|
2017-11-23 10:46:16 -05:00
|
|
|
|
var serverUrl = await _appHost.GetLocalApiUrl(cancellationToken).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);
|
2018-12-28 10:48:26 -05:00
|
|
|
|
source.OpenToken = string.Join(StreamIdDelimeterString, openKeys.ToArray());
|
2017-03-29 15:16:43 -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
|
|
|
|
}
|
|
|
|
|
|
2018-12-13 08:18:25 -05:00
|
|
|
|
_logger.LogDebug("MediaSources: {0}", _jsonSerializer.SerializeToString(list));
|
2015-03-29 00:56:39 -04:00
|
|
|
|
|
|
|
|
|
return list;
|
2015-03-28 16:22:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
|
public async Task<ILiveStream> OpenMediaSource(string openToken, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken)
|
2015-03-28 16:22:27 -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;
|
2015-04-04 15:35:29 -04:00
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
|
var info = await _liveTvManager.GetChannelStream(keys[1], mediaSourceId, currentLiveStreams, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
var liveStream = info.Item2;
|
2015-04-19 15:17:17 -04:00
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
|
return liveStream;
|
2015-03-28 16:22:27 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|