1/*
2 * Copyright (C) 2016 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#ifndef _ACAMERA_CAPTURE_SESSION_H
17#define _ACAMERA_CAPTURE_SESSION_H
18
19#include <set>
20#include <hardware/camera3.h>
21#include <NdkCameraDevice.h>
22#include "ACameraDevice.h"
23
24using namespace android;
25
26struct ACaptureSessionOutput {
27    explicit ACaptureSessionOutput(ANativeWindow* window) : mWindow(window) {};
28
29    bool operator == (const ACaptureSessionOutput& other) const {
30        return mWindow == other.mWindow;
31    }
32    bool operator != (const ACaptureSessionOutput& other) const {
33        return mWindow != other.mWindow;
34    }
35    bool operator < (const ACaptureSessionOutput& other) const {
36        return mWindow < other.mWindow;
37    }
38    bool operator > (const ACaptureSessionOutput& other) const {
39        return mWindow > other.mWindow;
40    }
41
42    ANativeWindow* mWindow;
43    int            mRotation = CAMERA3_STREAM_ROTATION_0;
44};
45
46struct ACaptureSessionOutputContainer {
47    std::set<ACaptureSessionOutput> mOutputs;
48};
49
50/**
51 * ACameraCaptureSession opaque struct definition
52 * Leave outside of android namespace because it's NDK struct
53 */
54struct ACameraCaptureSession : public RefBase {
55  public:
56    ACameraCaptureSession(
57            int id,
58            const ACaptureSessionOutputContainer* outputs,
59            const ACameraCaptureSession_stateCallbacks* cb,
60            CameraDevice* device) :
61            mId(id), mOutput(*outputs), mUserSessionCallback(*cb),
62            mDevice(device) {}
63
64    // This can be called in app calling close() or after some app callback is finished
65    // Make sure the caller does not hold device or session lock!
66    ~ACameraCaptureSession();
67
68    // No API except Session_Close will work if device is closed
69    // A session will enter closed state when one of the following happens:
70    //     1. Explicitly closed by app
71    //     2. Replaced by a newer session
72    //     3. Device is closed
73    bool isClosed() { Mutex::Autolock _l(mSessionLock); return mIsClosed; }
74
75    // Close the session and mark app no longer need this session.
76    void closeByApp();
77
78    camera_status_t stopRepeating();
79
80    camera_status_t abortCaptures();
81
82    camera_status_t setRepeatingRequest(
83            /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
84            int numRequests, ACaptureRequest** requests,
85            /*optional*/int* captureSequenceId);
86
87    camera_status_t capture(
88            /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
89            int numRequests, ACaptureRequest** requests,
90            /*optional*/int* captureSequenceId);
91
92    ACameraDevice* getDevice();
93
94  private:
95    friend class CameraDevice;
96
97    // Close session because app close camera device, camera device got ERROR_DISCONNECTED,
98    // or a new session is replacing this session.
99    void closeByDevice();
100
101    sp<CameraDevice> getDeviceSp();
102
103    const int mId;
104    const ACaptureSessionOutputContainer mOutput;
105    const ACameraCaptureSession_stateCallbacks mUserSessionCallback;
106    const wp<CameraDevice> mDevice;
107    bool  mIsClosed = false;
108    bool  mClosedByApp = false;
109    Mutex mSessionLock;
110};
111
112#endif // _ACAMERA_CAPTURE_SESSION_H
113