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