• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 "media_errors.h"
17 #include "media_log.h"
18 #include "soundpool_manager.h"
19 
20 namespace {
21     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SOUNDPOOL, "SoundPoolManager"};
22 }
23 
24 namespace OHOS {
25 namespace Media {
~SoundPoolManager()26 SoundPoolManager::~SoundPoolManager()
27 {
28     MEDIA_LOGI("Destruction SoundPoolManager.");
29     std::lock_guard<std::mutex> lock(mutex_);
30     soundPools_.clear();
31 };
32 
GetSoundPool(const pid_t pid,std::shared_ptr<SoundPool> & soundPool)33 int32_t SoundPoolManager::GetSoundPool(const pid_t pid, std::shared_ptr<SoundPool>& soundPool)
34 {
35     std::lock_guard<std::mutex> lock(mutex_);
36     auto it = soundPools_.find(pid);
37     if (it != soundPools_.end()) {
38         MEDIA_LOGI("SoundPool have setted, use old object.");
39         soundPool = it->second;
40         return MSERR_OK;
41     }
42     soundPool = std::make_shared<SoundPool>();
43     CHECK_AND_RETURN_RET_LOG(soundPool != nullptr, MSERR_INVALID_VAL,
44         "SoundPoolManager::GetSoundPool invalid soundPool");
45     soundPools_.emplace(pid, soundPool);
46     return MSERR_OK;
47 }
48 
Release(const pid_t pid)49 int32_t SoundPoolManager::Release(const pid_t pid)
50 {
51     std::lock_guard<std::mutex> lock(mutex_);
52     auto it = soundPools_.find(pid);
53     if (it != soundPools_.end()) {
54         MEDIA_LOGI("Release soundpool, pid:%{public}d.", pid);
55         soundPools_.erase(it);
56         return MSERR_OK;
57     }
58     return MSERR_OK;
59 }
60 } // namespace Media
61 } // namespace OHOS
62