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