-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImagePlayer.cs
92 lines (78 loc) · 2.62 KB
/
ImagePlayer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using SlimDX.Direct3D11;
using VVVV.Core.Logging;
using VVVV.Utils.VMath;
namespace VVVV.DX11.ImagePlayer
{
class Player : IDisposable
{
FileInfo[] files;
Dictionary<int, Frame> frames;
List<int> requestedKeys;
public string DirectoryName { get; private set; }
public string FileMask { get; private set; }
public int FrameCount { get; private set; }
public Frame this[int index]
{
get
{
var id = VMath.Zmod(index, requestedKeys.Count);
return frames[requestedKeys[id]];
}
}
public IEnumerable<bool> Loaded => requestedKeys.Select(k => frames[k].Loaded);
readonly Device device;
readonly MemoryPool FMemoryPool;
readonly ILogger FLogger;
public Player(string dir, string fileMask, MemoryPool memoryPool, ILogger logger)
{
FMemoryPool = memoryPool;
FLogger = logger;
DirectoryName = dir;
FileMask = fileMask;
var directory = new DirectoryInfo(dir);
requestedKeys = new List<int>();
frames = new Dictionary<int, Frame>();
if (directory.Exists)
{
files = directory.GetFiles(fileMask).Where(f => (f.Attributes & FileAttributes.Hidden) == 0).ToArray();
FrameCount = files.Length;
}
else
files = new FileInfo[0]{ };
device = Lib.Devices.DX11GlobalDevice.DeviceManager.RenderContexts[0].Device;
}
public void Preload(IEnumerable<int> indices, DecoderChoice decoderChoice = DecoderChoice.Automatic)
{
requestedKeys.Clear();
var toDelete = new List<int>(frames.Keys);
foreach (var id in indices)
{
var key = VMath.Zmod(id, FrameCount);
requestedKeys.Add(key);
if (frames.ContainsKey(key))
toDelete.Remove(key);
else
{
IDecoder decoder = Decoder.SelectFromFile(decoderChoice, files[key], FMemoryPool);
decoder.Device = device;
frames[key] = new Frame(files[key].FullName, decoder, FLogger);
frames[key].LoadAsync();
}
}
foreach (var d in toDelete)
{
frames[d].Dispose();
frames.Remove(d);
}
}
public void Dispose()
{
foreach (var frame in frames.Values)
frame.Dispose();
}
}
}