MediaCodecList.cpp revision 1381d4b5c0385aec3741073e5998773b064c1fb0
1/*
2 * Copyright 2012, 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 "MediaCodecList"
19#include <utils/Log.h>
20
21#include <binder/IServiceManager.h>
22
23#include <media/IMediaCodecList.h>
24#include <media/IMediaPlayerService.h>
25#include <media/MediaCodecInfo.h>
26
27#include <media/stagefright/foundation/ADebug.h>
28#include <media/stagefright/foundation/AMessage.h>
29#include <media/stagefright/MediaCodecList.h>
30#include <media/stagefright/MediaErrors.h>
31#include <media/stagefright/OMXClient.h>
32#include <media/stagefright/OMXCodec.h>
33
34#include <utils/threads.h>
35
36#include <libexpat/expat.h>
37
38namespace android {
39
40static Mutex sInitMutex;
41
42static MediaCodecList *gCodecList = NULL;
43
44// static
45sp<IMediaCodecList> MediaCodecList::sCodecList;
46
47// static
48sp<IMediaCodecList> MediaCodecList::getLocalInstance() {
49    Mutex::Autolock autoLock(sInitMutex);
50
51    if (gCodecList == NULL) {
52        gCodecList = new MediaCodecList;
53        if (gCodecList->initCheck() == OK) {
54            sCodecList = gCodecList;
55        }
56    }
57
58    return sCodecList;
59}
60
61static Mutex sRemoteInitMutex;
62
63sp<IMediaCodecList> MediaCodecList::sRemoteList;
64
65// static
66sp<IMediaCodecList> MediaCodecList::getInstance() {
67    Mutex::Autolock _l(sRemoteInitMutex);
68    if (sRemoteList == NULL) {
69        sp<IBinder> binder =
70            defaultServiceManager()->getService(String16("media.player"));
71        sp<IMediaPlayerService> service =
72            interface_cast<IMediaPlayerService>(binder);
73        if (service.get() != NULL) {
74            sRemoteList = service->getCodecList();
75        }
76
77        if (sRemoteList == NULL) {
78            // if failed to get remote list, create local list
79            sRemoteList = getLocalInstance();
80        }
81    }
82    return sRemoteList;
83}
84
85MediaCodecList::MediaCodecList()
86    : mInitCheck(NO_INIT) {
87    parseTopLevelXMLFile("/etc/media_codecs.xml");
88}
89
90void MediaCodecList::parseTopLevelXMLFile(const char *codecs_xml) {
91    // get href_base
92    char *href_base_end = strrchr(codecs_xml, '/');
93    if (href_base_end != NULL) {
94        mHrefBase = AString(codecs_xml, href_base_end - codecs_xml + 1);
95    }
96
97    mInitCheck = OK; // keeping this here for safety
98    mCurrentSection = SECTION_TOPLEVEL;
99    mDepth = 0;
100
101    OMXClient client;
102    mInitCheck = client.connect();
103    if (mInitCheck != OK) {
104        return;
105    }
106    mOMX = client.interface();
107    parseXMLFile(codecs_xml);
108    mOMX.clear();
109
110    if (mInitCheck != OK) {
111        mCodecInfos.clear();
112        return;
113    }
114
115    for (size_t i = mCodecInfos.size(); i-- > 0;) {
116        const MediaCodecInfo &info = *mCodecInfos.itemAt(i).get();
117
118        if (info.mCaps.size() == 0) {
119            // No types supported by this component???
120            ALOGW("Component %s does not support any type of media?",
121                  info.mName.c_str());
122
123            mCodecInfos.removeAt(i);
124#if LOG_NDEBUG == 0
125        } else {
126            for (size_t type_ix = 0; type_ix < info.mCaps.size(); ++type_ix) {
127                AString mime = info.mCaps.keyAt(type_ix);
128                const sp<MediaCodecInfo::Capabilities> &caps = info.mCaps.valueAt(type_ix);
129
130                ALOGV("%s codec info for %s: %s", info.mName.c_str(), mime.c_str(),
131                        caps->getDetails()->debugString().c_str());
132                ALOGV("    flags=%d", caps->getFlags());
133                {
134                    Vector<uint32_t> colorFormats;
135                    caps->getSupportedColorFormats(&colorFormats);
136                    AString nice;
137                    for (size_t ix = 0; ix < colorFormats.size(); ix++) {
138                        if (ix > 0) {
139                            nice.append(", ");
140                        }
141                        nice.append(colorFormats.itemAt(ix));
142                    }
143                    ALOGV("    colors=[%s]", nice.c_str());
144                }
145                {
146                    Vector<MediaCodecInfo::ProfileLevel> profileLevels;
147                    caps->getSupportedProfileLevels(&profileLevels);
148                    AString nice;
149                    for (size_t ix = 0; ix < profileLevels.size(); ix++) {
150                        if (ix > 0) {
151                            nice.append(", ");
152                        }
153                        const MediaCodecInfo::ProfileLevel &pl =
154                            profileLevels.itemAt(ix);
155                        nice.append(pl.mProfile);
156                        nice.append("/");
157                        nice.append(pl.mLevel);
158                    }
159                    ALOGV("    levels=[%s]", nice.c_str());
160                }
161            }
162#endif
163        }
164    }
165
166#if 0
167    for (size_t i = 0; i < mCodecInfos.size(); ++i) {
168        const CodecInfo &info = mCodecInfos.itemAt(i);
169
170        AString line = info.mName;
171        line.append(" supports ");
172        for (size_t j = 0; j < mTypes.size(); ++j) {
173            uint32_t value = mTypes.valueAt(j);
174
175            if (info.mTypes & (1ul << value)) {
176                line.append(mTypes.keyAt(j));
177                line.append(" ");
178            }
179        }
180
181        ALOGI("%s", line.c_str());
182    }
183#endif
184}
185
186MediaCodecList::~MediaCodecList() {
187}
188
189status_t MediaCodecList::initCheck() const {
190    return mInitCheck;
191}
192
193void MediaCodecList::parseXMLFile(const char *path) {
194    FILE *file = fopen(path, "r");
195
196    if (file == NULL) {
197        ALOGW("unable to open media codecs configuration xml file: %s", path);
198        mInitCheck = NAME_NOT_FOUND;
199        return;
200    }
201
202    XML_Parser parser = ::XML_ParserCreate(NULL);
203    CHECK(parser != NULL);
204
205    ::XML_SetUserData(parser, this);
206    ::XML_SetElementHandler(
207            parser, StartElementHandlerWrapper, EndElementHandlerWrapper);
208
209    const int BUFF_SIZE = 512;
210    while (mInitCheck == OK) {
211        void *buff = ::XML_GetBuffer(parser, BUFF_SIZE);
212        if (buff == NULL) {
213            ALOGE("failed in call to XML_GetBuffer()");
214            mInitCheck = UNKNOWN_ERROR;
215            break;
216        }
217
218        int bytes_read = ::fread(buff, 1, BUFF_SIZE, file);
219        if (bytes_read < 0) {
220            ALOGE("failed in call to read");
221            mInitCheck = ERROR_IO;
222            break;
223        }
224
225        XML_Status status = ::XML_ParseBuffer(parser, bytes_read, bytes_read == 0);
226        if (status != XML_STATUS_OK) {
227            ALOGE("malformed (%s)", ::XML_ErrorString(::XML_GetErrorCode(parser)));
228            mInitCheck = ERROR_MALFORMED;
229            break;
230        }
231
232        if (bytes_read == 0) {
233            break;
234        }
235    }
236
237    ::XML_ParserFree(parser);
238
239    fclose(file);
240    file = NULL;
241}
242
243// static
244void MediaCodecList::StartElementHandlerWrapper(
245        void *me, const char *name, const char **attrs) {
246    static_cast<MediaCodecList *>(me)->startElementHandler(name, attrs);
247}
248
249// static
250void MediaCodecList::EndElementHandlerWrapper(void *me, const char *name) {
251    static_cast<MediaCodecList *>(me)->endElementHandler(name);
252}
253
254status_t MediaCodecList::includeXMLFile(const char **attrs) {
255    const char *href = NULL;
256    size_t i = 0;
257    while (attrs[i] != NULL) {
258        if (!strcmp(attrs[i], "href")) {
259            if (attrs[i + 1] == NULL) {
260                return -EINVAL;
261            }
262            href = attrs[i + 1];
263            ++i;
264        } else {
265            return -EINVAL;
266        }
267        ++i;
268    }
269
270    // For security reasons and for simplicity, file names can only contain
271    // [a-zA-Z0-9_.] and must start with  media_codecs_ and end with .xml
272    for (i = 0; href[i] != '\0'; i++) {
273        if (href[i] == '.' || href[i] == '_' ||
274                (href[i] >= '0' && href[i] <= '9') ||
275                (href[i] >= 'A' && href[i] <= 'Z') ||
276                (href[i] >= 'a' && href[i] <= 'z')) {
277            continue;
278        }
279        ALOGE("invalid include file name: %s", href);
280        return -EINVAL;
281    }
282
283    AString filename = href;
284    if (!filename.startsWith("media_codecs_") ||
285        !filename.endsWith(".xml")) {
286        ALOGE("invalid include file name: %s", href);
287        return -EINVAL;
288    }
289    filename.insert(mHrefBase, 0);
290
291    parseXMLFile(filename.c_str());
292    return mInitCheck;
293}
294
295void MediaCodecList::startElementHandler(
296        const char *name, const char **attrs) {
297    if (mInitCheck != OK) {
298        return;
299    }
300
301    bool inType = true;
302
303    if (!strcmp(name, "Include")) {
304        mInitCheck = includeXMLFile(attrs);
305        if (mInitCheck == OK) {
306            mPastSections.push(mCurrentSection);
307            mCurrentSection = SECTION_INCLUDE;
308        }
309        ++mDepth;
310        return;
311    }
312
313    switch (mCurrentSection) {
314        case SECTION_TOPLEVEL:
315        {
316            if (!strcmp(name, "Decoders")) {
317                mCurrentSection = SECTION_DECODERS;
318            } else if (!strcmp(name, "Encoders")) {
319                mCurrentSection = SECTION_ENCODERS;
320            }
321            break;
322        }
323
324        case SECTION_DECODERS:
325        {
326            if (!strcmp(name, "MediaCodec")) {
327                mInitCheck =
328                    addMediaCodecFromAttributes(false /* encoder */, attrs);
329
330                mCurrentSection = SECTION_DECODER;
331            }
332            break;
333        }
334
335        case SECTION_ENCODERS:
336        {
337            if (!strcmp(name, "MediaCodec")) {
338                mInitCheck =
339                    addMediaCodecFromAttributes(true /* encoder */, attrs);
340
341                mCurrentSection = SECTION_ENCODER;
342            }
343            break;
344        }
345
346        case SECTION_DECODER:
347        case SECTION_ENCODER:
348        {
349            if (!strcmp(name, "Quirk")) {
350                mInitCheck = addQuirk(attrs);
351            } else if (!strcmp(name, "Type")) {
352                mInitCheck = addTypeFromAttributes(attrs);
353                mCurrentSection =
354                    (mCurrentSection == SECTION_DECODER
355                            ? SECTION_DECODER_TYPE : SECTION_ENCODER_TYPE);
356            }
357        }
358        inType = false;
359        // fall through
360
361        case SECTION_DECODER_TYPE:
362        case SECTION_ENCODER_TYPE:
363        {
364            // ignore limits and features specified outside of type
365            bool outside = !inType && !mCurrentInfo->mHasSoleMime;
366            if (outside && (!strcmp(name, "Limit") || !strcmp(name, "Feature"))) {
367                ALOGW("ignoring %s specified outside of a Type", name);
368            } else if (!strcmp(name, "Limit")) {
369                mInitCheck = addLimit(attrs);
370            } else if (!strcmp(name, "Feature")) {
371                mInitCheck = addFeature(attrs);
372            }
373            break;
374        }
375
376        default:
377            break;
378    }
379
380    ++mDepth;
381}
382
383void MediaCodecList::endElementHandler(const char *name) {
384    if (mInitCheck != OK) {
385        return;
386    }
387
388    switch (mCurrentSection) {
389        case SECTION_DECODERS:
390        {
391            if (!strcmp(name, "Decoders")) {
392                mCurrentSection = SECTION_TOPLEVEL;
393            }
394            break;
395        }
396
397        case SECTION_ENCODERS:
398        {
399            if (!strcmp(name, "Encoders")) {
400                mCurrentSection = SECTION_TOPLEVEL;
401            }
402            break;
403        }
404
405        case SECTION_DECODER_TYPE:
406        case SECTION_ENCODER_TYPE:
407        {
408            if (!strcmp(name, "Type")) {
409                mCurrentSection =
410                    (mCurrentSection == SECTION_DECODER_TYPE
411                            ? SECTION_DECODER : SECTION_ENCODER);
412
413                mCurrentInfo->complete();
414            }
415            break;
416        }
417
418        case SECTION_DECODER:
419        {
420            if (!strcmp(name, "MediaCodec")) {
421                mCurrentSection = SECTION_DECODERS;
422                mCurrentInfo->complete();
423                mCurrentInfo = NULL;
424            }
425            break;
426        }
427
428        case SECTION_ENCODER:
429        {
430            if (!strcmp(name, "MediaCodec")) {
431                mCurrentSection = SECTION_ENCODERS;
432                mCurrentInfo->complete();;
433                mCurrentInfo = NULL;
434            }
435            break;
436        }
437
438        case SECTION_INCLUDE:
439        {
440            if (!strcmp(name, "Include") && mPastSections.size() > 0) {
441                mCurrentSection = mPastSections.top();
442                mPastSections.pop();
443            }
444            break;
445        }
446
447        default:
448            break;
449    }
450
451    --mDepth;
452}
453
454status_t MediaCodecList::addMediaCodecFromAttributes(
455        bool encoder, const char **attrs) {
456    const char *name = NULL;
457    const char *type = NULL;
458
459    size_t i = 0;
460    while (attrs[i] != NULL) {
461        if (!strcmp(attrs[i], "name")) {
462            if (attrs[i + 1] == NULL) {
463                return -EINVAL;
464            }
465            name = attrs[i + 1];
466            ++i;
467        } else if (!strcmp(attrs[i], "type")) {
468            if (attrs[i + 1] == NULL) {
469                return -EINVAL;
470            }
471            type = attrs[i + 1];
472            ++i;
473        } else {
474            return -EINVAL;
475        }
476
477        ++i;
478    }
479
480    if (name == NULL) {
481        return -EINVAL;
482    }
483
484    mCurrentInfo = new MediaCodecInfo(name, encoder, type);
485    mCodecInfos.push_back(mCurrentInfo);
486    return initializeCapabilities(type);
487}
488
489status_t MediaCodecList::initializeCapabilities(const char *type) {
490    ALOGV("initializeCapabilities %s:%s",
491            mCurrentInfo->mName.c_str(), type);
492
493    CodecCapabilities caps;
494    status_t err = QueryCodec(
495            mOMX,
496            mCurrentInfo->mName.c_str(),
497            type,
498            mCurrentInfo->mIsEncoder,
499            &caps);
500    if (err != OK) {
501        return err;
502    }
503
504    return mCurrentInfo->initializeCapabilities(caps);
505}
506
507status_t MediaCodecList::addQuirk(const char **attrs) {
508    const char *name = NULL;
509
510    size_t i = 0;
511    while (attrs[i] != NULL) {
512        if (!strcmp(attrs[i], "name")) {
513            if (attrs[i + 1] == NULL) {
514                return -EINVAL;
515            }
516            name = attrs[i + 1];
517            ++i;
518        } else {
519            return -EINVAL;
520        }
521
522        ++i;
523    }
524
525    if (name == NULL) {
526        return -EINVAL;
527    }
528
529    mCurrentInfo->addQuirk(name);
530    return OK;
531}
532
533status_t MediaCodecList::addTypeFromAttributes(const char **attrs) {
534    const char *name = NULL;
535
536    size_t i = 0;
537    while (attrs[i] != NULL) {
538        if (!strcmp(attrs[i], "name")) {
539            if (attrs[i + 1] == NULL) {
540                return -EINVAL;
541            }
542            name = attrs[i + 1];
543            ++i;
544        } else {
545            return -EINVAL;
546        }
547
548        ++i;
549    }
550
551    if (name == NULL) {
552        return -EINVAL;
553    }
554
555    status_t ret = mCurrentInfo->addMime(name);
556    if (ret == OK) {
557        ret = initializeCapabilities(name);
558    }
559    return ret;
560}
561
562// legacy method for non-advanced codecs
563ssize_t MediaCodecList::findCodecByType(
564        const char *type, bool encoder, size_t startIndex) const {
565    static const char *advancedFeatures[] = {
566        "feature-secure-playback",
567        "feature-tunneled-playback",
568    };
569
570    size_t numCodecs = mCodecInfos.size();
571    for (; startIndex < numCodecs; ++startIndex) {
572        const MediaCodecInfo &info = *mCodecInfos.itemAt(startIndex).get();
573
574        if (info.isEncoder() != encoder) {
575            continue;
576        }
577        sp<MediaCodecInfo::Capabilities> capabilities = info.getCapabilitiesFor(type);
578        if (capabilities == NULL) {
579            continue;
580        }
581        const sp<AMessage> &details = capabilities->getDetails();
582
583        int32_t required;
584        bool isAdvanced = false;
585        for (size_t ix = 0; ix < ARRAY_SIZE(advancedFeatures); ix++) {
586            if (details->findInt32(advancedFeatures[ix], &required) &&
587                    required != 0) {
588                isAdvanced = true;
589                break;
590            }
591        }
592
593        if (!isAdvanced) {
594            return startIndex;
595        }
596    }
597
598    return -ENOENT;
599}
600
601static status_t limitFoundMissingAttr(AString name, const char *attr, bool found = true) {
602    ALOGE("limit '%s' with %s'%s' attribute", name.c_str(),
603            (found ? "" : "no "), attr);
604    return -EINVAL;
605}
606
607static status_t limitError(AString name, const char *msg) {
608    ALOGE("limit '%s' %s", name.c_str(), msg);
609    return -EINVAL;
610}
611
612static status_t limitInvalidAttr(AString name, const char *attr, AString value) {
613    ALOGE("limit '%s' with invalid '%s' attribute (%s)", name.c_str(),
614            attr, value.c_str());
615    return -EINVAL;
616}
617
618status_t MediaCodecList::addLimit(const char **attrs) {
619    sp<AMessage> msg = new AMessage();
620
621    size_t i = 0;
622    while (attrs[i] != NULL) {
623        if (attrs[i + 1] == NULL) {
624            return -EINVAL;
625        }
626
627        // attributes with values
628        if (!strcmp(attrs[i], "name")
629                || !strcmp(attrs[i], "default")
630                || !strcmp(attrs[i], "in")
631                || !strcmp(attrs[i], "max")
632                || !strcmp(attrs[i], "min")
633                || !strcmp(attrs[i], "range")
634                || !strcmp(attrs[i], "ranges")
635                || !strcmp(attrs[i], "scale")
636                || !strcmp(attrs[i], "value")) {
637            msg->setString(attrs[i], attrs[i + 1]);
638            ++i;
639        } else {
640            return -EINVAL;
641        }
642        ++i;
643    }
644
645    AString name;
646    if (!msg->findString("name", &name)) {
647        ALOGE("limit with no 'name' attribute");
648        return -EINVAL;
649    }
650
651    // size, blocks, bitrate, frame-rate, blocks-per-second, aspect-ratio: range
652    // quality: range + default + [scale]
653    // complexity: range + default
654    bool found;
655
656    if (name == "aspect-ratio" || name == "bitrate" || name == "block-count"
657            || name == "blocks-per-second" || name == "complexity"
658            || name == "frame-rate" || name == "quality" || name == "size") {
659        AString min, max;
660        if (msg->findString("min", &min) && msg->findString("max", &max)) {
661            min.append("-");
662            min.append(max);
663            if (msg->contains("range") || msg->contains("value")) {
664                return limitError(name, "has 'min' and 'max' as well as 'range' or "
665                        "'value' attributes");
666            }
667            msg->setString("range", min);
668        } else if (msg->contains("min") || msg->contains("max")) {
669            return limitError(name, "has only 'min' or 'max' attribute");
670        } else if (msg->findString("value", &max)) {
671            min = max;
672            min.append("-");
673            min.append(max);
674            if (msg->contains("range")) {
675                return limitError(name, "has both 'range' and 'value' attributes");
676            }
677            msg->setString("range", min);
678        }
679
680        AString range, scale = "linear", def, in_;
681        if (!msg->findString("range", &range)) {
682            return limitError(name, "with no 'range', 'value' or 'min'/'max' attributes");
683        }
684
685        if ((name == "quality" || name == "complexity") ^
686                (found = msg->findString("default", &def))) {
687            return limitFoundMissingAttr(name, "default", found);
688        }
689        if (name != "quality" && msg->findString("scale", &scale)) {
690            return limitFoundMissingAttr(name, "scale");
691        }
692        if ((name == "aspect-ratio") ^ (found = msg->findString("in", &in_))) {
693            return limitFoundMissingAttr(name, "in", found);
694        }
695
696        if (name == "aspect-ratio") {
697            if (!(in_ == "pixels") && !(in_ == "blocks")) {
698                return limitInvalidAttr(name, "in", in_);
699            }
700            in_.erase(5, 1); // (pixel|block)-aspect-ratio
701            in_.append("-");
702            in_.append(name);
703            name = in_;
704        }
705        if (name == "quality") {
706            mCurrentInfo->addDetail("quality-scale", scale);
707        }
708        if (name == "quality" || name == "complexity") {
709            AString tag = name;
710            tag.append("-default");
711            mCurrentInfo->addDetail(tag, def);
712        }
713        AString tag = name;
714        tag.append("-range");
715        mCurrentInfo->addDetail(tag, range);
716    } else {
717        AString max, value, ranges;
718        if (msg->contains("default")) {
719            return limitFoundMissingAttr(name, "default");
720        } else if (msg->contains("in")) {
721            return limitFoundMissingAttr(name, "in");
722        } else if ((name == "channel-count") ^
723                (found = msg->findString("max", &max))) {
724            return limitFoundMissingAttr(name, "max", found);
725        } else if (msg->contains("min")) {
726            return limitFoundMissingAttr(name, "min");
727        } else if (msg->contains("range")) {
728            return limitFoundMissingAttr(name, "range");
729        } else if ((name == "sample-rate") ^
730                (found = msg->findString("ranges", &ranges))) {
731            return limitFoundMissingAttr(name, "ranges", found);
732        } else if (msg->contains("scale")) {
733            return limitFoundMissingAttr(name, "scale");
734        } else if ((name == "alignment" || name == "block-size") ^
735                (found = msg->findString("value", &value))) {
736            return limitFoundMissingAttr(name, "value", found);
737        }
738
739        if (max.size()) {
740            AString tag = "max-";
741            tag.append(name);
742            mCurrentInfo->addDetail(tag, max);
743        } else if (value.size()) {
744            mCurrentInfo->addDetail(name, value);
745        } else if (ranges.size()) {
746            AString tag = name;
747            tag.append("-ranges");
748            mCurrentInfo->addDetail(tag, ranges);
749        } else {
750            ALOGW("Ignoring unrecognized limit '%s'", name.c_str());
751        }
752    }
753    return OK;
754}
755
756static bool parseBoolean(const char *s) {
757    if (!strcasecmp(s, "true") || !strcasecmp(s, "yes") || !strcasecmp(s, "y")) {
758        return true;
759    }
760    char *end;
761    unsigned long res = strtoul(s, &end, 10);
762    return *s != '\0' && *end == '\0' && res > 0;
763}
764
765status_t MediaCodecList::addFeature(const char **attrs) {
766    size_t i = 0;
767    const char *name = NULL;
768    int32_t optional = -1;
769    int32_t required = -1;
770
771    while (attrs[i] != NULL) {
772        if (attrs[i + 1] == NULL) {
773            return -EINVAL;
774        }
775
776        // attributes with values
777        if (!strcmp(attrs[i], "name")) {
778            name = attrs[i + 1];
779            ++i;
780        } else if (!strcmp(attrs[i], "optional") || !strcmp(attrs[i], "required")) {
781            int value = (int)parseBoolean(attrs[i + 1]);
782            if (!strcmp(attrs[i], "optional")) {
783                optional = value;
784            } else {
785                required = value;
786            }
787            ++i;
788        } else {
789            return -EINVAL;
790        }
791        ++i;
792    }
793    if (name == NULL) {
794        ALOGE("feature with no 'name' attribute");
795        return -EINVAL;
796    }
797
798    if (optional == required && optional != -1) {
799        ALOGE("feature '%s' is both/neither optional and required", name);
800        return -EINVAL;
801    }
802
803    mCurrentInfo->addFeature(name, (required == 1) || (optional == 0));
804    return OK;
805}
806
807ssize_t MediaCodecList::findCodecByName(const char *name) const {
808    for (size_t i = 0; i < mCodecInfos.size(); ++i) {
809        const MediaCodecInfo &info = *mCodecInfos.itemAt(i).get();
810
811        if (info.mName == name) {
812            return i;
813        }
814    }
815
816    return -ENOENT;
817}
818
819size_t MediaCodecList::countCodecs() const {
820    return mCodecInfos.size();
821}
822
823}  // namespace android
824