1fa7767c5Sopenharmony_ci/*
2fa7767c5Sopenharmony_ci * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License.
5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at
6fa7767c5Sopenharmony_ci *
7fa7767c5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fa7767c5Sopenharmony_ci *
9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and
13fa7767c5Sopenharmony_ci * limitations under the License.
14fa7767c5Sopenharmony_ci */
15fa7767c5Sopenharmony_ci
16fa7767c5Sopenharmony_ci#ifndef HISTREAMER_PLUGIN_COMMON_TIME_H
17fa7767c5Sopenharmony_ci#define HISTREAMER_PLUGIN_COMMON_TIME_H
18fa7767c5Sopenharmony_ci
19fa7767c5Sopenharmony_cinamespace OHOS {
20fa7767c5Sopenharmony_cinamespace Media {
21fa7767c5Sopenharmony_cinamespace Plugin {
22fa7767c5Sopenharmony_ci#define HST_TIME_NONE ((int64_t)-1)
23fa7767c5Sopenharmony_ci#define HST_TIME_BASE ((int64_t)1)
24fa7767c5Sopenharmony_ci#define HST_NSECOND HST_TIME_BASE
25fa7767c5Sopenharmony_ci#define HST_USECOND ((int64_t)1000 * HST_NSECOND)
26fa7767c5Sopenharmony_ci#define HST_MSECOND ((int64_t)1000 * HST_USECOND)
27fa7767c5Sopenharmony_ci#define HST_SECOND ((int64_t)1000 * HST_MSECOND)
28fa7767c5Sopenharmony_ci
29fa7767c5Sopenharmony_ciinline int64_t HstTime2Ns(int64_t hTime)
30fa7767c5Sopenharmony_ci{
31fa7767c5Sopenharmony_ci    return hTime / HST_NSECOND;
32fa7767c5Sopenharmony_ci}
33fa7767c5Sopenharmony_ci
34fa7767c5Sopenharmony_ciinline bool Ns2HstTime (int64_t ns, int64_t& hTime)
35fa7767c5Sopenharmony_ci{
36fa7767c5Sopenharmony_ci    hTime = ns * HST_NSECOND;
37fa7767c5Sopenharmony_ci    return true;
38fa7767c5Sopenharmony_ci}
39fa7767c5Sopenharmony_ci
40fa7767c5Sopenharmony_ciinline int64_t HstTime2Us(int64_t hTime)
41fa7767c5Sopenharmony_ci{
42fa7767c5Sopenharmony_ci    return hTime / HST_USECOND;
43fa7767c5Sopenharmony_ci}
44fa7767c5Sopenharmony_ci
45fa7767c5Sopenharmony_ciinline bool Us2HstTime (int64_t us, int64_t& hTime)
46fa7767c5Sopenharmony_ci{
47fa7767c5Sopenharmony_ci    if (INT64_MAX / HST_USECOND < us || INT64_MIN / HST_USECOND > us) { // overflow
48fa7767c5Sopenharmony_ci        return false;
49fa7767c5Sopenharmony_ci    }
50fa7767c5Sopenharmony_ci    hTime = us * HST_USECOND;
51fa7767c5Sopenharmony_ci    return true;
52fa7767c5Sopenharmony_ci}
53fa7767c5Sopenharmony_ci
54fa7767c5Sopenharmony_ciinline int64_t HstTime2Ms(int64_t hTime)
55fa7767c5Sopenharmony_ci{
56fa7767c5Sopenharmony_ci    return hTime / HST_MSECOND;
57fa7767c5Sopenharmony_ci}
58fa7767c5Sopenharmony_ci
59fa7767c5Sopenharmony_ciinline bool Ms2HstTime (int64_t ms, int64_t& hTime)
60fa7767c5Sopenharmony_ci{
61fa7767c5Sopenharmony_ci    if (INT64_MAX / HST_MSECOND < ms || INT64_MIN / HST_MSECOND > ms) { // overflow
62fa7767c5Sopenharmony_ci        return false;
63fa7767c5Sopenharmony_ci    }
64fa7767c5Sopenharmony_ci    hTime = ms * HST_MSECOND;
65fa7767c5Sopenharmony_ci    return true;
66fa7767c5Sopenharmony_ci}
67fa7767c5Sopenharmony_ci
68fa7767c5Sopenharmony_ciinline int64_t HstTime2Sec(int64_t hTime)
69fa7767c5Sopenharmony_ci{
70fa7767c5Sopenharmony_ci    return hTime / HST_SECOND;
71fa7767c5Sopenharmony_ci}
72fa7767c5Sopenharmony_ci
73fa7767c5Sopenharmony_ciinline bool Sec2HstTime (int64_t sec, int64_t& hTime)
74fa7767c5Sopenharmony_ci{
75fa7767c5Sopenharmony_ci    if (INT64_MAX / HST_SECOND < sec || INT64_MIN / HST_SECOND > sec) { // overflow
76fa7767c5Sopenharmony_ci        return false;
77fa7767c5Sopenharmony_ci    }
78fa7767c5Sopenharmony_ci    hTime = sec * HST_SECOND;
79fa7767c5Sopenharmony_ci    return true;
80fa7767c5Sopenharmony_ci}
81fa7767c5Sopenharmony_ci} // Plugin
82fa7767c5Sopenharmony_ci} // Media
83fa7767c5Sopenharmony_ci} // OHOS
84fa7767c5Sopenharmony_ci#endif // HISTREAMER_PLUGIN_COMMON_TIME_H
85