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