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