Eino-Ville Talvala | 78822d7 | 2012-07-18 17:52:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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_SERVERS_CAMERA_MEDIACONSUMER_H |
| 18 | #define ANDROID_SERVERS_CAMERA_MEDIACONSUMER_H |
| 19 | |
| 20 | #include <gui/BufferQueue.h> |
| 21 | |
| 22 | #include <ui/GraphicBuffer.h> |
| 23 | |
| 24 | #include <utils/String8.h> |
| 25 | #include <utils/Vector.h> |
| 26 | #include <utils/threads.h> |
| 27 | |
| 28 | #define ANDROID_GRAPHICS_MEDIACONSUMER_JNI_ID "mMediaConsumer" |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | /** |
| 33 | * MediaConsumer is a BufferQueue consumer endpoint that makes it |
| 34 | * straightforward to bridge Camera 2 to the existing media recording framework. |
| 35 | * This queue is synchronous by default. |
| 36 | * |
| 37 | * TODO: This is a temporary replacement for the full camera->media recording |
| 38 | * path using SurfaceMediaEncoder or equivalent. |
| 39 | */ |
| 40 | |
| 41 | class MediaConsumer: public virtual RefBase, |
| 42 | protected BufferQueue::ConsumerListener |
| 43 | { |
| 44 | public: |
| 45 | struct FrameAvailableListener : public virtual RefBase { |
| 46 | // onFrameAvailable() is called each time an additional frame becomes |
| 47 | // available for consumption. A new frame queued will always trigger the |
| 48 | // callback, whether the queue is empty or not. |
| 49 | // |
| 50 | // This is called without any lock held and can be called concurrently |
| 51 | // by multiple threads. |
| 52 | virtual void onFrameAvailable() = 0; |
| 53 | }; |
| 54 | |
| 55 | // Create a new media consumer. The maxBuffers parameter specifies |
| 56 | // how many buffers can be locked for user access at the same time. |
| 57 | MediaConsumer(uint32_t maxBuffers); |
| 58 | |
| 59 | virtual ~MediaConsumer(); |
| 60 | |
| 61 | // set the name of the MediaConsumer that will be used to identify it in |
| 62 | // log messages. |
| 63 | void setName(const String8& name); |
| 64 | |
| 65 | // Gets the next graphics buffer from the producer. Returns BAD_VALUE if no |
| 66 | // new buffer is available, and INVALID_OPERATION if the maximum number of |
| 67 | // buffers is already in use. |
| 68 | // |
| 69 | // Only a fixed number of buffers can be available at a time, determined by |
| 70 | // the construction-time maxBuffers parameter. If INVALID_OPERATION is |
| 71 | // returned by getNextBuffer, then old buffers must be returned to the |
| 72 | // queue by calling freeBuffer before more buffers can be acquired. |
| 73 | status_t getNextBuffer(buffer_handle_t *buffer, nsecs_t *timestamp); |
| 74 | |
| 75 | // Returns a buffer to the queue, allowing it to be reused. Since |
| 76 | // only a fixed number of buffers may be locked at a time, old buffers must |
| 77 | // be released by calling unlockBuffer to ensure new buffers can be acquired by |
| 78 | // lockNextBuffer. |
| 79 | status_t freeBuffer(buffer_handle_t buffer); |
| 80 | |
| 81 | // setFrameAvailableListener sets the listener object that will be notified |
| 82 | // when a new frame becomes available. |
| 83 | void setFrameAvailableListener(const sp<FrameAvailableListener>& listener); |
| 84 | |
| 85 | sp<ISurfaceTexture> getProducerInterface() const { return mBufferQueue; } |
| 86 | protected: |
| 87 | |
| 88 | // Implementation of the BufferQueue::ConsumerListener interface. These |
| 89 | // calls are used to notify the MediaConsumer of asynchronous events in the |
| 90 | // BufferQueue. |
| 91 | virtual void onFrameAvailable(); |
| 92 | virtual void onBuffersReleased(); |
| 93 | |
| 94 | private: |
| 95 | // Free local buffer state |
| 96 | status_t freeBufferLocked(int buf); |
| 97 | |
| 98 | // Maximum number of buffers that can be locked at a time |
| 99 | uint32_t mMaxLockedBuffers; |
| 100 | |
| 101 | // mName is a string used to identify the SurfaceTexture in log messages. |
| 102 | // It can be set by the setName method. |
| 103 | String8 mName; |
| 104 | |
| 105 | // mFrameAvailableListener is the listener object that will be called when a |
| 106 | // new frame becomes available. If it is not NULL it will be called from |
| 107 | // queueBuffer. |
| 108 | sp<FrameAvailableListener> mFrameAvailableListener; |
| 109 | |
| 110 | // Underlying buffer queue |
| 111 | sp<BufferQueue> mBufferQueue; |
| 112 | |
| 113 | // Array for caching buffers from the buffer queue |
| 114 | sp<GraphicBuffer> mBufferSlot[BufferQueue::NUM_BUFFER_SLOTS]; |
| 115 | // Count of currently outstanding buffers |
| 116 | uint32_t mCurrentLockedBuffers; |
| 117 | |
| 118 | // mMutex is the mutex used to prevent concurrent access to the member |
| 119 | // variables of MediaConsumer objects. It must be locked whenever the |
| 120 | // member variables are accessed. |
| 121 | mutable Mutex mMutex; |
| 122 | }; |
| 123 | |
| 124 | } // namespace android |
| 125 | |
| 126 | #endif // ANDROID_SERVERS_CAMERA_MEDIACONSUMER_H |