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 { AReplyTokenAReplyToken34 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 getLooperAReplyToken46 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 retrieveReplyAReplyToken50 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 static sp<AMessage> FromParcel(const Parcel &parcel); 66 void writeToParcel(Parcel *parcel) const; 67 68 void setWhat(uint32_t what); 69 uint32_t what() const; 70 71 void setTarget(const sp<const AHandler> &handler); 72 73 void clear(); 74 75 void setInt32(const char *name, int32_t value); 76 void setInt64(const char *name, int64_t value); 77 void setSize(const char *name, size_t value); 78 void setFloat(const char *name, float value); 79 void setDouble(const char *name, double value); 80 void setPointer(const char *name, void *value); 81 void setString(const char *name, const char *s, ssize_t len = -1); 82 void setString(const char *name, const AString &s); 83 void setObject(const char *name, const sp<RefBase> &obj); 84 void setBuffer(const char *name, const sp<ABuffer> &buffer); 85 void setMessage(const char *name, const sp<AMessage> &obj); 86 87 void setRect( 88 const char *name, 89 int32_t left, int32_t top, int32_t right, int32_t bottom); 90 91 bool contains(const char *name) const; 92 93 bool findInt32(const char *name, int32_t *value) const; 94 bool findInt64(const char *name, int64_t *value) const; 95 bool findSize(const char *name, size_t *value) const; 96 bool findFloat(const char *name, float *value) const; 97 bool findDouble(const char *name, double *value) const; 98 bool findPointer(const char *name, void **value) const; 99 bool findString(const char *name, AString *value) const; 100 bool findObject(const char *name, sp<RefBase> *obj) const; 101 bool findBuffer(const char *name, sp<ABuffer> *buffer) const; 102 bool findMessage(const char *name, sp<AMessage> *obj) const; 103 104 bool findRect( 105 const char *name, 106 int32_t *left, int32_t *top, int32_t *right, int32_t *bottom) const; 107 108 status_t post(int64_t delayUs = 0); 109 110 // Posts the message to its target and waits for a response (or error) 111 // before returning. 112 status_t postAndAwaitResponse(sp<AMessage> *response); 113 114 // If this returns true, the sender of this message is synchronously 115 // awaiting a response and the reply token is consumed from the message 116 // and stored into replyID. The reply token must be used to send the response 117 // using "postReply" below. 118 bool senderAwaitsResponse(sp<AReplyToken> *replyID); 119 120 // Posts the message as a response to a reply token. A reply token can 121 // only be used once. Returns OK if the response could be posted; otherwise, 122 // an error. 123 status_t postReply(const sp<AReplyToken> &replyID); 124 125 // Performs a deep-copy of "this", contained messages are in turn "dup'ed". 126 // Warning: RefBase items, i.e. "objects" are _not_ copied but only have 127 // their refcount incremented. 128 sp<AMessage> dup() const; 129 130 AString debugString(int32_t indent = 0) const; 131 132 enum Type { 133 kTypeInt32, 134 kTypeInt64, 135 kTypeSize, 136 kTypeFloat, 137 kTypeDouble, 138 kTypePointer, 139 kTypeString, 140 kTypeObject, 141 kTypeMessage, 142 kTypeRect, 143 kTypeBuffer, 144 }; 145 146 size_t countEntries() const; 147 const char *getEntryNameAt(size_t index, Type *type) const; 148 149 protected: 150 virtual ~AMessage(); 151 152 private: 153 friend struct ALooper; // deliver() 154 155 uint32_t mWhat; 156 157 // used only for debugging 158 ALooper::handler_id mTarget; 159 160 wp<AHandler> mHandler; 161 wp<ALooper> mLooper; 162 163 struct Rect { 164 int32_t mLeft, mTop, mRight, mBottom; 165 }; 166 167 struct Item { 168 union { 169 int32_t int32Value; 170 int64_t int64Value; 171 size_t sizeValue; 172 float floatValue; 173 double doubleValue; 174 void *ptrValue; 175 RefBase *refValue; 176 AString *stringValue; 177 Rect rectValue; 178 } u; 179 const char *mName; 180 size_t mNameLength; 181 Type mType; 182 void setName(const char *name, size_t len); 183 }; 184 185 enum { 186 kMaxNumItems = 64 187 }; 188 Item mItems[kMaxNumItems]; 189 size_t mNumItems; 190 191 Item *allocateItem(const char *name); 192 void freeItemValue(Item *item); 193 const Item *findItem(const char *name, Type type) const; 194 195 void setObjectInternal( 196 const char *name, const sp<RefBase> &obj, Type type); 197 198 size_t findItemIndex(const char *name, size_t len) const; 199 200 void deliver(); 201 202 DISALLOW_EVIL_CONSTRUCTORS(AMessage); 203 }; 204 205 } // namespace android 206 207 #endif // A_MESSAGE_H_ 208