Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT(plugin-api): Add function to mumble api to obtain current positional audio data provided by another plugin #6556

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

JordanPlayz158
Copy link

This is a draft PR (not in a working state), the reason for the expansion is to allow for plugins that perform changes to the users settings (configurable) in response to context or identity information provided by positional audio games (without needing for that plugin to potentially re-do fetching work). One such example is a plugin for gmod that when playing TTT and you are spectator, allowing you to hear everyone (disabling user setting positional audio, will have the last user set state saved and set back) and then when back alive, re-enabling positional audio.

Checks

…sn't match as it doesn't copy/write the data to `void *positionalData` pointer as when looking up cloning, wasn't sure how to achieve it.
@JordanPlayz158 JordanPlayz158 changed the title [DRAFT] Add function to mumble api to obtain current positional audio data provided by another plugin [DRAFT] FEAT(client,plugin-api) Add function to mumble api to obtain current positional audio data provided by another plugin Sep 19, 2024
@JordanPlayz158 JordanPlayz158 changed the title [DRAFT] FEAT(client,plugin-api) Add function to mumble api to obtain current positional audio data provided by another plugin [DRAFT] FEAT(plugin-api) Add function to mumble api to obtain current positional audio data provided by another plugin Sep 19, 2024
@Krzmbrzl Krzmbrzl marked this pull request as draft September 19, 2024 04:54
plugins/MumblePlugin.h Outdated Show resolved Hide resolved
src/mumble/API_v_1_x_x.cpp Outdated Show resolved Hide resolved
src/mumble/API_v_1_x_x.cpp Outdated Show resolved Hide resolved
src/mumble/API_v_1_x_x.cpp Outdated Show resolved Hide resolved
@JordanPlayz158
Copy link
Author

JordanPlayz158 commented Sep 19, 2024

Will squash and conform to git/commit guidelines once the PR is deemed ready

Only not ideal thing currently which will either need to be changed or mentioned in the docs is each plugin that grabs the positional data via that function WILL be responsible for freeing the struct they made (which is implied) but ALSO before that, the positionalData->m_context and positionalData->m_identity fields, unless free is smart and notices the malloc'd memory and frees any fields inside the struct that were also malloc'd (which I would assume it wouldn't be aware of)

With some brief testing, the 2 strcpy's of context and identity may be redundant, if it is confirmed to be, that is great and I will push the code with the strcpy's removed

@davidebeatrici
Copy link
Member

davidebeatrici commented Sep 19, 2024

unless free is smart and notices the malloc'd memory and frees any fields inside the struct that were also malloc'd (which I would assume it wouldn't be aware of)

It doesn't, in fact the usual approach is to write a specific function that takes care of freeing the members before the struct itself. Something like this:

void free_struct(Struct *obj) {
	if (obj) {
		free(obj->var1);
		free(obj->var2);
	}

	free(obj);
}

With some brief testing, the 2 strcpy's of context and identity may be redundant, if it is confirmed to be, that is great and I will push the code with the strcpy's removed

I'm assuming you're referring to this piece of code:

char *context = posData.getContext().toUtf8().data();
positionalDataNoQt.m_context = (char *) malloc(strlen(context) + 1);
std::strcpy(positionalDataNoQt.m_context, context);

char *identity = posData.getPlayerIdentity().toUtf8().data();
positionalDataNoQt.m_identity = (char *) malloc(strlen(identity) + 1);
std::strcpy(positionalDataNoQt.m_identity, identity);

Copying the string's data is mandatory, otherwise your context and identity variables point to uninitialized data.

However, you can replace the strcpy() calls with memcpy() ones since you already know the length of the strings, which is better in terms of performance:

char *context = posData.getContext().toUtf8().data();
size_t size = strlen(context) + 1;
positionalDataNoQt.m_context = (char *) malloc(size);
std::memcpy(positionalDataNoQt.m_context, context, size);

char *identity = posData.getPlayerIdentity().toUtf8().data();
size = strlen(identity) + 1;
positionalDataNoQt.m_identity = (char *) malloc(size);
std::memcpy(positionalDataNoQt.m_identity, identity, size);

One more thing: C-style casts are allowed in C++, but it's recommended to use the C++-style ones instead:

positionalDataNoQt.m_context = static_cast< char * >(malloc(size));
positionalDataNoQt.m_identity = static_cast< char * >(malloc(size));

…h as mentioned in code comment, could not get C++ destructor working
@JordanPlayz158
Copy link
Author

I have done the suggestions from david except couldn't get the C++ destructor working but other than that, any other suggestions before making it no longer a draft (other than (probably) coming up with a better name for the struct than PositionalDataNoQt)?

@JordanPlayz158 JordanPlayz158 marked this pull request as ready for review September 29, 2024 23:02
@JordanPlayz158
Copy link
Author

Did not know that marking ready for review removed it from the draft stage but I guess it is fine, I do think most of the things are ready.

@@ -444,6 +444,42 @@ struct MumbleStringWrapper {
bool needsReleasing;
};

struct PositionalDataNoQt;
Copy link
Member

Choose a reason for hiding this comment

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

This needs a different name

@@ -444,6 +444,42 @@ struct MumbleStringWrapper {
bool needsReleasing;
};

struct PositionalDataNoQt;

static void freePositionalDataStruct(PositionalDataNoQt *positionalData);
Copy link
Member

Choose a reason for hiding this comment

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

We don't need this.

* @since Plugin interface v1.3.0
*/
mumble_error_t(MUMBLE_PLUGIN_CALLING_CONVENTION *getPositionalAudioData)(mumble_plugin_id_t callerID,
PositionalDataNoQt **outPositionalData);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
PositionalDataNoQt **outPositionalData);
PositionalDataNoQt *outPositionalData);

