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