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 CloseAudioPort(int32_t audioHandleIndex) override; 36 int32_t SetDefaultSink(std::string name) override; 37 int32_t SetDefaultSource(std::string name) override; 38 int32_t SetSourceOutputMute(int32_t uid, bool setMute) override; 39 int32_t SuspendAudioDevice(std::string &audioPortName, bool isSuspend) override; 40 bool SetSinkMute(const std::string &sinkName, bool isMute, bool isSync = false) override; 41 std::vector<SinkInput> GetAllSinkInputs() override; 42 std::vector<SourceOutput> GetAllSourceOutputs() override; 43 void Disconnect() override; 44 45 std::vector<uint32_t> GetTargetSinks(std::string adapterName) override; 46 std::vector<SinkInfo> GetAllSinks() override; 47 int32_t SetLocalDefaultSink(std::string name) override; 48 int32_t MoveSinkInputByIndexOrName(uint32_t sinkInputId, uint32_t sinkIndex, std::string sinkName) override; 49 int32_t MoveSourceOutputByIndexOrName(uint32_t sourceOutputId, 50 uint32_t sourceIndex, std::string sourceName) override; 51 52 // Static Member functions 53 static void PaGetSinksCb(pa_context *c, const pa_sink_info *i, int eol, void *userdata); 54 static void PaMoveSinkInputCb(pa_context *c, int success, void *userdata); 55 static void PaMoveSourceOutputCb(pa_context *c, int success, void *userdata); 56 static void PaContextStateCb(pa_context *c, void *userdata); 57 static void PaModuleLoadCb(pa_context *c, uint32_t idx, void *userdata); 58 static void PaSubscribeCb(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata); 59 static void PaGetAllSinkInputsCb(pa_context *c, const pa_sink_input_info *i, int eol, void *userdata); 60 static void PaGetAllSourceOutputsCb(pa_context *c, const pa_source_output_info *i, int eol, void *userdata); 61 static void PaGetSourceOutputNoSignalCb(pa_context *c, const pa_source_output_info *i, int eol, void *userdata); 62 static void ProcessSourceOutputEvent(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata); 63 static void PaSinkMuteCb(pa_context *c, int success, void *userdata); 64 private: 65 struct UserData { 66 PulseAudioServiceAdapterImpl *thiz; 67 AudioStreamType streamType; 68 float volume; 69 bool mute; 70 bool isCorked; 71 uint32_t idx; 72 std::vector<SinkInput> sinkInputList; 73 std::vector<SourceOutput> sourceOutputList; 74 std::vector<SinkInfo> sinkInfos; 75 int32_t moveResult; 76 bool isSubscribingCb = false; 77 }; 78 79 bool ConnectToPulseAudio(); 80 AudioStreamType GetIdByStreamType(std::string streamType); 81 82 static constexpr uint32_t PA_CONNECT_RETRY_SLEEP_IN_MICRO_SECONDS = 500000; 83 pa_context *mContext = NULL; 84 pa_threaded_mainloop *mMainLoop = NULL; 85 static SafeMap<uint32_t, uint32_t> sinkIndexSessionIDMap; 86 static SafeMap<uint32_t, uint32_t> sourceIndexSessionIDMap; 87 std::mutex lock_; 88 bool isSetDefaultSink_ = false; 89 bool isSetDefaultSource_ = false; 90 }; 91 } // namespace AudioStandard 92 } // namespace OHOS 93 #endif // ST_PULSEAUDIO_AUDIO_SERVICE_ADAPTER_H 94