No need to allocate the passed struct on the heap (using e.g. new). The only thing required is that the plugin allocates some space for this struct (by creating an instance on the Stack) and then the Mumble side can simply populate the fields of that struct with the correct data.

*
* @since Plugin interface v1.3.0
*/
mumble_error_t(MUMBLE_PLUGIN_CALLING_CONVENTION *getPositionalAudioData)(mumble_plugin_id_t callerID,
Copy link
Member

Choose a reason for hiding this comment

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

I would turn this into multiple functions: One for retrieving the positional data, one for retrieving the identity and one for retrieving the context. Only the latter two involve dynamic memory allocations due to the involvement of strings (for which you should use the existing string wrapper classes).

@@ -1515,6 +1551,21 @@ struct MUMBLE_API_STRUCT_NAME {
mumble_channelid_t channelID,
const char **description);

#if SELECTED_API_VERSION >= MUMBLE_PLUGIN_VERSION_CHECK(1, 3, 0)
/**
* Gets the positional audio data provided by OTHER plugins
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Gets the positional audio data provided by OTHER plugins
* Gets the currently used positional data

which plugin has supplied the data is left unspecified. It's not like this function won't work if the positional data has been supplied by the same plugin that now asks for it.

Copy link
Author

Choose a reason for hiding this comment

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

Oh yeah true, your wording is better and conveys what I wanted to get across, made other in caps to convey it is not like the similarly named (imo it is similar names) fetchPositionalData (in my brain, get and fetch are similar, now get and put are more distinct imo but I see no reason to change the existing name of api functions when we can just be clear in the doc comments)

@Krzmbrzl
Copy link
Member

For reference, the way I think the API should look like from the plugin's perspective is something like this:

DataStruct posData;
error_t retCode = api.getPositionalData(id, &posData);

@Krzmbrzl Krzmbrzl changed the title [DRAFT] FEAT(plugin-api) Add function to mumble api to obtain current positional audio data provided by another plugin FEAT(plugin-api): Add function to mumble api to obtain current positional audio data provided by another plugin Sep 30, 2024
@Krzmbrzl Krzmbrzl marked this pull request as draft September 30, 2024 15:57
@Hartmnt Hartmnt added client positional audio feature-request This issue or PR deals with a new feature labels Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client feature-request This issue or PR deals with a new feature positional audio
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants