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_LOOPER_H_
18 
19 #define A_LOOPER_H_
20 
21 #include <media/stagefright/foundation/ABase.h>
22 #include <media/stagefright/foundation/AString.h>
23 #include <utils/Errors.h>
24 #include <utils/KeyedVector.h>
25 #include <utils/List.h>
26 #include <utils/RefBase.h>
27 #include <utils/threads.h>
28 
29 namespace android {
30 
31 struct AHandler;
32 struct AMessage;
33 struct AReplyToken;
34 
35 struct ALooper : public RefBase {
36     typedef int32_t event_id;
37     typedef int32_t handler_id;
38 
39     ALooper();
40 
41     // Takes effect in a subsequent call to start().
42     void setName(const char *name);
43 
44     handler_id registerHandler(const sp<AHandler> &handler);
45     void unregisterHandler(handler_id handlerID);
46 
47     status_t start(
48             bool runOnCallingThread = false,
49             bool canCallJava = false,
50             int32_t priority = PRIORITY_DEFAULT
51             );
52 
53     status_t stop();
54 
55     static int64_t GetNowUs();
56 
getNameALooper57     const char *getName() const {
58         return mName.c_str();
59     }
60 
61 protected:
62     virtual ~ALooper();
63 
64 private:
65     friend struct AMessage;       // post()
66 
67     struct Event {
68         int64_t mWhenUs;
69         sp<AMessage> mMessage;
70     };
71 
72     Mutex mLock;
73     Condition mQueueChangedCondition;
74 
75     AString mName;
76 
77     List<Event> mEventQueue;
78 
79     struct LooperThread;
80     sp<LooperThread> mThread;
81     bool mRunningLocally;
82 
83     // use a separate lock for reply handling, as it is always on another thread
84     // use a central lock, however, to avoid creating a mutex for each reply
85     Mutex mRepliesLock;
86     Condition mRepliesCondition;
87 
88     // START --- methods used only by AMessage
89 
90     // posts a message on this looper with the given timeout
91     void post(const sp<AMessage> &msg, int64_t delayUs);
92 
93     // creates a reply token to be used with this looper
94     sp<AReplyToken> createReplyToken();
95     // waits for a response for the reply token.  If status is OK, the response
96     // is stored into the supplied variable.  Otherwise, it is unchanged.
97     status_t awaitResponse(const sp<AReplyToken> &replyToken, sp<AMessage> *response);
98     // posts a reply for a reply token.  If the reply could be successfully posted,
99     // it returns OK. Otherwise, it returns an error value.
100     status_t postReply(const sp<AReplyToken> &replyToken, const sp<AMessage> &msg);
101 
102     // END --- methods used only by AMessage
103 
104     bool loop();
105 
106     DISALLOW_EVIL_CONSTRUCTORS(ALooper);
107 };
108 
109 } // namespace android
110 
111 #endif  // A_LOOPER_H_
112