1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ANDROID_GUI_SURFACEMEDIASOURCE_H 18 #define ANDROID_GUI_SURFACEMEDIASOURCE_H 19 20 #include <gui/ISurfaceTexture.h> 21 22 #include <utils/threads.h> 23 #include <utils/Vector.h> 24 #include <media/stagefright/MediaSource.h> 25 #include <media/stagefright/MediaBuffer.h> 26 27 namespace android { 28 // ---------------------------------------------------------------------------- 29 30 class IGraphicBufferAlloc; 31 class String8; 32 class GraphicBuffer; 33 34 class SurfaceMediaSource : public BnSurfaceTexture, public MediaSource, 35 public MediaBufferObserver { 36 public: 37 enum { MIN_UNDEQUEUED_BUFFERS = 4 }; 38 enum { 39 MIN_ASYNC_BUFFER_SLOTS = MIN_UNDEQUEUED_BUFFERS + 1, 40 MIN_SYNC_BUFFER_SLOTS = MIN_UNDEQUEUED_BUFFERS 41 }; 42 enum { NUM_BUFFER_SLOTS = 32 }; 43 enum { NO_CONNECTED_API = 0 }; 44 45 struct FrameAvailableListener : public virtual RefBase { 46 // onFrameAvailable() is called from queueBuffer() is the FIFO is 47 // empty. You can use SurfaceMediaSource::getQueuedCount() to 48 // figure out if there are more frames waiting. 49 // This is called without any lock held can be called concurrently by 50 // multiple threads. 51 virtual void onFrameAvailable() = 0; 52 }; 53 54 SurfaceMediaSource(uint32_t bufW, uint32_t bufH); 55 56 virtual ~SurfaceMediaSource(); 57 58 59 // For the MediaSource interface for use by StageFrightRecorder: 60 virtual status_t start(MetaData *params = NULL); 61 virtual status_t stop(); 62 virtual status_t read( 63 MediaBuffer **buffer, const ReadOptions *options = NULL); 64 virtual sp<MetaData> getFormat(); 65 66 // Pass the metadata over to the buffer, call when you have the lock 67 void passMetadataBufferLocked(MediaBuffer **buffer); 68 bool checkBufferMatchesSlot(int slot, MediaBuffer *buffer); 69 70 // Get / Set the frame rate used for encoding. Default fps = 30 71 status_t setFrameRate(int32_t fps) ; 72 int32_t getFrameRate( ) const; 73 74 // The call for the StageFrightRecorder to tell us that 75 // it is done using the MediaBuffer data so that its state 76 // can be set to FREE for dequeuing 77 virtual void signalBufferReturned(MediaBuffer* buffer); 78 // end of MediaSource interface 79 getBufferCount()80 uint32_t getBufferCount( ) const { return mBufferCount;} 81 82 83 // setBufferCount updates the number of available buffer slots. After 84 // calling this all buffer slots are both unallocated and owned by the 85 // SurfaceMediaSource object (i.e. they are not owned by the client). 86 virtual status_t setBufferCount(int bufferCount); 87 88 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf); 89 90 // dequeueBuffer gets the next buffer slot index for the client to use. If a 91 // buffer slot is available then that slot index is written to the location 92 // pointed to by the buf argument and a status of OK is returned. If no 93 // slot is available then a status of -EBUSY is returned and buf is 94 // unmodified. 95 virtual status_t dequeueBuffer(int *buf, uint32_t w, uint32_t h, 96 uint32_t format, uint32_t usage); 97 98 // queueBuffer returns a filled buffer to the SurfaceMediaSource. In addition, a 99 // timestamp must be provided for the buffer. The timestamp is in 100 // nanoseconds, and must be monotonically increasing. Its other semantics 101 // (zero point, etc) are client-dependent and should be documented by the 102 // client. 103 virtual status_t queueBuffer(int buf, int64_t timestamp, 104 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform); 105 virtual void cancelBuffer(int buf); 106 107 // onFrameReceivedLocked informs the buffer consumers (StageFrightRecorder) 108 // or listeners that a frame has been received 109 // The buffer is not made available for dequeueing immediately. We need to 110 // wait to hear from StageFrightRecorder to set the buffer FREE 111 // Make sure this is called when the mutex is locked 112 virtual status_t onFrameReceivedLocked(); 113 setScalingMode(int mode)114 virtual status_t setScalingMode(int mode) { } // no op for encoding 115 virtual int query(int what, int* value); 116 117 // Just confirming to the ISurfaceTexture interface as of now setCrop(const Rect & reg)118 virtual status_t setCrop(const Rect& reg) { return OK; } setTransform(uint32_t transform)119 virtual status_t setTransform(uint32_t transform) {return OK;} 120 121 // setSynchronousMode set whether dequeueBuffer is synchronous or 122 // asynchronous. In synchronous mode, dequeueBuffer blocks until 123 // a buffer is available, the currently bound buffer can be dequeued and 124 // queued buffers will be retired in order. 125 // The default mode is synchronous. 126 // TODO: Clarify the minute differences bet sycn /async 127 // modes (S.Encoder vis-a-vis SurfaceTexture) 128 virtual status_t setSynchronousMode(bool enabled); 129 130 // connect attempts to connect a client API to the SurfaceMediaSource. This 131 // must be called before any other ISurfaceTexture methods are called except 132 // for getAllocator. 133 // 134 // This method will fail if the connect was previously called on the 135 // SurfaceMediaSource and no corresponding disconnect call was made. 136 virtual status_t connect(int api, 137 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform); 138 139 // disconnect attempts to disconnect a client API from the SurfaceMediaSource. 140 // Calling this method will cause any subsequent calls to other 141 // ISurfaceTexture methods to fail except for getAllocator and connect. 142 // Successfully calling connect after this will allow the other methods to 143 // succeed again. 144 // 145 // This method will fail if the the SurfaceMediaSource is not currently 146 // connected to the specified client API. 147 virtual status_t disconnect(int api); 148 149 // getqueuedCount returns the number of queued frames waiting in the 150 // FIFO. In asynchronous mode, this always returns 0 or 1 since 151 // frames are not accumulating in the FIFO. 152 size_t getQueuedCount() const; 153 154 // setBufferCountServer set the buffer count. If the client has requested 155 // a buffer count using setBufferCount, the server-buffer count will 156 // take effect once the client sets the count back to zero. 157 status_t setBufferCountServer(int bufferCount); 158 159 // getTimestamp retrieves the timestamp associated with the image 160 // set by the most recent call to read() 161 // 162 // The timestamp is in nanoseconds, and is monotonically increasing. Its 163 // other semantics (zero point, etc) are source-dependent and should be 164 // documented by the source. 165 int64_t getTimestamp(); 166 167 // setFrameAvailableListener sets the listener object that will be notified 168 // when a new frame becomes available. 169 void setFrameAvailableListener(const sp<FrameAvailableListener>& listener); 170 171 // getCurrentBuffer returns the buffer associated with the current image. 172 sp<GraphicBuffer> getCurrentBuffer() const; 173 174 // dump our state in a String 175 void dump(String8& result) const; 176 void dump(String8& result, const char* prefix, char* buffer, 177 size_t SIZE) const; 178 179 // isMetaDataStoredInVideoBuffers tells the encoder whether we will 180 // pass metadata through the buffers. Currently, it is force set to true 181 bool isMetaDataStoredInVideoBuffers() const; 182 183 protected: 184 185 // freeAllBuffersLocked frees the resources (both GraphicBuffer and EGLImage) for 186 // all slots. 187 void freeAllBuffersLocked(); 188 static bool isExternalFormat(uint32_t format); 189 190 private: 191 192 status_t setBufferCountServerLocked(int bufferCount); 193 194 enum { INVALID_BUFFER_SLOT = -1 }; 195 196 struct BufferSlot { 197 BufferSlotBufferSlot198 BufferSlot() 199 : mBufferState(BufferSlot::FREE), 200 mRequestBufferCalled(false), 201 mTimestamp(0) { 202 } 203 204 // mGraphicBuffer points to the buffer allocated for this slot or is 205 // NULL if no buffer has been allocated. 206 sp<GraphicBuffer> mGraphicBuffer; 207 208 // BufferState represents the different states in which a buffer slot 209 // can be. 210 enum BufferState { 211 // FREE indicates that the buffer is not currently being used and 212 // will not be used in the future until it gets dequeued and 213 // subseqently queued by the client. 214 FREE = 0, 215 216 // DEQUEUED indicates that the buffer has been dequeued by the 217 // client, but has not yet been queued or canceled. The buffer is 218 // considered 'owned' by the client, and the server should not use 219 // it for anything. 220 // 221 // Note that when in synchronous-mode (mSynchronousMode == true), 222 // the buffer that's currently attached to the texture may be 223 // dequeued by the client. That means that the current buffer can 224 // be in either the DEQUEUED or QUEUED state. In asynchronous mode, 225 // however, the current buffer is always in the QUEUED state. 226 DEQUEUED = 1, 227 228 // QUEUED indicates that the buffer has been queued by the client, 229 // and has not since been made available for the client to dequeue. 230 // Attaching the buffer to the texture does NOT transition the 231 // buffer away from the QUEUED state. However, in Synchronous mode 232 // the current buffer may be dequeued by the client under some 233 // circumstances. See the note about the current buffer in the 234 // documentation for DEQUEUED. 235 QUEUED = 2, 236 }; 237 238 // mBufferState is the current state of this buffer slot. 239 BufferState mBufferState; 240 241 // mRequestBufferCalled is used for validating that the client did 242 // call requestBuffer() when told to do so. Technically this is not 243 // needed but useful for debugging and catching client bugs. 244 bool mRequestBufferCalled; 245 246 // mTimestamp is the current timestamp for this buffer slot. This gets 247 // to set by queueBuffer each time this slot is queued. 248 int64_t mTimestamp; 249 }; 250 251 // mSlots is the array of buffer slots that must be mirrored on the client 252 // side. This allows buffer ownership to be transferred between the client 253 // and server without sending a GraphicBuffer over binder. The entire array 254 // is initialized to NULL at construction time, and buffers are allocated 255 // for a slot when requestBuffer is called with that slot's index. 256 BufferSlot mSlots[NUM_BUFFER_SLOTS]; 257 258 // mDefaultWidth holds the default width of allocated buffers. It is used 259 // in requestBuffers() if a width and height of zero is specified. 260 uint32_t mDefaultWidth; 261 262 // mDefaultHeight holds the default height of allocated buffers. It is used 263 // in requestBuffers() if a width and height of zero is specified. 264 uint32_t mDefaultHeight; 265 266 // mPixelFormat holds the pixel format of allocated buffers. It is used 267 // in requestBuffers() if a format of zero is specified. 268 uint32_t mPixelFormat; 269 270 // mBufferCount is the number of buffer slots that the client and server 271 // must maintain. It defaults to MIN_ASYNC_BUFFER_SLOTS and can be changed 272 // by calling setBufferCount or setBufferCountServer 273 int mBufferCount; 274 275 // mClientBufferCount is the number of buffer slots requested by the 276 // client. The default is zero, which means the client doesn't care how 277 // many buffers there are 278 int mClientBufferCount; 279 280 // mServerBufferCount buffer count requested by the server-side 281 int mServerBufferCount; 282 283 // mCurrentSlot is the buffer slot index of the buffer that is currently 284 // being used by buffer consumer 285 // (e.g. StageFrightRecorder in the case of SurfaceMediaSource or GLTexture 286 // in the case of SurfaceTexture). 287 // It is initialized to INVALID_BUFFER_SLOT, 288 // indicating that no buffer slot is currently bound to the texture. Note, 289 // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean 290 // that no buffer is bound to the texture. A call to setBufferCount will 291 // reset mCurrentTexture to INVALID_BUFFER_SLOT. 292 int mCurrentSlot; 293 294 295 // mCurrentBuf is the graphic buffer of the current slot to be used by 296 // buffer consumer. It's possible that this buffer is not associated 297 // with any buffer slot, so we must track it separately in order to 298 // properly use IGraphicBufferAlloc::freeAllGraphicBuffersExcept. 299 sp<GraphicBuffer> mCurrentBuf; 300 301 302 // mCurrentTimestamp is the timestamp for the current texture. It 303 // gets set to mLastQueuedTimestamp each time updateTexImage is called. 304 int64_t mCurrentTimestamp; 305 306 // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to 307 // allocate new GraphicBuffer objects. 308 sp<IGraphicBufferAlloc> mGraphicBufferAlloc; 309 310 // mFrameAvailableListener is the listener object that will be called when a 311 // new frame becomes available. If it is not NULL it will be called from 312 // queueBuffer. 313 sp<FrameAvailableListener> mFrameAvailableListener; 314 315 // mSynchronousMode whether we're in synchronous mode or not 316 bool mSynchronousMode; 317 318 // mConnectedApi indicates the API that is currently connected to this 319 // SurfaceTexture. It defaults to NO_CONNECTED_API (= 0), and gets updated 320 // by the connect and disconnect methods. 321 int mConnectedApi; 322 323 // mDequeueCondition condition used for dequeueBuffer in synchronous mode 324 mutable Condition mDequeueCondition; 325 326 327 // mQueue is a FIFO of queued buffers used in synchronous mode 328 typedef Vector<int> Fifo; 329 Fifo mQueue; 330 331 // mMutex is the mutex used to prevent concurrent access to the member 332 // variables of SurfaceMediaSource objects. It must be locked whenever the 333 // member variables are accessed. 334 mutable Mutex mMutex; 335 336 ////////////////////////// For MediaSource 337 // Set to a default of 30 fps if not specified by the client side 338 int32_t mFrameRate; 339 340 // mStopped is a flag to check if the recording is going on 341 bool mStopped; 342 343 // mNumFramesReceived indicates the number of frames recieved from 344 // the client side 345 int mNumFramesReceived; 346 // mNumFramesEncoded indicates the number of frames passed on to the 347 // encoder 348 int mNumFramesEncoded; 349 350 // mFirstFrameTimestamp is the timestamp of the first received frame. 351 // It is used to offset the output timestamps so recording starts at time 0. 352 int64_t mFirstFrameTimestamp; 353 // mStartTimeNs is the start time passed into the source at start, used to 354 // offset timestamps. 355 int64_t mStartTimeNs; 356 357 // mFrameAvailableCondition condition used to indicate whether there 358 // is a frame available for dequeuing 359 Condition mFrameAvailableCondition; 360 Condition mFrameCompleteCondition; 361 362 // Avoid copying and equating and default constructor 363 DISALLOW_IMPLICIT_CONSTRUCTORS(SurfaceMediaSource); 364 }; 365 366 // ---------------------------------------------------------------------------- 367 }; // namespace android 368 369 #endif // ANDROID_GUI_SURFACEMEDIASOURCE_H 370