• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <vector>
21 #include "audio_interrupt_info.h"
22 #include "audio_session_info.h"
23 #include "audio_device_info.h"
24 #include "audio_device_descriptor.h"
25 #include "audio_policy_server_handler.h"
26 
27 namespace OHOS {
28 namespace AudioStandard {
29 enum class AudioSessionState {
30     SESSION_INVALID = -1,
31     SESSION_NEW = 0,
32     SESSION_ACTIVE = 1,
33     SESSION_DEACTIVE = 2,
34     SESSION_RELEASED = 3,
35 };
36 
37 class AudioSessionStateMonitor;
38 class AudioDeviceManager;
39 class AudioPipeManager;
40 
41 class AudioSession {
42 public:
43     AudioSession(const int32_t callerPid, const AudioSessionStrategy &strategy,
44         const std::shared_ptr<AudioSessionStateMonitor> audioSessionStateMonitor);
45     ~AudioSession();
46     bool IsSceneParameterSet();
47     int32_t SetAudioSessionScene(AudioSessionScene audioSessionScene);
48     bool IsActivated();
49     std::vector<AudioInterrupt> GetStreams();
50     AudioStreamType GetFakeStreamType();
51     void AddStreamInfo(const AudioInterrupt &incomingInterrupt);
52     void RemoveStreamInfo(uint32_t streamId);
53     void ClearStreamInfo(void);
54     uint32_t GetFakeStreamId();
55     void SaveFakeStreamId(uint32_t fakeStreamId);
56     bool ShouldExcludeStreamType(const AudioInterrupt &audioInterrupt);
57     void Dump(std::string &dumpString);
58     int32_t Activate(const AudioSessionStrategy strategy);
59     int32_t Deactivate();
60     AudioSessionStrategy GetSessionStrategy();
61     bool IsAudioSessionEmpty();
62     bool IsAudioRendererEmpty();
63     int32_t SetSessionDefaultOutputDevice(const DeviceType &deviceType);
64     void GetSessionDefaultOutputDevice(DeviceType &deviceType);
65     bool IsStreamContainedInCurrentSession(const uint32_t &streamId);
66     bool GetAndClearNeedToFetchFlag();
67     bool IsRecommendToStopAudio(const std::shared_ptr<AudioPolicyServerHandler::EventContextObj> eventContextObj);
68     bool IsSessionOutputDeviceChanged(const std::shared_ptr<AudioDeviceDescriptor> deviceDescriptor);
69     StreamUsage GetSessionStreamUsage();
70     bool IsBackGroundApp(void);
71 
72 private:
73     StreamUsage GetStreamUsageInner();
74     bool IsLegalDevice(const DeviceType deviceType);
75     bool IsCurrentDevicePrivateDevice(const std::shared_ptr<AudioDeviceDescriptor> desc);
76     bool IsDeviceContainedInVector(std::vector<std::shared_ptr<AudioDeviceDescriptor>> devices,
77         const std::shared_ptr<AudioDeviceDescriptor> desc);
78     void UpdateVoipStreamsDefaultOutputDevice();
79     bool CanCurrentStreamSetDefaultOutputDevice(const AudioInterrupt &interrupt);
80     int32_t EnableSingleVoipStreamDefaultOutputDevice(const AudioInterrupt &interrupt);
81     int32_t EnableVoipStreamsDefaultOutputDevice();
82     int32_t EnableDefaultDevice();
83     void UpdateSingleVoipStreamDefaultOutputDevice(const AudioInterrupt &interrupt);
84     bool IsSessionDefaultDeviceEnabled();
85     bool IsOutputDeviceConfigurableByStreamUsage(const StreamUsage &streamUsage);
86     std::mutex sessionMutex_;
87     int32_t callerPid_;
88     bool needToFetch_ = false;
89     AudioSessionStrategy strategy_;
90     std::weak_ptr<AudioSessionStateMonitor> audioSessionStateMonitor_;
91     AudioSessionScene audioSessionScene_ {AudioSessionScene::INVALID};
92     // These are streams included in audiosession focus.
93     std::vector<AudioInterrupt> streamsInSession_;
94     uint32_t fakeStreamId_ {0};
95     AudioSessionState state_ = AudioSessionState::SESSION_INVALID;
96     DeviceType defaultDeviceType_ = DEVICE_TYPE_INVALID;
97     AudioDeviceDescriptor deviceDescriptor_;
98     std::shared_ptr<AudioPipeManager> pipeManager_ = nullptr;
99     AudioDeviceManager &deviceManager_;
100 };
101 } // namespace AudioStandard
102 } // namespace OHOS
103 
104 #endif // ST_AUDIO_SESSION_SERVICE_H