AMessage.h revision 1734c7d01c1567568ced29398b23abd601fd44cb
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
26namespace android {
27
28struct ABuffer;
29struct AHandler;
30struct AString;
31class Parcel;
32
33struct AReplyToken : public RefBase {
34    AReplyToken(const sp<ALooper> &looper)
35        : mLooper(looper),
36          mReplied(false) {
37    }
38
39private:
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
61struct 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    // Performs a shallow or deep comparison of |this| and |other| and returns
131    // an AMessage with the differences.
132    // Warning: RefBase items, i.e. "objects" are _not_ copied but only have
133    // their refcount incremented.
134    // This is true for AMessages that have no corresponding AMessage equivalent in |other|.
135    // (E.g. there is no such key or the type is different.) On the other hand, changes in
136    // the AMessage (or AMessages if deep is |false|) are returned in new objects.
137    sp<AMessage> changesFrom(const sp<const AMessage> &other, bool deep = false) const;
138
139    AString debugString(int32_t indent = 0) const;
140
141    enum Type {
142        kTypeInt32,
143        kTypeInt64,
144        kTypeSize,
145        kTypeFloat,
146        kTypeDouble,
147        kTypePointer,
148        kTypeString,
149        kTypeObject,
150        kTypeMessage,
151        kTypeRect,
152        kTypeBuffer,
153    };
154
155    size_t countEntries() const;
156    const char *getEntryNameAt(size_t index, Type *type) const;
157
158protected:
159    virtual ~AMessage();
160
161private:
162    friend struct ALooper; // deliver()
163
164    uint32_t mWhat;
165
166    // used only for debugging
167    ALooper::handler_id mTarget;
168
169    wp<AHandler> mHandler;
170    wp<ALooper> mLooper;
171
172    struct Rect {
173        int32_t mLeft, mTop, mRight, mBottom;
174    };
175
176    struct Item {
177        union {
178            int32_t int32Value;
179            int64_t int64Value;
180            size_t sizeValue;
181            float floatValue;
182            double doubleValue;
183            void *ptrValue;
184            RefBase *refValue;
185            AString *stringValue;
186            Rect rectValue;
187        } u;
188        const char *mName;
189        size_t      mNameLength;
190        Type mType;
191        void setName(const char *name, size_t len);
192    };
193
194    enum {
195        kMaxNumItems = 64
196    };
197    Item mItems[kMaxNumItems];
198    size_t mNumItems;
199
200    Item *allocateItem(const char *name);
201    void freeItemValue(Item *item);
202    const Item *findItem(const char *name, Type type) const;
203
204    void setObjectInternal(
205            const char *name, const sp<RefBase> &obj, Type type);
206
207    size_t findItemIndex(const char *name, size_t len) const;
208
209    void deliver();
210
211    DISALLOW_EVIL_CONSTRUCTORS(AMessage);
212};
213
214}  // namespace android
215
216#endif  // A_MESSAGE_H_
217