1 /*
2 * Copyright (c) 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 "cj_soundpool.h"
17 #include "media_core.h"
18
19 namespace OHOS {
20 namespace Media {
CreatSoundPool(int32_t maxStreams,AudioStandard::CAudioRendererInfo info,int32_t & errorcode)21 int64_t CJSoundPool::CreatSoundPool(int32_t maxStreams, AudioStandard::CAudioRendererInfo info, int32_t &errorcode)
22 {
23 CJSoundPool *cjSoundPool = FFIData::Create<CJSoundPool>();
24 if (cjSoundPool == nullptr) {
25 MEDIA_LOGE("No memory!");
26 errorcode = MSERR_EXT_API9_NO_MEMORY;
27 return -1;
28 }
29 AudioStandard::AudioRendererInfo rendererInfo;
30 rendererInfo.contentType = AudioStandard::CONTENT_TYPE_UNKNOWN;
31 rendererInfo.streamUsage = static_cast<AudioStandard::StreamUsage>(info.usage);
32 rendererInfo.rendererFlags = info.rendererFlags;
33
34 cjSoundPool->soundPool_ = SoundPoolFactory::CreateSoundPool(maxStreams, rendererInfo);
35 if (cjSoundPool->soundPool_ == nullptr) {
36 FFIData::Release(cjSoundPool->GetID());
37 MEDIA_LOGE("failed to CreateSoundPool");
38 return -1;
39 }
40
41 if (cjSoundPool->callbackCj_ == nullptr && cjSoundPool->soundPool_ != nullptr) {
42 cjSoundPool->callbackCj_ = std::make_shared<CJSoundPoolCallBack>();
43 (void)cjSoundPool->soundPool_->SetSoundPoolCallback(cjSoundPool->callbackCj_);
44 }
45 MEDIA_LOGI("Constructor success");
46 return cjSoundPool->GetID();
47 }
48
Load(char * uri,int32_t & errorcode)49 int32_t CJSoundPool::Load(char *uri, int32_t &errorcode)
50 {
51 if (soundPool_ == nullptr) {
52 MEDIA_LOGE("[CJSoundPool] soundPool_ is nullptr!");
53 return ERR_INVALID_INSTANCE_CODE;
54 }
55 int32_t soundID = soundPool_->Load(uri);
56 if (soundID < 0) {
57 errorcode = MSERR_EXT_API9_OPERATE_NOT_PERMIT;
58 }
59 return soundID;
60 }
61
Load(int32_t fd,int64_t offset,int64_t length,int32_t & errorcode)62 int32_t CJSoundPool::Load(int32_t fd, int64_t offset, int64_t length,
63 int32_t &errorcode)
64 {
65 if (soundPool_ == nullptr) {
66 MEDIA_LOGE("[CJSoundPool] soundPool_ is nullptr!");
67 return ERR_INVALID_INSTANCE_CODE;
68 }
69 int32_t soundID = soundPool_->Load(fd, offset, length);
70 if (soundID < 0) {
71 errorcode = MSERR_EXT_API9_OPERATE_NOT_PERMIT;
72 }
73 return soundID;
74 }
75
Play(int32_t soundID,int32_t & errorcode)76 int32_t CJSoundPool::Play(int32_t soundID, int32_t &errorcode)
77 {
78 if (soundPool_ == nullptr) {
79 MEDIA_LOGE("[CJSoundPool] soundPool_ is nullptr!");
80 return ERR_INVALID_INSTANCE_CODE;
81 }
82 PlayParams params;
83 params.cacheDir = "/data/storage/el2/base/temp";
84 if (soundID <= 0) {
85 errorcode = MSERR_EXT_API9_INVALID_PARAMETER;
86 return -1;
87 }
88 int32_t streamID = soundPool_->Play(soundID, params);
89 if (streamID < 0) {
90 errorcode = MSERR_EXT_API9_OPERATE_NOT_PERMIT;
91 }
92 return streamID;
93 }
94
Play(int32_t soundID,CPlayParameters cParams,int32_t & errorcode)95 int32_t CJSoundPool::Play(int32_t soundID, CPlayParameters cParams,
96 int32_t &errorcode)
97 {
98 if (soundPool_ == nullptr) {
99 MEDIA_LOGE("[CJSoundPool] soundPool_ is nullptr!");
100 return ERR_INVALID_INSTANCE_CODE;
101 }
102 PlayParams params;
103 params.loop = cParams.loop;
104 params.rate = cParams.rate;
105 params.leftVolume = cParams.leftVolume;
106 params.rightVolume = cParams.rightVolume;
107 params.priority = cParams.priority;
108 params.cacheDir = "/data/storage/el2/base/temp";
109 if (soundID <= 0) {
110 errorcode = MSERR_EXT_API9_INVALID_PARAMETER;
111 return -1;
112 }
113 int32_t streamID = soundPool_->Play(soundID, params);
114 if (streamID < 0) {
115 errorcode = MSERR_EXT_API9_OPERATE_NOT_PERMIT;
116 }
117 return streamID;
118 }
119
Stop(int32_t streamID)120 int32_t CJSoundPool::Stop(int32_t streamID)
121 {
122 if (soundPool_ == nullptr) {
123 MEDIA_LOGE("[CJSoundPool] soundPool_ is nullptr!");
124 return ERR_INVALID_INSTANCE_CODE;
125 }
126 int32_t errorcode = MSERR_OK;
127 if (streamID <= 0) {
128 errorcode = MSERR_EXT_API9_INVALID_PARAMETER;
129 return errorcode;
130 }
131 errorcode = soundPool_->Stop(streamID);
132 if (errorcode != MSERR_OK) {
133 errorcode = MSERR_EXT_API9_OPERATE_NOT_PERMIT;
134 }
135 return errorcode;
136 }
137
SetLoop(int32_t streamID,int32_t loop)138 int32_t CJSoundPool::SetLoop(int32_t streamID, int32_t loop)
139 {
140 if (soundPool_ == nullptr) {
141 MEDIA_LOGE("[CJSoundPool] soundPool_ is nullptr!");
142 return ERR_INVALID_INSTANCE_CODE;
143 }
144 int32_t errorcode = MSERR_OK;
145 if (streamID <= 0) {
146 errorcode = MSERR_EXT_API9_INVALID_PARAMETER;
147 return errorcode;
148 }
149 errorcode = soundPool_->SetLoop(streamID, loop);
150 if (errorcode != MSERR_OK) {
151 errorcode = MSERR_EXT_API9_OPERATE_NOT_PERMIT;
152 }
153 return errorcode;
154 }
155
SetPriority(int32_t streamID,int32_t priority)156 int32_t CJSoundPool::SetPriority(int32_t streamID, int32_t priority)
157 {
158 if (soundPool_ == nullptr) {
159 MEDIA_LOGE("[CJSoundPool] soundPool_ is nullptr!");
160 return ERR_INVALID_INSTANCE_CODE;
161 }
162 int32_t errorcode = MSERR_OK;
163 if (streamID <= 0 || priority < 0) {
164 errorcode = MSERR_EXT_API9_INVALID_PARAMETER;
165 return errorcode;
166 }
167 errorcode = soundPool_->SetPriority(streamID, priority);
168 if (errorcode != MSERR_OK) {
169 errorcode = MSERR_EXT_API9_OPERATE_NOT_PERMIT;
170 }
171 return errorcode;
172 }
173
SetRate(int32_t streamID,int32_t rate)174 int32_t CJSoundPool::SetRate(int32_t streamID, int32_t rate)
175 {
176 if (soundPool_ == nullptr) {
177 MEDIA_LOGE("[CJSoundPool] soundPool_ is nullptr!");
178 return ERR_INVALID_INSTANCE_CODE;
179 }
180 int32_t errorcode = MSERR_OK;
181 if (streamID <= 0) {
182 errorcode = MSERR_EXT_API9_INVALID_PARAMETER;
183 return errorcode;
184 }
185 errorcode = soundPool_->SetRate(streamID, static_cast<AudioStandard::AudioRendererRate>(rate));
186 if (errorcode != MSERR_OK) {
187 errorcode = MSERR_EXT_API9_OPERATE_NOT_PERMIT;
188 }
189 return errorcode;
190 }
191
SetVolume(int32_t streamID,float leftVolume,float rightVolume)192 int32_t CJSoundPool::SetVolume(int32_t streamID, float leftVolume,
193 float rightVolume)
194 {
195 if (soundPool_ == nullptr) {
196 MEDIA_LOGE("[CJSoundPool] soundPool_ is nullptr!");
197 return ERR_INVALID_INSTANCE_CODE;
198 }
199 int32_t errorcode = MSERR_OK;
200 if (streamID <= 0) {
201 errorcode = MSERR_EXT_API9_INVALID_PARAMETER;
202 return errorcode;
203 }
204 errorcode = soundPool_->SetVolume(streamID, leftVolume, rightVolume);
205 if (errorcode != MSERR_OK) {
206 errorcode = MSERR_EXT_API9_OPERATE_NOT_PERMIT;
207 }
208 return errorcode;
209 }
210
Unload(int32_t soundID)211 int32_t CJSoundPool::Unload(int32_t soundID)
212 {
213 if (soundPool_ == nullptr) {
214 MEDIA_LOGE("[CJSoundPool] soundPool_ is nullptr!");
215 return ERR_INVALID_INSTANCE_CODE;
216 }
217 int32_t errorcode = MSERR_OK;
218 if (soundID <= 0) {
219 errorcode = MSERR_EXT_API9_IO;
220 return errorcode;
221 }
222 errorcode = soundPool_->Unload(soundID);
223 if (errorcode != MSERR_OK) {
224 errorcode = MSERR_EXT_API9_OPERATE_NOT_PERMIT;
225 }
226 return errorcode;
227 }
228
Release()229 int32_t CJSoundPool::Release()
230 {
231 if (soundPool_ == nullptr) {
232 MEDIA_LOGE("[CJSoundPool] soundPool_ is nullptr!");
233 return ERR_INVALID_INSTANCE_CODE;
234 }
235 int32_t errorcode = MSERR_OK;
236 errorcode = soundPool_->Release();
237 if (errorcode != MSERR_OK) {
238 errorcode = MSERR_EXT_API9_OPERATE_NOT_PERMIT;
239 }
240 return errorcode;
241 }
242
243 } // namespace Media
244 } // namespace OHOS
245