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 23 #include "audio_service_adapter.h" 24 25 namespace OHOS { 26 namespace AudioStandard { 27 class PulseAudioServiceAdapterImpl : public AudioServiceAdapter { 28 public: 29 explicit PulseAudioServiceAdapterImpl(std::unique_ptr<AudioServiceAdapterCallback> &cb); 30 ~PulseAudioServiceAdapterImpl(); 31 32 bool Connect() override; 33 uint32_t OpenAudioPort(std::string audioPortName, std::string moduleArgs) override; 34 int32_t CloseAudioPort(int32_t audioHandleIndex) override; 35 int32_t SetDefaultSink(std::string name) override; 36 int32_t SetDefaultSource(std::string name) override; 37 int32_t SetVolume(AudioStreamType streamType, float volume) override; 38 int32_t SetMute(AudioStreamType streamType, bool mute) override; 39 int32_t SuspendAudioDevice(std::string &audioPortName, bool isSuspend) override; 40 bool IsMute(AudioStreamType streamType) override; 41 bool IsStreamActive(AudioStreamType streamType) override; 42 void Disconnect() override; 43 44 // Static Member functions 45 static void PaContextStateCb(pa_context *c, void *userdata); 46 static void PaModuleLoadCb(pa_context *c, uint32_t idx, void *userdata); 47 static void PaGetSinkInputInfoVolumeCb(pa_context *c, const pa_sink_input_info *i, int eol, void *userdata); 48 static void PaSubscribeCb(pa_context *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata); 49 static void PaGetSinkInputInfoMuteCb(pa_context *c, const pa_sink_input_info *i, int eol, void *userdata); 50 static void PaGetSinkInputInfoMuteStatusCb(pa_context *c, const pa_sink_input_info *i, int eol, void *userdata); 51 static void PaGetSinkInputInfoCorkStatusCb(pa_context *c, const pa_sink_input_info *i, int eol, void *userdata); 52 private: 53 struct UserData { 54 PulseAudioServiceAdapterImpl *thiz; 55 AudioStreamType streamType; 56 float volume; 57 bool mute; 58 bool isCorked; 59 uint32_t idx; 60 }; 61 62 bool ConnectToPulseAudio(); 63 std::string GetNameByStreamType(AudioStreamType streamType); 64 AudioStreamType GetIdByStreamType(std::string streamType); 65 66 static constexpr uint32_t PA_CONNECT_RETRY_SLEEP_IN_MICRO_SECONDS = 500000; 67 pa_context *mContext = NULL; 68 pa_threaded_mainloop *mMainLoop = NULL; 69 static std::unordered_map<uint32_t, uint32_t> sinkIndexSessionIDMap; 70 std::mutex mMutex; 71 }; 72 } // namespace AudioStandard 73 } // namespace OHOS 74 #endif // ST_PULSEAUDIO_AUDIO_SERVICE_ADAPTER_H 75