1 /* 2 * Copyright (c) 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 AUDIO_STREAM_EVENT_DISPATCHER_INFO_H 17 #define AUDIO_STREAM_EVENT_DISPATCHER_INFO_H 18 19 #include <cstdlib> 20 #include <map> 21 #include <queue> 22 #include <stdio.h> 23 #include <thread> 24 #include <unordered_map> 25 #include <vector> 26 #include "audio_log.h" 27 #include "audio_stream_manager.h" 28 #include "iremote_proxy.h" 29 30 namespace OHOS { 31 namespace AudioStandard { 32 using namespace std; 33 struct StreamStateChangeRequest { 34 AudioMode mode; 35 std::vector<std::unique_ptr<AudioRendererChangeInfo>> audioRendererChangeInfos; 36 std::vector<std::unique_ptr<AudioCapturerChangeInfo>> audioCapturerChangeInfos; 37 }; 38 39 class AudioStreamEventDispatcher { 40 public: GetAudioStreamEventDispatcher()41 static AudioStreamEventDispatcher& GetAudioStreamEventDispatcher() 42 { 43 static AudioStreamEventDispatcher audioStreamEventDispatcher; 44 return audioStreamEventDispatcher; 45 } 46 47 AudioStreamEventDispatcher(); 48 ~AudioStreamEventDispatcher(); 49 50 void addRendererListener(int32_t clientUID, 51 const std::shared_ptr<AudioRendererStateChangeCallback> &callback); 52 void removeRendererListener(int32_t clientUID); 53 void addCapturerListener(int32_t clientUID, 54 const std::shared_ptr<AudioCapturerStateChangeCallback> &callback); 55 void removeCapturerListener(int32_t clientUID); 56 void SendRendererInfoEventToDispatcher(AudioMode mode, 57 std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos); 58 void SendCapturerInfoEventToDispatcher(AudioMode mode, 59 std::vector<std::unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos); 60 void HandleCapturerStreamStateChange(const unique_ptr<StreamStateChangeRequest> &streamStateChangeRequest); 61 void HandleRendererStreamStateChange(const unique_ptr<StreamStateChangeRequest> &streamStateChangeRequest); 62 void HandleStreamStateChange(); 63 void DispatcherEvent(); 64 65 private: 66 std::mutex rendererStateChangeListnerMutex_; 67 std::mutex capturerStateChangeListnerMutex_; 68 std::unordered_map<int32_t, std::shared_ptr<AudioRendererStateChangeCallback>> rendererCBMap_; 69 std::unordered_map<int32_t, std::shared_ptr<AudioCapturerStateChangeCallback>> capturerCBMap_; 70 std::queue<unique_ptr<StreamStateChangeRequest>> streamStateChangeQueue_; 71 std::unique_ptr<StreamStateChangeRequest> request; 72 73 uint32_t activeThread_; 74 static constexpr uint32_t MAX_THREAD = 1; 75 }; 76 } // namespace AudioStandard 77 } // namespace OHOS 78 #endif // AUDIO_STREAM_EVENT_DISPATCHER_INFO_H