blob: 256298d3df01c3c5e03694bec06cc0c901848486 [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);
36 virtual status_t setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture);
37 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 Talvala5e08d602012-05-16 14:59:25 -070056 int cameraId,
57 int cameraFacing,
58 int clientPid);
59 ~CameraClient();
60
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070061 status_t initialize(camera_module_t *module);
62
63 status_t dump(int fd, const Vector<String16>& args);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070064
65private:
66
67 // check whether the calling process matches mClientPid.
68 status_t checkPid() const;
69 status_t checkPidAndHardware() const; // also check mHardware != 0
70
71 // these are internal functions used to set up preview buffers
72 status_t registerPreviewBuffers();
73
74 // camera operation mode
75 enum camera_mode {
76 CAMERA_PREVIEW_MODE = 0, // frame automatically released
77 CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame()
78 };
79 // these are internal functions used for preview/recording
80 status_t startCameraMode(camera_mode mode);
81 status_t startPreviewMode();
82 status_t startRecordingMode();
83
84 // internal function used by sendCommand to enable/disable shutter sound.
85 status_t enableShutterSound(bool enable);
86
87 // these are static callback functions
88 static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user);
89 static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
90 camera_frame_metadata_t *metadata, void* user);
91 static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user);
92 // handlers for messages
93 void handleShutter(void);
94 void handlePreviewData(int32_t msgType, const sp<IMemory>& mem,
95 camera_frame_metadata_t *metadata);
96 void handlePostview(const sp<IMemory>& mem);
97 void handleRawPicture(const sp<IMemory>& mem);
98 void handleCompressedPicture(const sp<IMemory>& mem);
99 void handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2);
100 void handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr,
101 camera_frame_metadata_t *metadata);
102 void handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
103
104 void copyFrameAndPostCopiedFrame(
105 int32_t msgType,
106 const sp<ICameraClient>& client,
107 const sp<IMemoryHeap>& heap,
108 size_t offset, size_t size,
109 camera_frame_metadata_t *metadata);
110
111 int getOrientation(int orientation, bool mirror);
112
113 status_t setPreviewWindow(
114 const sp<IBinder>& binder,
115 const sp<ANativeWindow>& window);
116
117
118 // these are initialized in the constructor.
119 sp<CameraHardwareInterface> mHardware; // cleared after disconnect()
120 int mPreviewCallbackFlag;
121 int mOrientation; // Current display orientation
122 bool mPlayShutterSound;
123
124 // Ensures atomicity among the public methods
125 mutable Mutex mLock;
126 // This is a binder of Surface or SurfaceTexture.
127 sp<IBinder> mSurface;
128 sp<ANativeWindow> mPreviewWindow;
129
130 // If the user want us to return a copy of the preview frame (instead
131 // of the original one), we allocate mPreviewBuffer and reuse it if possible.
132 sp<MemoryHeapBase> mPreviewBuffer;
133
134 // We need to avoid the deadlock when the incoming command thread and
135 // the CameraHardwareInterface callback thread both want to grab mLock.
136 // An extra flag is used to tell the callback thread that it should stop
137 // trying to deliver the callback messages if the client is not
138 // interested in it anymore. For example, if the client is calling
139 // stopPreview(), the preview frame messages do not need to be delivered
140 // anymore.
141
142 // This function takes the same parameter as the enableMsgType() and
143 // disableMsgType() functions in CameraHardwareInterface.
144 void enableMsgType(int32_t msgType);
145 void disableMsgType(int32_t msgType);
146 volatile int32_t mMsgEnabled;
147
148 // This function keeps trying to grab mLock, or give up if the message
149 // is found to be disabled. It returns true if mLock is grabbed.
150 bool lockIfMessageWanted(int32_t msgType);
151};
152
153}
154
155#endif