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