1 /* 2 * Copyright (c) 2023-2023 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 #define HST_LOG_TAG "SteadyClock" 17 18 #include "common/log.h" 19 #include "osal/utils/steady_clock.h" 20 21 namespace { 22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "SteadyClock" }; 23 } 24 25 namespace OHOS { 26 namespace Media { 27 namespace { 28 using namespace std::chrono; 29 } 30 GetCurrentTimeMs()31int64_t SteadyClock::GetCurrentTimeMs() 32 { 33 return duration_cast<milliseconds>(high_resolution_clock::now().time_since_epoch()).count(); 34 } 35 GetCurrentTimeNanoSec()36int64_t SteadyClock::GetCurrentTimeNanoSec() 37 { 38 return duration_cast<nanoseconds>(high_resolution_clock::now().time_since_epoch()).count(); 39 } 40 SteadyClock()41SteadyClock::SteadyClock() : begin_(high_resolution_clock::now()) 42 { 43 MEDIA_LOG_D("ctor called."); 44 } 45 Reset()46void SteadyClock::Reset() 47 { 48 MEDIA_LOG_D("Reset timer."); 49 begin_ = high_resolution_clock::now(); 50 } 51 ElapsedMilliseconds()52int64_t SteadyClock::ElapsedMilliseconds() 53 { 54 return duration_cast<milliseconds>(high_resolution_clock::now() - begin_).count(); 55 } 56 ElapsedMicroseconds()57int64_t SteadyClock::ElapsedMicroseconds() 58 { 59 return duration_cast<microseconds>(high_resolution_clock::now() - begin_).count(); 60 } 61 ElapsedNanoseconds()62int64_t SteadyClock::ElapsedNanoseconds() 63 { 64 return duration_cast<nanoseconds>(high_resolution_clock::now() - begin_).count(); 65 } 66 ElapsedSeconds()67int64_t SteadyClock::ElapsedSeconds() 68 { 69 return duration_cast<seconds>(high_resolution_clock::now() - begin_).count(); 70 } 71 } // namespace Media 72 } // namespace OHOS 73