1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
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 MONITOR_CLIENT_H
17 #define MONITOR_CLIENT_H
18 
19 #include <thread>
20 #include <mutex>
21 #include <set>
22 #include "monitor_client_object.h"
23 #include "i_standard_monitor_service.h"
24 namespace OHOS {
25 namespace Media {
26 class MonitorClient {
27 public:
28     MonitorClient();
29     ~MonitorClient();
30 
31     static std::shared_ptr<MonitorClient> GetInstance();
32     int32_t StartClick(MonitorClientObject *obj);
33     int32_t StopClick(MonitorClientObject *obj);
34     void MediaServerDied();
35 
36 private:
37     bool IsVaildProxy();
38     void ClickThread();
39     void ClickThreadCtrl();
40 
41     sptr<IStandardMonitorService> monitorProxy_ = nullptr;
42     bool isVaildProxy_ = false;
43     std::mutex mutex_;
44     std::mutex thredMutex_;
45     std::condition_variable clickCond_;
46     std::unique_ptr<std::thread> clickThread_ = nullptr;
47     bool threadRunning_ = false;
48     std::set<MonitorClientObject *> objSet_;
49     std::atomic<bool> clientDestroy_ = false;
50 
51     class Destroy {
52     public:
53         Destroy() = default;
54         ~Destroy();
55     };
56     static std::mutex instanceMutex_;
57     static std::shared_ptr<MonitorClient> monitorClient_;
58     static Destroy destroy_;
59 };
60 } // namespace Media
61 } // namespace OHOS
62 #endif // MONITOR_CLIENT_H
63