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_PLUGIN_COMMON_TIME_H
17 #define HISTREAMER_PLUGIN_COMMON_TIME_H
18 
19 #include <chrono>
20 
21 namespace OHOS {
22 namespace Media {
23 namespace Plugins {
24 constexpr int HST_TIME_NONE = (int64_t)-1;
25 constexpr int HST_TIME_BASE = (int64_t)1;
26 constexpr int HST_NSECOND = HST_TIME_BASE;
27 constexpr int HST_USECOND = ((int64_t)1000 * HST_NSECOND);
28 constexpr int HST_MSECOND = ((int64_t)1000 * HST_USECOND);
29 constexpr int HST_SECOND = ((int64_t)1000 * HST_MSECOND);
30 
HstTime2Ns(int64_t hTime)31 inline int64_t HstTime2Ns(int64_t hTime)
32 {
33     return hTime / HST_NSECOND;
34 }
35 
Ns2HstTime(int64_t ns,int64_t & hTime)36 inline bool Ns2HstTime (int64_t ns, int64_t& hTime)
37 {
38     hTime = ns * HST_NSECOND;
39     return true;
40 }
41 
HstTime2Us(int64_t hTime)42 inline int64_t HstTime2Us(int64_t hTime)
43 {
44     return hTime / HST_USECOND;
45 }
46 
HstTime2Us32(int64_t hTime)47 inline int32_t HstTime2Us32(int64_t hTime)
48 {
49     int64_t ms = hTime / HST_USECOND;
50     if (INT32_MAX < ms ||  INT32_MIN > ms) { // overflow
51         return INT32_MAX;
52     }
53     return static_cast<int32_t>(ms);
54 }
55 
Us2HstTime(int64_t us,int64_t & hTime)56 inline bool Us2HstTime (int64_t us, int64_t& hTime)
57 {
58     if (INT64_MAX / HST_USECOND < us || INT64_MIN / HST_USECOND > us) { // overflow
59         return false;
60     }
61     hTime = us * HST_USECOND;
62     return true;
63 }
64 
HstTime2Ms(int64_t hTime)65 inline int64_t HstTime2Ms(int64_t hTime)
66 {
67     return hTime / HST_MSECOND;
68 }
69 
Ms2HstTime(int64_t ms,int64_t & hTime)70 inline bool Ms2HstTime (int64_t ms, int64_t& hTime)
71 {
72     if (INT64_MAX / HST_MSECOND < ms || INT64_MIN / HST_MSECOND > ms) { // overflow
73         return false;
74     }
75     hTime = ms * HST_MSECOND;
76     return true;
77 }
78 
HstTime2Sec(int64_t hTime)79 inline int64_t HstTime2Sec(int64_t hTime)
80 {
81     return hTime / HST_SECOND;
82 }
83 
Sec2HstTime(int64_t sec,int64_t & hTime)84 inline bool Sec2HstTime (int64_t sec, int64_t& hTime)
85 {
86     if (INT64_MAX / HST_SECOND < sec || INT64_MIN / HST_SECOND > sec) { // overflow
87         return false;
88     }
89     hTime = sec * HST_SECOND;
90     return true;
91 }
92 
Us2Ms(int64_t us)93 inline int64_t Us2Ms(int64_t us)
94 {
95     return us / HST_USECOND;
96 }
97 
GetCurrentMillisecond()98 inline int64_t GetCurrentMillisecond()
99 {
100     std::chrono::system_clock::duration duration = std::chrono::system_clock::now().time_since_epoch();
101     int64_t time = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
102     return time;
103 }
104 } // Plugins
105 } // Media
106 } // OHOS
107 #endif // HISTREAMER_PLUGIN_COMMON_TIME_H
108