2021-05-20 19:28:18 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2019-11-01 17:38:54 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 19:21:32 +00:00
|
|
|
using System;
|
2020-09-23 21:12:26 +00:00
|
|
|
using System.Collections.Concurrent;
|
2019-01-13 19:21:32 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2020-12-23 12:12:40 +00:00
|
|
|
using System.Text.Json;
|
2019-01-13 19:21:32 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-05-20 17:07:53 +00:00
|
|
|
using Jellyfin.Data.Entities;
|
2020-05-13 02:10:35 +00:00
|
|
|
using Jellyfin.Data.Enums;
|
2021-10-02 17:59:58 +00:00
|
|
|
using Jellyfin.Extensions.Json;
|
2019-01-13 19:21:32 +00:00
|
|
|
using MediaBrowser.Common.Configuration;
|
2019-01-06 20:50:43 +00:00
|
|
|
using MediaBrowser.Common.Extensions;
|
2023-12-28 19:34:44 +00:00
|
|
|
using MediaBrowser.Controller;
|
2015-03-07 22:43:53 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
2023-12-28 19:34:44 +00:00
|
|
|
using MediaBrowser.Controller.LiveTv;
|
2015-03-07 22:43:53 +00:00
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2015-02-07 21:03:09 +00:00
|
|
|
using MediaBrowser.Controller.Persistence;
|
2018-12-14 19:17:29 +00:00
|
|
|
using MediaBrowser.Controller.Providers;
|
2019-01-13 19:21:32 +00:00
|
|
|
using MediaBrowser.Model.Dlna;
|
2015-03-07 22:43:53 +00:00
|
|
|
using MediaBrowser.Model.Dto;
|
2015-02-07 21:03:09 +00:00
|
|
|
using MediaBrowser.Model.Entities;
|
2019-01-13 19:21:32 +00:00
|
|
|
using MediaBrowser.Model.Globalization;
|
|
|
|
using MediaBrowser.Model.IO;
|
2015-03-26 04:44:24 +00:00
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2019-01-13 19:21:32 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2015-02-07 21:03:09 +00:00
|
|
|
|
2016-11-03 06:37:52 +00:00
|
|
|
namespace Emby.Server.Implementations.Library
|
2015-02-07 21:03:09 +00:00
|
|
|
{
|
2015-03-28 20:22:27 +00:00
|
|
|
public class MediaSourceManager : IMediaSourceManager, IDisposable
|
2015-02-07 21:03:09 +00:00
|
|
|
{
|
2020-07-20 09:01:37 +00:00
|
|
|
// Do not use a pipe here because Roku http requests to the server will fail, without any explicit error message.
|
|
|
|
private const char LiveStreamIdDelimeter = '_';
|
|
|
|
|
2023-12-28 19:34:44 +00:00
|
|
|
private readonly IServerApplicationHost _appHost;
|
2015-02-07 21:03:09 +00:00
|
|
|
private readonly IItemRepository _itemRepo;
|
2015-03-07 22:43:53 +00:00
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2015-09-20 17:56:26 +00:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2020-06-06 00:15:56 +00:00
|
|
|
private readonly ILogger<MediaSourceManager> _logger;
|
2016-02-20 23:06:57 +00:00
|
|
|
private readonly IUserDataManager _userDataManager;
|
2020-04-04 19:28:21 +00:00
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
|
|
|
private readonly ILocalizationManager _localizationManager;
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
2021-11-07 21:32:08 +00:00
|
|
|
private readonly IDirectoryService _directoryService;
|
2020-04-04 19:28:21 +00:00
|
|
|
|
2020-09-23 21:12:26 +00:00
|
|
|
private readonly ConcurrentDictionary<string, ILiveStream> _openStreams = new ConcurrentDictionary<string, ILiveStream>(StringComparer.OrdinalIgnoreCase);
|
2020-07-20 09:01:37 +00:00
|
|
|
private readonly SemaphoreSlim _liveStreamSemaphore = new SemaphoreSlim(1, 1);
|
2021-03-09 04:57:38 +00:00
|
|
|
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
|
2020-07-20 09:01:37 +00:00
|
|
|
|
2020-04-04 19:28:21 +00:00
|
|
|
private IMediaSourceProvider[] _providers;
|
2015-03-07 22:43:53 +00:00
|
|
|
|
2019-01-17 22:55:05 +00:00
|
|
|
public MediaSourceManager(
|
2023-12-28 19:34:44 +00:00
|
|
|
IServerApplicationHost appHost,
|
2019-01-17 22:55:05 +00:00
|
|
|
IItemRepository itemRepo,
|
|
|
|
IApplicationPaths applicationPaths,
|
|
|
|
ILocalizationManager localizationManager,
|
|
|
|
IUserManager userManager,
|
|
|
|
ILibraryManager libraryManager,
|
2020-04-04 19:28:21 +00:00
|
|
|
ILogger<MediaSourceManager> logger,
|
2019-01-17 22:55:05 +00:00
|
|
|
IFileSystem fileSystem,
|
|
|
|
IUserDataManager userDataManager,
|
2021-11-07 21:32:08 +00:00
|
|
|
IMediaEncoder mediaEncoder,
|
|
|
|
IDirectoryService directoryService)
|
2015-02-07 21:03:09 +00:00
|
|
|
{
|
2023-12-28 19:34:44 +00:00
|
|
|
_appHost = appHost;
|
2015-02-07 21:03:09 +00:00
|
|
|
_itemRepo = itemRepo;
|
2015-03-07 22:43:53 +00:00
|
|
|
_userManager = userManager;
|
|
|
|
_libraryManager = libraryManager;
|
2020-04-04 19:28:21 +00:00
|
|
|
_logger = logger;
|
2015-09-20 17:56:26 +00:00
|
|
|
_fileSystem = fileSystem;
|
2016-02-20 23:06:57 +00:00
|
|
|
_userDataManager = userDataManager;
|
2018-09-12 17:26:21 +00:00
|
|
|
_mediaEncoder = mediaEncoder;
|
|
|
|
_localizationManager = localizationManager;
|
|
|
|
_appPaths = applicationPaths;
|
2021-11-07 21:32:08 +00:00
|
|
|
_directoryService = directoryService;
|
2015-03-07 22:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void AddParts(IEnumerable<IMediaSourceProvider> providers)
|
|
|
|
{
|
|
|
|
_providers = providers.ToArray();
|
2015-02-07 21:03:09 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 19:02:33 +00:00
|
|
|
public List<MediaStream> GetMediaStreams(MediaStreamQuery query)
|
2015-02-07 21:03:09 +00:00
|
|
|
{
|
2017-08-05 19:02:33 +00:00
|
|
|
var list = _itemRepo.GetMediaStreams(query);
|
2015-02-07 21:03:09 +00:00
|
|
|
|
|
|
|
foreach (var stream in list)
|
|
|
|
{
|
|
|
|
stream.SupportsExternalStream = StreamSupportsExternalStream(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
private static bool StreamSupportsExternalStream(MediaStream stream)
|
2015-02-07 21:03:09 +00:00
|
|
|
{
|
|
|
|
if (stream.IsExternal)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream.IsTextSubtitleStream)
|
|
|
|
{
|
2015-12-14 13:54:21 +00:00
|
|
|
return true;
|
2015-02-07 21:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-05 19:02:33 +00:00
|
|
|
public List<MediaStream> GetMediaStreams(Guid itemId)
|
2015-03-03 07:03:17 +00:00
|
|
|
{
|
|
|
|
var list = GetMediaStreams(new MediaStreamQuery
|
|
|
|
{
|
|
|
|
ItemId = itemId
|
|
|
|
});
|
|
|
|
|
|
|
|
return GetMediaStreamsForItem(list);
|
|
|
|
}
|
|
|
|
|
2017-10-13 19:22:24 +00:00
|
|
|
private List<MediaStream> GetMediaStreamsForItem(List<MediaStream> streams)
|
2015-03-03 07:03:17 +00:00
|
|
|
{
|
2017-10-13 19:22:24 +00:00
|
|
|
foreach (var stream in streams)
|
2015-03-03 07:03:17 +00:00
|
|
|
{
|
2017-10-13 19:22:24 +00:00
|
|
|
if (stream.Type == MediaStreamType.Subtitle)
|
2015-03-03 07:03:17 +00:00
|
|
|
{
|
2017-10-13 19:22:24 +00:00
|
|
|
stream.SupportsExternalStream = StreamSupportsExternalStream(stream);
|
2015-03-03 07:03:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 19:22:24 +00:00
|
|
|
return streams;
|
2015-03-03 07:03:17 +00:00
|
|
|
}
|
2015-03-07 22:43:53 +00:00
|
|
|
|
2019-11-07 16:23:28 +00:00
|
|
|
/// <inheritdoc />
|
2019-10-07 11:53:28 +00:00
|
|
|
public List<MediaAttachment> GetMediaAttachments(MediaAttachmentQuery query)
|
|
|
|
{
|
2019-11-04 15:37:19 +00:00
|
|
|
return _itemRepo.GetMediaAttachments(query);
|
2019-10-07 11:53:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 16:27:55 +00:00
|
|
|
/// <inheritdoc />
|
2019-10-07 11:53:28 +00:00
|
|
|
public List<MediaAttachment> GetMediaAttachments(Guid itemId)
|
|
|
|
{
|
2019-11-04 15:37:19 +00:00
|
|
|
return GetMediaAttachments(new MediaAttachmentQuery
|
2019-10-07 11:53:28 +00:00
|
|
|
{
|
|
|
|
ItemId = itemId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:07:53 +00:00
|
|
|
public async Task<List<MediaSourceInfo>> GetPlaybackMediaSources(BaseItem item, User user, bool allowMediaProbe, bool enablePathSubstitution, CancellationToken cancellationToken)
|
2015-03-07 22:43:53 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var mediaSources = GetStaticMediaSources(item, enablePathSubstitution, user);
|
2015-03-07 22:43:53 +00:00
|
|
|
|
2022-05-04 14:20:48 +00:00
|
|
|
// If file is strm or main media stream is missing, force a metadata refresh with remote probing
|
|
|
|
if (allowMediaProbe && mediaSources[0].Type != MediaSourceType.Placeholder
|
|
|
|
&& (item.Path.EndsWith(".strm", StringComparison.OrdinalIgnoreCase)
|
2023-04-06 17:27:57 +00:00
|
|
|
|| (item.MediaType == MediaType.Video && mediaSources[0].MediaStreams.All(i => i.Type != MediaStreamType.Video))
|
|
|
|
|| (item.MediaType == MediaType.Audio && mediaSources[0].MediaStreams.All(i => i.Type != MediaStreamType.Audio))))
|
2015-03-07 22:43:53 +00:00
|
|
|
{
|
2019-09-10 20:37:53 +00:00
|
|
|
await item.RefreshMetadata(
|
2021-11-07 21:32:08 +00:00
|
|
|
new MetadataRefreshOptions(_directoryService)
|
2019-09-10 20:37:53 +00:00
|
|
|
{
|
|
|
|
EnableRemoteContentProbe = true,
|
|
|
|
MetadataRefreshMode = MetadataRefreshMode.FullRefresh
|
|
|
|
},
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
mediaSources = GetStaticMediaSources(item, enablePathSubstitution, user);
|
2015-03-07 22:43:53 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var dynamicMediaSources = await GetDynamicMediaSources(item, cancellationToken).ConfigureAwait(false);
|
2015-03-07 23:39:24 +00:00
|
|
|
|
|
|
|
var list = new List<MediaSourceInfo>();
|
|
|
|
|
|
|
|
list.AddRange(mediaSources);
|
2015-03-08 16:25:46 +00:00
|
|
|
|
|
|
|
foreach (var source in dynamicMediaSources)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
// Validate that this is actually possible
|
|
|
|
if (source.SupportsDirectStream)
|
2015-03-26 04:44:24 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
source.SupportsDirectStream = SupportsDirectStream(source.Path, source.Protocol);
|
2015-03-26 04:44:24 +00:00
|
|
|
}
|
2015-03-17 03:51:35 +00:00
|
|
|
|
2022-12-05 14:01:13 +00:00
|
|
|
if (user is not null)
|
2015-04-05 15:01:57 +00:00
|
|
|
{
|
2022-03-17 16:13:46 +00:00
|
|
|
SetDefaultAudioAndSubtitleStreamIndexes(item, source, user);
|
|
|
|
|
2023-05-13 18:44:31 +00:00
|
|
|
if (item.MediaType == MediaType.Audio)
|
2015-04-05 15:01:57 +00:00
|
|
|
{
|
2020-05-13 02:10:35 +00:00
|
|
|
source.SupportsTranscoding = user.HasPermission(PermissionKind.EnableAudioPlaybackTranscoding);
|
2015-04-09 05:20:23 +00:00
|
|
|
}
|
2023-05-13 18:44:31 +00:00
|
|
|
else if (item.MediaType == MediaType.Video)
|
2021-04-19 08:49:52 +00:00
|
|
|
{
|
|
|
|
source.SupportsTranscoding = user.HasPermission(PermissionKind.EnableVideoPlaybackTranscoding);
|
|
|
|
source.SupportsDirectStream = user.HasPermission(PermissionKind.EnablePlaybackRemuxing);
|
|
|
|
}
|
2015-04-05 15:01:57 +00:00
|
|
|
}
|
2022-03-17 16:13:46 +00:00
|
|
|
|
|
|
|
list.Add(source);
|
2015-04-05 15:01:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 08:49:52 +00:00
|
|
|
return SortMediaSources(list);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 21:32:08 +00:00
|
|
|
/// <inheritdoc />>
|
2018-09-12 17:26:21 +00:00
|
|
|
public MediaProtocol GetPathProtocol(string path)
|
|
|
|
{
|
|
|
|
if (path.StartsWith("Rtsp", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return MediaProtocol.Rtsp;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (path.StartsWith("Rtmp", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return MediaProtocol.Rtmp;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (path.StartsWith("Http", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return MediaProtocol.Http;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (path.StartsWith("rtp", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return MediaProtocol.Rtp;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (path.StartsWith("ftp", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return MediaProtocol.Ftp;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (path.StartsWith("udp", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return MediaProtocol.Udp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _fileSystem.IsPathFile(path) ? MediaProtocol.File : MediaProtocol.Http;
|
2015-03-07 23:39:24 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public bool SupportsDirectStream(string path, MediaProtocol protocol)
|
|
|
|
{
|
|
|
|
if (protocol == MediaProtocol.File)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (protocol == MediaProtocol.Http)
|
|
|
|
{
|
2022-12-05 14:01:13 +00:00
|
|
|
if (path is not null)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2021-11-07 21:32:08 +00:00
|
|
|
if (path.Contains(".m3u", StringComparison.OrdinalIgnoreCase))
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<IEnumerable<MediaSourceInfo>> GetDynamicMediaSources(BaseItem item, CancellationToken cancellationToken)
|
2015-03-07 23:39:24 +00:00
|
|
|
{
|
|
|
|
var tasks = _providers.Select(i => GetDynamicMediaSources(item, i, cancellationToken));
|
|
|
|
var results = await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
|
|
|
|
return results.SelectMany(i => i.ToList());
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private async Task<IEnumerable<MediaSourceInfo>> GetDynamicMediaSources(BaseItem item, IMediaSourceProvider provider, CancellationToken cancellationToken)
|
2015-03-07 23:39:24 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-03-28 20:22:27 +00:00
|
|
|
var sources = await provider.GetMediaSources(item, cancellationToken).ConfigureAwait(false);
|
|
|
|
var list = sources.ToList();
|
|
|
|
|
|
|
|
foreach (var mediaSource in list)
|
|
|
|
{
|
2017-01-21 20:27:07 +00:00
|
|
|
mediaSource.InferTotalBitrate();
|
|
|
|
|
2015-03-28 20:22:27 +00:00
|
|
|
SetKeyProperties(provider, mediaSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
2015-03-07 23:39:24 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
_logger.LogError(ex, "Error getting media sources");
|
2021-11-07 21:32:08 +00:00
|
|
|
return Enumerable.Empty<MediaSourceInfo>();
|
2015-03-07 23:39:24 +00:00
|
|
|
}
|
2015-03-07 22:43:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
private static void SetKeyProperties(IMediaSourceProvider provider, MediaSourceInfo mediaSource)
|
2015-03-28 20:22:27 +00:00
|
|
|
{
|
2019-02-28 22:22:57 +00:00
|
|
|
var prefix = provider.GetType().FullName.GetMD5().ToString("N", CultureInfo.InvariantCulture) + LiveStreamIdDelimeter;
|
2015-03-28 20:22:27 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (!string.IsNullOrEmpty(mediaSource.OpenToken) && !mediaSource.OpenToken.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
|
2015-03-28 20:22:27 +00:00
|
|
|
{
|
2015-03-29 04:56:39 +00:00
|
|
|
mediaSource.OpenToken = prefix + mediaSource.OpenToken;
|
2015-03-28 20:22:27 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (!string.IsNullOrEmpty(mediaSource.LiveStreamId) && !mediaSource.LiveStreamId.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
|
2015-03-28 20:22:27 +00:00
|
|
|
{
|
2015-03-29 04:56:39 +00:00
|
|
|
mediaSource.LiveStreamId = prefix + mediaSource.LiveStreamId;
|
2015-03-28 20:22:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public async Task<MediaSourceInfo> GetMediaSource(BaseItem item, string mediaSourceId, string liveStreamId, bool enablePathSubstitution, CancellationToken cancellationToken)
|
2015-03-31 16:24:16 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (!string.IsNullOrEmpty(liveStreamId))
|
2016-09-18 20:38:38 +00:00
|
|
|
{
|
|
|
|
return await GetLiveStream(liveStreamId, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
2019-12-04 20:39:27 +00:00
|
|
|
var sources = await GetPlaybackMediaSources(item, null, false, enablePathSubstitution, cancellationToken).ConfigureAwait(false);
|
2015-04-09 05:20:23 +00:00
|
|
|
|
|
|
|
return sources.FirstOrDefault(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase));
|
2015-03-31 16:24:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 17:07:53 +00:00
|
|
|
public List<MediaSourceInfo> GetStaticMediaSources(BaseItem item, bool enablePathSubstitution, User user = null)
|
2015-03-07 22:43:53 +00:00
|
|
|
{
|
2022-10-06 18:21:23 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(item);
|
2015-03-07 22:43:53 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var hasMediaSources = (IHasMediaSources)item;
|
2015-03-07 22:43:53 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var sources = hasMediaSources.GetMediaSources(enablePathSubstitution);
|
2015-03-07 22:43:53 +00:00
|
|
|
|
2022-12-05 14:01:13 +00:00
|
|
|
if (user is not null)
|
2015-03-07 22:43:53 +00:00
|
|
|
{
|
2015-04-01 21:56:32 +00:00
|
|
|
foreach (var source in sources)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
SetDefaultAudioAndSubtitleStreamIndexes(item, source, user);
|
2022-03-17 16:13:46 +00:00
|
|
|
|
2023-05-13 18:44:31 +00:00
|
|
|
if (item.MediaType == MediaType.Audio)
|
2022-03-17 16:13:46 +00:00
|
|
|
{
|
|
|
|
source.SupportsTranscoding = user.HasPermission(PermissionKind.EnableAudioPlaybackTranscoding);
|
|
|
|
}
|
2023-05-13 18:44:31 +00:00
|
|
|
else if (item.MediaType == MediaType.Video)
|
2022-03-17 16:13:46 +00:00
|
|
|
{
|
|
|
|
source.SupportsTranscoding = user.HasPermission(PermissionKind.EnableVideoPlaybackTranscoding);
|
|
|
|
source.SupportsDirectStream = user.HasPermission(PermissionKind.EnablePlaybackRemuxing);
|
|
|
|
}
|
2015-04-01 21:56:32 +00:00
|
|
|
}
|
2015-03-07 22:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sources;
|
|
|
|
}
|
|
|
|
|
2022-01-22 14:40:05 +00:00
|
|
|
private IReadOnlyList<string> NormalizeLanguage(string language)
|
2015-03-07 22:43:53 +00:00
|
|
|
{
|
2021-05-23 22:30:41 +00:00
|
|
|
if (string.IsNullOrEmpty(language))
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2019-01-27 09:20:05 +00:00
|
|
|
return Array.Empty<string>();
|
|
|
|
}
|
2016-02-20 23:06:57 +00:00
|
|
|
|
2019-01-27 09:20:05 +00:00
|
|
|
var culture = _localizationManager.FindLanguageInfo(language);
|
2022-12-05 14:01:13 +00:00
|
|
|
if (culture is not null)
|
2019-01-27 09:20:05 +00:00
|
|
|
{
|
|
|
|
return culture.ThreeLetterISOLanguageNames;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
2016-04-27 18:44:08 +00:00
|
|
|
|
2019-01-27 09:20:05 +00:00
|
|
|
return new string[] { language };
|
2016-02-20 23:06:57 +00:00
|
|
|
}
|
2015-03-07 22:43:53 +00:00
|
|
|
|
2020-05-20 17:07:53 +00:00
|
|
|
private void SetDefaultSubtitleStreamIndex(MediaSourceInfo source, UserItemData userData, User user, bool allowRememberingSelection)
|
2016-02-20 23:06:57 +00:00
|
|
|
{
|
2020-05-13 02:10:35 +00:00
|
|
|
if (userData.SubtitleStreamIndex.HasValue
|
|
|
|
&& user.RememberSubtitleSelections
|
|
|
|
&& user.SubtitleMode != SubtitlePlaybackMode.None && allowRememberingSelection)
|
2016-02-20 23:06:57 +00:00
|
|
|
{
|
|
|
|
var index = userData.SubtitleStreamIndex.Value;
|
|
|
|
// Make sure the saved index is still valid
|
|
|
|
if (index == -1 || source.MediaStreams.Any(i => i.Type == MediaStreamType.Subtitle && i.Index == index))
|
|
|
|
{
|
|
|
|
source.DefaultSubtitleStreamIndex = index;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2021-05-23 22:30:41 +00:00
|
|
|
var preferredSubs = NormalizeLanguage(user.SubtitleLanguagePreference);
|
2015-03-07 22:43:53 +00:00
|
|
|
|
|
|
|
var defaultAudioIndex = source.DefaultAudioStreamIndex;
|
2022-12-05 14:00:20 +00:00
|
|
|
var audioLangage = defaultAudioIndex is null
|
2015-03-07 22:43:53 +00:00
|
|
|
? null
|
|
|
|
: source.MediaStreams.Where(i => i.Type == MediaStreamType.Audio && i.Index == defaultAudioIndex).Select(i => i.Language).FirstOrDefault();
|
|
|
|
|
2020-05-13 02:10:35 +00:00
|
|
|
source.DefaultSubtitleStreamIndex = MediaStreamSelector.GetDefaultSubtitleStreamIndex(
|
|
|
|
source.MediaStreams,
|
2015-03-07 22:43:53 +00:00
|
|
|
preferredSubs,
|
2020-05-13 02:10:35 +00:00
|
|
|
user.SubtitleMode,
|
2015-03-07 22:43:53 +00:00
|
|
|
audioLangage);
|
2015-03-31 16:24:16 +00:00
|
|
|
|
2020-05-13 02:10:35 +00:00
|
|
|
MediaStreamSelector.SetSubtitleStreamScores(source.MediaStreams, preferredSubs, user.SubtitleMode, audioLangage);
|
2015-03-07 22:43:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 17:07:53 +00:00
|
|
|
private void SetDefaultAudioStreamIndex(MediaSourceInfo source, UserItemData userData, User user, bool allowRememberingSelection)
|
2016-02-20 23:06:57 +00:00
|
|
|
{
|
2020-05-13 02:10:35 +00:00
|
|
|
if (userData.AudioStreamIndex.HasValue && user.RememberAudioSelections && allowRememberingSelection)
|
2016-02-20 23:06:57 +00:00
|
|
|
{
|
|
|
|
var index = userData.AudioStreamIndex.Value;
|
|
|
|
// Make sure the saved index is still valid
|
|
|
|
if (source.MediaStreams.Any(i => i.Type == MediaStreamType.Audio && i.Index == index))
|
|
|
|
{
|
|
|
|
source.DefaultAudioStreamIndex = index;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-23 22:30:41 +00:00
|
|
|
var preferredAudio = NormalizeLanguage(user.AudioLanguagePreference);
|
2016-02-20 23:06:57 +00:00
|
|
|
|
2020-05-13 02:10:35 +00:00
|
|
|
source.DefaultAudioStreamIndex = MediaStreamSelector.GetDefaultAudioStreamIndex(source.MediaStreams, preferredAudio, user.PlayDefaultAudioTrack);
|
2016-02-20 23:06:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 17:07:53 +00:00
|
|
|
public void SetDefaultAudioAndSubtitleStreamIndexes(BaseItem item, MediaSourceInfo source, User user)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
// Item would only be null if the app didn't supply ItemId as part of the live stream open request
|
2023-05-13 18:44:31 +00:00
|
|
|
var mediaType = item?.MediaType ?? MediaType.Video;
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2023-05-13 18:44:31 +00:00
|
|
|
if (mediaType == MediaType.Video)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2022-12-05 14:00:20 +00:00
|
|
|
var userData = item is null ? new UserItemData() : _userDataManager.GetUserData(user, item);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2022-12-05 14:00:20 +00:00
|
|
|
var allowRememberingSelection = item is null || item.EnableRememberingTrackSelections;
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
SetDefaultAudioStreamIndex(source, userData, user, allowRememberingSelection);
|
|
|
|
SetDefaultSubtitleStreamIndex(source, userData, user, allowRememberingSelection);
|
|
|
|
}
|
2023-05-13 18:44:31 +00:00
|
|
|
else if (mediaType == MediaType.Audio)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
var audio = source.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
|
|
|
|
|
2022-12-05 14:01:13 +00:00
|
|
|
if (audio is not null)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
source.DefaultAudioStreamIndex = audio.Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 08:49:52 +00:00
|
|
|
private static List<MediaSourceInfo> SortMediaSources(IEnumerable<MediaSourceInfo> sources)
|
2015-03-07 22:43:53 +00:00
|
|
|
{
|
|
|
|
return sources.OrderBy(i =>
|
|
|
|
{
|
|
|
|
if (i.VideoType.HasValue && i.VideoType.Value == VideoType.VideoFile)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}).ThenBy(i => i.Video3DFormat.HasValue ? 1 : 0)
|
|
|
|
.ThenByDescending(i =>
|
|
|
|
{
|
|
|
|
var stream = i.VideoStream;
|
|
|
|
|
2021-04-19 08:49:52 +00:00
|
|
|
return stream?.Width ?? 0;
|
2015-03-07 22:43:53 +00:00
|
|
|
})
|
2021-04-19 08:49:52 +00:00
|
|
|
.Where(i => i.Type != MediaSourceType.Placeholder)
|
2015-03-07 22:43:53 +00:00
|
|
|
.ToList();
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public async Task<Tuple<LiveStreamResponse, IDirectStreamProvider>> OpenLiveStreamInternal(LiveStreamRequest request, CancellationToken cancellationToken)
|
2015-03-28 20:22:27 +00:00
|
|
|
{
|
|
|
|
await _liveStreamSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
MediaSourceInfo mediaSource;
|
|
|
|
ILiveStream liveStream;
|
|
|
|
|
2015-03-28 20:22:27 +00:00
|
|
|
try
|
|
|
|
{
|
2021-12-24 21:18:24 +00:00
|
|
|
var (provider, keyId) = GetProvider(request.OpenToken);
|
2015-03-28 20:22:27 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var currentLiveStreams = _openStreams.Values.ToList();
|
|
|
|
|
2021-12-24 21:18:24 +00:00
|
|
|
liveStream = await provider.OpenMediaSource(keyId, currentLiveStreams, cancellationToken).ConfigureAwait(false);
|
2016-10-05 07:15:29 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
mediaSource = liveStream.MediaSource;
|
2015-03-28 20:22:27 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
// Validate that this is actually possible
|
|
|
|
if (mediaSource.SupportsDirectStream)
|
2015-03-29 22:38:32 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
mediaSource.SupportsDirectStream = SupportsDirectStream(mediaSource.Path, mediaSource.Protocol);
|
2015-03-29 22:38:32 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 20:22:27 +00:00
|
|
|
SetKeyProperties(provider, mediaSource);
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
_openStreams[mediaSource.LiveStreamId] = liveStream;
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
_liveStreamSemaphore.Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (mediaSource.MediaStreams.Any(i => i.Index != -1) || !mediaSource.SupportsProbing)
|
2015-03-29 04:56:39 +00:00
|
|
|
{
|
2021-11-07 21:32:08 +00:00
|
|
|
AddMediaInfo(mediaSource);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// hack - these two values were taken from LiveTVMediaSourceProvider
|
2019-01-21 19:18:52 +00:00
|
|
|
string cacheKey = request.OpenToken;
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2020-12-23 12:12:40 +00:00
|
|
|
await new LiveStreamHelper(_mediaEncoder, _logger, _appPaths)
|
2021-11-07 21:32:08 +00:00
|
|
|
.AddMediaInfoWithProbe(mediaSource, false, cacheKey, true, cancellationToken)
|
2019-01-21 19:18:52 +00:00
|
|
|
.ConfigureAwait(false);
|
2015-03-29 04:56:39 +00:00
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
_logger.LogError(ex, "Error probing live tv stream");
|
2021-11-07 21:32:08 +00:00
|
|
|
AddMediaInfo(mediaSource);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 19:18:52 +00:00
|
|
|
// TODO: @bond Fix
|
2021-01-12 14:37:18 +00:00
|
|
|
var json = JsonSerializer.SerializeToUtf8Bytes(mediaSource, _jsonOptions);
|
2021-09-07 12:18:04 +00:00
|
|
|
_logger.LogInformation("Live stream opened: {@MediaSource}", mediaSource);
|
2020-12-24 10:15:12 +00:00
|
|
|
var clone = JsonSerializer.Deserialize<MediaSourceInfo>(json, _jsonOptions);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2022-02-21 13:15:09 +00:00
|
|
|
if (!request.UserId.Equals(default))
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
var user = _userManager.GetUserById(request.UserId);
|
2022-02-21 13:15:09 +00:00
|
|
|
var item = request.ItemId.Equals(default)
|
2018-09-12 17:26:21 +00:00
|
|
|
? null
|
|
|
|
: _libraryManager.GetItemById(request.ItemId);
|
|
|
|
SetDefaultAudioAndSubtitleStreamIndexes(item, clone, user);
|
|
|
|
}
|
|
|
|
|
2020-04-05 16:10:56 +00:00
|
|
|
return new Tuple<LiveStreamResponse, IDirectStreamProvider>(new LiveStreamResponse(clone), liveStream as IDirectStreamProvider);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 21:32:08 +00:00
|
|
|
private static void AddMediaInfo(MediaSourceInfo mediaSource)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
mediaSource.DefaultSubtitleStreamIndex = null;
|
|
|
|
|
|
|
|
// Null this out so that it will be treated like a live stream
|
|
|
|
if (mediaSource.IsInfiniteStream)
|
|
|
|
{
|
|
|
|
mediaSource.RunTimeTicks = null;
|
|
|
|
}
|
2015-03-29 04:56:39 +00:00
|
|
|
|
2020-05-20 17:07:53 +00:00
|
|
|
var audioStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2022-12-05 14:00:20 +00:00
|
|
|
if (audioStream is null || audioStream.Index == -1)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
mediaSource.DefaultAudioStreamIndex = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mediaSource.DefaultAudioStreamIndex = audioStream.Index;
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:07:53 +00:00
|
|
|
var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
|
2022-12-05 14:01:13 +00:00
|
|
|
if (videoStream is not null)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
if (!videoStream.BitRate.HasValue)
|
2015-03-29 16:45:16 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var width = videoStream.Width ?? 1920;
|
|
|
|
|
|
|
|
if (width >= 3000)
|
|
|
|
{
|
|
|
|
videoStream.BitRate = 30000000;
|
|
|
|
}
|
|
|
|
else if (width >= 1900)
|
|
|
|
{
|
|
|
|
videoStream.BitRate = 20000000;
|
|
|
|
}
|
|
|
|
else if (width >= 1200)
|
|
|
|
{
|
|
|
|
videoStream.BitRate = 8000000;
|
|
|
|
}
|
|
|
|
else if (width >= 700)
|
|
|
|
{
|
|
|
|
videoStream.BitRate = 2000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to estimate this
|
|
|
|
mediaSource.InferTotalBitrate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<LiveStreamResponse> OpenLiveStream(LiveStreamRequest request, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var result = await OpenLiveStreamInternal(request, cancellationToken).ConfigureAwait(false);
|
|
|
|
return result.Item1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<MediaSourceInfo> GetLiveStreamMediaInfo(string id, CancellationToken cancellationToken)
|
|
|
|
{
|
2021-09-10 09:54:26 +00:00
|
|
|
// TODO probably shouldn't throw here but it is kept for "backwards compatibility"
|
|
|
|
var liveStreamInfo = GetLiveStreamInfo(id) ?? throw new ResourceNotFoundException();
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
var mediaSource = liveStreamInfo.MediaSource;
|
|
|
|
|
|
|
|
if (liveStreamInfo is IDirectStreamProvider)
|
|
|
|
{
|
2020-07-24 14:37:54 +00:00
|
|
|
var info = await _mediaEncoder.GetMediaInfo(
|
|
|
|
new MediaInfoRequest
|
|
|
|
{
|
|
|
|
MediaSource = mediaSource,
|
|
|
|
ExtractChapters = false,
|
|
|
|
MediaType = DlnaProfileType.Video
|
|
|
|
},
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
mediaSource.MediaStreams = info.MediaStreams;
|
|
|
|
mediaSource.Container = info.Container;
|
|
|
|
mediaSource.Bitrate = info.Bitrate;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mediaSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudio, string cacheKey, bool addProbeDelay, bool isLiveStream, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var originalRuntime = mediaSource.RunTimeTicks;
|
|
|
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
|
|
|
MediaInfo mediaInfo = null;
|
2019-02-28 22:22:57 +00:00
|
|
|
var cacheFilePath = string.IsNullOrEmpty(cacheKey) ? null : Path.Combine(_appPaths.CachePath, "mediainfo", cacheKey.GetMD5().ToString("N", CultureInfo.InvariantCulture) + ".json");
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(cacheKey))
|
|
|
|
{
|
2023-10-11 16:32:57 +00:00
|
|
|
FileStream jsonStream = AsyncFile.OpenRead(cacheFilePath);
|
2018-09-12 17:26:21 +00:00
|
|
|
try
|
|
|
|
{
|
2020-12-24 10:15:12 +00:00
|
|
|
mediaInfo = await JsonSerializer.DeserializeAsync<MediaInfo>(jsonStream, _jsonOptions, cancellationToken).ConfigureAwait(false);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
_logger.LogDebug(ex, "_jsonSerializer.DeserializeFromFile threw an exception.");
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
2023-10-11 16:32:57 +00:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
await jsonStream.DisposeAsync().ConfigureAwait(false);
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 14:00:20 +00:00
|
|
|
if (mediaInfo is null)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
if (addProbeDelay)
|
|
|
|
{
|
|
|
|
var delayMs = mediaSource.AnalyzeDurationMs ?? 0;
|
|
|
|
delayMs = Math.Max(3000, delayMs);
|
|
|
|
await Task.Delay(delayMs, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLiveStream)
|
|
|
|
{
|
|
|
|
mediaSource.AnalyzeDurationMs = 3000;
|
|
|
|
}
|
|
|
|
|
2020-05-13 02:10:35 +00:00
|
|
|
mediaInfo = await _mediaEncoder.GetMediaInfo(
|
|
|
|
new MediaInfoRequest
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
MediaSource = mediaSource,
|
|
|
|
MediaType = isAudio ? DlnaProfileType.Audio : DlnaProfileType.Video,
|
|
|
|
ExtractChapters = false
|
2020-05-13 02:10:35 +00:00
|
|
|
},
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2022-12-05 14:01:13 +00:00
|
|
|
if (cacheFilePath is not null)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(cacheFilePath));
|
2023-10-11 16:32:57 +00:00
|
|
|
FileStream createStream = File.Create(cacheFilePath);
|
|
|
|
await using (createStream.ConfigureAwait(false))
|
|
|
|
{
|
|
|
|
await JsonSerializer.SerializeAsync(createStream, mediaInfo, _jsonOptions, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2020-06-14 09:11:11 +00:00
|
|
|
// _logger.LogDebug("Saved media info to {0}", cacheFilePath);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var mediaStreams = mediaInfo.MediaStreams;
|
|
|
|
|
|
|
|
if (isLiveStream && !string.IsNullOrEmpty(cacheKey))
|
|
|
|
{
|
|
|
|
var newList = new List<MediaStream>();
|
|
|
|
newList.AddRange(mediaStreams.Where(i => i.Type == MediaStreamType.Video).Take(1));
|
|
|
|
newList.AddRange(mediaStreams.Where(i => i.Type == MediaStreamType.Audio).Take(1));
|
|
|
|
|
|
|
|
foreach (var stream in newList)
|
|
|
|
{
|
|
|
|
stream.Index = -1;
|
|
|
|
stream.Language = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
mediaStreams = newList;
|
|
|
|
}
|
|
|
|
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger.LogInformation("Live tv media info probe took {0} seconds", (DateTime.UtcNow - now).TotalSeconds.ToString(CultureInfo.InvariantCulture));
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
mediaSource.Bitrate = mediaInfo.Bitrate;
|
|
|
|
mediaSource.Container = mediaInfo.Container;
|
|
|
|
mediaSource.Formats = mediaInfo.Formats;
|
|
|
|
mediaSource.MediaStreams = mediaStreams;
|
|
|
|
mediaSource.RunTimeTicks = mediaInfo.RunTimeTicks;
|
|
|
|
mediaSource.Size = mediaInfo.Size;
|
|
|
|
mediaSource.Timestamp = mediaInfo.Timestamp;
|
|
|
|
mediaSource.Video3DFormat = mediaInfo.Video3DFormat;
|
|
|
|
mediaSource.VideoType = mediaInfo.VideoType;
|
|
|
|
|
|
|
|
mediaSource.DefaultSubtitleStreamIndex = null;
|
|
|
|
|
|
|
|
if (isLiveStream)
|
|
|
|
{
|
|
|
|
// Null this out so that it will be treated like a live stream
|
|
|
|
if (!originalRuntime.HasValue)
|
|
|
|
{
|
|
|
|
mediaSource.RunTimeTicks = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var audioStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
|
|
|
|
|
2022-12-05 14:00:20 +00:00
|
|
|
if (audioStream is null || audioStream.Index == -1)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
mediaSource.DefaultAudioStreamIndex = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mediaSource.DefaultAudioStreamIndex = audioStream.Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
|
2022-12-05 14:01:13 +00:00
|
|
|
if (videoStream is not null)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
if (!videoStream.BitRate.HasValue)
|
|
|
|
{
|
|
|
|
var width = videoStream.Width ?? 1920;
|
|
|
|
|
|
|
|
if (width >= 3000)
|
|
|
|
{
|
|
|
|
videoStream.BitRate = 30000000;
|
|
|
|
}
|
|
|
|
else if (width >= 1900)
|
|
|
|
{
|
|
|
|
videoStream.BitRate = 20000000;
|
|
|
|
}
|
|
|
|
else if (width >= 1200)
|
|
|
|
{
|
|
|
|
videoStream.BitRate = 8000000;
|
|
|
|
}
|
|
|
|
else if (width >= 700)
|
|
|
|
{
|
|
|
|
videoStream.BitRate = 2000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is coming up false and preventing stream copy
|
|
|
|
videoStream.IsAVC = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLiveStream)
|
|
|
|
{
|
|
|
|
mediaSource.AnalyzeDurationMs = 3000;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to estimate this
|
|
|
|
mediaSource.InferTotalBitrate(true);
|
|
|
|
}
|
|
|
|
|
2021-09-10 07:29:14 +00:00
|
|
|
public Task<Tuple<MediaSourceInfo, IDirectStreamProvider>> GetLiveStreamWithDirectStreamProvider(string id, CancellationToken cancellationToken)
|
2015-03-29 04:56:39 +00:00
|
|
|
{
|
2022-10-13 17:08:00 +00:00
|
|
|
ArgumentException.ThrowIfNullOrEmpty(id);
|
2015-03-29 22:38:32 +00:00
|
|
|
|
2021-09-10 09:54:26 +00:00
|
|
|
// TODO probably shouldn't throw here but it is kept for "backwards compatibility"
|
|
|
|
var info = GetLiveStreamInfo(id) ?? throw new ResourceNotFoundException();
|
2021-09-10 07:29:14 +00:00
|
|
|
return Task.FromResult(new Tuple<MediaSourceInfo, IDirectStreamProvider>(info.MediaSource, info as IDirectStreamProvider));
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 07:29:14 +00:00
|
|
|
public ILiveStream GetLiveStreamInfo(string id)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2022-10-13 17:08:00 +00:00
|
|
|
ArgumentException.ThrowIfNullOrEmpty(id);
|
2015-03-29 22:38:32 +00:00
|
|
|
|
2020-09-23 21:12:26 +00:00
|
|
|
if (_openStreams.TryGetValue(id, out ILiveStream info))
|
2015-03-29 04:56:39 +00:00
|
|
|
{
|
2021-09-10 07:29:14 +00:00
|
|
|
return info;
|
2015-03-29 04:56:39 +00:00
|
|
|
}
|
2021-09-10 07:29:14 +00:00
|
|
|
|
2021-09-10 09:54:26 +00:00
|
|
|
return null;
|
2015-03-29 04:56:39 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 10:53:45 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
public ILiveStream GetLiveStreamInfoByUniqueId(string uniqueId)
|
|
|
|
{
|
|
|
|
return _openStreams.Values.FirstOrDefault(stream => string.Equals(uniqueId, stream?.UniqueId, StringComparison.OrdinalIgnoreCase));
|
|
|
|
}
|
|
|
|
|
2016-10-05 07:15:29 +00:00
|
|
|
public async Task<MediaSourceInfo> GetLiveStream(string id, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var result = await GetLiveStreamWithDirectStreamProvider(id, cancellationToken).ConfigureAwait(false);
|
|
|
|
return result.Item1;
|
|
|
|
}
|
|
|
|
|
2023-12-28 19:34:44 +00:00
|
|
|
public async Task<List<MediaSourceInfo>> GetRecordingStreamMediaSources(ActiveRecordingInfo info, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var stream = new MediaSourceInfo
|
|
|
|
{
|
|
|
|
EncoderPath = _appHost.GetApiUrlForLocalAccess() + "/LiveTv/LiveRecordings/" + info.Id + "/stream",
|
|
|
|
EncoderProtocol = MediaProtocol.Http,
|
|
|
|
Path = info.Path,
|
|
|
|
Protocol = MediaProtocol.File,
|
|
|
|
Id = info.Id,
|
|
|
|
SupportsDirectPlay = false,
|
|
|
|
SupportsDirectStream = true,
|
|
|
|
SupportsTranscoding = true,
|
|
|
|
IsInfiniteStream = true,
|
|
|
|
RequiresOpening = false,
|
|
|
|
RequiresClosing = false,
|
|
|
|
BufferMs = 0,
|
|
|
|
IgnoreDts = true,
|
|
|
|
IgnoreIndex = true
|
|
|
|
};
|
|
|
|
|
|
|
|
await new LiveStreamHelper(_mediaEncoder, _logger, _appPaths)
|
|
|
|
.AddMediaInfoWithProbe(stream, false, false, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
return new List<MediaSourceInfo>
|
|
|
|
{
|
|
|
|
stream
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-29 12:55:49 +00:00
|
|
|
public async Task CloseLiveStream(string id)
|
2015-03-28 20:22:27 +00:00
|
|
|
{
|
2022-10-13 17:08:00 +00:00
|
|
|
ArgumentException.ThrowIfNullOrEmpty(id);
|
2016-07-28 06:29:14 +00:00
|
|
|
|
2016-09-29 12:55:49 +00:00
|
|
|
await _liveStreamSemaphore.WaitAsync().ConfigureAwait(false);
|
2015-03-28 20:22:27 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2019-01-17 17:47:41 +00:00
|
|
|
if (_openStreams.TryGetValue(id, out ILiveStream liveStream))
|
2015-03-31 16:24:16 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
liveStream.ConsumerCount--;
|
2016-09-29 12:55:49 +00:00
|
|
|
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger.LogInformation("Live stream {0} consumer count is now {1}", liveStream.OriginalStreamId, liveStream.ConsumerCount);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
if (liveStream.ConsumerCount <= 0)
|
2015-03-31 16:24:16 +00:00
|
|
|
{
|
2020-09-23 21:12:26 +00:00
|
|
|
_openStreams.TryRemove(id, out _);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger.LogInformation("Closing live stream {0}", id);
|
2015-03-28 20:22:27 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
await liveStream.Close().ConfigureAwait(false);
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger.LogInformation("Live stream {0} closed successfully", id);
|
2015-03-31 16:24:16 +00:00
|
|
|
}
|
2015-03-29 04:56:39 +00:00
|
|
|
}
|
2015-03-28 20:22:27 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
_liveStreamSemaphore.Release();
|
|
|
|
}
|
|
|
|
}
|
2015-07-28 19:54:45 +00:00
|
|
|
|
2021-12-24 21:18:24 +00:00
|
|
|
private (IMediaSourceProvider MediaSourceProvider, string KeyId) GetProvider(string key)
|
2015-03-28 20:22:27 +00:00
|
|
|
{
|
2022-10-13 17:08:00 +00:00
|
|
|
ArgumentException.ThrowIfNullOrEmpty(key);
|
2015-08-24 12:54:10 +00:00
|
|
|
|
2020-11-14 14:47:34 +00:00
|
|
|
var keys = key.Split(LiveStreamIdDelimeter, 2);
|
2015-03-28 20:22:27 +00:00
|
|
|
|
2019-02-28 22:22:57 +00:00
|
|
|
var provider = _providers.FirstOrDefault(i => string.Equals(i.GetType().FullName.GetMD5().ToString("N", CultureInfo.InvariantCulture), keys[0], StringComparison.OrdinalIgnoreCase));
|
2015-03-28 20:22:27 +00:00
|
|
|
|
2020-07-24 14:37:54 +00:00
|
|
|
var splitIndex = key.IndexOf(LiveStreamIdDelimeter, StringComparison.Ordinal);
|
2015-08-24 02:08:20 +00:00
|
|
|
var keyId = key.Substring(splitIndex + 1);
|
|
|
|
|
2020-07-24 14:37:54 +00:00
|
|
|
return (provider, keyId);
|
2015-03-28 20:22:27 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 21:32:08 +00:00
|
|
|
/// <inheritdoc />
|
2015-03-28 20:22:27 +00:00
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
2020-07-20 09:01:37 +00:00
|
|
|
GC.SuppressFinalize(this);
|
2015-03-28 20:22:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
protected virtual void Dispose(bool dispose)
|
|
|
|
{
|
|
|
|
if (dispose)
|
|
|
|
{
|
2020-07-24 14:37:54 +00:00
|
|
|
foreach (var key in _openStreams.Keys.ToList())
|
2015-03-28 20:22:27 +00:00
|
|
|
{
|
2020-07-24 14:37:54 +00:00
|
|
|
CloseLiveStream(key).GetAwaiter().GetResult();
|
2015-03-28 20:22:27 +00:00
|
|
|
}
|
2020-07-24 14:37:54 +00:00
|
|
|
|
|
|
|
_liveStreamSemaphore.Dispose();
|
2015-03-28 20:22:27 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-07 21:03:09 +00:00
|
|
|
}
|
2018-12-13 13:18:25 +00:00
|
|
|
}
|