From 18a7ddc2fa4644a10de0d9f72b3154ff4528be89 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 29 Aug 2014 00:06:30 -0400 Subject: [PATCH] add photo album --- MediaBrowser.Api/MediaBrowser.Api.csproj | 1 + .../Playback/Hls/DynamicHlsService.cs | 12 + MediaBrowser.Api/Playback/StreamRequest.cs | 2 + MediaBrowser.Api/Playback/ThrottledStream.cs | 339 ++++++++++++++++++ MediaBrowser.Api/UserLibrary/ItemsService.cs | 2 +- .../Entities/Audio/Audio.cs | 1 + MediaBrowser.Controller/Entities/Photo.cs | 27 +- .../Entities/PhotoAlbum.cs | 21 ++ .../Entities/TV/Episode.cs | 1 + .../MediaBrowser.Controller.csproj | 1 + .../HttpServer/RangeRequestWriter.cs | 4 +- .../Library/Resolvers/PhotoAlbumResolver.cs | 39 ++ .../Library/Resolvers/PhotoResolver.cs | 26 +- ...MediaBrowser.Server.Implementations.csproj | 1 + Nuget/MediaBrowser.Common.Internal.nuspec | 4 +- Nuget/MediaBrowser.Common.nuspec | 2 +- Nuget/MediaBrowser.Model.Signed.nuspec | 2 +- Nuget/MediaBrowser.Server.Core.nuspec | 4 +- 18 files changed, 462 insertions(+), 27 deletions(-) create mode 100644 MediaBrowser.Api/Playback/ThrottledStream.cs create mode 100644 MediaBrowser.Controller/Entities/PhotoAlbum.cs create mode 100644 MediaBrowser.Server.Implementations/Library/Resolvers/PhotoAlbumResolver.cs diff --git a/MediaBrowser.Api/MediaBrowser.Api.csproj b/MediaBrowser.Api/MediaBrowser.Api.csproj index 5cb9ebb1b0..30077c5472 100644 --- a/MediaBrowser.Api/MediaBrowser.Api.csproj +++ b/MediaBrowser.Api/MediaBrowser.Api.csproj @@ -73,6 +73,7 @@ + diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs index 134c28524b..9dd99c6ed0 100644 --- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs @@ -670,5 +670,17 @@ namespace MediaBrowser.Api.Playback.Hls return TranscodingJobType.Hls; } } + + protected override string GetInputArgument(StreamState state) + { + if (state.InputProtocol == Model.MediaInfo.MediaProtocol.File && + state.RunTimeTicks.HasValue && + !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) + { + return "http://localhost:8096/videos/" + state.Request.Id + "/stream?static=true&Throttle=true&mediaSourceId=" + state.Request.MediaSourceId; + } + + return base.GetInputArgument(state); + } } } diff --git a/MediaBrowser.Api/Playback/StreamRequest.cs b/MediaBrowser.Api/Playback/StreamRequest.cs index c72ead949d..0de8c28a9a 100644 --- a/MediaBrowser.Api/Playback/StreamRequest.cs +++ b/MediaBrowser.Api/Playback/StreamRequest.cs @@ -70,6 +70,8 @@ namespace MediaBrowser.Api.Playback public string DeviceProfileId { get; set; } public string Params { get; set; } + + public bool Throttle { get; set; } } public class VideoStreamRequest : StreamRequest diff --git a/MediaBrowser.Api/Playback/ThrottledStream.cs b/MediaBrowser.Api/Playback/ThrottledStream.cs new file mode 100644 index 0000000000..18421a3ccd --- /dev/null +++ b/MediaBrowser.Api/Playback/ThrottledStream.cs @@ -0,0 +1,339 @@ +using System; +using System.IO; +using System.Threading; + +namespace MediaBrowser.Api.Playback +{ + /// + /// Class for streaming data with throttling support. + /// + public class ThrottledStream : Stream + { + /// + /// A constant used to specify an infinite number of bytes that can be transferred per second. + /// + public const long Infinite = 0; + + #region Private members + /// + /// The base stream. + /// + private Stream _baseStream; + + /// + /// The maximum bytes per second that can be transferred through the base stream. + /// + private long _maximumBytesPerSecond; + + /// + /// The number of bytes that has been transferred since the last throttle. + /// + private long _byteCount; + + /// + /// The start time in milliseconds of the last throttle. + /// + private long _start; + #endregion + + #region Properties + /// + /// Gets the current milliseconds. + /// + /// The current milliseconds. + protected long CurrentMilliseconds + { + get + { + return Environment.TickCount; + } + } + + /// + /// Gets or sets the maximum bytes per second that can be transferred through the base stream. + /// + /// The maximum bytes per second. + public long MaximumBytesPerSecond + { + get + { + return _maximumBytesPerSecond; + } + set + { + if (MaximumBytesPerSecond != value) + { + _maximumBytesPerSecond = value; + Reset(); + } + } + } + + /// + /// Gets a value indicating whether the current stream supports reading. + /// + /// true if the stream supports reading; otherwise, false. + public override bool CanRead + { + get + { + return _baseStream.CanRead; + } + } + + /// + /// Gets a value indicating whether the current stream supports seeking. + /// + /// + /// true if the stream supports seeking; otherwise, false. + public override bool CanSeek + { + get + { + return _baseStream.CanSeek; + } + } + + /// + /// Gets a value indicating whether the current stream supports writing. + /// + /// + /// true if the stream supports writing; otherwise, false. + public override bool CanWrite + { + get + { + return _baseStream.CanWrite; + } + } + + /// + /// Gets the length in bytes of the stream. + /// + /// + /// A long value representing the length of the stream in bytes. + /// The base stream does not support seeking. + /// Methods were called after the stream was closed. + public override long Length + { + get + { + return _baseStream.Length; + } + } + + /// + /// Gets or sets the position within the current stream. + /// + /// + /// The current position within the stream. + /// An I/O error occurs. + /// The base stream does not support seeking. + /// Methods were called after the stream was closed. + public override long Position + { + get + { + return _baseStream.Position; + } + set + { + _baseStream.Position = value; + } + } + #endregion + + #region Ctor + /// + /// Initializes a new instance of the class with an + /// infinite amount of bytes that can be processed. + /// + /// The base stream. + public ThrottledStream(Stream baseStream) + : this(baseStream, ThrottledStream.Infinite) + { + // Nothing todo. + } + + /// + /// Initializes a new instance of the class. + /// + /// The base stream. + /// The maximum bytes per second that can be transferred through the base stream. + /// Thrown when is a null reference. + /// Thrown when is a negative value. + public ThrottledStream(Stream baseStream, long maximumBytesPerSecond) + { + if (baseStream == null) + { + throw new ArgumentNullException("baseStream"); + } + + if (maximumBytesPerSecond < 0) + { + throw new ArgumentOutOfRangeException("maximumBytesPerSecond", + maximumBytesPerSecond, "The maximum number of bytes per second can't be negatie."); + } + + _baseStream = baseStream; + _maximumBytesPerSecond = maximumBytesPerSecond; + _start = CurrentMilliseconds; + _byteCount = 0; + } + #endregion + + #region Public methods + /// + /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device. + /// + /// An I/O error occurs. + public override void Flush() + { + _baseStream.Flush(); + } + + /// + /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. + /// + /// An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source. + /// The zero-based byte offset in buffer at which to begin storing the data read from the current stream. + /// The maximum number of bytes to be read from the current stream. + /// + /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached. + /// + /// The sum of offset and count is larger than the buffer length. + /// Methods were called after the stream was closed. + /// The base stream does not support reading. + /// buffer is null. + /// An I/O error occurs. + /// offset or count is negative. + public override int Read(byte[] buffer, int offset, int count) + { + Throttle(count); + + return _baseStream.Read(buffer, offset, count); + } + + /// + /// Sets the position within the current stream. + /// + /// A byte offset relative to the origin parameter. + /// A value of type indicating the reference point used to obtain the new position. + /// + /// The new position within the current stream. + /// + /// An I/O error occurs. + /// The base stream does not support seeking, such as if the stream is constructed from a pipe or console output. + /// Methods were called after the stream was closed. + public override long Seek(long offset, SeekOrigin origin) + { + return _baseStream.Seek(offset, origin); + } + + /// + /// Sets the length of the current stream. + /// + /// The desired length of the current stream in bytes. + /// The base stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. + /// An I/O error occurs. + /// Methods were called after the stream was closed. + public override void SetLength(long value) + { + _baseStream.SetLength(value); + } + + /// + /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. + /// + /// An array of bytes. This method copies count bytes from buffer to the current stream. + /// The zero-based byte offset in buffer at which to begin copying bytes to the current stream. + /// The number of bytes to be written to the current stream. + /// An I/O error occurs. + /// The base stream does not support writing. + /// Methods were called after the stream was closed. + /// buffer is null. + /// The sum of offset and count is greater than the buffer length. + /// offset or count is negative. + public override void Write(byte[] buffer, int offset, int count) + { + Throttle(count); + + _baseStream.Write(buffer, offset, count); + } + + /// + /// Returns a that represents the current . + /// + /// + /// A that represents the current . + /// + public override string ToString() + { + return _baseStream.ToString(); + } + #endregion + + #region Protected methods + /// + /// Throttles for the specified buffer size in bytes. + /// + /// The buffer size in bytes. + protected void Throttle(int bufferSizeInBytes) + { + // Make sure the buffer isn't empty. + if (_maximumBytesPerSecond <= 0 || bufferSizeInBytes <= 0) + { + return; + } + + _byteCount += bufferSizeInBytes; + long elapsedMilliseconds = CurrentMilliseconds - _start; + + if (elapsedMilliseconds > 0) + { + // Calculate the current bps. + long bps = _byteCount * 1000L / elapsedMilliseconds; + + // If the bps are more then the maximum bps, try to throttle. + if (bps > _maximumBytesPerSecond) + { + // Calculate the time to sleep. + long wakeElapsed = _byteCount * 1000L / _maximumBytesPerSecond; + int toSleep = (int)(wakeElapsed - elapsedMilliseconds); + + if (toSleep > 1) + { + try + { + // The time to sleep is more then a millisecond, so sleep. + Thread.Sleep(toSleep); + } + catch (ThreadAbortException) + { + // Eatup ThreadAbortException. + } + + // A sleep has been done, reset. + Reset(); + } + } + } + } + + /// + /// Will reset the bytecount to 0 and reset the start time to the current time. + /// + protected void Reset() + { + long difference = CurrentMilliseconds - _start; + + // Only reset counters when a known history is available of more then 1 second. + if (difference > 1000) + { + _byteCount = 0; + _start = CurrentMilliseconds; + } + } + #endregion + } +} \ No newline at end of file diff --git a/MediaBrowser.Api/UserLibrary/ItemsService.cs b/MediaBrowser.Api/UserLibrary/ItemsService.cs index ba07571bf2..25821c2132 100644 --- a/MediaBrowser.Api/UserLibrary/ItemsService.cs +++ b/MediaBrowser.Api/UserLibrary/ItemsService.cs @@ -1430,7 +1430,7 @@ namespace MediaBrowser.Api.UserLibrary nextId = list[index + 1].Id; } - return list.Where(i => i.Id == previousId || i.Id == nextId); + return list.Where(i => i.Id == previousId || i.Id == nextId || i.Id == adjacentToIdGuid); } /// diff --git a/MediaBrowser.Controller/Entities/Audio/Audio.cs b/MediaBrowser.Controller/Entities/Audio/Audio.cs index 3ffdf744d2..b13403bbf8 100644 --- a/MediaBrowser.Controller/Entities/Audio/Audio.cs +++ b/MediaBrowser.Controller/Entities/Audio/Audio.cs @@ -77,6 +77,7 @@ namespace MediaBrowser.Controller.Entities.Audio } } + [IgnoreDataMember] public override Folder LatestItemsIndexContainer { get diff --git a/MediaBrowser.Controller/Entities/Photo.cs b/MediaBrowser.Controller/Entities/Photo.cs index aa9e63791e..1f38de9a31 100644 --- a/MediaBrowser.Controller/Entities/Photo.cs +++ b/MediaBrowser.Controller/Entities/Photo.cs @@ -1,5 +1,8 @@ -using MediaBrowser.Model.Drawing; +using MediaBrowser.Model.Configuration; +using MediaBrowser.Model.Drawing; using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; namespace MediaBrowser.Controller.Entities { @@ -14,6 +17,14 @@ namespace MediaBrowser.Controller.Entities Taglines = new List(); } + public override bool SupportsLocalMetadata + { + get + { + return false; + } + } + public override string MediaType { get @@ -22,6 +33,15 @@ namespace MediaBrowser.Controller.Entities } } + [IgnoreDataMember] + public override Folder LatestItemsIndexContainer + { + get + { + return Parents.OfType().FirstOrDefault(); + } + } + public int? Width { get; set; } public int? Height { get; set; } public string CameraMake { get; set; } @@ -32,5 +52,10 @@ namespace MediaBrowser.Controller.Entities public ImageOrientation? Orientation { get; set; } public double? Aperture { get; set; } public double? ShutterSpeed { get; set; } + + protected override bool GetBlockUnratedValue(UserConfiguration config) + { + return config.BlockUnratedItems.Contains(UnratedItem.Other); + } } } diff --git a/MediaBrowser.Controller/Entities/PhotoAlbum.cs b/MediaBrowser.Controller/Entities/PhotoAlbum.cs new file mode 100644 index 0000000000..7af4109f32 --- /dev/null +++ b/MediaBrowser.Controller/Entities/PhotoAlbum.cs @@ -0,0 +1,21 @@ +using MediaBrowser.Model.Configuration; +using System.Linq; + +namespace MediaBrowser.Controller.Entities +{ + public class PhotoAlbum : Folder + { + public override bool SupportsLocalMetadata + { + get + { + return false; + } + } + + protected override bool GetBlockUnratedValue(UserConfiguration config) + { + return config.BlockUnratedItems.Contains(UnratedItem.Other); + } + } +} diff --git a/MediaBrowser.Controller/Entities/TV/Episode.cs b/MediaBrowser.Controller/Entities/TV/Episode.cs index 70577bbfde..b95c7df9c5 100644 --- a/MediaBrowser.Controller/Entities/TV/Episode.cs +++ b/MediaBrowser.Controller/Entities/TV/Episode.cs @@ -95,6 +95,7 @@ namespace MediaBrowser.Controller.Entities.TV } } + [IgnoreDataMember] public override Folder LatestItemsIndexContainer { get diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index 561ae93261..aeeaae073b 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -156,6 +156,7 @@ + diff --git a/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs b/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs index 9de6972f86..2e2f5e9f87 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs @@ -1,10 +1,10 @@ -using System.Threading; -using ServiceStack.Web; +using ServiceStack.Web; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Net; +using System.Threading; using System.Threading.Tasks; namespace MediaBrowser.Server.Implementations.HttpServer diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/PhotoAlbumResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/PhotoAlbumResolver.cs new file mode 100644 index 0000000000..2fcfd70868 --- /dev/null +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/PhotoAlbumResolver.cs @@ -0,0 +1,39 @@ +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Entities; +using System; +using System.IO; +using System.Linq; + +namespace MediaBrowser.Server.Implementations.Library.Resolvers +{ + public class PhotoAlbumResolver : FolderResolver + { + /// + /// Resolves the specified args. + /// + /// The args. + /// Trailer. + protected override PhotoAlbum Resolve(ItemResolveArgs args) + { + // Must be an image file within a photo collection + if (!args.IsRoot && args.IsDirectory && string.Equals(args.GetCollectionType(), CollectionType.Photos, StringComparison.OrdinalIgnoreCase)) + { + if (HasPhotos(args)) + { + return new PhotoAlbum + { + Path = args.Path + }; + } + } + + return null; + } + + private static bool HasPhotos(ItemResolveArgs args) + { + return args.FileSystemChildren.Any(i => ((i.Attributes & FileAttributes.Directory) != FileAttributes.Directory) && PhotoResolver.IsImageFile(i.FullName)); + } + } +} diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/PhotoResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/PhotoResolver.cs index cba7aba9af..60e7edfdd4 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/PhotoResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/PhotoResolver.cs @@ -1,24 +1,14 @@ -using MediaBrowser.Controller; -using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Entities; using System; +using System.IO; using System.Linq; namespace MediaBrowser.Server.Implementations.Library.Resolvers { public class PhotoResolver : ItemResolver { - private readonly IServerApplicationPaths _applicationPaths; - - /// - /// Initializes a new instance of the class. - /// - /// The application paths. - public PhotoResolver(IServerApplicationPaths applicationPaths) - { - _applicationPaths = applicationPaths; - } - /// /// Resolves the specified args. /// @@ -27,7 +17,7 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers protected override Photo Resolve(ItemResolveArgs args) { // Must be an image file within a photo collection - if (!args.IsDirectory && IsImageFile(args.Path) && string.Equals(args.GetCollectionType(), "photos", StringComparison.OrdinalIgnoreCase)) + if (!args.IsDirectory && IsImageFile(args.Path) && string.Equals(args.GetCollectionType(), CollectionType.Photos, StringComparison.OrdinalIgnoreCase)) { return new Photo { @@ -39,10 +29,12 @@ namespace MediaBrowser.Server.Implementations.Library.Resolvers } protected static string[] ImageExtensions = { ".tiff", ".jpeg", ".jpg", ".png", ".aiff" }; - protected bool IsImageFile(string path) + internal static bool IsImageFile(string path) { - return !path.EndsWith("folder.jpg", StringComparison.OrdinalIgnoreCase) - && ImageExtensions.Any(p => path.EndsWith(p, StringComparison.OrdinalIgnoreCase)); + var filename = Path.GetFileName(path); + + return !string.Equals(filename, "folder.jpg", StringComparison.OrdinalIgnoreCase) + && ImageExtensions.Contains(Path.GetExtension(path) ?? string.Empty, StringComparer.OrdinalIgnoreCase); } } diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj index e8dd54f16e..4668d78913 100644 --- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj +++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj @@ -174,6 +174,7 @@ + diff --git a/Nuget/MediaBrowser.Common.Internal.nuspec b/Nuget/MediaBrowser.Common.Internal.nuspec index 935d267503..11ccc3075f 100644 --- a/Nuget/MediaBrowser.Common.Internal.nuspec +++ b/Nuget/MediaBrowser.Common.Internal.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Common.Internal - 3.0.428 + 3.0.429 MediaBrowser.Common.Internal Luke ebr,Luke,scottisafool @@ -12,7 +12,7 @@ Contains common components shared by Media Browser Theater and Media Browser Server. Not intended for plugin developer consumption. Copyright © Media Browser 2013 - + diff --git a/Nuget/MediaBrowser.Common.nuspec b/Nuget/MediaBrowser.Common.nuspec index 4c3626c8db..b912745934 100644 --- a/Nuget/MediaBrowser.Common.nuspec +++ b/Nuget/MediaBrowser.Common.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Common - 3.0.428 + 3.0.429 MediaBrowser.Common Media Browser Team ebr,Luke,scottisafool diff --git a/Nuget/MediaBrowser.Model.Signed.nuspec b/Nuget/MediaBrowser.Model.Signed.nuspec index 67127ea772..9280656b1b 100644 --- a/Nuget/MediaBrowser.Model.Signed.nuspec +++ b/Nuget/MediaBrowser.Model.Signed.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Model.Signed - 3.0.428 + 3.0.429 MediaBrowser.Model - Signed Edition Media Browser Team ebr,Luke,scottisafool diff --git a/Nuget/MediaBrowser.Server.Core.nuspec b/Nuget/MediaBrowser.Server.Core.nuspec index 23743e2d09..a44bc2eab4 100644 --- a/Nuget/MediaBrowser.Server.Core.nuspec +++ b/Nuget/MediaBrowser.Server.Core.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Server.Core - 3.0.428 + 3.0.429 Media Browser.Server.Core Media Browser Team ebr,Luke,scottisafool @@ -12,7 +12,7 @@ Contains core components required to build plugins for Media Browser Server. Copyright © Media Browser 2013 - +