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_lambda.h" 17 #include "cj_soundpool_callback.h" 18 #include "media_core.h" 19 20 namespace OHOS { 21 namespace Media { 22 MallocCString(const std::string & origin)23char *MallocCString(const std::string &origin) 24 { 25 if (origin.empty()) { 26 return nullptr; 27 } 28 auto len = origin.length() + 1; 29 char *res = static_cast<char *>(malloc(sizeof(char) * len)); 30 if (res == nullptr) { 31 return nullptr; 32 } 33 return std::char_traits<char>::copy(res, origin.c_str(), len); 34 } 35 UnRegister(int8_t type)36void CJSoundPoolCallBack::UnRegister(int8_t type) 37 { 38 std::lock_guard<std::recursive_mutex> lock(mutex_); 39 switch (type) { 40 case EVENT_LOAD_COMPLETED: 41 onLoadCompleted = nullptr; 42 break; 43 case EVENT_PLAY_FINISHED: 44 onPlayFinished = nullptr; 45 break; 46 case EVENT_ERROR: 47 onError = nullptr; 48 break; 49 default: 50 return; 51 } 52 } 53 InitLoadCompleted(int64_t id)54void CJSoundPoolCallBack::InitLoadCompleted(int64_t id) 55 { 56 auto callback = reinterpret_cast<void (*)(const int32_t)>(id); 57 onLoadCompleted = [lambda = CJLambda::Create(callback)](int32_t sourceId) -> void { 58 lambda(sourceId); 59 }; 60 } 61 OnLoadCompleted(int32_t soundId)62void CJSoundPoolCallBack::OnLoadCompleted(int32_t soundId) 63 { 64 if (onLoadCompleted == nullptr) { 65 MEDIA_LOGD("onLoadCompleted null"); 66 return; 67 } 68 69 MEDIA_LOGD("onLoadCompleted runs"); 70 return onLoadCompleted(soundId); 71 } 72 InitPlayFinished(int64_t id)73void CJSoundPoolCallBack::InitPlayFinished(int64_t id) 74 { 75 auto callback = reinterpret_cast<void (*)()>(id); 76 onPlayFinished = [lambda = CJLambda::Create(callback)]() -> void { 77 lambda(); 78 }; 79 } 80 OnPlayFinished(int32_t streamID)81void CJSoundPoolCallBack::OnPlayFinished(int32_t streamID) 82 { 83 if (onPlayFinished == nullptr) { 84 MEDIA_LOGD("onPlayFinished null"); 85 return; 86 } 87 88 MEDIA_LOGD("onPlayFinished runs"); 89 return onPlayFinished(); 90 } 91 InitError(int64_t id)92void CJSoundPoolCallBack::InitError(int64_t id) 93 { 94 auto callback = reinterpret_cast<void (*)(const CJException)>(id); 95 onError = [lambda = CJLambda::Create(callback)](CJException value) -> void { 96 lambda(value); 97 }; 98 } 99 OnError(int32_t errorCode)100void CJSoundPoolCallBack::OnError(int32_t errorCode) 101 { 102 if (onError == nullptr) { 103 MEDIA_LOGD("onError null"); 104 return; 105 } 106 107 MEDIA_LOGI("OnError recived:error:%{public}d", errorCode); 108 CJException exception; 109 if (errorCode == MSERR_INVALID_OPERATION) { 110 exception.code = MSERR_EXT_API9_OPERATE_NOT_PERMIT; 111 exception.msg = MallocCString("The soundpool timed out. Please confirm that the input stream is normal."); 112 } else if (errorCode == MSERR_NO_MEMORY) { 113 exception.code = MSERR_EXT_API9_NO_MEMORY; 114 exception.msg = MallocCString("soundpool memery error."); 115 } else if (errorCode == MSERR_SERVICE_DIED) { 116 exception.code = MSERR_EXT_API9_SERVICE_DIED; 117 exception.msg = MallocCString("releated server died"); 118 } else { 119 exception.code = MSERR_EXT_API9_IO; 120 exception.msg = MallocCString("IO error happened."); 121 } 122 123 return onError(exception); 124 } 125 126 } // namespace Media 127 } // namespace OHOS