blob: 4f6c8d171c9318f01176feda9385bab209c82ca4 [file] [log] [blame]
Nipun Kwatraf9b80182010-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>
23#include <media/stagefright/CameraSource.h>
24#include <media/stagefright/CameraSourceTimeLapse.h>
25#include <media/stagefright/MediaDebug.h>
26#include <media/stagefright/MetaData.h>
27#include <camera/Camera.h>
28#include <camera/CameraParameters.h>
29#include <utils/String8.h>
Nipun Kwatrab1fb6072010-07-30 18:30:55 -070030#include <utils/Vector.h>
Nipun Kwatraf9b80182010-07-12 09:17:14 -070031
32namespace android {
33
34// static
James Dong0c128b62010-10-08 11:59:32 -070035CameraSourceTimeLapse *CameraSourceTimeLapse::CreateFromCamera(
36 const sp<ICamera> &camera,
Wu-cheng Li42419ce2011-06-01 17:22:24 +080037 const sp<ICameraRecordingProxy> &proxy,
James Dong0c128b62010-10-08 11:59:32 -070038 int32_t cameraId,
39 Size videoSize,
40 int32_t videoFrameRate,
41 const sp<Surface>& surface,
James Dong96af14d2011-11-20 09:45:44 -080042 int64_t timeBetweenFrameCaptureUs) {
Nipun Kwatraf9b80182010-07-12 09:17:14 -070043
James Dong0c128b62010-10-08 11:59:32 -070044 CameraSourceTimeLapse *source = new
Wu-cheng Li42419ce2011-06-01 17:22:24 +080045 CameraSourceTimeLapse(camera, proxy, cameraId,
James Dong0c128b62010-10-08 11:59:32 -070046 videoSize, videoFrameRate, surface,
James Dong96af14d2011-11-20 09:45:44 -080047 timeBetweenFrameCaptureUs);
James Dong0c128b62010-10-08 11:59:32 -070048
49 if (source != NULL) {
50 if (source->initCheck() != OK) {
51 delete source;
52 return NULL;
53 }
Nipun Kwatraf9b80182010-07-12 09:17:14 -070054 }
James Dong0c128b62010-10-08 11:59:32 -070055 return source;
Nipun Kwatraf9b80182010-07-12 09:17:14 -070056}
57
James Dong0c128b62010-10-08 11:59:32 -070058CameraSourceTimeLapse::CameraSourceTimeLapse(
59 const sp<ICamera>& camera,
Wu-cheng Li42419ce2011-06-01 17:22:24 +080060 const sp<ICameraRecordingProxy>& proxy,
James Dong0c128b62010-10-08 11:59:32 -070061 int32_t cameraId,
62 Size videoSize,
63 int32_t videoFrameRate,
64 const sp<Surface>& surface,
James Dong96af14d2011-11-20 09:45:44 -080065 int64_t timeBetweenFrameCaptureUs)
Wu-cheng Li42419ce2011-06-01 17:22:24 +080066 : CameraSource(camera, proxy, cameraId, videoSize, videoFrameRate, surface, true),
Nipun Kwatraf9b80182010-07-12 09:17:14 -070067 mTimeBetweenTimeLapseVideoFramesUs(1E6/videoFrameRate),
68 mLastTimeLapseFrameRealTimestampUs(0),
69 mSkipCurrentFrame(false) {
70
James Dong96af14d2011-11-20 09:45:44 -080071 mTimeBetweenFrameCaptureUs = timeBetweenFrameCaptureUs;
James Dong83dd43f2011-06-29 16:56:52 -070072 LOGD("starting time lapse mode: %lld us",
James Dong96af14d2011-11-20 09:45:44 -080073 mTimeBetweenFrameCaptureUs);
James Dong83dd43f2011-06-29 16:56:52 -070074
James Dong0c128b62010-10-08 11:59:32 -070075 mVideoWidth = videoSize.width;
76 mVideoHeight = videoSize.height;
Nipun Kwatra4a857e62010-09-02 11:43:15 -070077
James Dong83dd43f2011-06-29 16:56:52 -070078 if (!trySettingVideoSize(videoSize.width, videoSize.height)) {
79 mInitCheck = NO_INIT;
Nipun Kwatraf9b80182010-07-12 09:17:14 -070080 }
Nipun Kwatra7553cf72010-09-15 15:08:49 -070081
82 // Initialize quick stop variables.
83 mQuickStop = false;
84 mForceRead = false;
85 mLastReadBufferCopy = NULL;
86 mStopWaitingForIdleCamera = false;
Nipun Kwatraf9b80182010-07-12 09:17:14 -070087}
88
89CameraSourceTimeLapse::~CameraSourceTimeLapse() {
90}
91
Nipun Kwatra7553cf72010-09-15 15:08:49 -070092void CameraSourceTimeLapse::startQuickReadReturns() {
Steve Block71f2cf12011-10-20 11:56:00 +010093 ALOGV("startQuickReadReturns");
Nipun Kwatra7553cf72010-09-15 15:08:49 -070094 Mutex::Autolock autoLock(mQuickStopLock);
Nipun Kwatra7553cf72010-09-15 15:08:49 -070095
96 // Enable quick stop mode.
97 mQuickStop = true;
98
James Dong83dd43f2011-06-29 16:56:52 -070099 // Force dataCallbackTimestamp() coming from the video camera to
100 // not skip the next frame as we want read() to get a get a frame
101 // right away.
102 mForceRead = true;
Nipun Kwatra7553cf72010-09-15 15:08:49 -0700103}
104
James Dong83dd43f2011-06-29 16:56:52 -0700105bool CameraSourceTimeLapse::trySettingVideoSize(
106 int32_t width, int32_t height) {
107
Steve Block71f2cf12011-10-20 11:56:00 +0100108 ALOGV("trySettingVideoSize");
Nipun Kwatra4a857e62010-09-02 11:43:15 -0700109 int64_t token = IPCThreadState::self()->clearCallingIdentity();
110 String8 s = mCamera->getParameters();
Nipun Kwatra4a857e62010-09-02 11:43:15 -0700111
112 CameraParameters params(s);
113 Vector<Size> supportedSizes;
James Donga1d2d8f2011-01-04 16:09:07 -0800114 params.getSupportedVideoSizes(supportedSizes);
115 bool videoOutputSupported = false;
116 if (supportedSizes.size() == 0) {
117 params.getSupportedPreviewSizes(supportedSizes);
118 } else {
119 videoOutputSupported = true;
120 }
Nipun Kwatra4a857e62010-09-02 11:43:15 -0700121
James Donga1d2d8f2011-01-04 16:09:07 -0800122 bool videoSizeSupported = false;
Nipun Kwatra4a857e62010-09-02 11:43:15 -0700123 for (uint32_t i = 0; i < supportedSizes.size(); ++i) {
124 int32_t pictureWidth = supportedSizes[i].width;
125 int32_t pictureHeight = supportedSizes[i].height;
126
127 if ((pictureWidth == width) && (pictureHeight == height)) {
James Donga1d2d8f2011-01-04 16:09:07 -0800128 videoSizeSupported = true;
Nipun Kwatra4a857e62010-09-02 11:43:15 -0700129 }
130 }
131
James Dong08800f32010-12-09 15:04:33 -0800132 bool isSuccessful = false;
James Donga1d2d8f2011-01-04 16:09:07 -0800133 if (videoSizeSupported) {
Steve Block71f2cf12011-10-20 11:56:00 +0100134 ALOGV("Video size (%d, %d) is supported", width, height);
James Donga1d2d8f2011-01-04 16:09:07 -0800135 if (videoOutputSupported) {
136 params.setVideoSize(width, height);
137 } else {
138 params.setPreviewSize(width, height);
139 }
James Dong08800f32010-12-09 15:04:33 -0800140 if (mCamera->setParameters(params.flatten()) == OK) {
141 isSuccessful = true;
142 } else {
143 LOGE("Failed to set preview size to %dx%d", width, height);
144 isSuccessful = false;
145 }
Nipun Kwatra4a857e62010-09-02 11:43:15 -0700146 }
147
James Dong08800f32010-12-09 15:04:33 -0800148 IPCThreadState::self()->restoreCallingIdentity(token);
149 return isSuccessful;
Nipun Kwatra4a857e62010-09-02 11:43:15 -0700150}
151
Nipun Kwatra7553cf72010-09-15 15:08:49 -0700152void CameraSourceTimeLapse::signalBufferReturned(MediaBuffer* buffer) {
Steve Block71f2cf12011-10-20 11:56:00 +0100153 ALOGV("signalBufferReturned");
Nipun Kwatra7553cf72010-09-15 15:08:49 -0700154 Mutex::Autolock autoLock(mQuickStopLock);
155 if (mQuickStop && (buffer == mLastReadBufferCopy)) {
156 buffer->setObserver(NULL);
157 buffer->release();
158 } else {
159 return CameraSource::signalBufferReturned(buffer);
160 }
161}
162
James Dong83dd43f2011-06-29 16:56:52 -0700163void createMediaBufferCopy(
164 const MediaBuffer& sourceBuffer,
165 int64_t frameTime,
166 MediaBuffer **newBuffer) {
167
Steve Block71f2cf12011-10-20 11:56:00 +0100168 ALOGV("createMediaBufferCopy");
Nipun Kwatra7553cf72010-09-15 15:08:49 -0700169 size_t sourceSize = sourceBuffer.size();
170 void* sourcePointer = sourceBuffer.data();
171
172 (*newBuffer) = new MediaBuffer(sourceSize);
173 memcpy((*newBuffer)->data(), sourcePointer, sourceSize);
174
175 (*newBuffer)->meta_data()->setInt64(kKeyTime, frameTime);
176}
177
178void CameraSourceTimeLapse::fillLastReadBufferCopy(MediaBuffer& sourceBuffer) {
Steve Block71f2cf12011-10-20 11:56:00 +0100179 ALOGV("fillLastReadBufferCopy");
Nipun Kwatra7553cf72010-09-15 15:08:49 -0700180 int64_t frameTime;
181 CHECK(sourceBuffer.meta_data()->findInt64(kKeyTime, &frameTime));
182 createMediaBufferCopy(sourceBuffer, frameTime, &mLastReadBufferCopy);
183 mLastReadBufferCopy->add_ref();
184 mLastReadBufferCopy->setObserver(this);
185}
186
187status_t CameraSourceTimeLapse::read(
188 MediaBuffer **buffer, const ReadOptions *options) {
Steve Block71f2cf12011-10-20 11:56:00 +0100189 ALOGV("read");
Nipun Kwatra7553cf72010-09-15 15:08:49 -0700190 if (mLastReadBufferCopy == NULL) {
191 mLastReadStatus = CameraSource::read(buffer, options);
192
James Dong83dd43f2011-06-29 16:56:52 -0700193 // mQuickStop may have turned to true while read was blocked.
194 // Make a copy of the buffer in that case.
Nipun Kwatra7553cf72010-09-15 15:08:49 -0700195 Mutex::Autolock autoLock(mQuickStopLock);
196 if (mQuickStop && *buffer) {
197 fillLastReadBufferCopy(**buffer);
198 }
199 return mLastReadStatus;
200 } else {
201 (*buffer) = mLastReadBufferCopy;
202 (*buffer)->add_ref();
203 return mLastReadStatus;
204 }
205}
206
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700207void CameraSourceTimeLapse::stopCameraRecording() {
Steve Block71f2cf12011-10-20 11:56:00 +0100208 ALOGV("stopCameraRecording");
James Dong83dd43f2011-06-29 16:56:52 -0700209 CameraSource::stopCameraRecording();
Nipun Kwatra7553cf72010-09-15 15:08:49 -0700210 if (mLastReadBufferCopy) {
211 mLastReadBufferCopy->release();
212 mLastReadBufferCopy = NULL;
213 }
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700214}
215
James Dong83dd43f2011-06-29 16:56:52 -0700216sp<IMemory> CameraSourceTimeLapse::createIMemoryCopy(
217 const sp<IMemory> &source_data) {
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700218
Steve Block71f2cf12011-10-20 11:56:00 +0100219 ALOGV("createIMemoryCopy");
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700220 size_t source_size = source_data->size();
221 void* source_pointer = source_data->pointer();
222
223 sp<MemoryHeapBase> newMemoryHeap = new MemoryHeapBase(source_size);
224 sp<MemoryBase> newMemory = new MemoryBase(newMemoryHeap, 0, source_size);
225 memcpy(newMemory->pointer(), source_pointer, source_size);
226 return newMemory;
227}
228
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700229bool CameraSourceTimeLapse::skipCurrentFrame(int64_t timestampUs) {
Steve Block71f2cf12011-10-20 11:56:00 +0100230 ALOGV("skipCurrentFrame");
Nipun Kwatradce4beb2010-07-27 22:21:44 -0700231 if (mSkipCurrentFrame) {
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700232 mSkipCurrentFrame = false;
233 return true;
234 } else {
235 return false;
236 }
237}
238
239bool CameraSourceTimeLapse::skipFrameAndModifyTimeStamp(int64_t *timestampUs) {
Steve Block71f2cf12011-10-20 11:56:00 +0100240 ALOGV("skipFrameAndModifyTimeStamp");
James Dong83dd43f2011-06-29 16:56:52 -0700241 if (mLastTimeLapseFrameRealTimestampUs == 0) {
242 // First time lapse frame. Initialize mLastTimeLapseFrameRealTimestampUs
243 // to current time (timestampUs) and save frame data.
Steve Block71f2cf12011-10-20 11:56:00 +0100244 ALOGV("dataCallbackTimestamp timelapse: initial frame");
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700245
James Dong83dd43f2011-06-29 16:56:52 -0700246 mLastTimeLapseFrameRealTimestampUs = *timestampUs;
247 return false;
248 }
249
250 {
251 Mutex::Autolock autoLock(mQuickStopLock);
252
253 // mForceRead may be set to true by startQuickReadReturns(). In that
254 // case don't skip this frame.
255 if (mForceRead) {
Steve Block71f2cf12011-10-20 11:56:00 +0100256 ALOGV("dataCallbackTimestamp timelapse: forced read");
James Dong83dd43f2011-06-29 16:56:52 -0700257 mForceRead = false;
258 *timestampUs =
259 mLastFrameTimestampUs + mTimeBetweenTimeLapseVideoFramesUs;
James Dongb888fd12011-10-26 23:47:55 -0700260
261 // Really make sure that this video recording frame will not be dropped.
262 if (*timestampUs < mStartTimeUs) {
263 LOGI("set timestampUs to start time stamp %lld us", mStartTimeUs);
264 *timestampUs = mStartTimeUs;
265 }
Nipun Kwatra7553cf72010-09-15 15:08:49 -0700266 return false;
267 }
James Dong83dd43f2011-06-29 16:56:52 -0700268 }
Nipun Kwatra7553cf72010-09-15 15:08:49 -0700269
James Dong83dd43f2011-06-29 16:56:52 -0700270 // Workaround to bypass the first 2 input frames for skipping.
271 // The first 2 output frames from the encoder are: decoder specific info and
272 // the compressed video frame data for the first input video frame.
273 if (mNumFramesEncoded >= 1 && *timestampUs <
James Dong96af14d2011-11-20 09:45:44 -0800274 (mLastTimeLapseFrameRealTimestampUs + mTimeBetweenFrameCaptureUs)) {
James Dong83dd43f2011-06-29 16:56:52 -0700275 // Skip all frames from last encoded frame until
James Dong96af14d2011-11-20 09:45:44 -0800276 // sufficient time (mTimeBetweenFrameCaptureUs) has passed.
James Dong83dd43f2011-06-29 16:56:52 -0700277 // Tell the camera to release its recording frame and return.
Steve Block71f2cf12011-10-20 11:56:00 +0100278 ALOGV("dataCallbackTimestamp timelapse: skipping intermediate frame");
James Dong83dd43f2011-06-29 16:56:52 -0700279 return true;
280 } else {
James Dong96af14d2011-11-20 09:45:44 -0800281 // Desired frame has arrived after mTimeBetweenFrameCaptureUs time:
James Dong83dd43f2011-06-29 16:56:52 -0700282 // - Reset mLastTimeLapseFrameRealTimestampUs to current time.
283 // - Artificially modify timestampUs to be one frame time (1/framerate) ahead
284 // of the last encoded frame's time stamp.
Steve Block71f2cf12011-10-20 11:56:00 +0100285 ALOGV("dataCallbackTimestamp timelapse: got timelapse frame");
Nipun Kwatra7553cf72010-09-15 15:08:49 -0700286
James Dong83dd43f2011-06-29 16:56:52 -0700287 mLastTimeLapseFrameRealTimestampUs = *timestampUs;
288 *timestampUs = mLastFrameTimestampUs + mTimeBetweenTimeLapseVideoFramesUs;
289 return false;
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700290 }
291 return false;
292}
293
294void CameraSourceTimeLapse::dataCallbackTimestamp(int64_t timestampUs, int32_t msgType,
295 const sp<IMemory> &data) {
Steve Block71f2cf12011-10-20 11:56:00 +0100296 ALOGV("dataCallbackTimestamp");
James Dong83dd43f2011-06-29 16:56:52 -0700297 mSkipCurrentFrame = skipFrameAndModifyTimeStamp(&timestampUs);
Nipun Kwatraf9b80182010-07-12 09:17:14 -0700298 CameraSource::dataCallbackTimestamp(timestampUs, msgType, data);
299}
300
301} // namespace android