MediaSourceManager.cs 34 KB

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