LiveTvMediaSourceProvider.cs 5.3 KB

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