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