Skip to content

Commit

Permalink
Add setPackageName and setAttributionTag to the Oboe builder (#1332)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertwu1 authored Jul 14, 2021
1 parent 75ccaa3 commit 4fa162b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 1 deletion.
6 changes: 6 additions & 0 deletions include/oboe/AudioStreamBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define OBOE_STREAM_BASE_H_

#include <memory>
#include <string>
#include "oboe/AudioStreamCallback.h"
#include "oboe/Definitions.h"

Expand Down Expand Up @@ -217,6 +218,11 @@ class AudioStreamBase {
/** Stream session ID allocation strategy. Only active on Android 28+ */
SessionId mSessionId = SessionId::None;

/** Control the name of the package creating the stream. Only active on Android 31+ */
std::string mPackageName;
/** Control the attribution tag of the context creating the stream. Only active on Android 31+ */
std::string mAttributionTag;

// Control whether Oboe can convert channel counts to achieve optimal results.
bool mChannelConversionAllowed = false;
// Control whether Oboe can convert data formats to achieve optimal results.
Expand Down
32 changes: 32 additions & 0 deletions include/oboe/AudioStreamBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,38 @@ class AudioStreamBuilder : public AudioStreamBase {
return this;
}

/**
* Declare the name of the package creating the stream.
*
* This is usually Context#getPackageName()
*
* The default, if you do not call this function, is a random package in the calling uid.
*
* Added in API level 31.
*
* @param packageName packageName of the calling app.
*/
AudioStreamBuilder *setPackageName(std::string packageName) {
mPackageName = packageName;
return this;
}

/**
* Declare the attribution tag of the context creating the stream.
*
* This is usually Context#getAttributionTag()
*
* The default, if you do not call this function, is the default attribution tag.
*
* Added in API level 31.
*
* @param attributionTag attributionTag of the calling context.
*/
AudioStreamBuilder *setAttributionTag(std::string attributionTag) {
mAttributionTag = attributionTag;
return this;
}

/**
* @return true if AAudio will be used based on the current settings.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/aaudio/AAudioLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ int AAudioLoader::open() {
builder_setSessionId = load_V_PBI("AAudioStreamBuilder_setSessionId");
}

if (getSdkVersion() >= __ANDROID_API_S__){
builder_setPackageName = load_V_PBCPH("AAudioStreamBuilder_setPackageName");
builder_setAttributionTag = load_V_PBCPH("AAudioStreamBuilder_setAttributionTag");
}

builder_delete = load_I_PB("AAudioStreamBuilder_delete");


Expand Down Expand Up @@ -160,6 +165,12 @@ AAudioLoader::signature_V_PBI AAudioLoader::load_V_PBI(const char *functionName)
return reinterpret_cast<signature_V_PBI>(proc);
}

AAudioLoader::signature_V_PBCPH AAudioLoader::load_V_PBCPH(const char *functionName) {
void *proc = dlsym(mLibHandle, functionName);
AAudioLoader_check(proc, functionName);
return reinterpret_cast<signature_V_PBCPH>(proc);
}

AAudioLoader::signature_V_PBPDPV AAudioLoader::load_V_PBPDPV(const char *functionName) {
void *proc = dlsym(mLibHandle, functionName);
AAudioLoader_check(proc, functionName);
Expand Down
10 changes: 10 additions & 0 deletions src/aaudio/AAudioLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ typedef int32_t aaudio_session_id_t;
#define __NDK_MAJOR__ 0
#endif

#ifndef __ANDROID_API_S__
#define __ANDROID_API_S__ 31
#endif

namespace oboe {

/**
Expand Down Expand Up @@ -98,6 +102,8 @@ class AAudioLoader {
// AAudioStreamBuilder_setSampleRate()
typedef void (*signature_V_PBI)(AAudioStreamBuilder *, int32_t);

typedef void (*signature_V_PBCPH)(AAudioStreamBuilder *, const char *);

typedef int32_t (*signature_I_PS)(AAudioStream *); // AAudioStream_getSampleRate()
typedef int64_t (*signature_L_PS)(AAudioStream *); // AAudioStream_getFramesRead()
// AAudioStream_setBufferSizeInFrames()
Expand Down Expand Up @@ -160,6 +166,9 @@ class AAudioLoader {
signature_V_PBI builder_setInputPreset = nullptr;
signature_V_PBI builder_setSessionId = nullptr;

signature_V_PBCPH builder_setPackageName = nullptr;
signature_V_PBCPH builder_setAttributionTag = nullptr;

signature_V_PBPDPV builder_setDataCallback = nullptr;
signature_V_PBPEPV builder_setErrorCallback = nullptr;

Expand Down Expand Up @@ -212,6 +221,7 @@ class AAudioLoader {
signature_I_PPB load_I_PPB(const char *name);
signature_CPH_I load_CPH_I(const char *name);
signature_V_PBI load_V_PBI(const char *name);
signature_V_PBCPH load_V_PBCPH(const char *name);
signature_V_PBPDPV load_V_PBPDPV(const char *name);
signature_V_PBPEPV load_V_PBPEPV(const char *name);
signature_I_PB load_I_PB(const char *name);
Expand Down
11 changes: 10 additions & 1 deletion src/aaudio/AudioStreamAAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,16 @@ Result AudioStreamAAudio::open() {
static_cast<aaudio_session_id_t>(mSessionId));
}

// TODO get more parameters from the builder?
// These were added in S so we have to check for the function pointer.
if (mLibLoader->builder_setPackageName != nullptr && !mPackageName.empty()) {
mLibLoader->builder_setPackageName(aaudioBuilder,
mPackageName.c_str());
}

if (mLibLoader->builder_setAttributionTag != nullptr && !mAttributionTag.empty()) {
mLibLoader->builder_setAttributionTag(aaudioBuilder,
mAttributionTag.c_str());
}

if (isDataCallbackSpecified()) {
mLibLoader->builder_setDataCallback(aaudioBuilder, oboe_aaudio_data_callback_proc, this);
Expand Down
18 changes: 18 additions & 0 deletions tests/testStreamOpen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,21 @@ TEST_F(StreamOpenOutput, AAudioOutputSampleRate44100) {
TEST_F(StreamOpenInput, AAudioInputSampleRate44100) {
checkSampleRateConversionAdvancing(Direction::Input);
}

TEST_F(StreamOpenOutput, AAudioSetPackageName){
if (getSdkVersion() >= __ANDROID_API_S__){
mBuilder.setAudioApi(AudioApi::AAudio);
mBuilder.setPackageName("TestSetPackageName");
ASSERT_TRUE(openStream());
ASSERT_TRUE(closeStream());
}
}

TEST_F(StreamOpenOutput, AAudioSetAttributionTag){
if (getSdkVersion() >= __ANDROID_API_S__){
mBuilder.setAudioApi(AudioApi::AAudio);
mBuilder.setAttributionTag("TestSetAttributionTag");
ASSERT_TRUE(openStream());
ASSERT_TRUE(closeStream());
}
}

0 comments on commit 4fa162b

Please sign in to comment.