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