diff --git a/frameworks/js/system_sound_manager/src/tone_attrs/tone_attrs_napi.cpp b/frameworks/js/system_sound_manager/src/tone_attrs/tone_attrs_napi.cpp index 50de3ec901995a34271b08d97e1b0c11d5546f5c..e651e410e1d1f067d0cc71b400e1b40e625d5b7f 100644 --- a/frameworks/js/system_sound_manager/src/tone_attrs/tone_attrs_napi.cpp +++ b/frameworks/js/system_sound_manager/src/tone_attrs/tone_attrs_napi.cpp @@ -378,7 +378,7 @@ napi_value ToneAttrsNapi::SetMediaType(napi_env env, napi_callback_info info) napi_unwrap(env, jsThis, reinterpret_cast(&toneAttrsNapi)); CHECK_AND_RETURN_RET_LOG(toneAttrsNapi != nullptr, nullptr, "toneAttrsNapi is nullptr"); CHECK_AND_RETURN_RET_LOG(toneAttrsNapi->toneAttrs_ != nullptr, nullptr, "toneAttrs_ is nullptr"); - toneAttrsNapi->toneAttrs_->SetMediaType(static_cast(toneAttrsMediaType)); + toneAttrsNapi->toneAttrs_->SetMediaType(static_cast(toneAttrsMediaType)); return nullptr; } @@ -397,7 +397,7 @@ napi_value ToneAttrsNapi::GetMediaType(napi_env env, napi_callback_info info) CHECK_AND_RETURN_RET_LOG(toneAttrsNapi != nullptr, nullptr, "toneAttrsNapi is nullptr"); CHECK_AND_RETURN_RET_LOG(toneAttrsNapi->toneAttrs_ != nullptr, nullptr, "toneAttrs_ is nullptr"); napi_value result; - napi_create_int32(env, toneAttrsNapi->toneAttrs_->GetMediaType(), &result); + napi_create_int32(env, static_cast(toneAttrsNapi->toneAttrs_->GetMediaType()), &result); return result; } } // namespace Media diff --git a/frameworks/native/system_sound_manager/BUILD.gn b/frameworks/native/system_sound_manager/BUILD.gn index db055f62ed2eb87a0d600682abf8e337c784879e..5dc07afe394c8dbed5a7b00928c3dd8b43d48331 100644 --- a/frameworks/native/system_sound_manager/BUILD.gn +++ b/frameworks/native/system_sound_manager/BUILD.gn @@ -89,6 +89,7 @@ ohos_shared_library("system_sound_client") { "ringtone_library:ringtone_data_extension", "ringtone_library:ringtone_data_helper", "samgr:samgr_proxy", + "hitrace:hitrace_meter", ] if (player_framework_support_vibrator) { diff --git a/frameworks/native/system_sound_manager/system_sound_manager_impl.cpp b/frameworks/native/system_sound_manager/system_sound_manager_impl.cpp index 2e257bc428aeb62a235dc77b42e4504988ac4fc5..7ddf1e1a16d761f834ce862a50904343196e6e6b 100644 --- a/frameworks/native/system_sound_manager/system_sound_manager_impl.cpp +++ b/frameworks/native/system_sound_manager/system_sound_manager_impl.cpp @@ -67,7 +67,9 @@ const int32_t NOT_ENOUGH_ROM = -234; const int32_t VIDEOS_NUM_EXCEEDS_SPECIFICATION = -235; const int32_t FILE_EXIST = -17; const int32_t MAX_VECTOR_LENGTH = 1024; -const off_t MAX_FILE_SIZE_1G = 1024 * 1024 * 1024; +const off_t MAX_FILE_SIZE_500M = 500 * 1024 * 1024; +const int32_t PARAM1 = 1; +const int32_t PARAM2 = 2; #ifdef SUPPORT_VIBRATOR const int OPERATION_ERROR = -4; #endif @@ -660,9 +662,9 @@ ToneAttrs SystemSoundManagerImpl::GetRingtoneAttrsByType(const DatabaseTool &dat toneAttrs.SetFileName(ringtoneAsset->GetDisplayName()); toneAttrs.SetCategory(ringtoneAsset->GetToneType()); if (ringtoneAsset->GetMediaType() == RINGTONE_MEDIA_TYPE_VIDEO) { - toneAttrs.SetMediaType(MediaType::MEDIA_TYPE_VID); + toneAttrs.SetMediaType(ToneMediaType::MEDIA_TYPE_VID); } else { - toneAttrs.SetMediaType(MediaType::MEDIA_TYPE_AUD); + toneAttrs.SetMediaType(ToneMediaType::MEDIA_TYPE_AUD); } } resultSet == nullptr ? : resultSet->Close(); @@ -742,9 +744,9 @@ ToneAttrs SystemSoundManagerImpl::GetPresetRingToneAttrByType(const DatabaseTool toneAttrs.SetFileName(ringtoneAsset->GetDisplayName()); toneAttrs.SetCategory(ringtoneAsset->GetToneType()); if (ringtoneAsset->GetMediaType() == RINGTONE_MEDIA_TYPE_VIDEO) { - toneAttrs.SetMediaType(MediaType::MEDIA_TYPE_VID); + toneAttrs.SetMediaType(ToneMediaType::MEDIA_TYPE_VID); } else { - toneAttrs.SetMediaType(MediaType::MEDIA_TYPE_AUD); + toneAttrs.SetMediaType(ToneMediaType::MEDIA_TYPE_AUD); } } resultSet == nullptr ? : resultSet->Close(); @@ -1773,54 +1775,47 @@ std::vector> SystemSoundManag errCode = ERROR_IO; return resultOfOpenList; } - OpenFilesInList(dataShareHelper, uriList, resultOfOpenList); + for (uint32_t i = 0; i < uriList.size(); i++) { + std::tuple resultOfOpen = std::make_tuple(uriList[i], INVALID_FD, ERROR_IO); + OpenOneFile(dataShareHelper, uriList[i], resultOfOpen); + resultOfOpenList.push_back(resultOfOpen); + } dataShareHelper->Release(); errCode = ERROR_OK; return resultOfOpenList; } -void SystemSoundManagerImpl::OpenFilesInList(std::shared_ptr &dataShareHelper, - const std::vector &uriList, - std::vector> &resultOfOpenList) +void SystemSoundManagerImpl::OpenOneFile(std::shared_ptr &dataShareHelper, + const std::string &uri, std::tuple &resultOfOpen) { - for (uint32_t i = 0; i < uriList.size(); i++) { - DataShare::DatashareBusinessError businessError; - DataShare::DataSharePredicates queryPredicates; - queryPredicates.EqualTo(RINGTONE_COLUMN_DATA, uriList[i]); - auto resultSet = dataShareHelper->Query(RINGTONEURI, queryPredicates, COLUMNS, &businessError); - auto results = make_unique>(move(resultSet)); - if (results == nullptr) { - MEDIA_LOGE("OpenFilesInList: Query failed, ringtone library error!"); - std::tuple resultOfOpen = - std::make_tuple(uriList[i], INVALID_FD, ERROR_IO); - resultOfOpenList.push_back(resultOfOpen); - continue; - } - unique_ptr ringtoneAsset = results->GetFirstObject(); - while ((ringtoneAsset != nullptr) && (uriList[i] != ringtoneAsset->GetPath())) { - ringtoneAsset = results->GetNextObject(); - } - if (ringtoneAsset != nullptr) { - string uriStr = RINGTONE_PATH_URI + RINGTONE_SLASH_CHAR + to_string(ringtoneAsset->GetId()); - Uri ofUri(uriStr); - int32_t fd = dataShareHelper->OpenFile(ofUri, "r"); - resultSet == nullptr ? : resultSet->Close(); - if (fd > 0) { - std::tuple resultOfOpen = std::make_tuple(uriList[i], fd, ERROR_OK); - resultOfOpenList.push_back(resultOfOpen); - } else { - MEDIA_LOGE("OpenFilesInList: OpenFile failed, uri: %{public}s.", uriList[i].c_str()); - std::tuple resultOfOpen = - std::make_tuple(uriList[i], INVALID_FD, ERROR_IO); - resultOfOpenList.push_back(resultOfOpen); - } - continue; - } - MEDIA_LOGE("OpenFilesInList: ringtoneAsset is nullptr, uri: %{public}s.", uriList[i].c_str()); + DataShare::DatashareBusinessError businessError; + DataShare::DataSharePredicates queryPredicates; + queryPredicates.EqualTo(RINGTONE_COLUMN_DATA, uri); + auto resultSet = dataShareHelper->Query(RINGTONEURI, queryPredicates, COLUMNS, &businessError); + auto results = make_unique>(move(resultSet)); + if (results == nullptr) { + MEDIA_LOGE("OpenOneFile: Query failed, ringtone library error!"); + return; + } + unique_ptr ringtoneAsset = results->GetFirstObject(); + while ((ringtoneAsset != nullptr) && (uri != ringtoneAsset->GetPath())) { + ringtoneAsset = results->GetNextObject(); + } + if (ringtoneAsset != nullptr) { + string uriStr = RINGTONE_PATH_URI + RINGTONE_SLASH_CHAR + to_string(ringtoneAsset->GetId()); + Uri ofUri(uriStr); + int32_t fd = dataShareHelper->OpenFile(ofUri, "r"); resultSet == nullptr ? : resultSet->Close(); - std::tuple resultOfOpen = std::make_tuple(uriList[i], INVALID_FD, ERROR_IO); - resultOfOpenList.push_back(resultOfOpen); + if (fd > 0) { + std::get(resultOfOpen) = fd; + std::get(resultOfOpen) = ERROR_OK; + } else { + MEDIA_LOGE("OpenOneFile: OpenFile failed, uri: %{public}s.", uri.c_str()); + } + return; } + MEDIA_LOGE("OpenOneFile: ringtoneAsset is nullptr, uri: %{public}s.", uri.c_str()); + resultSet == nullptr ? : resultSet->Close(); } int32_t SystemSoundManagerImpl::Close(const int32_t &fd) @@ -1855,6 +1850,7 @@ std::string SystemSoundManagerImpl::AddCustomizedToneByExternalUri( std::string SystemSoundManagerImpl::AddCustomizedToneByFd(const std::shared_ptr &context, const std::shared_ptr &toneAttrs, const int32_t &fd) { + MEDIA_LOGI("AddCustomizedToneByFd: Start."); return AddCustomizedToneByFdAndOffset(context, toneAttrs, fd, 0, INT_MAX); } @@ -1877,6 +1873,7 @@ void SystemSoundManagerImpl::GetCustomizedTone(const std::shared_ptr int32_t SystemSoundManagerImpl::AddCustomizedTone(const std::shared_ptr &dataShareHelper, const std::shared_ptr &toneAttrs) { + MEDIA_LOGI("AddCustomizedTone: Start."); CHECK_AND_RETURN_RET_LOG(dataShareHelper != nullptr, ERROR, "Invalid dataShareHelper."); int32_t category = -1; category = toneAttrs->GetCategory(); @@ -1884,9 +1881,9 @@ int32_t SystemSoundManagerImpl::AddCustomizedTone(const std::shared_ptr(displayName_)); valuesBucket.Put(RINGTONE_COLUMN_TITLE, static_cast(toneAttrs->GetTitle())); - if (toneAttrs->GetMediaType() == MediaType::MEDIA_TYPE_AUD) { + if (toneAttrs->GetMediaType() == ToneMediaType::MEDIA_TYPE_AUD) { valuesBucket.Put(RINGTONE_COLUMN_MEDIA_TYPE, static_cast(RINGTONE_MEDIA_TYPE_AUDIO)); - } else if (toneAttrs->GetMediaType() == MediaType::MEDIA_TYPE_VID) { + } else if (toneAttrs->GetMediaType() == ToneMediaType::MEDIA_TYPE_VID) { valuesBucket.Put(RINGTONE_COLUMN_MEDIA_TYPE, static_cast(RINGTONE_MEDIA_TYPE_VIDEO)); } valuesBucket.Put(RINGTONE_COLUMN_MIME_TYPE, static_cast(mimeType_)); @@ -1925,15 +1922,16 @@ int32_t SystemSoundManagerImpl::AddCustomizedTone(const std::shared_ptr &dataShareHelper, const std::shared_ptr &toneAttrs) { + MEDIA_LOGI("DeleteCustomizedTone: Start."); CHECK_AND_RETURN_RET_LOG(dataShareHelper != nullptr, ERROR, "Invalid dataShareHelper."); int32_t category = -1; category = toneAttrs->GetCategory(); DataShare::DataSharePredicates predicates; predicates.EqualTo(RINGTONE_COLUMN_DISPLAY_NAME, static_cast(displayName_)); predicates.EqualTo(RINGTONE_COLUMN_TITLE, static_cast(toneAttrs->GetTitle())); - if (toneAttrs->GetMediaType() == MediaType::MEDIA_TYPE_AUD) { + if (toneAttrs->GetMediaType() == ToneMediaType::MEDIA_TYPE_AUD) { predicates.EqualTo(RINGTONE_COLUMN_MEDIA_TYPE, static_cast(RINGTONE_MEDIA_TYPE_AUDIO)); - } else if (toneAttrs->GetMediaType() == MediaType::MEDIA_TYPE_VID) { + } else if (toneAttrs->GetMediaType() == ToneMediaType::MEDIA_TYPE_VID) { predicates.EqualTo(RINGTONE_COLUMN_MEDIA_TYPE, static_cast(RINGTONE_MEDIA_TYPE_VIDEO)); } predicates.EqualTo(RINGTONE_COLUMN_MIME_TYPE, static_cast(mimeType_)); @@ -1967,17 +1965,18 @@ std::string SystemSoundManagerImpl::AddCustomizedToneByFdAndOffset( const std::shared_ptr &context, const std::shared_ptr &toneAttrs, const int32_t &fd, const int32_t &offset, const int32_t &length) { + MEDIA_LOGI("AddCustomizedToneByFdAndOffset: Start."); std::string result = "TYPEERROR"; if (toneAttrs->GetCustomizedType() != CUSTOMISED) { MEDIA_LOGE("AddCustomizedToneByFdAndOffset: The ringtone is not customized!"); return result; } off_t fileSize = 0; - if (toneAttrs->GetMediaType() == MediaType::MEDIA_TYPE_VID) { + if (toneAttrs->GetMediaType() == ToneMediaType::MEDIA_TYPE_VID) { fileSize = lseek(fd, 0, SEEK_END); lseek(fd, 0, SEEK_SET); - if (fileSize > MAX_FILE_SIZE_1G) { - MEDIA_LOGE("AddCustomizedToneByFdAndOffset: The file size exceeds 1G."); + if (fileSize > MAX_FILE_SIZE_500M) { + MEDIA_LOGE("AddCustomizedToneByFdAndOffset: The file size exceeds 500M."); return FILE_SIZE_EXCEEDS_LIMIT; } } @@ -1989,6 +1988,7 @@ std::string SystemSoundManagerImpl::AddCustomizedToneByFdAndOffset( result.clear(); return result; } + MediaTrace::TraceBegin("SystemSoundManagerImpl::AddCustomizedToneByFdAndOffset", FAKE_POINTER(this)); std::shared_ptr dataShareHelper = SystemSoundManagerUtils::CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID); if (dataShareHelper == nullptr) { @@ -1997,15 +1997,17 @@ std::string SystemSoundManagerImpl::AddCustomizedToneByFdAndOffset( return result; } int32_t sert = AddCustomizedTone(dataShareHelper, toneAttrs); - if (sert == VIDEOS_NUM_EXCEEDS_SPECIFICATION) { - dataShareHelper->Release(); - return FILE_COUNT_EXCEEDS_LIMIT; - } else if (sert == NOT_ENOUGH_ROM) { + if (sert < 0) { dataShareHelper->Release(); - return ROM_IS_INSUFFICIENT; - } else if (sert == FILE_EXIST) { - dataShareHelper->Release(); - return toneAttrs->GetUri(); + SendCustomizedToneEvent(true, toneAttrs, fileSize, mimeType_, ERROR); + MediaTrace::TraceEnd("SystemSoundManagerImpl::AddCustomizedToneByFdAndOffset", FAKE_POINTER(this)); + if (sert == VIDEOS_NUM_EXCEEDS_SPECIFICATION) { + return FILE_COUNT_EXCEEDS_LIMIT; + } else if (sert == NOT_ENOUGH_ROM) { + return ROM_IS_INSUFFICIENT; + } else if (sert == FILE_EXIST) { + return toneAttrs->GetUri(); + } } std::string dstPath = RINGTONE_PATH_URI + RINGTONE_SLASH_CHAR + to_string(sert); ParamsForWriteFile paramsForWriteFile = { dstPath, fileSize, srcFd, length }; @@ -2016,6 +2018,7 @@ std::string SystemSoundManagerImpl::CustomizedToneWriteFile(const std::shared_pt std::shared_ptr &dataShareHelper, const std::shared_ptr &toneAttrs, ParamsForWriteFile ¶msForWriteFile) { + MEDIA_LOGI("CustomizedToneWriteFile: Start."); Uri ofUri(paramsForWriteFile.dstPath); int32_t dstFd = dataShareHelper->OpenFile(ofUri, "rw"); if (dstFd < 0) { @@ -2023,8 +2026,10 @@ std::string SystemSoundManagerImpl::CustomizedToneWriteFile(const std::shared_pt DeleteCustomizedTone(dataShareHelper, toneAttrs); dataShareHelper->Release(); SendCustomizedToneEvent(true, toneAttrs, paramsForWriteFile.fileSize, mimeType_, ERROR); + MediaTrace::TraceEnd("SystemSoundManagerImpl::AddCustomizedToneByFdAndOffset", FAKE_POINTER(this)); return ""; } + MEDIA_LOGI("CustomizedToneWriteFile: OpenFile success, begin write file."); char buffer[4096]; int32_t len = paramsForWriteFile.length; memset_s(buffer, sizeof(buffer), 0, sizeof(buffer)); @@ -2037,9 +2042,11 @@ std::string SystemSoundManagerImpl::CustomizedToneWriteFile(const std::shared_pt } len -= bytesWritten; } + MEDIA_LOGI("CustomizedToneWriteFile: Write file end."); close(dstFd); dataShareHelper->Release(); SendCustomizedToneEvent(true, toneAttrs, paramsForWriteFile.fileSize, mimeType_, SUCCESS); + MediaTrace::TraceEnd("SystemSoundManagerImpl::AddCustomizedToneByFdAndOffset", FAKE_POINTER(this)); return toneAttrs->GetUri(); } @@ -2052,9 +2059,10 @@ int32_t SystemSoundManagerImpl::RemoveCustomizedTone( SystemSoundManagerUtils::CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID); CHECK_AND_RETURN_RET_LOG(dataShareHelper != nullptr, ERROR, "RemoveCustomizedTone: Create dataShare failed, datashare or ringtone library error."); + std::tuple resultOfOpen = std::make_tuple(uri, INVALID_FD, ERROR_IO); + OpenOneFile(dataShareHelper, uri, resultOfOpen); + int64_t srcFd = std::get(resultOfOpen); off_t fileSize = 0; - Uri ofUri(uri); - int32_t srcFd = dataShareHelper->OpenFile(ofUri, "rw"); if (srcFd < 0) { MEDIA_LOGE("RemoveCustomizedTone: fd open error is %{public}s", strerror(errno)); } else { @@ -2092,9 +2100,9 @@ int32_t SystemSoundManagerImpl::DoRemove(std::shared_ptrSetCategory(ringtoneAsset->GetToneType()); if (ringtoneAsset->GetMediaType() == RINGTONE_MEDIA_TYPE_VIDEO) { - toneAttrs->SetMediaType(MediaType::MEDIA_TYPE_VID); + toneAttrs->SetMediaType(ToneMediaType::MEDIA_TYPE_VID); } else { - toneAttrs->SetMediaType(MediaType::MEDIA_TYPE_AUD); + toneAttrs->SetMediaType(ToneMediaType::MEDIA_TYPE_AUD); } mimeType = ringtoneAsset->GetMimeType(); DataShare::DataSharePredicates deletePredicates; diff --git a/frameworks/native/system_sound_manager/system_sound_manager_impl.h b/frameworks/native/system_sound_manager/system_sound_manager_impl.h index 4e81beb40c1aaa9a38b90f815fe488ba6d78164f..3e5706810f7b3c769d239190ebf782b64bd5f282 100644 --- a/frameworks/native/system_sound_manager/system_sound_manager_impl.h +++ b/frameworks/native/system_sound_manager/system_sound_manager_impl.h @@ -240,9 +240,8 @@ private: std::string CustomizedToneWriteFile(const std::shared_ptr &context, std::shared_ptr &dataShareHelper, const std::shared_ptr &toneAttrs, ParamsForWriteFile ¶msForWriteFile); - void OpenFilesInList(std::shared_ptr &dataShareHelper, - const std::vector &uriList, - std::vector> &resultOfOpenList); + void OpenOneFile(std::shared_ptr &dataShareHelper, + const std::string &uri, std::tuple &resultOfOpen); int32_t DoRemove(std::shared_ptr &dataShareHelper, const std::string &uri, off_t fileSize); std::string GetBundleName(); diff --git a/frameworks/native/system_sound_manager/system_sound_manager_utils.cpp b/frameworks/native/system_sound_manager/system_sound_manager_utils.cpp index 2cd15938c148810df4e884dc446cd433f3e86468..9224f95bbe6c90e33762e911856016f017575963 100644 --- a/frameworks/native/system_sound_manager/system_sound_manager_utils.cpp +++ b/frameworks/native/system_sound_manager/system_sound_manager_utils.cpp @@ -24,6 +24,7 @@ #include "system_sound_manager.h" #include "system_tone_player_impl.h" #include "parameter.h" +#include "hitrace_meter.h" using namespace std; using namespace nlohmann; @@ -203,5 +204,33 @@ std::string SystemSoundManagerUtils::GetErrorReason(const int32_t &errorCode) } return errorReason; } + +MediaTrace::MediaTrace(const std::string &funcName) +{ + StartTrace(HITRACE_TAG_ZMEDIA, funcName); + isSync_ = true; +} + +void MediaTrace::TraceBegin(const std::string &funcName, int32_t taskId) +{ + StartAsyncTrace(HITRACE_TAG_ZMEDIA, funcName, taskId); +} + +void MediaTrace::TraceEnd(const std::string &funcName, int32_t taskId) +{ + FinishAsyncTrace(HITRACE_TAG_ZMEDIA, funcName, taskId); +} + +void MediaTrace::CounterTrace(const std::string &varName, int32_t val) +{ + CountTrace(HITRACE_TAG_ZMEDIA, varName, val); +} + +MediaTrace::~MediaTrace() +{ + if (isSync_) { + FinishTrace(HITRACE_TAG_ZMEDIA); + } +} } // namesapce Media } // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/system_sound_manager/system_sound_manager_utils.h b/frameworks/native/system_sound_manager/system_sound_manager_utils.h index 809ac59f930a679de3e3f829b21306774fd6960f..3accc652874ceb0a31c6ca95d4aa22efc1c7ff01 100644 --- a/frameworks/native/system_sound_manager/system_sound_manager_utils.h +++ b/frameworks/native/system_sound_manager/system_sound_manager_utils.h @@ -40,6 +40,17 @@ public: static int32_t GetTypeForSystemSoundUri(const std::string &audioUri); static std::string GetErrorReason(const int32_t &errorCode); }; + +class __attribute__((visibility("default"))) MediaTrace : public NoCopyable { +public: + explicit MediaTrace(const std::string &funcName); + static void TraceBegin(const std::string &funcName, int32_t taskId); + static void TraceEnd(const std::string &funcName, int32_t taskId); + static void CounterTrace(const std::string &varName, int32_t val); + ~MediaTrace(); +private: + bool isSync_ = false; +}; } // namespace Media } // namespace OHOS #endif // SYSTEM_SOUND_MANAGER_UTILS_H \ No newline at end of file diff --git a/frameworks/native/system_sound_manager/unittest/ringtone_player_test/BUILD.gn b/frameworks/native/system_sound_manager/unittest/ringtone_player_test/BUILD.gn index 1a22eb4a551c30499a9f69e9cd1a552f145579bc..18ba40e24a09de14184c885f1cd7df2ea98d3db6 100644 --- a/frameworks/native/system_sound_manager/unittest/ringtone_player_test/BUILD.gn +++ b/frameworks/native/system_sound_manager/unittest/ringtone_player_test/BUILD.gn @@ -81,6 +81,7 @@ ohos_unittest("ringtone_player_unit_test") { "ringtone_library:ringtone_data_extension", "ringtone_library:ringtone_data_helper", "samgr:samgr_proxy", + "hitrace:hitrace_meter", ] resource_config_file = "../../../../../test/unittest/resources/ohos_test.xml" diff --git a/frameworks/native/system_sound_manager/unittest/sound_manager_test/BUILD.gn b/frameworks/native/system_sound_manager/unittest/sound_manager_test/BUILD.gn index 499909176a02a9ac9648a101f9ad183a5ea7d6c3..357e9ab8000a3c6e9801302375946ed4e577d6b4 100644 --- a/frameworks/native/system_sound_manager/unittest/sound_manager_test/BUILD.gn +++ b/frameworks/native/system_sound_manager/unittest/sound_manager_test/BUILD.gn @@ -82,5 +82,6 @@ ohos_unittest("system_sound_manager_unit_test") { "ringtone_library:ringtone_data_extension", "ringtone_library:ringtone_data_helper", "samgr:samgr_proxy", + "hitrace:hitrace_meter", ] } diff --git a/frameworks/native/system_sound_manager/unittest/sound_manager_test/include/access_token.h b/frameworks/native/system_sound_manager/unittest/sound_manager_test/include/access_token.h new file mode 100644 index 0000000000000000000000000000000000000000..c660f29f695f9cf2c512c5b063abd751c3ad0dd0 --- /dev/null +++ b/frameworks/native/system_sound_manager/unittest/sound_manager_test/include/access_token.h @@ -0,0 +1,70 @@ +/* + +Copyright (C) 2025 Huawei Device Co., Ltd. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +#ifndef RING_TONE_ACCESS_TOKEN_H +#define RING_TONE_ACCESS_TOKEN_H + +#include "accesstoken_kit.h" +#include "token_setproc.h" + +namespace OHOS { +namespace Media { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; + +inline HapInfoParams testInfoParams = { + .bundleName = "test", + .userID = 1, + .instIndex = 0, + .appIDDesc = "test", + //.apiVersion = 8, + .isSystemApp = true, +}; + +inline PermissionStateFull testPlaceCallState = { + .permissionName = "ohos.permission.WRITE_RINGTONE", + .isGeneral = true, + .resDeviceID = { "local" }, + .grantStatus = { PermissionState::PERMISSION_GRANTED }, + .grantFlags = { 1 }, // PERMISSION_USER_SET +}; + +inline HapPolicyParams testPolicyParams = { + .apl = APL_SYSTEM_BASIC, + .domain = "test", + .permList = {}, + .permStateList = { testPlaceCallState }, +}; + +class AccessToken { +public: + AccessToken() + { + currentID_ = GetSelfTokenID(); + AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParams, testPolicyParams); + accessID_ = tokenIdEx.tokenIdExStruct.tokenID; + SetSelfTokenID(tokenIdEx.tokenIDEx); + } + ~AccessToken() + { + AccessTokenKit::DeleteToken(accessID_); + SetSelfTokenID(currentID_); + } +private: + AccessTokenID currentID_ = 0; + AccessTokenID accessID_ = 0; +}; +} // namespace Media +} // namespace OHOS + +#endif //RING_TONE_ACCESS_TOKEN_H \ No newline at end of file diff --git a/frameworks/native/system_sound_manager/unittest/sound_manager_test/include/system_sound_manager_unit_test.h b/frameworks/native/system_sound_manager/unittest/sound_manager_test/include/system_sound_manager_unit_test.h index 78a392f8dfe906e421dc4efaa93e75e1c3944882..4a92fd8df0efe7be21354094b96fd419d78fb94d 100644 --- a/frameworks/native/system_sound_manager/unittest/sound_manager_test/include/system_sound_manager_unit_test.h +++ b/frameworks/native/system_sound_manager/unittest/sound_manager_test/include/system_sound_manager_unit_test.h @@ -15,10 +15,7 @@ #ifndef MEDIA_SOUND_MANAGER_UNIT_TEST_H #define MEDIA_SOUND_MANAGER_UNIT_TEST_H -#include "../../../system_sound_manager_impl.h" #include -#include "context_impl.h" -#include "tone_attrs.h" namespace OHOS { namespace Media { diff --git a/frameworks/native/system_sound_manager/unittest/sound_manager_test/src/system_sound_manager_unit_test.cpp b/frameworks/native/system_sound_manager/unittest/sound_manager_test/src/system_sound_manager_unit_test.cpp index cfa019b9ca1577eddf7adfca148407a477ba32b0..aa022bb5fea165d7debe98fe7b15e1cfa037ac01 100644 --- a/frameworks/native/system_sound_manager/unittest/sound_manager_test/src/system_sound_manager_unit_test.cpp +++ b/frameworks/native/system_sound_manager/unittest/sound_manager_test/src/system_sound_manager_unit_test.cpp @@ -13,6 +13,11 @@ * limitations under the License. */ #include "system_sound_manager_unit_test.h" +#include "access_token.h" +#include "../../../system_sound_manager_impl.h" +#include "context_impl.h" +#include "tone_attrs.h" +#include "system_sound_manager_utils.h" using namespace OHOS::AbilityRuntime; using namespace testing::ext; @@ -21,6 +26,8 @@ namespace OHOS { namespace Media { const int ERROR = -1; const int SUCCESS = 0; +const int32_t PARAM1 = 1; +const int INVALID_FD = -1; const int RESULT_DEFAULT = -13; const int RESULT_SUCCESS = -3; constexpr int32_t TONE_CATEGORY_DEFAULT = 8; @@ -911,6 +918,232 @@ HWTEST(SystemSoundManagerUnitTest, Media_SoundManager_AddCustomizedToneByFdAndOf systemSoundManager_->Close(fd); } +/** + * @tc.name : Test AddCustomizedToneByFdAndOffset API + * @tc.number: Media_SoundManager_AddCustomizedToneByFdAndOffset_004 + * @tc.desc : Test AddCustomizedToneByFdAndOffset interface. Returns attributes of the default system tone. + */ +HWTEST(SystemSoundManagerUnitTest, Media_SoundManager_AddCustomizedToneByFdAndOffset_004, TestSize.Level2) +{ + std::shared_ptr systemSoundManager_ = std::make_shared(); + std::shared_ptr context_ = std::make_shared(); + std::shared_ptr toneAttrs_ = std::make_shared("default", + "default", "default", CUSTOMISED, TONE_CATEGORY_RINGTONE); + auto vec = systemSoundManager_->GetAlarmToneAttrList(context_); + std::string uri = ""; + if (vec.size() > 0) { + uri = vec[0]->GetUri(); + } + std::shared_ptr dataShareHelper = + SystemSoundManagerUtils::CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID); + if (dataShareHelper == nullptr) { + std::cout << "dataShareHelper is nullptr"<< std::endl; + return; + } + std::tuple resultOfOpen = std::make_tuple(uri, INVALID_FD, ERROR_IO); + systemSoundManager_->OpenOneFile(dataShareHelper, uri, resultOfOpen); + int64_t srcFd = std::get(resultOfOpen); + std::string res; + toneAttrs_->SetMediaType(ToneMediaType::MEDIA_TYPE_VID); + toneAttrs_->SetTitle("06172"); + toneAttrs_->SetFileName("06172.mp4"); + res = systemSoundManager_->AddCustomizedToneByFdAndOffset(context_, toneAttrs_, srcFd, 10, 0); + EXPECT_EQ(res.empty(), true); + toneAttrs_->SetCategory(TONE_CATEGORY_INVALID); + res = systemSoundManager_->AddCustomizedToneByFdAndOffset(context_, toneAttrs_, srcFd, 10, 0); + EXPECT_EQ(res.empty(), true); + toneAttrs_->SetCategory(TONE_CATEGORY_TEXT_MESSAGE); + res = systemSoundManager_->AddCustomizedToneByFdAndOffset(context_, toneAttrs_, srcFd, 10, 0); + EXPECT_EQ(res.empty(), true); + toneAttrs_->SetCategory(TONE_CATEGORY_NOTIFICATION); + res = systemSoundManager_->AddCustomizedToneByFdAndOffset(context_, toneAttrs_, srcFd, 10, 0); + EXPECT_EQ(res.empty(), true); + toneAttrs_->SetCategory(TONE_CATEGORY_ALARM); + res = systemSoundManager_->AddCustomizedToneByFdAndOffset(context_, toneAttrs_, srcFd, 10, 0); + EXPECT_EQ(res.empty(), true); + toneAttrs_->SetCategory(TONE_CATEGORY_CONTACTS); + res = systemSoundManager_->AddCustomizedToneByFdAndOffset(context_, toneAttrs_, srcFd, 10, 0); + EXPECT_EQ(res.empty(), true); +} + +/** + * @tc.name : Test AddCustomizedToneByFdAndOffset API + * @tc.number: Media_SoundManager_AddCustomizedToneByFdAndOffset_005 + * @tc.desc : Test AddCustomizedToneByFdAndOffset interface. Returns attributes of the default system tone. + */ +HWTEST(SystemSoundManagerUnitTest, Media_SoundManager_AddCustomizedToneByFdAndOffset_005, TestSize.Level2) +{ + AccessToken token; + std::shared_ptr systemSoundManager_ = std::make_shared(); + std::shared_ptr context_ = std::make_shared(); + std::shared_ptr toneAttrs_ = std::make_shared("default", + "default", "default", CUSTOMISED, TONE_CATEGORY_RINGTONE); + auto vec = systemSoundManager_->GetAlarmToneAttrList(context_); + std::string uri = ""; + if (vec.size() > 0) { + uri = vec[0]->GetUri(); + } + std::shared_ptr dataShareHelper = + SystemSoundManagerUtils::CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID); + if (dataShareHelper == nullptr) { + std::cout << "dataShareHelper is nullptr"<< std::endl; + } + std::tuple resultOfOpen = std::make_tuple(uri, INVALID_FD, ERROR_IO); + systemSoundManager_->OpenOneFile(dataShareHelper, uri, resultOfOpen); + int64_t srcFd = std::get(resultOfOpen); + std::string res; + toneAttrs_->SetMediaType(ToneMediaType::MEDIA_TYPE_VID); + toneAttrs_->SetTitle("06172"); + toneAttrs_->SetFileName("06172.mp4"); + res = systemSoundManager_->AddCustomizedToneByFdAndOffset(context_, toneAttrs_, srcFd, 0, 1024); + EXPECT_EQ(res.empty(), false); + res = systemSoundManager_->AddCustomizedToneByFdAndOffset(context_, toneAttrs_, srcFd, 0, 1024); + EXPECT_EQ(res.empty(), false); + toneAttrs_->SetTitle("06173"); + toneAttrs_->SetFileName("06173.mp4"); + res = systemSoundManager_->AddCustomizedToneByFdAndOffset(context_, toneAttrs_, srcFd, 0, 1024); + EXPECT_EQ(res.empty(), false); +} + +/** + * @tc.name : Test GetCurrentRingtoneAttribute API + * @tc.number: Media_SoundManager_GetCurrentRingtoneAttribute_001 + * @tc.desc : Test GetCurrentRingtoneAttribute interface. + */ +HWTEST(SystemSoundManagerUnitTest, Media_SoundManager_GetCurrentRingtoneAttribute_001, TestSize.Level2) +{ + std::shared_ptr systemSoundManager_ = std::make_shared(); + std::shared_ptr dataShareHelper = + SystemSoundManagerUtils::CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID); + int32_t toneId = 1; + RingtoneType ringtoneType = RingtoneType::RINGTONE_TYPE_SIM_CARD_1; + int32_t num = 0; + systemSoundManager_->UpdateRingtoneUri(dataShareHelper, toneId, ringtoneType, num); + ToneAttrs toneAttrs_ = systemSoundManager_->GetCurrentRingtoneAttribute(RINGTONE_TYPE_SIM_CARD_0); + EXPECT_EQ(toneAttrs_.GetUri().empty(), false); + toneAttrs_ = systemSoundManager_->GetCurrentRingtoneAttribute(static_cast(2)); + EXPECT_EQ(toneAttrs_.GetUri().empty(), true); +} + +/** + * @tc.name : Test GetRingtoneAttrs API + * @tc.number: Media_SoundManager_GetRingtoneAttrs_001 + * @tc.desc : Test GetRingtoneAttrs interface. + */ +HWTEST(SystemSoundManagerUnitTest, Media_SoundManager_GetRingtoneAttrs_001, TestSize.Level2) +{ + std::shared_ptr systemSoundManager_ = std::make_shared(); + bool isProxy = false; + DatabaseTool databaseTool = {true, isProxy, nullptr}; + ToneAttrs toneAttrs_ = systemSoundManager_->GetRingtoneAttrs(databaseTool, RINGTONE_TYPE_SIM_CARD_0); + EXPECT_EQ(toneAttrs_.GetUri().empty(), true); + std::shared_ptr dataShareHelper = + SystemSoundManagerUtils::CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID); + databaseTool = {true, isProxy, dataShareHelper}; + toneAttrs_ = systemSoundManager_->GetRingtoneAttrs(databaseTool, static_cast(2)); + EXPECT_EQ(toneAttrs_.GetUri().empty(), false); +} + +/** + * @tc.name : Test GetRingtoneAttrsByType API + * @tc.number: Media_SoundManager_GetRingtoneAttrsByType_001 + * @tc.desc : Test GetRingtoneAttrs interface. + */ +HWTEST(SystemSoundManagerUnitTest, Media_SoundManager_GetRingtoneAttrsByType_001, TestSize.Level2) +{ + std::shared_ptr systemSoundManager_ = std::make_shared(); + bool isProxy = false; + DatabaseTool databaseTool = {true, isProxy, nullptr}; + ToneAttrs toneAttrs_ = + systemSoundManager_->GetRingtoneAttrsByType(databaseTool, std::to_string(RINGTONE_TYPE_SIM_CARD_0)); + EXPECT_EQ(toneAttrs_.GetUri().empty(), true); + std::shared_ptr dataShareHelper = + SystemSoundManagerUtils::CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID); + isProxy = true; + databaseTool = {true, isProxy, dataShareHelper}; + toneAttrs_ = systemSoundManager_->GetRingtoneAttrsByType(databaseTool, std::to_string(RINGTONE_TYPE_SIM_CARD_0)); + EXPECT_EQ(toneAttrs_.GetUri().empty(), true); + isProxy = false; + databaseTool = {true, isProxy, dataShareHelper}; + toneAttrs_ = systemSoundManager_->GetRingtoneAttrsByType(databaseTool, std::to_string(3)); + EXPECT_EQ(toneAttrs_.GetUri().empty(), true); +} + +/** + * @tc.name : Test GetPresetRingToneAttrByType API + * @tc.number: Media_SoundManager_GetPresetRingToneAttrByType_001 + * @tc.desc : Test GetRingtoneAttrs interface. + */ +HWTEST(SystemSoundManagerUnitTest, Media_SoundManager_GetPresetRingToneAttrByType_001, TestSize.Level2) +{ + std::shared_ptr systemSoundManager_ = std::make_shared(); + bool isProxy = false; + DatabaseTool databaseTool = {true, isProxy, nullptr}; + ToneAttrs toneAttrs_ = + systemSoundManager_->GetPresetRingToneAttrByType(databaseTool, std::to_string(RINGTONE_TYPE_SIM_CARD_0)); + EXPECT_EQ(toneAttrs_.GetUri().empty(), true); + std::shared_ptr dataShareHelper = + SystemSoundManagerUtils::CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID); + isProxy = true; + databaseTool = {true, isProxy, dataShareHelper}; + toneAttrs_ = + systemSoundManager_->GetPresetRingToneAttrByType(databaseTool, std::to_string(RINGTONE_TYPE_SIM_CARD_0)); + EXPECT_EQ(toneAttrs_.GetUri().empty(), true); + isProxy = false; + databaseTool = {true, isProxy, dataShareHelper}; + toneAttrs_ = systemSoundManager_->GetPresetRingToneAttrByType(databaseTool, std::to_string(3)); + EXPECT_EQ(toneAttrs_.GetUri().empty(), true); +} + +/** + * @tc.name : Test OpenToneList API + * @tc.number: Media_SoundManager_OpenToneList_001 + * @tc.desc : Test GetRingtoneAttrs interface. + */ +HWTEST(SystemSoundManagerUnitTest, Media_SoundManager_OpenToneList_001, TestSize.Level2) +{ + std::shared_ptr systemSoundManager_ = std::make_shared(); + std::vector uriList; + SystemSoundError errCode; + uriList.push_back("/data/storage/el2/base/files/Ringtone/ringtones/06172.mp4"); + uriList.push_back("/data/storage/el2/base/files/Ringtone/ringtones/06173.mp4"); + uriList.push_back("/data/storage/el2/base/files/Ringtone/ringtones/06174.mp4"); + std::vector> resultVec = + systemSoundManager_->OpenToneList(uriList, errCode); + EXPECT_EQ(errCode, ERROR_OK); + for (int i = 0; i < 1024; i++) { + uriList.push_back("/data/storage/el2/base/files/Ringtone/ringtones/06174.mp4"); + } + resultVec = systemSoundManager_->OpenToneList(uriList, errCode); + EXPECT_EQ(errCode, ERROR_INVALID_PARAM); +} + +/** + * @tc.name : Test RemoveCustomizedToneList API + * @tc.number: Media_SoundManager_RemoveCustomizedToneList_001 + * @tc.desc : Test GetRingtoneAttrs interface. + */ +HWTEST(SystemSoundManagerUnitTest, Media_SoundManager_RemoveCustomizedToneList_001, TestSize.Level2) +{ + std::shared_ptr systemSoundManager_ = std::make_shared(); + std::vector uriList; + SystemSoundError errCode; + uriList.push_back("/data/storage/el2/base/files/Ringtone/ringtones/06172.mp4"); + uriList.push_back("/data/storage/el2/base/files/Ringtone/ringtones/06173.mp4"); + uriList.push_back("/data/storage/el2/base/files/Ringtone/ringtones/06174.mp4"); + std::vector> resultVec = + systemSoundManager_->RemoveCustomizedToneList(uriList, errCode); + EXPECT_EQ(errCode, ERROR_OK); + AccessToken token; + resultVec = systemSoundManager_->RemoveCustomizedToneList(uriList, errCode); + EXPECT_EQ(resultVec.size(), 3); + for (int i = 0; i < 1024; i++) { + uriList.push_back("/data/storage/el2/base/files/Ringtone/ringtones/06174.mp4"); + } + resultVec = systemSoundManager_->RemoveCustomizedToneList(uriList, errCode); + EXPECT_EQ(errCode, ERROR_INVALID_PARAM); +} + /** * @tc.name : Test AddCustomizedToneByFd API * @tc.number: Media_SoundManager_AddCustomizedToneByFd_001 diff --git a/frameworks/native/system_sound_manager/unittest/tone_player_test/BUILD.gn b/frameworks/native/system_sound_manager/unittest/tone_player_test/BUILD.gn index 50aaf641d815df86d69144db2ebdb4eb2be84cda..85de0f5f3da89c28a698a0fc4149a2159b972ba0 100644 --- a/frameworks/native/system_sound_manager/unittest/tone_player_test/BUILD.gn +++ b/frameworks/native/system_sound_manager/unittest/tone_player_test/BUILD.gn @@ -81,6 +81,7 @@ ohos_unittest("system_tone_player_unit_test") { "ringtone_library:ringtone_data_extension", "ringtone_library:ringtone_data_helper", "samgr:samgr_proxy", + "hitrace:hitrace_meter", ] resource_config_file = "../../../../../test/unittest/resources/ohos_test.xml" diff --git a/interfaces/inner_api/native/system_sound_manager/include/tone_attrs.h b/interfaces/inner_api/native/system_sound_manager/include/tone_attrs.h index 5660156ef9d1605b05d8a33e33402d90b5b9bb08..7f7f43f974a0c0a530b3109f464e57cb9ae57a6c 100644 --- a/interfaces/inner_api/native/system_sound_manager/include/tone_attrs.h +++ b/interfaces/inner_api/native/system_sound_manager/include/tone_attrs.h @@ -19,7 +19,6 @@ #include #include "audio_info.h" -#include "media_core.h" namespace OHOS { namespace Media { @@ -29,6 +28,17 @@ enum ToneCustomizedType { CUSTOMISED = 1, }; +enum class ToneMediaType : int32_t { + /** + * track is audio. + */ + MEDIA_TYPE_AUD = 0, + /** + * track is video. + */ + MEDIA_TYPE_VID = 1, +}; + constexpr int32_t TONE_CATEGORY_INVALID = -1; constexpr int32_t TONE_CATEGORY_RINGTONE = 1; constexpr int32_t TONE_CATEGORY_TEXT_MESSAGE = 2; @@ -93,12 +103,12 @@ public: return category_; } - void SetMediaType(const MediaType mediaType) + void SetMediaType(const ToneMediaType mediaType) { mediaType_ = mediaType; } - MediaType GetMediaType() const + ToneMediaType GetMediaType() const { return mediaType_; } @@ -109,7 +119,7 @@ private: std::string uri_ = "uri_test"; ToneCustomizedType custType_ = CUSTOMISED; int32_t category_ = 0; - MediaType mediaType_ = MediaType::MEDIA_TYPE_AUD; + ToneMediaType mediaType_ = ToneMediaType::MEDIA_TYPE_AUD; }; } // namespace Media } // namespace OHOS