• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 AVCODEC_XCOLLIE_H
17 #define AVCODEC_XCOLLIE_H
18 
19 #include <string>
20 #include <map>
21 #include <shared_mutex>
22 #include <functional>
23 #include <ctime>
24 
25 namespace OHOS {
26 namespace MediaAVCodec {
27 class AVCodecXCollie {
28 public:
29     static AVCodecXCollie &GetInstance();
30     int32_t SetTimer(const std::string &name, bool recovery, uint32_t timeout, std::function<void(void *)> callback);
31     int32_t SetInterfaceTimer(const std::string &name, bool isService, bool recovery, uint32_t timeout);
32     void CancelTimer(int32_t timerId);
33     int32_t Dump(int32_t fd);
34     constexpr static uint32_t timerTimeout = 10;
35 private:
36     struct TimerInfo {
37         std::string name;
38         std::time_t startTime;
39         uint32_t timeout;
40     };
41 
42     AVCodecXCollie() = default;
43     ~AVCodecXCollie() = default;
44 
45     std::shared_mutex mutex_;
46     std::map<int32_t, TimerInfo> dfxDumper_;
47 
48 // For interfacec timer
49 private:
50     static void ServiceInterfaceTimerCallback(void *data);
51     static void ClientInterfaceTimerCallback(void *data);
52 };
53 
54 class AVCodecXcollieTimer {
55 public:
AVCodecXcollieTimer(const std::string & name,bool recovery,uint32_t timeout,std::function<void (void *)> callback)56     AVCodecXcollieTimer(const std::string &name, bool recovery, uint32_t timeout, std::function<void(void *)> callback)
57     {
58         index_ = AVCodecXCollie::GetInstance().SetTimer(name, recovery, timeout, callback);
59     };
60 
~AVCodecXcollieTimer()61     ~AVCodecXcollieTimer()
62     {
63         AVCodecXCollie::GetInstance().CancelTimer(index_);
64     }
65 private:
66     int32_t index_ = 0;
67 };
68 
69 class AVCodecXcollieInterfaceTimer {
70 public:
71     AVCodecXcollieInterfaceTimer(const std::string &name, bool isService = true,
72         bool recovery = false, uint32_t timeout = 30)
73     {
74         index_ = AVCodecXCollie::GetInstance().SetInterfaceTimer(name, isService, recovery, timeout);
75     };
76 
~AVCodecXcollieInterfaceTimer()77     ~AVCodecXcollieInterfaceTimer()
78     {
79         AVCodecXCollie::GetInstance().CancelTimer(index_);
80     }
81 private:
82     int32_t index_ = 0;
83 };
84 
85 #define COLLIE_LISTEN(statement, args...)                               \
86     {                                                                   \
87         AVCodecXcollieInterfaceTimer xCollie(args);                     \
88         statement;                                                      \
89     }
90 #define CLIENT_COLLIE_LISTEN(statement, name)                           \
91     {                                                                   \
92         AVCodecXcollieInterfaceTimer xCollie(name, false, false, 30);   \
93         statement;                                                      \
94     }
95 } // namespace MediaAVCodec
96 } // namespace OHOS
97 #endif // AVCODEC_XCOLLIE_H