blob: 20214e8d664822617c3d93e8e6c9cd400e1ad920 [file] [log] [blame]
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -07001/*
2 * Copyright (C) 2010 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//#define LOG_NDEBUG 0
18#define LOG_TAG "CameraSourceTimeLapse"
19
20#include <binder/IPCThreadState.h>
21#include <binder/MemoryBase.h>
22#include <binder/MemoryHeapBase.h>
James Dongf1d5aa12012-02-06 23:46:37 -080023#include <media/stagefright/foundation/ADebug.h>
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -070024#include <media/stagefright/CameraSource.h>
25#include <media/stagefright/CameraSourceTimeLapse.h>
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -070026#include <media/stagefright/MetaData.h>
27#include <camera/Camera.h>
28#include <camera/CameraParameters.h>
29#include <utils/String8.h>
Nipun Kwatra40e2f3f2010-07-30 18:30:55 -070030#include <utils/Vector.h>
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -070031
32namespace android {
33
34// static
James Dong54ff19a2010-10-08 11:59:32 -070035CameraSourceTimeLapse *CameraSourceTimeLapse::CreateFromCamera(
36 const sp<ICamera> &camera,
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080037 const sp<ICameraRecordingProxy> &proxy,
James Dong54ff19a2010-10-08 11:59:32 -070038 int32_t cameraId,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -080039 const String16& clientName,
40 uid_t clientUid,
James Dong54ff19a2010-10-08 11:59:32 -070041 Size videoSize,
42 int32_t videoFrameRate,
Mathias Agopian33da4022013-03-12 18:42:23 -070043 const sp<IGraphicBufferProducer>& surface,
James Donge8e5f862011-11-20 09:45:44 -080044 int64_t timeBetweenFrameCaptureUs) {
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -070045
James Dong54ff19a2010-10-08 11:59:32 -070046 CameraSourceTimeLapse *source = new
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080047 CameraSourceTimeLapse(camera, proxy, cameraId,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -080048 clientName, clientUid,
James Dong54ff19a2010-10-08 11:59:32 -070049 videoSize, videoFrameRate, surface,
James Donge8e5f862011-11-20 09:45:44 -080050 timeBetweenFrameCaptureUs);
James Dong54ff19a2010-10-08 11:59:32 -070051
52 if (source != NULL) {
53 if (source->initCheck() != OK) {
54 delete source;
55 return NULL;
56 }
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -070057 }
James Dong54ff19a2010-10-08 11:59:32 -070058 return source;
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -070059}
60
James Dong54ff19a2010-10-08 11:59:32 -070061CameraSourceTimeLapse::CameraSourceTimeLapse(
62 const sp<ICamera>& camera,
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +080063 const sp<ICameraRecordingProxy>& proxy,
James Dong54ff19a2010-10-08 11:59:32 -070064 int32_t cameraId,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -080065 const String16& clientName,
66 uid_t clientUid,
James Dong54ff19a2010-10-08 11:59:32 -070067 Size videoSize,
68 int32_t videoFrameRate,
Mathias Agopian33da4022013-03-12 18:42:23 -070069 const sp<IGraphicBufferProducer>& surface,
James Donge8e5f862011-11-20 09:45:44 -080070 int64_t timeBetweenFrameCaptureUs)
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -080071 : CameraSource(camera, proxy, cameraId, clientName, clientUid,
72 videoSize, videoFrameRate, surface, true),
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -070073 mTimeBetweenTimeLapseVideoFramesUs(1E6/videoFrameRate),
74 mLastTimeLapseFrameRealTimestampUs(0),
75 mSkipCurrentFrame(false) {
76
James Donge8e5f862011-11-20 09:45:44 -080077 mTimeBetweenFrameCaptureUs = timeBetweenFrameCaptureUs;
Steve Blockb8a80522011-12-20 16:23:08 +000078 ALOGD("starting time lapse mode: %lld us",
James Donge8e5f862011-11-20 09:45:44 -080079 mTimeBetweenFrameCaptureUs);
James Dong3cecf642011-06-29 16:56:52 -070080
James Dong54ff19a2010-10-08 11:59:32 -070081 mVideoWidth = videoSize.width;
82 mVideoHeight = videoSize.height;
Nipun Kwatra155e8332010-09-02 11:43:15 -070083
James Dong3cecf642011-06-29 16:56:52 -070084 if (!trySettingVideoSize(videoSize.width, videoSize.height)) {
85 mInitCheck = NO_INIT;
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -070086 }
Nipun Kwatra78eff722010-09-15 15:08:49 -070087
88 // Initialize quick stop variables.
89 mQuickStop = false;
90 mForceRead = false;
91 mLastReadBufferCopy = NULL;
92 mStopWaitingForIdleCamera = false;
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -070093}
94
95CameraSourceTimeLapse::~CameraSourceTimeLapse() {
James Dongb44c9d22012-02-02 18:04:02 -080096 if (mLastReadBufferCopy) {
97 mLastReadBufferCopy->release();
98 mLastReadBufferCopy = NULL;
99 }
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700100}
101
Nipun Kwatra78eff722010-09-15 15:08:49 -0700102void CameraSourceTimeLapse::startQuickReadReturns() {
Steve Block3856b092011-10-20 11:56:00 +0100103 ALOGV("startQuickReadReturns");
Nipun Kwatra78eff722010-09-15 15:08:49 -0700104 Mutex::Autolock autoLock(mQuickStopLock);
Nipun Kwatra78eff722010-09-15 15:08:49 -0700105
106 // Enable quick stop mode.
107 mQuickStop = true;
108
James Dong3cecf642011-06-29 16:56:52 -0700109 // Force dataCallbackTimestamp() coming from the video camera to
110 // not skip the next frame as we want read() to get a get a frame
111 // right away.
112 mForceRead = true;
Nipun Kwatra78eff722010-09-15 15:08:49 -0700113}
114
James Dong3cecf642011-06-29 16:56:52 -0700115bool CameraSourceTimeLapse::trySettingVideoSize(
116 int32_t width, int32_t height) {
117
Steve Block3856b092011-10-20 11:56:00 +0100118 ALOGV("trySettingVideoSize");
Nipun Kwatra155e8332010-09-02 11:43:15 -0700119 int64_t token = IPCThreadState::self()->clearCallingIdentity();
120 String8 s = mCamera->getParameters();
Nipun Kwatra155e8332010-09-02 11:43:15 -0700121
122 CameraParameters params(s);
123 Vector<Size> supportedSizes;
James Dong28934a92011-01-04 16:09:07 -0800124 params.getSupportedVideoSizes(supportedSizes);
125 bool videoOutputSupported = false;
126 if (supportedSizes.size() == 0) {
127 params.getSupportedPreviewSizes(supportedSizes);
128 } else {
129 videoOutputSupported = true;
130 }
Nipun Kwatra155e8332010-09-02 11:43:15 -0700131
James Dong28934a92011-01-04 16:09:07 -0800132 bool videoSizeSupported = false;
Nipun Kwatra155e8332010-09-02 11:43:15 -0700133 for (uint32_t i = 0; i < supportedSizes.size(); ++i) {
134 int32_t pictureWidth = supportedSizes[i].width;
135 int32_t pictureHeight = supportedSizes[i].height;
136
137 if ((pictureWidth == width) && (pictureHeight == height)) {
James Dong28934a92011-01-04 16:09:07 -0800138 videoSizeSupported = true;
Nipun Kwatra155e8332010-09-02 11:43:15 -0700139 }
140 }
141
James Dongba290022010-12-09 15:04:33 -0800142 bool isSuccessful = false;
James Dong28934a92011-01-04 16:09:07 -0800143 if (videoSizeSupported) {
Steve Block3856b092011-10-20 11:56:00 +0100144 ALOGV("Video size (%d, %d) is supported", width, height);
James Dong28934a92011-01-04 16:09:07 -0800145 if (videoOutputSupported) {
146 params.setVideoSize(width, height);
147 } else {
148 params.setPreviewSize(width, height);
149 }
James Dongba290022010-12-09 15:04:33 -0800150 if (mCamera->setParameters(params.flatten()) == OK) {
151 isSuccessful = true;
152 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000153 ALOGE("Failed to set preview size to %dx%d", width, height);
James Dongba290022010-12-09 15:04:33 -0800154 isSuccessful = false;
155 }
Nipun Kwatra155e8332010-09-02 11:43:15 -0700156 }
157
James Dongba290022010-12-09 15:04:33 -0800158 IPCThreadState::self()->restoreCallingIdentity(token);
159 return isSuccessful;
Nipun Kwatra155e8332010-09-02 11:43:15 -0700160}
161
Nipun Kwatra78eff722010-09-15 15:08:49 -0700162void CameraSourceTimeLapse::signalBufferReturned(MediaBuffer* buffer) {
Steve Block3856b092011-10-20 11:56:00 +0100163 ALOGV("signalBufferReturned");
Nipun Kwatra78eff722010-09-15 15:08:49 -0700164 Mutex::Autolock autoLock(mQuickStopLock);
165 if (mQuickStop && (buffer == mLastReadBufferCopy)) {
166 buffer->setObserver(NULL);
167 buffer->release();
168 } else {
169 return CameraSource::signalBufferReturned(buffer);
170 }
171}
172
James Dong3cecf642011-06-29 16:56:52 -0700173void createMediaBufferCopy(
174 const MediaBuffer& sourceBuffer,
175 int64_t frameTime,
176 MediaBuffer **newBuffer) {
177
Steve Block3856b092011-10-20 11:56:00 +0100178 ALOGV("createMediaBufferCopy");
Nipun Kwatra78eff722010-09-15 15:08:49 -0700179 size_t sourceSize = sourceBuffer.size();
180 void* sourcePointer = sourceBuffer.data();
181
182 (*newBuffer) = new MediaBuffer(sourceSize);
183 memcpy((*newBuffer)->data(), sourcePointer, sourceSize);
184
185 (*newBuffer)->meta_data()->setInt64(kKeyTime, frameTime);
186}
187
188void CameraSourceTimeLapse::fillLastReadBufferCopy(MediaBuffer& sourceBuffer) {
Steve Block3856b092011-10-20 11:56:00 +0100189 ALOGV("fillLastReadBufferCopy");
Nipun Kwatra78eff722010-09-15 15:08:49 -0700190 int64_t frameTime;
191 CHECK(sourceBuffer.meta_data()->findInt64(kKeyTime, &frameTime));
192 createMediaBufferCopy(sourceBuffer, frameTime, &mLastReadBufferCopy);
193 mLastReadBufferCopy->add_ref();
194 mLastReadBufferCopy->setObserver(this);
195}
196
197status_t CameraSourceTimeLapse::read(
198 MediaBuffer **buffer, const ReadOptions *options) {
Steve Block3856b092011-10-20 11:56:00 +0100199 ALOGV("read");
Nipun Kwatra78eff722010-09-15 15:08:49 -0700200 if (mLastReadBufferCopy == NULL) {
201 mLastReadStatus = CameraSource::read(buffer, options);
202
James Dong3cecf642011-06-29 16:56:52 -0700203 // mQuickStop may have turned to true while read was blocked.
204 // Make a copy of the buffer in that case.
Nipun Kwatra78eff722010-09-15 15:08:49 -0700205 Mutex::Autolock autoLock(mQuickStopLock);
206 if (mQuickStop && *buffer) {
207 fillLastReadBufferCopy(**buffer);
208 }
209 return mLastReadStatus;
210 } else {
211 (*buffer) = mLastReadBufferCopy;
212 (*buffer)->add_ref();
213 return mLastReadStatus;
214 }
215}
216
James Dong3cecf642011-06-29 16:56:52 -0700217sp<IMemory> CameraSourceTimeLapse::createIMemoryCopy(
218 const sp<IMemory> &source_data) {
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700219
Steve Block3856b092011-10-20 11:56:00 +0100220 ALOGV("createIMemoryCopy");
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700221 size_t source_size = source_data->size();
222 void* source_pointer = source_data->pointer();
223
224 sp<MemoryHeapBase> newMemoryHeap = new MemoryHeapBase(source_size);
225 sp<MemoryBase> newMemory = new MemoryBase(newMemoryHeap, 0, source_size);
226 memcpy(newMemory->pointer(), source_pointer, source_size);
227 return newMemory;
228}
229
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700230bool CameraSourceTimeLapse::skipCurrentFrame(int64_t timestampUs) {
Steve Block3856b092011-10-20 11:56:00 +0100231 ALOGV("skipCurrentFrame");
Nipun Kwatrac4e47d12010-07-27 22:21:44 -0700232 if (mSkipCurrentFrame) {
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700233 mSkipCurrentFrame = false;
234 return true;
235 } else {
236 return false;
237 }
238}
239
240bool CameraSourceTimeLapse::skipFrameAndModifyTimeStamp(int64_t *timestampUs) {
Steve Block3856b092011-10-20 11:56:00 +0100241 ALOGV("skipFrameAndModifyTimeStamp");
James Dong3cecf642011-06-29 16:56:52 -0700242 if (mLastTimeLapseFrameRealTimestampUs == 0) {
243 // First time lapse frame. Initialize mLastTimeLapseFrameRealTimestampUs
244 // to current time (timestampUs) and save frame data.
Steve Block3856b092011-10-20 11:56:00 +0100245 ALOGV("dataCallbackTimestamp timelapse: initial frame");
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700246
James Dong3cecf642011-06-29 16:56:52 -0700247 mLastTimeLapseFrameRealTimestampUs = *timestampUs;
248 return false;
249 }
250
251 {
252 Mutex::Autolock autoLock(mQuickStopLock);
253
254 // mForceRead may be set to true by startQuickReadReturns(). In that
255 // case don't skip this frame.
256 if (mForceRead) {
Steve Block3856b092011-10-20 11:56:00 +0100257 ALOGV("dataCallbackTimestamp timelapse: forced read");
James Dong3cecf642011-06-29 16:56:52 -0700258 mForceRead = false;
259 *timestampUs =
260 mLastFrameTimestampUs + mTimeBetweenTimeLapseVideoFramesUs;
James Dong93594b52011-10-26 23:47:55 -0700261
262 // Really make sure that this video recording frame will not be dropped.
263 if (*timestampUs < mStartTimeUs) {
Steve Blockdf64d152012-01-04 20:05:49 +0000264 ALOGI("set timestampUs to start time stamp %lld us", mStartTimeUs);
James Dong93594b52011-10-26 23:47:55 -0700265 *timestampUs = mStartTimeUs;
266 }
Nipun Kwatra78eff722010-09-15 15:08:49 -0700267 return false;
268 }
James Dong3cecf642011-06-29 16:56:52 -0700269 }
Nipun Kwatra78eff722010-09-15 15:08:49 -0700270
James Dong3cecf642011-06-29 16:56:52 -0700271 // Workaround to bypass the first 2 input frames for skipping.
272 // The first 2 output frames from the encoder are: decoder specific info and
273 // the compressed video frame data for the first input video frame.
274 if (mNumFramesEncoded >= 1 && *timestampUs <
James Donge8e5f862011-11-20 09:45:44 -0800275 (mLastTimeLapseFrameRealTimestampUs + mTimeBetweenFrameCaptureUs)) {
James Dong3cecf642011-06-29 16:56:52 -0700276 // Skip all frames from last encoded frame until
James Donge8e5f862011-11-20 09:45:44 -0800277 // sufficient time (mTimeBetweenFrameCaptureUs) has passed.
James Dong3cecf642011-06-29 16:56:52 -0700278 // Tell the camera to release its recording frame and return.
Steve Block3856b092011-10-20 11:56:00 +0100279 ALOGV("dataCallbackTimestamp timelapse: skipping intermediate frame");
James Dong3cecf642011-06-29 16:56:52 -0700280 return true;
281 } else {
James Donge8e5f862011-11-20 09:45:44 -0800282 // Desired frame has arrived after mTimeBetweenFrameCaptureUs time:
James Dong3cecf642011-06-29 16:56:52 -0700283 // - Reset mLastTimeLapseFrameRealTimestampUs to current time.
284 // - Artificially modify timestampUs to be one frame time (1/framerate) ahead
285 // of the last encoded frame's time stamp.
Steve Block3856b092011-10-20 11:56:00 +0100286 ALOGV("dataCallbackTimestamp timelapse: got timelapse frame");
Nipun Kwatra78eff722010-09-15 15:08:49 -0700287
James Dong3cecf642011-06-29 16:56:52 -0700288 mLastTimeLapseFrameRealTimestampUs = *timestampUs;
289 *timestampUs = mLastFrameTimestampUs + mTimeBetweenTimeLapseVideoFramesUs;
290 return false;
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700291 }
292 return false;
293}
294
295void CameraSourceTimeLapse::dataCallbackTimestamp(int64_t timestampUs, int32_t msgType,
296 const sp<IMemory> &data) {
Steve Block3856b092011-10-20 11:56:00 +0100297 ALOGV("dataCallbackTimestamp");
James Dong3cecf642011-06-29 16:56:52 -0700298 mSkipCurrentFrame = skipFrameAndModifyTimeStamp(&timestampUs);
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700299 CameraSource::dataCallbackTimestamp(timestampUs, msgType, data);
300}
301
302} // namespace android