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