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