SurfaceMediaSource.h revision df4a59c61103e7e0e9332e167a0be67da5d82ff3
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 8829a142c7237821b6dc7bd1e8b56bb1efdc56767bJamie Gennis virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* 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. 136820a509687599edb8ff1a7577de8b9295f416263Mathias Agopian virtual status_t connect(int api, 137820a509687599edb8ff1a7577de8b9295f416263Mathias Agopian uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform); 1383399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 1391a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi // disconnect attempts to disconnect a client API from the SurfaceMediaSource. 1403399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // Calling this method will cause any subsequent calls to other 1413399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // ISurfaceTexture methods to fail except for getAllocator and connect. 1423399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // Successfully calling connect after this will allow the other methods to 1433399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // succeed again. 1443399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // 1451a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi // This method will fail if the the SurfaceMediaSource is not currently 1463399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // connected to the specified client API. 1473399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi virtual status_t disconnect(int api); 1483399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 1493399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // getqueuedCount returns the number of queued frames waiting in the 1503399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // FIFO. In asynchronous mode, this always returns 0 or 1 since 1513399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // frames are not accumulating in the FIFO. 1523399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi size_t getQueuedCount() const; 1533399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 1543399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // setBufferCountServer set the buffer count. If the client has requested 1553399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // a buffer count using setBufferCount, the server-buffer count will 1563399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // take effect once the client sets the count back to zero. 1573399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi status_t setBufferCountServer(int bufferCount); 1583399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 1593399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // getTimestamp retrieves the timestamp associated with the image 160a361483bb5dbd3bbf132c5b99b2df7d197c3fc50Pannag Sanketi // set by the most recent call to read() 1613399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // 1623399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // The timestamp is in nanoseconds, and is monotonically increasing. Its 1633399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // other semantics (zero point, etc) are source-dependent and should be 1643399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // documented by the source. 1653399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi int64_t getTimestamp(); 1663399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 1673399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // setFrameAvailableListener sets the listener object that will be notified 1683399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // when a new frame becomes available. 1693399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi void setFrameAvailableListener(const sp<FrameAvailableListener>& listener); 1703399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 1713399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // getCurrentBuffer returns the buffer associated with the current image. 1723399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi sp<GraphicBuffer> getCurrentBuffer() const; 1733399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 1743399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // dump our state in a String 1753399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi void dump(String8& result) const; 1763399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi void dump(String8& result, const char* prefix, char* buffer, 1773399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi size_t SIZE) const; 1783399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 179b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi // isMetaDataStoredInVideoBuffers tells the encoder whether we will 180b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi // pass metadata through the buffers. Currently, it is force set to true 181b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi bool isMetaDataStoredInVideoBuffers() const; 182b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketi 183b33f3407bab0970a7f9241680723a1140b177c50Pannag Sanketiprotected: 1843399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 1850c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi // freeAllBuffersLocked frees the resources (both GraphicBuffer and EGLImage) for 1863399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // all slots. 1870c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi void freeAllBuffersLocked(); 1883399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi static bool isExternalFormat(uint32_t format); 1893399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 1903399b7267185646c69b04352211fca4fad9d7547Pannag Sanketiprivate: 1913399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 1923399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi status_t setBufferCountServerLocked(int bufferCount); 1933399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 1943399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi enum { INVALID_BUFFER_SLOT = -1 }; 1953399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 1963399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi struct BufferSlot { 1973399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 1983399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi BufferSlot() 1993399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi : mBufferState(BufferSlot::FREE), 2003399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi mRequestBufferCalled(false), 2013399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi mTimestamp(0) { 2023399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi } 2033399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2043399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mGraphicBuffer points to the buffer allocated for this slot or is 2053399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // NULL if no buffer has been allocated. 2063399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi sp<GraphicBuffer> mGraphicBuffer; 2073399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2083399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // BufferState represents the different states in which a buffer slot 2093399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // can be. 2103399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi enum BufferState { 2113399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // FREE indicates that the buffer is not currently being used and 2123399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // will not be used in the future until it gets dequeued and 2133399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // subseqently queued by the client. 2143399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi FREE = 0, 2153399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2163399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // DEQUEUED indicates that the buffer has been dequeued by the 2173399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // client, but has not yet been queued or canceled. The buffer is 2183399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // considered 'owned' by the client, and the server should not use 2193399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // it for anything. 2203399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // 2213399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // Note that when in synchronous-mode (mSynchronousMode == true), 2223399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // the buffer that's currently attached to the texture may be 2233399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // dequeued by the client. That means that the current buffer can 2243399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // be in either the DEQUEUED or QUEUED state. In asynchronous mode, 2253399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // however, the current buffer is always in the QUEUED state. 2263399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi DEQUEUED = 1, 2273399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2283399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // QUEUED indicates that the buffer has been queued by the client, 2293399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // and has not since been made available for the client to dequeue. 2303399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // Attaching the buffer to the texture does NOT transition the 2313399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // buffer away from the QUEUED state. However, in Synchronous mode 2323399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // the current buffer may be dequeued by the client under some 2333399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // circumstances. See the note about the current buffer in the 2343399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // documentation for DEQUEUED. 2353399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi QUEUED = 2, 2363399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi }; 2373399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2383399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mBufferState is the current state of this buffer slot. 2393399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi BufferState mBufferState; 2403399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2413399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mRequestBufferCalled is used for validating that the client did 2423399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // call requestBuffer() when told to do so. Technically this is not 2433399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // needed but useful for debugging and catching client bugs. 2443399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi bool mRequestBufferCalled; 2453399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2463399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mTimestamp is the current timestamp for this buffer slot. This gets 2473399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // to set by queueBuffer each time this slot is queued. 2483399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi int64_t mTimestamp; 2493399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi }; 2503399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2513399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mSlots is the array of buffer slots that must be mirrored on the client 2523399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // side. This allows buffer ownership to be transferred between the client 2533399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // and server without sending a GraphicBuffer over binder. The entire array 2543399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // is initialized to NULL at construction time, and buffers are allocated 2553399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // for a slot when requestBuffer is called with that slot's index. 2563399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi BufferSlot mSlots[NUM_BUFFER_SLOTS]; 2573399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2583399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mDefaultWidth holds the default width of allocated buffers. It is used 2593399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // in requestBuffers() if a width and height of zero is specified. 2603399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi uint32_t mDefaultWidth; 2613399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2623399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mDefaultHeight holds the default height of allocated buffers. It is used 2633399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // in requestBuffers() if a width and height of zero is specified. 2643399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi uint32_t mDefaultHeight; 2653399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2663399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mPixelFormat holds the pixel format of allocated buffers. It is used 2673399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // in requestBuffers() if a format of zero is specified. 2683399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi uint32_t mPixelFormat; 2693399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2703399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mBufferCount is the number of buffer slots that the client and server 2713399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // must maintain. It defaults to MIN_ASYNC_BUFFER_SLOTS and can be changed 2723399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // by calling setBufferCount or setBufferCountServer 2733399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi int mBufferCount; 2743399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2753399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mClientBufferCount is the number of buffer slots requested by the 2763399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // client. The default is zero, which means the client doesn't care how 2773399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // many buffers there are 2783399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi int mClientBufferCount; 2793399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2803399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mServerBufferCount buffer count requested by the server-side 2813399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi int mServerBufferCount; 2823399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2833399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mCurrentSlot is the buffer slot index of the buffer that is currently 2843399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // being used by buffer consumer 2851a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi // (e.g. StageFrightRecorder in the case of SurfaceMediaSource or GLTexture 2863399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // in the case of SurfaceTexture). 2873399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // It is initialized to INVALID_BUFFER_SLOT, 2883399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // indicating that no buffer slot is currently bound to the texture. Note, 2893399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean 2903399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // that no buffer is bound to the texture. A call to setBufferCount will 2913399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // reset mCurrentTexture to INVALID_BUFFER_SLOT. 2923399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi int mCurrentSlot; 2933399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2943399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 2953399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mCurrentBuf is the graphic buffer of the current slot to be used by 2963399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // buffer consumer. It's possible that this buffer is not associated 2973399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // with any buffer slot, so we must track it separately in order to 2983399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // properly use IGraphicBufferAlloc::freeAllGraphicBuffersExcept. 2993399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi sp<GraphicBuffer> mCurrentBuf; 3003399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 3013399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 3023399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mCurrentTimestamp is the timestamp for the current texture. It 3033399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // gets set to mLastQueuedTimestamp each time updateTexImage is called. 3043399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi int64_t mCurrentTimestamp; 3053399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 3063399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to 3073399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // allocate new GraphicBuffer objects. 3083399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi sp<IGraphicBufferAlloc> mGraphicBufferAlloc; 3093399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 3103399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mFrameAvailableListener is the listener object that will be called when a 3113399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // new frame becomes available. If it is not NULL it will be called from 3123399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // queueBuffer. 3133399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi sp<FrameAvailableListener> mFrameAvailableListener; 3143399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 3153399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mSynchronousMode whether we're in synchronous mode or not 3163399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi bool mSynchronousMode; 3173399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 3183399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mConnectedApi indicates the API that is currently connected to this 3193399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // SurfaceTexture. It defaults to NO_CONNECTED_API (= 0), and gets updated 3203399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // by the connect and disconnect methods. 3213399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi int mConnectedApi; 3223399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 3233399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mDequeueCondition condition used for dequeueBuffer in synchronous mode 3243399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi mutable Condition mDequeueCondition; 3253399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 3263399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 3273399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mQueue is a FIFO of queued buffers used in synchronous mode 3283399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi typedef Vector<int> Fifo; 3293399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi Fifo mQueue; 3303399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 3313399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mMutex is the mutex used to prevent concurrent access to the member 3321a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi // variables of SurfaceMediaSource objects. It must be locked whenever the 3333399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // member variables are accessed. 3343399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi mutable Mutex mMutex; 3353399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 3363399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi ////////////////////////// For MediaSource 3373399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // Set to a default of 30 fps if not specified by the client side 3383399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi int32_t mFrameRate; 3393399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 3400c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi // mStopped is a flag to check if the recording is going on 3410c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi bool mStopped; 3420c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi 3430c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi // mNumFramesReceived indicates the number of frames recieved from 3440c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi // the client side 3450c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi int mNumFramesReceived; 3460c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi // mNumFramesEncoded indicates the number of frames passed on to the 3470c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi // encoder 3480c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi int mNumFramesEncoded; 3493399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 350df4a59c61103e7e0e9332e167a0be67da5d82ff3Eino-Ville Talvala // mFirstFrameTimestamp is the timestamp of the first received frame. 351df4a59c61103e7e0e9332e167a0be67da5d82ff3Eino-Ville Talvala // It is used to offset the output timestamps so recording starts at time 0. 352df4a59c61103e7e0e9332e167a0be67da5d82ff3Eino-Ville Talvala int64_t mFirstFrameTimestamp; 353df4a59c61103e7e0e9332e167a0be67da5d82ff3Eino-Ville Talvala // mStartTimeNs is the start time passed into the source at start, used to 354df4a59c61103e7e0e9332e167a0be67da5d82ff3Eino-Ville Talvala // offset timestamps. 355df4a59c61103e7e0e9332e167a0be67da5d82ff3Eino-Ville Talvala int64_t mStartTimeNs; 356df4a59c61103e7e0e9332e167a0be67da5d82ff3Eino-Ville Talvala 3573399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // mFrameAvailableCondition condition used to indicate whether there 3583399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // is a frame available for dequeuing 3593399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi Condition mFrameAvailableCondition; 3603399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi Condition mFrameCompleteCondition; 3613399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 3623399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi // Avoid copying and equating and default constructor 3631a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi DISALLOW_IMPLICIT_CONSTRUCTORS(SurfaceMediaSource); 3643399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}; 3653399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 3663399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi// ---------------------------------------------------------------------------- 3673399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}; // namespace android 3683399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi 3691a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi#endif // ANDROID_GUI_SURFACEMEDIASOURCE_H 370