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