blob: a270f90a3bd2e4500df1bab125dcbe1da9451fd2 [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)
36 virtual void disconnect();
37 virtual status_t connect(const sp<ICameraClient>& client);
38 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();
47 virtual status_t storeMetaDataInBuffers(bool enabled);
48 virtual status_t startRecording();
49 virtual void stopRecording();
50 virtual bool recordingEnabled();
51 virtual void releaseRecordingFrame(const sp<IMemory>& mem);
52 virtual status_t autoFocus();
53 virtual status_t cancelAutoFocus();
54 virtual status_t takePicture(int msgType);
55 virtual status_t setParameters(const String8& params);
56 virtual String8 getParameters() const;
57 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
58
59 // Interface used by CameraService
60 CameraClient(const sp<CameraService>& cameraService,
61 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080062 const String16& clientPackageName,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070063 int cameraId,
64 int cameraFacing,
Igor Murashkinecf17e82012-10-02 16:05:11 -070065 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080066 int clientUid,
Igor Murashkinecf17e82012-10-02 16:05:11 -070067 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070068 ~CameraClient();
69
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070070 status_t initialize(camera_module_t *module);
71
Eino-Ville Talvalac9ab2b02016-01-13 10:07:04 -080072 virtual status_t dump(int fd, const Vector<String16>& args);
73
74 virtual status_t dumpClient(int fd, const Vector<String16>& args);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070075
76private:
77
78 // check whether the calling process matches mClientPid.
79 status_t checkPid() const;
80 status_t checkPidAndHardware() const; // also check mHardware != 0
81
82 // these are internal functions used to set up preview buffers
83 status_t registerPreviewBuffers();
84
85 // camera operation mode
86 enum camera_mode {
87 CAMERA_PREVIEW_MODE = 0, // frame automatically released
88 CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame()
89 };
90 // these are internal functions used for preview/recording
91 status_t startCameraMode(camera_mode mode);
92 status_t startPreviewMode();
93 status_t startRecordingMode();
94
95 // internal function used by sendCommand to enable/disable shutter sound.
96 status_t enableShutterSound(bool enable);
97
98 // these are static callback functions
99 static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user);
100 static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
101 camera_frame_metadata_t *metadata, void* user);
102 static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user);
103 // handlers for messages
104 void handleShutter(void);
105 void handlePreviewData(int32_t msgType, const sp<IMemory>& mem,
106 camera_frame_metadata_t *metadata);
107 void handlePostview(const sp<IMemory>& mem);
108 void handleRawPicture(const sp<IMemory>& mem);
109 void handleCompressedPicture(const sp<IMemory>& mem);
110 void handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2);
111 void handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr,
112 camera_frame_metadata_t *metadata);
113 void handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
114
115 void copyFrameAndPostCopiedFrame(
116 int32_t msgType,
117 const sp<ICameraClient>& client,
118 const sp<IMemoryHeap>& heap,
119 size_t offset, size_t size,
120 camera_frame_metadata_t *metadata);
121
122 int getOrientation(int orientation, bool mirror);
123
124 status_t setPreviewWindow(
125 const sp<IBinder>& binder,
126 const sp<ANativeWindow>& window);
127
128
129 // these are initialized in the constructor.
130 sp<CameraHardwareInterface> mHardware; // cleared after disconnect()
131 int mPreviewCallbackFlag;
132 int mOrientation; // Current display orientation
133 bool mPlayShutterSound;
134
135 // Ensures atomicity among the public methods
136 mutable Mutex mLock;
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800137 // This is a binder of Surface or Surface.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700138 sp<IBinder> mSurface;
139 sp<ANativeWindow> mPreviewWindow;
140
141 // If the user want us to return a copy of the preview frame (instead
142 // of the original one), we allocate mPreviewBuffer and reuse it if possible.
143 sp<MemoryHeapBase> mPreviewBuffer;
144
145 // We need to avoid the deadlock when the incoming command thread and
146 // the CameraHardwareInterface callback thread both want to grab mLock.
147 // An extra flag is used to tell the callback thread that it should stop
148 // trying to deliver the callback messages if the client is not
149 // interested in it anymore. For example, if the client is calling
150 // stopPreview(), the preview frame messages do not need to be delivered
151 // anymore.
152
153 // This function takes the same parameter as the enableMsgType() and
154 // disableMsgType() functions in CameraHardwareInterface.
155 void enableMsgType(int32_t msgType);
156 void disableMsgType(int32_t msgType);
157 volatile int32_t mMsgEnabled;
158
159 // This function keeps trying to grab mLock, or give up if the message
160 // is found to be disabled. It returns true if mLock is grabbed.
161 bool lockIfMessageWanted(int32_t msgType);
162};
163
164}
165
166#endif