LiveStreamHelper.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Controller.MediaEncoding;
  7. using MediaBrowser.Model.Dlna;
  8. using MediaBrowser.Model.Dto;
  9. using MediaBrowser.Model.Entities;
  10. using MediaBrowser.Model.Logging;
  11. namespace Emby.Server.Implementations.LiveTv
  12. {
  13. public class LiveStreamHelper
  14. {
  15. private readonly IMediaEncoder _mediaEncoder;
  16. private readonly ILogger _logger;
  17. const int ProbeAnalyzeDurationMs = 3000;
  18. const int PlaybackAnalyzeDurationMs = 3000;
  19. public LiveStreamHelper(IMediaEncoder mediaEncoder, ILogger logger)
  20. {
  21. _mediaEncoder = mediaEncoder;
  22. _logger = logger;
  23. }
  24. public async Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudio, CancellationToken cancellationToken)
  25. {
  26. var originalRuntime = mediaSource.RunTimeTicks;
  27. var now = DateTime.UtcNow;
  28. var info = await _mediaEncoder.GetMediaInfo(new MediaInfoRequest
  29. {
  30. InputPath = mediaSource.Path,
  31. Protocol = mediaSource.Protocol,
  32. MediaType = isAudio ? DlnaProfileType.Audio : DlnaProfileType.Video,
  33. ExtractChapters = false,
  34. AnalyzeDurationMs = ProbeAnalyzeDurationMs
  35. }, cancellationToken).ConfigureAwait(false);
  36. _logger.Info("Live tv media info probe took {0} seconds", (DateTime.UtcNow - now).TotalSeconds.ToString(CultureInfo.InvariantCulture));
  37. mediaSource.Bitrate = info.Bitrate;
  38. mediaSource.Container = info.Container;
  39. mediaSource.Formats = info.Formats;
  40. mediaSource.MediaStreams = info.MediaStreams;
  41. mediaSource.RunTimeTicks = info.RunTimeTicks;
  42. mediaSource.Size = info.Size;
  43. mediaSource.Timestamp = info.Timestamp;
  44. mediaSource.Video3DFormat = info.Video3DFormat;
  45. mediaSource.VideoType = info.VideoType;
  46. mediaSource.DefaultSubtitleStreamIndex = null;
  47. // Null this out so that it will be treated like a live stream
  48. if (!originalRuntime.HasValue)
  49. {
  50. mediaSource.RunTimeTicks = null;
  51. }
  52. var audioStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Audio);
  53. if (audioStream == null || audioStream.Index == -1)
  54. {
  55. mediaSource.DefaultAudioStreamIndex = null;
  56. }
  57. else
  58. {
  59. mediaSource.DefaultAudioStreamIndex = audioStream.Index;
  60. }
  61. var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Video);
  62. if (videoStream != null)
  63. {
  64. if (!videoStream.BitRate.HasValue)
  65. {
  66. var width = videoStream.Width ?? 1920;
  67. if (width >= 3000)
  68. {
  69. videoStream.BitRate = 30000000;
  70. }
  71. else if (width >= 1900)
  72. {
  73. videoStream.BitRate = 20000000;
  74. }
  75. else if (width >= 1200)
  76. {
  77. videoStream.BitRate = 8000000;
  78. }
  79. else if (width >= 700)
  80. {
  81. videoStream.BitRate = 2000000;
  82. }
  83. }
  84. // This is coming up false and preventing stream copy
  85. videoStream.IsAVC = null;
  86. }
  87. // Try to estimate this
  88. mediaSource.InferTotalBitrate(true);
  89. mediaSource.AnalyzeDurationMs = PlaybackAnalyzeDurationMs;
  90. }
  91. }
  92. }