LiveStreamHelper.cs 3.8 KB

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