SurfaceMediaSource.h revision a361483bb5dbd3bbf132c5b99b2df7d197c3fc50
13399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi/*
23399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * Copyright (C) 2011 The Android Open Source Project
33399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi *
43399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * Licensed under the Apache License, Version 2.0 (the "License");
53399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * you may not use this file except in compliance with the License.
63399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * You may obtain a copy of the License at
73399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi *
83399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi *      http://www.apache.org/licenses/LICENSE-2.0
93399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi *
103399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * Unless required by applicable law or agreed to in writing, software
113399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * distributed under the License is distributed on an "AS IS" BASIS,
123399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * See the License for the specific language governing permissions and
143399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * limitations under the License.
153399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi */
163399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
171a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi#ifndef ANDROID_GUI_SURFACEMEDIASOURCE_H
181a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi#define ANDROID_GUI_SURFACEMEDIASOURCE_H
193399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
203399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <gui/ISurfaceTexture.h>
213399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
223399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <utils/threads.h>
233399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <utils/Vector.h>
243399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <media/stagefright/MediaSource.h>
253399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <media/stagefright/MediaBuffer.h>
263399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
273399b7267185646c69b04352211fca4fad9d7547Pannag Sanketinamespace android {
283399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi// ----------------------------------------------------------------------------
293399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
303399b7267185646c69b04352211fca4fad9d7547Pannag Sanketiclass IGraphicBufferAlloc;
313399b7267185646c69b04352211fca4fad9d7547Pannag Sanketiclass String8;
323399b7267185646c69b04352211fca4fad9d7547Pannag Sanketiclass GraphicBuffer;
333399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
341a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketiclass SurfaceMediaSource : public BnSurfaceTexture, public MediaSource,
353399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                                            public MediaBufferObserver {
363399b7267185646c69b04352211fca4fad9d7547Pannag Sanketipublic:
373399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    enum { MIN_UNDEQUEUED_BUFFERS = 3 };
383399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    enum {
393399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        MIN_ASYNC_BUFFER_SLOTS = MIN_UNDEQUEUED_BUFFERS + 1,
403399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        MIN_SYNC_BUFFER_SLOTS  = MIN_UNDEQUEUED_BUFFERS
413399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    };
423399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    enum { NUM_BUFFER_SLOTS = 32 };
433399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    enum { NO_CONNECTED_API = 0 };
443399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
453399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    struct FrameAvailableListener : public virtual RefBase {
463399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // onFrameAvailable() is called from queueBuffer() is the FIFO is
471a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi        // empty. You can use SurfaceMediaSource::getQueuedCount() to
483399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // figure out if there are more frames waiting.
493399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // This is called without any lock held can be called concurrently by
503399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // multiple threads.
513399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        virtual void onFrameAvailable() = 0;
523399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    };
533399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
541a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    SurfaceMediaSource(uint32_t bufW, uint32_t bufH);
553399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
561a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    virtual ~SurfaceMediaSource();
573399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
583399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
593399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // For the MediaSource interface for use by StageFrightRecorder:
603399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual status_t start(MetaData *params = NULL);
613399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual status_t stop();
623399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual status_t read(
633399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            MediaBuffer **buffer, const ReadOptions *options = NULL);
643399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual sp<MetaData> getFormat();
653399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
66a361483bb5dbd3bbf132c5b99b2df7d197c3fc50Pannag Sanketi    // Pass the metadata over to the buffer, call when you have the lock
67a361483bb5dbd3bbf132c5b99b2df7d197c3fc50Pannag Sanketi    void passMetadataBufferLocked(MediaBuffer **buffer);
68a361483bb5dbd3bbf132c5b99b2df7d197c3fc50Pannag Sanketi    bool checkBufferMatchesSlot(int slot, MediaBuffer *buffer);
69a361483bb5dbd3bbf132c5b99b2df7d197c3fc50Pannag Sanketi
703399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // Get / Set the frame rate used for encoding. Default fps = 30
71b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    status_t setFrameRate(int32_t fps) ;
72b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    int32_t getFrameRate( ) const;
733399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
743399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // The call for the StageFrightRecorder to tell us that
753399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // it is done using the MediaBuffer data so that its state
763399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // can be set to FREE for dequeuing
773399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual void signalBufferReturned(MediaBuffer* buffer);
783399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // end of MediaSource interface
793399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
803399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    uint32_t getBufferCount( ) const { return mBufferCount;}
813399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
823399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
833399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // setBufferCount updates the number of available buffer slots.  After
843399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // calling this all buffer slots are both unallocated and owned by the
851a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    // SurfaceMediaSource object (i.e. they are not owned by the client).
863399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual status_t setBufferCount(int bufferCount);
873399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
883399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual sp<GraphicBuffer> requestBuffer(int buf);
893399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
903399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // dequeueBuffer gets the next buffer slot index for the client to use. If a
913399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // buffer slot is available then that slot index is written to the location
923399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // pointed to by the buf argument and a status of OK is returned.  If no
933399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // slot is available then a status of -EBUSY is returned and buf is
943399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // unmodified.
953399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual status_t dequeueBuffer(int *buf, uint32_t w, uint32_t h,
963399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            uint32_t format, uint32_t usage);
973399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
981a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    // queueBuffer returns a filled buffer to the SurfaceMediaSource. In addition, a
993399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // timestamp must be provided for the buffer. The timestamp is in
1003399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // nanoseconds, and must be monotonically increasing. Its other semantics
1013399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // (zero point, etc) are client-dependent and should be documented by the
1023399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // client.
1033399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual status_t queueBuffer(int buf, int64_t timestamp,
1043399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform);
1053399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual void cancelBuffer(int buf);
1063399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1073399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // onFrameReceivedLocked informs the buffer consumers (StageFrightRecorder)
1083399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // or listeners that a frame has been received
1093399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // The buffer is not made available for dequeueing immediately. We need to
1103399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // wait to hear from StageFrightRecorder to set the buffer FREE
1113399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // Make sure this is called when the mutex is locked
1123399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual status_t onFrameReceivedLocked();
1133399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1143399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual status_t setScalingMode(int mode) { } // no op for encoding
1153399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual int query(int what, int* value);
1163399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1173399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // Just confirming to the ISurfaceTexture interface as of now
1183399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual status_t setCrop(const Rect& reg) { return OK; }
1193399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual status_t setTransform(uint32_t transform) {return OK;}
1203399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1213399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // setSynchronousMode set whether dequeueBuffer is synchronous or
1223399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // asynchronous. In synchronous mode, dequeueBuffer blocks until
1233399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // a buffer is available, the currently bound buffer can be dequeued and
1243399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // queued buffers will be retired in order.
1253399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // The default mode is synchronous.
1263399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // TODO: Clarify the minute differences bet sycn /async
1273399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // modes (S.Encoder vis-a-vis SurfaceTexture)
1283399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual status_t setSynchronousMode(bool enabled);
1293399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1301a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    // connect attempts to connect a client API to the SurfaceMediaSource.  This
1313399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // must be called before any other ISurfaceTexture methods are called except
1323399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // for getAllocator.
1333399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    //
1343399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // This method will fail if the connect was previously called on the
1351a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    // SurfaceMediaSource and no corresponding disconnect call was made.
1363399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual status_t connect(int api);
1373399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1381a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    // disconnect attempts to disconnect a client API from the SurfaceMediaSource.
1393399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // Calling this method will cause any subsequent calls to other
1403399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // ISurfaceTexture methods to fail except for getAllocator and connect.
1413399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // Successfully calling connect after this will allow the other methods to
1423399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // succeed again.
1433399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    //
1441a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    // This method will fail if the the SurfaceMediaSource is not currently
1453399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // connected to the specified client API.
1463399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual status_t disconnect(int api);
1473399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1483399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // getqueuedCount returns the number of queued frames waiting in the
1493399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // FIFO. In asynchronous mode, this always returns 0 or 1 since
1503399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // frames are not accumulating in the FIFO.
1513399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    size_t getQueuedCount() const;
1523399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1533399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // setBufferCountServer set the buffer count. If the client has requested
1543399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // a buffer count using setBufferCount, the server-buffer count will
1553399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // take effect once the client sets the count back to zero.
1563399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    status_t setBufferCountServer(int bufferCount);
1573399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1583399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // getTimestamp retrieves the timestamp associated with the image
159a361483bb5dbd3bbf132c5b99b2df7d197c3fc50Pannag Sanketi    // set by the most recent call to read()
1603399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    //
1613399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // The timestamp is in nanoseconds, and is monotonically increasing. Its
1623399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // other semantics (zero point, etc) are source-dependent and should be
1633399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // documented by the source.
1643399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int64_t getTimestamp();
1653399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1663399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // setFrameAvailableListener sets the listener object that will be notified
1673399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // when a new frame becomes available.
1683399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    void setFrameAvailableListener(const sp<FrameAvailableListener>& listener);
1693399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1703399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // getAllocator retrieves the binder object that must be referenced as long
1711a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    // as the GraphicBuffers dequeued from this SurfaceMediaSource are referenced.
1723399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // Holding this binder reference prevents SurfaceFlinger from freeing the
1733399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // buffers before the client is done with them.
1743399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    sp<IBinder> getAllocator();
1753399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1763399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1773399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // getCurrentBuffer returns the buffer associated with the current image.
1783399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    sp<GraphicBuffer> getCurrentBuffer() const;
1793399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1803399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // dump our state in a String
1813399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    void dump(String8& result) const;
1823399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    void dump(String8& result, const char* prefix, char* buffer,
1833399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                                                    size_t SIZE) const;
1843399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
185b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // isMetaDataStoredInVideoBuffers tells the encoder whether we will
186b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    // pass metadata through the buffers. Currently, it is force set to true
187b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi    bool isMetaDataStoredInVideoBuffers() const;
188b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi
189b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketiprotected:
1903399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1913399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // freeAllBuffers frees the resources (both GraphicBuffer and EGLImage) for
1923399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // all slots.
1933399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    void freeAllBuffers();
1943399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    static bool isExternalFormat(uint32_t format);
1953399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1963399b7267185646c69b04352211fca4fad9d7547Pannag Sanketiprivate:
1973399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
1983399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    status_t setBufferCountServerLocked(int bufferCount);
1993399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2003399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    enum { INVALID_BUFFER_SLOT = -1 };
2013399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2023399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    struct BufferSlot {
2033399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2043399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        BufferSlot()
2053399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            : mBufferState(BufferSlot::FREE),
2063399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi              mRequestBufferCalled(false),
2073399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi              mTimestamp(0) {
2083399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        }
2093399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2103399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // mGraphicBuffer points to the buffer allocated for this slot or is
2113399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // NULL if no buffer has been allocated.
2123399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        sp<GraphicBuffer> mGraphicBuffer;
2133399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2143399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // BufferState represents the different states in which a buffer slot
2153399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // can be.
2163399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        enum BufferState {
2173399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // FREE indicates that the buffer is not currently being used and
2183399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // will not be used in the future until it gets dequeued and
2193399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // subseqently queued by the client.
2203399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            FREE = 0,
2213399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2223399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // DEQUEUED indicates that the buffer has been dequeued by the
2233399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // client, but has not yet been queued or canceled. The buffer is
2243399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // considered 'owned' by the client, and the server should not use
2253399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // it for anything.
2263399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            //
2273399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // Note that when in synchronous-mode (mSynchronousMode == true),
2283399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // the buffer that's currently attached to the texture may be
2293399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // dequeued by the client.  That means that the current buffer can
2303399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // be in either the DEQUEUED or QUEUED state.  In asynchronous mode,
2313399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // however, the current buffer is always in the QUEUED state.
2323399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            DEQUEUED = 1,
2333399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2343399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // QUEUED indicates that the buffer has been queued by the client,
2353399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // and has not since been made available for the client to dequeue.
2363399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // Attaching the buffer to the texture does NOT transition the
2373399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // buffer away from the QUEUED state. However, in Synchronous mode
2383399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // the current buffer may be dequeued by the client under some
2393399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // circumstances. See the note about the current buffer in the
2403399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            // documentation for DEQUEUED.
2413399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            QUEUED = 2,
2423399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        };
2433399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2443399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // mBufferState is the current state of this buffer slot.
2453399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        BufferState mBufferState;
2463399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2473399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // mRequestBufferCalled is used for validating that the client did
2483399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // call requestBuffer() when told to do so. Technically this is not
2493399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // needed but useful for debugging and catching client bugs.
2503399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        bool mRequestBufferCalled;
2513399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2523399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // mTimestamp is the current timestamp for this buffer slot. This gets
2533399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // to set by queueBuffer each time this slot is queued.
2543399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        int64_t mTimestamp;
2553399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    };
2563399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2573399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mSlots is the array of buffer slots that must be mirrored on the client
2583399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // side. This allows buffer ownership to be transferred between the client
2593399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // and server without sending a GraphicBuffer over binder. The entire array
2603399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // is initialized to NULL at construction time, and buffers are allocated
2613399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // for a slot when requestBuffer is called with that slot's index.
2623399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    BufferSlot mSlots[NUM_BUFFER_SLOTS];
2633399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2643399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mDefaultWidth holds the default width of allocated buffers. It is used
2653399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // in requestBuffers() if a width and height of zero is specified.
2663399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    uint32_t mDefaultWidth;
2673399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2683399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mDefaultHeight holds the default height of allocated buffers. It is used
2693399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // in requestBuffers() if a width and height of zero is specified.
2703399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    uint32_t mDefaultHeight;
2713399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2723399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mPixelFormat holds the pixel format of allocated buffers. It is used
2733399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // in requestBuffers() if a format of zero is specified.
2743399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    uint32_t mPixelFormat;
2753399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2763399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mBufferCount is the number of buffer slots that the client and server
2773399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // must maintain. It defaults to MIN_ASYNC_BUFFER_SLOTS and can be changed
2783399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // by calling setBufferCount or setBufferCountServer
2793399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int mBufferCount;
2803399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2813399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mClientBufferCount is the number of buffer slots requested by the
2823399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // client. The default is zero, which means the client doesn't care how
2833399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // many buffers there are
2843399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int mClientBufferCount;
2853399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2863399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mServerBufferCount buffer count requested by the server-side
2873399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int mServerBufferCount;
2883399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2893399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mCurrentSlot is the buffer slot index of the buffer that is currently
2903399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // being used by buffer consumer
2911a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    // (e.g. StageFrightRecorder in the case of SurfaceMediaSource or GLTexture
2923399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // in the case of SurfaceTexture).
2933399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // It is initialized to INVALID_BUFFER_SLOT,
2943399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // indicating that no buffer slot is currently bound to the texture. Note,
2953399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
2963399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // that no buffer is bound to the texture. A call to setBufferCount will
2973399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // reset mCurrentTexture to INVALID_BUFFER_SLOT.
2983399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int mCurrentSlot;
2993399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3003399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3013399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mCurrentBuf is the graphic buffer of the current slot to be used by
3023399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // buffer consumer. It's possible that this buffer is not associated
3033399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // with any buffer slot, so we must track it separately in order to
3043399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // properly use IGraphicBufferAlloc::freeAllGraphicBuffersExcept.
3053399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    sp<GraphicBuffer> mCurrentBuf;
3063399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3073399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3083399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mCurrentTimestamp is the timestamp for the current texture. It
3093399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // gets set to mLastQueuedTimestamp each time updateTexImage is called.
3103399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int64_t mCurrentTimestamp;
3113399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3123399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
3133399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // allocate new GraphicBuffer objects.
3143399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
3153399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3163399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mFrameAvailableListener is the listener object that will be called when a
3173399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // new frame becomes available. If it is not NULL it will be called from
3183399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // queueBuffer.
3193399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    sp<FrameAvailableListener> mFrameAvailableListener;
3203399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3213399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mSynchronousMode whether we're in synchronous mode or not
3223399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    bool mSynchronousMode;
3233399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3243399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mConnectedApi indicates the API that is currently connected to this
3253399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // SurfaceTexture.  It defaults to NO_CONNECTED_API (= 0), and gets updated
3263399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // by the connect and disconnect methods.
3273399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int mConnectedApi;
3283399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3293399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mDequeueCondition condition used for dequeueBuffer in synchronous mode
3303399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    mutable Condition mDequeueCondition;
3313399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3323399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3333399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mQueue is a FIFO of queued buffers used in synchronous mode
3343399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    typedef Vector<int> Fifo;
3353399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    Fifo mQueue;
3363399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3373399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mMutex is the mutex used to prevent concurrent access to the member
3381a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    // variables of SurfaceMediaSource objects. It must be locked whenever the
3393399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // member variables are accessed.
3403399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    mutable Mutex mMutex;
3413399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3423399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    ////////////////////////// For MediaSource
3433399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // Set to a default of 30 fps if not specified by the client side
3443399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int32_t mFrameRate;
3453399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3463399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mStarted is a flag to check if the recording has started
3473399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    bool mStarted;
3483399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3493399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // mFrameAvailableCondition condition used to indicate whether there
3503399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // is a frame available for dequeuing
3513399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    Condition mFrameAvailableCondition;
3523399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    Condition mFrameCompleteCondition;
3533399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3543399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // Avoid copying and equating and default constructor
3551a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    DISALLOW_IMPLICIT_CONSTRUCTORS(SurfaceMediaSource);
3563399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi};
3573399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3583399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi// ----------------------------------------------------------------------------
3593399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}; // namespace android
3603399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3611a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi#endif // ANDROID_GUI_SURFACEMEDIASOURCE_H
362