1 /* 2 * Copyright 2015 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 "MediaCodecListOverrides" 19 #include <utils/Log.h> 20 21 #include "MediaCodecListOverrides.h" 22 23 #include <cutils/properties.h> 24 #include <gui/Surface.h> 25 #include <media/ICrypto.h> 26 #include <media/IMediaCodecList.h> 27 #include <media/MediaCodecInfo.h> 28 #include <media/MediaResourcePolicy.h> 29 #include <media/openmax/OMX_IVCommon.h> 30 #include <media/stagefright/foundation/AMessage.h> 31 #include <media/stagefright/MediaCodec.h> 32 #include <media/stagefright/MediaCodecList.h> 33 34 namespace android { 35 36 const char *kProfilingResults = "/data/misc/media/media_codecs_profiling_results.xml"; 37 38 AString getProfilingVersionString() { 39 char val[PROPERTY_VALUE_MAX]; 40 if (property_get("ro.build.display.id", val, NULL) && (strlen(val) > 0)) { 41 return AStringPrintf("<!-- Profiled-with: %s -->", val); 42 } 43 44 return "<!-- Profiled-with: UNKNOWN_BUILD_ID -->"; 45 } 46 47 // a limit to avoid allocating unreasonable number of codec instances in the measurement. 48 // this should be in sync with the MAX_SUPPORTED_INSTANCES defined in MediaCodecInfo.java. 49 static const int kMaxInstances = 32; 50 51 // TODO: move MediaCodecInfo to C++. Until then, some temp methods to parse out info. 52 static bool getMeasureSize(const sp<MediaCodecInfo::Capabilities> &caps, int32_t *width, int32_t *height) { 53 AString sizeRange; 54 if (!caps->getDetails()->findString("size-range", &sizeRange)) { 55 return false; 56 } 57 AString minSize; 58 AString maxSize; 59 if (!splitString(sizeRange, "-", &minSize, &maxSize)) { 60 return false; 61 } 62 AString sWidth; 63 AString sHeight; 64 if (!splitString(minSize, "x", &sWidth, &sHeight)) { 65 if (!splitString(minSize, "*", &sWidth, &sHeight)) { 66 return false; 67 } 68 } 69 70 *width = strtol(sWidth.c_str(), NULL, 10); 71 *height = strtol(sHeight.c_str(), NULL, 10); 72 return (*width > 0) && (*height > 0); 73 } 74 75 static void getMeasureBitrate(const sp<MediaCodecInfo::Capabilities> &caps, int32_t *bitrate) { 76 // Until have native MediaCodecInfo, we cannot get bitrates based on profile/levels. 77 // We use 200000 as default value for our measurement. 78 *bitrate = 200000; 79 AString bitrateRange; 80 if (!caps->getDetails()->findString("bitrate-range", &bitrateRange)) { 81 return; 82 } 83 AString minBitrate; 84 AString maxBitrate; 85 if (!splitString(bitrateRange, "-", &minBitrate, &maxBitrate)) { 86 return; 87 } 88 89 *bitrate = strtol(minBitrate.c_str(), NULL, 10); 90 } 91 92 static sp<AMessage> getMeasureFormat( 93 bool isEncoder, const AString &mime, const sp<MediaCodecInfo::Capabilities> &caps) { 94 sp<AMessage> format = new AMessage(); 95 format->setString("mime", mime); 96 97 if (isEncoder) { 98 int32_t bitrate = 0; 99 getMeasureBitrate(caps, &bitrate); 100 format->setInt32("bitrate", bitrate); 101 format->setInt32("encoder", 1); 102 } 103 104 if (mime.startsWith("video/")) { 105 int32_t width = 0; 106 int32_t height = 0; 107 if (!getMeasureSize(caps, &width, &height)) { 108 return NULL; 109 } 110 format->setInt32("width", width); 111 format->setInt32("height", height); 112 113 Vector<uint32_t> colorFormats; 114 caps->getSupportedColorFormats(&colorFormats); 115 if (colorFormats.size() == 0) { 116 return NULL; 117 } 118 format->setInt32("color-format", colorFormats[0]); 119 120 format->setFloat("frame-rate", 10.0); 121 format->setInt32("i-frame-interval", 10); 122 } else { 123 // TODO: profile hw audio 124 return NULL; 125 } 126 127 return format; 128 } 129 130 static size_t doProfileCodecs( 131 bool isEncoder, const AString &name, const AString &mime, const sp<MediaCodecInfo::Capabilities> &caps) { 132 sp<AMessage> format = getMeasureFormat(isEncoder, mime, caps); 133 if (format == NULL) { 134 return 0; 135 } 136 ALOGV("doProfileCodecs %s %s %s %s", 137 name.c_str(), mime.c_str(), isEncoder ? "encoder" : "decoder", 138 format->debugString().c_str()); 139 140 status_t err = OK; 141 Vector<sp<MediaCodec>> codecs; 142 while (err == OK && codecs.size() < kMaxInstances) { 143 sp<ALooper> looper = new ALooper; 144 looper->setName("MediaCodec_looper"); 145 ALOGV("doProfileCodecs for codec #%zu", codecs.size()); 146 ALOGV("doProfileCodecs start looper"); 147 looper->start( 148 false /* runOnCallingThread */, false /* canCallJava */, ANDROID_PRIORITY_AUDIO); 149 ALOGV("doProfileCodecs CreateByComponentName"); 150 sp<MediaCodec> codec = MediaCodec::CreateByComponentName(looper, name.c_str(), &err); 151 if (err != OK) { 152 ALOGV("Failed to create codec: %s", name.c_str()); 153 break; 154 } 155 const sp<Surface> nativeWindow; 156 const sp<ICrypto> crypto; 157 uint32_t flags = isEncoder ? MediaCodec::CONFIGURE_FLAG_ENCODE : 0; 158 ALOGV("doProfileCodecs configure"); 159 err = codec->configure(format, nativeWindow, crypto, flags); 160 if (err != OK) { 161 ALOGV("Failed to configure codec: %s with mime: %s", name.c_str(), mime.c_str()); 162 codec->release(); 163 break; 164 } 165 ALOGV("doProfileCodecs start"); 166 err = codec->start(); 167 if (err != OK) { 168 ALOGV("Failed to start codec: %s with mime: %s", name.c_str(), mime.c_str()); 169 codec->release(); 170 break; 171 } 172 codecs.push_back(codec); 173 } 174 175 for (size_t i = 0; i < codecs.size(); ++i) { 176 ALOGV("doProfileCodecs release %s", name.c_str()); 177 err = codecs[i]->release(); 178 if (err != OK) { 179 ALOGE("Failed to release codec: %s with mime: %s", name.c_str(), mime.c_str()); 180 } 181 } 182 183 return codecs.size(); 184 } 185 186 bool splitString(const AString &s, const AString &delimiter, AString *s1, AString *s2) { 187 ssize_t pos = s.find(delimiter.c_str()); 188 if (pos < 0) { 189 return false; 190 } 191 *s1 = AString(s, 0, pos); 192 *s2 = AString(s, pos + 1, s.size() - pos - 1); 193 return true; 194 } 195 196 bool splitString( 197 const AString &s, const AString &delimiter, AString *s1, AString *s2, AString *s3) { 198 AString temp; 199 if (!splitString(s, delimiter, s1, &temp)) { 200 return false; 201 } 202 if (!splitString(temp, delimiter, s2, s3)) { 203 return false; 204 } 205 return true; 206 } 207 208 void profileCodecs(const Vector<sp<MediaCodecInfo>> &infos) { 209 CodecSettings global_results; 210 KeyedVector<AString, CodecSettings> encoder_results; 211 KeyedVector<AString, CodecSettings> decoder_results; 212 profileCodecs(infos, &global_results, &encoder_results, &decoder_results); 213 exportResultsToXML(kProfilingResults, global_results, encoder_results, decoder_results); 214 } 215 216 void profileCodecs( 217 const Vector<sp<MediaCodecInfo>> &infos, 218 CodecSettings *global_results, 219 KeyedVector<AString, CodecSettings> *encoder_results, 220 KeyedVector<AString, CodecSettings> *decoder_results, 221 bool forceToMeasure) { 222 KeyedVector<AString, sp<MediaCodecInfo::Capabilities>> codecsNeedMeasure; 223 AString supportMultipleSecureCodecs = "true"; 224 for (size_t i = 0; i < infos.size(); ++i) { 225 const sp<MediaCodecInfo> info = infos[i]; 226 AString name = info->getCodecName(); 227 if (name.startsWith("OMX.google.") || 228 // TODO: reenable below codecs once fixed 229 name == "OMX.Intel.VideoDecoder.VP9.hybrid") { 230 continue; 231 } 232 233 Vector<AString> mimes; 234 info->getSupportedMimes(&mimes); 235 for (size_t i = 0; i < mimes.size(); ++i) { 236 const sp<MediaCodecInfo::Capabilities> &caps = 237 info->getCapabilitiesFor(mimes[i].c_str()); 238 if (!forceToMeasure && 239 (caps->getDetails()->contains("max-supported-instances") || 240 caps->getDetails()->contains("max-concurrent-instances"))) { 241 continue; 242 } 243 244 size_t max = doProfileCodecs(info->isEncoder(), name, mimes[i], caps); 245 if (max > 0) { 246 CodecSettings settings; 247 char maxStr[32]; 248 sprintf(maxStr, "%zu", max); 249 settings.add("max-supported-instances", maxStr); 250 251 AString key = name; 252 key.append(" "); 253 key.append(mimes[i]); 254 255 if (info->isEncoder()) { 256 encoder_results->add(key, settings); 257 } else { 258 decoder_results->add(key, settings); 259 } 260 261 if (name.endsWith(".secure")) { 262 if (max <= 1) { 263 supportMultipleSecureCodecs = "false"; 264 } 265 } 266 } 267 } 268 } 269 global_results->add(kPolicySupportsMultipleSecureCodecs, supportMultipleSecureCodecs); 270 } 271 272 static AString globalResultsToXml(const CodecSettings &results) { 273 AString ret; 274 for (size_t i = 0; i < results.size(); ++i) { 275 AString setting = AStringPrintf( 276 " <Setting name=\"%s\" value=\"%s\" />\n", 277 results.keyAt(i).c_str(), 278 results.valueAt(i).c_str()); 279 ret.append(setting); 280 } 281 return ret; 282 } 283 284 static AString codecResultsToXml(const KeyedVector<AString, CodecSettings> &results) { 285 AString ret; 286 for (size_t i = 0; i < results.size(); ++i) { 287 AString name; 288 AString mime; 289 if (!splitString(results.keyAt(i), " ", &name, &mime)) { 290 continue; 291 } 292 AString codec = 293 AStringPrintf(" <MediaCodec name=\"%s\" type=\"%s\" update=\"true\" >\n", 294 name.c_str(), 295 mime.c_str()); 296 ret.append(codec); 297 const CodecSettings &settings = results.valueAt(i); 298 for (size_t i = 0; i < settings.size(); ++i) { 299 // WARNING: we assume all the settings are "Limit". Currently we have only one type 300 // of setting in this case, which is "max-supported-instances". 301 AString setting = AStringPrintf( 302 " <Limit name=\"%s\" value=\"%s\" />\n", 303 settings.keyAt(i).c_str(), 304 settings.valueAt(i).c_str()); 305 ret.append(setting); 306 } 307 ret.append(" </MediaCodec>\n"); 308 } 309 return ret; 310 } 311 312 void exportResultsToXML( 313 const char *fileName, 314 const CodecSettings &global_results, 315 const KeyedVector<AString, CodecSettings> &encoder_results, 316 const KeyedVector<AString, CodecSettings> &decoder_results) { 317 if (global_results.size() == 0 && encoder_results.size() == 0 && decoder_results.size() == 0) { 318 return; 319 } 320 321 AString overrides; 322 overrides.append(getProfilingVersionString()); 323 overrides.append("\n"); 324 overrides.append("<MediaCodecs>\n"); 325 if (global_results.size() > 0) { 326 overrides.append(" <Settings>\n"); 327 overrides.append(globalResultsToXml(global_results)); 328 overrides.append(" </Settings>\n"); 329 } 330 if (encoder_results.size() > 0) { 331 overrides.append(" <Encoders>\n"); 332 overrides.append(codecResultsToXml(encoder_results)); 333 overrides.append(" </Encoders>\n"); 334 } 335 if (decoder_results.size() > 0) { 336 overrides.append(" <Decoders>\n"); 337 overrides.append(codecResultsToXml(decoder_results)); 338 overrides.append(" </Decoders>\n"); 339 } 340 overrides.append("</MediaCodecs>\n"); 341 342 FILE *f = fopen(fileName, "wb"); 343 if (f == NULL) { 344 ALOGE("Failed to open %s for writing.", fileName); 345 return; 346 } 347 if (fwrite(overrides.c_str(), 1, overrides.size(), f) != overrides.size()) { 348 ALOGE("Failed to write to %s.", fileName); 349 } 350 fclose(f); 351 } 352 353 } // namespace android 354