• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_PULSEAUDIO_AUDIO_SERVICE_ADAPTER_H
17 #define ST_PULSEAUDIO_AUDIO_SERVICE_ADAPTER_H
18 #include <mutex>
19 #include "safe_map.h"
20 
21 #include <pulse/pulseaudio.h>
22 #include <vector>
23 
24 #include "audio_service_adapter.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 class PulseAudioServiceAdapterImpl : public AudioServiceAdapter {
29 public:
30     explicit PulseAudioServiceAdapterImpl(std::unique_ptr<AudioServiceAdapterCallback> &cb);
31     ~PulseAudioServiceAdapterImpl();
32 
33     bool Connect() override;
34     uint32_t OpenAudioPort(std::string audioPortName, std::string moduleArgs) override;
35     int32_t OpenAudioPort(std::string audioPortName, const AudioModuleInfo& audioModuleInfo) override;
36     int32_t ReloadAudioPort(const std::string &audioPortName, const AudioModuleInfo& audioModuleInfo) override;
37     int32_t CloseAudioPort(int32_t audioHandleIndex) override;
38     int32_t SetDefaultSink(std::string name) override;
39     int32_t SetDefaultSource(std::string name) override;
40     int32_t SetSourceOutputMute(int32_t uid, bool setMute) override;
41     int32_t SuspendAudioDevice(std::string &audioPortName, bool isSuspend) override;
42     bool SetSinkMute(const std::string &sinkName, bool isMute, bool isSync = false) override;
43     std::vector<SinkInput> GetAllSinkInputs() override;
44     std::vector<SourceOutput> GetAllSourceOutputs() override;
45     void Disconnect() override;
46 
47     std::vector<uint32_t> GetTargetSinks(std::string adapterName) override;
48     std::vector<SinkInfo> GetAllSinks() override;
49     int32_t SetLocalDefaultSink(std::string name) override;
50     int32_t MoveSinkInputByIndexOrName(uint32_t sinkInputId, uint32_t sinkIndex, std::string sinkName) override;
51     int32_t MoveSourceOutputByIndexOrName(uint32_t sourceOutputId,
52         uint32_t sourceIndex, std::string sourceName) override;
GetAudioEffectProperty(AudioEffectPropertyArrayV3 & propertyArray)53     int32_t GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray) override { return 0; }
GetAudioEffectProperty(AudioEffectPropertyArray & propertyArray)54     int32_t GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray) override { return 0; }
55     int32_t GetAudioEnhanceProperty(AudioEffectPropertyArrayV3 &propertyArray,
56         DeviceType deviceType = DEVICE_TYPE_NONE) override { return 0; }
57     int32_t GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray,
58         DeviceType deviceType = DEVICE_TYPE_NONE) override { return 0; }
59 
60     // Static Member functions
61     static void PaGetSinksCb(pa_context *c, const pa_sink_info *i, int eol, void *userdata);
62     static void PaMoveSinkInputCb(pa_context *c, int success, void *userdata);
63     static void PaMoveSourceOutputCb(pa_context *c, int success, void *userdata);
64     static void PaContextStateCb(pa_context *c, void *userdata);
65     static void PaModuleLoadCb(pa_context *c, uint32_t idx, void *userdata);
66     static void PaUnloadModuleCb(pa_context *c, int success, void *userdata);
67     static void PaSubscribeCb(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata);
68     static void PaGetAllSinkInputsCb(pa_context *c, const pa_sink_input_info *i, int eol, void *userdata);
69     static void PaGetAllSourceOutputsCb(pa_context *c, const pa_source_output_info *i, int eol, void *userdata);
70     static void PaGetSourceOutputNoSignalCb(pa_context *c, const pa_source_output_info *i, int eol, void *userdata);
71     static void ProcessSourceOutputEvent(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata);
72     static void PaSinkMuteCb(pa_context *c, int success, void *userdata);
73 private:
74     struct UserData {
75         PulseAudioServiceAdapterImpl *thiz;
76         AudioStreamType streamType;
77         float volume;
78         bool mute;
79         bool isCorked;
80         uint32_t idx;
81         std::vector<SinkInput> sinkInputList;
82         std::vector<SourceOutput> sourceOutputList;
83         std::vector<SinkInfo> sinkInfos;
84         int32_t moveResult;
85         bool isSubscribingCb = false;
86     };
87 
88     bool ConnectToPulseAudio();
89     AudioStreamType GetIdByStreamType(std::string streamType);
90     bool SetThreadPriority();
91 
92     static constexpr uint32_t PA_CONNECT_RETRY_SLEEP_IN_MICRO_SECONDS = 500000;
93     pa_context *mContext = NULL;
94     pa_threaded_mainloop *mMainLoop = NULL;
95     static SafeMap<uint32_t, uint32_t> sinkIndexSessionIDMap;
96     static SafeMap<uint32_t, uint32_t> sourceIndexSessionIDMap;
97     std::mutex lock_;
98     bool isSetDefaultSink_ = false;
99     bool isSetDefaultSource_ = false;
100 };
101 }  // namespace AudioStandard
102 }  // namespace OHOS
103 #endif  // ST_PULSEAUDIO_AUDIO_SERVICE_ADAPTER_H
104