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 #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 "camera/CameraMetadata.h"
     25 
     26 namespace android {
     27 
     28 class Camera2Client;
     29 class CameraDeviceBase;
     30 class IMemory;
     31 
     32 namespace camera2 {
     33 
     34 class Parameters;
     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<Surface> 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 setRecordingFormat(int format, android_dataspace_t dataspace);
     57 
     58     status_t updateRecordingRequest(const Parameters &params);
     59     // If needsUpdate is set to true, a updateRecordingStream call with params will recreate
     60     // recording stream
     61     status_t recordingStreamNeedsUpdate(const Parameters &params, bool *needsUpdate);
     62     status_t updateRecordingStream(const Parameters &params);
     63     status_t deleteRecordingStream();
     64     int getRecordingStreamId() const;
     65 
     66     enum StreamType {
     67         NONE,
     68         PREVIEW,
     69         RECORD
     70     };
     71     status_t startStream(StreamType type,
     72             const Vector<int32_t> &outputStreams);
     73 
     74     // Toggle between paused and unpaused. Stream must be started first.
     75     status_t togglePauseStream(bool pause);
     76 
     77     status_t stopStream();
     78 
     79     // Returns the request ID for the currently streaming request
     80     // Returns 0 if there is no active request.
     81     status_t getActiveRequestId() const;
     82     status_t incrementStreamingIds();
     83 
     84     // Callback for new recording frames from HAL
     85     virtual void onFrameAvailable(const BufferItem& item);
     86     // Callback from stagefright which returns used recording frames
     87     void releaseRecordingFrame(const sp<IMemory>& mem);
     88 
     89     status_t dump(int fd, const Vector<String16>& args);
     90 
     91   private:
     92     mutable Mutex mMutex;
     93 
     94     enum {
     95         NO_STREAM = -1
     96     };
     97 
     98     wp<Camera2Client> mClient;
     99     wp<CameraDeviceBase> mDevice;
    100     int mId;
    101 
    102     StreamType mActiveRequest;
    103     bool mPaused;
    104 
    105     Vector<int32_t> mActiveStreamIds;
    106 
    107     // Preview-related members
    108     int32_t mPreviewRequestId;
    109     int mPreviewStreamId;
    110     CameraMetadata mPreviewRequest;
    111     sp<Surface> mPreviewWindow;
    112 
    113     // Recording-related members
    114     static const nsecs_t kWaitDuration = 50000000; // 50 ms
    115 
    116     int32_t mRecordingRequestId;
    117     int mRecordingStreamId;
    118     int mRecordingFrameCount;
    119     sp<BufferItemConsumer> mRecordingConsumer;
    120     sp<Surface>  mRecordingWindow;
    121     CameraMetadata mRecordingRequest;
    122     sp<camera2::Camera2Heap> mRecordingHeap;
    123 
    124     bool mRecordingFrameAvailable;
    125     Condition mRecordingFrameAvailableSignal;
    126 
    127     static const size_t kDefaultRecordingHeapCount = 8;
    128     size_t mRecordingHeapCount;
    129     Vector<BufferItem> mRecordingBuffers;
    130     size_t mRecordingHeapHead, mRecordingHeapFree;
    131 
    132     static const int kDefaultRecordingFormat =
    133             HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
    134     int mRecordingFormat;
    135 
    136     static const android_dataspace kDefaultRecordingDataSpace =
    137             HAL_DATASPACE_BT709;
    138     android_dataspace mRecordingDataSpace;
    139 
    140     static const int kDefaultRecordingGrallocUsage =
    141             GRALLOC_USAGE_HW_VIDEO_ENCODER;
    142     int mRecordingGrallocUsage;
    143 
    144     virtual bool threadLoop();
    145 
    146     status_t processRecordingFrame();
    147 
    148     // Unilaterally free any buffers still outstanding to stagefright
    149     void releaseAllRecordingFramesLocked();
    150 
    151     // Determine if the specified stream is currently in use
    152     static bool isStreamActive(const Vector<int32_t> &streams,
    153             int32_t recordingStreamId);
    154 };
    155 
    156 
    157 }; // namespace camera2
    158 }; // namespace android
    159 
    160 #endif
    161