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 #ifndef LOG_TAG
17 #define LOG_TAG "OHAudioSessionManager"
18 #endif
19
20 #include "OHAudioSessionManager.h"
21 #include <ostream>
22 #include <iostream>
23
24 using OHOS::AudioStandard::OHAudioSessionManager;
25 using OHOS::AudioStandard::AudioSessionManager;
26 using namespace std;
27
convertManager(OH_AudioSessionManager * manager)28 static OHOS::AudioStandard::OHAudioSessionManager *convertManager(OH_AudioSessionManager* manager)
29 {
30 return (OHAudioSessionManager*) manager;
31 }
32
33
OH_AudioManager_GetAudioSessionManager(OH_AudioSessionManager ** audioSessionManager)34 OH_AudioCommon_Result OH_AudioManager_GetAudioSessionManager(OH_AudioSessionManager **audioSessionManager)
35 {
36 OHAudioSessionManager* ohAudioSessionManager = OHAudioSessionManager::GetInstance();
37 if (audioSessionManager == nullptr) {
38 AUDIO_ERR_LOG("audioSessionManager is nullptr");
39 }
40 *audioSessionManager = reinterpret_cast<OH_AudioSessionManager*>(ohAudioSessionManager);
41 return AUDIOCOMMON_RESULT_SUCCESS;
42 }
43
OH_AudioSessionManager_RegisterSessionDeactivatedCallback(OH_AudioSessionManager * audioSessionManager,OH_AudioSession_DeactivatedCallback callback)44 OH_AudioCommon_Result OH_AudioSessionManager_RegisterSessionDeactivatedCallback(
45 OH_AudioSessionManager *audioSessionManager, OH_AudioSession_DeactivatedCallback callback)
46 {
47 OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager);
48 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr,
49 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr");
50 CHECK_AND_RETURN_RET_LOG(callback != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "callback is nullptr");
51 return ohAudioSessionManager->SetAudioSessionCallback(callback);
52 }
53
OH_AudioSessionManager_UnregisterSessionDeactivatedCallback(OH_AudioSessionManager * audioSessionManager,OH_AudioSession_DeactivatedCallback callback)54 OH_AudioCommon_Result OH_AudioSessionManager_UnregisterSessionDeactivatedCallback(
55 OH_AudioSessionManager *audioSessionManager, OH_AudioSession_DeactivatedCallback callback)
56 {
57 OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager);
58 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr,
59 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr");
60 CHECK_AND_RETURN_RET_LOG(callback != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "callback is nullptr");
61 return ohAudioSessionManager->UnsetAudioSessionCallback(callback);
62 }
63
OH_AudioSessionManager_ActivateAudioSession(OH_AudioSessionManager * audioSessionManager,const OH_AudioSession_Strategy * strategy)64 OH_AudioCommon_Result OH_AudioSessionManager_ActivateAudioSession(
65 OH_AudioSessionManager *audioSessionManager, const OH_AudioSession_Strategy *strategy)
66 {
67 OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager);
68 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr,
69 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr");
70 CHECK_AND_RETURN_RET_LOG(strategy != nullptr,
71 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "strategy is nullptr");
72 OHOS::AudioStandard::AudioSessionStrategy audioStrategy;
73 audioStrategy.concurrencyMode =
74 static_cast<OHOS::AudioStandard::AudioConcurrencyMode>(strategy->concurrencyMode);
75 return ohAudioSessionManager->ActivateAudioSession(audioStrategy);
76 }
77
OH_AudioSessionManager_DeactivateAudioSession(OH_AudioSessionManager * audioSessionManager)78 OH_AudioCommon_Result OH_AudioSessionManager_DeactivateAudioSession(
79 OH_AudioSessionManager *audioSessionManager)
80 {
81 OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager);
82 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr,
83 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr");
84 return ohAudioSessionManager->DeactivateAudioSession();
85 }
86
OH_AudioSessionManager_IsAudioSessionActivated(OH_AudioSessionManager * audioSessionManager)87 bool OH_AudioSessionManager_IsAudioSessionActivated(
88 OH_AudioSessionManager *audioSessionManager)
89 {
90 OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager);
91 CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr, false, "ohAudioSessionManager is nullptr");
92 return ohAudioSessionManager->IsAudioSessionActivated();
93 }
94
95
96 namespace OHOS {
97 namespace AudioStandard {
98
OHAudioSessionManager()99 OHAudioSessionManager::OHAudioSessionManager()
100 {
101 AUDIO_INFO_LOG("OHAudioSessionManager created!");
102 }
103
~OHAudioSessionManager()104 OHAudioSessionManager::~OHAudioSessionManager()
105 {
106 AUDIO_INFO_LOG("OHAudioSessionManager destroyed!");
107 }
108
SetAudioSessionCallback(OH_AudioSession_DeactivatedCallback callback)109 OH_AudioCommon_Result OHAudioSessionManager::SetAudioSessionCallback(OH_AudioSession_DeactivatedCallback callback)
110 {
111 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr,
112 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "failed, audioSessionManager_ is null");
113 std::shared_ptr<OHAudioSessionCallback> ohAudioSessionCallback =
114 std::make_shared<OHAudioSessionCallback>(callback);
115 audioSessionManager_->SetAudioSessionCallback(ohAudioSessionCallback);
116 return AUDIOCOMMON_RESULT_SUCCESS;
117 }
118
UnsetAudioSessionCallback(OH_AudioSession_DeactivatedCallback callback)119 OH_AudioCommon_Result OHAudioSessionManager::UnsetAudioSessionCallback(OH_AudioSession_DeactivatedCallback callback)
120 {
121 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr,
122 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "failed, audioSessionManager_ is null");
123 std::shared_ptr<OHAudioSessionCallback> ohAudioSessionCallback =
124 std::make_shared<OHAudioSessionCallback>(callback);
125 audioSessionManager_->UnsetAudioSessionCallback(ohAudioSessionCallback);
126 return AUDIOCOMMON_RESULT_SUCCESS;
127 }
128
ActivateAudioSession(const AudioSessionStrategy & strategy)129 OH_AudioCommon_Result OHAudioSessionManager::ActivateAudioSession(const AudioSessionStrategy &strategy)
130 {
131 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr,
132 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "failed, audioSessionManager_ is null");
133 int32_t ret = audioSessionManager_->ActivateAudioSession(strategy);
134 if (ret == 0) {
135 return AUDIOCOMMON_RESULT_SUCCESS;
136 } else {
137 return AUDIOCOMMON_RESULT_ERROR_ILLEGAL_STATE;
138 }
139 }
140
DeactivateAudioSession()141 OH_AudioCommon_Result OHAudioSessionManager::DeactivateAudioSession()
142 {
143 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr,
144 AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "failed, audioSessionManager_ is null");
145 int32_t ret = audioSessionManager_->DeactivateAudioSession();
146 if (ret == 0) {
147 return AUDIOCOMMON_RESULT_SUCCESS;
148 } else {
149 return AUDIOCOMMON_RESULT_ERROR_ILLEGAL_STATE;
150 }
151 }
152
IsAudioSessionActivated()153 bool OHAudioSessionManager::IsAudioSessionActivated()
154 {
155 CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr, false, "failed, audioSessionManager_ is null");
156 return audioSessionManager_->IsAudioSessionActivated();
157 }
158
OnAudioSessionDeactive(const AudioSessionDeactiveEvent & deactiveEvent)159 void OHAudioSessionCallback::OnAudioSessionDeactive(const AudioSessionDeactiveEvent &deactiveEvent)
160 {
161 OH_AudioSession_DeactivatedEvent event;
162 event.reason = static_cast<OH_AudioSession_DeactivatedReason>(deactiveEvent.deactiveReason);
163 callback_(event);
164 }
165
166 } // namespace AudioStandard
167 } // namespace OHOS