Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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_PHOTOGRAPHY_CAMERADEVICECLIENT_H |
| 18 | #define ANDROID_SERVERS_CAMERA_PHOTOGRAPHY_CAMERADEVICECLIENT_H |
| 19 | |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 20 | #include <camera/camera2/ICameraDeviceUser.h> |
| 21 | #include <camera/camera2/ICameraDeviceCallbacks.h> |
Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 22 | #include <camera/camera2/OutputConfiguration.h> |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 23 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 24 | #include "CameraService.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 25 | #include "common/FrameProcessorBase.h" |
| 26 | #include "common/Camera2ClientBase.h" |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
Ruben Brunk | 77a77f6 | 2015-06-12 16:15:35 -0700 | [diff] [blame] | 30 | struct CameraDeviceClientBase : public CameraService::BasicClient, public BnCameraDeviceUser |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 31 | { |
| 32 | typedef ICameraDeviceCallbacks TCamCallbacks; |
| 33 | |
| 34 | const sp<ICameraDeviceCallbacks>& getRemoteCallback() { |
| 35 | return mRemoteCallback; |
| 36 | } |
| 37 | |
| 38 | protected: |
| 39 | CameraDeviceClientBase(const sp<CameraService>& cameraService, |
| 40 | const sp<ICameraDeviceCallbacks>& remoteCallback, |
| 41 | const String16& clientPackageName, |
| 42 | int cameraId, |
| 43 | int cameraFacing, |
| 44 | int clientPid, |
| 45 | uid_t clientUid, |
| 46 | int servicePid); |
| 47 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 48 | sp<ICameraDeviceCallbacks> mRemoteCallback; |
| 49 | }; |
| 50 | |
| 51 | /** |
| 52 | * Implements the binder ICameraDeviceUser API, |
| 53 | * meant for HAL3-public implementation of |
| 54 | * android.hardware.photography.CameraDevice |
| 55 | */ |
| 56 | class CameraDeviceClient : |
| 57 | public Camera2ClientBase<CameraDeviceClientBase>, |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 58 | public camera2::FrameProcessorBase::FilteredListener |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 59 | { |
| 60 | public: |
| 61 | /** |
| 62 | * ICameraDeviceUser interface (see ICameraDeviceUser for details) |
| 63 | */ |
| 64 | |
| 65 | // Note that the callee gets a copy of the metadata. |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 66 | virtual status_t submitRequest(sp<CaptureRequest> request, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 67 | bool streaming = false, |
| 68 | /*out*/ |
| 69 | int64_t* lastFrameNumber = NULL); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 70 | // List of requests are copied. |
| 71 | virtual status_t submitRequestList(List<sp<CaptureRequest> > requests, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 72 | bool streaming = false, |
| 73 | /*out*/ |
| 74 | int64_t* lastFrameNumber = NULL); |
| 75 | virtual status_t cancelRequest(int requestId, |
| 76 | /*out*/ |
| 77 | int64_t* lastFrameNumber = NULL); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 78 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 79 | virtual status_t beginConfigure(); |
| 80 | |
Zhijun He | 1fa8999 | 2015-06-01 15:44:31 -0700 | [diff] [blame] | 81 | virtual status_t endConfigure(bool isConstrainedHighSpeed = false); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 82 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 83 | // Returns -EBUSY if device is not idle |
| 84 | virtual status_t deleteStream(int streamId); |
| 85 | |
Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 86 | virtual status_t createStream(const OutputConfiguration &outputConfiguration); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 87 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 88 | // Create an input stream of width, height, and format. |
| 89 | virtual status_t createInputStream(int width, int height, int format); |
| 90 | |
| 91 | // Get the buffer producer of the input stream |
| 92 | virtual status_t getInputBufferProducer( |
| 93 | /*out*/sp<IGraphicBufferProducer> *producer); |
| 94 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 95 | // Create a request object from a template. |
| 96 | virtual status_t createDefaultRequest(int templateId, |
| 97 | /*out*/ |
| 98 | CameraMetadata* request); |
| 99 | |
| 100 | // Get the static metadata for the camera |
| 101 | // -- Caller owns the newly allocated metadata |
Igor Murashkin | 099b457 | 2013-07-12 17:52:16 -0700 | [diff] [blame] | 102 | virtual status_t getCameraInfo(/*out*/CameraMetadata* info); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 103 | |
Zhijun He | 2ab500c | 2013-07-23 08:02:53 -0700 | [diff] [blame] | 104 | // Wait until all the submitted requests have finished processing |
| 105 | virtual status_t waitUntilIdle(); |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 106 | |
| 107 | // Flush all active and pending requests as fast as possible |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 108 | virtual status_t flush(/*out*/ |
| 109 | int64_t* lastFrameNumber = NULL); |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 110 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 111 | // Prepare stream by preallocating its buffers |
| 112 | virtual status_t prepare(int streamId); |
| 113 | |
Eino-Ville Talvala | b25e3c8 | 2015-07-15 16:04:27 -0700 | [diff] [blame] | 114 | // Tear down stream resources by freeing its unused buffers |
| 115 | virtual status_t tearDown(int streamId); |
| 116 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 117 | /** |
| 118 | * Interface used by CameraService |
| 119 | */ |
| 120 | |
| 121 | CameraDeviceClient(const sp<CameraService>& cameraService, |
| 122 | const sp<ICameraDeviceCallbacks>& remoteCallback, |
| 123 | const String16& clientPackageName, |
| 124 | int cameraId, |
| 125 | int cameraFacing, |
| 126 | int clientPid, |
| 127 | uid_t clientUid, |
| 128 | int servicePid); |
| 129 | virtual ~CameraDeviceClient(); |
| 130 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 131 | virtual status_t initialize(CameraModule *module); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 132 | |
| 133 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 134 | |
| 135 | /** |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 136 | * Device listener interface |
| 137 | */ |
| 138 | |
| 139 | virtual void notifyIdle(); |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 140 | virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 141 | const CaptureResultExtras& resultExtras); |
| 142 | virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 143 | virtual void notifyPrepared(int streamId); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 144 | |
| 145 | /** |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 146 | * Interface used by independent components of CameraDeviceClient. |
| 147 | */ |
| 148 | protected: |
| 149 | /** FilteredListener implementation **/ |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 150 | virtual void onResultAvailable(const CaptureResult& result); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 151 | virtual void detachDevice(); |
| 152 | |
Igor Murashkin | f8b2a6f | 2013-09-17 17:03:28 -0700 | [diff] [blame] | 153 | // Calculate the ANativeWindow transform from android.sensor.orientation |
| 154 | status_t getRotationTransformLocked(/*out*/int32_t* transform); |
| 155 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 156 | private: |
| 157 | /** ICameraDeviceUser interface-related private members */ |
| 158 | |
| 159 | /** Preview callback related members */ |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 160 | sp<camera2::FrameProcessorBase> mFrameProcessor; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 161 | static const int32_t FRAME_PROCESSOR_LISTENER_MIN_ID = 0; |
| 162 | static const int32_t FRAME_PROCESSOR_LISTENER_MAX_ID = 0x7fffffffL; |
| 163 | |
| 164 | /** Utility members */ |
| 165 | bool enforceRequestPermissions(CameraMetadata& metadata); |
| 166 | |
Ruben Brunk | bba7557 | 2014-11-20 17:29:50 -0800 | [diff] [blame] | 167 | // Find the square of the euclidean distance between two points |
| 168 | static int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1); |
| 169 | |
| 170 | // Find the closest dimensions for a given format in available stream configurations with |
| 171 | // a width <= ROUNDING_WIDTH_CAP |
Ruben Brunk | 77a77f6 | 2015-06-12 16:15:35 -0700 | [diff] [blame] | 172 | static const int32_t ROUNDING_WIDTH_CAP = 1920; |
Ruben Brunk | bba7557 | 2014-11-20 17:29:50 -0800 | [diff] [blame] | 173 | static bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 174 | android_dataspace dataSpace, const CameraMetadata& info, |
| 175 | /*out*/int32_t* outWidth, /*out*/int32_t* outHeight); |
Ruben Brunk | bba7557 | 2014-11-20 17:29:50 -0800 | [diff] [blame] | 176 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 177 | // IGraphicsBufferProducer binder -> Stream ID for output streams |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 178 | KeyedVector<sp<IBinder>, int> mStreamMap; |
| 179 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 180 | struct InputStreamConfiguration { |
| 181 | bool configured; |
| 182 | int32_t width; |
| 183 | int32_t height; |
| 184 | int32_t format; |
| 185 | int32_t id; |
| 186 | } mInputStream; |
| 187 | |
| 188 | // Request ID |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 189 | Vector<int> mStreamingRequestList; |
| 190 | |
| 191 | int32_t mRequestIdCounter; |
| 192 | }; |
| 193 | |
| 194 | }; // namespace android |
| 195 | |
| 196 | #endif |