• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 #include "avcodec_list_impl.h"
17 #include "media_log.h"
18 #include "media_errors.h"
19 #include "i_media_service.h"
20 
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecListImpl"};
23 }
24 namespace OHOS {
25 namespace Media {
CreateAVCodecList()26 std::shared_ptr<AVCodecList> AVCodecListFactory::CreateAVCodecList()
27 {
28     std::shared_ptr<AVCodecListImpl> impl = std::make_shared<AVCodecListImpl>();
29     CHECK_AND_RETURN_RET_LOG(impl != nullptr, nullptr, "failed to new AVCodecListImpl");
30 
31     int32_t ret = impl->Init();
32     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to init AVCodecListImpl");
33 
34     return impl;
35 }
36 
Init()37 int32_t AVCodecListImpl::Init()
38 {
39     codecListService_ = MediaServiceFactory::GetInstance().CreateAVCodecListService();
40     CHECK_AND_RETURN_RET_LOG(codecListService_ != nullptr, MSERR_UNKNOWN, "failed to create AVCodecList service");
41     return MSERR_OK;
42 }
43 
AVCodecListImpl()44 AVCodecListImpl::AVCodecListImpl()
45 {
46     MEDIA_LOGD("AVCodecListImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
47 }
48 
~AVCodecListImpl()49 AVCodecListImpl::~AVCodecListImpl()
50 {
51     if (codecListService_ != nullptr) {
52         (void)MediaServiceFactory::GetInstance().DestroyAVCodecListService(codecListService_);
53         codecListService_ = nullptr;
54     }
55     MEDIA_LOGD("AVCodecListImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
56 }
57 
FindVideoDecoder(const Format & format)58 std::string AVCodecListImpl::FindVideoDecoder(const Format &format)
59 {
60     CHECK_AND_RETURN_RET_LOG(codecListService_ != nullptr, "", "AvCodecList service does not exist..");
61     return codecListService_->FindVideoDecoder(format);
62 }
63 
FindVideoEncoder(const Format & format)64 std::string AVCodecListImpl::FindVideoEncoder(const Format &format)
65 {
66     CHECK_AND_RETURN_RET_LOG(codecListService_ != nullptr, "", "AvCodecList service does not exist..");
67     return codecListService_->FindVideoEncoder(format);
68 }
69 
FindAudioDecoder(const Format & format)70 std::string AVCodecListImpl::FindAudioDecoder(const Format &format)
71 {
72     CHECK_AND_RETURN_RET_LOG(codecListService_ != nullptr, "", "AvCodecList service does not exist..");
73     return codecListService_->FindAudioDecoder(format);
74 }
75 
FindAudioEncoder(const Format & format)76 std::string AVCodecListImpl::FindAudioEncoder(const Format &format)
77 {
78     CHECK_AND_RETURN_RET_LOG(codecListService_ != nullptr, "", "AvCodecList service does not exist..");
79     return codecListService_->FindAudioEncoder(format);
80 }
81 
GetCodecCapabilityInfos()82 std::vector<CapabilityData> AVCodecListImpl::GetCodecCapabilityInfos()
83 {
84     return codecListService_->GetCodecCapabilityInfos();
85 }
86 
GetVideoDecoderCaps()87 std::vector<std::shared_ptr<VideoCaps>> AVCodecListImpl::GetVideoDecoderCaps()
88 {
89     std::vector<CapabilityData> capabilityArray = GetCodecCapabilityInfos();
90 
91     SelectTargetCapabilityDataArray(capabilityArray, AVCODEC_TYPE_VIDEO_DECODER);
92     std::vector<std::shared_ptr<VideoCaps>> videoCapsArray;
93     for (auto iter = capabilityArray.begin(); iter != capabilityArray.end(); iter++) {
94         std::shared_ptr<VideoCaps> videoCaps = std::make_shared<VideoCaps>(*iter);
95         CHECK_AND_RETURN_RET_LOG(videoCaps != nullptr, videoCapsArray, "Is null mem");
96         videoCapsArray.push_back(videoCaps);
97     }
98     return videoCapsArray;
99 }
100 
GetVideoEncoderCaps()101 std::vector<std::shared_ptr<VideoCaps>> AVCodecListImpl::GetVideoEncoderCaps()
102 {
103     std::vector<CapabilityData> capabilityArray = GetCodecCapabilityInfos();
104     SelectTargetCapabilityDataArray(capabilityArray, AVCODEC_TYPE_VIDEO_ENCODER);
105     std::vector<std::shared_ptr<VideoCaps>> videoCapsArray;
106     for (auto iter = capabilityArray.begin(); iter != capabilityArray.end(); iter++) {
107         std::shared_ptr<VideoCaps> videoCaps = std::make_shared<VideoCaps>(*iter);
108         CHECK_AND_RETURN_RET_LOG(videoCaps != nullptr, videoCapsArray, "Is null mem");
109         videoCapsArray.push_back(videoCaps);
110     }
111     return videoCapsArray;
112 }
113 
GetAudioDecoderCaps()114 std::vector<std::shared_ptr<AudioCaps>> AVCodecListImpl::GetAudioDecoderCaps()
115 {
116     std::vector<CapabilityData> capabilityArray = GetCodecCapabilityInfos();
117     SelectTargetCapabilityDataArray(capabilityArray, AVCODEC_TYPE_AUDIO_DECODER);
118     std::vector<std::shared_ptr<AudioCaps>> audioCapsArray;
119     for (auto iter = capabilityArray.begin(); iter != capabilityArray.end(); iter++) {
120         std::shared_ptr<AudioCaps> audioCaps = std::make_shared<AudioCaps>(*iter);
121         CHECK_AND_RETURN_RET_LOG(audioCaps != nullptr, audioCapsArray, "Is null mem");
122         audioCapsArray.push_back(audioCaps);
123     }
124     return audioCapsArray;
125 }
126 
GetAudioEncoderCaps()127 std::vector<std::shared_ptr<AudioCaps>> AVCodecListImpl::GetAudioEncoderCaps()
128 {
129     std::vector<CapabilityData> capabilityArray = GetCodecCapabilityInfos();
130     SelectTargetCapabilityDataArray(capabilityArray, AVCODEC_TYPE_AUDIO_ENCODER);
131     std::vector<std::shared_ptr<AudioCaps>> audioCapsArray;
132     for (auto iter = capabilityArray.begin(); iter != capabilityArray.end(); iter++) {
133         std::shared_ptr<AudioCaps> audioCaps = std::make_shared<AudioCaps>(*iter);
134         CHECK_AND_RETURN_RET_LOG(audioCaps != nullptr, audioCapsArray, "Is null mem");
135         audioCapsArray.push_back(audioCaps);
136     }
137     return audioCapsArray;
138 }
139 
SelectTargetCapabilityDataArray(std::vector<CapabilityData> & capabilityArray,const AVCodecType & codecType)140 void AVCodecListImpl::SelectTargetCapabilityDataArray(std::vector<CapabilityData> &capabilityArray,
141                                                       const AVCodecType &codecType)
142 {
143     for (auto iter = capabilityArray.begin(); iter != capabilityArray.end();) {
144         if (iter->codecType == codecType) {
145             ++iter;
146         } else {
147             iter = capabilityArray.erase(iter);
148         }
149     }
150 }
151 } // namespace Media
152 } // namespace OHOS