1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "OmxInfoBuilder"
19
20 #ifdef __LP64__
21 #define OMX_ANDROID_COMPILE_AS_32BIT_ON_64BIT_PLATFORMS
22 #endif
23
24 #include <utils/Log.h>
25 #include <cutils/properties.h>
26
27 #include <media/stagefright/foundation/MediaDefs.h>
28 #include <media/stagefright/OmxInfoBuilder.h>
29 #include <media/stagefright/ACodec.h>
30
31 #include <android/hardware/media/omx/1.0/IOmxStore.h>
32 #include <android/hardware/media/omx/1.0/IOmx.h>
33 #include <android/hardware/media/omx/1.0/IOmxNode.h>
34 #include <media/stagefright/omx/OMXUtils.h>
35
36 #include <media/IOMX.h>
37 #include <media/omx/1.0/WOmx.h>
38 #include <media/stagefright/omx/1.0/OmxStore.h>
39
40 #include <media/openmax/OMX_Index.h>
41 #include <media/openmax/OMX_IndexExt.h>
42 #include <media/openmax/OMX_Audio.h>
43 #include <media/openmax/OMX_AudioExt.h>
44 #include <media/openmax/OMX_Video.h>
45 #include <media/openmax/OMX_VideoExt.h>
46
47 namespace android {
48
49 using ::android::hardware::hidl_string;
50 using ::android::hardware::hidl_vec;
51 using namespace ::android::hardware::media::omx::V1_0;
52
53 namespace /* unnamed */ {
54
hasPrefix(const hidl_string & s,const char * prefix)55 bool hasPrefix(const hidl_string& s, const char* prefix) {
56 return strncmp(s.c_str(), prefix, strlen(prefix)) == 0;
57 }
58
queryCapabilities(const IOmxStore::NodeInfo & node,const char * mime,bool isEncoder,MediaCodecInfo::CapabilitiesWriter * caps)59 status_t queryCapabilities(
60 const IOmxStore::NodeInfo& node, const char* mime, bool isEncoder,
61 MediaCodecInfo::CapabilitiesWriter* caps) {
62 sp<ACodec> codec = new ACodec();
63 status_t err = codec->queryCapabilities(
64 node.owner.c_str(), node.name.c_str(), mime, isEncoder, caps);
65 if (err != OK) {
66 return err;
67 }
68 for (const auto& attribute : node.attributes) {
69 // All features have an int32 value except
70 // "feature-bitrate-modes", which has a string value.
71 if (hasPrefix(attribute.key, "feature-") &&
72 !hasPrefix(attribute.key, "feature-bitrate-modes")) {
73 // If this attribute.key is a feature that is not bitrate modes,
74 // add an int32 value.
75 caps->addDetail(
76 attribute.key.c_str(),
77 hasPrefix(attribute.value, "1") ? 1 : 0);
78 } else {
79 // Non-feature attributes
80 caps->addDetail(
81 attribute.key.c_str(), attribute.value.c_str());
82 }
83 }
84 return OK;
85 }
86
87 } // unnamed namespace
88
OmxInfoBuilder()89 OmxInfoBuilder::OmxInfoBuilder() {
90 }
91
buildMediaCodecList(MediaCodecListWriter * writer)92 status_t OmxInfoBuilder::buildMediaCodecList(MediaCodecListWriter* writer) {
93 // Obtain IOmxStore
94 sp<IOmxStore> omxStore = IOmxStore::getService();
95 if (omxStore == nullptr) {
96 ALOGE("Cannot find an IOmxStore service.");
97 return NO_INIT;
98 }
99
100 // List service attributes (global settings)
101 Status status;
102 hidl_vec<IOmxStore::RoleInfo> roles;
103 auto transStatus = omxStore->listRoles(
104 [&roles] (
105 const hidl_vec<IOmxStore::RoleInfo>& inRoleList) {
106 roles = inRoleList;
107 });
108 if (!transStatus.isOk()) {
109 ALOGE("Fail to obtain codec roles from IOmxStore.");
110 return NO_INIT;
111 }
112
113 hidl_vec<IOmxStore::ServiceAttribute> serviceAttributes;
114 transStatus = omxStore->listServiceAttributes(
115 [&status, &serviceAttributes] (
116 Status inStatus,
117 const hidl_vec<IOmxStore::ServiceAttribute>& inAttributes) {
118 status = inStatus;
119 serviceAttributes = inAttributes;
120 });
121 if (!transStatus.isOk()) {
122 ALOGE("Fail to obtain global settings from IOmxStore.");
123 return NO_INIT;
124 }
125 if (status != Status::OK) {
126 ALOGE("IOmxStore reports parsing error.");
127 return NO_INIT;
128 }
129 for (const auto& p : serviceAttributes) {
130 writer->addGlobalSetting(
131 p.key.c_str(), p.value.c_str());
132 }
133
134 // Convert roles to lists of codecs
135
136 // codec name -> index into swCodecs/hwCodecs
137 std::map<hidl_string, std::unique_ptr<MediaCodecInfoWriter>>
138 swCodecName2Info, hwCodecName2Info;
139
140 char rank[PROPERTY_VALUE_MAX];
141 uint32_t defaultRank = 0x100;
142 if (property_get("debug.stagefright.omx_default_rank", rank, nullptr)) {
143 defaultRank = std::strtoul(rank, nullptr, 10);
144 }
145 for (const IOmxStore::RoleInfo& role : roles) {
146 const hidl_string& typeName = role.type;
147 bool isEncoder = role.isEncoder;
148 bool preferPlatformNodes = role.preferPlatformNodes;
149 // If preferPlatformNodes is true, hardware nodes must be added after
150 // platform (software) nodes. hwCodecs is used to hold hardware nodes
151 // that need to be added after software nodes for the same role.
152 std::vector<const IOmxStore::NodeInfo*> hwCodecs;
153 for (const IOmxStore::NodeInfo& node : role.nodes) {
154 const hidl_string& nodeName = node.name;
155 bool isSoftware = hasPrefix(nodeName, "OMX.google");
156 MediaCodecInfoWriter* info;
157 if (isSoftware) {
158 auto c2i = swCodecName2Info.find(nodeName);
159 if (c2i == swCodecName2Info.end()) {
160 // Create a new MediaCodecInfo for a new node.
161 c2i = swCodecName2Info.insert(std::make_pair(
162 nodeName, writer->addMediaCodecInfo())).first;
163 info = c2i->second.get();
164 info->setName(nodeName.c_str());
165 info->setOwner(node.owner.c_str());
166 info->setEncoder(isEncoder);
167 info->setRank(defaultRank);
168 } else {
169 // The node has been seen before. Simply retrieve the
170 // existing MediaCodecInfoWriter.
171 info = c2i->second.get();
172 }
173 } else {
174 auto c2i = hwCodecName2Info.find(nodeName);
175 if (c2i == hwCodecName2Info.end()) {
176 // Create a new MediaCodecInfo for a new node.
177 if (!preferPlatformNodes) {
178 c2i = hwCodecName2Info.insert(std::make_pair(
179 nodeName, writer->addMediaCodecInfo())).first;
180 info = c2i->second.get();
181 info->setName(nodeName.c_str());
182 info->setOwner(node.owner.c_str());
183 info->setEncoder(isEncoder);
184 info->setRank(defaultRank);
185 } else {
186 // If preferPlatformNodes is true, this node must be
187 // added after all software nodes.
188 hwCodecs.push_back(&node);
189 continue;
190 }
191 } else {
192 // The node has been seen before. Simply retrieve the
193 // existing MediaCodecInfoWriter.
194 info = c2i->second.get();
195 }
196 }
197 std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps =
198 info->addMime(typeName.c_str());
199 if (queryCapabilities(
200 node, typeName.c_str(), isEncoder, caps.get()) != OK) {
201 ALOGW("Fail to add mime %s to codec %s",
202 typeName.c_str(), nodeName.c_str());
203 info->removeMime(typeName.c_str());
204 }
205 }
206
207 // If preferPlatformNodes is true, hardware nodes will not have been
208 // added in the loop above, but rather saved in hwCodecs. They are
209 // going to be added here.
210 if (preferPlatformNodes) {
211 for (const IOmxStore::NodeInfo *node : hwCodecs) {
212 MediaCodecInfoWriter* info;
213 const hidl_string& nodeName = node->name;
214 auto c2i = hwCodecName2Info.find(nodeName);
215 if (c2i == hwCodecName2Info.end()) {
216 // Create a new MediaCodecInfo for a new node.
217 c2i = hwCodecName2Info.insert(std::make_pair(
218 nodeName, writer->addMediaCodecInfo())).first;
219 info = c2i->second.get();
220 info->setName(nodeName.c_str());
221 info->setOwner(node->owner.c_str());
222 info->setEncoder(isEncoder);
223 info->setRank(defaultRank);
224 } else {
225 // The node has been seen before. Simply retrieve the
226 // existing MediaCodecInfoWriter.
227 info = c2i->second.get();
228 }
229 std::unique_ptr<MediaCodecInfo::CapabilitiesWriter> caps =
230 info->addMime(typeName.c_str());
231 if (queryCapabilities(
232 *node, typeName.c_str(), isEncoder, caps.get()) != OK) {
233 ALOGW("Fail to add mime %s to codec %s "
234 "after software codecs",
235 typeName.c_str(), nodeName.c_str());
236 info->removeMime(typeName.c_str());
237 }
238 }
239 }
240 }
241 return OK;
242 }
243
244 } // namespace android
245
246