1f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden/*
2f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * Copyright (C) 2013 The Android Open Source Project
3f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *
4f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * Licensed under the Apache License, Version 2.0 (the "License");
5f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * you may not use this file except in compliance with the License.
6f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * You may obtain a copy of the License at
7f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *
8f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *      http://www.apache.org/licenses/LICENSE-2.0
9f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *
10f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * Unless required by applicable law or agreed to in writing, software
11f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * distributed under the License is distributed on an "AS IS" BASIS,
12f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * See the License for the specific language governing permissions and
14f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * limitations under the License.
15f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden */
16f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
17f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden#ifndef GRAPHIC_BUFFER_SOURCE_H_
18f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
19f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden#define GRAPHIC_BUFFER_SOURCE_H_
20f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
21f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden#include <gui/IGraphicBufferProducer.h>
22f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden#include <gui/BufferQueue.h>
23f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden#include <utils/RefBase.h>
24f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
25f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden#include <OMX_Core.h>
26f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden#include "../include/OMXNodeInstance.h"
27f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden#include <media/stagefright/foundation/ABase.h>
28a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber#include <media/stagefright/foundation/AHandlerReflector.h>
29a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber#include <media/stagefright/foundation/ALooper.h>
30f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
31f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFaddennamespace android {
32f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
33f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden/*
34f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * This class is used to feed OMX codecs from a Surface via BufferQueue.
35f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *
36f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * Instances of the class don't run on a dedicated thread.  Instead,
37f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * various events trigger data movement:
38f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *
39f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *  - Availability of a new frame of data from the BufferQueue (notified
40f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *    via the onFrameAvailable callback).
41f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *  - The return of a codec buffer (via OnEmptyBufferDone).
42f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *  - Application signaling end-of-stream.
43f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *  - Transition to or from "executing" state.
44f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *
45f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * Frames of data (and, perhaps, the end-of-stream indication) can arrive
46f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * before the codec is in the "executing" state, so we need to queue
47f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * things up until we're ready to go.
48f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden */
49f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFaddenclass GraphicBufferSource : public BufferQueue::ConsumerListener {
50f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFaddenpublic:
51f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    GraphicBufferSource(OMXNodeInstance* nodeInstance,
52512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar            uint32_t bufferWidth, uint32_t bufferHeight, uint32_t bufferCount,
53512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar            bool useGraphicBufferInMeta = false);
54f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    virtual ~GraphicBufferSource();
55f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
56f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // We can't throw an exception if the constructor fails, so we just set
57f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // this and require that the caller test the value.
58f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    status_t initCheck() const {
59f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden        return mInitCheck;
60f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    }
61f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
62f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Returns the handle to the producer side of the BufferQueue.  Buffers
63f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // queued on this will be received by GraphicBufferSource.
64f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    sp<IGraphicBufferProducer> getIGraphicBufferProducer() const {
655205977929c8a63d3bba026c6bd7b4cc1e236627Dan Stoza        return mProducer;
66f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    }
67f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
68f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // This is called when OMX transitions to OMX_StateExecuting, which means
69f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // we can start handing it buffers.  If we already have buffers of data
70f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // sitting in the BufferQueue, this will send them to the codec.
71f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    void omxExecuting();
72f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
73ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber    // This is called when OMX transitions to OMX_StateIdle, indicating that
74ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber    // the codec is meant to return all buffers back to the client for them
75ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber    // to be freed. Do NOT submit any more buffers to the component.
76ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber    void omxIdle();
77ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber
78ba6218eae3dbcf3f962b3561b26374a214dbf5e2Andy McFadden    // This is called when OMX transitions to OMX_StateLoaded, indicating that
79ba6218eae3dbcf3f962b3561b26374a214dbf5e2Andy McFadden    // we are shutting down.
80ba6218eae3dbcf3f962b3561b26374a214dbf5e2Andy McFadden    void omxLoaded();
81f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
82f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // A "codec buffer", i.e. a buffer that can be used to pass data into
83f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // the encoder, has been allocated.  (This call does not call back into
84f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // OMXNodeInstance.)
85f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    void addCodecBuffer(OMX_BUFFERHEADERTYPE* header);
86f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
87f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Called from OnEmptyBufferDone.  If we have a BQ buffer available,
88f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // fill it with a new frame of data; otherwise, just mark it as available.
89f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    void codecBufferEmptied(OMX_BUFFERHEADERTYPE* header);
90f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
9194ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // Called when omx_message::FILL_BUFFER_DONE is received. (Currently the
9294ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // buffer source will fix timestamp in the header if needed.)
9394ee4b708acfa941581160b267afb79192b1d816Chong Zhang    void codecBufferFilled(OMX_BUFFERHEADERTYPE* header);
9494ee4b708acfa941581160b267afb79192b1d816Chong Zhang
95f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // This is called after the last input frame has been submitted.  We
96f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // need to submit an empty buffer with the EOS flag set.  If we don't
97f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // have a codec buffer ready, we just set the mEndOfStream flag.
98ba6218eae3dbcf3f962b3561b26374a214dbf5e2Andy McFadden    status_t signalEndOfInputStream();
99f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
100e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber    // If suspend is true, all incoming buffers (including those currently
101e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber    // in the BufferQueue) will be discarded until the suspension is lifted.
102e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber    void suspend(bool suspend);
103e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber
104a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // Specifies the interval after which we requeue the buffer previously
105a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // queued to the encoder. This is useful in the case of surface flinger
106a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // providing the input surface if the resulting encoded stream is to
107a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // be displayed "live". If we were not to push through the extra frame
108a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // the decoder on the remote end would be unable to decode the latest frame.
109a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // This API must be called before transitioning the encoder to "executing"
110a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // state and once this behaviour is specified it cannot be reset.
111a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    status_t setRepeatPreviousFrameDelayUs(int64_t repeatAfterUs);
112a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
11394ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // When set, the timestamp fed to the encoder will be modified such that
11494ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // the gap between two adjacent frames is capped at maxGapUs. Timestamp
11594ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // will be restored to the original when the encoded frame is returned to
11694ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // the client.
11794ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // This is to solve a problem in certain real-time streaming case, where
11894ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // encoder's rate control logic produces huge frames after a long period
11994ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // of suspension on input.
12094ee4b708acfa941581160b267afb79192b1d816Chong Zhang    status_t setMaxTimestampGapUs(int64_t maxGapUs);
12194ee4b708acfa941581160b267afb79192b1d816Chong Zhang
1222c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    // Sets the time lapse (or slow motion) parameters.
1232c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    // data[0] is the time (us) between two frames for playback
1242c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    // data[1] is the time (us) between two frames for capture
1252c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    // When set, the sample's timestamp will be modified to playback framerate,
1262c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    // and capture timestamp will be modified to capture rate.
1272c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    status_t setTimeLapseUs(int64_t* data);
1282c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang
12972cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    // Sets the start time us (in system time), samples before which should
13072cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    // be dropped and not submitted to encoder
13172cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    void setSkipFramesBeforeUs(int64_t startTimeUs);
13272cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang
133f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFaddenprotected:
134f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // BufferQueue::ConsumerListener interface, called when a new frame of
135f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // data is available.  If we're executing and a codec buffer is
136f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // available, we acquire the buffer, copy the GraphicBuffer reference
137f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // into the codec buffer, and call Empty[This]Buffer.  If we're not yet
138f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // executing or there's no codec buffer available, we just increment
139f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // mNumFramesAvailable and return.
14004f101c35eaa90b1f95939afac30674ec1611e6fDan Stoza    virtual void onFrameAvailable(const BufferItem& item);
141f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
142f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // BufferQueue::ConsumerListener interface, called when the client has
143f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // released one or more GraphicBuffers.  We clear out the appropriate
144f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // set of mBufferSlot entries.
145f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    virtual void onBuffersReleased();
146f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
1478dcc81a2fdb35905347cf7ef46d198afa7ae79cdJesse Hall    // BufferQueue::ConsumerListener interface, called when the client has
1488dcc81a2fdb35905347cf7ef46d198afa7ae79cdJesse Hall    // changed the sideband stream. GraphicBufferSource doesn't handle sideband
1498dcc81a2fdb35905347cf7ef46d198afa7ae79cdJesse Hall    // streams so this is a no-op (and should never be called).
1508dcc81a2fdb35905347cf7ef46d198afa7ae79cdJesse Hall    virtual void onSidebandStreamChanged();
1518dcc81a2fdb35905347cf7ef46d198afa7ae79cdJesse Hall
152f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFaddenprivate:
153f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Keep track of codec input buffers.  They may either be available
154f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // (mGraphicBuffer == NULL) or in use by the codec.
155f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    struct CodecBuffer {
156f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden        OMX_BUFFERHEADERTYPE* mHeader;
157d030447b617105b31bf3013e5e4b39d422b53b77Lajos Molnar
158d030447b617105b31bf3013e5e4b39d422b53b77Lajos Molnar        // buffer producer's frame-number for buffer
159d030447b617105b31bf3013e5e4b39d422b53b77Lajos Molnar        uint64_t mFrameNumber;
160d030447b617105b31bf3013e5e4b39d422b53b77Lajos Molnar
161d030447b617105b31bf3013e5e4b39d422b53b77Lajos Molnar        // buffer producer's buffer slot for buffer
162d030447b617105b31bf3013e5e4b39d422b53b77Lajos Molnar        int mBuf;
163d030447b617105b31bf3013e5e4b39d422b53b77Lajos Molnar
164f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden        sp<GraphicBuffer> mGraphicBuffer;
165f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    };
166f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
167f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Returns the index of an available codec buffer.  If none are
168f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // available, returns -1.  Mutex must be held by caller.
169f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    int findAvailableCodecBuffer_l();
170f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
171f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Returns true if a codec buffer is available.
172f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    bool isCodecBufferAvailable_l() {
173f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden        return findAvailableCodecBuffer_l() >= 0;
174f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    }
175f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
176f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Finds the mCodecBuffers entry that matches.  Returns -1 if not found.
177f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    int findMatchingCodecBuffer_l(const OMX_BUFFERHEADERTYPE* header);
178f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
179f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Fills a codec buffer with a frame from the BufferQueue.  This must
180f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // only be called when we know that a frame of data is ready (i.e. we're
181f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // in the onFrameAvailable callback, or if we're in codecBufferEmptied
182f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // and mNumFramesAvailable is nonzero).  Returns without doing anything if
183f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // we don't have a codec buffer available.
1840c37f9d1320bb87fd242f9425c67dacd6ce20112Andy McFadden    //
1850c37f9d1320bb87fd242f9425c67dacd6ce20112Andy McFadden    // Returns true if we successfully filled a codec buffer with a BQ buffer.
1860c37f9d1320bb87fd242f9425c67dacd6ce20112Andy McFadden    bool fillCodecBuffer_l();
187f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
188f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Marks the mCodecBuffers entry as in-use, copies the GraphicBuffer
189f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // reference into the codec buffer, and submits the data to the codec.
190d030447b617105b31bf3013e5e4b39d422b53b77Lajos Molnar    status_t submitBuffer_l(const BufferQueue::BufferItem &item, int cbi);
191f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
192f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Submits an empty buffer, with the EOS flag set.   Returns without
193f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // doing anything if we don't have a codec buffer available.
194f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    void submitEndOfInputStream_l();
195f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
196a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    void setLatestSubmittedBuffer_l(const BufferQueue::BufferItem &item);
197a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    bool repeatLatestSubmittedBuffer_l();
19894ee4b708acfa941581160b267afb79192b1d816Chong Zhang    int64_t getTimestamp(const BufferQueue::BufferItem &item);
199a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
200f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Lock, covers all member variables.
201f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    mutable Mutex mMutex;
202f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
203f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Used to report constructor failure.
204f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    status_t mInitCheck;
205f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
206f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Pointer back to the object that contains us.  We send buffers here.
207f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    OMXNodeInstance* mNodeInstance;
208f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
209f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Set by omxExecuting() / omxIdling().
210f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    bool mExecuting;
211f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
212e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber    bool mSuspended;
213e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber
2145205977929c8a63d3bba026c6bd7b4cc1e236627Dan Stoza    // Our BufferQueue interfaces. mProducer is passed to the producer through
2155205977929c8a63d3bba026c6bd7b4cc1e236627Dan Stoza    // getIGraphicBufferProducer, and mConsumer is used internally to retrieve
2165205977929c8a63d3bba026c6bd7b4cc1e236627Dan Stoza    // the buffers queued by the producer.
2175205977929c8a63d3bba026c6bd7b4cc1e236627Dan Stoza    sp<IGraphicBufferProducer> mProducer;
2185205977929c8a63d3bba026c6bd7b4cc1e236627Dan Stoza    sp<IGraphicBufferConsumer> mConsumer;
219f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
220f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Number of frames pending in BufferQueue that haven't yet been
221f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // forwarded to the codec.
222f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    size_t mNumFramesAvailable;
223f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
224f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Set to true if we want to send end-of-stream after we run out of
225f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // frames in BufferQueue.
226f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    bool mEndOfStream;
227f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    bool mEndOfStreamSent;
228f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
229f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Cache of GraphicBuffers from the buffer queue.  When the codec
230f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // is done processing a GraphicBuffer, we can use this to map back
231f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // to a slot number.
232f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    sp<GraphicBuffer> mBufferSlot[BufferQueue::NUM_BUFFER_SLOTS];
233f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
234f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Tracks codec buffers.
235f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    Vector<CodecBuffer> mCodecBuffers;
236f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
237a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    ////
238a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    friend class AHandlerReflector<GraphicBufferSource>;
239a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
240a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    enum {
241a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber        kWhatRepeatLastFrame,
242a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    };
24394ee4b708acfa941581160b267afb79192b1d816Chong Zhang    enum {
24494ee4b708acfa941581160b267afb79192b1d816Chong Zhang        kRepeatLastFrameCount = 10,
24594ee4b708acfa941581160b267afb79192b1d816Chong Zhang    };
24694ee4b708acfa941581160b267afb79192b1d816Chong Zhang
24794ee4b708acfa941581160b267afb79192b1d816Chong Zhang    KeyedVector<int64_t, int64_t> mOriginalTimeUs;
24872cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    int64_t mMaxTimestampGapUs;
24994ee4b708acfa941581160b267afb79192b1d816Chong Zhang    int64_t mPrevOriginalTimeUs;
25094ee4b708acfa941581160b267afb79192b1d816Chong Zhang    int64_t mPrevModifiedTimeUs;
25172cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    int64_t mSkipFramesBeforeNs;
252a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
253a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    sp<ALooper> mLooper;
254a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    sp<AHandlerReflector<GraphicBufferSource> > mReflector;
255a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
25672cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    int64_t mRepeatAfterUs;
257a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    int32_t mRepeatLastFrameGeneration;
25894ee4b708acfa941581160b267afb79192b1d816Chong Zhang    int64_t mRepeatLastFrameTimestamp;
25994ee4b708acfa941581160b267afb79192b1d816Chong Zhang    int32_t mRepeatLastFrameCount;
260a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
261a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    int mLatestSubmittedBufferId;
262a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    uint64_t mLatestSubmittedBufferFrameNum;
263a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    int32_t mLatestSubmittedBufferUseCount;
264a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
265a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // The previously submitted buffer should've been repeated but
266a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // no codec buffer was available at the time.
267a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    bool mRepeatBufferDeferred;
268a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
2692c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    // Time lapse / slow motion configuration
2702c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    int64_t mTimePerCaptureUs;
2712c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    int64_t mTimePerFrameUs;
2722c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    int64_t mPrevCaptureUs;
2732c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    int64_t mPrevFrameUs;
2742c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang
275512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar    bool mUseGraphicBufferInMeta;
276512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar
277a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    void onMessageReceived(const sp<AMessage> &msg);
278a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
279f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    DISALLOW_EVIL_CONSTRUCTORS(GraphicBufferSource);
280f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden};
281f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
282f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden}  // namespace android
283f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
284f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden#endif  // GRAPHIC_BUFFER_SOURCE_H_
285