Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #if !defined(OmxDataDecoder_h_)
8 : #define OmxDataDecoder_h_
9 :
10 : #include "mozilla/Monitor.h"
11 :
12 : #include "AudioCompactor.h"
13 : #include "ImageContainer.h"
14 : #include "MediaInfo.h"
15 : #include "PlatformDecoderModule.h"
16 :
17 : #include "OMX_Component.h"
18 :
19 : #include "OmxPromiseLayer.h"
20 :
21 : namespace mozilla {
22 :
23 : class MediaDataHelper;
24 :
25 : typedef OmxPromiseLayer::OmxCommandPromise OmxCommandPromise;
26 : typedef OmxPromiseLayer::OmxBufferPromise OmxBufferPromise;
27 : typedef OmxPromiseLayer::OmxBufferFailureHolder OmxBufferFailureHolder;
28 : typedef OmxPromiseLayer::OmxCommandFailureHolder OmxCommandFailureHolder;
29 : typedef OmxPromiseLayer::BufferData BufferData;
30 : typedef OmxPromiseLayer::BUFFERLIST BUFFERLIST;
31 :
32 : /* OmxDataDecoder is the major class which performs followings:
33 : * 1. Translate PDM function into OMX commands.
34 : * 2. Keeping the buffers between client and component.
35 : * 3. Manage the OMX state.
36 : *
37 : * From the definition in OpenMax spec. "2.2.1", there are 3 major roles in
38 : * OpenMax IL.
39 : *
40 : * IL client:
41 : * "The IL client may be a layer below the GUI application, such as GStreamer,
42 : * or may be several layers below the GUI layer."
43 : *
44 : * OmxDataDecoder acts as the IL client.
45 : *
46 : * OpenMAX IL component:
47 : * "A component that is intended to wrap functionality that is required in the
48 : * target system."
49 : *
50 : * OmxPromiseLayer acts as the OpenMAX IL component.
51 : *
52 : * OpenMAX IL core:
53 : * "Platform-specific code that has the functionality necessary to locate and
54 : * then load an OpenMAX IL component into main memory."
55 : *
56 : * OmxPlatformLayer acts as the OpenMAX IL core.
57 : */
58 : class OmxDataDecoder : public MediaDataDecoder {
59 : protected:
60 : virtual ~OmxDataDecoder();
61 :
62 : public:
63 : OmxDataDecoder(const TrackInfo& aTrackInfo,
64 : layers::ImageContainer* aImageContainer);
65 :
66 : RefPtr<InitPromise> Init() override;
67 : RefPtr<DecodePromise> Decode(MediaRawData* aSample) override;
68 : RefPtr<DecodePromise> Drain() override;
69 : RefPtr<FlushPromise> Flush() override;
70 : RefPtr<ShutdownPromise> Shutdown() override;
71 :
72 0 : const char* GetDescriptionName() const override
73 : {
74 0 : return "omx decoder";
75 : }
76 :
77 0 : ConversionRequired NeedsConversion() const override
78 : {
79 0 : return ConversionRequired::kNeedAnnexB;
80 : }
81 :
82 : // Return true if event is handled.
83 : bool Event(OMX_EVENTTYPE aEvent, OMX_U32 aData1, OMX_U32 aData2);
84 :
85 : protected:
86 : void InitializationTask();
87 :
88 : void ResolveInitPromise(const char* aMethodName);
89 :
90 : void RejectInitPromise(MediaResult aError, const char* aMethodName);
91 :
92 : void OmxStateRunner();
93 :
94 : void FillAndEmptyBuffers();
95 :
96 : void FillBufferDone(BufferData* aData);
97 :
98 : void FillBufferFailure(OmxBufferFailureHolder aFailureHolder);
99 :
100 : void EmptyBufferDone(BufferData* aData);
101 :
102 : void EmptyBufferFailure(OmxBufferFailureHolder aFailureHolder);
103 :
104 : void NotifyError(OMX_ERRORTYPE aOmxError,
105 : const char* aLine,
106 : const MediaResult& aError = MediaResult(NS_ERROR_DOM_MEDIA_FATAL_ERR));
107 :
108 : // Configure audio/video codec.
109 : // Some codec may just ignore this and rely on codec specific data in
110 : // FillCodecConfigDataToOmx().
111 : void ConfigCodec();
112 :
113 : // Sending codec specific data to OMX component. OMX component could send a
114 : // OMX_EventPortSettingsChanged back to client. And then client needs to
115 : // disable port and reallocate buffer.
116 : void FillCodecConfigDataToOmx();
117 :
118 : void SendEosBuffer();
119 :
120 : void EndOfStream();
121 :
122 : // It could be called after codec specific data is sent and component found
123 : // the port format is changed due to different codec specific.
124 : void PortSettingsChanged();
125 :
126 : void Output(BufferData* aData);
127 :
128 : // Buffer can be released if its status is not OMX_COMPONENT or
129 : // OMX_CLIENT_OUTPUT.
130 : bool BuffersCanBeReleased(OMX_DIRTYPE aType);
131 :
132 : OMX_DIRTYPE GetPortDirection(uint32_t aPortIndex);
133 :
134 : RefPtr<ShutdownPromise> DoAsyncShutdown();
135 :
136 : RefPtr<FlushPromise> DoFlush();
137 :
138 : void FlushComplete(OMX_COMMANDTYPE aCommandType);
139 :
140 : void FlushFailure(OmxCommandFailureHolder aFailureHolder);
141 :
142 : BUFFERLIST* GetBuffers(OMX_DIRTYPE aType);
143 :
144 : nsresult AllocateBuffers(OMX_DIRTYPE aType);
145 :
146 : nsresult ReleaseBuffers(OMX_DIRTYPE aType);
147 :
148 : BufferData* FindAvailableBuffer(OMX_DIRTYPE aType);
149 :
150 : // aType could be OMX_DirMax for all types.
151 : RefPtr<OmxPromiseLayer::OmxBufferPromise::AllPromiseType>
152 : CollectBufferPromises(OMX_DIRTYPE aType);
153 :
154 : // The Omx TaskQueue.
155 : RefPtr<TaskQueue> mOmxTaskQueue;
156 :
157 : RefPtr<layers::ImageContainer> mImageContainer;
158 :
159 : WatchManager<OmxDataDecoder> mWatchManager;
160 :
161 : // It is accessed in omx TaskQueue.
162 : Watchable<OMX_STATETYPE> mOmxState;
163 :
164 : RefPtr<OmxPromiseLayer> mOmxLayer;
165 :
166 : UniquePtr<TrackInfo> mTrackInfo;
167 :
168 : // It is accessed in both omx and reader TaskQueue.
169 : Atomic<bool> mFlushing;
170 :
171 : // It is accessed in Omx/reader TaskQueue.
172 : Atomic<bool> mShuttingDown;
173 :
174 : // It is accessed in Omx TaskQeueu.
175 : bool mCheckingInputExhausted;
176 :
177 : // It is accessed in OMX TaskQueue.
178 : MozPromiseHolder<InitPromise> mInitPromise;
179 : MozPromiseHolder<DecodePromise> mDecodePromise;
180 : MozPromiseHolder<DecodePromise> mDrainPromise;
181 : MozPromiseHolder<FlushPromise> mFlushPromise;
182 : MozPromiseHolder<ShutdownPromise> mShutdownPromise;
183 : // Where decoded samples will be stored until the decode promise is resolved.
184 : DecodedData mDecodedData;
185 :
186 : void CompleteDrain();
187 :
188 : // It is written in Omx TaskQueue. Read in Omx TaskQueue.
189 : // It value means the port index which port settings is changed.
190 : // -1 means no port setting changed.
191 : //
192 : // Note: when port setting changed, there should be no buffer operations
193 : // via EmptyBuffer or FillBuffer.
194 : Watchable<int32_t> mPortSettingsChanged;
195 :
196 : // It is access in Omx TaskQueue.
197 : nsTArray<RefPtr<MediaRawData>> mMediaRawDatas;
198 :
199 : BUFFERLIST mInPortBuffers;
200 :
201 : BUFFERLIST mOutPortBuffers;
202 :
203 : RefPtr<MediaDataHelper> mMediaDataHelper;
204 : };
205 :
206 : template<class T>
207 0 : void InitOmxParameter(T* aParam)
208 : {
209 0 : PodZero(aParam);
210 0 : aParam->nSize = sizeof(T);
211 0 : aParam->nVersion.s.nVersionMajor = 1;
212 0 : }
213 :
214 : }
215 :
216 : #endif /* OmxDataDecoder_h_ */
|