LiveStreamHelper.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.IO;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using MediaBrowser.Common.Configuration;
  11. using MediaBrowser.Common.Extensions;
  12. using MediaBrowser.Controller.MediaEncoding;
  13. using MediaBrowser.Model.Dlna;
  14. using MediaBrowser.Model.Dto;
  15. using MediaBrowser.Model.Entities;
  16. using MediaBrowser.Model.MediaInfo;
  17. using MediaBrowser.Model.Serialization;
  18. using Microsoft.Extensions.Logging;
  19. namespace Emby.Server.Implementations.Library
  20. {
  21. public class LiveStreamHelper
  22. {
  23. private readonly IMediaEncoder _mediaEncoder;
  24. private readonly ILogger _logger;
  25. private IJsonSerializer _json;
  26. private IApplicationPaths _appPaths;
  27. public LiveStreamHelper(IMediaEncoder mediaEncoder, ILogger logger, IJsonSerializer json, IApplicationPaths appPaths)
  28. {
  29. _mediaEncoder = mediaEncoder;
  30. _logger = logger;
  31. _json = json;
  32. _appPaths = appPaths;
  33. }
  34. public async Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudio, string cacheKey, bool addProbeDelay, CancellationToken cancellationToken)
  35. {
  36. var originalRuntime = mediaSource.RunTimeTicks;
  37. var now = DateTime.UtcNow;
  38. MediaInfo mediaInfo = null;
  39. var cacheFilePath = string.IsNullOrEmpty(cacheKey) ? null : Path.Combine(_appPaths.CachePath, "mediainfo", cacheKey.GetMD5().ToString("N", CultureInfo.InvariantCulture) + ".json");
  40. if (!string.IsNullOrEmpty(cacheKey))
  41. {
  42. try
  43. {
  44. mediaInfo = _json.DeserializeFromFile<MediaInfo>(cacheFilePath);
  45. //_logger.LogDebug("Found cached media info");
  46. }
  47. catch
  48. {
  49. }
  50. }
  51. if (mediaInfo == null)
  52. {
  53. if (addProbeDelay)
  54. {
  55. var delayMs = mediaSource.AnalyzeDurationMs ?? 0;
  56. delayMs = Math.Max(3000, delayMs);
  57. if (delayMs > 0)
  58. {
  59. _logger.LogInformation("Waiting {0}ms before probing the live stream", delayMs);
  60. await Task.Delay(delayMs, cancellationToken).ConfigureAwait(false);
  61. }
  62. }
  63. mediaSource.AnalyzeDurationMs = 3000;
  64. mediaInfo = await _mediaEncoder.GetMediaInfo(new MediaInfoRequest
  65. {
  66. MediaSource = mediaSource,
  67. MediaType = isAudio ? DlnaProfileType.Audio : DlnaProfileType.Video,
  68. ExtractChapters = false
  69. }, cancellationToken).ConfigureAwait(false);
  70. if (cacheFilePath != null)
  71. {
  72. Directory.CreateDirectory(Path.GetDirectoryName(cacheFilePath));
  73. _json.SerializeToFile(mediaInfo, cacheFilePath);
  74. //_logger.LogDebug("Saved media info to {0}", cacheFilePath);
  75. }
  76. }
  77. var mediaStreams = mediaInfo.MediaStreams;
  78. if (!string.IsNullOrEmpty(cacheKey))
  79. {
  80. var newList = new List<MediaStream>();
  81. newList.AddRange(mediaStreams.Where(i => i.Type == MediaStreamType.Video).Take(1));
  82. newList.AddRange(mediaStreams.Where(i => i.Type == MediaStreamType.Audio).Take(1));
  83. foreach (var stream in newList)
  84. {
  85. stream.Index = -1;
  86. stream.Language = null;
  87. }
  88. mediaStreams = newList;
  89. }
  90. _logger.LogInformation("Live tv media info probe took {0} seconds", (DateTime.UtcNow - now).TotalSeconds.ToString(CultureInfo.InvariantCulture));
  91. mediaSource.Bitrate = mediaInfo.Bitrate;
  92. mediaSource.Container = mediaInfo.Container;
  93. mediaSource.Formats = mediaInfo.Formats;
  94. mediaSource.MediaStreams = mediaStreams;
  95. mediaSource.RunTimeTicks = mediaInfo.RunTimeTicks;
  96. mediaSource.Size = mediaInfo.Size;
  97. mediaSource.Timestamp = mediaInfo.Timestamp;
  98. mediaSource.Video3DFormat = mediaInfo.Video3DFormat;
  99. mediaSource.VideoType = mediaInfo.VideoType;
  100. mediaSource.DefaultSubtitleStreamIndex = null;
  101. // Null this out so that it will be treated like a live stream
  102. if (!originalRuntime.HasValue)
  103. {
  104. mediaSource.RunTimeTicks = null;
  105. }
  106. var audioStream = mediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Audio);
  107. if (audioStream == null || audioStream.Index == -1)
  108. {
  109. mediaSource.DefaultAudioStreamIndex = null;
  110. }
  111. else
  112. {
  113. mediaSource.DefaultAudioStreamIndex = audioStream.Index;
  114. }
  115. var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Video);
  116. if (videoStream != null)
  117. {
  118. if (!videoStream.BitRate.HasValue)
  119. {
  120. var width = videoStream.Width ?? 1920;
  121. if (width >= 3000)
  122. {
  123. videoStream.BitRate = 30000000;
  124. }
  125. else if (width >= 1900)
  126. {
  127. videoStream.BitRate = 20000000;
  128. }
  129. else if (width >= 1200)
  130. {
  131. videoStream.BitRate = 8000000;
  132. }
  133. else if (width >= 700)
  134. {
  135. videoStream.BitRate = 2000000;
  136. }
  137. }
  138. // This is coming up false and preventing stream copy
  139. videoStream.IsAVC = null;
  140. }
  141. mediaSource.AnalyzeDurationMs = 3000;
  142. // Try to estimate this
  143. mediaSource.InferTotalBitrate(true);
  144. }
  145. public Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudio, bool addProbeDelay, CancellationToken cancellationToken)
  146. {
  147. return AddMediaInfoWithProbe(mediaSource, isAudio, null, addProbeDelay, cancellationToken);
  148. }
  149. }
  150. }