diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2014-08-29 08:14:41 -0400 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2014-08-29 08:14:41 -0400 |
| commit | 6a9dbf6ae85b4e7abcf06f7f29ef9d8b0b890876 (patch) | |
| tree | 562525930bef52e56ec03295f3b3ed401e8b34aa /MediaBrowser.Api | |
| parent | 7dc9c2b77f7bde8f1f383d8cfc98b06e2487ad4d (diff) | |
update translations
Diffstat (limited to 'MediaBrowser.Api')
| -rw-r--r-- | MediaBrowser.Api/MediaBrowser.Api.csproj | 1 | ||||
| -rw-r--r-- | MediaBrowser.Api/Playback/BaseStreamingService.cs | 29 | ||||
| -rw-r--r-- | MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs | 12 | ||||
| -rw-r--r-- | MediaBrowser.Api/Playback/Hls/VideoHlsService.cs | 8 | ||||
| -rw-r--r-- | MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs | 4 | ||||
| -rw-r--r-- | MediaBrowser.Api/Playback/StreamState.cs | 2 | ||||
| -rw-r--r-- | MediaBrowser.Api/Playback/ThrottledStream.cs | 339 |
7 files changed, 37 insertions, 358 deletions
diff --git a/MediaBrowser.Api/MediaBrowser.Api.csproj b/MediaBrowser.Api/MediaBrowser.Api.csproj index 30077c5472..5cb9ebb1b0 100644 --- a/MediaBrowser.Api/MediaBrowser.Api.csproj +++ b/MediaBrowser.Api/MediaBrowser.Api.csproj @@ -73,7 +73,6 @@ <Compile Include="Dlna\DlnaServerService.cs" /> <Compile Include="Dlna\DlnaService.cs" /> <Compile Include="Library\ChapterService.cs" /> - <Compile Include="Playback\ThrottledStream.cs" /> <Compile Include="PlaylistService.cs" /> <Compile Include="Subtitles\SubtitleService.cs" /> <Compile Include="Movies\CollectionService.cs" /> diff --git a/MediaBrowser.Api/Playback/BaseStreamingService.cs b/MediaBrowser.Api/Playback/BaseStreamingService.cs index b965bf6f14..a0a8ee61ea 100644 --- a/MediaBrowser.Api/Playback/BaseStreamingService.cs +++ b/MediaBrowser.Api/Playback/BaseStreamingService.cs @@ -353,11 +353,9 @@ namespace MediaBrowser.Api.Playback { case EncodingQuality.HighSpeed: crf = "12"; - profileScore = 2; break; case EncodingQuality.HighQuality: crf = "8"; - profileScore = 1; break; case EncodingQuality.MaxQuality: crf = "4"; @@ -369,10 +367,11 @@ namespace MediaBrowser.Api.Playback if (isVc1) { profileScore++; - // Max of 2 - profileScore = Math.Min(profileScore, 2); } + // Max of 2 + profileScore = Math.Min(profileScore, 2); + // http://www.webmproject.org/docs/encoder-parameters/ param = string.Format("-speed 16 -quality good -profile:v {0} -slices 8 -crf {1}", profileScore.ToString(UsCulture), @@ -771,13 +770,31 @@ namespace MediaBrowser.Api.Playback return "copy"; } + protected virtual bool SupportsThrottling + { + get { return false; } + } + /// <summary> /// Gets the input argument. /// </summary> /// <param name="state">The state.</param> /// <returns>System.String.</returns> - protected virtual string GetInputArgument(StreamState state) + protected string GetInputArgument(StreamState state) { + if (state.InputProtocol == MediaProtocol.File && + state.RunTimeTicks.HasValue && + state.VideoType == VideoType.VideoFile && + !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) + { + if (state.RunTimeTicks.Value >= TimeSpan.FromMinutes(5).Ticks && state.IsInputVideo) + { + var url = "http://localhost:8096/mediabrowser/videos/" + state.Request.Id + "/stream?static=true&Throttle=true&mediaSourceId=" + state.Request.MediaSourceId; + + return string.Format("\"{0}\"", url); + } + } + var protocol = state.InputProtocol; var inputPath = new[] { state.MediaPath }; @@ -1494,6 +1511,7 @@ namespace MediaBrowser.Api.Playback state.MediaPath = mediaSource.Path; state.RunTimeTicks = item.RunTimeTicks; state.RemoteHttpHeaders = mediaSource.RequiredHttpHeaders; + state.InputBitrate = mediaSource.Bitrate; mediaStreams = mediaSource.MediaStreams; } else @@ -1508,6 +1526,7 @@ namespace MediaBrowser.Api.Playback state.MediaPath = mediaSource.Path; state.InputProtocol = mediaSource.Protocol; state.InputContainer = mediaSource.Container; + state.InputBitrate = mediaSource.Bitrate; if (item is Video) { diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs index 9dd99c6ed0..134c28524b 100644 --- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs @@ -670,17 +670,5 @@ 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/Hls/VideoHlsService.cs b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs index d4aa0836ff..0d90e37395 100644 --- a/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs @@ -79,6 +79,14 @@ namespace MediaBrowser.Api.Playback.Hls return ResultFactory.GetStaticFileResult(Request, file); } + protected override bool SupportsThrottling + { + get + { + return false; + } + } + /// <summary> /// Called when [begin request]. /// </summary> diff --git a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs index 028ada37b9..d8255bd290 100644 --- a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs +++ b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs @@ -151,7 +151,9 @@ namespace MediaBrowser.Api.Playback.Progressive using (state) { - return ResultFactory.GetStaticFileResult(Request, state.MediaPath, contentType, null, FileShare.Read, responseHeaders, isHeadRequest); + var throttleLimit = state.InputBitrate.HasValue ? (state.InputBitrate.Value / 8) : 0; + + return ResultFactory.GetStaticFileResult(Request, state.MediaPath, contentType, null, FileShare.Read, responseHeaders, isHeadRequest, request.Throttle, throttleLimit); } } diff --git a/MediaBrowser.Api/Playback/StreamState.cs b/MediaBrowser.Api/Playback/StreamState.cs index 1d3ff939af..139a78c800 100644 --- a/MediaBrowser.Api/Playback/StreamState.cs +++ b/MediaBrowser.Api/Playback/StreamState.cs @@ -68,6 +68,8 @@ namespace MediaBrowser.Api.Playback public long? RunTimeTicks; + public long? InputBitrate { get; set; } + public string OutputAudioSync = "1"; public string OutputVideoSync = "vfr"; diff --git a/MediaBrowser.Api/Playback/ThrottledStream.cs b/MediaBrowser.Api/Playback/ThrottledStream.cs deleted file mode 100644 index 18421a3ccd..0000000000 --- a/MediaBrowser.Api/Playback/ThrottledStream.cs +++ /dev/null @@ -1,339 +0,0 @@ -using System; -using System.IO; -using System.Threading; - -namespace MediaBrowser.Api.Playback -{ - /// <summary> - /// Class for streaming data with throttling support. - /// </summary> - public class ThrottledStream : Stream - { - /// <summary> - /// A constant used to specify an infinite number of bytes that can be transferred per second. - /// </summary> - public const long Infinite = 0; - - #region Private members - /// <summary> - /// The base stream. - /// </summary> - private Stream _baseStream; - - /// <summary> - /// The maximum bytes per second that can be transferred through the base stream. - /// </summary> - private long _maximumBytesPerSecond; - - /// <summary> - /// The number of bytes that has been transferred since the last throttle. - /// </summary> - private long _byteCount; - - /// <summary> - /// The start time in milliseconds of the last throttle. - /// </summary> - private long _start; - #endregion - - #region Properties - /// <summary> - /// Gets the current milliseconds. - /// </summary> - /// <value>The current milliseconds.</value> - protected long CurrentMilliseconds - { - get - { - return Environment.TickCount; - } - } - - /// <summary> - /// Gets or sets the maximum bytes per second that can be transferred through the base stream. - /// </summary> - /// <value>The maximum bytes per second.</value> - public long MaximumBytesPerSecond - { - get - { - return _maximumBytesPerSecond; - } - set - { - if (MaximumBytesPerSecond != value) - { - _maximumBytesPerSecond = value; - Reset(); - } - } - } - - /// <summary> - /// Gets a value indicating whether the current stream supports reading. - /// </summary> - /// <returns>true if the stream supports reading; otherwise, false.</returns> - public override bool CanRead - { - get - { - return _baseStream.CanRead; - } - } - - /// <summary> - /// Gets a value indicating whether the current stream supports seeking. - /// </summary> - /// <value></value> - /// <returns>true if the stream supports seeking; otherwise, false.</returns> - public override bool CanSeek - { - get - { - return _baseStream.CanSeek; - } - } - - /// <summary> - /// Gets a value indicating whether the current stream supports writing. - /// </summary> - /// <value></value> - /// <returns>true if the stream supports writing; otherwise, false.</returns> - public override bool CanWrite - { - get - { - return _baseStream.CanWrite; - } - } - - /// <summary> - /// Gets the length in bytes of the stream. - /// </summary> - /// <value></value> - /// <returns>A long value representing the length of the stream in bytes.</returns> - /// <exception cref="T:System.NotSupportedException">The base stream does not support seeking. </exception> - /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception> - public override long Length - { - get - { - return _baseStream.Length; - } - } - - /// <summary> - /// Gets or sets the position within the current stream. - /// </summary> - /// <value></value> - /// <returns>The current position within the stream.</returns> - /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception> - /// <exception cref="T:System.NotSupportedException">The base stream does not support seeking. </exception> - /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception> - public override long Position - { - get - { - return _baseStream.Position; - } - set - { - _baseStream.Position = value; - } - } - #endregion - - #region Ctor - /// <summary> - /// Initializes a new instance of the <see cref="T:ThrottledStream"/> class with an - /// infinite amount of bytes that can be processed. - /// </summary> - /// <param name="baseStream">The base stream.</param> - public ThrottledStream(Stream baseStream) - : this(baseStream, ThrottledStream.Infinite) - { - // Nothing todo. - } - - /// <summary> - /// Initializes a new instance of the <see cref="T:ThrottledStream"/> class. - /// </summary> - /// <param name="baseStream">The base stream.</param> - /// <param name="maximumBytesPerSecond">The maximum bytes per second that can be transferred through the base stream.</param> - /// <exception cref="ArgumentNullException">Thrown when <see cref="baseStream"/> is a null reference.</exception> - /// <exception cref="ArgumentOutOfRangeException">Thrown when <see cref="maximumBytesPerSecond"/> is a negative value.</exception> - 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 - /// <summary> - /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device. - /// </summary> - /// <exception cref="T:System.IO.IOException">An I/O error occurs.</exception> - public override void Flush() - { - _baseStream.Flush(); - } - - /// <summary> - /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. - /// </summary> - /// <param name="buffer">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.</param> - /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param> - /// <param name="count">The maximum number of bytes to be read from the current stream.</param> - /// <returns> - /// 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. - /// </returns> - /// <exception cref="T:System.ArgumentException">The sum of offset and count is larger than the buffer length. </exception> - /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception> - /// <exception cref="T:System.NotSupportedException">The base stream does not support reading. </exception> - /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception> - /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception> - /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception> - public override int Read(byte[] buffer, int offset, int count) - { - Throttle(count); - - return _baseStream.Read(buffer, offset, count); - } - - /// <summary> - /// Sets the position within the current stream. - /// </summary> - /// <param name="offset">A byte offset relative to the origin parameter.</param> - /// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"></see> indicating the reference point used to obtain the new position.</param> - /// <returns> - /// The new position within the current stream. - /// </returns> - /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception> - /// <exception cref="T:System.NotSupportedException">The base stream does not support seeking, such as if the stream is constructed from a pipe or console output. </exception> - /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception> - public override long Seek(long offset, SeekOrigin origin) - { - return _baseStream.Seek(offset, origin); - } - - /// <summary> - /// Sets the length of the current stream. - /// </summary> - /// <param name="value">The desired length of the current stream in bytes.</param> - /// <exception cref="T:System.NotSupportedException">The base stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. </exception> - /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception> - /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception> - public override void SetLength(long value) - { - _baseStream.SetLength(value); - } - - /// <summary> - /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. - /// </summary> - /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param> - /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param> - /// <param name="count">The number of bytes to be written to the current stream.</param> - /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception> - /// <exception cref="T:System.NotSupportedException">The base stream does not support writing. </exception> - /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception> - /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception> - /// <exception cref="T:System.ArgumentException">The sum of offset and count is greater than the buffer length. </exception> - /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception> - public override void Write(byte[] buffer, int offset, int count) - { - Throttle(count); - - _baseStream.Write(buffer, offset, count); - } - - /// <summary> - /// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>. - /// </summary> - /// <returns> - /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>. - /// </returns> - public override string ToString() - { - return _baseStream.ToString(); - } - #endregion - - #region Protected methods - /// <summary> - /// Throttles for the specified buffer size in bytes. - /// </summary> - /// <param name="bufferSizeInBytes">The buffer size in bytes.</param> - 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(); - } - } - } - } - - /// <summary> - /// Will reset the bytecount to 0 and reset the start time to the current time. - /// </summary> - 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 |
