Skip to content

UI: Add support for additional canvases to multitrack video output - #10635

Closed
palana wants to merge 8 commits into
obsproject:masterfrom
palana:ruwen/vertical-canvas-integration
Closed

palana wants to merge 8 commits into
obsproject:masterfrom
palana:ruwen/vertical-canvas-integration

Conversation

@palana

@palana palana commented May 3, 2024 •

Copy link
Copy Markdown
Contributor

Description

Adds frontend API calls to allow plugins to register additional canvases/video outputs that will participate in multitrack video configuration generation and multitrack video streams

Motivation and Context

Currently additional views/canvases have to use separate rtmp connections or similar to be streamed, where the multitrack video output allows both multiple renditions of the same view and also different views to be streamed

How Has This Been Tested?

Twitch Enhanced Broadcasting beta

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.

@palana
palana force-pushed the ruwen/vertical-canvas-integration branch 2 times, most recently from ce1b263 to c03b4fb Compare May 3, 2024 16:22
@RytoEX RytoEX self-assigned this May 3, 2024
@WizardCM WizardCM added the kind/feature Functionality or other elements that the project doesn't currently have. label May 18, 2024
@palana
palana force-pushed the ruwen/vertical-canvas-integration branch 2 times, most recently from 6dcd68c to d404b1b Compare June 5, 2024 12:55
@RytoEX RytoEX added this to the OBS Studio 31 milestone Jun 19, 2024
@palana
palana force-pushed the ruwen/vertical-canvas-integration branch 2 times, most recently from 420b8d7 to 60f2951 Compare July 16, 2024 13:32
@RytoEX

RytoEX commented Jul 29, 2024

Copy link
Copy Markdown
Member

#10633 was merged some time ago. This will need to be rebased and get CI to pass.

@palana
palana force-pushed the ruwen/vertical-canvas-integration branch from 60f2951 to 7c05363 Compare August 1, 2024 11:11
@palana

palana commented Aug 1, 2024

Copy link
Copy Markdown
Contributor Author

#10633 was merged some time ago. This will need to be rebased and get CI to pass.

@RytoEX: rebased and updated the description

@RytoEX

RytoEX commented Aug 6, 2024

Copy link
Copy Markdown
Member

@palana It looks like the build failures are still present.

@palana
palana force-pushed the ruwen/vertical-canvas-integration branch from 7c05363 to 8dffc0a Compare August 7, 2024 14:59
@palana

palana commented Aug 7, 2024

Copy link
Copy Markdown
Contributor Author

@palana It looks like the build failures are still present.

@RytoEX: oh, yea, missed the bit about making CI pass; latest push should make it pass 😅

@RytoEX

RytoEX commented Aug 7, 2024

Copy link
Copy Markdown
Member

@tt2468 @derrod Any feedback on this?

@RytoEX
RytoEX requested review from derrod and tt2468 August 8, 2024 20:15
Comment thread docs/sphinx/reference-frontend-api.rst Resolved
Comment thread UI/window-basic-main.cpp Resolved
Comment thread UI/window-basic-main.cpp Outdated
}

const std::vector<MultitrackVideoViewInfo> &
OBSBasic::GetAdditionalMultitrackVideoViews()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does nothing but return a copy of the value. Probably fine to fetch the value directly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I follow, this returns a const reference, rather than a copy (i.e. you're allowed to read entries, but not modify them)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tt2468 Could you clarify the concern here?

}
obs_video_info *ovi = &ovi_storage;

const char *view_name = nullptr;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably safer as std::string.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

safer in what way? encoder_config can't be modified in here (unless someone adds a const cast?), so the pointer (once assigned) stays valid for this scope at least (and it's not used outside of this scope)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tt2468 Could you clarify the concern here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a cursory glance there are multiple reasons:

  1. This is C++ code. By default one should use C++ types and avoid low-level C types at all costs.
  2. The view_name is derived from encoder_config.view, which is a std::string already. So the code taps into the internal data of that string instead of using a reference to the original std::string, particularly as it is only used for std::map::find anyway.

