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