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