• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "OHAudioResourceManager.h"
17 
18 using OHOS::AudioStandard::OHAudioResourceManager;
19 using OHOS::AudioStandard::OHAudioWorkgroup;
20 
convertResourceManager(OH_AudioResourceManager * resourceManager)21 static OHOS::AudioStandard::OHAudioResourceManager *convertResourceManager(OH_AudioResourceManager* resourceManager)
22 {
23     return (OHAudioResourceManager*) resourceManager;
24 }
25 
convertWorkgroup(OH_AudioWorkgroup * group)26 static OHOS::AudioStandard::OHAudioWorkgroup *convertWorkgroup(OH_AudioWorkgroup* group)
27 {
28     return (OHAudioWorkgroup*) group;
29 }
30 
OH_AudioManager_GetAudioResourceManager(OH_AudioResourceManager ** resourceManager)31 OH_AudioCommon_Result OH_AudioManager_GetAudioResourceManager(OH_AudioResourceManager **resourceManager)
32 {
33     if (resourceManager == nullptr) {
34         AUDIO_ERR_LOG("invalid OH_AudioResourceManager");
35         return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
36     }
37     OHAudioResourceManager *audioResourceManager = OHAudioResourceManager::GetInstance();
38     *resourceManager = (OH_AudioResourceManager*)audioResourceManager;
39     return AUDIOCOMMON_RESULT_SUCCESS;
40 }
41 
OH_AudioResourceManager_CreateWorkgroup(OH_AudioResourceManager * resourceManager,const char * name,OH_AudioWorkgroup ** group)42 OH_AudioCommon_Result OH_AudioResourceManager_CreateWorkgroup(OH_AudioResourceManager *resourceManager,
43     const char *name, OH_AudioWorkgroup **group)
44 {
45     if (resourceManager == nullptr || name == nullptr || group == nullptr) {
46         AUDIO_ERR_LOG("invalid create param");
47         return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
48     }
49 
50     *group = (OH_AudioWorkgroup *)(convertResourceManager(resourceManager)->CreateWorkgroup());
51     CHECK_AND_RETURN_RET_LOG(group != nullptr, AUDIOCOMMON_RESULT_ERROR_SYSTEM, "workgroup returned nullptr");
52     return AUDIOCOMMON_RESULT_SUCCESS;
53 }
54 
OH_AudioResourceManager_ReleaseWorkgroup(OH_AudioResourceManager * resourceManager,OH_AudioWorkgroup * group)55 OH_AudioCommon_Result OH_AudioResourceManager_ReleaseWorkgroup(OH_AudioResourceManager *resourceManager,
56     OH_AudioWorkgroup *group)
57 {
58     if (resourceManager == nullptr || group == nullptr) {
59         AUDIO_ERR_LOG("invalid OH_AudioResourceManager or OH_AudioWorkgroup");
60         return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
61     }
62     if (convertResourceManager(resourceManager)->ReleaseWorkgroup((OHAudioWorkgroup *)group)) {
63         return AUDIOCOMMON_RESULT_SUCCESS;
64     } else {
65         return AUDIOCOMMON_RESULT_ERROR_SYSTEM;
66     }
67 }
68 
OH_AudioWorkgroup_AddCurrentThread(OH_AudioWorkgroup * group,int32_t * tokenId)69 OH_AudioCommon_Result OH_AudioWorkgroup_AddCurrentThread(OH_AudioWorkgroup *group, int32_t *tokenId)
70 {
71     CHECK_AND_RETURN_RET_LOG(group != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "workgroup is nullptr");
72     *tokenId = gettid();
73     if (convertWorkgroup(group)->AddThread(*tokenId)) {
74         return AUDIOCOMMON_RESULT_SUCCESS;
75     } else {
76         return AUDIOCOMMON_RESULT_ERROR_SYSTEM;
77     }
78 }
79 
OH_AudioWorkgroup_RemoveThread(OH_AudioWorkgroup * group,int32_t tokenId)80 OH_AudioCommon_Result OH_AudioWorkgroup_RemoveThread(OH_AudioWorkgroup *group, int32_t tokenId)
81 {
82     CHECK_AND_RETURN_RET_LOG(group != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "workgroup is nullptr");
83     if (convertWorkgroup(group)->RemoveThread(tokenId)) {
84         return AUDIOCOMMON_RESULT_SUCCESS;
85     } else {
86         return AUDIOCOMMON_RESULT_ERROR_SYSTEM;
87     }
88 }
89 
OH_AudioWorkgroup_Start(OH_AudioWorkgroup * group,uint64_t startTime,uint64_t deadlineTime)90 OH_AudioCommon_Result OH_AudioWorkgroup_Start(OH_AudioWorkgroup *group, uint64_t startTime, uint64_t deadlineTime)
91 {
92     CHECK_AND_RETURN_RET_LOG(group != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "workgroup is nullptr");
93     if (convertWorkgroup(group)->Start(startTime, deadlineTime)) {
94         return AUDIOCOMMON_RESULT_SUCCESS;
95     } else {
96         return AUDIOCOMMON_RESULT_ERROR_SYSTEM;
97     }
98 }
99 
OH_AudioWorkgroup_Stop(OH_AudioWorkgroup * group)100 OH_AudioCommon_Result OH_AudioWorkgroup_Stop(OH_AudioWorkgroup *group)
101 {
102     CHECK_AND_RETURN_RET_LOG(group != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "workgroup is nullptr");
103     if (convertWorkgroup(group)->Stop()) {
104         return AUDIOCOMMON_RESULT_SUCCESS;
105     } else {
106         return AUDIOCOMMON_RESULT_ERROR_SYSTEM;
107     }
108 }
109 
110 namespace OHOS {
111 namespace AudioStandard {
GetInstance()112 OHAudioResourceManager *OHAudioResourceManager::GetInstance()
113 {
114     static OHAudioResourceManager audioResourceManager;
115     return &audioResourceManager;
116 }
117 
CreateWorkgroup()118 OHAudioWorkgroup *OHAudioResourceManager::CreateWorkgroup()
119 {
120     int id = AudioSystemManager::GetInstance()->CreateAudioWorkgroup();
121     CHECK_AND_RETURN_RET_LOG(id >= 0, nullptr, "Create failed, the max num of workgroup is 2.");
122     OHAudioWorkgroup *group = new(std::nothrow) OHAudioWorkgroup(id);
123     if (group == nullptr) {
124         AUDIO_ERR_LOG("construct OHAudioWorkgroup failed");
125         AudioSystemManager::GetInstance()->ReleaseAudioWorkgroup(id);
126     }
127     return group;
128 }
129 
ReleaseWorkgroup(OHAudioWorkgroup * group)130 bool OHAudioResourceManager::ReleaseWorkgroup(OHAudioWorkgroup *group)
131 {
132     CHECK_AND_RETURN_RET_LOG(group != nullptr, false, "group is nullptr");
133     AudioSystemManager::GetInstance()->ReleaseAudioWorkgroup(group->workgroupId);
134     delete group;
135     group = nullptr;
136     return true;
137 }
138 } //namespace AudioStandard
139 } //namespace OHOS
140