SurfaceMediaSource.h revision 66e72bc85fb762876baff60ef29de729da93cf26
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#include <gui/BufferQueue.h>
22
23#include <utils/threads.h>
24#include <utils/Vector.h>
25#include <media/stagefright/MediaSource.h>
26#include <media/stagefright/MediaBuffer.h>
27
28namespace android {
29// ----------------------------------------------------------------------------
30
31class IGraphicBufferAlloc;
32class String8;
33class GraphicBuffer;
34
35// ASSUMPTIONS
36// 1. SurfaceMediaSource is initialized with width*height which
37// can never change.  However, deqeueue buffer does not currently
38// enforce this as in BufferQueue, dequeue can be used by SurfaceTexture
39// which can modify the default width and heght.  Also neither the width
40// nor height can be 0.
41// 2. setSynchronousMode is never used (basically no one should call
42// setSynchronousMode(false)
43// 3. setCrop, setTransform, setScalingMode should never be used
44// 4. queueBuffer returns a filled buffer to the SurfaceMediaSource. In addition, a
45// timestamp must be provided for the buffer. The timestamp is in
46// nanoseconds, and must be monotonically increasing. Its other semantics
47// (zero point, etc) are client-dependent and should be documented by the
48// client.
49// 5. Once disconnected, SurfaceMediaSource can be reused (can not
50// connect again)
51// 6. Stop is a hard stop, the last few frames held by the encoder
52// may be dropped.  It is possible to wait for the buffers to be
53// returned (but not implemented)
54
55class SurfaceMediaSource : public MediaSource,
56                                public MediaBufferObserver,
57                                protected BufferQueue::ConsumerListener {
58public:
59    enum { MIN_UNDEQUEUED_BUFFERS = 4};
60
61    struct FrameAvailableListener : public virtual RefBase {
62        // onFrameAvailable() is called from queueBuffer() is the FIFO is
63        // empty. You can use SurfaceMediaSource::getQueuedCount() to
64        // figure out if there are more frames waiting.
65        // This is called without any lock held can be called concurrently by
66        // multiple threads.
67        virtual void onFrameAvailable() = 0;
68    };
69
70    SurfaceMediaSource(uint32_t bufferWidth, uint32_t bufferHeight);
71
72    virtual ~SurfaceMediaSource();
73
74    // For the MediaSource interface for use by StageFrightRecorder:
75    virtual status_t start(MetaData *params = NULL);
76    virtual status_t stop();
77    virtual status_t read(MediaBuffer **buffer,
78            const ReadOptions *options = NULL);
79    virtual sp<MetaData> getFormat();
80
81    // Get / Set the frame rate used for encoding. Default fps = 30
82    status_t setFrameRate(int32_t fps) ;
83    int32_t getFrameRate( ) const;
84
85    // The call for the StageFrightRecorder to tell us that
86    // it is done using the MediaBuffer data so that its state
87    // can be set to FREE for dequeuing
88    virtual void signalBufferReturned(MediaBuffer* buffer);
89    // end of MediaSource interface
90
91    // getTimestamp retrieves the timestamp associated with the image
92    // set by the most recent call to read()
93    //
94    // The timestamp is in nanoseconds, and is monotonically increasing. Its
95    // other semantics (zero point, etc) are source-dependent and should be
96    // documented by the source.
97    int64_t getTimestamp();
98
99    // setFrameAvailableListener sets the listener object that will be notified
100    // when a new frame becomes available.
101    void setFrameAvailableListener(const sp<FrameAvailableListener>& listener);
102
103    // dump our state in a String
104    void dump(String8& result) const;
105    void dump(String8& result, const char* prefix, char* buffer,
106                                                    size_t SIZE) const;
107
108    // isMetaDataStoredInVideoBuffers tells the encoder whether we will
109    // pass metadata through the buffers. Currently, it is force set to true
110    bool isMetaDataStoredInVideoBuffers() const;
111
112    sp<BufferQueue> getBufferQueue() const { return mBufferQueue; }
113
114    // To be called before start()
115    status_t setMaxAcquiredBufferCount(size_t count);
116
117protected:
118
119    // Implementation of the BufferQueue::ConsumerListener interface.  These
120    // calls are used to notify the SurfaceTexture of asynchronous events in the
121    // BufferQueue.
122    virtual void onFrameAvailable();
123
124    // Used as a hook to BufferQueue::disconnect()
125    // This is called by the client side when it is done
126    // TODO: Currently, this also sets mStopped to true which
127    // is needed for unblocking the encoder which might be
128    // waiting to read more frames. So if on the client side,
129    // the same thread supplies the frames and also calls stop
130    // on the encoder, the client has to call disconnect before
131    // it calls stop.
132    // In the case of the camera,
133    // that need not be required since the thread supplying the
134    // frames is separate than the one calling stop.
135    virtual void onBuffersReleased();
136
137    static bool isExternalFormat(uint32_t format);
138
139private:
140    // mBufferQueue is the exchange point between the producer and
141    // this consumer
142    sp<BufferQueue> mBufferQueue;
143
144    // mBufferSlot caches GraphicBuffers from the buffer queue
145    sp<GraphicBuffer> mBufferSlot[BufferQueue::NUM_BUFFER_SLOTS];
146
147
148    // The permenent width and height of SMS buffers
149    int mWidth;
150    int mHeight;
151
152    // mCurrentSlot is the buffer slot index of the buffer that is currently
153    // being used by buffer consumer
154    // (e.g. StageFrightRecorder in the case of SurfaceMediaSource or GLTexture
155    // in the case of SurfaceTexture).
156    // It is initialized to INVALID_BUFFER_SLOT,
157    // indicating that no buffer slot is currently bound to the texture. Note,
158    // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
159    // that no buffer is bound to the texture. A call to setBufferCount will
160    // reset mCurrentTexture to INVALID_BUFFER_SLOT.
161    int mCurrentSlot;
162
163    // mCurrentBuffers is a list of the graphic buffers that are being used by
164    // buffer consumer (i.e. the video encoder). It's possible that these
165    // buffers are not associated with any buffer slots, so we must track them
166    // separately.  Buffers are added to this list in read, and removed from
167    // this list in signalBufferReturned
168    Vector<sp<GraphicBuffer> > mCurrentBuffers;
169
170    // mCurrentTimestamp is the timestamp for the current texture. It
171    // gets set to mLastQueuedTimestamp each time updateTexImage is called.
172    int64_t mCurrentTimestamp;
173
174    // mFrameAvailableListener is the listener object that will be called when a
175    // new frame becomes available. If it is not NULL it will be called from
176    // queueBuffer.
177    sp<FrameAvailableListener> mFrameAvailableListener;
178
179    // mMutex is the mutex used to prevent concurrent access to the member
180    // variables of SurfaceMediaSource objects. It must be locked whenever the
181    // member variables are accessed.
182    mutable Mutex mMutex;
183
184    ////////////////////////// For MediaSource
185    // Set to a default of 30 fps if not specified by the client side
186    int32_t mFrameRate;
187
188    // mStopped is a flag to check if the recording is going on
189    bool mStopped;
190
191    // mNumFramesReceived indicates the number of frames recieved from
192    // the client side
193    int mNumFramesReceived;
194    // mNumFramesEncoded indicates the number of frames passed on to the
195    // encoder
196    int mNumFramesEncoded;
197
198    // mFirstFrameTimestamp is the timestamp of the first received frame.
199    // It is used to offset the output timestamps so recording starts at time 0.
200    int64_t mFirstFrameTimestamp;
201    // mStartTimeNs is the start time passed into the source at start, used to
202    // offset timestamps.
203    int64_t mStartTimeNs;
204
205    // mFrameAvailableCondition condition used to indicate whether there
206    // is a frame available for dequeuing
207    Condition mFrameAvailableCondition;
208
209    // Avoid copying and equating and default constructor
210    DISALLOW_IMPLICIT_CONSTRUCTORS(SurfaceMediaSource);
211};
212
213// ----------------------------------------------------------------------------
214}; // namespace android
215
216#endif // ANDROID_GUI_SURFACEMEDIASOURCE_H
217