MediaSourceManager.cs 34 KB

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