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 <unordered_map> 19 #include <mutex> 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 CloseAudioPort(int32_t audioHandleIndex) override; 36 int32_t SetDefaultSink(std::string name) override; 37 int32_t SetDefaultSource(std::string name) override; 38 int32_t SetVolumeDb(AudioStreamType streamType, float volumeDb) override; 39 int32_t SetSourceOutputMute(int32_t uid, bool setMute) override; 40 int32_t SuspendAudioDevice(std::string &audioPortName, bool isSuspend) override; 41 bool SetSinkMute(const std::string &sinkName, bool isMute) override; 42 bool IsStreamActive(AudioStreamType streamType) 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; 53 int32_t UpdateSwapDeviceStatus() override; 54 55 // Static Member functions 56 static void PaGetSinksCb(pa_context *c, const pa_sink_info *i, int eol, void *userdata); 57 static void PaMoveSinkInputCb(pa_context *c, int success, void *userdata); 58 static void PaMoveSourceOutputCb(pa_context *c, int success, void *userdata); 59 static void PaContextStateCb(pa_context *c, void *userdata); 60 static void PaModuleLoadCb(pa_context *c, uint32_t idx, void *userdata); 61 static void PaGetSinkInputInfoVolumeCb(pa_context *c, const pa_sink_input_info *i, int eol, void *userdata); 62 static void PaSubscribeCb(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata); 63 static void PaGetSinkInputInfoCorkStatusCb(pa_context *c, const pa_sink_input_info *i, int eol, void *userdata); 64 static void PaGetAllSinkInputsCb(pa_context *c, const pa_sink_input_info *i, int eol, void *userdata); 65 static void PaGetAllSourceOutputsCb(pa_context *c, const pa_source_output_info *i, int eol, void *userdata); 66 static void PaGetSourceOutputCb(pa_context *c, const pa_source_output_info *i, int eol, void *userdata); 67 static void ProcessSourceOutputEvent(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata); 68 private: 69 struct UserData { 70 PulseAudioServiceAdapterImpl *thiz; 71 AudioStreamType streamType; 72 float volume; 73 bool mute; 74 bool isCorked; 75 uint32_t idx; 76 std::vector<SinkInput> sinkInputList; 77 std::vector<SourceOutput> sourceOutputList; 78 std::vector<SinkInfo> sinkInfos; 79 int32_t moveResult; 80 bool isSubscribingCb = false; 81 }; 82 83 bool ConnectToPulseAudio(); 84 AudioStreamType GetIdByStreamType(std::string streamType); 85 86 static constexpr uint32_t PA_CONNECT_RETRY_SLEEP_IN_MICRO_SECONDS = 500000; 87 pa_context *mContext = NULL; 88 pa_threaded_mainloop *mMainLoop = NULL; 89 static std::unordered_map<uint32_t, uint32_t> sinkIndexSessionIDMap; 90 static std::unordered_map<uint32_t, uint32_t> sourceIndexSessionIDMap; 91 std::mutex lock_; 92 bool isSetDefaultSink_ = false; 93 bool isSetDefaultSource_ = false; 94 int32_t swapStatus = 0; 95 }; 96 } // namespace AudioStandard 97 } // namespace OHOS 98 #endif // ST_PULSEAUDIO_AUDIO_SERVICE_ADAPTER_H 99