Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ea15fd29 authored by Ronghua Wu's avatar Ronghua Wu
Browse files

media: changed resource type from String8 to enum.

Bug: 27338692
Change-Id: I0161de084682e2317cba81dc5f55c17d5d478570
parent 7fa3efa6
Loading
Loading
Loading
Loading
+36 −10
Original line number Diff line number Diff line
@@ -23,17 +23,24 @@

namespace android {

extern const char kResourceSecureCodec[];
extern const char kResourceNonSecureCodec[];
extern const char kResourceAudioCodec[];
extern const char kResourceVideoCodec[];
extern const char kResourceGraphicMemory[];

class MediaResource {
public:
    enum Type {
        kUnspecified = 0,
        kSecureCodec,
        kNonSecureCodec,
        kGraphicMemory
    };

    enum SubType {
        kUnspecifiedSubType = 0,
        kAudioCodec,
        kVideoCodec
    };

    MediaResource();
    MediaResource(String8 type, uint64_t value);
    MediaResource(String8 type, String8 subType, uint64_t value);
    MediaResource(Type type, uint64_t value);
    MediaResource(Type type, SubType subType, uint64_t value);

    void readFromParcel(const Parcel &parcel);
    void writeToParcel(Parcel *parcel) const;
@@ -43,11 +50,30 @@ public:
    bool operator==(const MediaResource &other) const;
    bool operator!=(const MediaResource &other) const;

    String8 mType;
    String8 mSubType;
    Type mType;
    SubType mSubType;
    uint64_t mValue;
};

inline static const char *asString(MediaResource::Type i, const char *def = "??") {
    switch (i) {
        case MediaResource::kUnspecified:    return "unspecified";
        case MediaResource::kSecureCodec:    return "secure-codec";
        case MediaResource::kNonSecureCodec: return "non-secure-codec";
        case MediaResource::kGraphicMemory:  return "graphic-memory";
        default:                             return def;
    }
}

inline static const char *asString(MediaResource::SubType i, const char *def = "??") {
    switch (i) {
        case MediaResource::kUnspecifiedSubType: return "unspecified";
        case MediaResource::kAudioCodec:         return "audio-codec";
        case MediaResource::kVideoCodec:         return "video-codec";
        default:                                 return def;
    }
}

}; // namespace android

#endif  // ANDROID_MEDIA_RESOURCE_H
+1 −1
Original line number Diff line number Diff line
@@ -394,7 +394,7 @@ private:
    bool isExecuting() const;

    uint64_t getGraphicBufferSize();
    void addResource(const String8 &type, const String8 &subtype, uint64_t value);
    void addResource(MediaResource::Type type, MediaResource::SubType subtype, uint64_t value);

    bool hasPendingBuffer(int portIndex);
    bool hasPendingBuffer();
+12 −14
Original line number Diff line number Diff line
@@ -21,38 +21,36 @@

