Skip to content

Commit

Permalink
feat: add CALLBG to perform CALL on the background producer #1525
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Mar 19, 2024
1 parent d8e970f commit 55e3a04
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/core/producer/stage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ struct stage::impl : public std::enable_shared_from_this<impl>
{
return flatten(executor_.begin_invoke([=] { return get_layer(index).foreground()->call(params).share(); }));
}
std::future<std::wstring> callbg(int index, const std::vector<std::wstring>& params)
{
return flatten(executor_.begin_invoke([=] { return get_layer(index).background()->call(params).share(); }));
}

std::unique_lock<std::mutex> get_lock() { return std::move(std::unique_lock<std::mutex>(lock_)); }

Expand Down Expand Up @@ -414,6 +418,10 @@ std::future<std::wstring> stage::call(int index, const std::vector<std::wstring>
{
return impl_->call(index, params);
}
std::future<std::wstring> stage::callbg(int index, const std::vector<std::wstring>& params)
{
return impl_->callbg(index, params);
}
std::future<void> stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms)
{
return impl_->apply_transforms(transforms);
Expand Down Expand Up @@ -488,6 +496,10 @@ std::future<std::wstring> stage_delayed::call(int index, const std::vector<std::
{
return executor_.begin_invoke([=]() -> std::wstring { return stage_->call(index, params).get(); });
}
std::future<std::wstring> stage_delayed::callbg(int index, const std::vector<std::wstring>& params)
{
return executor_.begin_invoke([=]() -> std::wstring { return stage_->callbg(index, params).get(); });
}
std::future<void> stage_delayed::apply_transforms(const std::vector<stage_delayed::transform_tuple_t>& transforms)
{
return executor_.begin_invoke([=]() { return stage_->apply_transforms(transforms).get(); });
Expand Down
3 changes: 3 additions & 0 deletions src/core/producer/stage.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class stage_base
virtual std::future<void> play(int index) = 0;
virtual std::future<void> stop(int index) = 0;
virtual std::future<std::wstring> call(int index, const std::vector<std::wstring>& params) = 0;
virtual std::future<std::wstring> callbg(int index, const std::vector<std::wstring>& params) = 0;
virtual std::future<void> clear(int index) = 0;
virtual std::future<void> clear() = 0;
virtual std::future<void> swap_layers(const std::shared_ptr<stage_base>& other, bool swap_transforms) = 0;
Expand Down Expand Up @@ -136,6 +137,7 @@ class stage final : public stage_base
std::future<void> play(int index) override;
std::future<void> stop(int index) override;
std::future<std::wstring> call(int index, const std::vector<std::wstring>& params) override;
std::future<std::wstring> callbg(int index, const std::vector<std::wstring>& params) override;
std::future<void> clear(int index) override;
std::future<void> clear() override;
std::future<void> swap_layers(const std::shared_ptr<stage_base>& other, bool swap_transforms) override;
Expand Down Expand Up @@ -191,6 +193,7 @@ class stage_delayed final : public stage_base
std::future<void> play(int index) override;
std::future<void> stop(int index) override;
std::future<std::wstring> call(int index, const std::vector<std::wstring>& params) override;
std::future<std::wstring> callbg(int index, const std::vector<std::wstring>& params) override;
std::future<void> clear(int index) override;
std::future<void> clear() override;
std::future<void> swap_layers(const std::shared_ptr<stage_base>& other, bool swap_transforms) override;
Expand Down
24 changes: 24 additions & 0 deletions src/protocol/amcp/AMCPCommandsImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,29 @@ std::wstring clear_all_command(command_context& ctx)
return L"202 CLEAR ALL OK\r\n";
}

std::future<std::wstring> callbg_command(command_context& ctx)
{
const auto result = ctx.channel.stage->callbg(ctx.layer_index(), ctx.parameters).share();

// TODO: because of std::async deferred timed waiting does not work

/*auto wait_res = result.wait_for(std::chrono::seconds(2));
if (wait_res == std::future_status::timeout)
CASPAR_THROW_EXCEPTION(timed_out());*/

return std::async(std::launch::deferred, [result]() -> std::wstring {
std::wstring res = result.get();

std::wstringstream replyString;
if (res.empty())
replyString << L"202 CALLBG OK\r\n";
else
replyString << L"201 CALLBG OK\r\n" << res << L"\r\n";

return replyString.str();
});
}

std::future<std::wstring> call_command(command_context& ctx)
{
const auto result = ctx.channel.stage->call(ctx.layer_index(), ctx.parameters).share();
Expand Down Expand Up @@ -1674,6 +1697,7 @@ std::wstring osc_unsubscribe_command(command_context& ctx)
void register_commands(std::shared_ptr<amcp_command_repository_wrapper>& repo)
{
repo->register_channel_command(L"Basic Commands", L"LOADBG", loadbg_command, 1);
repo->register_channel_command(L"Basic Commands", L"CALLBG", callbg_command, 1);
repo->register_channel_command(L"Basic Commands", L"LOAD", load_command, 0);
repo->register_channel_command(L"Basic Commands", L"PLAY", play_command, 0);
repo->register_channel_command(L"Basic Commands", L"PAUSE", pause_command, 0);
Expand Down

0 comments on commit 55e3a04

Please sign in to comment.