Skip to content

Commit

Permalink
gathering changes from various forks and updating version to 7x
Browse files Browse the repository at this point in the history
  • Loading branch information
mariodivece committed May 22, 2024
1 parent d51150b commit 143ef7d
Show file tree
Hide file tree
Showing 15 changed files with 760 additions and 734 deletions.
163 changes: 82 additions & 81 deletions Unosquare.FFME.Windows.Sample/Foundation/FileInputStream.cs
Original file line number Diff line number Diff line change
@@ -1,105 +1,106 @@
namespace Unosquare.FFME.Windows.Sample.Foundation
namespace Unosquare.FFME.Windows.Sample.Foundation;

using Common;
using FFmpeg.AutoGen;
using System;
using System.IO;
using System.Runtime.InteropServices;

/// <inheritdoc />
/// <summary>
/// Provides an example of a very simple custom input stream.
/// </summary>
/// <seealso cref="IMediaInputStream" />
public sealed unsafe class FileInputStream : IMediaInputStream
{
using Common;
using FFmpeg.AutoGen;
using System;
using System.IO;
using System.Runtime.InteropServices;
private readonly FileStream BackingStream;
private readonly object ReadLock = new();
private readonly byte[] ReadBuffer;

/// <inheritdoc />
/// <summary>
/// Provides an example of a very simple custom input stream.
/// Initializes a new instance of the <see cref="FileInputStream"/> class.
/// </summary>
/// <seealso cref="IMediaInputStream" />
public sealed unsafe class FileInputStream : IMediaInputStream
/// <param name="path">The path.</param>
public FileInputStream(string path)
{
private readonly FileStream BackingStream;
private readonly object ReadLock = new object();
private readonly byte[] ReadBuffer;

/// <summary>
/// Initializes a new instance of the <see cref="FileInputStream"/> class.
/// </summary>
/// <param name="path">The path.</param>
public FileInputStream(string path)
{
var fullPath = Path.GetFullPath(path);
BackingStream = File.OpenRead(fullPath);
var uri = new Uri(fullPath);
StreamUri = new Uri(uri.ToString().ReplaceOrdinal("file://", Scheme));
CanSeek = true;
ReadBuffer = new byte[ReadBufferLength];
}
var fullPath = Path.GetFullPath(path);
BackingStream = File.OpenRead(fullPath);
var uri = new Uri(fullPath);
StreamUri = new Uri(uri.ToString().ReplaceOrdinal("file://", Scheme));
CanSeek = true;
ReadBuffer = new byte[ReadBufferLength];
}

/// <summary>
/// The custom file scheme (URL prefix) including the :// sequence.
/// </summary>
public static string Scheme => "customfile://";
/// <summary>
/// The custom file scheme (URL prefix) including the :// sequence.
/// </summary>
public static string Scheme => "customfile://";

/// <inheritdoc />
public Uri StreamUri { get; }
/// <inheritdoc />
public Uri StreamUri { get; }

/// <inheritdoc />
public bool CanSeek { get; }
/// <inheritdoc />
public bool CanSeek { get; }

/// <inheritdoc />
public int ReadBufferLength => 1024 * 16;
/// <inheritdoc />
public int ReadBufferLength => 1024 * 16;

/// <inheritdoc />
public InputStreamInitializing OnInitializing { get; }
/// <inheritdoc />
public InputStreamInitializing OnInitializing { get; }

/// <inheritdoc />
public InputStreamInitialized OnInitialized { get; }
/// <inheritdoc />
public InputStreamInitialized OnInitialized { get; }

/// <inheritdoc />
public void Dispose()
{
BackingStream?.Dispose();
}
/// <inheritdoc />
public void Dispose()
{
BackingStream?.Dispose();
}

/// <summary>
/// Reads from the underlying stream and writes up to <paramref name="targetBufferLength" /> bytes
/// to the <paramref name="targetBuffer" />. Returns the number of bytes that were written.
/// </summary>
/// <param name="opaque">The opaque.</param>
/// <param name="targetBuffer">The target buffer.</param>
/// <param name="targetBufferLength">Length of the target buffer.</param>
/// <returns>
/// The number of bytes that have been read.
/// </returns>
public int Read(void* opaque, byte* targetBuffer, int targetBufferLength)
/// <summary>
/// Reads from the underlying stream and writes up to <paramref name="targetBufferLength" /> bytes
/// to the <paramref name="targetBuffer" />. Returns the number of bytes that were written.
/// </summary>
/// <param name="opaque">The opaque.</param>
/// <param name="targetBuffer">The target buffer.</param>
/// <param name="targetBufferLength">Length of the target buffer.</param>
/// <returns>
/// The number of bytes that have been read.
/// </returns>
public int Read(void* opaque, byte* targetBuffer, int targetBufferLength)
{
lock (ReadLock)
{
lock (ReadLock)
try
{
try
{
var readCount = BackingStream.Read(ReadBuffer, 0, ReadBuffer.Length);
if (readCount > 0)
Marshal.Copy(ReadBuffer, 0, (IntPtr)targetBuffer, readCount);

return readCount;
}
catch (Exception)
{
var readCount = BackingStream.Read(ReadBuffer, 0, ReadBuffer.Length);
if (readCount > 0)
Marshal.Copy(ReadBuffer, 0, (IntPtr)targetBuffer, readCount);
else if (readCount == 0)
return ffmpeg.AVERROR_EOF;
}

return readCount;
}
catch (Exception)
{
return ffmpeg.AVERROR_EOF;
}
}
}

/// <inheritdoc />
public long Seek(void* opaque, long offset, int whence)
/// <inheritdoc />
public long Seek(void* opaque, long offset, int whence)
{
lock (ReadLock)
{
lock (ReadLock)
try
{
try
{
return whence == ffmpeg.AVSEEK_SIZE ?
BackingStream.Length : BackingStream.Seek(offset, SeekOrigin.Begin);
}
catch
{
return ffmpeg.AVERROR_EOF;
}
return whence == ffmpeg.AVSEEK_SIZE ?
BackingStream.Length : BackingStream.Seek(offset, SeekOrigin.Begin);
}
catch
{
return ffmpeg.AVERROR_EOF;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
<RepositoryType>GitHub</RepositoryType>
<RepositoryUrl>https://github.com/unosquare/ffmediaelement</RepositoryUrl>

<Version>4.4.360</Version>
<AssemblyVersion>4.4.360.0</AssemblyVersion>
<FileVersion>4.4.360.0</FileVersion>
<Version>7.0.360</Version>
<AssemblyVersion>7.0.360.0</AssemblyVersion>
<FileVersion>7.0.360.0</FileVersion>
<LangVersion>preview</LangVersion>

<Authors>Mario Di Vece, and Contributors to the FFME project</Authors>
Expand Down
Loading

0 comments on commit 143ef7d

Please sign in to comment.