Home | History | Annotate | Download | only in camera2
      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 #ifndef ANDROID_SERVERS_CAMERA_CAMERA2_STREAMINGPROCESSOR_H
     18 #define ANDROID_SERVERS_CAMERA_CAMERA2_STREAMINGPROCESSOR_H
     19 
     20 #include <utils/Mutex.h>
     21 #include <utils/String16.h>
     22 #include <gui/BufferItemConsumer.h>
     23 
     24 #include "Parameters.h"
     25 #include "camera/CameraMetadata.h"
     26 
     27 namespace android {
     28 
     29 class Camera2Client;
     30 class CameraDeviceBase;
     31 class IMemory;
     32 
     33 namespace camera2 {
     34 
     35 class Camera2Heap;
     36 
     37 /**
     38  * Management and processing for preview and recording streams
     39  */
     40 class StreamingProcessor:
     41             public Thread, public BufferItemConsumer::FrameAvailableListener {
     42   public:
     43     StreamingProcessor(sp<Camera2Client> client);
     44     ~StreamingProcessor();
     45 
     46     status_t setPreviewWindow(sp<ANativeWindow> window);
     47 
     48     bool haveValidPreviewWindow() const;
     49 
     50     status_t updatePreviewRequest(const Parameters &params);
     51     status_t updatePreviewStream(const Parameters &params);
     52     status_t deletePreviewStream();
     53     int getPreviewStreamId() const;
     54 
     55     status_t setRecordingBufferCount(size_t count);
     56     status_t updateRecordingRequest(const Parameters &params);
     57     status_t updateRecordingStream(const Parameters &params);
     58     status_t deleteRecordingStream();
     59     int getRecordingStreamId() const;
     60 
     61     enum StreamType {
     62         NONE,
     63         PREVIEW,
     64         RECORD
     65     };
     66     status_t startStream(StreamType type,
     67             const Vector<uint8_t> &outputStreams);
     68 
     69     // Toggle between paused and unpaused. Stream must be started first.
     70     status_t togglePauseStream(bool pause);
     71 
     72     status_t stopStream();
     73 
     74     // Returns the request ID for the currently streaming request
     75     // Returns 0 if there is no active request.
     76     status_t getActiveRequestId() const;
     77     status_t incrementStreamingIds();
     78 
     79     // Callback for new recording frames from HAL
     80     virtual void onFrameAvailable();
     81     // Callback from stagefright which returns used recording frames
     82     void releaseRecordingFrame(const sp<IMemory>& mem);
     83 
     84     status_t dump(int fd, const Vector<String16>& args);
     85 
     86   private:
     87     mutable Mutex mMutex;
     88 
     89     enum {
     90         NO_STREAM = -1
     91     };
     92 
     93     wp<Camera2Client> mClient;
     94     wp<CameraDeviceBase> mDevice;
     95     int mId;
     96 
     97     StreamType mActiveRequest;
     98     bool mPaused;
     99 
    100     Vector<uint8_t> mActiveStreamIds;
    101 
    102     // Preview-related members
    103     int32_t mPreviewRequestId;
    104     int mPreviewStreamId;
    105     CameraMetadata mPreviewRequest;
    106     sp<ANativeWindow> mPreviewWindow;
    107 
    108     // Recording-related members
    109     static const nsecs_t kWaitDuration = 50000000; // 50 ms
    110 
    111     int32_t mRecordingRequestId;
    112     int mRecordingStreamId;
    113     int mRecordingFrameCount;
    114     sp<BufferItemConsumer> mRecordingConsumer;
    115     sp<ANativeWindow>  mRecordingWindow;
    116     CameraMetadata mRecordingRequest;
    117     sp<camera2::Camera2Heap> mRecordingHeap;
    118 
    119     bool mRecordingFrameAvailable;
    120     Condition mRecordingFrameAvailableSignal;
    121 
    122     static const size_t kDefaultRecordingHeapCount = 8;
    123     size_t mRecordingHeapCount;
    124     Vector<BufferItemConsumer::BufferItem> mRecordingBuffers;
    125     size_t mRecordingHeapHead, mRecordingHeapFree;
    126 
    127     virtual bool threadLoop();
    128 
    129     status_t processRecordingFrame();
    130 
    131     // Unilaterally free any buffers still outstanding to stagefright
    132     void releaseAllRecordingFramesLocked();
    133 
    134     // Determine if the specified stream is currently in use
    135     static bool isStreamActive(const Vector<uint8_t> &streams,
    136             uint8_t recordingStreamId);
    137 };
    138 
    139 
    140 }; // namespace camera2
    141 }; // namespace android
    142 
    143 #endif
    144