Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 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_CAMERACLIENT_H |
| 18 | #define ANDROID_SERVERS_CAMERA_CAMERACLIENT_H |
| 19 | |
| 20 | #include "CameraService.h" |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | class MemoryHeapBase; |
| 25 | class CameraHardwareInterface; |
| 26 | |
Eino-Ville Talvala | b99c5b8 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 27 | /** |
| 28 | * Interface between android.hardware.Camera API and Camera HAL device for version |
| 29 | * CAMERA_DEVICE_API_VERSION_1_0. |
| 30 | */ |
| 31 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 32 | class CameraClient : public CameraService::Client |
| 33 | { |
| 34 | public: |
| 35 | // ICamera interface (see ICamera for details) |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 36 | virtual binder::Status disconnect(); |
| 37 | virtual status_t connect(const sp<hardware::ICameraClient>& client); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 38 | virtual status_t lock(); |
| 39 | virtual status_t unlock(); |
Eino-Ville Talvala | 1ce7c34 | 2013-08-21 13:57:21 -0700 | [diff] [blame] | 40 | virtual status_t setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 41 | virtual void setPreviewCallbackFlag(int flag); |
Eino-Ville Talvala | 3ee3550 | 2013-04-02 15:45:11 -0700 | [diff] [blame] | 42 | virtual status_t setPreviewCallbackTarget( |
| 43 | const sp<IGraphicBufferProducer>& callbackProducer); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 44 | virtual status_t startPreview(); |
| 45 | virtual void stopPreview(); |
| 46 | virtual bool previewEnabled(); |
Chien-Yu Chen | 8cca075 | 2015-11-13 15:28:48 -0800 | [diff] [blame] | 47 | virtual status_t setVideoBufferMode(int32_t videoBufferMode); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 48 | virtual status_t startRecording(); |
| 49 | virtual void stopRecording(); |
| 50 | virtual bool recordingEnabled(); |
| 51 | virtual void releaseRecordingFrame(const sp<IMemory>& mem); |
Chien-Yu Chen | 2d13b1d | 2016-04-28 12:11:20 -0700 | [diff] [blame] | 52 | virtual void releaseRecordingFrameHandle(native_handle_t *handle); |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 53 | virtual void releaseRecordingFrameHandleBatch( |
| 54 | const std::vector<native_handle_t*>& handles); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 55 | virtual status_t autoFocus(); |
| 56 | virtual status_t cancelAutoFocus(); |
| 57 | virtual status_t takePicture(int msgType); |
| 58 | virtual status_t setParameters(const String8& params); |
| 59 | virtual String8 getParameters() const; |
| 60 | virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); |
Chien-Yu Chen | 8cca075 | 2015-11-13 15:28:48 -0800 | [diff] [blame] | 61 | virtual status_t setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer); |
Yin-Chia Yeh | cfab4e1 | 2019-09-09 13:08:28 -0700 | [diff] [blame] | 62 | virtual status_t setAudioRestriction(int mode); |
| 63 | virtual int32_t getGlobalAudioRestriction(); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 64 | |
| 65 | // Interface used by CameraService |
| 66 | CameraClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 67 | const sp<hardware::ICameraClient>& cameraClient, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 68 | const String16& clientPackageName, |
Philip P. Moltmann | 9e648f6 | 2019-11-04 12:52:45 -0800 | [diff] [blame] | 69 | const std::unique_ptr<String16>& clientFeatureId, |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 70 | int cameraId, |
| 71 | int cameraFacing, |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 72 | int clientPid, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 73 | int clientUid, |
Shuzhen Wang | 06fcfb0 | 2018-07-30 18:23:31 -0700 | [diff] [blame] | 74 | int servicePid); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 75 | ~CameraClient(); |
| 76 | |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 77 | virtual status_t initialize(sp<CameraProviderManager> manager, |
| 78 | const String8& monitorTags) override; |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 79 | |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 80 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 81 | |
| 82 | virtual status_t dumpClient(int fd, const Vector<String16>& args); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 83 | |
| 84 | private: |
| 85 | |
| 86 | // check whether the calling process matches mClientPid. |
| 87 | status_t checkPid() const; |
| 88 | status_t checkPidAndHardware() const; // also check mHardware != 0 |
| 89 | |
| 90 | // these are internal functions used to set up preview buffers |
| 91 | status_t registerPreviewBuffers(); |
| 92 | |
| 93 | // camera operation mode |
| 94 | enum camera_mode { |
| 95 | CAMERA_PREVIEW_MODE = 0, // frame automatically released |
| 96 | CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame() |
| 97 | }; |
| 98 | // these are internal functions used for preview/recording |
| 99 | status_t startCameraMode(camera_mode mode); |
| 100 | status_t startPreviewMode(); |
| 101 | status_t startRecordingMode(); |
| 102 | |
| 103 | // internal function used by sendCommand to enable/disable shutter sound. |
| 104 | status_t enableShutterSound(bool enable); |
| 105 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 106 | static sp<CameraClient> getClientFromCookie(void* user); |
| 107 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 108 | // these are static callback functions |
| 109 | static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user); |
| 110 | static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, |
| 111 | camera_frame_metadata_t *metadata, void* user); |
| 112 | static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user); |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 113 | static void handleCallbackTimestampBatch( |
| 114 | int32_t msgType, const std::vector<HandleTimestampMessage>&, void* user); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 115 | // handlers for messages |
| 116 | void handleShutter(void); |
| 117 | void handlePreviewData(int32_t msgType, const sp<IMemory>& mem, |
| 118 | camera_frame_metadata_t *metadata); |
| 119 | void handlePostview(const sp<IMemory>& mem); |
| 120 | void handleRawPicture(const sp<IMemory>& mem); |
| 121 | void handleCompressedPicture(const sp<IMemory>& mem); |
| 122 | void handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2); |
| 123 | void handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr, |
| 124 | camera_frame_metadata_t *metadata); |
| 125 | void handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); |
| 126 | |
| 127 | void copyFrameAndPostCopiedFrame( |
| 128 | int32_t msgType, |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 129 | const sp<hardware::ICameraClient>& client, |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 130 | const sp<IMemoryHeap>& heap, |
| 131 | size_t offset, size_t size, |
| 132 | camera_frame_metadata_t *metadata); |
| 133 | |
| 134 | int getOrientation(int orientation, bool mirror); |
| 135 | |
| 136 | status_t setPreviewWindow( |
| 137 | const sp<IBinder>& binder, |
| 138 | const sp<ANativeWindow>& window); |
| 139 | |
| 140 | |
| 141 | // these are initialized in the constructor. |
| 142 | sp<CameraHardwareInterface> mHardware; // cleared after disconnect() |
| 143 | int mPreviewCallbackFlag; |
| 144 | int mOrientation; // Current display orientation |
| 145 | bool mPlayShutterSound; |
Igor Murashkin | a858ea0 | 2014-08-19 14:53:08 -0700 | [diff] [blame] | 146 | bool mLegacyMode; // camera2 api legacy mode? |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 147 | |
| 148 | // Ensures atomicity among the public methods |
| 149 | mutable Mutex mLock; |
Mathias Agopian | 1a2952a | 2013-02-14 17:11:27 -0800 | [diff] [blame] | 150 | // This is a binder of Surface or Surface. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 151 | sp<IBinder> mSurface; |
| 152 | sp<ANativeWindow> mPreviewWindow; |
| 153 | |
| 154 | // If the user want us to return a copy of the preview frame (instead |
| 155 | // of the original one), we allocate mPreviewBuffer and reuse it if possible. |
| 156 | sp<MemoryHeapBase> mPreviewBuffer; |
| 157 | |
Igor Murashkin | fcf5fea | 2014-09-11 14:43:24 -0700 | [diff] [blame] | 158 | // Debugging information |
| 159 | CameraParameters mLatestSetParameters; |
| 160 | |
Chien-Yu Chen | 2d13b1d | 2016-04-28 12:11:20 -0700 | [diff] [blame] | 161 | // mAvailableCallbackBuffers stores sp<IMemory> that HAL uses to send VideoNativeHandleMetadata. |
| 162 | // It will be used to send VideoNativeHandleMetadata back to HAL when camera receives the |
| 163 | // native handle from releaseRecordingFrameHandle. |
| 164 | Mutex mAvailableCallbackBuffersLock; |
| 165 | std::vector<sp<IMemory>> mAvailableCallbackBuffers; |
| 166 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 167 | // We need to avoid the deadlock when the incoming command thread and |
| 168 | // the CameraHardwareInterface callback thread both want to grab mLock. |
| 169 | // An extra flag is used to tell the callback thread that it should stop |
| 170 | // trying to deliver the callback messages if the client is not |
| 171 | // interested in it anymore. For example, if the client is calling |
| 172 | // stopPreview(), the preview frame messages do not need to be delivered |
| 173 | // anymore. |
| 174 | |
| 175 | // This function takes the same parameter as the enableMsgType() and |
| 176 | // disableMsgType() functions in CameraHardwareInterface. |
| 177 | void enableMsgType(int32_t msgType); |
| 178 | void disableMsgType(int32_t msgType); |
| 179 | volatile int32_t mMsgEnabled; |
| 180 | |
| 181 | // This function keeps trying to grab mLock, or give up if the message |
| 182 | // is found to be disabled. It returns true if mLock is grabbed. |
| 183 | bool lockIfMessageWanted(int32_t msgType); |
| 184 | }; |
| 185 | |
| 186 | } |
| 187 | |
| 188 | #endif |