1 /* 2 * Copyright (c) 2025 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 CALLBACK_WRAPPER_H 17 #define CALLBACK_WRAPPER_H 18 19 #include <iostream> 20 #include <vector> 21 #include <mutex> 22 #include "sink/i_audio_render_sink.h" 23 #include "source/i_audio_capture_source.h" 24 25 namespace OHOS { 26 namespace AudioStandard { 27 class SinkCallbackWrapper : public IAudioSinkCallback { 28 public: 29 SinkCallbackWrapper() = default; 30 ~SinkCallbackWrapper() = default; 31 32 void RegistCallback(uint32_t type, std::shared_ptr<IAudioSinkCallback> cb); 33 void RegistCallback(uint32_t type, IAudioSinkCallback *cb); 34 void RegistCallbackGenerator(uint32_t type, 35 std::function<std::shared_ptr<IAudioSinkCallback>(uint32_t)> cbGenerator); 36 std::shared_ptr<IAudioSinkCallback> GetCallback(uint32_t type, uint32_t renderId); 37 IAudioSinkCallback *GetRawCallback(uint32_t type); 38 39 void OnRenderSinkParamChange(const std::string &networkId, const AudioParamKey key, 40 const std::string &condition, const std::string &value) override; 41 void OnRenderSinkStateChange(uint32_t uniqueId, bool started) override; 42 43 private: 44 std::unordered_map<uint32_t, std::shared_ptr<IAudioSinkCallback> > cbs_; 45 std::unordered_map<uint32_t, IAudioSinkCallback *> rawCbs_; 46 std::unordered_map<uint32_t, std::function<std::shared_ptr<IAudioSinkCallback>(uint32_t)> > cbGenerators_; 47 std::mutex cbMtx_; 48 std::mutex rawCbMtx_; 49 std::mutex cbGeneratorMtx_; 50 }; 51 52 class SourceCallbackWrapper : public IAudioSourceCallback { 53 public: 54 SourceCallbackWrapper() = default; 55 ~SourceCallbackWrapper() = default; 56 57 void RegistCallback(uint32_t type, std::shared_ptr<IAudioSourceCallback> cb); 58 void RegistCallback(uint32_t type, IAudioSourceCallback *cb); 59 void RegistCallbackGenerator(uint32_t type, 60 std::function<std::shared_ptr<IAudioSourceCallback>(uint32_t)> cbGenerator); 61 std::shared_ptr<IAudioSourceCallback> GetCallback(uint32_t type, uint32_t captureId); 62 IAudioSourceCallback *GetRawCallback(uint32_t type); 63 64 void OnCaptureSourceParamChange(const std::string &networkId, const AudioParamKey key, 65 const std::string &condition, const std::string &value) override; 66 void OnCaptureState(bool isActive) override; 67 void OnWakeupClose(void) override; 68 69 private: 70 std::unordered_map<uint32_t, std::shared_ptr<IAudioSourceCallback> > cbs_; 71 std::unordered_map<uint32_t, IAudioSourceCallback *> rawCbs_; 72 std::unordered_map<uint32_t, std::function<std::shared_ptr<IAudioSourceCallback>(uint32_t)> > cbGenerators_; 73 std::mutex cbMtx_; 74 std::mutex rawCbMtx_; 75 std::mutex cbGeneratorMtx_; 76 }; 77 78 } // namespace AudioStandard 79 } // namespace OHOS 80 81 #endif // CALLBACK_WRAPPER_H 82