1 /*
2 * Copyright (c) 2024 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 "OHAudioManager.h"
17
18 #include <set>
19
20 #include "audio_info.h"
21 #include "audio_common_log.h"
22 #include "audio_system_manager.h"
23
24 namespace {
25 // should be same with OH_AudioScene in native_audio_common.h
26 const std::set<OHOS::AudioStandard::AudioScene> INVALID_AUDIO_SCENES = {
27 OHOS::AudioStandard::AUDIO_SCENE_DEFAULT,
28 OHOS::AudioStandard::AUDIO_SCENE_RINGING,
29 OHOS::AudioStandard::AUDIO_SCENE_PHONE_CALL,
30 OHOS::AudioStandard::AUDIO_SCENE_PHONE_CHAT,
31 OHOS::AudioStandard::AUDIO_SCENE_VOICE_RINGING
32 };
33 }
34 using OHOS::AudioStandard::OHAudioManager;
convertManager(OH_AudioManager * audioManager)35 static OHOS::AudioStandard::OHAudioManager *convertManager(OH_AudioManager *audioManager)
36 {
37 return (OHAudioManager*) audioManager;
38 }
39
OH_GetAudioManager(OH_AudioManager ** audioManager)40 OH_AudioCommon_Result OH_GetAudioManager(OH_AudioManager **audioManager)
41 {
42 if (audioManager == nullptr) {
43 AUDIO_ERR_LOG("invalid OH_AudioManager");
44 return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
45 }
46 OHAudioManager *manager = OHAudioManager::GetInstance();
47 *audioManager = (OH_AudioManager *)manager;
48 return AUDIOCOMMON_RESULT_SUCCESS;
49 }
50
OH_GetAudioScene(OH_AudioManager * manager,OH_AudioScene * scene)51 OH_AudioCommon_Result OH_GetAudioScene(OH_AudioManager* manager, OH_AudioScene *scene)
52 {
53 if (manager == nullptr || scene == nullptr) {
54 AUDIO_ERR_LOG("invalid OH_AudioManager or scene");
55 return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
56 }
57 *scene = static_cast<OH_AudioScene>(convertManager(manager)->GetAudioScene());
58 return AUDIOCOMMON_RESULT_SUCCESS;
59 }
60
61 namespace OHOS {
62 namespace AudioStandard {
GetInstance()63 OHAudioManager *OHAudioManager::GetInstance()
64 {
65 static OHAudioManager manager;
66 return &manager;
67 }
68
GetAudioScene()69 AudioScene OHAudioManager::GetAudioScene()
70 {
71 AudioScene scene = AudioSystemManager::GetInstance()->GetAudioScene();
72 if (!INVALID_AUDIO_SCENES.count(scene)) {
73 AUDIO_WARNING_LOG("Get scene:%{public}d that is not defined, return defalut!", scene);
74 return AUDIO_SCENE_DEFAULT;
75 }
76 if (scene == AUDIO_SCENE_VOICE_RINGING) {
77 return AUDIO_SCENE_RINGING;
78 }
79 return scene;
80 }
81 } // namespace AudioStandard
82 } // namespace OHOS
83