2
0

LiveStreamHelper.cs 6.4 KB

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