• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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 CAMERA_SOURCE_H_
18 
19 #define CAMERA_SOURCE_H_
20 
21 #include <deque>
22 #include <media/stagefright/MediaSource.h>
23 #include <media/stagefright/MediaBuffer.h>
24 #include <camera/android/hardware/ICamera.h>
25 #include <camera/ICameraRecordingProxy.h>
26 #include <camera/CameraParameters.h>
27 #include <gui/BufferItemConsumer.h>
28 #include <gui/Surface.h>
29 #include <gui/Flags.h>
30 #include <utils/List.h>
31 #include <utils/RefBase.h>
32 #include <utils/String16.h>
33 #include <media/hardware/MetadataBufferType.h>
34 
35 namespace android {
36 
37 class IMemory;
38 class Camera;
39 class Surface;
40 
41 class CameraSource : public MediaSource, public MediaBufferObserver {
42 public:
43     /**
44      * Factory method to create a new CameraSource.
45      *
46      * @param camera the video input frame data source. If it is NULL,
47      *          we will try to connect to the camera with the given
48      *          cameraId.
49      *
50      * @param cameraId the id of the camera that the source will connect
51      *          to if camera is NULL; otherwise ignored.
52      * @param clientName the package/process name of the camera-using
53      *          application if camera is NULL; otherwise ignored. Used for
54      *          permissions checking.
55      * @param clientUid the UID of the camera-using application if camera is
56      *          NULL; otherwise ignored. Used for permissions checking.
57      * @param clientPid the PID of the camera-using application if camera is
58      *          NULL; otherwise ignored. Used for permissions checking.
59      * @param videoSize the dimension (in pixels) of the video frame
60      * @param frameRate the target frames per second
61      * @param surface the preview surface for display where preview
62      *          frames are sent to
63      * @param storeMetaDataInVideoBuffers true to request the camera
64      *          source to store meta data in video buffers; false to
65      *          request the camera source to store real YUV frame data
66      *          in the video buffers. The camera source may not support
67      *          storing meta data in video buffers, if so, a request
68      *          to do that will NOT be honored. To find out whether
69      *          meta data is actually being stored in video buffers
70      *          during recording, call isMetaDataStoredInVideoBuffers().
71      *
72      * @return NULL on error.
73      */
74     static CameraSource *CreateFromCamera(const sp<hardware::ICamera> &camera,
75                                           const sp<ICameraRecordingProxy> &proxy,
76                                           int32_t cameraId,
77                                           const String16& clientName,
78                                           uid_t clientUid,
79                                           pid_t clientPid,
80                                           Size videoSize,
81                                           int32_t frameRate,
82                                           const sp<SurfaceType>& surface);
83 
84     virtual ~CameraSource();
85 
86     virtual status_t start(MetaData *params = NULL);
stop()87     virtual status_t stop() { return reset(); }
88     virtual status_t read(
89             MediaBufferBase **buffer, const ReadOptions *options = NULL);
90     virtual status_t setStopTimeUs(int64_t stopTimeUs);
91 
92     /**
93      * Check whether a CameraSource object is properly initialized.
94      * Must call this method before stop().
95      * @return OK if initialization has successfully completed.
96      */
97     virtual status_t initCheck() const;
98 
99     /**
100      * Returns the MetaData associated with the CameraSource,
101      * including:
102      * kKeyColorFormat: YUV color format of the video frames
103      * kKeyWidth, kKeyHeight: dimension (in pixels) of the video frames
104      * kKeySampleRate: frame rate in frames per second
105      * kKeyMIMEType: always fixed to be MEDIA_MIMETYPE_VIDEO_RAW
106      */
107     virtual sp<MetaData> getFormat();
108 
109     /**
110      * Tell whether this camera source stores meta data or real YUV
111      * frame data in video buffers.
112      *
113      * @return a valid type if meta data is stored in the video
114      *      buffers; kMetadataBufferTypeInvalid if real YUV data is stored in
115      *      the video buffers.
116      */
117     MetadataBufferType metaDataStoredInVideoBuffers() const;
118 
119     virtual void signalBufferReturned(MediaBufferBase* buffer);
120 
121 protected:
122 
123     /**
124      * The class for listening to BufferQueue's onFrameAvailable. This is used to receive video
125      * buffers in VIDEO_BUFFER_MODE_BUFFER_QUEUE mode. When a frame is available,
126      * CameraSource::processBufferQueueFrame() will be called.
127      */
128     class BufferQueueListener : public Thread,  public BufferItemConsumer::FrameAvailableListener {
129     public:
130         BufferQueueListener(const sp<BufferItemConsumer> &consumer,
131                 const sp<CameraSource> &cameraSource);
132         virtual void onFrameAvailable(const BufferItem& item);
133         virtual bool threadLoop();
134     private:
135         static const nsecs_t kFrameAvailableTimeout = 50000000; // 50ms
136 
137         sp<BufferItemConsumer> mConsumer;
138         sp<CameraSource> mCameraSource;
139 
140         Mutex mLock;
141         Condition mFrameAvailableSignal;
142         bool mFrameAvailable;
143     };
144 
145     // isBinderAlive needs linkToDeath to work.
146     class DeathNotifier: public IBinder::DeathRecipient {
147     public:
DeathNotifier()148         DeathNotifier() {}
149         virtual void binderDied(const wp<IBinder>& who);
150     };
151 
152     enum CameraFlags {
153         FLAGS_SET_CAMERA = 1L << 0,
154         FLAGS_HOT_CAMERA = 1L << 1,
155     };
156 
157     int32_t  mCameraFlags;
158     Size     mVideoSize;
159     int32_t  mNumInputBuffers;
160     int32_t  mVideoFrameRate;
161     int32_t  mColorFormat;
162     int32_t  mEncoderFormat;
163     int32_t  mEncoderDataSpace;
164     int32_t  mBufferDataSpace;
165     status_t mInitCheck;
166 
167     sp<Camera>   mCamera;
168     sp<ICameraRecordingProxy>   mCameraRecordingProxy;
169     sp<DeathNotifier> mDeathNotifier;
170     sp<SurfaceType>  mSurface;
171     sp<MetaData> mMeta;
172 
173     int64_t mStartTimeUs;
174     int32_t mNumFramesReceived;
175     int64_t mLastFrameTimestampUs;
176     bool mStarted;
177     bool mEos;
178     int32_t mNumFramesEncoded;
179 
180     // Time between capture of two frames.
181     int64_t mTimeBetweenFrameCaptureUs;
182 
183     CameraSource(const sp<hardware::ICamera>& camera, const sp<ICameraRecordingProxy>& proxy,
184                  int32_t cameraId, const String16& clientName, uid_t clientUid, pid_t clientPid,
185                  Size videoSize, int32_t frameRate, const sp<SurfaceType> & surface);
186 
187     virtual status_t startCameraRecording();
188     virtual void releaseRecordingFrame(const sp<IMemory>& frame);
189 
190     // Returns true if need to skip the current frame.
191     // Called from dataCallbackTimestamp.
skipCurrentFrame(int64_t)192     virtual bool skipCurrentFrame(int64_t /*timestampUs*/) {return false;}
193 
194     // Process a buffer item received in BufferQueueListener.
195     virtual void processBufferQueueFrame(BufferItem& buffer);
196 
197     void releaseCamera();
198 
199 private:
200     friend struct CameraSourceListener;
201 
202     Mutex mLock;
203     Condition mFrameAvailableCondition;
204     Condition mFrameCompleteCondition;
205     List<sp<IMemory> > mFramesReceived;
206     List<sp<IMemory> > mFramesBeingEncoded;
207     List<int64_t> mFrameTimes;
208 
209     int64_t mFirstFrameTimeUs;
210     int64_t mStopSystemTimeUs;
211     int32_t mNumFramesDropped;
212     int32_t mNumGlitches;
213     int64_t mGlitchDurationThresholdUs;
214     bool mCollectStats;
215 
216     static const uint32_t kDefaultVideoBufferCount = 32;
217 
218     /**
219      * The following variables are used in VIDEO_BUFFER_MODE_BUFFER_QUEUE mode.
220      */
221     static const size_t kConsumerBufferCount = 8;
222     static const nsecs_t kMemoryBaseAvailableTimeoutNs = 200000000; // 200ms
223     // Consumer and producer of the buffer queue between this class and camera.
224     sp<BufferItemConsumer> mVideoBufferConsumer;
225     sp<SurfaceType> mVideoBufferProducer;
226     // Memory used to send the buffers to encoder, where sp<IMemory> stores VideoNativeMetadata.
227     sp<IMemoryHeap> mMemoryHeapBase;
228     List<sp<IMemory>> mMemoryBases;
229     // The condition that will be signaled when there is an entry available in mMemoryBases.
230     Condition mMemoryBaseAvailableCond;
231     // A mapping from ANativeWindowBuffer sent to encoder to BufferItem received from camera.
232     // This is protected by mLock.
233     KeyedVector<ANativeWindowBuffer*, BufferItem> mReceivedBufferItemMap;
234     sp<BufferQueueListener> mBufferQueueListener;
235 
236     Mutex mBatchLock; // protecting access to mInflightXXXXX members below
237     // Start of members protected by mBatchLock
238     std::deque<uint32_t> mInflightBatchSizes;
239     std::vector<native_handle_t*> mInflightReturnedHandles;
240     std::vector<sp<IMemory>> mInflightReturnedMemorys;
241     // End of members protected by mBatchLock
242 
243     void releaseQueuedFrames();
244     void releaseOneRecordingFrame(const sp<IMemory>& frame);
245     void createVideoBufferMemoryHeap(size_t size, uint32_t bufferCount);
246 
247     status_t init(const sp<hardware::ICamera>& camera, const sp<ICameraRecordingProxy>& proxy,
248                   int32_t cameraId, const String16& clientName, uid_t clientUid, pid_t clientPid,
249                   Size videoSize, int32_t frameRate);
250 
251     status_t initWithCameraAccess(
252                   const sp<hardware::ICamera>& camera, const sp<ICameraRecordingProxy>& proxy,
253                   int32_t cameraId, const String16& clientName, uid_t clientUid, pid_t clientPid,
254                   Size videoSize, int32_t frameRate);
255 
256     // Initialize the buffer queue used in VIDEO_BUFFER_MODE_BUFFER_QUEUE mode.
257     status_t initBufferQueue(uint32_t width, uint32_t height, uint32_t format,
258                   android_dataspace dataSpace, uint32_t bufferCount);
259 
260     status_t isCameraAvailable(const sp<hardware::ICamera>& camera,
261                                const sp<ICameraRecordingProxy>& proxy,
262                                int32_t cameraId,
263                                const std::string& clientName,
264                                uid_t clientUid,
265                                pid_t clientPid);
266 
267     status_t isCameraColorFormatSupported(const CameraParameters& params);
268     status_t configureCamera(CameraParameters* params,
269                     int32_t width, int32_t height,
270                     int32_t frameRate);
271 
272     status_t checkVideoSize(const CameraParameters& params,
273                     int32_t width, int32_t height);
274 
275     status_t checkFrameRate(const CameraParameters& params,
276                     int32_t frameRate);
277 
278     // Check if this frame should be skipped based on the frame's timestamp in microsecond.
279     // mLock must be locked before calling this function.
280     bool shouldSkipFrameLocked(int64_t timestampUs);
281 
282     void stopCameraRecording();
283     status_t reset();
284 
285     CameraSource(const CameraSource &);
286     CameraSource &operator=(const CameraSource &);
287 };
288 
289 }  // namespace android
290 
291 #endif  // CAMERA_SOURCE_H_
292