LiveTvMediaSourceProvider.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #pragma warning disable CS1591
  2. #pragma warning disable SA1600
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Controller;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Controller.LiveTv;
  13. using MediaBrowser.Model.Dto;
  14. using MediaBrowser.Model.MediaInfo;
  15. using Microsoft.Extensions.Logging;
  16. namespace Emby.Server.Implementations.LiveTv
  17. {
  18. public class LiveTvMediaSourceProvider : IMediaSourceProvider
  19. {
  20. // Do not use a pipe here because Roku http requests to the server will fail, without any explicit error message.
  21. private const char StreamIdDelimeter = '_';
  22. private const string StreamIdDelimeterString = "_";
  23. private readonly ILiveTvManager _liveTvManager;
  24. private readonly ILogger _logger;
  25. private readonly IMediaSourceManager _mediaSourceManager;
  26. private readonly IServerApplicationHost _appHost;
  27. public LiveTvMediaSourceProvider(ILiveTvManager liveTvManager, ILogger<LiveTvMediaSourceProvider> logger, IMediaSourceManager mediaSourceManager, IServerApplicationHost appHost)
  28. {
  29. _liveTvManager = liveTvManager;
  30. _logger = logger;
  31. _mediaSourceManager = mediaSourceManager;
  32. _appHost = appHost;
  33. }
  34. public Task<IEnumerable<MediaSourceInfo>> GetMediaSources(BaseItem item, CancellationToken cancellationToken)
  35. {
  36. if (item.SourceType == SourceType.LiveTV)
  37. {
  38. var activeRecordingInfo = _liveTvManager.GetActiveRecordingInfo(item.Path);
  39. if (string.IsNullOrEmpty(item.Path) || activeRecordingInfo != null)
  40. {
  41. return GetMediaSourcesInternal(item, activeRecordingInfo, cancellationToken);
  42. }
  43. }
  44. return Task.FromResult<IEnumerable<MediaSourceInfo>>(Array.Empty<MediaSourceInfo>());
  45. }
  46. private async Task<IEnumerable<MediaSourceInfo>> GetMediaSourcesInternal(BaseItem item, ActiveRecordingInfo activeRecordingInfo, CancellationToken cancellationToken)
  47. {
  48. IEnumerable<MediaSourceInfo> sources;
  49. var forceRequireOpening = false;
  50. try
  51. {
  52. if (activeRecordingInfo != null)
  53. {
  54. sources = await EmbyTV.EmbyTV.Current.GetRecordingStreamMediaSources(activeRecordingInfo, cancellationToken)
  55. .ConfigureAwait(false);
  56. }
  57. else
  58. {
  59. sources = await _liveTvManager.GetChannelMediaSources(item, cancellationToken)
  60. .ConfigureAwait(false);
  61. }
  62. }
  63. catch (NotImplementedException)
  64. {
  65. sources = _mediaSourceManager.GetStaticMediaSources(item, false);
  66. forceRequireOpening = true;
  67. }
  68. var list = sources.ToList();
  69. var serverUrl = await _appHost.GetLocalApiUrl(cancellationToken).ConfigureAwait(false);
  70. foreach (var source in list)
  71. {
  72. source.Type = MediaSourceType.Default;
  73. source.BufferMs ??= 1500;
  74. if (source.RequiresOpening || forceRequireOpening)
  75. {
  76. source.RequiresOpening = true;
  77. }
  78. if (source.RequiresOpening)
  79. {
  80. var openKeys = new List<string>
  81. {
  82. item.GetType().Name,
  83. item.Id.ToString("N", CultureInfo.InvariantCulture),
  84. source.Id ?? string.Empty
  85. };
  86. source.OpenToken = string.Join(StreamIdDelimeterString, openKeys);
  87. }
  88. // Dummy this up so that direct play checks can still run
  89. if (string.IsNullOrEmpty(source.Path) && source.Protocol == MediaProtocol.Http)
  90. {
  91. source.Path = serverUrl;
  92. }
  93. }
  94. _logger.LogDebug("MediaSources: {@MediaSources}", list);
  95. return list;
  96. }
  97. /// <inheritdoc />
  98. public async Task<ILiveStream> OpenMediaSource(string openToken, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken)
  99. {
  100. var keys = openToken.Split(new[] { StreamIdDelimeter }, 3);
  101. var mediaSourceId = keys.Length >= 3 ? keys[2] : null;
  102. var info = await _liveTvManager.GetChannelStream(keys[1], mediaSourceId, currentLiveStreams, cancellationToken).ConfigureAwait(false);
  103. var liveStream = info.Item2;
  104. return liveStream;
  105. }
  106. }
  107. }