OMXClient.h revision 408b8e1073385d0d09bb96b9952f84731a0b4aeb
1/*
2 * Copyright (C) 2009 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 OMX_CLIENT_H_
18
19#define OMX_CLIENT_H_
20
21#include <media/IOMX.h>
22
23#include <utils/KeyedVector.h>
24#include <utils/List.h>
25#include <utils/threads.h>
26
27namespace android {
28
29class OMXObserver {
30public:
31    OMXObserver();
32    virtual ~OMXObserver();
33
34    void postMessage(const omx_message &msg);
35
36protected:
37    virtual void onOMXMessage(const omx_message &msg) = 0;
38
39private:
40    friend class OMXClient;
41
42    pthread_t mThread;
43    Mutex mLock;
44    Condition mQueueNotEmpty;
45    List<omx_message> mQueue;
46
47    void start();
48    void stop();
49
50    static void *ThreadWrapper(void *me);
51    void threadEntry();
52
53    OMXObserver(const OMXObserver &);
54    OMXObserver &operator=(const OMXObserver &);
55};
56
57class OMXClient;
58
59class OMXClientReflector : public BnOMXObserver {
60public:
61    OMXClientReflector(OMXClient *client);
62
63    virtual void on_message(const omx_message &msg);
64    void reset();
65
66private:
67    OMXClient *mClient;
68
69    OMXClientReflector(const OMXClientReflector &);
70    OMXClientReflector &operator=(const OMXClientReflector &);
71};
72
73class OMXClient {
74public:
75    friend class OMXClientReflector;
76
77    OMXClient();
78    ~OMXClient();
79
80    status_t connect();
81    void disconnect();
82
83    sp<IOMX> interface() {
84        return mOMX;
85    }
86
87    status_t registerObserver(IOMX::node_id node, OMXObserver *observer);
88    void unregisterObserver(IOMX::node_id node);
89
90private:
91    sp<IOMX> mOMX;
92    Mutex mLock;
93
94    KeyedVector<IOMX::node_id, OMXObserver *> mObservers;
95
96    sp<OMXClientReflector> mReflector;
97
98    bool onOMXMessage(const omx_message &msg);
99
100    OMXClient(const OMXClient &);
101    OMXClient &operator=(const OMXClient &);
102};
103
104}  // namespace android
105
106#endif  // OMX_CLIENT_H_
107