1 /*
2 * Copyright (C) 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 #include "test_utils.h"
17 #include "hcodec_api.h"
18 #include "hcodec_utils.h"
19 #include "hcodec_log.h"
20
21 namespace OHOS::MediaAVCodec {
22 using namespace std;
23 using namespace std::chrono;
24
GetCodecName(bool isEncoder,const string & mime)25 string GetCodecName(bool isEncoder, const string& mime)
26 {
27 vector<CapabilityData> caps;
28 int32_t ret = GetHCodecCapabilityList(caps);
29 if (ret != 0) {
30 return {};
31 }
32 AVCodecType targetType = isEncoder ? AVCODEC_TYPE_VIDEO_ENCODER : AVCODEC_TYPE_VIDEO_DECODER;
33 auto it = find_if(caps.begin(), caps.end(), [&mime, targetType](const CapabilityData& cap) {
34 return cap.mimeType == mime && cap.codecType == targetType;
35 });
36 if (it != caps.end()) {
37 return it->codecName;
38 }
39 return {};
40 }
41
Instance()42 CostRecorder& CostRecorder::Instance()
43 {
44 static CostRecorder inst;
45 return inst;
46 }
47
Clear()48 void CostRecorder::Clear()
49 {
50 records_.clear();
51 }
52
Update(steady_clock::time_point begin,const string & apiName)53 void CostRecorder::Update(steady_clock::time_point begin, const string& apiName)
54 {
55 auto cost = duration_cast<microseconds>(steady_clock::now() - begin).count();
56 records_[apiName].totalCost += cost;
57 records_[apiName].totalCnt++;
58 }
59
Print() const60 void CostRecorder::Print() const
61 {
62 for (const auto& one : records_) {
63 TLOGI("%s everage cost %.3f ms", one.first.c_str(),
64 one.second.totalCost / US_TO_MS / one.second.totalCnt);
65 }
66 }
67 }