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>
26dd81af7ef969981748f35ec839869d34ed0cc768Lajos Molnar#include <VideoAPI.h>
27f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden#include "../include/OMXNodeInstance.h"
28f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden#include <media/stagefright/foundation/ABase.h>
29a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber#include <media/stagefright/foundation/AHandlerReflector.h>
30a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber#include <media/stagefright/foundation/ALooper.h>
31f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
32f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFaddennamespace android {
33f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
34ee4e1b1a63758941460ae79a064249d3a5189443Lajos Molnarstruct FrameDropper;
3537b2b389139ed638831e49708c947863eef631efRonghua Wu
36f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden/*
37f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * This class is used to feed OMX codecs from a Surface via BufferQueue.
38f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *
39f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * Instances of the class don't run on a dedicated thread.  Instead,
40f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * various events trigger data movement:
41f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *
42f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *  - Availability of a new frame of data from the BufferQueue (notified
43f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *    via the onFrameAvailable callback).
44f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *  - The return of a codec buffer (via OnEmptyBufferDone).
45f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *  - Application signaling end-of-stream.
46f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *  - Transition to or from "executing" state.
47f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden *
48f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * Frames of data (and, perhaps, the end-of-stream indication) can arrive
49f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * before the codec is in the "executing" state, so we need to queue
50f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden * things up until we're ready to go.
51f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden */
52f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFaddenclass GraphicBufferSource : public BufferQueue::ConsumerListener {
53f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFaddenpublic:
54d291c222357303b9611cab89d0c3b047584ef377Chong Zhang    GraphicBufferSource(
55d291c222357303b9611cab89d0c3b047584ef377Chong Zhang            OMXNodeInstance* nodeInstance,
56d291c222357303b9611cab89d0c3b047584ef377Chong Zhang            uint32_t bufferWidth,
57d291c222357303b9611cab89d0c3b047584ef377Chong Zhang            uint32_t bufferHeight,
58d291c222357303b9611cab89d0c3b047584ef377Chong Zhang            uint32_t bufferCount,
5949605e8ab171a2b1f474645d632d3982f5f7b8e6Lajos Molnar            uint32_t consumerUsage,
60d291c222357303b9611cab89d0c3b047584ef377Chong Zhang            const sp<IGraphicBufferConsumer> &consumer = NULL
61d291c222357303b9611cab89d0c3b047584ef377Chong Zhang    );
62d291c222357303b9611cab89d0c3b047584ef377Chong Zhang
63f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    virtual ~GraphicBufferSource();
64f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
65f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // We can't throw an exception if the constructor fails, so we just set
66f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // this and require that the caller test the value.
67f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    status_t initCheck() const {
68f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden        return mInitCheck;
69f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    }
70f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
71f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Returns the handle to the producer side of the BufferQueue.  Buffers
72f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // queued on this will be received by GraphicBufferSource.
73f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    sp<IGraphicBufferProducer> getIGraphicBufferProducer() const {
745205977929c8a63d3bba026c6bd7b4cc1e236627Dan Stoza        return mProducer;
75f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    }
76f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
7757fad3c31f46ec98d15bc253c16f9d269aeb8ea7Lajos Molnar    // Sets the default buffer data space
7857fad3c31f46ec98d15bc253c16f9d269aeb8ea7Lajos Molnar    void setDefaultDataSpace(android_dataspace dataSpace);
7957fad3c31f46ec98d15bc253c16f9d269aeb8ea7Lajos Molnar
80f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // This is called when OMX transitions to OMX_StateExecuting, which means
81f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // we can start handing it buffers.  If we already have buffers of data
82f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // sitting in the BufferQueue, this will send them to the codec.
83f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    void omxExecuting();
84f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
85ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber    // This is called when OMX transitions to OMX_StateIdle, indicating that
86ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber    // the codec is meant to return all buffers back to the client for them
87ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber    // to be freed. Do NOT submit any more buffers to the component.
88ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber    void omxIdle();
89ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber
90ba6218eae3dbcf3f962b3561b26374a214dbf5e2Andy McFadden    // This is called when OMX transitions to OMX_StateLoaded, indicating that
91ba6218eae3dbcf3f962b3561b26374a214dbf5e2Andy McFadden    // we are shutting down.
92ba6218eae3dbcf3f962b3561b26374a214dbf5e2Andy McFadden    void omxLoaded();
93f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
94f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // A "codec buffer", i.e. a buffer that can be used to pass data into
95f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // the encoder, has been allocated.  (This call does not call back into
96f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // OMXNodeInstance.)
97f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    void addCodecBuffer(OMX_BUFFERHEADERTYPE* header);
98f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
99f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Called from OnEmptyBufferDone.  If we have a BQ buffer available,
100f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // fill it with a new frame of data; otherwise, just mark it as available.
10115ab4996019387f27a48b81cb4774c21502bc0e5Lajos Molnar    void codecBufferEmptied(OMX_BUFFERHEADERTYPE* header, int fenceFd);
102f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
10394ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // Called when omx_message::FILL_BUFFER_DONE is received. (Currently the
10494ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // buffer source will fix timestamp in the header if needed.)
10594ee4b708acfa941581160b267afb79192b1d816Chong Zhang    void codecBufferFilled(OMX_BUFFERHEADERTYPE* header);
10694ee4b708acfa941581160b267afb79192b1d816Chong Zhang
107f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // This is called after the last input frame has been submitted.  We
108f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // need to submit an empty buffer with the EOS flag set.  If we don't
109f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // have a codec buffer ready, we just set the mEndOfStream flag.
110ba6218eae3dbcf3f962b3561b26374a214dbf5e2Andy McFadden    status_t signalEndOfInputStream();
111f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
112e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber    // If suspend is true, all incoming buffers (including those currently
113e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber    // in the BufferQueue) will be discarded until the suspension is lifted.
114e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber    void suspend(bool suspend);
115e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber
116a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // Specifies the interval after which we requeue the buffer previously
117a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // queued to the encoder. This is useful in the case of surface flinger
118a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // providing the input surface if the resulting encoded stream is to
119a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // be displayed "live". If we were not to push through the extra frame
120a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // the decoder on the remote end would be unable to decode the latest frame.
121a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // This API must be called before transitioning the encoder to "executing"
122a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // state and once this behaviour is specified it cannot be reset.
123a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    status_t setRepeatPreviousFrameDelayUs(int64_t repeatAfterUs);
124a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
12594ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // When set, the timestamp fed to the encoder will be modified such that
12694ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // the gap between two adjacent frames is capped at maxGapUs. Timestamp
12794ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // will be restored to the original when the encoded frame is returned to
12894ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // the client.
12994ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // This is to solve a problem in certain real-time streaming case, where
13094ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // encoder's rate control logic produces huge frames after a long period
13194ee4b708acfa941581160b267afb79192b1d816Chong Zhang    // of suspension on input.
13294ee4b708acfa941581160b267afb79192b1d816Chong Zhang    status_t setMaxTimestampGapUs(int64_t maxGapUs);
13394ee4b708acfa941581160b267afb79192b1d816Chong Zhang
13437b2b389139ed638831e49708c947863eef631efRonghua Wu    // When set, the max frame rate fed to the encoder will be capped at maxFps.
13537b2b389139ed638831e49708c947863eef631efRonghua Wu    status_t setMaxFps(float maxFps);
13637b2b389139ed638831e49708c947863eef631efRonghua Wu
137dd81af7ef969981748f35ec839869d34ed0cc768Lajos Molnar    struct TimeLapseConfig {
138dd81af7ef969981748f35ec839869d34ed0cc768Lajos Molnar        int64_t mTimePerFrameUs;   // the time (us) between two frames for playback
139dd81af7ef969981748f35ec839869d34ed0cc768Lajos Molnar        int64_t mTimePerCaptureUs; // the time (us) between two frames for capture
140dd81af7ef969981748f35ec839869d34ed0cc768Lajos Molnar    };
141dd81af7ef969981748f35ec839869d34ed0cc768Lajos Molnar
1422c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    // Sets the time lapse (or slow motion) parameters.
1432c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    // When set, the sample's timestamp will be modified to playback framerate,
1442c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    // and capture timestamp will be modified to capture rate.
145dd81af7ef969981748f35ec839869d34ed0cc768Lajos Molnar    status_t setTimeLapseConfig(const TimeLapseConfig &config);
1462c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang
14772cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    // Sets the start time us (in system time), samples before which should
14872cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    // be dropped and not submitted to encoder
14972cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    void setSkipFramesBeforeUs(int64_t startTimeUs);
15072cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang
151dd81af7ef969981748f35ec839869d34ed0cc768Lajos Molnar    // Sets the desired color aspects, e.g. to be used when producer does not specify a dataspace.
152dd81af7ef969981748f35ec839869d34ed0cc768Lajos Molnar    void setColorAspects(const ColorAspects &aspects);
153dd81af7ef969981748f35ec839869d34ed0cc768Lajos Molnar
154f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFaddenprotected:
155f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // BufferQueue::ConsumerListener interface, called when a new frame of
156f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // data is available.  If we're executing and a codec buffer is
157f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // available, we acquire the buffer, copy the GraphicBuffer reference
158f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // into the codec buffer, and call Empty[This]Buffer.  If we're not yet
159f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // executing or there's no codec buffer available, we just increment
160f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // mNumFramesAvailable and return.
16104f101c35eaa90b1f95939afac30674ec1611e6fDan Stoza    virtual void onFrameAvailable(const BufferItem& item);
162f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
163f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // BufferQueue::ConsumerListener interface, called when the client has
164f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // released one or more GraphicBuffers.  We clear out the appropriate
165f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // set of mBufferSlot entries.
166f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    virtual void onBuffersReleased();
167f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
1688dcc81a2fdb35905347cf7ef46d198afa7ae79cdJesse Hall    // BufferQueue::ConsumerListener interface, called when the client has
1698dcc81a2fdb35905347cf7ef46d198afa7ae79cdJesse Hall    // changed the sideband stream. GraphicBufferSource doesn't handle sideband
1708dcc81a2fdb35905347cf7ef46d198afa7ae79cdJesse Hall    // streams so this is a no-op (and should never be called).
1718dcc81a2fdb35905347cf7ef46d198afa7ae79cdJesse Hall    virtual void onSidebandStreamChanged();
1728dcc81a2fdb35905347cf7ef46d198afa7ae79cdJesse Hall
173f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFaddenprivate:
174ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang    // PersistentProxyListener is similar to BufferQueue::ProxyConsumerListener
175ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang    // except that it returns (acquire/detach/re-attache/release) buffers
176ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang    // in onFrameAvailable() if the actual consumer object is no longer valid.
177ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang    //
178ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang    // This class is used in persistent input surface case to prevent buffer
179ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang    // loss when onFrameAvailable() is received while we don't have a valid
180ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang    // consumer around.
181ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang    class PersistentProxyListener : public BnConsumerListener {
182ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang        public:
183ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang            PersistentProxyListener(
184ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang                    const wp<IGraphicBufferConsumer> &consumer,
185ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang                    const wp<ConsumerListener>& consumerListener);
186ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang            virtual ~PersistentProxyListener();
187ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang            virtual void onFrameAvailable(const BufferItem& item) override;
188ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang            virtual void onFrameReplaced(const BufferItem& item) override;
189ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang            virtual void onBuffersReleased() override;
190ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang            virtual void onSidebandStreamChanged() override;
191ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang         private:
192ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang            // mConsumerListener is a weak reference to the IConsumerListener.
193ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang            wp<ConsumerListener> mConsumerListener;
194ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang            // mConsumer is a weak reference to the IGraphicBufferConsumer, use
195ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang            // a weak ref to avoid circular ref between mConsumer and this class
196ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang            wp<IGraphicBufferConsumer> mConsumer;
197ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang    };
198ffd8cbb288f096b53df0392bf40d99b89e34bea7Chong Zhang
199f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Keep track of codec input buffers.  They may either be available
200f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // (mGraphicBuffer == NULL) or in use by the codec.
201f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    struct CodecBuffer {
202f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden        OMX_BUFFERHEADERTYPE* mHeader;
203d030447b617105b31bf3013e5e4b39d422b53b77Lajos Molnar
204d030447b617105b31bf3013e5e4b39d422b53b77Lajos Molnar        // buffer producer's frame-number for buffer
205d030447b617105b31bf3013e5e4b39d422b53b77Lajos Molnar        uint64_t mFrameNumber;
206d030447b617105b31bf3013e5e4b39d422b53b77Lajos Molnar
207d030447b617105b31bf3013e5e4b39d422b53b77Lajos Molnar        // buffer producer's buffer slot for buffer
208a8f5e0c24143299e3b3d722487de7322f7761559Pablo Ceballos        int mSlot;
209d030447b617105b31bf3013e5e4b39d422b53b77Lajos Molnar
210f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden        sp<GraphicBuffer> mGraphicBuffer;
211f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    };
212f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
213f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Returns the index of an available codec buffer.  If none are
214f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // available, returns -1.  Mutex must be held by caller.
215f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    int findAvailableCodecBuffer_l();
216f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
217f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Returns true if a codec buffer is available.
218f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    bool isCodecBufferAvailable_l() {
219f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden        return findAvailableCodecBuffer_l() >= 0;
220f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    }
221f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
222f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Finds the mCodecBuffers entry that matches.  Returns -1 if not found.
223f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    int findMatchingCodecBuffer_l(const OMX_BUFFERHEADERTYPE* header);
224f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
225f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Fills a codec buffer with a frame from the BufferQueue.  This must
226f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // only be called when we know that a frame of data is ready (i.e. we're
227f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // in the onFrameAvailable callback, or if we're in codecBufferEmptied
228f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // and mNumFramesAvailable is nonzero).  Returns without doing anything if
229f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // we don't have a codec buffer available.
2300c37f9d1320bb87fd242f9425c67dacd6ce20112Andy McFadden    //
2310c37f9d1320bb87fd242f9425c67dacd6ce20112Andy McFadden    // Returns true if we successfully filled a codec buffer with a BQ buffer.
2320c37f9d1320bb87fd242f9425c67dacd6ce20112Andy McFadden    bool fillCodecBuffer_l();
233f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
234f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Marks the mCodecBuffers entry as in-use, copies the GraphicBuffer
235f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // reference into the codec buffer, and submits the data to the codec.
2368ed8ceda7cfe29e8417142ef460cd70060204459Dan Stoza    status_t submitBuffer_l(const BufferItem &item, int cbi);
237f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
238f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Submits an empty buffer, with the EOS flag set.   Returns without
239f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // doing anything if we don't have a codec buffer available.
240f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    void submitEndOfInputStream_l();
241f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
2429700f5fe4b3becfe858cbf5aa7964296975081bbChong Zhang    // Release buffer to the consumer
2439700f5fe4b3becfe858cbf5aa7964296975081bbChong Zhang    void releaseBuffer(
2449700f5fe4b3becfe858cbf5aa7964296975081bbChong Zhang            int &id, uint64_t frameNum,
2459700f5fe4b3becfe858cbf5aa7964296975081bbChong Zhang            const sp<GraphicBuffer> buffer, const sp<Fence> &fence);
2469700f5fe4b3becfe858cbf5aa7964296975081bbChong Zhang
2478ed8ceda7cfe29e8417142ef460cd70060204459Dan Stoza    void setLatestBuffer_l(const BufferItem &item, bool dropped);
24837b2b389139ed638831e49708c947863eef631efRonghua Wu    bool repeatLatestBuffer_l();
2498ed8ceda7cfe29e8417142ef460cd70060204459Dan Stoza    int64_t getTimestamp(const BufferItem &item);
250a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
251b77d03b62cb743d0faf74b54aa466b4d220b5e61Lajos Molnar    // called when the data space of the input buffer changes
252b77d03b62cb743d0faf74b54aa466b4d220b5e61Lajos Molnar    void onDataSpaceChanged_l(android_dataspace dataSpace, android_pixel_format pixelFormat);
253b77d03b62cb743d0faf74b54aa466b4d220b5e61Lajos Molnar
254f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Lock, covers all member variables.
255f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    mutable Mutex mMutex;
256f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
257f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Used to report constructor failure.
258f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    status_t mInitCheck;
259f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
260f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Pointer back to the object that contains us.  We send buffers here.
261f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    OMXNodeInstance* mNodeInstance;
262f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
263f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Set by omxExecuting() / omxIdling().
264f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    bool mExecuting;
265f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
266e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber    bool mSuspended;
267e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber
26857fad3c31f46ec98d15bc253c16f9d269aeb8ea7Lajos Molnar    // Last dataspace seen
26957fad3c31f46ec98d15bc253c16f9d269aeb8ea7Lajos Molnar    android_dataspace mLastDataSpace;
27057fad3c31f46ec98d15bc253c16f9d269aeb8ea7Lajos Molnar
2715205977929c8a63d3bba026c6bd7b4cc1e236627Dan Stoza    // Our BufferQueue interfaces. mProducer is passed to the producer through
2725205977929c8a63d3bba026c6bd7b4cc1e236627Dan Stoza    // getIGraphicBufferProducer, and mConsumer is used internally to retrieve
2735205977929c8a63d3bba026c6bd7b4cc1e236627Dan Stoza    // the buffers queued by the producer.
274d291c222357303b9611cab89d0c3b047584ef377Chong Zhang    bool mIsPersistent;
2755205977929c8a63d3bba026c6bd7b4cc1e236627Dan Stoza    sp<IGraphicBufferProducer> mProducer;
2765205977929c8a63d3bba026c6bd7b4cc1e236627Dan Stoza    sp<IGraphicBufferConsumer> mConsumer;
277f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
278f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Number of frames pending in BufferQueue that haven't yet been
279f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // forwarded to the codec.
280f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    size_t mNumFramesAvailable;
281f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
2829700f5fe4b3becfe858cbf5aa7964296975081bbChong Zhang    // Number of frames acquired from consumer (debug only)
2839700f5fe4b3becfe858cbf5aa7964296975081bbChong Zhang    int32_t mNumBufferAcquired;
2849700f5fe4b3becfe858cbf5aa7964296975081bbChong Zhang
285f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Set to true if we want to send end-of-stream after we run out of
286f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // frames in BufferQueue.
287f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    bool mEndOfStream;
288f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    bool mEndOfStreamSent;
289f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
290f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Cache of GraphicBuffers from the buffer queue.  When the codec
291f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // is done processing a GraphicBuffer, we can use this to map back
292f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // to a slot number.
293f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    sp<GraphicBuffer> mBufferSlot[BufferQueue::NUM_BUFFER_SLOTS];
294f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
295f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    // Tracks codec buffers.
296f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    Vector<CodecBuffer> mCodecBuffers;
297f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
298a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    ////
299ee4e1b1a63758941460ae79a064249d3a5189443Lajos Molnar    friend struct AHandlerReflector<GraphicBufferSource>;
300a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
301a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    enum {
302a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber        kWhatRepeatLastFrame,
303a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    };
30494ee4b708acfa941581160b267afb79192b1d816Chong Zhang    enum {
30594ee4b708acfa941581160b267afb79192b1d816Chong Zhang        kRepeatLastFrameCount = 10,
30694ee4b708acfa941581160b267afb79192b1d816Chong Zhang    };
30794ee4b708acfa941581160b267afb79192b1d816Chong Zhang
30894ee4b708acfa941581160b267afb79192b1d816Chong Zhang    KeyedVector<int64_t, int64_t> mOriginalTimeUs;
30972cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    int64_t mMaxTimestampGapUs;
31094ee4b708acfa941581160b267afb79192b1d816Chong Zhang    int64_t mPrevOriginalTimeUs;
31194ee4b708acfa941581160b267afb79192b1d816Chong Zhang    int64_t mPrevModifiedTimeUs;
31272cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    int64_t mSkipFramesBeforeNs;
313a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
31437b2b389139ed638831e49708c947863eef631efRonghua Wu    sp<FrameDropper> mFrameDropper;
31537b2b389139ed638831e49708c947863eef631efRonghua Wu
316a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    sp<ALooper> mLooper;
317a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    sp<AHandlerReflector<GraphicBufferSource> > mReflector;
318a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
31972cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang    int64_t mRepeatAfterUs;
320a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    int32_t mRepeatLastFrameGeneration;
32194ee4b708acfa941581160b267afb79192b1d816Chong Zhang    int64_t mRepeatLastFrameTimestamp;
32294ee4b708acfa941581160b267afb79192b1d816Chong Zhang    int32_t mRepeatLastFrameCount;
323a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
32437b2b389139ed638831e49708c947863eef631efRonghua Wu    int mLatestBufferId;
32537b2b389139ed638831e49708c947863eef631efRonghua Wu    uint64_t mLatestBufferFrameNum;
32637b2b389139ed638831e49708c947863eef631efRonghua Wu    int32_t mLatestBufferUseCount;
32715ab4996019387f27a48b81cb4774c21502bc0e5Lajos Molnar    sp<Fence> mLatestBufferFence;
328a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
32937b2b389139ed638831e49708c947863eef631efRonghua Wu    // The previous buffer should've been repeated but
330a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    // no codec buffer was available at the time.
331a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    bool mRepeatBufferDeferred;
332a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
3332c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    // Time lapse / slow motion configuration
3342c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    int64_t mTimePerCaptureUs;
3352c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    int64_t mTimePerFrameUs;
3362c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    int64_t mPrevCaptureUs;
3372c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang    int64_t mPrevFrameUs;
3382c9c8cba8562cc3a27532e4cd348912cc78d8d98Chong Zhang
339054219874873b41f1c815552987c10465c34ba2bLajos Molnar    MetadataBufferType mMetadataBufferType;
340dd81af7ef969981748f35ec839869d34ed0cc768Lajos Molnar    ColorAspects mColorAspects;
341512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar
342a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber    void onMessageReceived(const sp<AMessage> &msg);
343a61285dcf1da8a2cf40c499ee3a7b9fc4d74ac58Andreas Huber
344f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    DISALLOW_EVIL_CONSTRUCTORS(GraphicBufferSource);
345f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden};
346f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
347f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden}  // namespace android
348f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
349f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden#endif  // GRAPHIC_BUFFER_SOURCE_H_
350