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