namespace android {

const char kResourceSecureCodec[] = "secure-codec";
const char kResourceNonSecureCodec[] = "non-secure-codec";
const char kResourceAudioCodec[] = "audio-codec";
const char kResourceVideoCodec[] = "video-codec";
const char kResourceGraphicMemory[] = "graphic-memory";
MediaResource::MediaResource()
        : mType(kUnspecified),
          mSubType(kUnspecifiedSubType),
          mValue(0) {}

MediaResource::MediaResource() : mValue(0) {}

MediaResource::MediaResource(String8 type, uint64_t value)
MediaResource::MediaResource(Type type, uint64_t value)
        : mType(type),
          mSubType(kUnspecifiedSubType),
          mValue(value) {}

MediaResource::MediaResource(String8 type, String8 subType, uint64_t value)
MediaResource::MediaResource(Type type, SubType subType, uint64_t value)
        : mType(type),
          mSubType(subType),
          mValue(value) {}

void MediaResource::readFromParcel(const Parcel &parcel) {
    mType = parcel.readString8();
    mSubType = parcel.readString8();
    mType = static_cast<Type>(parcel.readInt32());
    mSubType = static_cast<SubType>(parcel.readInt32());
    mValue = parcel.readUint64();
}

void MediaResource::writeToParcel(Parcel *parcel) const {
    parcel->writeString8(mType);
    parcel->writeString8(mSubType);
    parcel->writeInt32(static_cast<int32_t>(mType));
    parcel->writeInt32(static_cast<int32_t>(mSubType));
    parcel->writeUint64(mValue);
}

String8 MediaResource::toString() const {
    String8 str;
    str.appendFormat("%s/%s:%llu", mType.string(), mSubType.string(), (unsigned long long)mValue);
    str.appendFormat("%s/%s:%llu", asString(mType), asString(mSubType), (unsigned long long)mValue);
    return str;
}

+25 −21
Original line number Diff line number Diff line
@@ -399,9 +399,11 @@ status_t MediaCodec::init(const AString &name, bool nameIsType, bool encoder) {

    status_t err;
    Vector<MediaResource> resources;
    const char *type = secureCodec ? kResourceSecureCodec : kResourceNonSecureCodec;
    const char *subtype = mIsVideo ? kResourceVideoCodec : kResourceAudioCodec;
    resources.push_back(MediaResource(String8(type), String8(subtype), 1));
    MediaResource::Type type =
            secureCodec ? MediaResource::kSecureCodec : MediaResource::kNonSecureCodec;
    MediaResource::SubType subtype =
            mIsVideo ? MediaResource::kVideoCodec : MediaResource::kAudioCodec;
    resources.push_back(MediaResource(type, subtype, 1));
    for (int i = 0; i <= kMaxRetry; ++i) {
        if (i > 0) {
            // Don't try to reclaim resource for the first time.
@@ -468,13 +470,14 @@ status_t MediaCodec::configure(

    status_t err;
    Vector<MediaResource> resources;
    const char *type = (mFlags & kFlagIsSecure) ?
            kResourceSecureCodec : kResourceNonSecureCodec;
    const char *subtype = mIsVideo ? kResourceVideoCodec : kResourceAudioCodec;
    resources.push_back(MediaResource(String8(type), String8(subtype), 1));
    MediaResource::Type type = (mFlags & kFlagIsSecure) ?
            MediaResource::kSecureCodec : MediaResource::kNonSecureCodec;
    MediaResource::SubType subtype =
            mIsVideo ? MediaResource::kVideoCodec : MediaResource::kAudioCodec;
    resources.push_back(MediaResource(type, subtype, 1));
    // Don't know the buffer size at this point, but it's fine to use 1 because
    // the reclaimResource call doesn't consider the requester's buffer size for now.
    resources.push_back(MediaResource(String8(kResourceGraphicMemory), 1));
    resources.push_back(MediaResource(MediaResource::kGraphicMemory, 1));
    for (int i = 0; i <= kMaxRetry; ++i) {
        if (i > 0) {
            // Don't try to reclaim resource for the first time.
@@ -553,7 +556,8 @@ uint64_t MediaCodec::getGraphicBufferSize() {
    return size;
}

void MediaCodec::addResource(const String8 &type, const String8 &subtype, uint64_t value) {
void MediaCodec::addResource(
        MediaResource::Type type, MediaResource::SubType subtype, uint64_t value) {
    Vector<MediaResource> resources;
    resources.push_back(MediaResource(type, subtype, value));
    mResourceManagerService->addResource(
@@ -565,13 +569,14 @@ status_t MediaCodec::start() {

    status_t err;
    Vector<MediaResource> resources;
    const char *type = (mFlags & kFlagIsSecure) ?
            kResourceSecureCodec : kResourceNonSecureCodec;
    const char *subtype = mIsVideo ? kResourceVideoCodec : kResourceAudioCodec;
    resources.push_back(MediaResource(String8(type), String8(subtype), 1));
    MediaResource::Type type = (mFlags & kFlagIsSecure) ?
            MediaResource::kSecureCodec : MediaResource::kNonSecureCodec;
    MediaResource::SubType subtype =
            mIsVideo ? MediaResource::kVideoCodec : MediaResource::kAudioCodec;
    resources.push_back(MediaResource(type, subtype, 1));
    // Don't know the buffer size at this point, but it's fine to use 1 because
    // the reclaimResource call doesn't consider the requester's buffer size for now.
    resources.push_back(MediaResource(String8(kResourceGraphicMemory), 1));
    resources.push_back(MediaResource(MediaResource::kGraphicMemory, 1));
    for (int i = 0; i <= kMaxRetry; ++i) {
        if (i > 0) {
            // Don't try to reclaim resource for the first time.
@@ -1228,18 +1233,18 @@ void MediaCodec::onMessageReceived(const sp<AMessage> &msg) {
                        mFlags &= ~kFlagUsesSoftwareRenderer;
                    }

                    String8 resourceType;
                    MediaResource::Type resourceType;
                    if (mComponentName.endsWith(".secure")) {
                        mFlags |= kFlagIsSecure;
                        resourceType = String8(kResourceSecureCodec);
                        resourceType = MediaResource::kSecureCodec;
                    } else {
                        mFlags &= ~kFlagIsSecure;
                        resourceType = String8(kResourceNonSecureCodec);
                        resourceType = MediaResource::kNonSecureCodec;
                    }

                    if (mIsVideo) {
                        // audio codec is currently ignored.
                        addResource(resourceType, String8(kResourceVideoCodec), 1);
                        addResource(resourceType, MediaResource::kVideoCodec, 1);
                    }

                    (new AMessage)->postReply(mReplyID);
@@ -1376,10 +1381,9 @@ void MediaCodec::onMessageReceived(const sp<AMessage> &msg) {
                            // allocating input buffers, so this is a good
                            // indication that now all buffers are allocated.
                            if (mIsVideo) {
                                String8 subtype;
                                addResource(
                                        String8(kResourceGraphicMemory),
                                        subtype,
                                        MediaResource::kGraphicMemory,
                                        MediaResource::kUnspecifiedSubType,
                                        getGraphicBufferSize());
                            }
                            setState(STARTED);
+20 −20
Original line number Diff line number Diff line
@@ -43,7 +43,7 @@ static String8 getString(const Vector<T> &items) {
    return itemsStr;
}

static bool hasResourceType(String8 type, Vector<MediaResource> resources) {
static bool hasResourceType(MediaResource::Type type, Vector<MediaResource> resources) {
    for (size_t i = 0; i < resources.size(); ++i) {
        if (resources[i].mType == type) {
            return true;
@@ -52,7 +52,7 @@ static bool hasResourceType(String8 type, Vector<MediaResource> resources) {
    return false;
}

static bool hasResourceType(String8 type, ResourceInfos infos) {
static bool hasResourceType(MediaResource::Type type, ResourceInfos infos) {
    for (size_t i = 0; i < infos.size(); ++i) {
        if (hasResourceType(type, infos[i].resources)) {
            return true;
@@ -96,8 +96,8 @@ static void notifyResourceGranted(int pid, const Vector<MediaResource> &resource
    if (binder != NULL) {
        sp<IMediaResourceMonitor> service = interface_cast<IMediaResourceMonitor>(binder);
        for (size_t i = 0; i < resources.size(); ++i) {
            service->notifyResourceGranted(pid, String16(resources[i].mType),
                    String16(resources[i].mSubType), resources[i].mValue);
            service->notifyResourceGranted(pid, String16(asString(resources[i].mType)),
                    String16(asString(resources[i].mSubType)), resources[i].mValue);
        }
    }
}
@@ -275,12 +275,12 @@ bool ResourceManagerService::reclaimResource(
        const MediaResource *nonSecureCodec = NULL;
        const MediaResource *graphicMemory = NULL;
        for (size_t i = 0; i < resources.size(); ++i) {
            String8 type = resources[i].mType;
            if (resources[i].mType == kResourceSecureCodec) {
            MediaResource::Type type = resources[i].mType;
            if (resources[i].mType == MediaResource::kSecureCodec) {
                secureCodec = &resources[i];
            } else if (type == kResourceNonSecureCodec) {
            } else if (type == MediaResource::kNonSecureCodec) {
                nonSecureCodec = &resources[i];
            } else if (type == kResourceGraphicMemory) {
            } else if (type == MediaResource::kGraphicMemory) {
                graphicMemory = &resources[i];
            }
        }
@@ -288,19 +288,19 @@ bool ResourceManagerService::reclaimResource(
        // first pass to handle secure/non-secure codec conflict
        if (secureCodec != NULL) {
            if (!mSupportsMultipleSecureCodecs) {
                if (!getAllClients_l(callingPid, String8(kResourceSecureCodec), &clients)) {
                if (!getAllClients_l(callingPid, MediaResource::kSecureCodec, &clients)) {
                    return false;
                }
            }
            if (!mSupportsSecureWithNonSecureCodec) {
                if (!getAllClients_l(callingPid, String8(kResourceNonSecureCodec), &clients)) {
                if (!getAllClients_l(callingPid, MediaResource::kNonSecureCodec, &clients)) {
                    return false;
                }
            }
        }
        if (nonSecureCodec != NULL) {
            if (!mSupportsSecureWithNonSecureCodec) {
                if (!getAllClients_l(callingPid, String8(kResourceSecureCodec), &clients)) {
                if (!getAllClients_l(callingPid, MediaResource::kSecureCodec, &clients)) {
                    return false;
                }
            }
@@ -320,11 +320,11 @@ bool ResourceManagerService::reclaimResource(
        if (clients.size() == 0) {
            // if we are here, run the fourth pass to free one codec with the different type.
            if (secureCodec != NULL) {
                MediaResource temp(String8(kResourceNonSecureCodec), 1);
                MediaResource temp(MediaResource::kNonSecureCodec, 1);
                getClientForResource_l(callingPid, &temp, &clients);
            }
            if (nonSecureCodec != NULL) {
                MediaResource temp(String8(kResourceSecureCodec), 1);
                MediaResource temp(MediaResource::kSecureCodec, 1);
                getClientForResource_l(callingPid, &temp, &clients);
            }
        }
@@ -374,7 +374,7 @@ bool ResourceManagerService::reclaimResource(
}

bool ResourceManagerService::getAllClients_l(
        int callingPid, const String8 &type, Vector<sp<IResourceManagerClient>> *clients) {
        int callingPid, MediaResource::Type type, Vector<sp<IResourceManagerClient>> *clients) {
    Vector<sp<IResourceManagerClient>> temp;
    for (size_t i = 0; i < mMap.size(); ++i) {
        ResourceInfos &infos = mMap.editValueAt(i);
@@ -384,7 +384,7 @@ bool ResourceManagerService::getAllClients_l(
                    // some higher/equal priority process owns the resource,
                    // this request can't be fulfilled.
                    ALOGE("getAllClients_l: can't reclaim resource %s from pid %d",
                            type.string(), mMap.keyAt(i));
                            asString(type), mMap.keyAt(i));
                    return false;
                }
                temp.push_back(infos[j].client);
@@ -392,7 +392,7 @@ bool ResourceManagerService::getAllClients_l(
        }
    }
    if (temp.size() == 0) {
        ALOGV("getAllClients_l: didn't find any resource %s", type.string());
        ALOGV("getAllClients_l: didn't find any resource %s", asString(type));
        return true;
    }
    clients->appendVector(temp);
@@ -400,7 +400,7 @@ bool ResourceManagerService::getAllClients_l(
}

bool ResourceManagerService::getLowestPriorityBiggestClient_l(
        int callingPid, const String8 &type, sp<IResourceManagerClient> *client) {
        int callingPid, MediaResource::Type type, sp<IResourceManagerClient> *client) {
    int lowestPriorityPid;
    int lowestPriority;
    int callingPriority;
@@ -425,7 +425,7 @@ bool ResourceManagerService::getLowestPriorityBiggestClient_l(
}

bool ResourceManagerService::getLowestPriorityPid_l(
        const String8 &type, int *lowestPriorityPid, int *lowestPriority) {
        MediaResource::Type type, int *lowestPriorityPid, int *lowestPriority) {
    int pid = -1;
    int priority = -1;
    for (size_t i = 0; i < mMap.size(); ++i) {
@@ -472,7 +472,7 @@ bool ResourceManagerService::isCallingPriorityHigher_l(int callingPid, int pid)
}

bool ResourceManagerService::getBiggestClient_l(
        int pid, const String8 &type, sp<IResourceManagerClient> *client) {
        int pid, MediaResource::Type type, sp<IResourceManagerClient> *client) {
    ssize_t index = mMap.indexOfKey(pid);
    if (index < 0) {
        ALOGE("getBiggestClient_l: can't find resource info for pid %d", pid);
@@ -495,7 +495,7 @@ bool ResourceManagerService::getBiggestClient_l(
    }

    if (clientTemp == NULL) {
        ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", type.string(), pid);
        ALOGE("getBiggestClient_l: can't find resource type %s for pid %d", asString(type), pid);
        return false;
    }

Loading