146f34cbfSopenharmony_ci/* 246f34cbfSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 346f34cbfSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 446f34cbfSopenharmony_ci * you may not use this file except in compliance with the License. 546f34cbfSopenharmony_ci * You may obtain a copy of the License at 646f34cbfSopenharmony_ci * 746f34cbfSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 846f34cbfSopenharmony_ci * 946f34cbfSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1046f34cbfSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1146f34cbfSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1246f34cbfSopenharmony_ci * See the License for the specific language governing permissions and 1346f34cbfSopenharmony_ci * limitations under the License. 1446f34cbfSopenharmony_ci */ 1546f34cbfSopenharmony_ci 1646f34cbfSopenharmony_ci#ifndef LOG_TAG 1746f34cbfSopenharmony_ci#define LOG_TAG "OHAudioSessionManager" 1846f34cbfSopenharmony_ci#endif 1946f34cbfSopenharmony_ci 2046f34cbfSopenharmony_ci#include "OHAudioSessionManager.h" 2146f34cbfSopenharmony_ci#include <ostream> 2246f34cbfSopenharmony_ci#include <iostream> 2346f34cbfSopenharmony_ci 2446f34cbfSopenharmony_ciusing OHOS::AudioStandard::OHAudioSessionManager; 2546f34cbfSopenharmony_ciusing OHOS::AudioStandard::AudioSessionManager; 2646f34cbfSopenharmony_ciusing namespace std; 2746f34cbfSopenharmony_ci 2846f34cbfSopenharmony_cistatic OHOS::AudioStandard::OHAudioSessionManager *convertManager(OH_AudioSessionManager* manager) 2946f34cbfSopenharmony_ci{ 3046f34cbfSopenharmony_ci return (OHAudioSessionManager*) manager; 3146f34cbfSopenharmony_ci} 3246f34cbfSopenharmony_ci 3346f34cbfSopenharmony_ci 3446f34cbfSopenharmony_ciOH_AudioCommon_Result OH_AudioManager_GetAudioSessionManager(OH_AudioSessionManager **audioSessionManager) 3546f34cbfSopenharmony_ci{ 3646f34cbfSopenharmony_ci OHAudioSessionManager* ohAudioSessionManager = OHAudioSessionManager::GetInstance(); 3746f34cbfSopenharmony_ci *audioSessionManager = reinterpret_cast<OH_AudioSessionManager*>(ohAudioSessionManager); 3846f34cbfSopenharmony_ci return AUDIOCOMMON_RESULT_SUCCESS; 3946f34cbfSopenharmony_ci} 4046f34cbfSopenharmony_ci 4146f34cbfSopenharmony_ciOH_AudioCommon_Result OH_AudioSessionManager_RegisterSessionDeactivatedCallback( 4246f34cbfSopenharmony_ci OH_AudioSessionManager *audioSessionManager, OH_AudioSession_DeactivatedCallback callback) 4346f34cbfSopenharmony_ci{ 4446f34cbfSopenharmony_ci OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager); 4546f34cbfSopenharmony_ci CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr, 4646f34cbfSopenharmony_ci AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr"); 4746f34cbfSopenharmony_ci CHECK_AND_RETURN_RET_LOG(callback != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "callback is nullptr"); 4846f34cbfSopenharmony_ci return ohAudioSessionManager->SetAudioSessionCallback(callback); 4946f34cbfSopenharmony_ci} 5046f34cbfSopenharmony_ci 5146f34cbfSopenharmony_ciOH_AudioCommon_Result OH_AudioSessionManager_UnregisterSessionDeactivatedCallback( 5246f34cbfSopenharmony_ci OH_AudioSessionManager *audioSessionManager, OH_AudioSession_DeactivatedCallback callback) 5346f34cbfSopenharmony_ci{ 5446f34cbfSopenharmony_ci OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager); 5546f34cbfSopenharmony_ci CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr, 5646f34cbfSopenharmony_ci AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr"); 5746f34cbfSopenharmony_ci CHECK_AND_RETURN_RET_LOG(callback != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "callback is nullptr"); 5846f34cbfSopenharmony_ci return ohAudioSessionManager->UnsetAudioSessionCallback(callback); 5946f34cbfSopenharmony_ci} 6046f34cbfSopenharmony_ci 6146f34cbfSopenharmony_ciOH_AudioCommon_Result OH_AudioSessionManager_ActivateAudioSession( 6246f34cbfSopenharmony_ci OH_AudioSessionManager *audioSessionManager, const OH_AudioSession_Strategy *strategy) 6346f34cbfSopenharmony_ci{ 6446f34cbfSopenharmony_ci OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager); 6546f34cbfSopenharmony_ci CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr, 6646f34cbfSopenharmony_ci AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr"); 6746f34cbfSopenharmony_ci OHOS::AudioStandard::AudioSessionStrategy audioStrategy; 6846f34cbfSopenharmony_ci audioStrategy.concurrencyMode = 6946f34cbfSopenharmony_ci static_cast<OHOS::AudioStandard::AudioConcurrencyMode>(strategy->concurrencyMode); 7046f34cbfSopenharmony_ci return ohAudioSessionManager->ActivateAudioSession(audioStrategy); 7146f34cbfSopenharmony_ci} 7246f34cbfSopenharmony_ci 7346f34cbfSopenharmony_ciOH_AudioCommon_Result OH_AudioSessionManager_DeactivateAudioSession( 7446f34cbfSopenharmony_ci OH_AudioSessionManager *audioSessionManager) 7546f34cbfSopenharmony_ci{ 7646f34cbfSopenharmony_ci OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager); 7746f34cbfSopenharmony_ci CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr, 7846f34cbfSopenharmony_ci AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "ohAudioSessionManager is nullptr"); 7946f34cbfSopenharmony_ci return ohAudioSessionManager->DeactivateAudioSession(); 8046f34cbfSopenharmony_ci} 8146f34cbfSopenharmony_ci 8246f34cbfSopenharmony_cibool OH_AudioSessionManager_IsAudioSessionActivated( 8346f34cbfSopenharmony_ci OH_AudioSessionManager *audioSessionManager) 8446f34cbfSopenharmony_ci{ 8546f34cbfSopenharmony_ci OHAudioSessionManager* ohAudioSessionManager = convertManager(audioSessionManager); 8646f34cbfSopenharmony_ci CHECK_AND_RETURN_RET_LOG(ohAudioSessionManager != nullptr, false, "ohAudioSessionManager is nullptr"); 8746f34cbfSopenharmony_ci return ohAudioSessionManager->IsAudioSessionActivated(); 8846f34cbfSopenharmony_ci} 8946f34cbfSopenharmony_ci 9046f34cbfSopenharmony_ci 9146f34cbfSopenharmony_cinamespace OHOS { 9246f34cbfSopenharmony_cinamespace AudioStandard { 9346f34cbfSopenharmony_ci 9446f34cbfSopenharmony_ciOHAudioSessionManager::OHAudioSessionManager() 9546f34cbfSopenharmony_ci{ 9646f34cbfSopenharmony_ci AUDIO_INFO_LOG("OHAudioSessionManager created!"); 9746f34cbfSopenharmony_ci} 9846f34cbfSopenharmony_ci 9946f34cbfSopenharmony_ciOHAudioSessionManager::~OHAudioSessionManager() 10046f34cbfSopenharmony_ci{ 10146f34cbfSopenharmony_ci AUDIO_INFO_LOG("OHAudioSessionManager destroyed!"); 10246f34cbfSopenharmony_ci} 10346f34cbfSopenharmony_ci 10446f34cbfSopenharmony_ciOH_AudioCommon_Result OHAudioSessionManager::SetAudioSessionCallback(OH_AudioSession_DeactivatedCallback callback) 10546f34cbfSopenharmony_ci{ 10646f34cbfSopenharmony_ci CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr, 10746f34cbfSopenharmony_ci AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "failed, audioSessionManager_ is null"); 10846f34cbfSopenharmony_ci std::shared_ptr<OHAudioSessionCallback> ohAudioSessionCallback = 10946f34cbfSopenharmony_ci std::make_shared<OHAudioSessionCallback>(callback); 11046f34cbfSopenharmony_ci audioSessionManager_->SetAudioSessionCallback(ohAudioSessionCallback); 11146f34cbfSopenharmony_ci return AUDIOCOMMON_RESULT_SUCCESS; 11246f34cbfSopenharmony_ci} 11346f34cbfSopenharmony_ci 11446f34cbfSopenharmony_ciOH_AudioCommon_Result OHAudioSessionManager::UnsetAudioSessionCallback(OH_AudioSession_DeactivatedCallback callback) 11546f34cbfSopenharmony_ci{ 11646f34cbfSopenharmony_ci CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr, 11746f34cbfSopenharmony_ci AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "failed, audioSessionManager_ is null"); 11846f34cbfSopenharmony_ci std::shared_ptr<OHAudioSessionCallback> ohAudioSessionCallback = 11946f34cbfSopenharmony_ci std::make_shared<OHAudioSessionCallback>(callback); 12046f34cbfSopenharmony_ci audioSessionManager_->UnsetAudioSessionCallback(ohAudioSessionCallback); 12146f34cbfSopenharmony_ci return AUDIOCOMMON_RESULT_SUCCESS; 12246f34cbfSopenharmony_ci} 12346f34cbfSopenharmony_ci 12446f34cbfSopenharmony_ciOH_AudioCommon_Result OHAudioSessionManager::ActivateAudioSession(const AudioSessionStrategy &strategy) 12546f34cbfSopenharmony_ci{ 12646f34cbfSopenharmony_ci CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr, 12746f34cbfSopenharmony_ci AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "failed, audioSessionManager_ is null"); 12846f34cbfSopenharmony_ci int32_t ret = audioSessionManager_->ActivateAudioSession(strategy); 12946f34cbfSopenharmony_ci if (ret == 0) { 13046f34cbfSopenharmony_ci return AUDIOCOMMON_RESULT_SUCCESS; 13146f34cbfSopenharmony_ci } else { 13246f34cbfSopenharmony_ci return AUDIOCOMMON_RESULT_ERROR_ILLEGAL_STATE; 13346f34cbfSopenharmony_ci } 13446f34cbfSopenharmony_ci} 13546f34cbfSopenharmony_ci 13646f34cbfSopenharmony_ciOH_AudioCommon_Result OHAudioSessionManager::DeactivateAudioSession() 13746f34cbfSopenharmony_ci{ 13846f34cbfSopenharmony_ci CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr, 13946f34cbfSopenharmony_ci AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "failed, audioSessionManager_ is null"); 14046f34cbfSopenharmony_ci int32_t ret = audioSessionManager_->DeactivateAudioSession(); 14146f34cbfSopenharmony_ci if (ret == 0) { 14246f34cbfSopenharmony_ci return AUDIOCOMMON_RESULT_SUCCESS; 14346f34cbfSopenharmony_ci } else { 14446f34cbfSopenharmony_ci return AUDIOCOMMON_RESULT_ERROR_ILLEGAL_STATE; 14546f34cbfSopenharmony_ci } 14646f34cbfSopenharmony_ci} 14746f34cbfSopenharmony_ci 14846f34cbfSopenharmony_cibool OHAudioSessionManager::IsAudioSessionActivated() 14946f34cbfSopenharmony_ci{ 15046f34cbfSopenharmony_ci CHECK_AND_RETURN_RET_LOG(audioSessionManager_ != nullptr, false, "failed, audioSessionManager_ is null"); 15146f34cbfSopenharmony_ci return audioSessionManager_->IsAudioSessionActivated(); 15246f34cbfSopenharmony_ci} 15346f34cbfSopenharmony_ci 15446f34cbfSopenharmony_civoid OHAudioSessionCallback::OnAudioSessionDeactive(const AudioSessionDeactiveEvent &deactiveEvent) 15546f34cbfSopenharmony_ci{ 15646f34cbfSopenharmony_ci OH_AudioSession_DeactivatedEvent event; 15746f34cbfSopenharmony_ci event.reason = static_cast<OH_AudioSession_DeactivatedReason>(deactiveEvent.deactiveReason); 15846f34cbfSopenharmony_ci callback_(event); 15946f34cbfSopenharmony_ci} 16046f34cbfSopenharmony_ci 16146f34cbfSopenharmony_ci} // namespace AudioStandard 16246f34cbfSopenharmony_ci} // namespace OHOS