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 AUDIO_RESOURCE_SERVICE_H 17 #define AUDIO_RESOURCE_SERVICE_H 18 19 #include <cstdint> 20 #include <unordered_map> 21 #include <memory> 22 #include <cstdio> 23 #include <thread> 24 25 #include "audio_workgroup.h" 26 #include "audio_common_log.h" 27 #include "audio_stream_info.h" 28 29 namespace OHOS { 30 namespace AudioStandard { 31 32 struct AudioWorkgroupPerProcess { 33 std::unordered_map<int32_t, std::shared_ptr<AudioWorkgroup>> groups; 34 bool permission; 35 bool hasSystemPermission; 36 AudioWorkgroupPerProcessAudioWorkgroupPerProcess37 AudioWorkgroupPerProcess() 38 { 39 groups.clear(); 40 permission = false; 41 hasSystemPermission = false; 42 } ~AudioWorkgroupPerProcessAudioWorkgroupPerProcess43 ~AudioWorkgroupPerProcess() { 44 } 45 }; 46 47 class AudioResourceService { 48 public: 49 static AudioResourceService *GetInstance(); 50 explicit AudioResourceService(); 51 ~AudioResourceService(); 52 53 int32_t CreateAudioWorkgroup(int32_t pid, const sptr<IRemoteObject> &object); 54 int32_t ReleaseAudioWorkgroup(int32_t pid, int32_t workgroupId); 55 int32_t AddThreadToGroup(int32_t pid, int32_t workgroupId, int32_t tokenId); 56 int32_t RemoveThreadFromGroup(int32_t pid, int32_t workgroupId, int32_t tokenId); 57 int32_t StartGroup(int32_t pid, int32_t workgroupId, uint64_t startTime, uint64_t deadlineTime); 58 int32_t StopGroup(int32_t pid, int32_t workgroupId); 59 void OnWorkgroupRemoteDied(const std::shared_ptr<AudioWorkgroup> &workgroup, 60 const sptr<IRemoteObject> &remoteObj); 61 void ReleaseWorkgroupDeathRecipient(const std::shared_ptr<AudioWorkgroup> &workgroup, 62 const sptr<IRemoteObject> &remoteObj); 63 void WorkgroupRendererMonitor(int32_t pid, const bool isAllowed); 64 bool IsProcessInWorkgroup(int32_t pid); 65 bool IsProcessHasSystemPermission(int32_t pid); 66 void RegisterAudioWorkgroupDeathRecipient(pid_t pid); 67 std::vector<int32_t> GetProcessesOfAudioWorkgroup(); 68 int32_t ImproveAudioWorkgroupPrio(int32_t pid, const std::unordered_map<int32_t, bool> &threads); 69 int32_t RestoreAudioWorkgroupPrio(int32_t pid, const std::unordered_map<int32_t, int32_t> &threads); 70 // Inner class for death handler 71 class AudioWorkgroupDeathRecipient : public IRemoteObject::DeathRecipient { 72 public: 73 explicit AudioWorkgroupDeathRecipient(); 74 virtual ~AudioWorkgroupDeathRecipient() = default; 75 DISALLOW_COPY_AND_MOVE(AudioWorkgroupDeathRecipient); 76 void OnRemoteDied(const wptr<IRemoteObject> &remote); 77 78 using NotifyCbFunc = std::function<void()>; 79 void SetNotifyCb(NotifyCbFunc func); 80 private: 81 NotifyCbFunc diedCb_ = nullptr; 82 }; 83 private: 84 int32_t AudioWorkgroupCheck(int32_t pid); 85 AudioWorkgroup *GetAudioWorkgroupPtr(int32_t pid, int32_t workgroupId); 86 int32_t RegisterAudioWorkgroupMonitor(int32_t pid, int32_t groupId, const sptr<IRemoteObject> &object); 87 int32_t GetThreadsNumPerProcess(int32_t pid); 88 void DumpAudioWorkgroupMap(); 89 90 std::mutex workgroupLock_; 91 std::unordered_map<int32_t, struct AudioWorkgroupPerProcess> audioWorkgroupMap_; 92 std::unordered_map<std::shared_ptr<AudioWorkgroup>, 93 std::pair<sptr<IRemoteObject>, sptr<AudioWorkgroupDeathRecipient>>> deathRecipientMap_; 94 }; 95 96 } // namespace AudioStandard 97 } // namespace OHOS 98 #endif 99