Skip to content

Commit 4c43037

Browse files
committed
Extract threading logic to EffectThread and other changes
1 parent 90773bb commit 4c43037

File tree

7 files changed

+418
-74
lines changed

7 files changed

+418
-74
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ set(FILES
161161
src/shim.cpp
162162

163163
# Main
164+
src/aidl/EffectThread.cpp
165+
164166
src/viper/ViPER.cpp
165167
src/ViPER4Android.cpp
166168
src/ViperContext.cpp

src/aidl/EffectThread.cpp

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright (C) 2022 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <cstddef>
18+
#include <memory>
19+
20+
#define LOG_TAG "AHAL_EffectThread"
21+
#include <log/log.h>
22+
#include <pthread.h>
23+
#include <sys/resource.h>
24+
25+
#include "EffectThread.h"
26+
#include "EffectTypes.h"
27+
28+
namespace aidl::android::hardware::audio::effect {
29+
30+
EffectThread::~EffectThread() {
31+
destroyThread();
32+
}
33+
34+
RetCode EffectThread::createThread(const std::string& name, int priority) {
35+
if (mThread.joinable()) {
36+
ALOGW("%s %s thread already created, no-op", mName.c_str(), __func__);
37+
return RetCode::SUCCESS;
38+
}
39+
40+
mName = name;
41+
mPriority = priority;
42+
{
43+
std::lock_guard lg(mThreadMutex);
44+
mStop = true;
45+
mExit = false;
46+
}
47+
48+
mThread = std::thread(&EffectThread::threadLoop, this);
49+
ALOGV("%s %s priority %d done", mName.c_str(), __func__, mPriority);
50+
return RetCode::SUCCESS;
51+
}
52+
53+
RetCode EffectThread::destroyThread() {
54+
{
55+
std::lock_guard lg(mThreadMutex);
56+
mStop = mExit = true;
57+
}
58+
59+
mCv.notify_one();
60+
if (mThread.joinable()) {
61+
mThread.join();
62+
}
63+
64+
ALOGV("%s", mName.c_str());
65+
return RetCode::SUCCESS;
66+
}
67+
68+
RetCode EffectThread::startThread() {
69+
{
70+
std::lock_guard lg(mThreadMutex);
71+
if (mDraining) {
72+
mDraining = false;
73+
} else {
74+
mStop = false;
75+
}
76+
mCv.notify_one();
77+
}
78+
79+
ALOGV("%s", mName.c_str());
80+
return RetCode::SUCCESS;
81+
}
82+
83+
RetCode EffectThread::stopThread() {
84+
{
85+
std::lock_guard lg(mThreadMutex);
86+
mStop = true;
87+
mCv.notify_one();
88+
}
89+
90+
ALOGV("%s", mName.c_str());
91+
return RetCode::SUCCESS;
92+
}
93+
94+
RetCode EffectThread::startDraining() {
95+
std::lock_guard lg(mThreadMutex);
96+
mDraining = true;
97+
mCv.notify_one();
98+
99+
ALOGV("%s", mName.c_str());
100+
return RetCode::SUCCESS;
101+
}
102+
103+
RetCode EffectThread::finishDraining() {
104+
std::lock_guard lg(mThreadMutex);
105+
mDraining = false;
106+
mStop = true;
107+
mCv.notify_one();
108+
109+
ALOGV("%s", mName.c_str());
110+
return RetCode::SUCCESS;
111+
}
112+
113+
void EffectThread::threadLoop() {
114+
pthread_setname_np(pthread_self(), mName.substr(0, kMaxTaskNameLen - 1).c_str());
115+
setpriority(PRIO_PROCESS, 0, mPriority);
116+
while (true) {
117+
{
118+
std::unique_lock l(mThreadMutex);
119+
::android::base::ScopedLockAssertion lock_assertion(mThreadMutex);
120+
mCv.wait(l, [&]() REQUIRES(mThreadMutex) { return mExit || !mStop; });
121+
if (mExit) {
122+
ALOGV("%s threadLoop EXIT!", mName.c_str());
123+
return;
124+
}
125+
}
126+
process();
127+
}
128+
}
129+
130+
} // namespace aidl::android::hardware::audio::effect

src/aidl/EffectThread.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (C) 2022 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
#include <atomic>
19+
#include <memory>
20+
#include <string>
21+
#include <thread>
22+
23+
#include <android-base/thread_annotations.h>
24+
#include <fmq/EventFlag.h>
25+
#include <system/thread_defs.h>
26+
27+
//#include "EffectContext.h"
28+
#include "EffectTypes.h"
29+
30+
namespace aidl::android::hardware::audio::effect {
31+
32+
class EffectThread {
33+
public:
34+
virtual ~EffectThread();
35+
36+
// called by effect implementation
37+
RetCode createThread(const std::string& name, int priority = ANDROID_PRIORITY_URGENT_AUDIO);
38+
RetCode destroyThread();
39+
RetCode startThread();
40+
RetCode stopThread();
41+
RetCode startDraining();
42+
RetCode finishDraining();
43+
44+
// Will call process() in a loop if the thread is running.
45+
void threadLoop();
46+
47+
/**
48+
* process() call effectProcessImpl() for effect data processing, it is necessary for the
49+
* processing to be called under Effect thread mutex mThreadMutex, to avoid the effect state
50+
* change before/during data processing, and keep the thread and effect state consistent.
51+
*/
52+
virtual void process() = 0;
53+
54+
protected:
55+
bool mDraining GUARDED_BY(mThreadMutex) = false;
56+
57+
private:
58+
static constexpr int kMaxTaskNameLen = 15;
59+
60+
std::mutex mThreadMutex;
61+
std::condition_variable mCv;
62+
bool mStop GUARDED_BY(mThreadMutex) = true;
63+
bool mExit GUARDED_BY(mThreadMutex) = false;
64+
65+
std::thread mThread;
66+
int mPriority;
67+
std::string mName;
68+
};
69+
} // namespace aidl::android::hardware::audio::effect

src/aidl/EffectTypes.h

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright (C) 2022 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
#include <string>
19+
20+
#include <aidl/android/hardware/audio/effect/BnEffect.h>
21+
#include <aidl/android/hardware/audio/effect/Range.h>
22+
#include <android-base/logging.h>
23+
//#include <system/audio_effects/aidl_effects_utils.h>
24+
25+
typedef binder_exception_t (*EffectCreateFunctor)(
26+
const ::aidl::android::media::audio::common::AudioUuid*,
27+
std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>*);
28+
typedef binder_exception_t (*EffectDestroyFunctor)(
29+
const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>&);
30+
typedef binder_exception_t (*EffectQueryFunctor)(
31+
const ::aidl::android::media::audio::common::AudioUuid*,
32+
::aidl::android::hardware::audio::effect::Descriptor*);
33+
34+
struct effect_dl_interface_s {
35+
EffectCreateFunctor createEffectFunc;
36+
EffectDestroyFunctor destroyEffectFunc;
37+
EffectQueryFunctor queryEffectFunc;
38+
};
39+
40+
namespace aidl::android::hardware::audio::effect {
41+
42+
enum class RetCode {
43+
SUCCESS,
44+
ERROR_ILLEGAL_PARAMETER, /* Illegal parameter */
45+
ERROR_THREAD, /* Effect thread error */
46+
ERROR_NULL_POINTER, /* NULL pointer */
47+
ERROR_ALIGNMENT_ERROR, /* Memory alignment error */
48+
ERROR_BLOCK_SIZE_EXCEED, /* Maximum block size exceeded */
49+
ERROR_EFFECT_LIB_ERROR, /* Effect implementation library error */
50+
ERROR_EVENT_FLAG_ERROR /* Error with effect event flags */
51+
};
52+
53+
static const int INVALID_AUDIO_SESSION_ID = -1;
54+
55+
inline std::ostream& operator<<(std::ostream& out, const RetCode& code) {
56+
switch (code) {
57+
case RetCode::SUCCESS:
58+
return out << "SUCCESS";
59+
case RetCode::ERROR_ILLEGAL_PARAMETER:
60+
return out << "ERROR_ILLEGAL_PARAMETER";
61+
case RetCode::ERROR_THREAD:
62+
return out << "ERROR_THREAD";
63+
case RetCode::ERROR_NULL_POINTER:
64+
return out << "ERROR_NULL_POINTER";
65+
case RetCode::ERROR_ALIGNMENT_ERROR:
66+
return out << "ERROR_ALIGNMENT_ERROR";
67+
case RetCode::ERROR_BLOCK_SIZE_EXCEED:
68+
return out << "ERROR_BLOCK_SIZE_EXCEED";
69+
case RetCode::ERROR_EFFECT_LIB_ERROR:
70+
return out << "ERROR_EFFECT_LIB_ERROR";
71+
case RetCode::ERROR_EVENT_FLAG_ERROR:
72+
return out << "ERROR_EVENT_FLAG_ERROR";
73+
}
74+
75+
return out << "EnumError: " << code;
76+
}
77+
78+
#define RETURN_IF_ASTATUS_NOT_OK(status, message) \
79+
do { \
80+
const ::ndk::ScopedAStatus curr_status = (status); \
81+
if (!curr_status.isOk()) { \
82+
LOG(ERROR) << __func__ << ": line" << __LINE__ \
83+
<< " return with status: " << curr_status.getDescription() << (message); \
84+
return ndk::ScopedAStatus::fromExceptionCodeWithMessage( \
85+
curr_status.getExceptionCode(), (message)); \
86+
} \
87+
} while (0)
88+
89+
#define RETURN_IF(expr, exception, message) \
90+
do { \
91+
if (expr) { \
92+
LOG(ERROR) << __func__ << ": line" << __LINE__ << " return with expr " << #expr; \
93+
return ndk::ScopedAStatus::fromExceptionCodeWithMessage((exception), (message)); \
94+
} \
95+
} while (0)
96+
97+
#define RETURN_OK_IF(expr) \
98+
do { \
99+
if (expr) { \
100+
LOG(INFO) << __func__ << ": line" << __LINE__ << " return with expr " << #expr; \
101+
return ndk::ScopedAStatus::ok(); \
102+
} \
103+
} while (0)
104+
105+
#define RETURN_VALUE_IF(expr, ret, log) \
106+
do { \
107+
if (expr) { \
108+
LOG(ERROR) << __func__ << ": line" << __LINE__ << " return with expr \"" << #expr \
109+
<< "\":" << (log); \
110+
return ret; \
111+
} \
112+
} while (0)
113+
114+
#define RETURN_IF_BINDER_EXCEPTION(functor) \
115+
{ \
116+
binder_exception_t exception = functor; \
117+
if (EX_NONE != exception) { \
118+
LOG(ERROR) << #functor << ": failed with error " << exception; \
119+
return ndk::ScopedAStatus::fromExceptionCode(exception); \
120+
} \
121+
}
122+
123+
/**
124+
* Make a Range::$EffectType$Range.
125+
* T: The $EffectType$, Visualizer for example.
126+
* Tag: The union tag name in $EffectType$ definition, latencyMs for example.
127+
* l: The value of Range::$EffectType$Range.min.
128+
* r: The value of Range::$EffectType$Range.max.
129+
*/
130+
#define MAKE_RANGE(T, Tag, l, r) \
131+
{ .min = T::make<T::Tag>(l), .max = T::make<T::Tag>(r) }
132+
133+
} // namespace aidl::android::hardware::audio::effect

src/shim.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
#include <string>
2+
#include <log/log.h>
23

34
namespace android::hardware::details {
45
void check(bool exp) {
5-
6+
ALOGE_IF(!exp, "Check failed");
67
}
78

89
void check(bool exp, const char* message) {
9-
10+
ALOGE_IF(!exp, "%s", message);
1011
}
1112

1213
void logError(const std::string &message) {
13-
14+
ALOGE("%s", message.c_str());
1415
}
1516

1617
void errorWriteLog(int tag, const char* info) {
17-
18+
ALOGE("%d: %s", tag, info);
1819
}
1920
} // namespace android::hardware::details

0 commit comments

Comments
 (0)