It's precisely the kind of code style that the C++ committee and the language designers tell developers to avoid. Use C++ types and avoid pointers and C types at all costs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PatTheMav: can you help me translate what you're suggesting? https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f16-for-in-parameters-pass-cheaply-copied-types-by-value-and-others-by-reference-to-const even lists pointers as first option when passing an optional argument into a function (c++ doesn't really support rust's Option<&str> or similar unfortunately)

so another option would be to do special handling for empty strings, instead of requiring special handling on a different layer (c pointers, and generally everything c++ is kind of terrible for this unfortunately, since most of these won't noisily fail when you use them wrong, and the compiler also doesn't help you due to lack of borrow checking); if that's the preference here I can make that change

or are you asking for this to be translated into std::string *view_name = nullptr; instead?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root cause of this is that the view name is optional in VideoEncoderConfiguration, but I wonder if this has to be the case:

If OBS is supposed to support multiple views and views carry a name, then the main view should also hold a name and not be identified by "the lack of any name", which is brittle.

So instead the requirement could be: OBS can have multiple views, every view needs to have a unique name, including the main view. This would also allow everyone working with views to clearly identify the views and gives OBS itself the ability to "reserve" view names (and there are already ternary expressions trying to use the fallback name "obs_base", so there clearly is a requirement for the main/base view to have a name).

That also has the potential to solve the following issues:

  • adjust_video_encoder_scaling doesn't have to accept a mish-mash of actual values or nullptrs and use ternary expressions to get some video dimensions or view name from something else.
  • In the same vein adjust_video_encoder_scaling's argument list wouldn't need to mix mandatory and logically optional arguments instead of following the good practice of putting mandatory arguments first and optional ones last

So there should be a single list of views to which the "main view" is added and additional views can be added by the frontend API. Then the code could work with the expectation that:

  • The list contains at least a single view (not having any views would be an invalid application state)
  • Every view (including the main view) carries a name and the same metadata and video info

This is all complex stuff and I appreciate the work put into this, but that's even more reason to make it as straightforward as possible and avoid a pile of little workarounds here, there, and everywhere to squeeze in this functionality.

Comment thread docs/sphinx/reference-frontend-api.rst Resolved
@palana
palana force-pushed the ruwen/vertical-canvas-integration branch 2 times, most recently from dabc2f6 to 212f177 Compare September 17, 2024 12:30
@palana
palana force-pushed the ruwen/vertical-canvas-integration branch from 212f177 to 2b549f8 Compare October 8, 2024 15:47
@RytoEX
RytoEX requested a review from tt2468 November 18, 2024 20:39
Comment thread UI/goliveapi-postdata.cpp
}

