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 ST_AUDIO_SESSION_H
17 #define ST_AUDIO_SESSION_H
18 
19 #include <mutex>
20 
21 #include "audio_interrupt_info.h"
22 #include "audio_session_info.h"
23 #include "audio_session_timer.h"
24 
25 namespace OHOS {
26 namespace AudioStandard {
27 enum class AudioSessionState {
28     SESSION_INVALID = -1,
29     SESSION_NEW = 0,
30     SESSION_ACTIVE = 1,
31     SESSION_DEACTIVE = 2,
32     SESSION_RELEASED = 3,
33 };
34 
35 class AudioSession {
36 public:
37     AudioSession(const int32_t callerPid, const AudioSessionStrategy &strategy,
38         const std::shared_ptr<AudioSessionTimer> sessionTimer);
39     ~AudioSession();
40 
41     int32_t Activate();
42     int32_t Deactivate();
43     AudioSessionState GetSessionState();
44     void SetSessionStrategy(const AudioSessionStrategy strategy);
45     AudioSessionStrategy GetSessionStrategy();
46     int32_t AddAudioInterrpt(const std::pair<AudioInterrupt, AudioFocuState> interruptPair);
47     int32_t RemoveAudioInterrpt(const std::pair<AudioInterrupt, AudioFocuState> interruptPair);
48     int32_t RemoveAudioInterrptByStreamId(const uint32_t &streamId);
49     bool IsAudioSessionEmpty();
50     bool IsAudioRendererEmpty();
51 
52 private:
53     std::mutex sessionMutex_;
54 
55     int32_t callerPid_;
56     AudioSessionStrategy strategy_;
57     std::shared_ptr<AudioSessionTimer> sessionTimer_;
58 
59     AudioSessionState state_ = AudioSessionState::SESSION_INVALID;
60     std::unordered_map<uint32_t, std::pair<AudioInterrupt, AudioFocuState>> interruptMap_;
61 };
62 } // namespace AudioStandard
63 } // namespace OHOS
64 
65 #endif // ST_AUDIO_SESSION_SERVICE_H