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 namespace OHOS {
20 namespace Media {
21 namespace Plugin {
22 #define HST_TIME_BASE ((int64_t)1)
23 #define HST_NSECOND HST_TIME_BASE
24 #define HST_USECOND ((int64_t)1000 * HST_NSECOND)
25 #define HST_MSECOND ((int64_t)1000 * HST_USECOND)
26 #define HST_SECOND ((int64_t)1000 * HST_MSECOND)
27
HstTime2Ns(int64_t hTime)28 inline int64_t HstTime2Ns(int64_t hTime)
29 {
30 return hTime / HST_NSECOND;
31 }
32
Ns2HstTime(int64_t ns,int64_t & hTime)33 inline bool Ns2HstTime (int64_t ns, int64_t& hTime)
34 {
35 hTime = ns * HST_NSECOND;
36 return true;
37 }
38
HstTime2Us(int64_t hTime)39 inline int64_t HstTime2Us(int64_t hTime)
40 {
41 return hTime / HST_USECOND;
42 }
43
Us2HstTime(int64_t us,int64_t & hTime)44 inline bool Us2HstTime (int64_t us, int64_t& hTime)
45 {
46 if (INT64_MAX / HST_USECOND < us || INT64_MIN / HST_USECOND > us) { // overflow
47 return false;
48 }
49 hTime = us * HST_USECOND;
50 return true;
51 }
52
HstTime2Ms(int64_t hTime)53 inline int64_t HstTime2Ms(int64_t hTime)
54 {
55 return hTime / HST_MSECOND;
56 }
57
Ms2HstTime(int64_t ms,int64_t & hTime)58 inline bool Ms2HstTime (int64_t ms, int64_t& hTime)
59 {
60 if (INT64_MAX / HST_MSECOND < ms || INT64_MIN / HST_MSECOND > ms) { // overflow
61 return false;
62 }
63 hTime = ms * HST_MSECOND;
64 return true;
65 }
66
HstTime2Sec(int64_t hTime)67 inline int64_t HstTime2Sec(int64_t hTime)
68 {
69 return hTime / HST_SECOND;
70 }
71
Sec2HstTime(int64_t sec,int64_t & hTime)72 inline bool Sec2HstTime (int64_t sec, int64_t& hTime)
73 {
74 if (INT64_MAX / HST_SECOND < sec || INT64_MIN / HST_SECOND > sec) { // overflow
75 return false;
76 }
77 hTime = sec * HST_SECOND;
78 return true;
79 }
80 } // Plugin
81 } // Media
82 } // OHOS
83 #endif // HISTREAMER_PLUGIN_COMMON_TIME_H
84