if (!extra_views.empty()) {
auto &extra_views_capability = *post_data.capabilities.extra_views;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm missing something, but extra_views is of type optional<std::vector<ExtraView>> and operator* is an unchecked access to the underlying data. By this point in code the optional should have a disengaged value and operator* crashes.

Is there code that explicitly initialises the std::vector in the optional that I missed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, seems like an accident that it currently works as is (as in, an empty vector looks the same as whatever contents the optional gets default-initialized with?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 6bc5f21

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I wondered the same (particularly if the declaration leads to a default initialisation of the wrapped std::vector) but at least this small reproducer crashes at operator*:

#include <iostream>
#include <optional>
#include <vector>
#include <string>

struct Test {
    std::optional<std::vector<std::string>> optionals;
};

int main(int argc, const char * argv[]) {
    Test test;
    
    auto &some = *test.optionals;
    some.reserve(10);
    
    some.push_back("Hello");
    
    for (auto &opt : some) {
        std::cout << opt.data() << "\n";
    }
    
    
    return 0;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unfortunately too late to the party, but function signatures like this:

static OBSOutputs SetupOBSOutput(QWidget *parent, const QString &multitrack_video_name,
				 obs_data_t *dump_stream_to_file_config, const GoLiveApi::Config &go_live_config,
				 std::vector<OBSEncoderAutoRelease> &audio_encoders,
				 std::shared_ptr<obs_encoder_group_t> &video_encoder_group,
				 const char *audio_encoder_id, size_t main_audio_mixer,
				 std::optional<size_t> vod_track_mixer,
				 const std::map<std::string, video_t *> &extra_views);

or this:

void MultitrackVideoOutput::PrepareStreaming(
	QWidget *parent, const char *service_name, obs_service_t *service, const std::optional<std::string> &rtmp_url,
	const QString &stream_key, const char *audio_encoder_id, std::optional<uint32_t> maximum_aggregate_bitrate,
	std::optional<uint32_t> maximum_video_tracks, std::optional<std::string> custom_config,
	obs_data_t *dump_stream_to_file_config, size_t main_audio_mixer, std::optional<size_t> vod_track_mixer)

make it already visually obvious that we're doing something wrong here.

How many of these fields are actually invariant? Because if they are (i.e. only in combination do they constitute the minimally required data necessary for the function to do its job) they should've been combined in a single "object" (aka a struct), which would also potentially decouple the order of the arguments and fix the issue of mandatory arguments being mixed in with optional arguments.

Right now, these functions create unreasonable high barriers for other maintainers to parse properly because it's just visual (and logical) noise. And just piling on even more optional arguments (which are not truly "optional" but still require a nullopt) makes it worse.

video_t *video = video_output.start_video(video_output.name.c_str(), video_output.param);
if (!video)
continue;
extra_views->views_[video_output.name] = video;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the use of the subscript operator intended here? extra_views is initialised just before, so it should hold no pre-existing values and the subscript operator is doing a whole lot more things then what an emplace would do:

It first searches the map for the given key, inserts a default-constructed value if not found, returns a reference to that value, to which you then assign video.

Maybe that's desired, but I usually advise to avoid operator[] when just inserting elements into a map.

}
obs_video_info *ovi = &ovi_storage;

const char *view_name = nullptr;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root cause of this is that the view name is optional in VideoEncoderConfiguration, but I wonder if this has to be the case:

If OBS is supposed to support multiple views and views carry a name, then the main view should also hold a name and not be identified by "the lack of any name", which is brittle.

So instead the requirement could be: OBS can have multiple views, every view needs to have a unique name, including the main view. This would also allow everyone working with views to clearly identify the views and gives OBS itself the ability to "reserve" view names (and there are already ternary expressions trying to use the fallback name "obs_base", so there clearly is a requirement for the main/base view to have a name).

That also has the potential to solve the following issues:

  • adjust_video_encoder_scaling doesn't have to accept a mish-mash of actual values or nullptrs and use ternary expressions to get some video dimensions or view name from something else.
  • In the same vein adjust_video_encoder_scaling's argument list wouldn't need to mix mandatory and logically optional arguments instead of following the good practice of putting mandatory arguments first and optional ones last

So there should be a single list of views to which the "main view" is added and additional views can be added by the frontend API. Then the code could work with the expectation that:

  • The list contains at least a single view (not having any views would be an invalid application state)
  • Every view (including the main view) carries a name and the same metadata and video info

This is all complex stuff and I appreciate the work put into this, but that's even more reason to make it as straightforward as possible and avoid a pile of little workarounds here, there, and everywhere to squeeze in this functionality.

Comment thread UI/api-interface.cpp
}
}

void obs_frontend_multitrack_video_register(const char *name,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two questions about this:

  • Is this implemented in the frontend API because the frontend is nominally the part of the program that manages views and outputs (that'd be unfortunate as it violates what the word "frontend" implies, but that'd be an issue with OBS' internal architecture then)
  • Is there anything about this functionality that couples it with the multitrack feature? Because if it doesn't, then I'd rather see this use a more generic term.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its only purpose is to enable views to participate in multitrack streaming, at least I'm not aware of any other functionality that would make use of this right now

upcoming webrtc multitrack (and other/future multitrack outputs) might be interested in this at some point, but so far webrtc multitrack is stuck in review

doing this in libobs-land is somewhat very uncharted territory, since views/video outputs/general obs objects don't carry meta information about their intended purpose (and given the private data capabilities, they're currently very much in "don't touch these objects unless you know where they come from/why they're there" territory)

initialize `vector` inside `optional` before using it
@dsaedtler dsaedtler mentioned this pull request Feb 4, 2025
6 tasks done
@RytoEX

RytoEX commented Mar 12, 2025

Copy link
Copy Markdown
Member

@RytoEX RytoEX closed this Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/feature Functionality or other elements that the project doesn't currently have.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants