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 AHandler; 30 struct AString; 31 class Parcel; 32 33 struct AReplyToken : public RefBase { 34 AReplyToken(const sp<ALooper> &looper) 35 : mLooper(looper), 36 mReplied(false) { 37 } 38 39 private: 40 friend struct AMessage; 41 friend struct ALooper; 42 wp<ALooper> mLooper; 43 sp<AMessage> mReply; 44 bool mReplied; 45 46 sp<ALooper> getLooper() const { 47 return mLooper.promote(); 48 } 49 // if reply is not set, returns false; otherwise, it retrieves the reply and returns true 50 bool retrieveReply(sp<AMessage> *reply) { 51 if (mReplied) { 52 *reply = mReply; 53 mReply.clear(); 54 } 55 return mReplied; 56 } 57 // sets the reply for this token. returns OK or error 58 status_t setReply(const sp<AMessage> &reply); 59 }; 60 61 struct AMessage : public RefBase { 62 AMessage(); 63 AMessage(uint32_t what, const sp<const AHandler> &handler); 64 65 // Construct an AMessage from a parcel. 66 // nestingAllowed determines how many levels AMessage can be nested inside 67 // AMessage. The default value here is arbitrarily set to 255. 68 // FromParcel() returns NULL on error, which occurs when the input parcel 69 // contains 70 // - an AMessage nested deeper than maxNestingLevel; or 71 // - an item whose type is not recognized by this function. 72 // Types currently recognized by this function are: 73 // Item types set/find function suffixes 74 // ========================================== 75 // int32_t Int32 76 // int64_t Int64 77 // size_t Size 78 // float Float 79 // double Double 80 // AString String 81 // AMessage Message 82 static sp<AMessage> FromParcel(const Parcel &parcel, 83 size_t maxNestingLevel = 255); 84 85 // Write this AMessage to a parcel. 86 // All items in the AMessage must have types that are recognized by 87 // FromParcel(); otherwise, TRESPASS error will occur. 88 void writeToParcel(Parcel *parcel) const; 89 90 void setWhat(uint32_t what); 91 uint32_t what() const; 92 93 void setTarget(const sp<const AHandler> &handler); 94 95 void clear(); 96 97 void setInt32(const char *name, int32_t value); 98 void setInt64(const char *name, int64_t value); 99 void setSize(const char *name, size_t value); 100 void setFloat(const char *name, float value); 101 void setDouble(const char *name, double value); 102 void setPointer(const char *name, void *value); 103 void setString(const char *name, const char *s, ssize_t len = -1); 104 void setString(const char *name, const AString &s); 105 void setObject(const char *name, const sp<RefBase> &obj); 106 void setBuffer(const char *name, const sp<ABuffer> &buffer); 107 void setMessage(const char *name, const sp<AMessage> &obj); 108 109 void setRect( 110 const char *name, 111 int32_t left, int32_t top, int32_t right, int32_t bottom); 112 113 bool contains(const char *name) const; 114 115 bool findInt32(const char *name, int32_t *value) const; 116 bool findInt64(const char *name, int64_t *value) const; 117 bool findSize(const char *name, size_t *value) const; 118 bool findFloat(const char *name, float *value) const; 119 bool findDouble(const char *name, double *value) const; 120 bool findPointer(const char *name, void **value) const; 121 bool findString(const char *name, AString *value) const; 122 bool findObject(const char *name, sp<RefBase> *obj) const; 123 bool findBuffer(const char *name, sp<ABuffer> *buffer) const; 124 bool findMessage(const char *name, sp<AMessage> *obj) const; 125 126 // finds any numeric type cast to a float 127 bool findAsFloat(const char *name, float *value) const; 128 129 bool findRect( 130 const char *name, 131 int32_t *left, int32_t *top, int32_t *right, int32_t *bottom) const; 132 133 status_t post(int64_t delayUs = 0); 134 135 // Posts the message to its target and waits for a response (or error) 136 // before returning. 137 status_t postAndAwaitResponse(sp<AMessage> *response); 138 139 // If this returns true, the sender of this message is synchronously 140 // awaiting a response and the reply token is consumed from the message 141 // and stored into replyID. The reply token must be used to send the response 142 // using "postReply" below. 143 bool senderAwaitsResponse(sp<AReplyToken> *replyID); 144 145 // Posts the message as a response to a reply token. A reply token can 146 // only be used once. Returns OK if the response could be posted; otherwise, 147 // an error. 148 status_t postReply(const sp<AReplyToken> &replyID); 149 150 // Performs a deep-copy of "this", contained messages are in turn "dup'ed". 151 // Warning: RefBase items, i.e. "objects" are _not_ copied but only have 152 // their refcount incremented. 153 sp<AMessage> dup() const; 154 155 // Performs a shallow or deep comparison of |this| and |other| and returns 156 // an AMessage with the differences. 157 // Warning: RefBase items, i.e. "objects" are _not_ copied but only have 158 // their refcount incremented. 159 // This is true for AMessages that have no corresponding AMessage equivalent in |other|. 160 // (E.g. there is no such key or the type is different.) On the other hand, changes in 161 // the AMessage (or AMessages if deep is |false|) are returned in new objects. 162 sp<AMessage> changesFrom(const sp<const AMessage> &other, bool deep = false) const; 163 164 AString debugString(int32_t indent = 0) const; 165 166 enum Type { 167 kTypeInt32, 168 kTypeInt64, 169 kTypeSize, 170 kTypeFloat, 171 kTypeDouble, 172 kTypePointer, 173 kTypeString, 174 kTypeObject, 175 kTypeMessage, 176 kTypeRect, 177 kTypeBuffer, 178 }; 179 180 size_t countEntries() const; 181 const char *getEntryNameAt(size_t index, Type *type) const; 182 183 protected: 184 virtual ~AMessage(); 185 186 private: 187 friend struct ALooper; // deliver() 188 189 uint32_t mWhat; 190 191 // used only for debugging 192 ALooper::handler_id mTarget; 193 194 wp<AHandler> mHandler; 195 wp<ALooper> mLooper; 196 197 struct Rect { 198 int32_t mLeft, mTop, mRight, mBottom; 199 }; 200 201 struct Item { 202 union { 203 int32_t int32Value; 204 int64_t int64Value; 205 size_t sizeValue; 206 float floatValue; 207 double doubleValue; 208 void *ptrValue; 209 RefBase *refValue; 210 AString *stringValue; 211 Rect rectValue; 212 } u; 213 const char *mName; 214 size_t mNameLength; 215 Type mType; 216 void setName(const char *name, size_t len); 217 }; 218 219 enum { 220 kMaxNumItems = 64 221 }; 222 Item mItems[kMaxNumItems]; 223 size_t mNumItems; 224 225 Item *allocateItem(const char *name); 226 void freeItemValue(Item *item); 227 const Item *findItem(const char *name, Type type) const; 228 229 void setObjectInternal( 230 const char *name, const sp<RefBase> &obj, Type type); 231 232 size_t findItemIndex(const char *name, size_t len) const; 233 234 void deliver(); 235 236 DISALLOW_EVIL_CONSTRUCTORS(AMessage); 237 }; 238 239 } // namespace android 240 241 #endif // A_MESSAGE_H_ 242