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