Skip to content

Commit

Permalink
Clean up imports and variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevinjil committed Jun 20, 2022
1 parent 9801dde commit 19b9dfe
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 49 deletions.
1 change: 0 additions & 1 deletion Jellyfin.Xtream/LiveTvService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.MediaInfo;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
Expand Down
6 changes: 3 additions & 3 deletions Jellyfin.Xtream/Service/Restream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ public class Restream : ILiveStream, IDisposable
private readonly IHttpClientFactory httpClientFactory;
private readonly ILogger logger;
private readonly CancellationTokenSource tokenSource;
private readonly bool enableStreamSharing;
private readonly string uniqueId;
private readonly string uri;

private Task? copyTask;
private Stream? inputStream;

private int consumerCount;
private string originalStreamId;
private bool enableStreamSharing;
private MediaSourceInfo mediaSource;
private string uniqueId;
private string uri;

/// <summary>
/// Initializes a new instance of the <see cref="Restream"/> class.
Expand Down
38 changes: 12 additions & 26 deletions Jellyfin.Xtream/Service/WrappedBufferReadStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Xtream.Client.Models;
using Jellyfin.Xtream.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.MediaInfo;
using Microsoft.Extensions.Logging;

namespace Jellyfin.Xtream.Service
{
Expand All @@ -37,7 +23,7 @@ namespace Jellyfin.Xtream.Service
/// </summary>
public class WrappedBufferReadStream : Stream
{
private readonly WrappedBufferStream buffer;
private readonly WrappedBufferStream sourceBuffer;

private long position;
private long readHead;
Expand All @@ -46,13 +32,13 @@ public class WrappedBufferReadStream : Stream
/// <summary>
/// Initializes a new instance of the <see cref="WrappedBufferReadStream"/> class.
/// </summary>
/// <param name="buffer">The source buffer to read from.</param>
public WrappedBufferReadStream(WrappedBufferStream buffer)
/// <param name="sourceBuffer">The source buffer to read from.</param>
public WrappedBufferReadStream(WrappedBufferStream sourceBuffer)
{
this.buffer = buffer;
this.readHead = buffer.TotalBytesWritten;
this.sourceBuffer = sourceBuffer;
this.readHead = sourceBuffer.TotalBytesWritten;
this.totalBytesRead = 0;
this.Position = buffer.Position;
this.position = sourceBuffer.Position;
}

/// <summary>
Expand Down Expand Up @@ -85,8 +71,8 @@ public WrappedBufferReadStream(WrappedBufferStream buffer)
/// <inheritdoc />
public override int Read(byte[] buffer, int offset, int count)
{
long gap = this.buffer.TotalBytesWritten - readHead;
if (gap > this.buffer.BufferSize)
long gap = sourceBuffer.TotalBytesWritten - readHead;
if (gap > sourceBuffer.BufferSize)
{
// TODO: design good handling method.
// Options:
Expand All @@ -96,7 +82,7 @@ public override int Read(byte[] buffer, int offset, int count)
}

// The bytes that still need to be copied.
long remaining = Math.Min(count, this.buffer.TotalBytesWritten - readHead);
long remaining = Math.Min(count, sourceBuffer.TotalBytesWritten - readHead);
long remainingOffset = offset;

long read = 0;
Expand All @@ -105,10 +91,10 @@ public override int Read(byte[] buffer, int offset, int count)
while (remaining > 0)
{
// The amount of bytes that we can directly write from the current position without wrapping.
long readable = Math.Min(remaining, this.buffer.BufferSize - Position);
long readable = Math.Min(remaining, sourceBuffer.BufferSize - Position);

// Copy the data.
Array.Copy(this.buffer.Buffer, Position, buffer, remainingOffset, readable);
Array.Copy(sourceBuffer.Buffer, Position, buffer, remainingOffset, readable);
remaining -= readable;
remainingOffset += readable;

Expand All @@ -118,7 +104,7 @@ public override int Read(byte[] buffer, int offset, int count)
totalBytesRead += readable;

// We might have to loop the position.
Position %= this.buffer.BufferSize;
Position %= sourceBuffer.BufferSize;
}

return (int)read;
Expand Down
24 changes: 5 additions & 19 deletions Jellyfin.Xtream/Service/WrappedBufferStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Xtream.Client.Models;
using Jellyfin.Xtream.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.MediaInfo;
using Microsoft.Extensions.Logging;

namespace Jellyfin.Xtream.Service
{
Expand All @@ -37,7 +23,7 @@ namespace Jellyfin.Xtream.Service
/// </summary>
public class WrappedBufferStream : Stream
{
private readonly byte[] buffer;
private readonly byte[] sourceBuffer;

private long position;
private long totalBytesWritten;
Expand All @@ -48,20 +34,20 @@ public class WrappedBufferStream : Stream
/// <param name="bufferSize">Size in bytes of the internal buffer.</param>
public WrappedBufferStream(int bufferSize)
{
this.buffer = new byte[bufferSize];
this.sourceBuffer = new byte[bufferSize];
this.totalBytesWritten = 0;
}

/// <summary>
/// Gets the maximal size in bytes of read/write chunks.
/// </summary>
public int BufferSize { get => buffer.Length; }
public int BufferSize { get => sourceBuffer.Length; }

#pragma warning disable CA1819
/// <summary>
/// Gets the internal buffer.
/// </summary>
public byte[] Buffer { get => buffer; }
public byte[] Buffer { get => sourceBuffer; }
#pragma warning restore CA1819

/// <summary>
Expand Down Expand Up @@ -106,7 +92,7 @@ public override void Write(byte[] buffer, int offset, int count)
long writable = Math.Min(remaining, BufferSize - Position);

// Copy the data.
Array.Copy(buffer, remainingOffset, this.buffer, Position, writable);
Array.Copy(buffer, remainingOffset, sourceBuffer, Position, writable);
remaining -= writable;
remainingOffset += writable;
Position += writable;
Expand Down

0 comments on commit 19b9dfe

Please sign in to comment.