• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     explicit 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     // 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 signed integer types cast to int64_t
127     bool findAsInt64(const char *name, int64_t *value) const;
128 
129     // finds any numeric type cast to a float
130     bool findAsFloat(const char *name, float *value) const;
131 
132     bool findRect(
133             const char *name,
134             int32_t *left, int32_t *top, int32_t *right, int32_t *bottom) const;
135 
136     status_t post(int64_t delayUs = 0);
137 
138     // Posts the message to its target and waits for a response (or error)
139     // before returning.
140     status_t postAndAwaitResponse(sp<AMessage> *response);
141 
142     // If this returns true, the sender of this message is synchronously
143     // awaiting a response and the reply token is consumed from the message
144     // and stored into replyID. The reply token must be used to send the response
145     // using "postReply" below.
146     bool senderAwaitsResponse(sp<AReplyToken> *replyID);
147 
148     // Posts the message as a response to a reply token.  A reply token can
149     // only be used once. Returns OK if the response could be posted; otherwise,
150     // an error.
151     status_t postReply(const sp<AReplyToken> &replyID);
152 
153     // Performs a deep-copy of "this", contained messages are in turn "dup'ed".
154     // Warning: RefBase items, i.e. "objects" are _not_ copied but only have
155     // their refcount incremented.
156     sp<AMessage> dup() const;
157 
158     // Performs a shallow or deep comparison of |this| and |other| and returns
159     // an AMessage with the differences.
160     // Warning: RefBase items, i.e. "objects" are _not_ copied but only have
161     // their refcount incremented.
162     // This is true for AMessages that have no corresponding AMessage equivalent in |other|.
163     // (E.g. there is no such key or the type is different.) On the other hand, changes in
164     // the AMessage (or AMessages if deep is |false|) are returned in new objects.
165     sp<AMessage> changesFrom(const sp<const AMessage> &other, bool deep = false) const;
166 
167     AString debugString(int32_t indent = 0) const;
168 
169     enum Type {
170         kTypeInt32,
171         kTypeInt64,
172         kTypeSize,
173         kTypeFloat,
174         kTypeDouble,
175         kTypePointer,
176         kTypeString,
177         kTypeObject,
178         kTypeMessage,
179         kTypeRect,
180         kTypeBuffer,
181     };
182 
183     size_t countEntries() const;
184     const char *getEntryNameAt(size_t index, Type *type) const;
185 
186 protected:
187     virtual ~AMessage();
188 
189 private:
190     friend struct ALooper; // deliver()
191 
192     uint32_t mWhat;
193 
194     // used only for debugging
195     ALooper::handler_id mTarget;
196 
197     wp<AHandler> mHandler;
198     wp<ALooper> mLooper;
199 
200     struct Rect {
201         int32_t mLeft, mTop, mRight, mBottom;
202     };
203 
204     struct Item {
205         union {
206             int32_t int32Value;
207             int64_t int64Value;
208             size_t sizeValue;
209             float floatValue;
210             double doubleValue;
211             void *ptrValue;
212             RefBase *refValue;
213             AString *stringValue;
214             Rect rectValue;
215         } u;
216         const char *mName;
217         size_t      mNameLength;
218         Type mType;
219         void setName(const char *name, size_t len);
220     };
221 
222     enum {
223         kMaxNumItems = 64
224     };
225     Item mItems[kMaxNumItems];
226     size_t mNumItems;
227 
228     Item *allocateItem(const char *name);
229     void freeItemValue(Item *item);
230     const Item *findItem(const char *name, Type type) const;
231 
232     void setObjectInternal(
233             const char *name, const sp<RefBase> &obj, Type type);
234 
235     size_t findItemIndex(const char *name, size_t len) const;
236 
237     void deliver();
238 
239     DISALLOW_EVIL_CONSTRUCTORS(AMessage);
240 };
241 
242 }  // namespace android
243 
244 #endif  // A_MESSAGE_H_
245