Home | History | Annotate | Download | only in client2
      1 /*
      2  * Copyright (C) 2012 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 "Camera2-BurstCapture"
     19 
     20 #include <utils/Log.h>
     21 #include <utils/Trace.h>
     22 
     23 #include "BurstCapture.h"
     24 
     25 #include "api1/Camera2Client.h"
     26 #include "api1/client2/JpegCompressor.h"
     27 
     28 namespace android {
     29 namespace camera2 {
     30 
     31 BurstCapture::BurstCapture(wp<Camera2Client> client, wp<CaptureSequencer> sequencer):
     32     mCaptureStreamId(NO_STREAM),
     33     mClient(client),
     34     mSequencer(sequencer)
     35 {
     36 }
     37 
     38 BurstCapture::~BurstCapture() {
     39 }
     40 
     41 status_t BurstCapture::start(Vector<CameraMetadata> &/*metadatas*/,
     42                              int32_t /*firstCaptureId*/) {
     43     ALOGE("Not completely implemented");
     44     return INVALID_OPERATION;
     45 }
     46 
     47 void BurstCapture::onFrameAvailable() {
     48     ALOGV("%s", __FUNCTION__);
     49     Mutex::Autolock l(mInputMutex);
     50     if(!mInputChanged) {
     51         mInputChanged = true;
     52         mInputSignal.signal();
     53     }
     54 }
     55 
     56 bool BurstCapture::threadLoop() {
     57     status_t res;
     58     {
     59         Mutex::Autolock l(mInputMutex);
     60         while(!mInputChanged) {
     61             res = mInputSignal.waitRelative(mInputMutex, kWaitDuration);
     62             if(res == TIMED_OUT) return true;
     63         }
     64         mInputChanged = false;
     65     }
     66 
     67     do {
     68         sp<Camera2Client> client = mClient.promote();
     69         if(client == 0) return false;
     70         ALOGV("%s: Calling processFrameAvailable()", __FUNCTION__);
     71         res = processFrameAvailable(client);
     72     } while(res == OK);
     73 
     74     return true;
     75 }
     76 
     77 CpuConsumer::LockedBuffer* BurstCapture::jpegEncode(
     78     CpuConsumer::LockedBuffer *imgBuffer,
     79     int /*quality*/)
     80 {
     81     ALOGV("%s", __FUNCTION__);
     82 
     83     CpuConsumer::LockedBuffer *imgEncoded = new CpuConsumer::LockedBuffer;
     84     uint8_t *data = new uint8_t[ANDROID_JPEG_MAX_SIZE];
     85     imgEncoded->data = data;
     86     imgEncoded->width = imgBuffer->width;
     87     imgEncoded->height = imgBuffer->height;
     88     imgEncoded->stride = imgBuffer->stride;
     89 
     90     Vector<CpuConsumer::LockedBuffer*> buffers;
     91     buffers.push_back(imgBuffer);
     92     buffers.push_back(imgEncoded);
     93 
     94     sp<JpegCompressor> jpeg = new JpegCompressor();
     95     jpeg->start(buffers, 1);
     96 
     97     bool success = jpeg->waitForDone(10 * 1e9);
     98     if(success) {
     99         return buffers[1];
    100     }
    101     else {
    102         ALOGE("%s: JPEG encode timed out", __FUNCTION__);
    103         return NULL;  // TODO: maybe change function return value to status_t
    104     }
    105 }
    106 
    107 status_t BurstCapture::processFrameAvailable(sp<Camera2Client> &/*client*/) {
    108     ALOGE("Not implemented");
    109     return INVALID_OPERATION;
    110 }
    111 
    112 } // namespace camera2
    113 } // namespace android
    114