1 /*
2  * Copyright (C) 2019 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 SIMPLE_GOLDFISH_OMX_COMPONENT_H_
18 
19 #define SIMPLE_GOLDFISH_OMX_COMPONENT_H_
20 
21 #include "GoldfishOMXComponent.h"
22 
23 #include <atomic>
24 #include <media/stagefright/foundation/AHandlerReflector.h>
25 #include <utils/RefBase.h>
26 #include <utils/threads.h>
27 #include <utils/Vector.h>
28 
29 namespace android {
30 
31 struct ALooper;
32 struct ABuffer;
33 
34 struct CodecProfileLevel {
35     OMX_U32 mProfile;
36     OMX_U32 mLevel;
37 };
38 
39 struct SimpleGoldfishOMXComponent : public GoldfishOMXComponent {
40     SimpleGoldfishOMXComponent(
41             const char *name,
42             const OMX_CALLBACKTYPE *callbacks,
43             OMX_PTR appData,
44             OMX_COMPONENTTYPE **component);
45 
46     virtual void prepareForDestruction();
47 
48     void onMessageReceived(const sp<AMessage> &msg);
49 
50 protected:
51     struct BufferInfo {
52         OMX_BUFFERHEADERTYPE *mHeader;
53         bool mOwnedByUs;
54         bool mFrameConfig;
55     };
56 
57     struct PortInfo {
58         OMX_PARAM_PORTDEFINITIONTYPE mDef;
59         Vector<BufferInfo> mBuffers;
60         List<BufferInfo *> mQueue;
61 
62         enum {
63             NONE,
64             DISABLING,
65             ENABLING,
66         } mTransition;
67     };
68 
69     enum {
70         kStoreMetaDataExtensionIndex = OMX_IndexVendorStartUnused + 1,
71         kPrepareForAdaptivePlaybackIndex,
72     };
73 
74     void addPort(const OMX_PARAM_PORTDEFINITIONTYPE &def);
75 
76     virtual OMX_ERRORTYPE internalGetParameter(
77             OMX_INDEXTYPE index, OMX_PTR params);
78 
79     virtual OMX_ERRORTYPE internalSetParameter(
80             OMX_INDEXTYPE index, const OMX_PTR params);
81 
82     virtual OMX_ERRORTYPE internalSetConfig(
83             OMX_INDEXTYPE index, const OMX_PTR params, bool *frameConfig);
84 
85     virtual void onQueueFilled(OMX_U32 portIndex);
86     List<BufferInfo *> &getPortQueue(OMX_U32 portIndex);
87 
88     virtual void onPortFlushCompleted(OMX_U32 portIndex);
89     virtual void onPortEnableCompleted(OMX_U32 portIndex, bool enabled);
90     virtual void onReset();
91 
92     PortInfo *editPortInfo(OMX_U32 portIndex);
93 
94 protected:
95     enum {
96         kWhatSendCommand,
97         kWhatEmptyThisBuffer,
98         kWhatFillThisBuffer,
99     };
100 
101     Mutex mLock;
102 
103     sp<ALooper> mLooper;
104     sp<AHandlerReflector<SimpleGoldfishOMXComponent> > mHandler;
105 
106     OMX_STATETYPE mState;
107     OMX_STATETYPE mTargetState;
108 
109     Vector<PortInfo> mPorts;
110     std::atomic_bool mFrameConfig;
111 
112     bool isSetParameterAllowed(
113             OMX_INDEXTYPE index, const OMX_PTR params) const;
114 
115     virtual OMX_ERRORTYPE sendCommand(
116             OMX_COMMANDTYPE cmd, OMX_U32 param, OMX_PTR data);
117 
118     virtual OMX_ERRORTYPE getParameter(
119             OMX_INDEXTYPE index, OMX_PTR params);
120 
121     virtual OMX_ERRORTYPE setParameter(
122             OMX_INDEXTYPE index, const OMX_PTR params);
123 
124     virtual OMX_ERRORTYPE setConfig(
125             OMX_INDEXTYPE index, const OMX_PTR params);
126 
127     virtual OMX_ERRORTYPE useBuffer(
128             OMX_BUFFERHEADERTYPE **buffer,
129             OMX_U32 portIndex,
130             OMX_PTR appPrivate,
131             OMX_U32 size,
132             OMX_U8 *ptr);
133 
134     OMX_ERRORTYPE useBufferCallerLockedAlready(
135             OMX_BUFFERHEADERTYPE **buffer,
136             OMX_U32 portIndex,
137             OMX_PTR appPrivate,
138             OMX_U32 size,
139             OMX_U8 *ptr);
140 
141     virtual OMX_ERRORTYPE allocateBuffer(
142             OMX_BUFFERHEADERTYPE **buffer,
143             OMX_U32 portIndex,
144             OMX_PTR appPrivate,
145             OMX_U32 size);
146 
147     virtual OMX_ERRORTYPE freeBuffer(
148             OMX_U32 portIndex,
149             OMX_BUFFERHEADERTYPE *buffer);
150 
151     virtual OMX_ERRORTYPE emptyThisBuffer(
152             OMX_BUFFERHEADERTYPE *buffer);
153 
154     virtual OMX_ERRORTYPE fillThisBuffer(
155             OMX_BUFFERHEADERTYPE *buffer);
156 
157     virtual OMX_ERRORTYPE getState(OMX_STATETYPE *state);
158 
159     void onSendCommand(OMX_COMMANDTYPE cmd, OMX_U32 param);
160     void onChangeState(OMX_STATETYPE state);
161     void onPortEnable(OMX_U32 portIndex, bool enable);
162     void onPortFlush(OMX_U32 portIndex, bool sendFlushComplete);
163 
164     void checkTransitions();
165 
166     DISALLOW_EVIL_CONSTRUCTORS(SimpleGoldfishOMXComponent);
167 };
168 
169 }  // namespace android
170 
171 #endif  // SIMPLE_GOLDFISH_OMX_COMPONENT_H_
172