Skip to content

Commit

Permalink
Add input image resize options (#59)
Browse files Browse the repository at this point in the history
* add input image resize options

* lint

* add space

* file url

* Revert "file url"

This reverts commit 6dcb4ee.
  • Loading branch information
royshil committed Nov 27, 2023
1 parent 684a19d commit d023dd2
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/obs-source-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ void destroy_source_render_data(source_render_data *tf)
* @param tf The filter data
* @param width The width of the stage surface (output)
* @param height The height of the stage surface (output)
* @return true if successful
* @return false if unsuccessful
* @param scale Scale the output by this factor
* @return The RGBA buffer (4 bytes per pixel) or an empty vector if there was an error
*/
std::vector<uint8_t> get_rgba_from_source_render(obs_source_t *source, source_render_data *tf,
uint32_t &width, uint32_t &height)
uint32_t &width, uint32_t &height, float scale)
{
if (!obs_source_enabled(source)) {
obs_log(LOG_ERROR, "Source is not enabled");
Expand All @@ -50,6 +50,9 @@ std::vector<uint8_t> get_rgba_from_source_render(obs_source_t *source, source_re
obs_log(LOG_ERROR, "Width or height is 0");
return std::vector<uint8_t>();
}
// scale the width and height
width *= scale;
height *= scale;

// enter graphics context
obs_enter_graphics();
Expand Down
2 changes: 1 addition & 1 deletion src/obs-source-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void init_source_render_data(source_render_data *tf);
void destroy_source_render_data(source_render_data *tf);

std::vector<uint8_t> get_rgba_from_source_render(obs_source_t *source, source_render_data *tf,
uint32_t &width, uint32_t &height);
uint32_t &width, uint32_t &height, float scale);

std::string convert_rgba_buffer_to_png_base64(const std::vector<uint8_t> &rgba, uint32_t width,
uint32_t height);
Expand Down
22 changes: 19 additions & 3 deletions src/request-data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ struct request_data_handler_response request_data_handler(url_source_request_dat
// aggregate to empty is requested and the text is not empty
// trim the text and add it to the aggregate buffer
std::string textStr = text;
request_data->aggregate_to_empty_buffer += trim(textStr);
request_data->aggregate_to_empty_buffer +=
" " + trim(textStr);
// if the buffer is larger than the limit, remove the first part
if (request_data->aggregate_to_empty_buffer.size() >
URL_SOURCE_AGG_BUFFER_MAX_SIZE) {
Expand Down Expand Up @@ -236,9 +237,21 @@ struct request_data_handler_response request_data_handler(url_source_request_dat
// render the source to an image using get_rgba_from_source_render
source_render_data tf;
init_source_render_data(&tf);
// get the scale factor from the request_data->obs_input_source_resize_option
float scale = 1.0;
if (request_data->obs_input_source_resize_option != "100%") {
// parse the scale from the string
std::string scaleStr =
request_data->obs_input_source_resize_option;
scaleStr.erase(std::remove(scaleStr.begin(), scaleStr.end(),
'%'),
scaleStr.end());
scale = (float)(std::stof(scaleStr) / 100.0f);
}

uint32_t width, height;
std::vector<uint8_t> rgba =
get_rgba_from_source_render(source, &tf, width, height);
std::vector<uint8_t> rgba = get_rgba_from_source_render(
source, &tf, width, height, scale);
if (rgba.empty()) {
obs_log(LOG_INFO, "Failed to get RGBA from source render");
// Return an error response
Expand Down Expand Up @@ -426,6 +439,7 @@ std::string serialize_request_data(url_source_request_data *request_data)
json["obs_text_source_skip_if_empty"] = request_data->obs_text_source_skip_if_empty;
json["obs_text_source_skip_if_same"] = request_data->obs_text_source_skip_if_same;
json["aggregate_to_empty"] = request_data->aggregate_to_empty;
json["obs_input_source_resize_option"] = request_data->obs_input_source_resize_option;
// SSL options
json["ssl_client_cert_file"] = request_data->ssl_client_cert_file;
json["ssl_client_key_file"] = request_data->ssl_client_key_file;
Expand Down Expand Up @@ -474,6 +488,8 @@ url_source_request_data unserialize_request_data(std::string serialized_request_
request_data.obs_text_source_skip_if_same =
json.value("obs_text_source_skip_if_same", false);
request_data.aggregate_to_empty = json.value("aggregate_to_empty", false);
request_data.obs_input_source_resize_option =
json.value("obs_input_source_resize_option", "100%");

// SSL options
request_data.ssl_client_cert_file = json.value("ssl_client_cert_file", "");
Expand Down
2 changes: 2 additions & 0 deletions src/request-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct url_source_request_data {
bool aggregate_to_empty;
std::string aggregate_to_empty_buffer;
std::string last_obs_text_source_value;
std::string obs_input_source_resize_option;
// SSL options
std::string ssl_client_cert_file;
std::string ssl_client_key_file;
Expand Down Expand Up @@ -56,6 +57,7 @@ struct url_source_request_data {
aggregate_to_empty = false;
aggregate_to_empty_buffer = std::string("");
last_obs_text_source_value = std::string("");
obs_input_source_resize_option = std::string("100%");
ssl_verify_peer = false;
headers = {};
output_type = std::string("text");
Expand Down
20 changes: 20 additions & 0 deletions src/ui/RequestBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ RequestBuilder::RequestBuilder(url_source_request_data *request_data,
ui->obsTextSourceEnabledCheckBox->setChecked(request_data->obs_text_source_skip_if_empty);
ui->obsTextSourceSkipSameCheckBox->setChecked(request_data->obs_text_source_skip_if_same);
ui->aggToEmpty->setChecked(request_data->aggregate_to_empty);
ui->comboBox_resizeInput->setCurrentText(
QString::fromStdString(request_data->obs_input_source_resize_option));

auto inputSourceSelected = [=]() {
// if the source is a media source, show the resize option, otherwise hide it
auto current_data = ui->obsTextSourceComboBox->currentData();
bool hide_resize_option = true;
if (current_data.isValid()) {
const std::string source_name = current_data.toString().toStdString();
hide_resize_option = is_obs_source_text(source_name);
}
ui->comboBox_resizeInput->setVisible(!hide_resize_option);
ui->label_resizeInput->setVisible(!hide_resize_option);
};
connect(ui->obsTextSourceComboBox, &QComboBox::currentTextChanged, this,
inputSourceSelected);
inputSourceSelected();

ui->bodyTextEdit->setText(QString::fromStdString(request_data->body));

auto setVisibilityOfBody = [=]() {
Expand Down Expand Up @@ -289,6 +307,8 @@ RequestBuilder::RequestBuilder(url_source_request_data *request_data,
request_data_for_saving->obs_text_source_skip_if_same =
ui->obsTextSourceSkipSameCheckBox->isChecked();
request_data_for_saving->aggregate_to_empty = ui->aggToEmpty->isChecked();
request_data_for_saving->obs_input_source_resize_option =
ui->comboBox_resizeInput->currentText().toStdString();

// Save the SSL certificate file
request_data_for_saving->ssl_client_cert_file =
Expand Down
62 changes: 61 additions & 1 deletion src/ui/requestbuilder.ui
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@
<widget class="QWidget" name="widget_8" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<property name="spacing">
<number>0</number>
<number>3</number>
</property>
<property name="leftMargin">
<number>0</number>
Expand All @@ -465,6 +465,12 @@
</property>
<item>
<widget class="QComboBox" name="obsTextSourceComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>&quot;Select a OBS text source to use its current text in the querystring or request body as `{{input}}`.&quot;</string>
</property>
Expand All @@ -475,11 +481,65 @@
</item>
</widget>
</item>
<item>
<widget class="QLabel" name="label_resizeInput">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Resize</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox_resizeInput">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<item>
<property name="text">
<string>100%</string>
</property>
</item>
<item>
<property name="text">
<string>50%</string>
</property>
</item>
<item>
<property name="text">
<string>25%</string>
</property>
</item>
<item>
<property name="text">
<string>10%</string>
</property>
</item>
<item>
<property name="text">
<string>5%</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="widget_10" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_8">
<property name="spacing">
<number>2</number>
Expand Down

0 comments on commit d023dd2

Please sign in to comment.