• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2021 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 HISTREAMER_STEADY_CLOCK_H
17 #define HISTREAMER_STEADY_CLOCK_H
18 
19 #include <chrono>
20 #if defined(PROFILE)
21 #include "foundation/log.h"
22 #endif
23 
24 namespace OHOS {
25 namespace Media {
26 class SteadyClock {
27 public:
28     static int64_t GetCurrentTimeMs();
29     static int64_t GetCurrentTimeNanoSec();
30     SteadyClock();
31     ~SteadyClock() = default;
32     void Reset();
33     int64_t ElapsedMilliseconds();
34     int64_t ElapsedMicroseconds();
35     int64_t ElapsedNanoseconds();
36     int64_t ElapsedSeconds();
37 
38 private:
39     std::chrono::time_point<std::chrono::high_resolution_clock> begin_;
40 };
41 } // namespace Media
42 } // namespace OHOS
43 
44 #if defined(PROFILE)
45 #define PROFILE_BEGIN(message, ...)                                                                                    \
46     do {                                                                                                               \
47         MEDIA_LOG_I(message ", timestamp(ms): " PUBLIC_LOG_D64, ##__VA_ARGS__, SteadyClock::GetCurrentTimeMs());      \
48     } while (0);                                                                                                       \
49     OHOS::Media::SteadyClock _steadyClock;
50 #define PROFILE_RESET() _steadyClock.Reset()
51 
52 #define PROFILE_END(message, ...)                                                                                      \
53     do {                                                                                                               \
54         MEDIA_LOG_I(message ", timestamp(ms): " PUBLIC_LOG_D64 ", duration(ms): " PUBLIC_LOG_D64,                    \
55                     ##__VA_ARGS__, SteadyClock::GetCurrentTimeMs(), _steadyClock.ElapsedMilliseconds());               \
56     } while (0)
57 
58 #else
59 #define PROFILE_BEGIN(message, ...)
60 #define PROFILE_RESET()
61 #define PROFILE_END(message, ...)
62 #endif
63 
64 #endif // HISTREAMER_STEADY_CLOCK_H
65