| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | #pragma warning disable CS1591 |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Concurrent; |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.Collections.Immutable; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using System.IO; |
| | | 11 | | using System.Linq; |
| | | 12 | | using System.Text.Json; |
| | | 13 | | using System.Threading; |
| | | 14 | | using System.Threading.Tasks; |
| | | 15 | | using AsyncKeyedLock; |
| | | 16 | | using Jellyfin.Data; |
| | | 17 | | using Jellyfin.Data.Enums; |
| | | 18 | | using Jellyfin.Database.Implementations.Entities; |
| | | 19 | | using Jellyfin.Database.Implementations.Enums; |
| | | 20 | | using Jellyfin.Extensions; |
| | | 21 | | using Jellyfin.Extensions.Json; |
| | | 22 | | using MediaBrowser.Common.Configuration; |
| | | 23 | | using MediaBrowser.Common.Extensions; |
| | | 24 | | using MediaBrowser.Controller; |
| | | 25 | | using MediaBrowser.Controller.Entities; |
| | | 26 | | using MediaBrowser.Controller.Entities.TV; |
| | | 27 | | using MediaBrowser.Controller.IO; |
| | | 28 | | using MediaBrowser.Controller.Library; |
| | | 29 | | using MediaBrowser.Controller.LiveTv; |
| | | 30 | | using MediaBrowser.Controller.MediaEncoding; |
| | | 31 | | using MediaBrowser.Controller.Persistence; |
| | | 32 | | using MediaBrowser.Controller.Providers; |
| | | 33 | | using MediaBrowser.Model.Dlna; |
| | | 34 | | using MediaBrowser.Model.Dto; |
| | | 35 | | using MediaBrowser.Model.Entities; |
| | | 36 | | using MediaBrowser.Model.Globalization; |
| | | 37 | | using MediaBrowser.Model.IO; |
| | | 38 | | using MediaBrowser.Model.MediaInfo; |
| | | 39 | | using Microsoft.Extensions.Logging; |
| | | 40 | | |
| | | 41 | | namespace Emby.Server.Implementations.Library |
| | | 42 | | { |
| | | 43 | | public class MediaSourceManager : IMediaSourceManager, IDisposable |
| | | 44 | | { |
| | | 45 | | // Do not use a pipe here because Roku http requests to the server will fail, without any explicit error message |
| | | 46 | | private const char LiveStreamIdDelimiter = '_'; |
| | | 47 | | |
| | | 48 | | private readonly IServerApplicationHost _appHost; |
| | | 49 | | private readonly IItemRepository _itemRepo; |
| | | 50 | | private readonly IUserManager _userManager; |
| | | 51 | | private readonly ILibraryManager _libraryManager; |
| | | 52 | | private readonly IFileSystem _fileSystem; |
| | | 53 | | private readonly ILogger<MediaSourceManager> _logger; |
| | | 54 | | private readonly IUserDataManager _userDataManager; |
| | | 55 | | private readonly IMediaEncoder _mediaEncoder; |
| | | 56 | | private readonly ILocalizationManager _localizationManager; |
| | | 57 | | private readonly IApplicationPaths _appPaths; |
| | | 58 | | private readonly IDirectoryService _directoryService; |
| | | 59 | | private readonly IMediaStreamRepository _mediaStreamRepository; |
| | | 60 | | private readonly IMediaAttachmentRepository _mediaAttachmentRepository; |
| | 49 | 61 | | private readonly ConcurrentDictionary<string, ILiveStream> _openStreams = new ConcurrentDictionary<string, ILive |
| | 49 | 62 | | private readonly AsyncNonKeyedLocker _liveStreamLocker = new(1); |
| | 49 | 63 | | private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; |
| | | 64 | | |
| | | 65 | | private IMediaSourceProvider[] _providers; |
| | | 66 | | |
| | | 67 | | public MediaSourceManager( |
| | | 68 | | IServerApplicationHost appHost, |
| | | 69 | | IItemRepository itemRepo, |
| | | 70 | | IApplicationPaths applicationPaths, |
| | | 71 | | ILocalizationManager localizationManager, |
| | | 72 | | IUserManager userManager, |
| | | 73 | | ILibraryManager libraryManager, |
| | | 74 | | ILogger<MediaSourceManager> logger, |
| | | 75 | | IFileSystem fileSystem, |
| | | 76 | | IUserDataManager userDataManager, |
| | | 77 | | IMediaEncoder mediaEncoder, |
| | | 78 | | IDirectoryService directoryService, |
| | | 79 | | IMediaStreamRepository mediaStreamRepository, |
| | | 80 | | IMediaAttachmentRepository mediaAttachmentRepository) |
| | | 81 | | { |
| | 49 | 82 | | _appHost = appHost; |
| | 49 | 83 | | _itemRepo = itemRepo; |
| | 49 | 84 | | _userManager = userManager; |
| | 49 | 85 | | _libraryManager = libraryManager; |
| | 49 | 86 | | _logger = logger; |
| | 49 | 87 | | _fileSystem = fileSystem; |
| | 49 | 88 | | _userDataManager = userDataManager; |
| | 49 | 89 | | _mediaEncoder = mediaEncoder; |
| | 49 | 90 | | _localizationManager = localizationManager; |
| | 49 | 91 | | _appPaths = applicationPaths; |
| | 49 | 92 | | _directoryService = directoryService; |
| | 49 | 93 | | _mediaStreamRepository = mediaStreamRepository; |
| | 49 | 94 | | _mediaAttachmentRepository = mediaAttachmentRepository; |
| | 49 | 95 | | } |
| | | 96 | | |
| | | 97 | | public void AddParts(IEnumerable<IMediaSourceProvider> providers) |
| | | 98 | | { |
| | 21 | 99 | | _providers = providers.ToArray(); |
| | 21 | 100 | | } |
| | | 101 | | |
| | | 102 | | public IReadOnlyList<MediaStream> GetMediaStreams(MediaStreamQuery query) |
| | | 103 | | { |
| | 0 | 104 | | var list = _mediaStreamRepository.GetMediaStreams(query); |
| | | 105 | | |
| | 0 | 106 | | foreach (var stream in list) |
| | | 107 | | { |
| | 0 | 108 | | stream.SupportsExternalStream = StreamSupportsExternalStream(stream); |
| | | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | return list; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | private static bool StreamSupportsExternalStream(MediaStream stream) |
| | | 115 | | { |
| | 0 | 116 | | if (stream.IsExternal) |
| | | 117 | | { |
| | 0 | 118 | | return true; |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | if (stream.IsTextSubtitleStream) |
| | | 122 | | { |
| | 0 | 123 | | return true; |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | if (stream.IsPgsSubtitleStream) |
| | | 127 | | { |
| | 0 | 128 | | return true; |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | if (stream.IsVobSubSubtitleStream) |
| | | 132 | | { |
| | 0 | 133 | | return true; |
| | | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | return false; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | public IReadOnlyList<MediaStream> GetMediaStreams(Guid itemId) |
| | | 140 | | { |
| | 0 | 141 | | var list = GetMediaStreams(new MediaStreamQuery |
| | 0 | 142 | | { |
| | 0 | 143 | | ItemId = itemId |
| | 0 | 144 | | }); |
| | | 145 | | |
| | 0 | 146 | | return GetMediaStreamsForItem(list); |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | private IReadOnlyList<MediaStream> GetMediaStreamsForItem(IReadOnlyList<MediaStream> streams) |
| | | 150 | | { |
| | 0 | 151 | | foreach (var stream in streams) |
| | | 152 | | { |
| | 0 | 153 | | if (stream.Type == MediaStreamType.Subtitle) |
| | | 154 | | { |
| | 0 | 155 | | stream.SupportsExternalStream = StreamSupportsExternalStream(stream); |
| | | 156 | | } |
| | | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | return streams; |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | /// <inheritdoc /> |
| | | 163 | | public IReadOnlyList<MediaAttachment> GetMediaAttachments(MediaAttachmentQuery query) |
| | | 164 | | { |
| | 0 | 165 | | return _mediaAttachmentRepository.GetMediaAttachments(query); |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <inheritdoc /> |
| | | 169 | | public IReadOnlyList<MediaAttachment> GetMediaAttachments(Guid itemId) |
| | | 170 | | { |
| | 0 | 171 | | return GetMediaAttachments(new MediaAttachmentQuery |
| | 0 | 172 | | { |
| | 0 | 173 | | ItemId = itemId |
| | 0 | 174 | | }); |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | public async Task<IReadOnlyList<MediaSourceInfo>> GetPlaybackMediaSources(BaseItem item, User user, bool allowMe |
| | | 178 | | { |
| | 0 | 179 | | var mediaSources = GetStaticMediaSources(item, enablePathSubstitution, user); |
| | 0 | 180 | | ResolveSymlinkPaths(mediaSources, enablePathSubstitution); |
| | | 181 | | |
| | | 182 | | // If file is strm or main media stream is missing, force a metadata refresh with remote probing |
| | 0 | 183 | | if (allowMediaProbe && mediaSources[0].Type != MediaSourceType.Placeholder |
| | 0 | 184 | | && (item.Path.EndsWith(".strm", StringComparison.OrdinalIgnoreCase) |
| | 0 | 185 | | || (item.MediaType == MediaType.Video && mediaSources[0].MediaStreams.All(i => i.Type != MediaStream |
| | 0 | 186 | | || (item.MediaType == MediaType.Audio && mediaSources[0].MediaStreams.All(i => i.Type != MediaStream |
| | | 187 | | { |
| | 0 | 188 | | await item.RefreshMetadata( |
| | 0 | 189 | | new MetadataRefreshOptions(_directoryService) |
| | 0 | 190 | | { |
| | 0 | 191 | | EnableRemoteContentProbe = true, |
| | 0 | 192 | | MetadataRefreshMode = MetadataRefreshMode.FullRefresh |
| | 0 | 193 | | }, |
| | 0 | 194 | | cancellationToken).ConfigureAwait(false); |
| | | 195 | | |
| | 0 | 196 | | mediaSources = GetStaticMediaSources(item, enablePathSubstitution, user); |
| | 0 | 197 | | ResolveSymlinkPaths(mediaSources, enablePathSubstitution); |
| | | 198 | | } |
| | | 199 | | |
| | 0 | 200 | | var dynamicMediaSources = await GetDynamicMediaSources(item, cancellationToken).ConfigureAwait(false); |
| | | 201 | | |
| | 0 | 202 | | var list = new List<MediaSourceInfo>(); |
| | | 203 | | |
| | 0 | 204 | | list.AddRange(mediaSources); |
| | | 205 | | |
| | 0 | 206 | | foreach (var source in dynamicMediaSources) |
| | | 207 | | { |
| | | 208 | | // Validate that this is actually possible |
| | 0 | 209 | | if (source.SupportsDirectStream) |
| | | 210 | | { |
| | 0 | 211 | | source.SupportsDirectStream = SupportsDirectStream(source.Path, source.Protocol); |
| | | 212 | | } |
| | | 213 | | |
| | 0 | 214 | | if (user is not null) |
| | | 215 | | { |
| | 0 | 216 | | SetDefaultAudioAndSubtitleStreamIndices(item, source, user); |
| | | 217 | | |
| | 0 | 218 | | if (item.MediaType == MediaType.Audio) |
| | | 219 | | { |
| | 0 | 220 | | source.SupportsTranscoding = user.HasPermission(PermissionKind.EnableAudioPlaybackTranscoding); |
| | | 221 | | } |
| | 0 | 222 | | else if (item.MediaType == MediaType.Video) |
| | | 223 | | { |
| | 0 | 224 | | source.SupportsTranscoding = user.HasPermission(PermissionKind.EnableVideoPlaybackTranscoding); |
| | 0 | 225 | | source.SupportsDirectStream = user.HasPermission(PermissionKind.EnablePlaybackRemuxing); |
| | | 226 | | } |
| | | 227 | | } |
| | | 228 | | |
| | 0 | 229 | | list.Add(source); |
| | | 230 | | } |
| | | 231 | | |
| | 0 | 232 | | return SortMediaSources(list, item.Id).ToArray(); |
| | 0 | 233 | | } |
| | | 234 | | |
| | | 235 | | /// <inheritdoc />> |
| | | 236 | | public MediaProtocol GetPathProtocol(string path) |
| | | 237 | | { |
| | 2248 | 238 | | if (string.IsNullOrEmpty(path)) |
| | | 239 | | { |
| | 0 | 240 | | return MediaProtocol.File; |
| | | 241 | | } |
| | | 242 | | |
| | 2248 | 243 | | if (path.StartsWith("Rtsp", StringComparison.OrdinalIgnoreCase)) |
| | | 244 | | { |
| | 1 | 245 | | return MediaProtocol.Rtsp; |
| | | 246 | | } |
| | | 247 | | |
| | 2247 | 248 | | if (path.StartsWith("Rtmp", StringComparison.OrdinalIgnoreCase)) |
| | | 249 | | { |
| | 0 | 250 | | return MediaProtocol.Rtmp; |
| | | 251 | | } |
| | | 252 | | |
| | 2247 | 253 | | if (path.StartsWith("Http", StringComparison.OrdinalIgnoreCase)) |
| | | 254 | | { |
| | 2 | 255 | | return MediaProtocol.Http; |
| | | 256 | | } |
| | | 257 | | |
| | 2245 | 258 | | if (path.StartsWith("rtp", StringComparison.OrdinalIgnoreCase)) |
| | | 259 | | { |
| | 0 | 260 | | return MediaProtocol.Rtp; |
| | | 261 | | } |
| | | 262 | | |
| | 2245 | 263 | | if (path.StartsWith("ftp", StringComparison.OrdinalIgnoreCase)) |
| | | 264 | | { |
| | 0 | 265 | | return MediaProtocol.Ftp; |
| | | 266 | | } |
| | | 267 | | |
| | 2245 | 268 | | if (path.StartsWith("udp", StringComparison.OrdinalIgnoreCase)) |
| | | 269 | | { |
| | 0 | 270 | | return MediaProtocol.Udp; |
| | | 271 | | } |
| | | 272 | | |
| | 2245 | 273 | | return _fileSystem.IsPathFile(path) ? MediaProtocol.File : MediaProtocol.Http; |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | public bool SupportsDirectStream(string path, MediaProtocol protocol) |
| | | 277 | | { |
| | 0 | 278 | | if (protocol == MediaProtocol.File) |
| | | 279 | | { |
| | 0 | 280 | | return true; |
| | | 281 | | } |
| | | 282 | | |
| | 0 | 283 | | if (protocol == MediaProtocol.Http) |
| | | 284 | | { |
| | 0 | 285 | | if (path is not null) |
| | | 286 | | { |
| | 0 | 287 | | if (path.Contains(".m3u", StringComparison.OrdinalIgnoreCase)) |
| | | 288 | | { |
| | 0 | 289 | | return false; |
| | | 290 | | } |
| | | 291 | | |
| | 0 | 292 | | return true; |
| | | 293 | | } |
| | | 294 | | } |
| | | 295 | | |
| | 0 | 296 | | return false; |
| | | 297 | | } |
| | | 298 | | |
| | | 299 | | private async Task<IEnumerable<MediaSourceInfo>> GetDynamicMediaSources(BaseItem item, CancellationToken cancell |
| | | 300 | | { |
| | 0 | 301 | | var tasks = _providers.Select(i => GetDynamicMediaSources(item, i, cancellationToken)); |
| | 0 | 302 | | var results = await Task.WhenAll(tasks).ConfigureAwait(false); |
| | | 303 | | |
| | 0 | 304 | | return results.SelectMany(i => i); |
| | 0 | 305 | | } |
| | | 306 | | |
| | | 307 | | private async Task<IEnumerable<MediaSourceInfo>> GetDynamicMediaSources(BaseItem item, IMediaSourceProvider prov |
| | | 308 | | { |
| | | 309 | | try |
| | | 310 | | { |
| | 0 | 311 | | var sources = await provider.GetMediaSources(item, cancellationToken).ConfigureAwait(false); |
| | 0 | 312 | | var list = sources.ToList(); |
| | | 313 | | |
| | 0 | 314 | | foreach (var mediaSource in list) |
| | | 315 | | { |
| | 0 | 316 | | mediaSource.InferTotalBitrate(); |
| | | 317 | | |
| | 0 | 318 | | SetKeyProperties(provider, mediaSource); |
| | | 319 | | } |
| | | 320 | | |
| | 0 | 321 | | return list; |
| | | 322 | | } |
| | 0 | 323 | | catch (Exception ex) |
| | | 324 | | { |
| | 0 | 325 | | _logger.LogError(ex, "Error getting media sources"); |
| | 0 | 326 | | return []; |
| | | 327 | | } |
| | 0 | 328 | | } |
| | | 329 | | |
| | | 330 | | /// <summary> |
| | | 331 | | /// Resolves symlinked file paths on the supplied sources to the real on-disk target. |
| | | 332 | | /// Skipped when <paramref name="enablePathSubstitution"/> is set because the path may |
| | | 333 | | /// already have been rewritten to a UNC/URL meant for the client to consume directly. |
| | | 334 | | /// </summary> |
| | | 335 | | private static void ResolveSymlinkPaths(IReadOnlyList<MediaSourceInfo> sources, bool enablePathSubstitution) |
| | | 336 | | { |
| | 0 | 337 | | if (enablePathSubstitution) |
| | | 338 | | { |
| | 0 | 339 | | return; |
| | | 340 | | } |
| | | 341 | | |
| | 0 | 342 | | foreach (var source in sources) |
| | | 343 | | { |
| | 0 | 344 | | if (source.Protocol == MediaProtocol.File |
| | 0 | 345 | | && FileSystemHelper.ResolveLinkTarget(source.Path, returnFinalTarget: true) is { Exists: true } targ |
| | | 346 | | { |
| | 0 | 347 | | source.Path = target.FullName; |
| | | 348 | | } |
| | | 349 | | } |
| | 0 | 350 | | } |
| | | 351 | | |
| | | 352 | | private static void SetKeyProperties(IMediaSourceProvider provider, MediaSourceInfo mediaSource) |
| | | 353 | | { |
| | 0 | 354 | | var prefix = provider.GetType().FullName.GetMD5().ToString("N", CultureInfo.InvariantCulture) + LiveStreamId |
| | | 355 | | |
| | 0 | 356 | | if (!string.IsNullOrEmpty(mediaSource.OpenToken) && !mediaSource.OpenToken.StartsWith(prefix, StringComparis |
| | | 357 | | { |
| | 0 | 358 | | mediaSource.OpenToken = prefix + mediaSource.OpenToken; |
| | | 359 | | } |
| | | 360 | | |
| | 0 | 361 | | if (!string.IsNullOrEmpty(mediaSource.LiveStreamId) && !mediaSource.LiveStreamId.StartsWith(prefix, StringCo |
| | | 362 | | { |
| | 0 | 363 | | mediaSource.LiveStreamId = prefix + mediaSource.LiveStreamId; |
| | | 364 | | } |
| | 0 | 365 | | } |
| | | 366 | | |
| | | 367 | | public async Task<MediaSourceInfo> GetMediaSource(BaseItem item, string mediaSourceId, string liveStreamId, bool |
| | | 368 | | { |
| | 0 | 369 | | if (!string.IsNullOrEmpty(liveStreamId)) |
| | | 370 | | { |
| | 0 | 371 | | return await GetLiveStream(liveStreamId, cancellationToken).ConfigureAwait(false); |
| | | 372 | | } |
| | | 373 | | |
| | 0 | 374 | | var sources = await GetPlaybackMediaSources(item, null, false, enablePathSubstitution, cancellationToken).Co |
| | | 375 | | |
| | 0 | 376 | | return sources.FirstOrDefault(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase)); |
| | 0 | 377 | | } |
| | | 378 | | |
| | | 379 | | public IReadOnlyList<MediaSourceInfo> GetStaticMediaSources(BaseItem item, bool enablePathSubstitution, User use |
| | | 380 | | { |
| | 0 | 381 | | ArgumentNullException.ThrowIfNull(item); |
| | | 382 | | |
| | 0 | 383 | | var hasMediaSources = (IHasMediaSources)item; |
| | | 384 | | |
| | 0 | 385 | | var sources = hasMediaSources.GetMediaSources(enablePathSubstitution); |
| | | 386 | | |
| | 0 | 387 | | if (user is not null) |
| | | 388 | | { |
| | 0 | 389 | | sources = sources |
| | 0 | 390 | | .Where(source => !Guid.TryParse(source.Id, out var sourceId) |
| | 0 | 391 | | || sourceId.Equals(item.Id) |
| | 0 | 392 | | || _libraryManager.GetItemById<BaseItem>(sourceId, user) is not null) |
| | 0 | 393 | | .ToArray(); |
| | | 394 | | |
| | 0 | 395 | | foreach (var source in sources) |
| | | 396 | | { |
| | 0 | 397 | | SetDefaultAudioAndSubtitleStreamIndices(item, source, user); |
| | | 398 | | |
| | 0 | 399 | | if (item.MediaType == MediaType.Audio) |
| | | 400 | | { |
| | 0 | 401 | | source.SupportsTranscoding = user.HasPermission(PermissionKind.EnableAudioPlaybackTranscoding); |
| | | 402 | | } |
| | 0 | 403 | | else if (item.MediaType == MediaType.Video) |
| | | 404 | | { |
| | 0 | 405 | | source.SupportsTranscoding = user.HasPermission(PermissionKind.EnableVideoPlaybackTranscoding); |
| | 0 | 406 | | source.SupportsDirectStream = user.HasPermission(PermissionKind.EnablePlaybackRemuxing); |
| | | 407 | | } |
| | | 408 | | } |
| | | 409 | | } |
| | | 410 | | |
| | 0 | 411 | | return sources; |
| | | 412 | | } |
| | | 413 | | |
| | | 414 | | private IReadOnlyList<string> NormalizeLanguage(string language) |
| | | 415 | | { |
| | 49 | 416 | | if (string.IsNullOrEmpty(language)) |
| | | 417 | | { |
| | 1 | 418 | | return []; |
| | | 419 | | } |
| | | 420 | | |
| | 48 | 421 | | var culture = _localizationManager.FindLanguageInfo(language); |
| | 48 | 422 | | if (culture is not null) |
| | | 423 | | { |
| | 48 | 424 | | return culture.Name.Contains('-', StringComparison.OrdinalIgnoreCase) ? [culture.Name] : culture.ThreeLe |
| | | 425 | | } |
| | | 426 | | |
| | 0 | 427 | | return [language]; |
| | | 428 | | } |
| | | 429 | | |
| | | 430 | | private void SetDefaultSubtitleStreamIndex(MediaSourceInfo source, UserItemData userData, User user, bool allowR |
| | | 431 | | { |
| | 22 | 432 | | if (userData is not null |
| | 22 | 433 | | && userData.SubtitleStreamIndex.HasValue |
| | 22 | 434 | | && user.RememberSubtitleSelections |
| | 22 | 435 | | && user.SubtitleMode != SubtitlePlaybackMode.None |
| | 22 | 436 | | && allowRememberingSelection) |
| | | 437 | | { |
| | 0 | 438 | | var index = userData.SubtitleStreamIndex.Value; |
| | | 439 | | // Make sure the saved index is still valid |
| | 0 | 440 | | if (index == -1 || source.MediaStreams.Any(i => i.Type == MediaStreamType.Subtitle && i.Index == index)) |
| | | 441 | | { |
| | 0 | 442 | | source.DefaultSubtitleStreamIndex = index; |
| | 0 | 443 | | return; |
| | | 444 | | } |
| | | 445 | | } |
| | | 446 | | |
| | 22 | 447 | | var preferredSubs = NormalizeLanguage(user.SubtitleLanguagePreference); |
| | | 448 | | |
| | 22 | 449 | | var defaultAudioIndex = source.DefaultAudioStreamIndex; |
| | 22 | 450 | | var audioLanguage = defaultAudioIndex is null |
| | 22 | 451 | | ? null |
| | 22 | 452 | | : source.MediaStreams.Where(i => i.Type == MediaStreamType.Audio && i.Index == defaultAudioIndex).Select |
| | | 453 | | |
| | 22 | 454 | | source.DefaultSubtitleStreamIndex = MediaStreamSelector.GetDefaultSubtitleStreamIndex( |
| | 22 | 455 | | source.MediaStreams, |
| | 22 | 456 | | preferredSubs, |
| | 22 | 457 | | user.SubtitleMode, |
| | 22 | 458 | | audioLanguage); |
| | | 459 | | |
| | 22 | 460 | | MediaStreamSelector.SetSubtitleStreamScores(source.MediaStreams, preferredSubs, user.SubtitleMode, audioLang |
| | 22 | 461 | | } |
| | | 462 | | |
| | | 463 | | private void SetDefaultAudioStreamIndex(MediaSourceInfo source, UserItemData userData, User user, bool allowReme |
| | | 464 | | { |
| | 22 | 465 | | if (userData is not null && userData.AudioStreamIndex.HasValue && user.RememberAudioSelections && allowRemem |
| | | 466 | | { |
| | 0 | 467 | | var index = userData.AudioStreamIndex.Value; |
| | | 468 | | // Make sure the saved index is still valid |
| | 0 | 469 | | if (source.MediaStreams.Any(i => i.Type == MediaStreamType.Audio && i.Index == index)) |
| | | 470 | | { |
| | 0 | 471 | | source.DefaultAudioStreamIndex = index; |
| | 0 | 472 | | source.DefaultAudioIndexSource = AudioIndexSource.User; |
| | 0 | 473 | | return; |
| | | 474 | | } |
| | | 475 | | } |
| | | 476 | | |
| | 22 | 477 | | if (string.Equals(user.AudioLanguagePreference, "OriginalLanguage", StringComparison.OrdinalIgnoreCase)) |
| | | 478 | | { |
| | 16 | 479 | | if (user.PlayDefaultAudioTrack) |
| | | 480 | | { |
| | 6 | 481 | | source.DefaultAudioStreamIndex = MediaStreamSelector.GetDefaultAudioStreamIndex( |
| | 6 | 482 | | source.MediaStreams, |
| | 6 | 483 | | NormalizeLanguage(originalLanguage), |
| | 6 | 484 | | user.PlayDefaultAudioTrack); |
| | 6 | 485 | | return; |
| | | 486 | | } |
| | | 487 | | |
| | 10 | 488 | | var originalIndex = source.MediaStreams.FindIndex(i => i.Type == MediaStreamType.Audio && i.IsOriginal); |
| | | 489 | | |
| | 10 | 490 | | if (!string.IsNullOrWhiteSpace(originalLanguage) && originalIndex != -1) |
| | | 491 | | { |
| | 4 | 492 | | var mediaLanguageOriginal = source.MediaStreams[originalIndex].Language; |
| | 4 | 493 | | if (NormalizeLanguage(mediaLanguageOriginal).Contains(NormalizeLanguage(originalLanguage).FirstOrDef |
| | | 494 | | { |
| | 1 | 495 | | source.DefaultAudioStreamIndex = originalIndex; |
| | 1 | 496 | | return; |
| | | 497 | | } |
| | | 498 | | } |
| | 6 | 499 | | else if (originalIndex != -1) |
| | | 500 | | { |
| | 2 | 501 | | source.DefaultAudioStreamIndex = originalIndex; |
| | 2 | 502 | | return; |
| | | 503 | | } |
| | | 504 | | } |
| | | 505 | | |
| | 13 | 506 | | var preferredAudio = string.Equals(user.AudioLanguagePreference, "OriginalLanguage", StringComparison.Ordina |
| | 13 | 507 | | ? NormalizeLanguage(originalLanguage) |
| | 13 | 508 | | : NormalizeLanguage(user.AudioLanguagePreference); |
| | | 509 | | |
| | 13 | 510 | | source.DefaultAudioStreamIndex = MediaStreamSelector.GetDefaultAudioStreamIndex(source.MediaStreams, preferr |
| | 13 | 511 | | if (user.PlayDefaultAudioTrack) |
| | | 512 | | { |
| | 3 | 513 | | source.DefaultAudioIndexSource |= AudioIndexSource.Default; |
| | | 514 | | } |
| | | 515 | | |
| | 13 | 516 | | if (preferredAudio.Count > 0) |
| | | 517 | | { |
| | 13 | 518 | | source.DefaultAudioIndexSource |= AudioIndexSource.Language; |
| | | 519 | | } |
| | 13 | 520 | | } |
| | | 521 | | |
| | | 522 | | public void SetDefaultAudioAndSubtitleStreamIndices(BaseItem item, MediaSourceInfo source, User user) |
| | | 523 | | { |
| | | 524 | | // Item would only be null if the app didn't supply ItemId as part of the live stream open request |
| | 22 | 525 | | var mediaType = item?.MediaType ?? MediaType.Video; |
| | | 526 | | |
| | 22 | 527 | | if (mediaType == MediaType.Video) |
| | | 528 | | { |
| | 22 | 529 | | var userData = item is null ? null : _userDataManager.GetUserData(user, item); |
| | | 530 | | |
| | 22 | 531 | | var allowRememberingSelection = item is null || item.EnableRememberingTrackSelections; |
| | | 532 | | |
| | 22 | 533 | | var originalLanguage = item?.GetInheritedOriginalLanguage(); |
| | | 534 | | |
| | 22 | 535 | | SetDefaultAudioStreamIndex(source, userData, user, allowRememberingSelection, originalLanguage); |
| | 22 | 536 | | SetDefaultSubtitleStreamIndex(source, userData, user, allowRememberingSelection); |
| | | 537 | | } |
| | 0 | 538 | | else if (mediaType == MediaType.Audio) |
| | | 539 | | { |
| | 0 | 540 | | var audio = source.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio); |
| | | 541 | | |
| | 0 | 542 | | if (audio is not null) |
| | | 543 | | { |
| | 0 | 544 | | source.DefaultAudioStreamIndex = audio.Index; |
| | | 545 | | } |
| | | 546 | | } |
| | 0 | 547 | | } |
| | | 548 | | |
| | | 549 | | private static IEnumerable<MediaSourceInfo> SortMediaSources(IEnumerable<MediaSourceInfo> sources, Guid preferre |
| | | 550 | | { |
| | | 551 | | // The source belonging to the queried item sorts first so it stays the default that gets played. |
| | 0 | 552 | | var preferredId = preferredItemId.IsEmpty() |
| | 0 | 553 | | ? null |
| | 0 | 554 | | : preferredItemId.ToString("N", CultureInfo.InvariantCulture); |
| | | 555 | | |
| | 0 | 556 | | return sources |
| | 0 | 557 | | .OrderByDescending(i => preferredId is not null && string.Equals(i.Id, preferredId, StringComparison.Ord |
| | 0 | 558 | | .ThenBy(i => |
| | 0 | 559 | | { |
| | 0 | 560 | | if (i.VideoType.HasValue && i.VideoType.Value == VideoType.VideoFile) |
| | 0 | 561 | | { |
| | 0 | 562 | | return 0; |
| | 0 | 563 | | } |
| | 0 | 564 | | |
| | 0 | 565 | | return 1; |
| | 0 | 566 | | }) |
| | 0 | 567 | | .ThenBy(i => i.Video3DFormat.HasValue ? 1 : 0) |
| | 0 | 568 | | .ThenByDescending(i => |
| | 0 | 569 | | { |
| | 0 | 570 | | var stream = i.VideoStream; |
| | 0 | 571 | | |
| | 0 | 572 | | return stream?.Width ?? 0; |
| | 0 | 573 | | }) |
| | 0 | 574 | | .Where(i => i.Type != MediaSourceType.Placeholder); |
| | | 575 | | } |
| | | 576 | | |
| | | 577 | | public async Task<Tuple<LiveStreamResponse, IDirectStreamProvider>> OpenLiveStreamInternal(LiveStreamRequest req |
| | | 578 | | { |
| | | 579 | | MediaSourceInfo mediaSource; |
| | | 580 | | ILiveStream liveStream; |
| | | 581 | | |
| | 0 | 582 | | using (await _liveStreamLocker.LockAsync(cancellationToken).ConfigureAwait(false)) |
| | | 583 | | { |
| | 0 | 584 | | var (provider, keyId) = GetProvider(request.OpenToken); |
| | | 585 | | |
| | 0 | 586 | | var currentLiveStreams = _openStreams.Values.ToList(); |
| | | 587 | | |
| | 0 | 588 | | liveStream = await provider.OpenMediaSource(keyId, currentLiveStreams, cancellationToken).ConfigureAwait |
| | | 589 | | |
| | 0 | 590 | | mediaSource = liveStream.MediaSource; |
| | | 591 | | |
| | | 592 | | // Validate that this is actually possible |
| | 0 | 593 | | if (mediaSource.SupportsDirectStream) |
| | | 594 | | { |
| | 0 | 595 | | mediaSource.SupportsDirectStream = SupportsDirectStream(mediaSource.Path, mediaSource.Protocol); |
| | | 596 | | } |
| | | 597 | | |
| | 0 | 598 | | SetKeyProperties(provider, mediaSource); |
| | | 599 | | |
| | 0 | 600 | | _openStreams[mediaSource.LiveStreamId] = liveStream; |
| | 0 | 601 | | } |
| | | 602 | | |
| | | 603 | | try |
| | | 604 | | { |
| | 0 | 605 | | if (mediaSource.MediaStreams.Any(i => i.Index != -1) || !mediaSource.SupportsProbing) |
| | | 606 | | { |
| | 0 | 607 | | AddMediaInfo(mediaSource); |
| | | 608 | | } |
| | | 609 | | else |
| | | 610 | | { |
| | | 611 | | // hack - these two values were taken from LiveTVMediaSourceProvider |
| | 0 | 612 | | string cacheKey = request.OpenToken; |
| | | 613 | | |
| | 0 | 614 | | await new LiveStreamHelper(_mediaEncoder, _logger, _appPaths) |
| | 0 | 615 | | .AddMediaInfoWithProbe(mediaSource, false, cacheKey, true, cancellationToken) |
| | 0 | 616 | | .ConfigureAwait(false); |
| | | 617 | | } |
| | 0 | 618 | | } |
| | 0 | 619 | | catch (Exception ex) |
| | | 620 | | { |
| | 0 | 621 | | _logger.LogError(ex, "Error probing live tv stream"); |
| | 0 | 622 | | AddMediaInfo(mediaSource); |
| | 0 | 623 | | } |
| | | 624 | | |
| | | 625 | | // TODO: @bond Fix |
| | 0 | 626 | | var json = JsonSerializer.SerializeToUtf8Bytes(mediaSource, _jsonOptions); |
| | 0 | 627 | | _logger.LogInformation("Live stream opened: {@MediaSource}", mediaSource); |
| | 0 | 628 | | var clone = JsonSerializer.Deserialize<MediaSourceInfo>(json, _jsonOptions); |
| | | 629 | | |
| | 0 | 630 | | if (!request.UserId.IsEmpty()) |
| | | 631 | | { |
| | 0 | 632 | | var user = _userManager.GetUserById(request.UserId); |
| | 0 | 633 | | var item = request.ItemId.IsEmpty() |
| | 0 | 634 | | ? null |
| | 0 | 635 | | : _libraryManager.GetItemById(request.ItemId); |
| | 0 | 636 | | SetDefaultAudioAndSubtitleStreamIndices(item, clone, user); |
| | | 637 | | } |
| | | 638 | | |
| | 0 | 639 | | return new Tuple<LiveStreamResponse, IDirectStreamProvider>(new LiveStreamResponse(clone), liveStream as IDi |
| | 0 | 640 | | } |
| | | 641 | | |
| | | 642 | | private static void AddMediaInfo(MediaSourceInfo mediaSource) |
| | | 643 | | { |
| | 0 | 644 | | mediaSource.DefaultSubtitleStreamIndex = null; |
| | | 645 | | |
| | | 646 | | // Null this out so that it will be treated like a live stream |
| | 0 | 647 | | if (mediaSource.IsInfiniteStream) |
| | | 648 | | { |
| | 0 | 649 | | mediaSource.RunTimeTicks = null; |
| | | 650 | | } |
| | | 651 | | |
| | 0 | 652 | | var audioStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio); |
| | | 653 | | |
| | 0 | 654 | | if (audioStream is null || audioStream.Index == -1) |
| | | 655 | | { |
| | 0 | 656 | | mediaSource.DefaultAudioStreamIndex = null; |
| | | 657 | | } |
| | | 658 | | else |
| | | 659 | | { |
| | 0 | 660 | | mediaSource.DefaultAudioStreamIndex = audioStream.Index; |
| | | 661 | | } |
| | | 662 | | |
| | 0 | 663 | | var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video); |
| | 0 | 664 | | if (videoStream is not null) |
| | | 665 | | { |
| | 0 | 666 | | if (!videoStream.BitRate.HasValue) |
| | | 667 | | { |
| | 0 | 668 | | var width = videoStream.Width ?? 1920; |
| | | 669 | | |
| | 0 | 670 | | if (width >= 3000) |
| | | 671 | | { |
| | 0 | 672 | | videoStream.BitRate = 30000000; |
| | | 673 | | } |
| | 0 | 674 | | else if (width >= 1900) |
| | | 675 | | { |
| | 0 | 676 | | videoStream.BitRate = 20000000; |
| | | 677 | | } |
| | 0 | 678 | | else if (width >= 1200) |
| | | 679 | | { |
| | 0 | 680 | | videoStream.BitRate = 8000000; |
| | | 681 | | } |
| | 0 | 682 | | else if (width >= 700) |
| | | 683 | | { |
| | 0 | 684 | | videoStream.BitRate = 2000000; |
| | | 685 | | } |
| | | 686 | | } |
| | | 687 | | } |
| | | 688 | | |
| | | 689 | | // Try to estimate this |
| | 0 | 690 | | mediaSource.InferTotalBitrate(); |
| | 0 | 691 | | } |
| | | 692 | | |
| | | 693 | | public async Task<LiveStreamResponse> OpenLiveStream(LiveStreamRequest request, CancellationToken cancellationTo |
| | | 694 | | { |
| | 0 | 695 | | var result = await OpenLiveStreamInternal(request, cancellationToken).ConfigureAwait(false); |
| | 0 | 696 | | return result.Item1; |
| | 0 | 697 | | } |
| | | 698 | | |
| | | 699 | | public async Task<MediaSourceInfo> GetLiveStreamMediaInfo(string id, CancellationToken cancellationToken) |
| | | 700 | | { |
| | | 701 | | // TODO probably shouldn't throw here but it is kept for "backwards compatibility" |
| | 0 | 702 | | var liveStreamInfo = GetLiveStreamInfo(id) ?? throw new ResourceNotFoundException(); |
| | | 703 | | |
| | 0 | 704 | | var mediaSource = liveStreamInfo.MediaSource; |
| | | 705 | | |
| | 0 | 706 | | if (liveStreamInfo is IDirectStreamProvider) |
| | | 707 | | { |
| | 0 | 708 | | var info = await _mediaEncoder.GetMediaInfo( |
| | 0 | 709 | | new MediaInfoRequest |
| | 0 | 710 | | { |
| | 0 | 711 | | MediaSource = mediaSource, |
| | 0 | 712 | | ExtractChapters = false, |
| | 0 | 713 | | MediaType = DlnaProfileType.Video |
| | 0 | 714 | | }, |
| | 0 | 715 | | cancellationToken).ConfigureAwait(false); |
| | | 716 | | |
| | 0 | 717 | | mediaSource.MediaStreams = info.MediaStreams; |
| | 0 | 718 | | mediaSource.Container = info.Container; |
| | 0 | 719 | | mediaSource.Bitrate = info.Bitrate; |
| | | 720 | | } |
| | | 721 | | |
| | 0 | 722 | | return mediaSource; |
| | 0 | 723 | | } |
| | | 724 | | |
| | | 725 | | public async Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudio, string cacheKey, bool addProb |
| | | 726 | | { |
| | 0 | 727 | | var originalRuntime = mediaSource.RunTimeTicks; |
| | | 728 | | |
| | 0 | 729 | | var now = DateTime.UtcNow; |
| | | 730 | | |
| | 0 | 731 | | MediaInfo mediaInfo = null; |
| | 0 | 732 | | var cacheFilePath = string.IsNullOrEmpty(cacheKey) ? null : Path.Combine(_appPaths.CachePath, "mediainfo", c |
| | | 733 | | |
| | 0 | 734 | | if (!string.IsNullOrEmpty(cacheKey)) |
| | | 735 | | { |
| | 0 | 736 | | FileStream jsonStream = AsyncFile.OpenRead(cacheFilePath); |
| | | 737 | | try |
| | | 738 | | { |
| | 0 | 739 | | mediaInfo = await JsonSerializer.DeserializeAsync<MediaInfo>(jsonStream, _jsonOptions, cancellationT |
| | 0 | 740 | | } |
| | 0 | 741 | | catch (Exception ex) |
| | | 742 | | { |
| | 0 | 743 | | _logger.LogDebug(ex, "Error parsing cached media info."); |
| | 0 | 744 | | } |
| | | 745 | | finally |
| | | 746 | | { |
| | 0 | 747 | | await jsonStream.DisposeAsync().ConfigureAwait(false); |
| | | 748 | | } |
| | 0 | 749 | | } |
| | | 750 | | |
| | 0 | 751 | | if (mediaInfo is null) |
| | | 752 | | { |
| | 0 | 753 | | if (addProbeDelay) |
| | | 754 | | { |
| | 0 | 755 | | var delayMs = mediaSource.AnalyzeDurationMs ?? 0; |
| | 0 | 756 | | delayMs = Math.Max(3000, delayMs); |
| | 0 | 757 | | await Task.Delay(delayMs, cancellationToken).ConfigureAwait(false); |
| | | 758 | | } |
| | | 759 | | |
| | 0 | 760 | | if (isLiveStream) |
| | | 761 | | { |
| | 0 | 762 | | mediaSource.AnalyzeDurationMs = 3000; |
| | | 763 | | } |
| | | 764 | | |
| | 0 | 765 | | mediaInfo = await _mediaEncoder.GetMediaInfo( |
| | 0 | 766 | | new MediaInfoRequest |
| | 0 | 767 | | { |
| | 0 | 768 | | MediaSource = mediaSource, |
| | 0 | 769 | | MediaType = isAudio ? DlnaProfileType.Audio : DlnaProfileType.Video, |
| | 0 | 770 | | ExtractChapters = false |
| | 0 | 771 | | }, |
| | 0 | 772 | | cancellationToken).ConfigureAwait(false); |
| | | 773 | | |
| | 0 | 774 | | if (cacheFilePath is not null) |
| | | 775 | | { |
| | 0 | 776 | | Directory.CreateDirectory(Path.GetDirectoryName(cacheFilePath)); |
| | 0 | 777 | | FileStream createStream = AsyncFile.Create(cacheFilePath); |
| | 0 | 778 | | await using (createStream.ConfigureAwait(false)) |
| | | 779 | | { |
| | 0 | 780 | | await JsonSerializer.SerializeAsync(createStream, mediaInfo, _jsonOptions, cancellationToken).Co |
| | | 781 | | } |
| | | 782 | | |
| | | 783 | | // _logger.LogDebug("Saved media info to {0}", cacheFilePath); |
| | | 784 | | } |
| | | 785 | | } |
| | | 786 | | |
| | 0 | 787 | | var mediaStreams = mediaInfo.MediaStreams; |
| | | 788 | | |
| | 0 | 789 | | if (isLiveStream && !string.IsNullOrEmpty(cacheKey)) |
| | | 790 | | { |
| | 0 | 791 | | var newList = new List<MediaStream>(); |
| | 0 | 792 | | newList.AddRange(mediaStreams.Where(i => i.Type == MediaStreamType.Video).Take(1)); |
| | 0 | 793 | | newList.AddRange(mediaStreams.Where(i => i.Type == MediaStreamType.Audio).Take(1)); |
| | | 794 | | |
| | 0 | 795 | | foreach (var stream in newList) |
| | | 796 | | { |
| | 0 | 797 | | stream.Index = -1; |
| | 0 | 798 | | stream.Language = null; |
| | | 799 | | } |
| | | 800 | | |
| | 0 | 801 | | mediaStreams = newList; |
| | | 802 | | } |
| | | 803 | | |
| | 0 | 804 | | _logger.LogInformation("Live tv media info probe took {0} seconds", (DateTime.UtcNow - now).TotalSeconds.ToS |
| | | 805 | | |
| | 0 | 806 | | mediaSource.Bitrate = mediaInfo.Bitrate; |
| | 0 | 807 | | mediaSource.Container = mediaInfo.Container; |
| | 0 | 808 | | mediaSource.Formats = mediaInfo.Formats; |
| | 0 | 809 | | mediaSource.MediaStreams = mediaStreams; |
| | 0 | 810 | | mediaSource.RunTimeTicks = mediaInfo.RunTimeTicks; |
| | 0 | 811 | | mediaSource.Size = mediaInfo.Size; |
| | 0 | 812 | | mediaSource.Timestamp = mediaInfo.Timestamp; |
| | 0 | 813 | | mediaSource.Video3DFormat = mediaInfo.Video3DFormat; |
| | 0 | 814 | | mediaSource.VideoType = mediaInfo.VideoType; |
| | | 815 | | |
| | 0 | 816 | | mediaSource.DefaultSubtitleStreamIndex = null; |
| | | 817 | | |
| | 0 | 818 | | if (isLiveStream) |
| | | 819 | | { |
| | | 820 | | // Null this out so that it will be treated like a live stream |
| | 0 | 821 | | if (!originalRuntime.HasValue) |
| | | 822 | | { |
| | 0 | 823 | | mediaSource.RunTimeTicks = null; |
| | | 824 | | } |
| | | 825 | | } |
| | | 826 | | |
| | 0 | 827 | | var audioStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio); |
| | | 828 | | |
| | 0 | 829 | | if (audioStream is null || audioStream.Index == -1) |
| | | 830 | | { |
| | 0 | 831 | | mediaSource.DefaultAudioStreamIndex = null; |
| | | 832 | | } |
| | | 833 | | else |
| | | 834 | | { |
| | 0 | 835 | | mediaSource.DefaultAudioStreamIndex = audioStream.Index; |
| | | 836 | | } |
| | | 837 | | |
| | 0 | 838 | | var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video); |
| | 0 | 839 | | if (videoStream is not null) |
| | | 840 | | { |
| | 0 | 841 | | if (!videoStream.BitRate.HasValue) |
| | | 842 | | { |
| | 0 | 843 | | var width = videoStream.Width ?? 1920; |
| | | 844 | | |
| | 0 | 845 | | if (width >= 3000) |
| | | 846 | | { |
| | 0 | 847 | | videoStream.BitRate = 30000000; |
| | | 848 | | } |
| | 0 | 849 | | else if (width >= 1900) |
| | | 850 | | { |
| | 0 | 851 | | videoStream.BitRate = 20000000; |
| | | 852 | | } |
| | 0 | 853 | | else if (width >= 1200) |
| | | 854 | | { |
| | 0 | 855 | | videoStream.BitRate = 8000000; |
| | | 856 | | } |
| | 0 | 857 | | else if (width >= 700) |
| | | 858 | | { |
| | 0 | 859 | | videoStream.BitRate = 2000000; |
| | | 860 | | } |
| | | 861 | | } |
| | | 862 | | |
| | | 863 | | // This is coming up false and preventing stream copy |
| | 0 | 864 | | videoStream.IsAVC = null; |
| | | 865 | | } |
| | | 866 | | |
| | 0 | 867 | | if (isLiveStream) |
| | | 868 | | { |
| | 0 | 869 | | mediaSource.AnalyzeDurationMs = 3000; |
| | | 870 | | } |
| | | 871 | | |
| | | 872 | | // Try to estimate this |
| | 0 | 873 | | mediaSource.InferTotalBitrate(true); |
| | 0 | 874 | | } |
| | | 875 | | |
| | | 876 | | public Task<Tuple<MediaSourceInfo, IDirectStreamProvider>> GetLiveStreamWithDirectStreamProvider(string id, Canc |
| | | 877 | | { |
| | 0 | 878 | | ArgumentException.ThrowIfNullOrEmpty(id); |
| | | 879 | | |
| | 0 | 880 | | var info = GetLiveStreamInfo(id); |
| | 0 | 881 | | if (info is null) |
| | | 882 | | { |
| | 0 | 883 | | return Task.FromResult<Tuple<MediaSourceInfo, IDirectStreamProvider>>(new Tuple<MediaSourceInfo, IDirect |
| | | 884 | | } |
| | | 885 | | |
| | 0 | 886 | | return Task.FromResult<Tuple<MediaSourceInfo, IDirectStreamProvider>>(new Tuple<MediaSourceInfo, IDirectStre |
| | | 887 | | } |
| | | 888 | | |
| | | 889 | | public ILiveStream GetLiveStreamInfo(string id) |
| | | 890 | | { |
| | 0 | 891 | | ArgumentException.ThrowIfNullOrEmpty(id); |
| | | 892 | | |
| | 0 | 893 | | if (_openStreams.TryGetValue(id, out ILiveStream info)) |
| | | 894 | | { |
| | 0 | 895 | | return info; |
| | | 896 | | } |
| | | 897 | | |
| | 0 | 898 | | return null; |
| | | 899 | | } |
| | | 900 | | |
| | | 901 | | /// <inheritdoc /> |
| | | 902 | | public ILiveStream GetLiveStreamInfoByUniqueId(string uniqueId) |
| | | 903 | | { |
| | 0 | 904 | | return _openStreams.Values.FirstOrDefault(stream => string.Equals(uniqueId, stream?.UniqueId, StringComparis |
| | | 905 | | } |
| | | 906 | | |
| | | 907 | | public async Task<MediaSourceInfo> GetLiveStream(string id, CancellationToken cancellationToken) |
| | | 908 | | { |
| | 0 | 909 | | var result = await GetLiveStreamWithDirectStreamProvider(id, cancellationToken).ConfigureAwait(false); |
| | 0 | 910 | | return result.Item1; |
| | 0 | 911 | | } |
| | | 912 | | |
| | | 913 | | public async Task<IReadOnlyList<MediaSourceInfo>> GetRecordingStreamMediaSources(ActiveRecordingInfo info, Cance |
| | | 914 | | { |
| | 0 | 915 | | var stream = new MediaSourceInfo |
| | 0 | 916 | | { |
| | 0 | 917 | | EncoderPath = _appHost.GetApiUrlForLocalAccess() + "/LiveTv/LiveRecordings/" + info.Id + "/stream", |
| | 0 | 918 | | EncoderProtocol = MediaProtocol.Http, |
| | 0 | 919 | | Path = info.Path, |
| | 0 | 920 | | Protocol = MediaProtocol.File, |
| | 0 | 921 | | Id = info.Id, |
| | 0 | 922 | | SupportsDirectPlay = false, |
| | 0 | 923 | | SupportsDirectStream = true, |
| | 0 | 924 | | SupportsTranscoding = true, |
| | 0 | 925 | | IsInfiniteStream = true, |
| | 0 | 926 | | RequiresOpening = false, |
| | 0 | 927 | | RequiresClosing = false, |
| | 0 | 928 | | BufferMs = 0, |
| | 0 | 929 | | IgnoreDts = true, |
| | 0 | 930 | | IgnoreIndex = true |
| | 0 | 931 | | }; |
| | | 932 | | |
| | 0 | 933 | | await new LiveStreamHelper(_mediaEncoder, _logger, _appPaths) |
| | 0 | 934 | | .AddMediaInfoWithProbe(stream, false, false, cancellationToken).ConfigureAwait(false); |
| | | 935 | | |
| | 0 | 936 | | return [stream]; |
| | 0 | 937 | | } |
| | | 938 | | |
| | | 939 | | public async Task CloseLiveStream(string id) |
| | | 940 | | { |
| | 0 | 941 | | ArgumentException.ThrowIfNullOrEmpty(id); |
| | | 942 | | |
| | 0 | 943 | | using (await _liveStreamLocker.LockAsync().ConfigureAwait(false)) |
| | | 944 | | { |
| | 0 | 945 | | if (_openStreams.TryGetValue(id, out ILiveStream liveStream)) |
| | | 946 | | { |
| | 0 | 947 | | liveStream.ConsumerCount--; |
| | | 948 | | |
| | 0 | 949 | | _logger.LogInformation("Live stream {0} consumer count is now {1}", liveStream.OriginalStreamId, liv |
| | | 950 | | |
| | 0 | 951 | | if (liveStream.ConsumerCount <= 0) |
| | | 952 | | { |
| | 0 | 953 | | _openStreams.TryRemove(id, out _); |
| | | 954 | | |
| | 0 | 955 | | _logger.LogInformation("Closing live stream {0}", id); |
| | | 956 | | |
| | 0 | 957 | | await liveStream.Close().ConfigureAwait(false); |
| | 0 | 958 | | _logger.LogInformation("Live stream {0} closed successfully", id); |
| | | 959 | | } |
| | | 960 | | } |
| | 0 | 961 | | } |
| | 0 | 962 | | } |
| | | 963 | | |
| | | 964 | | private (IMediaSourceProvider MediaSourceProvider, string KeyId) GetProvider(string key) |
| | | 965 | | { |
| | 0 | 966 | | ArgumentException.ThrowIfNullOrEmpty(key); |
| | | 967 | | |
| | 0 | 968 | | var keys = key.Split(LiveStreamIdDelimiter, 2); |
| | | 969 | | |
| | 0 | 970 | | var provider = _providers.FirstOrDefault(i => string.Equals(i.GetType().FullName.GetMD5().ToString("N", Cult |
| | | 971 | | |
| | 0 | 972 | | var splitIndex = key.IndexOf(LiveStreamIdDelimiter, StringComparison.Ordinal); |
| | 0 | 973 | | var keyId = key.Substring(splitIndex + 1); |
| | | 974 | | |
| | 0 | 975 | | return (provider, keyId); |
| | | 976 | | } |
| | | 977 | | |
| | | 978 | | /// <inheritdoc /> |
| | | 979 | | public void Dispose() |
| | | 980 | | { |
| | 21 | 981 | | Dispose(true); |
| | 21 | 982 | | GC.SuppressFinalize(this); |
| | 21 | 983 | | } |
| | | 984 | | |
| | | 985 | | /// <summary> |
| | | 986 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 987 | | /// </summary> |
| | | 988 | | /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release o |
| | | 989 | | protected virtual void Dispose(bool dispose) |
| | | 990 | | { |
| | 21 | 991 | | if (dispose) |
| | | 992 | | { |
| | 42 | 993 | | foreach (var key in _openStreams.Keys.ToList()) |
| | | 994 | | { |
| | 0 | 995 | | CloseLiveStream(key).GetAwaiter().GetResult(); |
| | | 996 | | } |
| | | 997 | | |
| | 21 | 998 | | _liveStreamLocker.Dispose(); |
| | | 999 | | } |
| | 21 | 1000 | | } |
| | | 1001 | | } |
| | | 1002 | | } |