-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirect3DVideoEncoder.cpp
More file actions
236 lines (218 loc) · 9.33 KB
/
Copy pathDirect3DVideoEncoder.cpp
File metadata and controls
236 lines (218 loc) · 9.33 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <atomic>
#include <map>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <string>
#include <Windows.h>
#include "Common/CaptureSession.h"
#include "D3D11/CaptureSessionFactory.h"
namespace
{
std::mutex registryMutex;
std::map<int, std::shared_ptr<CaptureSession>> instances;
std::atomic<int> nextInstanceId = 1;
thread_local std::string exportedError;
thread_local std::string exportedTelemetry;
// Stores and writes an exported native error to the Windows diagnostic stream.
void ReportExportedError(const std::exception& exception)
{
exportedError = exception.what();
OutputDebugStringA((exportedError + "\n").c_str());
}
// Finds an instance while retaining it beyond the registry lock.
std::shared_ptr<CaptureSession> FindInstance(int id)
{
std::lock_guard<std::mutex> lock(registryMutex);
auto iterator = instances.find(id);
return iterator == instances.end() ? nullptr : iterator->second;
}
// Dispatches a Unity render event to its instance.
void __stdcall ProcessRenderEvent(int eventId)
{
auto instance = FindInstance(eventId);
if (instance)
{
instance->ProcessRenderEvent();
}
}
}
extern "C"
{
__declspec(dllexport) int __stdcall Direct3DVideoEncoderStartWithConstantQP(
void* texture, int width, int height, int frameRate, int codec, int qp, PacketCallback callback)
{
try
{
if (codec != 1 && codec != 2) throw std::invalid_argument("Invalid SDR video codec.");
if (width <= 0 || height <= 0 || (width & 1) || (height & 1) || frameRate <= 0 || qp < 1 || qp > 51)
throw std::invalid_argument("Invalid SDR dimensions, frame rate or QP.");
auto instance = CreateD3D11CaptureSession(texture, width, height, frameRate, 5, callback, codec, 0, 0, 0, qp);
int id = nextInstanceId.fetch_add(1);
if (id <= 0) throw std::overflow_error("Encoder identifiers exhausted.");
{ std::lock_guard<std::mutex> lock(registryMutex); instances.emplace(id, std::move(instance)); }
exportedError.clear();
return id;
}
catch (const std::exception& exception)
{
ReportExportedError(exception);
return 0;
}
}
// Creates a CQP session with an explicit preset and optional multi-output optimizations.
__declspec(dllexport) int __stdcall Direct3DVideoEncoderStartWithConstantQPOptions(
void* texture, int width, int height, int frameRate, int preset, int codec, int qp, int concurrentEncoding, PacketCallback callback)
{
try
{
if (codec != 1 && codec != 2) throw std::invalid_argument("Invalid SDR video codec.");
if (preset < 1 || preset > 7 || width <= 0 || height <= 0 || (width & 1) || (height & 1) || frameRate <= 0 || qp < 1 || qp > 51)
throw std::invalid_argument("Invalid SDR dimensions, frame rate, preset or QP.");
auto instance = CreateD3D11CaptureSession(texture, width, height, frameRate, preset, callback, codec, 0, 0, 0, qp, concurrentEncoding != 0);
int id = nextInstanceId.fetch_add(1);
if (id <= 0) throw std::overflow_error("Encoder identifiers exhausted.");
{ std::lock_guard<std::mutex> lock(registryMutex); instances.emplace(id, std::move(instance)); }
exportedError.clear();
return id;
}
catch (const std::exception& exception)
{
ReportExportedError(exception);
return 0;
}
}
// Creates an independent session with an explicit codec: 1 is H.264 and 2 is HEVC.
__declspec(dllexport) int __stdcall Direct3DVideoEncoderStartWithQuality(
void* texture, int width, int height, int frameRate, int preset, int codec, int averageBitRate, int maximumBitRate, int constantQuality, PacketCallback callback)
{
try
{
if (codec != 1 && codec != 2)
{
throw std::invalid_argument("Video codec must be 1 (H.264) or 2 (HEVC).");
}
if (averageBitRate <= 0 || maximumBitRate < averageBitRate || constantQuality < 1 || constantQuality > 51)
throw std::invalid_argument("Invalid quality profile rates.");
auto instance = CreateD3D11CaptureSession(texture, width, height, frameRate, preset, callback, codec, averageBitRate, maximumBitRate, constantQuality);
int id = nextInstanceId.fetch_add(1);
if (id <= 0)
{
throw std::overflow_error("The encoder session identifier space is exhausted.");
}
{ std::lock_guard<std::mutex> lock(registryMutex); instances.emplace(id, std::move(instance)); }
exportedError.clear();
return id;
}
catch (const std::exception& exception)
{
ReportExportedError(exception);
return 0;
}
}
// Creates an independent session with an explicit codec: 1 is H.264 and 2 is HEVC.
__declspec(dllexport) int __stdcall Direct3DVideoEncoderStartWithCodec(
void* texture, int width, int height, int frameRate, int preset, int codec, PacketCallback callback)
{
try
{
if (codec != 1 && codec != 2)
{
throw std::invalid_argument("Video codec must be 1 (H.264) or 2 (HEVC).");
}
auto instance = CreateD3D11CaptureSession(texture, width, height, frameRate, preset, callback, codec);
int id = nextInstanceId.fetch_add(1);
if (id <= 0)
{
throw std::overflow_error("The encoder session identifier space is exhausted.");
}
{ std::lock_guard<std::mutex> lock(registryMutex); instances.emplace(id, std::move(instance)); }
exportedError.clear();
return id;
}
catch (const std::exception& exception)
{
ReportExportedError(exception);
return 0;
}
}
// Creates an independent encoder and returns its positive identifier.
__declspec(dllexport) int __stdcall Direct3DVideoEncoderStart(
void* texture,
int width,
int height,
int frameRate,
int preset,
PacketCallback callback)
{
try
{
auto instance = CreateD3D11CaptureSession(texture, width, height, frameRate, preset, callback);
int id = nextInstanceId.fetch_add(1);
if (id <= 0)
{
throw std::overflow_error("The encoder session identifier space is exhausted.");
}
{ std::lock_guard<std::mutex> lock(registryMutex); instances.emplace(id, std::move(instance)); }
exportedError.clear();
return id;
}
catch (const std::exception& exception)
{
ReportExportedError(exception);
return 0;
}
}
// Queues a texture for one encoder instance.
__declspec(dllexport) void __stdcall Direct3DVideoEncoderQueueTexture(int id, void* texture, long long timestamp)
{
auto instance = FindInstance(id);
if (instance)
{
instance->QueueTexture(texture, timestamp);
}
}
// Returns the shared Unity render-event dispatcher.
__declspec(dllexport) void* __stdcall Direct3DVideoEncoderGetRenderEventFunction() { return reinterpret_cast<void*>(ProcessRenderEvent); }
// Flushes one instance while preserving its diagnostics.
__declspec(dllexport) void __stdcall Direct3DVideoEncoderStop(int id)
{
auto instance = FindInstance(id);
if (instance)
{
instance->Stop();
}
}
// Removes a stopped instance from the registry.
__declspec(dllexport) void __stdcall Direct3DVideoEncoderDestroy(int id)
{
std::lock_guard<std::mutex> lock(registryMutex);
instances.erase(id);
}
// Returns the latest instance or creation error.
__declspec(dllexport) const char* __stdcall Direct3DVideoEncoderGetLastError(int id)
{
auto instance = FindInstance(id);
if (instance)
{
exportedError = instance->LastError();
}
return exportedError.c_str();
}
// Returns the accepted frame count for one instance.
__declspec(dllexport) unsigned long long __stdcall Direct3DVideoEncoderGetQueuedFrameCount(int id)
{ auto instance = FindInstance(id); return instance ? instance->Queued() : 0; }
// Returns the encoded frame count for one instance.
__declspec(dllexport) unsigned long long __stdcall Direct3DVideoEncoderGetEncodedFrameCount(int id)
{ auto instance = FindInstance(id); return instance ? instance->Encoded() : 0; }
// Returns the dropped frame count for one instance.
__declspec(dllexport) unsigned long long __stdcall Direct3DVideoEncoderGetDroppedFrameCount(int id)
{ auto instance = FindInstance(id); return instance ? instance->Dropped() : 0; }
// Returns session telemetry while retaining the returned string until the next call on this thread.
__declspec(dllexport) const char* __stdcall Direct3DVideoEncoderGetTelemetry(int id)
{
auto instance = FindInstance(id);
exportedTelemetry = instance ? instance->Telemetry() : "{}";
return exportedTelemetry.c_str();
}
}