• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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_SERVER_H
17 #define ST_AUDIO_SERVER_H
18 
19 #include <mutex>
20 #include <pthread.h>
21 #include <unordered_map>
22 
23 #include "accesstoken_kit.h"
24 #include "ipc_skeleton.h"
25 #include "iremote_stub.h"
26 #include "system_ability.h"
27 
28 #include "audio_manager_base.h"
29 #include "audio_server_death_recipient.h"
30 #include "audio_system_manager.h"
31 #include "i_audio_renderer_sink.h"
32 #include "i_audio_capturer_source.h"
33 #include "audio_effect_server.h"
34 
35 namespace OHOS {
36 namespace AudioStandard {
37 class AudioServer : public SystemAbility, public AudioManagerStub, public IAudioSinkCallback, IAudioSourceCallback {
38     DECLARE_SYSTEM_ABILITY(AudioServer);
39 public:
40     DISALLOW_COPY_AND_MOVE(AudioServer);
41     explicit AudioServer(int32_t systemAbilityId, bool runOnCreate = true);
42     virtual ~AudioServer() = default;
43     void OnDump() override;
44     void OnStart() override;
45     void OnStop() override;
46     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
47 
48     bool LoadAudioEffectLibraries(std::vector<Library> libraries, std::vector<Effect> effects,
49         std::vector<Effect>& successEffectList) override;
50     bool CreatePlaybackCapturerManager() override;
51     bool CreateEffectChainManager(std::vector<EffectChain> &effectChains,
52         std::unordered_map<std::string, std::string> &map) override;
53     bool SetOutputDeviceSink(int32_t deviceType, std::string &sinkName) override;
54     int32_t SetMicrophoneMute(bool isMute) override;
55     bool IsMicrophoneMute() override;
56     int32_t SetVoiceVolume(float volume) override;
57     int32_t SetAudioScene(AudioScene audioScene, DeviceType activeDevice) override;
58     std::vector<sptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag) override;
59     static void *paDaemonThread(void *arg);
60     void SetAudioParameter(const std::string& key, const std::string& value) override;
61     void SetAudioParameter(const std::string& networkId, const AudioParamKey key, const std::string& condition,
62         const std::string& value) override;
63     const std::string GetAudioParameter(const std::string &key) override;
64     const std::string GetAudioParameter(const std::string& networkId, const AudioParamKey key,
65         const std::string& condition) override;
66     const char *RetrieveCookie(int32_t &size) override;
67     uint64_t GetTransactionId(DeviceType deviceType, DeviceRole deviceRole) override;
68     int32_t UpdateActiveDeviceRoute(DeviceType type, DeviceFlag flag) override;
69     void SetAudioMonoState(bool audioMono) override;
70     void SetAudioBalanceValue(float audioBalance) override;
71 
72     void NotifyDeviceInfo(std::string networkId, bool connected) override;
73 
74     int32_t CheckRemoteDeviceState(std::string networkId, DeviceRole deviceRole, bool isStartDevice) override;
75 
76     sptr<IRemoteObject> CreateAudioProcess(const AudioProcessConfig &config) override;
77 
78     // ISinkParameterCallback
79     void OnAudioParameterChange(std::string netWorkId, const AudioParamKey key,
80         const std::string& condition, const std::string& value) override;
81     // IAudioSourceCallback
82     void OnWakeupClose() override;
83 
84     void OnCapturerState(bool isActive) override;
85 
86     int32_t SetParameterCallback(const sptr<IRemoteObject>& object) override;
87 
88     int32_t RegiestPolicyProvider(const sptr<IRemoteObject> &object) override;
89 
90     int32_t SetWakeupSourceCallback(const sptr<IRemoteObject>& object) override;
91 
92     void RequestThreadPriority(uint32_t tid, std::string bundleName) override;
93 
94     int32_t SetSupportStreamUsage(std::vector<int32_t> usage) override;
95 
96     int32_t SetCaptureSilentState(bool state) override;
97 
98 protected:
99     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
100 
101 private:
102     bool VerifyClientPermission(const std::string &permissionName,
103         Security::AccessToken::AccessTokenID tokenId = Security::AccessToken::INVALID_TOKENID);
104     void AudioServerDied(pid_t pid);
105     void RegisterPolicyServerDeathRecipient();
106     void RegisterAudioCapturerSourceCallback();
107 
108 private:
109     static constexpr int32_t MEDIA_SERVICE_UID = 1013;
110     static constexpr int32_t MAX_VOLUME = 15;
111     static constexpr int32_t MIN_VOLUME = 0;
112     static std::unordered_map<int, float> AudioStreamVolumeMap;
113     static std::map<std::string, std::string> audioParameters;
114 
115     int32_t audioUid_ = 1041;
116     pthread_t m_paDaemonThread;
117     AudioScene audioScene_ = AUDIO_SCENE_DEFAULT;
118     std::shared_ptr<AudioParameterCallback> audioParameterCallback_;
119     std::shared_ptr<WakeUpSourceCallback> wakeupCallback_;
120     std::mutex setParameterCallbackMutex_;
121     std::mutex setWakeupCloseCallbackMutex_;
122     std::mutex audioParameterMutex_;
123     std::mutex wakeupCloseMutex_;
124     bool isGetProcessEnabled_ = false;
125     std::unique_ptr<AudioEffectServer> audioEffectServer_;
126 };
127 } // namespace AudioStandard
128 } // namespace OHOS
129 #endif // ST_AUDIO_SERVER_H
130