1 /*
2 * Copyright (c) 2025-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef LOG_TAG
16 #define LOG_TAG "AudioPolicyAsyncActionHandler"
17 #endif
18
19 #include "audio_policy_async_action_handler.h"
20 #include "audio_policy_log.h"
21
22 namespace OHOS {
23 namespace AudioStandard {
24
25 enum {
26 DEFAULT_EVENT_ID = 0,
27 };
28
AudioPolicyAsyncActionHandler()29 AudioPolicyAsyncActionHandler::AudioPolicyAsyncActionHandler() : AppExecFwk::EventHandler(
30 AppExecFwk::EventRunner::Create("OS_APAsyncActionHandler", AppExecFwk::ThreadMode::FFRT))
31 {
32 AUDIO_DEBUG_LOG("ctor");
33 }
34
~AudioPolicyAsyncActionHandler()35 AudioPolicyAsyncActionHandler::~AudioPolicyAsyncActionHandler()
36 {
37 AUDIO_WARNING_LOG("dtor should not happen");
38 };
39
PostAsyncAction(const AsyncActionDesc & desc)40 bool AudioPolicyAsyncActionHandler::PostAsyncAction(const AsyncActionDesc &desc)
41 {
42 bool ret = false;
43 AUDIO_INFO_LOG("priority type = %{public}u", desc.priority);
44 switch (desc.priority) {
45 case ActionPriority::IMMEDIATE: {
46 std::lock_guard<std::mutex> lock(actionMutex_);
47 ret = SendImmediateEvent(DEFAULT_EVENT_ID, desc.action);
48 break;
49 }
50 case ActionPriority::HIGH: {
51 std::lock_guard<std::mutex> lock(actionMutex_);
52 ret = SendHighPriorityEvent(AppExecFwk::InnerEvent::Get(DEFAULT_EVENT_ID, desc.action), desc.delayTimeMs);
53 break;
54 }
55 case ActionPriority::LOW: {
56 std::lock_guard<std::mutex> lock(actionMutex_);
57 ret = SendEvent(DEFAULT_EVENT_ID, desc.action, desc.delayTimeMs);
58 break;
59 }
60 default: {
61 AUDIO_ERR_LOG("Unknown priority type = %{public}u", desc.priority);
62 break;
63 }
64 }
65 if (!ret) {
66 CHECK_AND_RETURN_RET_LOG(ret, ret, "PostAsyncAction failed, priority type = %{public}u", desc.priority);
67 }
68 return ret;
69 }
70
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)71 void AudioPolicyAsyncActionHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
72 {
73 CHECK_AND_RETURN_LOG(event != nullptr, "event is nullptr");
74
75 uint32_t eventId = event->GetInnerEventId();
76 std::shared_ptr<PolicyAsyncAction> action = event->GetSharedObject<PolicyAsyncAction>();
77 CHECK_AND_RETURN_LOG(action != nullptr, "action is nullptr");
78 action->Exec();
79 }
80
81 } // namespace AudioStandard
82 } // namespace OHOS
83