2015-03-28 16:22:27 -04:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.LiveTv;
|
2015-04-04 15:35:29 -04:00
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2015-04-05 11:01:57 -04:00
|
|
|
|
using MediaBrowser.Model.Dlna;
|
2015-03-28 16:22:27 -04:00
|
|
|
|
using MediaBrowser.Model.Dto;
|
2015-03-29 00:56:39 -04:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2015-03-28 16:22:27 -04: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-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-03-28 16:22:27 -04:00
|
|
|
|
|
2015-04-04 15:35:29 -04:00
|
|
|
|
public LiveTvMediaSourceProvider(ILiveTvManager liveTvManager, IJsonSerializer jsonSerializer, ILogManager logManager, IMediaSourceManager mediaSourceManager, IMediaEncoder mediaEncoder)
|
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-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)
|
|
|
|
|
{
|
|
|
|
|
var channelItem = item as ILiveTvItem;
|
|
|
|
|
|
|
|
|
|
if (channelItem != null)
|
|
|
|
|
{
|
|
|
|
|
var hasMetadata = (IHasMetadata)channelItem;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(hasMetadata.Path))
|
|
|
|
|
{
|
|
|
|
|
return GetMediaSourcesInternal(channelItem, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.FromResult<IEnumerable<MediaSourceInfo>>(new List<MediaSourceInfo>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<IEnumerable<MediaSourceInfo>> GetMediaSourcesInternal(ILiveTvItem item, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2015-03-29 00:56:39 -04:00
|
|
|
|
IEnumerable<MediaSourceInfo> sources;
|
2015-03-28 16:22:27 -04:00
|
|
|
|
|
2015-03-29 00:56:39 -04: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 12:24:16 -04:00
|
|
|
|
sources = _mediaSourceManager.GetStaticMediaSources(hasMediaSources, false)
|
2015-03-29 00:56:39 -04:00
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2015-03-28 16:22:27 -04:00
|
|
|
|
|
2015-03-29 00:56:39 -04:00
|
|
|
|
var list = sources.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var source in list)
|
2015-03-28 16:22:27 -04:00
|
|
|
|
{
|
|
|
|
|
source.Type = MediaSourceType.Default;
|
|
|
|
|
source.RequiresOpening = true;
|
2015-03-29 00:56:39 -04:00
|
|
|
|
source.BufferMs = source.BufferMs ?? 1500;
|
2015-03-28 16:22:27 -04:00
|
|
|
|
|
|
|
|
|
var openKeys = new List<string>();
|
|
|
|
|
openKeys.Add(item.GetType().Name);
|
|
|
|
|
openKeys.Add(item.Id.ToString("N"));
|
2015-03-29 00:56:39 -04:00
|
|
|
|
source.OpenToken = string.Join("|", openKeys.ToArray());
|
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
|
|
|
|
}
|
|
|
|
|
|
2015-03-29 00:56:39 -04:00
|
|
|
|
public async Task<MediaSourceInfo> OpenMediaSource(string openToken, CancellationToken cancellationToken)
|
2015-03-28 16:22:27 -04:00
|
|
|
|
{
|
2015-04-04 15:35:29 -04:00
|
|
|
|
MediaSourceInfo stream;
|
|
|
|
|
var isAudio = false;
|
|
|
|
|
|
2015-03-29 00:56:39 -04:00
|
|
|
|
var keys = openToken.Split(new[] { '|' }, 2);
|
2015-03-28 16:22:27 -04:00
|
|
|
|
|
|
|
|
|
if (string.Equals(keys[0], typeof(LiveTvChannel).Name, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2015-04-04 15:35:29 -04:00
|
|
|
|
stream = await _liveTvManager.GetChannelStream(keys[1], cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
var inputPaths = new[] { mediaSource.Path };
|
|
|
|
|
|
2015-04-05 11:01:57 -04:00
|
|
|
|
var info = await _mediaEncoder.GetMediaInfo(new MediaInfoRequest
|
|
|
|
|
{
|
|
|
|
|
InputPath = mediaSource.Path,
|
|
|
|
|
Protocol = mediaSource.Protocol,
|
|
|
|
|
MediaType = isAudio ? DlnaProfileType.Audio : DlnaProfileType.Video,
|
|
|
|
|
ExtractChapters = false
|
|
|
|
|
|
|
|
|
|
}, cancellationToken)
|
2015-04-04 15:35:29 -04:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
mediaSource.Bitrate = info.Bitrate;
|
|
|
|
|
mediaSource.Container = info.Container;
|
|
|
|
|
mediaSource.Formats = info.Formats;
|
|
|
|
|
mediaSource.MediaStreams = info.MediaStreams;
|
|
|
|
|
mediaSource.RunTimeTicks = info.RunTimeTicks;
|
|
|
|
|
mediaSource.Size = info.Size;
|
|
|
|
|
mediaSource.Timestamp = info.Timestamp;
|
|
|
|
|
mediaSource.Video3DFormat = info.Video3DFormat;
|
|
|
|
|
mediaSource.VideoType = info.VideoType;
|
|
|
|
|
|
|
|
|
|
mediaSource.DefaultSubtitleStreamIndex = null;
|
|
|
|
|
|
|
|
|
|
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 16:22:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-04 15:35:29 -04:00
|
|
|
|
// Try to estimate this
|
|
|
|
|
if (!mediaSource.Bitrate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == Model.Entities.MediaStreamType.Video);
|
|
|
|
|
if (videoStream != null)
|
|
|
|
|
{
|
|
|
|
|
var width = videoStream.Width ?? 1920;
|
|
|
|
|
|
|
|
|
|
if (width >= 1900)
|
|
|
|
|
{
|
|
|
|
|
mediaSource.Bitrate = 10000000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (width >= 1260)
|
|
|
|
|
{
|
|
|
|
|
mediaSource.Bitrate = 6000000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (width >= 700)
|
|
|
|
|
{
|
|
|
|
|
mediaSource.Bitrate = 4000000;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-28 16:22:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-29 00:56:39 -04:00
|
|
|
|
public Task CloseMediaSource(string liveStreamId, CancellationToken cancellationToken)
|
2015-03-28 16:22:27 -04:00
|
|
|
|
{
|
2015-03-29 00:56:39 -04:00
|
|
|
|
return _liveTvManager.CloseLiveStream(liveStreamId, cancellationToken);
|
2015-03-28 16:22:27 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|