blob: b26b61292cf1115c5161feea05d6e9c51272dd50 [file] [log] [blame]
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001/*
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
22namespace android {
23
24class MemoryHeapBase;
25class CameraHardwareInterface;
26
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080027/**
28 * Interface between android.hardware.Camera API and Camera HAL device for version
29 * CAMERA_DEVICE_API_VERSION_1_0.
30 */
31
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070032class CameraClient : public CameraService::Client
33{
34public:
35 // ICamera interface (see ICamera for details)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080036 virtual binder::Status disconnect();
37 virtual status_t connect(const sp<hardware::ICameraClient>& client);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070038 virtual status_t lock();
39 virtual status_t unlock();
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -070040 virtual status_t setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070041 virtual void setPreviewCallbackFlag(int flag);
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -070042 virtual status_t setPreviewCallbackTarget(
43 const sp<IGraphicBufferProducer>& callbackProducer);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070044 virtual status_t startPreview();
45 virtual void stopPreview();
46 virtual bool previewEnabled();
Chien-Yu Chen8cca0752015-11-13 15:28:48 -080047 virtual status_t setVideoBufferMode(int32_t videoBufferMode);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070048 virtual status_t startRecording();
49 virtual void stopRecording();
50 virtual bool recordingEnabled();
51 virtual void releaseRecordingFrame(const sp<IMemory>& mem);
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -070052 virtual void releaseRecordingFrameHandle(native_handle_t *handle);
Yin-Chia Yehb5df5472017-03-20 19:32:19 -070053 virtual void releaseRecordingFrameHandleBatch(
54 const std::vector<native_handle_t*>& handles);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070055 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 Chen8cca0752015-11-13 15:28:48 -080061 virtual status_t setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -070062 virtual status_t setAudioRestriction(int mode);
63 virtual int32_t getGlobalAudioRestriction();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070064
65 // Interface used by CameraService
66 CameraClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080067 const sp<hardware::ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080068 const String16& clientPackageName,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070069 int cameraId,
70 int cameraFacing,
Igor Murashkinecf17e82012-10-02 16:05:11 -070071 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080072 int clientUid,
Shuzhen Wang06fcfb02018-07-30 18:23:31 -070073 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070074 ~CameraClient();
75
Emilian Peevbd8c5032018-02-14 23:05:40 +000076 virtual status_t initialize(sp<CameraProviderManager> manager,
77 const String8& monitorTags) override;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070078
Eino-Ville Talvalac4003962016-01-13 10:07:04 -080079 virtual status_t dump(int fd, const Vector<String16>& args);
80
81 virtual status_t dumpClient(int fd, const Vector<String16>& args);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070082
83private:
84
85 // check whether the calling process matches mClientPid.
86 status_t checkPid() const;
87 status_t checkPidAndHardware() const; // also check mHardware != 0
88
89 // these are internal functions used to set up preview buffers
90 status_t registerPreviewBuffers();
91
92 // camera operation mode
93 enum camera_mode {
94 CAMERA_PREVIEW_MODE = 0, // frame automatically released
95 CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame()
96 };
97 // these are internal functions used for preview/recording
98 status_t startCameraMode(camera_mode mode);
99 status_t startPreviewMode();
100 status_t startRecordingMode();
101
102 // internal function used by sendCommand to enable/disable shutter sound.
103 status_t enableShutterSound(bool enable);
104
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800105 static sp<CameraClient> getClientFromCookie(void* user);
106
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700107 // these are static callback functions
108 static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user);
109 static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
110 camera_frame_metadata_t *metadata, void* user);
111 static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user);
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700112 static void handleCallbackTimestampBatch(
113 int32_t msgType, const std::vector<HandleTimestampMessage>&, void* user);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700114 // handlers for messages
115 void handleShutter(void);
116 void handlePreviewData(int32_t msgType, const sp<IMemory>& mem,
117 camera_frame_metadata_t *metadata);
118 void handlePostview(const sp<IMemory>& mem);
119 void handleRawPicture(const sp<IMemory>& mem);
120 void handleCompressedPicture(const sp<IMemory>& mem);
121 void handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2);
122 void handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr,
123 camera_frame_metadata_t *metadata);
124 void handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
125
126 void copyFrameAndPostCopiedFrame(
127 int32_t msgType,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800128 const sp<hardware::ICameraClient>& client,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700129 const sp<IMemoryHeap>& heap,
130 size_t offset, size_t size,
131 camera_frame_metadata_t *metadata);
132
133 int getOrientation(int orientation, bool mirror);
134
135 status_t setPreviewWindow(
136 const sp<IBinder>& binder,
137 const sp<ANativeWindow>& window);
138
139
140 // these are initialized in the constructor.
141 sp<CameraHardwareInterface> mHardware; // cleared after disconnect()
142 int mPreviewCallbackFlag;
143 int mOrientation; // Current display orientation
144 bool mPlayShutterSound;
Igor Murashkina858ea02014-08-19 14:53:08 -0700145 bool mLegacyMode; // camera2 api legacy mode?
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700146
147 // Ensures atomicity among the public methods
148 mutable Mutex mLock;
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800149 // This is a binder of Surface or Surface.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700150 sp<IBinder> mSurface;
151 sp<ANativeWindow> mPreviewWindow;
152
153 // If the user want us to return a copy of the preview frame (instead
154 // of the original one), we allocate mPreviewBuffer and reuse it if possible.
155 sp<MemoryHeapBase> mPreviewBuffer;
156
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700157 // Debugging information
158 CameraParameters mLatestSetParameters;
159
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700160 // mAvailableCallbackBuffers stores sp<IMemory> that HAL uses to send VideoNativeHandleMetadata.
161 // It will be used to send VideoNativeHandleMetadata back to HAL when camera receives the
162 // native handle from releaseRecordingFrameHandle.
163 Mutex mAvailableCallbackBuffersLock;
164 std::vector<sp<IMemory>> mAvailableCallbackBuffers;
165
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700166 // We need to avoid the deadlock when the incoming command thread and
167 // the CameraHardwareInterface callback thread both want to grab mLock.
168 // An extra flag is used to tell the callback thread that it should stop
169 // trying to deliver the callback messages if the client is not
170 // interested in it anymore. For example, if the client is calling
171 // stopPreview(), the preview frame messages do not need to be delivered
172 // anymore.
173
174 // This function takes the same parameter as the enableMsgType() and
175 // disableMsgType() functions in CameraHardwareInterface.
176 void enableMsgType(int32_t msgType);
177 void disableMsgType(int32_t msgType);
178 volatile int32_t mMsgEnabled;
179
180 // This function keeps trying to grab mLock, or give up if the message
181 // is found to be disabled. It returns true if mLock is grabbed.
182 bool lockIfMessageWanted(int32_t msgType);
183};
184
185}
186
187#endif