-
-
Notifications
You must be signed in to change notification settings - Fork 747
Expand file tree
/
Copy pathRequestHandler_Sources.cpp
More file actions
369 lines (295 loc) · 14.8 KB
/
RequestHandler_Sources.cpp
File metadata and controls
369 lines (295 loc) · 14.8 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
obs-websocket
Copyright (C) 2016-2021 Stephane Lepin <stephane.lepin@gmail.com>
Copyright (C) 2020-2021 Kyle Manning <tt2468@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <https://www.gnu.org/licenses/>
*/
#include <QBuffer>
#include <QImageWriter>
#include <QFileInfo>
#include <QImage>
#include <QDir>
#include <QMimeDatabase>
#include <QMimeType>
#include "RequestHandler.h"
QImage TakeSourceScreenshot(obs_source_t *source, bool &success, uint32_t requestedWidth = 0, uint32_t requestedHeight = 0)
{
// Get info about the requested source
const uint32_t sourceWidth = obs_source_get_width(source);
const uint32_t sourceHeight = obs_source_get_height(source);
const double sourceAspectRatio = ((double)sourceWidth / (double)sourceHeight);
uint32_t imgWidth = sourceWidth;
uint32_t imgHeight = sourceHeight;
// Determine suitable image width
if (requestedWidth) {
imgWidth = requestedWidth;
if (!requestedHeight)
imgHeight = ((double)imgWidth / sourceAspectRatio);
}
// Determine suitable image height
if (requestedHeight) {
imgHeight = requestedHeight;
if (!requestedWidth)
imgWidth = ((double)imgHeight * sourceAspectRatio);
}
// Create final image texture
QImage ret(imgWidth, imgHeight, QImage::Format::Format_RGBA8888);
ret.fill(0);
// Video image buffer
uint8_t *videoData = nullptr;
uint32_t videoLinesize = 0;
// Enter graphics context
obs_enter_graphics();
gs_texrender_t *texRender = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
gs_stagesurf_t *stageSurface = gs_stagesurface_create(imgWidth, imgHeight, GS_RGBA);
success = false;
gs_texrender_reset(texRender);
if (gs_texrender_begin(texRender, imgWidth, imgHeight)) {
vec4 background;
vec4_zero(&background);
gs_clear(GS_CLEAR_COLOR, &background, 0.0f, 0);
gs_ortho(0.0f, (float)sourceWidth, 0.0f, (float)sourceHeight, -100.0f, 100.0f);
gs_blend_state_push();
gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
obs_source_inc_showing(source);
obs_source_video_render(source);
obs_source_dec_showing(source);
gs_blend_state_pop();
gs_texrender_end(texRender);
gs_stage_texture(stageSurface, gs_texrender_get_texture(texRender));
if (gs_stagesurface_map(stageSurface, &videoData, &videoLinesize)) {
int lineSize = ret.bytesPerLine();
for (uint y = 0; y < imgHeight; y++) {
memcpy(ret.scanLine(y), videoData + (y * videoLinesize), lineSize);
}
gs_stagesurface_unmap(stageSurface);
success = true;
}
}
gs_stagesurface_destroy(stageSurface);
gs_texrender_destroy(texRender);
obs_leave_graphics();
return ret;
}
bool IsImageFormatValid(std::string format)
{
QByteArrayList supportedFormats = QImageWriter::supportedImageFormats();
return supportedFormats.contains(format.c_str());
}
/**
* Gets the active and show state of a source.
*
* **Compatible with inputs and scenes.**
*
* @requestField ?canvasUuid | String | UUID of the canvas the source is in, if using sourceName field
* @requestField ?sourceName | String | Name of the source to get the active state of
* @requestField ?sourceUuid | String | UUID of the source to get the active state of
*
* @responseField videoActive | Boolean | Whether the source is showing in Program
* @responseField videoShowing | Boolean | Whether the source is showing in the UI (Preview, Projector, Properties)
*
* @requestType GetSourceActive
* @complexity 2
* @rpcVersion -1
* @initialVersion 5.0.0
* @api requests
* @category sources
*/
RequestResult RequestHandler::GetSourceActive(const Request &request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
OBSSourceAutoRelease source = request.AcquireSource("canvasUuid", "sourceName", "sourceUuid", statusCode, comment);
if (!source)
return RequestResult::Error(statusCode, comment);
if (obs_source_get_type(source) != OBS_SOURCE_TYPE_INPUT && obs_source_get_type(source) != OBS_SOURCE_TYPE_SCENE)
return RequestResult::Error(RequestStatus::InvalidResourceType, "The specified source is not an input or a scene.");
json responseData;
responseData["videoActive"] = obs_source_active(source);
responseData["videoShowing"] = obs_source_showing(source);
return RequestResult::Success(responseData);
}
/**
* Gets a Base64-encoded screenshot of a source.
*
* The `imageWidth` and `imageHeight` parameters are treated as "scale to inner", meaning the smallest ratio will be used and the aspect ratio of the original resolution is kept.
* If `imageWidth` and `imageHeight` are not specified, the compressed image will use the full resolution of the source.
*
* **Compatible with inputs and scenes.**
*
* @requestField ?canvasUuid | String | UUID of the canvas the source is in, if using sourceName field
* @requestField ?sourceName | String | Name of the source to take a screenshot of
* @requestField ?sourceUuid | String | UUID of the source to take a screenshot of
* @requestField imageFormat | String | Image compression format to use. Use `GetVersion` to get compatible image formats
* @requestField ?imageWidth | Number | Width to scale the screenshot to | >= 8, <= 4096 | Source value is used
* @requestField ?imageHeight | Number | Height to scale the screenshot to | >= 8, <= 4096 | Source value is used
* @requestField ?imageCompressionQuality | Number | Compression quality to use. 0 for high compression, 100 for uncompressed. -1 to use "default" (whatever that means, idk) | >= -1, <= 100 | -1
*
* @responseField imageData | String | Base64-encoded screenshot
*
* @requestType GetSourceScreenshot
* @complexity 4
* @rpcVersion -1
* @initialVersion 5.0.0
* @api requests
* @category sources
*/
RequestResult RequestHandler::GetSourceScreenshot(const Request &request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
OBSSourceAutoRelease source = request.AcquireSource("canvasUuid", "sourceName", "sourceUuid", statusCode, comment);
if (!(source && request.ValidateString("imageFormat", statusCode, comment)))
return RequestResult::Error(statusCode, comment);
if (obs_source_get_type(source) != OBS_SOURCE_TYPE_INPUT && obs_source_get_type(source) != OBS_SOURCE_TYPE_SCENE)
return RequestResult::Error(RequestStatus::InvalidResourceType, "The specified source is not an input or a scene.");
std::string imageFormat = request.RequestData["imageFormat"];
if (!IsImageFormatValid(imageFormat))
return RequestResult::Error(RequestStatus::InvalidRequestField,
"Your specified image format is invalid or not supported by this system.");
// Used to get the correct MIME type that is compatible with web browsers.
// Ex: Both when requested "imageFormat" is "jpeg" and "jpg", the returned MIME type should be "image/jpeg" for both.
// See Issue #1298
QMimeDatabase mimeDatabase;
// Create fake file path with user requested image format(file extension)
QString fileName = QString::fromStdString("1." + imageFormat);
QMimeType mimeType = mimeDatabase.mimeTypeForFile(QFileInfo(fileName));
QString responseMimeStr = mimeType.name();
uint32_t requestedWidth{0};
uint32_t requestedHeight{0};
int compressionQuality{-1};
if (request.Contains("imageWidth")) {
if (!request.ValidateOptionalNumber("imageWidth", statusCode, comment, 8, 4096))
return RequestResult::Error(statusCode, comment);
requestedWidth = request.RequestData["imageWidth"];
}
if (request.Contains("imageHeight")) {
if (!request.ValidateOptionalNumber("imageHeight", statusCode, comment, 8, 4096))
return RequestResult::Error(statusCode, comment);
requestedHeight = request.RequestData["imageHeight"];
}
if (request.Contains("imageCompressionQuality")) {
if (!request.ValidateOptionalNumber("imageCompressionQuality", statusCode, comment, -1, 100))
return RequestResult::Error(statusCode, comment);
compressionQuality = request.RequestData["imageCompressionQuality"];
}
bool success;
QImage renderedImage = TakeSourceScreenshot(source, success, requestedWidth, requestedHeight);
if (!success)
return RequestResult::Error(RequestStatus::RequestProcessingFailed, "Failed to render screenshot.");
QByteArray encodedImgBytes;
QBuffer buffer(&encodedImgBytes);
buffer.open(QBuffer::WriteOnly);
if (!renderedImage.save(&buffer, imageFormat.c_str(), compressionQuality))
return RequestResult::Error(RequestStatus::RequestProcessingFailed, "Failed to encode screenshot.");
buffer.close();
QString encodedPicture = QString("data:%1;base64,").arg(responseMimeStr).append(encodedImgBytes.toBase64());
json responseData;
responseData["imageData"] = encodedPicture.toStdString();
return RequestResult::Success(responseData);
}
/**
* Saves a screenshot of a source to the filesystem.
*
* The `imageWidth` and `imageHeight` parameters are treated as "scale to inner", meaning the smallest ratio will be used and the aspect ratio of the original resolution is kept.
* If `imageWidth` and `imageHeight` are not specified, the compressed image will use the full resolution of the source.
*
* **Compatible with inputs and scenes.**
*
* @requestField ?canvasUuid | String | UUID of the canvas the source is in, if using sourceName field
* @requestField ?sourceName | String | Name of the source to take a screenshot of
* @requestField ?sourceUuid | String | UUID of the source to take a screenshot of
* @requestField imageFormat | String | Image compression format to use. Use `GetVersion` to get compatible image formats
* @requestField imageFilePath | String | Path to save the screenshot file to. Eg. `C:\Users\user\Desktop\screenshot.png`
* @requestField ?imageWidth | Number | Width to scale the screenshot to | >= 8, <= 4096 | Source value is used
* @requestField ?imageHeight | Number | Height to scale the screenshot to | >= 8, <= 4096 | Source value is used
* @requestField ?imageCompressionQuality | Number | Compression quality to use. 0 for high compression, 100 for uncompressed. -1 to use "default" (whatever that means, idk) | >= -1, <= 100 | -1
*
* @requestType SaveSourceScreenshot
* @complexity 3
* @rpcVersion -1
* @initialVersion 5.0.0
* @api requests
* @category sources
*/
RequestResult RequestHandler::SaveSourceScreenshot(const Request &request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
OBSSourceAutoRelease source = request.AcquireSource("canvasUuid", "sourceName", "sourceUuid", statusCode, comment);
if (!(source && request.ValidateString("imageFormat", statusCode, comment) &&
request.ValidateString("imageFilePath", statusCode, comment)))
return RequestResult::Error(statusCode, comment);
if (obs_source_get_type(source) != OBS_SOURCE_TYPE_INPUT && obs_source_get_type(source) != OBS_SOURCE_TYPE_SCENE)
return RequestResult::Error(RequestStatus::InvalidResourceType, "The specified source is not an input or a scene.");
std::string imageFormat = request.RequestData["imageFormat"];
std::string imageFilePath = request.RequestData["imageFilePath"];
if (!IsImageFormatValid(imageFormat))
return RequestResult::Error(RequestStatus::InvalidRequestField,
"Your specified image format is invalid or not supported by this system.");
QFileInfo filePathInfo(QString::fromStdString(imageFilePath));
if (!filePathInfo.absoluteDir().exists())
return RequestResult::Error(RequestStatus::ResourceNotFound, "The directory for your file path does not exist.");
uint32_t requestedWidth{0};
uint32_t requestedHeight{0};
int compressionQuality{-1};
if (request.Contains("imageWidth")) {
if (!request.ValidateOptionalNumber("imageWidth", statusCode, comment, 8, 4096))
return RequestResult::Error(statusCode, comment);
requestedWidth = request.RequestData["imageWidth"];
}
if (request.Contains("imageHeight")) {
if (!request.ValidateOptionalNumber("imageHeight", statusCode, comment, 8, 4096))
return RequestResult::Error(statusCode, comment);
requestedHeight = request.RequestData["imageHeight"];
}
if (request.Contains("imageCompressionQuality")) {
if (!request.ValidateOptionalNumber("imageCompressionQuality", statusCode, comment, -1, 100))
return RequestResult::Error(statusCode, comment);
compressionQuality = request.RequestData["imageCompressionQuality"];
}
bool success;
QImage renderedImage = TakeSourceScreenshot(source, success, requestedWidth, requestedHeight);
if (!success)
return RequestResult::Error(RequestStatus::RequestProcessingFailed, "Failed to render screenshot.");
QString absoluteFilePath = filePathInfo.absoluteFilePath();
if (!renderedImage.save(absoluteFilePath, imageFormat.c_str(), compressionQuality))
return RequestResult::Error(RequestStatus::RequestProcessingFailed, "Failed to save screenshot.");
return RequestResult::Success();
}
// Intentionally undocumented
RequestResult RequestHandler::GetSourcePrivateSettings(const Request &request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
OBSSourceAutoRelease source = request.AcquireSource("canvasUuid", "sourceName", "sourceUuid", statusCode, comment);
if (!source)
return RequestResult::Error(statusCode, comment);
OBSDataAutoRelease privateSettings = obs_source_get_private_settings(source);
json responseData;
responseData["sourceSettings"] = Utils::Json::ObsDataToJson(privateSettings);
return RequestResult::Success(responseData);
}
// Intentionally undocumented
RequestResult RequestHandler::SetSourcePrivateSettings(const Request &request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
OBSSourceAutoRelease source = request.AcquireSource("canvasUuid", "sourceName", "sourceUuid", statusCode, comment);
if (!source || !request.ValidateObject("sourceSettings", statusCode, comment, true))
return RequestResult::Error(statusCode, comment);
OBSDataAutoRelease privateSettings = obs_source_get_private_settings(source);
OBSDataAutoRelease newSettings = Utils::Json::JsonToObsData(request.RequestData["sourceSettings"]);
// Always overlays to prevent destroying internal source data unintentionally
obs_data_apply(privateSettings, newSettings);
return RequestResult::Success();
}