• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 SESSION_PROCESSOR_H
17 #define SESSION_PROCESSOR_H
18 
19 #include <thread>
20 #include <mutex>
21 #include <condition_variable>
22 #include <queue>
23 #include <functional>
24 #include "audio_policy_server.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 struct SessionEvent {
29     enum class Type {
30         ADD,
31         REMOVE,
32         CLOSE_WAKEUP_SOURCE,
33     };
34 
35     SessionEvent() = delete;
36 
37     Type type;
38     uint64_t sessionID;
39     SessionInfo sessionInfo_ = {};
40 };
41 
42 class SessionProcessor {
43 public:
44     DISALLOW_COPY_AND_MOVE(SessionProcessor);
45 
SessionProcessor(std::function<void (const uint64_t,const int32_t)> processorSessionRemoved,std::function<void (SessionEvent)> processorSessionAdded,std::function<void (const uint64_t)> processorCloseWakeupSource)46     SessionProcessor(std::function<void(const uint64_t, const int32_t)> processorSessionRemoved,
47         std::function<void(SessionEvent)> processorSessionAdded,
48         std::function<void(const uint64_t)> processorCloseWakeupSource)
49         : processorSessionRemoved_(processorSessionRemoved), processorSessionAdded_(processorSessionAdded),
50         processorCloseWakeupSource_(processorCloseWakeupSource)
51     {
52     }
53 
~SessionProcessor()54     ~SessionProcessor()
55     {
56         Stop();
57     }
58 
Post(const SessionEvent event)59     void Post(const SessionEvent event)
60     {
61         std::unique_lock<std::mutex> lock(mutex_);
62         sessionEvents_.emplace(event);
63         cv_.notify_one();
64     }
65 
Start()66     void Start()
67     {
68         std::unique_lock<std::mutex> lock(mutex_);
69         if (exitLoop_ == false) {
70             AUDIO_INFO_LOG("exitLoop_ == false return");
71             return;
72         } else if (started_ == true) {
73             AUDIO_INFO_LOG("started_ == false return");
74             return;
75         }
76         exitLoop_ = false;
77         started_ = true;
78         loopThread_ = std::thread{[this] { this->Loop(); }};
79         AUDIO_INFO_LOG("Start end");
80     }
81 
Stop()82     void Stop()
83     {
84         {
85             std::unique_lock<std::mutex> lock(mutex_);
86             exitLoop_ = true;
87         }
88         cv_.notify_one();
89         if (loopThread_.joinable()) {
90             loopThread_.join();
91         }
92         AUDIO_INFO_LOG("Stop end");
93     }
94 
95 private:
ProcessSessionEvent(SessionEvent event,const int32_t zoneID)96     void ProcessSessionEvent(SessionEvent event, const int32_t zoneID)
97     {
98         switch (event.type) {
99             case SessionEvent::Type::REMOVE :
100                 processorSessionRemoved_(event.sessionID, zoneID);
101                 break;
102             case SessionEvent::Type::ADD :
103                 processorSessionAdded_(event);
104                 break;
105             case SessionEvent::Type::CLOSE_WAKEUP_SOURCE :
106                 processorCloseWakeupSource_(event.sessionID);
107                 break;
108             default:
109                 break;
110         }
111     }
112 
Loop()113     void Loop()
114     {
115         std::unique_lock<std::mutex> lock(mutex_);
116         while (!exitLoop_) {
117             while (sessionEvents_.size() > 0) {
118                 auto frontEvent = sessionEvents_.front();
119                 sessionEvents_.pop();
120                 lock.unlock();
121                 ProcessSessionEvent(frontEvent, 0);
122                 lock.lock();
123             }
124             cv_.wait(lock, [this] {
125                 bool res = (sessionEvents_.size() > 0 || exitLoop_);
126                 return res;
127             });
128         }
129         started_ = false;
130         AUDIO_INFO_LOG("Loop exit");
131         return;
132     }
133 
134     bool exitLoop_ = true;
135     bool started_ = false;
136     std::thread loopThread_;
137     std::mutex mutex_;
138     std::condition_variable cv_;
139     std::queue<SessionEvent> sessionEvents_;
140     std::function<void(const uint64_t, const int32_t)> processorSessionRemoved_;
141     std::function<void(SessionEvent)> processorSessionAdded_;
142     std::function<void(const uint64_t)> processorCloseWakeupSource_;
143 };
144 } // namespace AudioStandard
145 } // namespace OHOS
146 #endif // SESSION_PROCESSOR_H