Home | History | Annotate | Download | only in foundation
      1 /*
      2  * Copyright (C) 2010 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 #ifndef A_MESSAGE_H_
     18 
     19 #define A_MESSAGE_H_
     20 
     21 #include <media/stagefright/foundation/ABase.h>
     22 #include <media/stagefright/foundation/ALooper.h>
     23 #include <utils/KeyedVector.h>
     24 #include <utils/RefBase.h>
     25 
     26 namespace android {
     27 
     28 struct ABuffer;
     29 struct AString;
     30 struct Parcel;
     31 
     32 struct AMessage : public RefBase {
     33     AMessage(uint32_t what = 0, ALooper::handler_id target = 0);
     34 
     35     static sp<AMessage> FromParcel(const Parcel &parcel);
     36     void writeToParcel(Parcel *parcel) const;
     37 
     38     void setWhat(uint32_t what);
     39     uint32_t what() const;
     40 
     41     void setTarget(ALooper::handler_id target);
     42     ALooper::handler_id target() const;
     43 
     44     void clear();
     45 
     46     void setInt32(const char *name, int32_t value);
     47     void setInt64(const char *name, int64_t value);
     48     void setSize(const char *name, size_t value);
     49     void setFloat(const char *name, float value);
     50     void setDouble(const char *name, double value);
     51     void setPointer(const char *name, void *value);
     52     void setString(const char *name, const char *s, ssize_t len = -1);
     53     void setObject(const char *name, const sp<RefBase> &obj);
     54     void setBuffer(const char *name, const sp<ABuffer> &buffer);
     55     void setMessage(const char *name, const sp<AMessage> &obj);
     56 
     57     void setRect(
     58             const char *name,
     59             int32_t left, int32_t top, int32_t right, int32_t bottom);
     60 
     61     bool findInt32(const char *name, int32_t *value) const;
     62     bool findInt64(const char *name, int64_t *value) const;
     63     bool findSize(const char *name, size_t *value) const;
     64     bool findFloat(const char *name, float *value) const;
     65     bool findDouble(const char *name, double *value) const;
     66     bool findPointer(const char *name, void **value) const;
     67     bool findString(const char *name, AString *value) const;
     68     bool findObject(const char *name, sp<RefBase> *obj) const;
     69     bool findBuffer(const char *name, sp<ABuffer> *buffer) const;
     70     bool findMessage(const char *name, sp<AMessage> *obj) const;
     71 
     72     bool findRect(
     73             const char *name,
     74             int32_t *left, int32_t *top, int32_t *right, int32_t *bottom) const;
     75 
     76     void post(int64_t delayUs = 0);
     77 
     78     // Posts the message to its target and waits for a response (or error)
     79     // before returning.
     80     status_t postAndAwaitResponse(sp<AMessage> *response);
     81 
     82     // If this returns true, the sender of this message is synchronously
     83     // awaiting a response, the "replyID" can be used to send the response
     84     // via "postReply" below.
     85     bool senderAwaitsResponse(uint32_t *replyID) const;
     86 
     87     void postReply(uint32_t replyID);
     88 
     89     // Performs a deep-copy of "this", contained messages are in turn "dup'ed".
     90     // Warning: RefBase items, i.e. "objects" are _not_ copied but only have
     91     // their refcount incremented.
     92     sp<AMessage> dup() const;
     93 
     94     AString debugString(int32_t indent = 0) const;
     95 
     96     enum Type {
     97         kTypeInt32,
     98         kTypeInt64,
     99         kTypeSize,
    100         kTypeFloat,
    101         kTypeDouble,
    102         kTypePointer,
    103         kTypeString,
    104         kTypeObject,
    105         kTypeMessage,
    106         kTypeRect,
    107         kTypeBuffer,
    108     };
    109 
    110     size_t countEntries() const;
    111     const char *getEntryNameAt(size_t index, Type *type) const;
    112 
    113 protected:
    114     virtual ~AMessage();
    115 
    116 private:
    117     uint32_t mWhat;
    118     ALooper::handler_id mTarget;
    119 
    120     struct Rect {
    121         int32_t mLeft, mTop, mRight, mBottom;
    122     };
    123 
    124     struct Item {
    125         union {
    126             int32_t int32Value;
    127             int64_t int64Value;
    128             size_t sizeValue;
    129             float floatValue;
    130             double doubleValue;
    131             void *ptrValue;
    132             RefBase *refValue;
    133             AString *stringValue;
    134             Rect rectValue;
    135         } u;
    136         const char *mName;
    137         Type mType;
    138     };
    139 
    140     enum {
    141         kMaxNumItems = 64
    142     };
    143     Item mItems[kMaxNumItems];
    144     size_t mNumItems;
    145 
    146     Item *allocateItem(const char *name);
    147     void freeItem(Item *item);
    148     const Item *findItem(const char *name, Type type) const;
    149 
    150     void setObjectInternal(
    151             const char *name, const sp<RefBase> &obj, Type type);
    152 
    153     DISALLOW_EVIL_CONSTRUCTORS(AMessage);
    154 };
    155 
    156 }  // namespace android
    157 
    158 #endif  // A_MESSAGE_H_
    159