BifService.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.MediaEncoding;
  6. using ServiceStack;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Globalization;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace MediaBrowser.Api.Playback
  15. {
  16. [Route("/Videos/{Id}/index.bif", "GET")]
  17. public class GetBifFile
  18. {
  19. [ApiMember(Name = "MediaSourceId", Description = "The media version id, if playing an alternate version", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  20. public string MediaSourceId { get; set; }
  21. [ApiMember(Name = "MaxWidth", Description = "Optional. The maximum horizontal resolution of the encoded video.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  22. public int? MaxWidth { get; set; }
  23. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  24. public string Id { get; set; }
  25. }
  26. public class BifService : BaseApiService
  27. {
  28. private readonly IServerApplicationPaths _appPaths;
  29. private readonly ILibraryManager _libraryManager;
  30. private readonly IMediaEncoder _mediaEncoder;
  31. private readonly IFileSystem _fileSystem;
  32. public BifService(IServerApplicationPaths appPaths, ILibraryManager libraryManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem)
  33. {
  34. _appPaths = appPaths;
  35. _libraryManager = libraryManager;
  36. _mediaEncoder = mediaEncoder;
  37. _fileSystem = fileSystem;
  38. }
  39. public object Get(GetBifFile request)
  40. {
  41. return ToStaticFileResult(GetBifFile(request).Result);
  42. }
  43. private async Task<string> GetBifFile(GetBifFile request)
  44. {
  45. var widthVal = request.MaxWidth.HasValue ? request.MaxWidth.Value.ToString(CultureInfo.InvariantCulture) : string.Empty;
  46. var item = _libraryManager.GetItemById(request.Id);
  47. var mediaSources = ((IHasMediaSources)item).GetMediaSources(false).ToList();
  48. var mediaSource = mediaSources.FirstOrDefault(i => string.Equals(i.Id, request.MediaSourceId)) ?? mediaSources.First();
  49. var path = Path.Combine(_appPaths.ImageCachePath, "bif", request.Id, request.MediaSourceId, widthVal, "index.bif");
  50. if (File.Exists(path))
  51. {
  52. return path;
  53. }
  54. var protocol = mediaSource.Protocol;
  55. var inputPath = MediaEncoderHelpers.GetInputArgument(mediaSource.Path, protocol, null, mediaSource.PlayableStreamFileNames);
  56. var semaphore = GetLock(path);
  57. await semaphore.WaitAsync().ConfigureAwait(false);
  58. try
  59. {
  60. if (File.Exists(path))
  61. {
  62. return path;
  63. }
  64. await _mediaEncoder.ExtractVideoImagesOnInterval(inputPath, protocol, mediaSource.Video3DFormat,
  65. TimeSpan.FromSeconds(10), Path.GetDirectoryName(path), "img_", request.MaxWidth, CancellationToken.None)
  66. .ConfigureAwait(false);
  67. var images = new DirectoryInfo(Path.GetDirectoryName(path))
  68. .EnumerateFiles()
  69. .Where(img => string.Equals(img.Extension, ".jpg", StringComparison.Ordinal))
  70. .OrderBy(i => i.FullName)
  71. .ToList();
  72. using (var fs = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, true))
  73. {
  74. var magicNumber = new byte[] { 0x89, 0x42, 0x49, 0x46, 0x0d, 0x0a, 0x1a, 0x0a };
  75. await fs.WriteAsync(magicNumber, 0, magicNumber.Length);
  76. // version
  77. var bytes = GetBytes(0);
  78. await fs.WriteAsync(bytes, 0, bytes.Length);
  79. // image count
  80. bytes = GetBytes(images.Count);
  81. await fs.WriteAsync(bytes, 0, bytes.Length);
  82. // interval in ms
  83. bytes = GetBytes(10000);
  84. await fs.WriteAsync(bytes, 0, bytes.Length);
  85. // reserved
  86. for (var i = 20; i <= 63; i++)
  87. {
  88. bytes = new byte[] { 0x00 };
  89. await fs.WriteAsync(bytes, 0, bytes.Length);
  90. }
  91. // write the bif index
  92. var index = 0;
  93. long imageOffset = 64 + (8 * images.Count) + 8;
  94. foreach (var img in images)
  95. {
  96. bytes = GetBytes(index);
  97. await fs.WriteAsync(bytes, 0, bytes.Length);
  98. bytes = GetBytes(imageOffset);
  99. await fs.WriteAsync(bytes, 0, bytes.Length);
  100. imageOffset += img.Length;
  101. index++;
  102. }
  103. bytes = new byte[] { 0xff, 0xff, 0xff, 0xff };
  104. await fs.WriteAsync(bytes, 0, bytes.Length);
  105. bytes = GetBytes(imageOffset);
  106. await fs.WriteAsync(bytes, 0, bytes.Length);
  107. // write the images
  108. foreach (var img in images)
  109. {
  110. using (var imgStream = _fileSystem.GetFileStream(img.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  111. {
  112. await imgStream.CopyToAsync(fs).ConfigureAwait(false);
  113. }
  114. }
  115. }
  116. return path;
  117. }
  118. finally
  119. {
  120. semaphore.Release();
  121. }
  122. }
  123. private byte[] GetBytes(int value)
  124. {
  125. byte[] bytes = BitConverter.GetBytes(value);
  126. if (BitConverter.IsLittleEndian)
  127. Array.Reverse(bytes);
  128. return bytes;
  129. }
  130. private byte[] GetBytes(long value)
  131. {
  132. var intVal = Convert.ToInt32(value);
  133. return GetBytes(intVal);
  134. //byte[] bytes = BitConverter.GetBytes(value);
  135. //if (BitConverter.IsLittleEndian)
  136. // Array.Reverse(bytes);
  137. //return bytes;
  138. }
  139. private static readonly ConcurrentDictionary<string, SemaphoreSlim> SemaphoreLocks = new ConcurrentDictionary<string, SemaphoreSlim>();
  140. /// <summary>
  141. /// Gets the lock.
  142. /// </summary>
  143. /// <param name="filename">The filename.</param>
  144. /// <returns>System.Object.</returns>
  145. private static SemaphoreSlim GetLock(string filename)
  146. {
  147. return SemaphoreLocks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
  148. }
  149. }
  150. }