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 <string> 17 #include "cj_soundpool.h" 18 #include "cj_soundpool_callback.h" 19 #include "soundpool_ffi.h" 20 21 namespace OHOS { 22 namespace Media { 23 using namespace OHOS::FFI; 24 25 extern "C" 26 { FfiSoundPoolCreateSoundPool(int32_t maxStreams,AudioStandard::CAudioRendererInfo info,int32_t * errorcode)27 int64_t FfiSoundPoolCreateSoundPool(int32_t maxStreams, AudioStandard::CAudioRendererInfo info, int32_t *errorcode) 28 { 29 return CJSoundPool::CreatSoundPool(maxStreams, info, *errorcode); 30 } 31 FfiSoundPoolLoadURI(int64_t id,char * uri,int32_t * errorcode)32 int32_t FfiSoundPoolLoadURI(int64_t id, char *uri, int32_t *errorcode) 33 { 34 auto cjSoundPool = FFIData::GetData<CJSoundPool>(id); 35 if (!cjSoundPool) { 36 MEDIA_LOGE("[CJSoundPool] instance is nullptr!"); 37 return ERR_INVALID_INSTANCE_CODE; 38 } 39 return cjSoundPool->Load(uri, *errorcode); 40 } 41 FfiSoundPoolLoad(int64_t id,int32_t fd,int64_t offset,int64_t length,int32_t * errorcode)42 int32_t FfiSoundPoolLoad(int64_t id, int32_t fd, int64_t offset, int64_t length, int32_t *errorcode) 43 { 44 auto cjSoundPool = FFIData::GetData<CJSoundPool>(id); 45 if (!cjSoundPool) { 46 MEDIA_LOGE("[CJSoundPool] instance is nullptr!"); 47 return ERR_INVALID_INSTANCE_CODE; 48 } 49 return cjSoundPool->Load(fd, offset, length, *errorcode); 50 } 51 FfiSoundPoolPlayParam(int64_t id,int32_t soundID,CPlayParameters params,int32_t * errorcode)52 int32_t FfiSoundPoolPlayParam(int64_t id, int32_t soundID, CPlayParameters params, int32_t *errorcode) 53 { 54 auto cjSoundPool = FFIData::GetData<CJSoundPool>(id); 55 if (!cjSoundPool) { 56 MEDIA_LOGE("[CJSoundPool] instance is nullptr!"); 57 return ERR_INVALID_INSTANCE_CODE; 58 } 59 return cjSoundPool->Play(soundID, params, *errorcode); 60 } 61 FfiSoundPoolPlay(int64_t id,int32_t soundID,int32_t * errorcode)62 int32_t FfiSoundPoolPlay(int64_t id, int32_t soundID, int32_t *errorcode) 63 { 64 auto cjSoundPool = FFIData::GetData<CJSoundPool>(id); 65 if (!cjSoundPool) { 66 MEDIA_LOGE("[CJSoundPool] instance is nullptr!"); 67 return ERR_INVALID_INSTANCE_CODE; 68 } 69 return cjSoundPool->Play(soundID, *errorcode); 70 } 71 FfiSoundPoolStop(int64_t id,int32_t streamID)72 int32_t FfiSoundPoolStop(int64_t id, int32_t streamID) 73 { 74 auto cjSoundPool = FFIData::GetData<CJSoundPool>(id); 75 if (!cjSoundPool) { 76 MEDIA_LOGE("[CJSoundPool] instance is nullptr!"); 77 return ERR_INVALID_INSTANCE_CODE; 78 } 79 return cjSoundPool->Stop(streamID); 80 } 81 FfiSoundPoolSetLoop(int64_t id,int32_t streamID,int32_t loop)82 int32_t FfiSoundPoolSetLoop(int64_t id, int32_t streamID, int32_t loop) 83 { 84 auto cjSoundPool = FFIData::GetData<CJSoundPool>(id); 85 if (!cjSoundPool) { 86 MEDIA_LOGE("[CJSoundPool] instance is nullptr!"); 87 return ERR_INVALID_INSTANCE_CODE; 88 } 89 return cjSoundPool->SetLoop(streamID, loop); 90 } 91 FfiSoundPoolSetPriority(int64_t id,int32_t streamID,int32_t priority)92 int32_t FfiSoundPoolSetPriority(int64_t id, int32_t streamID, int32_t priority) 93 { 94 auto cjSoundPool = FFIData::GetData<CJSoundPool>(id); 95 if (!cjSoundPool) { 96 MEDIA_LOGE("[CJSoundPool] instance is nullptr!"); 97 return ERR_INVALID_INSTANCE_CODE; 98 } 99 return cjSoundPool->SetLoop(streamID, priority); 100 } 101 FfiSoundPoolSetRate(int64_t id,int32_t streamID,int32_t rate)102 int32_t FfiSoundPoolSetRate(int64_t id, int32_t streamID, int32_t rate) 103 { 104 auto cjSoundPool = FFIData::GetData<CJSoundPool>(id); 105 if (!cjSoundPool) { 106 MEDIA_LOGE("[CJSoundPool] instance is nullptr!"); 107 return ERR_INVALID_INSTANCE_CODE; 108 } 109 return cjSoundPool->SetRate(streamID, rate); 110 } 111 FfiSoundPoolSetVolume(int64_t id,int32_t streamID,float leftVolume,float rightVolume)112 int32_t FfiSoundPoolSetVolume(int64_t id, int32_t streamID, float leftVolume, float rightVolume) 113 { 114 auto cjSoundPool = FFIData::GetData<CJSoundPool>(id); 115 if (!cjSoundPool) { 116 MEDIA_LOGE("[CJSoundPool] instance is nullptr!"); 117 return ERR_INVALID_INSTANCE_CODE; 118 } 119 return cjSoundPool->SetVolume(streamID, leftVolume, rightVolume); 120 } 121 FfiSoundPoolUnload(int64_t id,int32_t soundID)122 int32_t FfiSoundPoolUnload(int64_t id, int32_t soundID) 123 { 124 auto cjSoundPool = FFIData::GetData<CJSoundPool>(id); 125 if (!cjSoundPool) { 126 MEDIA_LOGE("[CJSoundPool] instance is nullptr!"); 127 return ERR_INVALID_INSTANCE_CODE; 128 } 129 return cjSoundPool->Unload(soundID); 130 } 131 FfiSoundPoolRelease(int64_t id)132 int32_t FfiSoundPoolRelease(int64_t id) 133 { 134 auto cjSoundPool = FFIData::GetData<CJSoundPool>(id); 135 if (!cjSoundPool) { 136 MEDIA_LOGE("[CJSoundPool] instance is nullptr!"); 137 return ERR_INVALID_INSTANCE_CODE; 138 } 139 return cjSoundPool->Release(); 140 } 141 FfiSoundPoolOnLoadCompleted(int64_t id,int64_t callbackId)142 void FfiSoundPoolOnLoadCompleted(int64_t id, int64_t callbackId) 143 { 144 auto cjSoundPool = FFIData::GetData<CJSoundPool>(id); 145 if (!cjSoundPool) { 146 MEDIA_LOGE("[CJSoundPool] instance is nullptr!"); 147 return; 148 } 149 std::shared_ptr<ISoundPool> soundPool_ = cjSoundPool->soundPool_; 150 if (!soundPool_) { 151 MEDIA_LOGE("[CJSoundPool] soundPool_ is nullptr!"); 152 return; 153 } 154 if (!cjSoundPool->callbackCj_) { 155 MEDIA_LOGE("[CJSoundPool] callbackCj_ is nullptr!"); 156 return; 157 } 158 auto Cb_ = std::static_pointer_cast<CJSoundPoolCallBack>(cjSoundPool->callbackCj_); 159 Cb_->InitLoadCompleted(callbackId); 160 return; 161 } 162 FfiSoundPoolOnPlayFinished(int64_t id,int64_t callbackId)163 void FfiSoundPoolOnPlayFinished(int64_t id, int64_t callbackId) 164 { 165 auto cjSoundPool = FFIData::GetData<CJSoundPool>(id); 166 if (!cjSoundPool) { 167 MEDIA_LOGE("[CJSoundPool] instance is nullptr!"); 168 return; 169 } 170 std::shared_ptr<ISoundPool> soundPool_ = cjSoundPool->soundPool_; 171 if (!soundPool_) { 172 MEDIA_LOGE("[CJSoundPool] soundPool_ is nullptr!"); 173 return; 174 } 175 if (!cjSoundPool->callbackCj_) { 176 MEDIA_LOGE("[CJSoundPool] callbackCj_ is nullptr!"); 177 return; 178 } 179 auto Cb_ = std::static_pointer_cast<CJSoundPoolCallBack>(cjSoundPool->callbackCj_); 180 Cb_->InitPlayFinished(callbackId); 181 return; 182 } 183 FfiSoundPoolOnError(int64_t id,int64_t callbackId)184 void FfiSoundPoolOnError(int64_t id, int64_t callbackId) 185 { 186 auto cjSoundPool = FFIData::GetData<CJSoundPool>(id); 187 if (!cjSoundPool) { 188 MEDIA_LOGE("[CJSoundPool] instance is nullptr!"); 189 return; 190 } 191 std::shared_ptr<ISoundPool> soundPool_ = cjSoundPool->soundPool_; 192 if (!soundPool_) { 193 MEDIA_LOGE("[CJSoundPool] soundPool_ is nullptr!"); 194 return; 195 } 196 if (!cjSoundPool->callbackCj_) { 197 MEDIA_LOGE("[CJSoundPool] callbackCj_ is nullptr!"); 198 return; 199 } 200 auto Cb_ = std::static_pointer_cast<CJSoundPoolCallBack>(cjSoundPool->callbackCj_); 201 Cb_->InitError(callbackId); 202 return; 203 } 204 FfiSoundPoolOff(int64_t id,int8_t type)205 void FfiSoundPoolOff(int64_t id, int8_t type) 206 { 207 auto cjSoundPool = FFIData::GetData<CJSoundPool>(id); 208 if (!cjSoundPool) { 209 MEDIA_LOGE("[CJSoundPool] instance is nullptr!"); 210 return; 211 } 212 std::shared_ptr<ISoundPool> soundPool_ = cjSoundPool->soundPool_; 213 if (!soundPool_) { 214 MEDIA_LOGE("[CJSoundPool] soundPool_ is nullptr!"); 215 return; 216 } 217 if (!cjSoundPool->callbackCj_) { 218 MEDIA_LOGE("[CJSoundPool] callbackCj_ is nullptr!"); 219 return; 220 } 221 auto Cb_ = std::static_pointer_cast<CJSoundPoolCallBack>(cjSoundPool->callbackCj_); 222 Cb_->UnRegister(type); 223 return; 224 } 225 } 226 227 } // namespace Media 228 } // namespace OHOS