LiveTvMediaSourceProvider.cs 5.2 KB

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