• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 
Ms2Us(int64_t ms,int64_t & us)65 inline bool Ms2Us(int64_t ms, int64_t& us)
66 {
67     return Us2HstTime(ms, us);
68 }
69 
HstTime2Ms(int64_t hTime)70 inline int64_t HstTime2Ms(int64_t hTime)
71 {
72     return hTime / HST_MSECOND;
73 }
74 
Ms2HstTime(int64_t ms,int64_t & hTime)75 inline bool Ms2HstTime (int64_t ms, int64_t& hTime)
76 {
77     if (INT64_MAX / HST_MSECOND < ms || INT64_MIN / HST_MSECOND > ms) { // overflow
78         return false;
79     }
80     hTime = ms * HST_MSECOND;
81     return true;
82 }
83 
HstTime2Sec(int64_t hTime)84 inline int64_t HstTime2Sec(int64_t hTime)
85 {
86     return hTime / HST_SECOND;
87 }
88 
Sec2HstTime(int64_t sec,int64_t & hTime)89 inline bool Sec2HstTime (int64_t sec, int64_t& hTime)
90 {
91     if (INT64_MAX / HST_SECOND < sec || INT64_MIN / HST_SECOND > sec) { // overflow
92         return false;
93     }
94     hTime = sec * HST_SECOND;
95     return true;
96 }
97 
Us2Ms(int64_t us)98 inline int64_t Us2Ms(int64_t us)
99 {
100     return us / HST_USECOND;
101 }
102 
GetCurrentMillisecond()103 inline int64_t GetCurrentMillisecond()
104 {
105     std::chrono::system_clock::duration duration = std::chrono::system_clock::now().time_since_epoch();
106     int64_t time = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
107     return time;
108 }
109 
GetCurrentMicrosecond()110 inline int64_t GetCurrentMicrosecond()
111 {
112     std::chrono::system_clock::duration duration = std::chrono::system_clock::now().time_since_epoch();
113     int64_t time = std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
114     return time;
115 }
116 } // Plugins
117 } // Media
118 } // OHOS
119 #endif // HISTREAMER_PLUGIN_COMMON_TIME_H
120