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