Providing streaming AudFormat data.

AudFormat.GetPCMInputStream now returns data that is streamed, rather than a MemoryStream.
This commit is contained in:
RoosterDragon
2017-07-09 20:21:02 +00:00
committed by Paul Chote
parent 85c948fd8d
commit 9413d9595c
7 changed files with 218 additions and 75 deletions

View File

@@ -161,6 +161,7 @@
<Compile Include="Player.cs" />
<Compile Include="Primitives\MergedStream.cs" />
<Compile Include="Primitives\PlayerDictionary.cs" />
<Compile Include="Primitives\ReadOnlyAdapterStream.cs" />
<Compile Include="Primitives\SegmentStream.cs" />
<Compile Include="Primitives\SpatiallyPartitioned.cs" />
<Compile Include="Primitives\ConcurrentCache.cs" />

View File

@@ -0,0 +1,89 @@
#region Copyright & License Information
/*
* Copyright 2007-2017 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
namespace OpenRA.Primitives
{
/// <summary>
/// Provides a read-only buffering layer so data can be streamed from sources where reading arbitrary amounts of
/// data is difficult.
/// </summary>
public abstract class ReadOnlyAdapterStream : Stream
{
readonly Queue<byte> data = new Queue<byte>(1024);
readonly Stream baseStream;
protected ReadOnlyAdapterStream(Stream stream)
{
if (stream == null)
throw new ArgumentNullException("stream");
if (!stream.CanRead)
throw new ArgumentException("stream must be readable.", "stream");
baseStream = stream;
}
public sealed override bool CanSeek { get { return false; } }
public sealed override bool CanRead { get { return true; } }
public sealed override bool CanWrite { get { return false; } }
public sealed override long Length { get { throw new NotSupportedException(); } }
public sealed override long Position
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}
public sealed override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
public sealed override void SetLength(long value) { throw new NotSupportedException(); }
public sealed override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
public sealed override void Flush() { throw new NotSupportedException(); }
public sealed override int Read(byte[] buffer, int offset, int count)
{
var copied = 0;
ConsumeData(buffer, offset, count, ref copied);
var finished = false;
while (copied < count && !finished)
{
finished = BufferData(baseStream, data);
ConsumeData(buffer, offset, count, ref copied);
}
return copied;
}
/// <summary>
/// Reads data into a buffer, which will be used to satisfy <see cref="Read(byte[], int, int)"/> calls.
/// </summary>
/// <param name="baseStream">The source stream from which bytes should be read.</param>
/// <param name="data">The queue where bytes should be enqueued. Do not dequeue from this buffer.</param>
/// <returns>Return true if all data has been read; otherwise, false.</returns>
protected abstract bool BufferData(Stream baseStream, Queue<byte> data);
void ConsumeData(byte[] buffer, int offset, int count, ref int copied)
{
while (copied < count && data.Count > 0)
buffer[offset + copied++] = data.Dequeue();
}
protected override void Dispose(bool disposing)
{
if (disposing)
baseStream.Dispose();
base.Dispose(disposing);
}
}
}

View File

@@ -21,6 +21,15 @@ namespace OpenRA.Primitives
public readonly long BaseOffset;
public readonly long BaseCount;
/// <summary>
/// Creates a new <see cref="SegmentStream"/> that wraps a subset of the source stream. This takes ownership of
/// the source stream. The <see cref="SegmentStream"/> is dependent on the source and changes its underlying
/// position.
/// </summary>
/// <param name="stream">The source stream, of which only a segment should be exposed. Ownership is transferred
/// to the <see cref="SegmentStream"/>.</param>
/// <param name="offset">The offset at which the segment starts.</param>
/// <param name="count">The length of the segment.</param>
public SegmentStream(Stream stream, long offset, long count)
{
if (stream == null)
@@ -90,7 +99,7 @@ namespace OpenRA.Primitives
base.Dispose(disposing);
}
public static long GetOverallNestedOffset(Stream stream, out Stream overallBaseStream)
static long GetOverallNestedOffset(Stream stream, out Stream overallBaseStream)
{
var offset = 0L;
overallBaseStream = stream;
@@ -99,5 +108,34 @@ namespace OpenRA.Primitives
offset += segmentStream.BaseOffset + GetOverallNestedOffset(segmentStream.BaseStream, out overallBaseStream);
return offset;
}
/// <summary>
/// Creates a new <see cref="Stream"/> that wraps a subset of the source stream without taking ownership of it,
/// allowing it to be reused by the caller. The <see cref="Stream"/> is independent of the source stream and
/// won't affect its position.
/// </summary>
/// <param name="stream">The source stream, of which only a segment should be exposed. Ownership is retained by
/// the caller.</param>
/// <param name="offset">The offset at which the segment starts.</param>
/// <param name="count">The length of the segment.</param>
public static Stream CreateWithoutOwningStream(Stream stream, long offset, int count)
{
Stream parentStream;
var nestedOffset = offset + GetOverallNestedOffset(stream, out parentStream);
// Special case FileStream - instead of creating an in-memory copy,
// just reference the portion of the on-disk file that we need to save memory.
// We use GetType instead of 'is' here since we can't handle any derived classes of FileStream.
if (parentStream.GetType() == typeof(FileStream))
{
var path = ((FileStream)parentStream).Name;
return new SegmentStream(File.OpenRead(path), nestedOffset, count);
}
// For all other streams, create a copy in memory.
// This uses more memory but is the only way in general to ensure the returned streams won't clash.
stream.Seek(offset, SeekOrigin.Begin);
return new MemoryStream(stream.ReadBytes(count));
}
}
}

View File

@@ -12,6 +12,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace OpenRA
@@ -117,7 +118,17 @@ namespace OpenRA
public static byte[] ReadAllBytes(this Stream s)
{
using (s)
return s.ReadBytes((int)(s.Length - s.Position));
{
if (s.CanSeek)
return s.ReadBytes((int)(s.Length - s.Position));
var bytes = new List<byte>();
var buffer = new byte[1024];
int count;
while ((count = s.Read(buffer, 0, buffer.Length)) > 0)
bytes.AddRange(buffer.Take(count));
return bytes.ToArray();
}
}
public static void Write(this Stream s, byte[] data)