stream video from memory

pull/22/head
cosmonaut 2022-08-18 11:47:23 -07:00
parent b648d1b1c3
commit e17a29cbf9
2 changed files with 20 additions and 1 deletions

View File

@ -17,6 +17,8 @@ namespace MoonWorks.Video
public unsafe class Video : IDisposable
{
internal IntPtr Handle;
private IntPtr rwData;
private void* videoData;
public bool Loop { get; private set; }
public float Volume {
@ -71,7 +73,12 @@ namespace MoonWorks.Video
throw new ArgumentException("Video file not found!");
}
if (Theorafile.tf_fopen(filename, out Handle) < 0)
var bytes = System.IO.File.ReadAllBytes(filename);
videoData = NativeMemory.Alloc((nuint) bytes.Length);
Marshal.Copy(bytes, 0, (IntPtr) videoData, bytes.Length);
rwData = SDL2.SDL.SDL_RWFromMem((IntPtr) videoData, bytes.Length);
if (Theorafile.tf_open_callbacks(rwData, out Handle, callbacks) < 0)
{
throw new ArgumentException("Invalid video file!");
}
@ -317,6 +324,17 @@ namespace MoonWorks.Video
currentFrame = -1;
}
private static IntPtr Read(IntPtr ptr, IntPtr size, IntPtr nmemb, IntPtr datasource) => (IntPtr) SDL2.SDL.SDL_RWread(datasource, ptr, size, nmemb);
private static int Seek(IntPtr datasource, long offset, Theorafile.SeekWhence whence) => (int) SDL2.SDL.SDL_RWseek(datasource, offset, (int) whence);
private static int Close(IntPtr datasource) => (int) SDL2.SDL.SDL_RWclose(datasource);
private static Theorafile.tf_callbacks callbacks = new Theorafile.tf_callbacks
{
read_func = Read,
seek_func = Seek,
close_func = Close
};
protected virtual void Dispose(bool disposing)
{
if (!disposed)
@ -332,6 +350,7 @@ namespace MoonWorks.Video
// free unmanaged resources (unmanaged objects)
Theorafile.tf_close(ref Handle);
NativeMemory.Free(yuvData);
NativeMemory.Free(videoData);
disposed = true;
}

0
src/Video/VideoData.cs Normal file
View File