• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 GRAPHIC_BUFFER_SOURCE_H_
18 
19 #define GRAPHIC_BUFFER_SOURCE_H_
20 
21 #include <gui/IGraphicBufferProducer.h>
22 #include <gui/BufferQueue.h>
23 #include <utils/RefBase.h>
24 
25 #include <OMX_Core.h>
26 #include <VideoAPI.h>
27 #include "../include/OMXNodeInstance.h"
28 #include <media/stagefright/foundation/ABase.h>
29 #include <media/stagefright/foundation/AHandlerReflector.h>
30 #include <media/stagefright/foundation/ALooper.h>
31 
32 namespace android {
33 
34 struct FrameDropper;
35 
36 /*
37  * This class is used to feed OMX codecs from a Surface via BufferQueue.
38  *
39  * Instances of the class don't run on a dedicated thread.  Instead,
40  * various events trigger data movement:
41  *
42  *  - Availability of a new frame of data from the BufferQueue (notified
43  *    via the onFrameAvailable callback).
44  *  - The return of a codec buffer (via OnEmptyBufferDone).
45  *  - Application signaling end-of-stream.
46  *  - Transition to or from "executing" state.
47  *
48  * Frames of data (and, perhaps, the end-of-stream indication) can arrive
49  * before the codec is in the "executing" state, so we need to queue
50  * things up until we're ready to go.
51  */
52 class GraphicBufferSource : public BufferQueue::ConsumerListener {
53 public:
54     GraphicBufferSource(
55             OMXNodeInstance* nodeInstance,
56             uint32_t bufferWidth,
57             uint32_t bufferHeight,
58             uint32_t bufferCount,
59             uint32_t consumerUsage,
60             const sp<IGraphicBufferConsumer> &consumer = NULL
61     );
62 
63     virtual ~GraphicBufferSource();
64 
65     // We can't throw an exception if the constructor fails, so we just set
66     // this and require that the caller test the value.
initCheck()67     status_t initCheck() const {
68         return mInitCheck;
69     }
70 
71     // Returns the handle to the producer side of the BufferQueue.  Buffers
72     // queued on this will be received by GraphicBufferSource.
getIGraphicBufferProducer()73     sp<IGraphicBufferProducer> getIGraphicBufferProducer() const {
74         return mProducer;
75     }
76 
77     // Sets the default buffer data space
78     void setDefaultDataSpace(android_dataspace dataSpace);
79 
80     // This is called when OMX transitions to OMX_StateExecuting, which means
81     // we can start handing it buffers.  If we already have buffers of data
82     // sitting in the BufferQueue, this will send them to the codec.
83     void omxExecuting();
84 
85     // This is called when OMX transitions to OMX_StateIdle, indicating that
86     // the codec is meant to return all buffers back to the client for them
87     // to be freed. Do NOT submit any more buffers to the component.
88     void omxIdle();
89 
90     // This is called when OMX transitions to OMX_StateLoaded, indicating that
91     // we are shutting down.
92     void omxLoaded();
93 
94     // A "codec buffer", i.e. a buffer that can be used to pass data into
95     // the encoder, has been allocated.  (This call does not call back into
96     // OMXNodeInstance.)
97     void addCodecBuffer(OMX_BUFFERHEADERTYPE* header);
98 
99     // Called from OnEmptyBufferDone.  If we have a BQ buffer available,
100     // fill it with a new frame of data; otherwise, just mark it as available.
101     void codecBufferEmptied(OMX_BUFFERHEADERTYPE* header, int fenceFd);
102 
103     // Called when omx_message::FILL_BUFFER_DONE is received. (Currently the
104     // buffer source will fix timestamp in the header if needed.)
105     void codecBufferFilled(OMX_BUFFERHEADERTYPE* header);
106 
107     // This is called after the last input frame has been submitted.  We
108     // need to submit an empty buffer with the EOS flag set.  If we don't
109     // have a codec buffer ready, we just set the mEndOfStream flag.
110     status_t signalEndOfInputStream();
111 
112     // If suspend is true, all incoming buffers (including those currently
113     // in the BufferQueue) will be discarded until the suspension is lifted.
114     void suspend(bool suspend);
115 
116     // Specifies the interval after which we requeue the buffer previously
117     // queued to the encoder. This is useful in the case of surface flinger
118     // providing the input surface if the resulting encoded stream is to
119     // be displayed "live". If we were not to push through the extra frame
120     // the decoder on the remote end would be unable to decode the latest frame.
121     // This API must be called before transitioning the encoder to "executing"
122     // state and once this behaviour is specified it cannot be reset.
123     status_t setRepeatPreviousFrameDelayUs(int64_t repeatAfterUs);
124 
125     // When set, the timestamp fed to the encoder will be modified such that
126     // the gap between two adjacent frames is capped at maxGapUs. Timestamp
127     // will be restored to the original when the encoded frame is returned to
128     // the client.
129     // This is to solve a problem in certain real-time streaming case, where
130     // encoder's rate control logic produces huge frames after a long period
131     // of suspension on input.
132     status_t setMaxTimestampGapUs(int64_t maxGapUs);
133 
134     // Sets the input buffer timestamp offset.
135     // When set, the sample's timestamp will be adjusted with the timeOffsetUs.
136     status_t setInputBufferTimeOffset(int64_t timeOffsetUs);
137 
138     // When set, the max frame rate fed to the encoder will be capped at maxFps.
139     status_t setMaxFps(float maxFps);
140 
141     struct TimeLapseConfig {
142         int64_t mTimePerFrameUs;   // the time (us) between two frames for playback
143         int64_t mTimePerCaptureUs; // the time (us) between two frames for capture
144     };
145 
146     // Sets the time lapse (or slow motion) parameters.
147     // When set, the sample's timestamp will be modified to playback framerate,
148     // and capture timestamp will be modified to capture rate.
149     status_t setTimeLapseConfig(const TimeLapseConfig &config);
150 
151     // Sets the start time us (in system time), samples before which should
152     // be dropped and not submitted to encoder
153     void setSkipFramesBeforeUs(int64_t startTimeUs);
154 
155     // Sets the desired color aspects, e.g. to be used when producer does not specify a dataspace.
156     void setColorAspects(const ColorAspects &aspects);
157 
158 protected:
159     // BufferQueue::ConsumerListener interface, called when a new frame of
160     // data is available.  If we're executing and a codec buffer is
161     // available, we acquire the buffer, copy the GraphicBuffer reference
162     // into the codec buffer, and call Empty[This]Buffer.  If we're not yet
163     // executing or there's no codec buffer available, we just increment
164     // mNumFramesAvailable and return.
165     virtual void onFrameAvailable(const BufferItem& item);
166 
167     // BufferQueue::ConsumerListener interface, called when the client has
168     // released one or more GraphicBuffers.  We clear out the appropriate
169     // set of mBufferSlot entries.
170     virtual void onBuffersReleased();
171 
172     // BufferQueue::ConsumerListener interface, called when the client has
173     // changed the sideband stream. GraphicBufferSource doesn't handle sideband
174     // streams so this is a no-op (and should never be called).
175     virtual void onSidebandStreamChanged();
176 
177 private:
178     // PersistentProxyListener is similar to BufferQueue::ProxyConsumerListener
179     // except that it returns (acquire/detach/re-attache/release) buffers
180     // in onFrameAvailable() if the actual consumer object is no longer valid.
181     //
182     // This class is used in persistent input surface case to prevent buffer
183     // loss when onFrameAvailable() is received while we don't have a valid
184     // consumer around.
185     class PersistentProxyListener : public BnConsumerListener {
186         public:
187             PersistentProxyListener(
188                     const wp<IGraphicBufferConsumer> &consumer,
189                     const wp<ConsumerListener>& consumerListener);
190             virtual ~PersistentProxyListener();
191             virtual void onFrameAvailable(const BufferItem& item) override;
192             virtual void onFrameReplaced(const BufferItem& item) override;
193             virtual void onBuffersReleased() override;
194             virtual void onSidebandStreamChanged() override;
195          private:
196             // mConsumerListener is a weak reference to the IConsumerListener.
197             wp<ConsumerListener> mConsumerListener;
198             // mConsumer is a weak reference to the IGraphicBufferConsumer, use
199             // a weak ref to avoid circular ref between mConsumer and this class
200             wp<IGraphicBufferConsumer> mConsumer;
201     };
202 
203     // Keep track of codec input buffers.  They may either be available
204     // (mGraphicBuffer == NULL) or in use by the codec.
205     struct CodecBuffer {
206         OMX_BUFFERHEADERTYPE* mHeader;
207 
208         // buffer producer's frame-number for buffer
209         uint64_t mFrameNumber;
210 
211         // buffer producer's buffer slot for buffer
212         int mSlot;
213 
214         sp<GraphicBuffer> mGraphicBuffer;
215     };
216 
217     // Returns the index of an available codec buffer.  If none are
218     // available, returns -1.  Mutex must be held by caller.
219     int findAvailableCodecBuffer_l();
220 
221     // Returns true if a codec buffer is available.
isCodecBufferAvailable_l()222     bool isCodecBufferAvailable_l() {
223         return findAvailableCodecBuffer_l() >= 0;
224     }
225 
226     // Finds the mCodecBuffers entry that matches.  Returns -1 if not found.
227     int findMatchingCodecBuffer_l(const OMX_BUFFERHEADERTYPE* header);
228 
229     // Fills a codec buffer with a frame from the BufferQueue.  This must
230     // only be called when we know that a frame of data is ready (i.e. we're
231     // in the onFrameAvailable callback, or if we're in codecBufferEmptied
232     // and mNumFramesAvailable is nonzero).  Returns without doing anything if
233     // we don't have a codec buffer available.
234     //
235     // Returns true if we successfully filled a codec buffer with a BQ buffer.
236     bool fillCodecBuffer_l();
237 
238     // Marks the mCodecBuffers entry as in-use, copies the GraphicBuffer
239     // reference into the codec buffer, and submits the data to the codec.
240     status_t submitBuffer_l(const BufferItem &item, int cbi);
241 
242     // Submits an empty buffer, with the EOS flag set.   Returns without
243     // doing anything if we don't have a codec buffer available.
244     void submitEndOfInputStream_l();
245 
246     // Release buffer to the consumer
247     void releaseBuffer(
248             int &id, uint64_t frameNum,
249             const sp<GraphicBuffer> buffer, const sp<Fence> &fence);
250 
251     void setLatestBuffer_l(const BufferItem &item, bool dropped);
252     bool repeatLatestBuffer_l();
253     int64_t getTimestamp(const BufferItem &item);
254 
255     // called when the data space of the input buffer changes
256     void onDataSpaceChanged_l(android_dataspace dataSpace, android_pixel_format pixelFormat);
257 
258     // Lock, covers all member variables.
259     mutable Mutex mMutex;
260 
261     // Used to report constructor failure.
262     status_t mInitCheck;
263 
264     // Pointer back to the object that contains us.  We send buffers here.
265     OMXNodeInstance* mNodeInstance;
266 
267     // Set by omxExecuting() / omxIdling().
268     bool mExecuting;
269 
270     bool mSuspended;
271 
272     // Last dataspace seen
273     android_dataspace mLastDataSpace;
274 
275     // Our BufferQueue interfaces. mProducer is passed to the producer through
276     // getIGraphicBufferProducer, and mConsumer is used internally to retrieve
277     // the buffers queued by the producer.
278     bool mIsPersistent;
279     sp<IGraphicBufferProducer> mProducer;
280     sp<IGraphicBufferConsumer> mConsumer;
281 
282     // Number of frames pending in BufferQueue that haven't yet been
283     // forwarded to the codec.
284     size_t mNumFramesAvailable;
285 
286     // Number of frames acquired from consumer (debug only)
287     int32_t mNumBufferAcquired;
288 
289     // Set to true if we want to send end-of-stream after we run out of
290     // frames in BufferQueue.
291     bool mEndOfStream;
292     bool mEndOfStreamSent;
293 
294     // Cache of GraphicBuffers from the buffer queue.  When the codec
295     // is done processing a GraphicBuffer, we can use this to map back
296     // to a slot number.
297     sp<GraphicBuffer> mBufferSlot[BufferQueue::NUM_BUFFER_SLOTS];
298 
299     // Tracks codec buffers.
300     Vector<CodecBuffer> mCodecBuffers;
301 
302     ////
303     friend struct AHandlerReflector<GraphicBufferSource>;
304 
305     enum {
306         kWhatRepeatLastFrame,
307     };
308     enum {
309         kRepeatLastFrameCount = 10,
310     };
311 
312     KeyedVector<int64_t, int64_t> mOriginalTimeUs;
313     int64_t mMaxTimestampGapUs;
314     int64_t mPrevOriginalTimeUs;
315     int64_t mPrevModifiedTimeUs;
316     int64_t mSkipFramesBeforeNs;
317 
318     sp<FrameDropper> mFrameDropper;
319 
320     sp<ALooper> mLooper;
321     sp<AHandlerReflector<GraphicBufferSource> > mReflector;
322 
323     int64_t mRepeatAfterUs;
324     int32_t mRepeatLastFrameGeneration;
325     int64_t mRepeatLastFrameTimestamp;
326     int32_t mRepeatLastFrameCount;
327 
328     int mLatestBufferId;
329     uint64_t mLatestBufferFrameNum;
330     int32_t mLatestBufferUseCount;
331     sp<Fence> mLatestBufferFence;
332 
333     // The previous buffer should've been repeated but
334     // no codec buffer was available at the time.
335     bool mRepeatBufferDeferred;
336 
337     // Time lapse / slow motion configuration
338     int64_t mTimePerCaptureUs;
339     int64_t mTimePerFrameUs;
340     int64_t mPrevCaptureUs;
341     int64_t mPrevFrameUs;
342 
343     int64_t mInputBufferTimeOffsetUs;
344 
345     MetadataBufferType mMetadataBufferType;
346     ColorAspects mColorAspects;
347 
348     void onMessageReceived(const sp<AMessage> &msg);
349 
350     DISALLOW_EVIL_CONSTRUCTORS(GraphicBufferSource);
351 };
352 
353 }  // namespace android
354 
355 #endif  // GRAPHIC_BUFFER_SOURCE_H_
356