blob: 812b9280aa0bf3325bccdc684ec7e5f2db5cd2b0 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2**
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08003** Copyright (C) 2008, The Android Open Source Project
4** Copyright (C) 2008 HTC Inc.
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07005**
6** Licensed under the Apache License, Version 2.0 (the "License");
7** you may not use this file except in compliance with the License.
8** You may obtain a copy of the License at
9**
10** http://www.apache.org/licenses/LICENSE-2.0
11**
12** Unless required by applicable law or agreed to in writing, software
13** distributed under the License is distributed on an "AS IS" BASIS,
14** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15** See the License for the specific language governing permissions and
16** limitations under the License.
17*/
18
19#ifndef ANDROID_SERVERS_CAMERA_CAMERASERVICE_H
20#define ANDROID_SERVERS_CAMERA_CAMERASERVICE_H
21
22#include <ui/ICameraService.h>
23#include <ui/CameraHardwareInterface.h>
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080024#include <ui/Camera.h>
25
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070026class android::MemoryHeapBase;
27
28namespace android {
29
30// ----------------------------------------------------------------------------
31
32#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
33#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
34
35// When enabled, this feature allows you to send an event to the CameraService
36// so that you can cause all references to the heap object gWeakHeap, defined
37// below, to be printed. You will also need to set DEBUG_REFS=1 and
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080038// DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp. You just have to
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070039// set gWeakHeap to the appropriate heap you want to track.
40
41#define DEBUG_HEAP_LEAKS 0
42
43// ----------------------------------------------------------------------------
44
45class CameraService : public BnCameraService
46{
47 class Client;
48
49public:
50 static void instantiate();
51
52 // ICameraService interface
53 virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient);
54
55 virtual status_t dump(int fd, const Vector<String16>& args);
56
57 void removeClient(const sp<ICameraClient>& cameraClient);
58
59#if DEBUG_HEAP_LEAKS
60 virtual status_t onTransact(
61 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
62#endif
63
64private:
65
66// ----------------------------------------------------------------------------
67
68 class Client : public BnCamera {
69
70 public:
71 virtual void disconnect();
72
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080073 // connect new client with existing camera remote
74 virtual status_t connect(const sp<ICameraClient>& client);
75
The Android Open Source Project27629322009-01-09 17:51:23 -080076 // prevent other processes from using this ICamera interface
77 virtual status_t lock();
78
79 // allow other processes to use this ICamera interface
80 virtual status_t unlock();
81
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070082 // pass the buffered ISurface to the camera service
83 virtual status_t setPreviewDisplay(const sp<ISurface>& surface);
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080084
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -080085 // set the preview callback flag to affect how the received frames from
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080086 // preview are handled.
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -080087 virtual void setPreviewCallbackFlag(int callback_flag);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070088
89 // start preview mode, must call setPreviewDisplay first
90 virtual status_t startPreview();
91
92 // stop preview mode
93 virtual void stopPreview();
94
The Android Open Source Project27629322009-01-09 17:51:23 -080095 // get preview state
96 virtual bool previewEnabled();
97
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -080098 // start recording mode
99 virtual status_t startRecording();
100
101 // stop recording mode
102 virtual void stopRecording();
103
104 // get recording state
105 virtual bool recordingEnabled();
106
107 // release a recording frame
108 virtual void releaseRecordingFrame(const sp<IMemory>& mem);
109
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700110 // auto focus
111 virtual status_t autoFocus();
112
113 // take a picture - returns an IMemory (ref-counted mmap)
114 virtual status_t takePicture();
115
116 // set preview/capture parameters - key/value pairs
117 virtual status_t setParameters(const String8& params);
118
119 // get preview/capture parameters - key/value pairs
120 virtual String8 getParameters() const;
121
122 // our client...
123 const sp<ICameraClient>& getCameraClient() const { return mCameraClient; }
124
125 private:
126 friend class CameraService;
The Android Open Source Project27629322009-01-09 17:51:23 -0800127 Client(const sp<CameraService>& cameraService,
128 const sp<ICameraClient>& cameraClient,
129 pid_t clientPid);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700130 Client();
131 virtual ~Client();
132
The Android Open Source Project27629322009-01-09 17:51:23 -0800133 status_t checkPid();
134
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800135 static void recordingCallback(const sp<IMemory>& mem, void* user);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700136 static void previewCallback(const sp<IMemory>& mem, void* user);
137 static void shutterCallback(void *user);
138 static void yuvPictureCallback(const sp<IMemory>& mem, void* user);
139 static void jpegPictureCallback(const sp<IMemory>& mem, void* user);
140 static void autoFocusCallback(bool focused, void* user);
141 static sp<Client> getClientFromCookie(void* user);
142
143 void postShutter();
144 void postRaw(const sp<IMemory>& mem);
145 void postJpeg(const sp<IMemory>& mem);
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800146 void postPreviewFrame(const sp<IMemory>& mem);
147 void postRecordingFrame(const sp<IMemory>& frame);
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800148 void copyFrameAndPostCopiedFrame(sp<IMemoryHeap> heap, size_t offset, size_t size);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700149 void postError(status_t error);
150 void postAutoFocus(bool focused);
151
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800152 // camera operation mode
153 enum camera_mode {
154 CAMERA_PREVIEW_MODE = 0, // frame automatically released
155 CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame()
156 };
157 status_t startCameraMode(camera_mode mode);
158 status_t startPreviewMode();
159 status_t startRecordingMode();
160
161 // Ensures atomicity among the public methods
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700162 mutable Mutex mLock;
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800163
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700164 // mSurfaceLock synchronizes access to mSurface between
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800165 // setPreviewSurface() and postPreviewFrame(). Note that among
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800166 // the public methods, all accesses to mSurface are
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800167 // syncrhonized by mLock. However, postPreviewFrame() is called
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800168 // by the CameraHardwareInterface callback, and needs to
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700169 // access mSurface. It cannot hold mLock, however, because
170 // stopPreview() may be holding that lock while attempting
The Android Open Source Project27629322009-01-09 17:51:23 -0800171 // to stop preview, and stopPreview itself will block waiting
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800172 // for a callback from CameraHardwareInterface. If this
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700173 // happens, it will cause a deadlock.
174 mutable Mutex mSurfaceLock;
The Android Open Source Project0bb03402009-03-02 22:54:33 -0800175 // mBufferLock synchronizes buffer registration between takePicture()
176 // and yuvPictureCallback().
177 mutable Mutex mBufferLock;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700178 mutable Condition mReady;
179 sp<CameraService> mCameraService;
180 sp<ISurface> mSurface;
181 sp<MemoryHeapBase> mPreviewBuffer;
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800182 int mPreviewCallbackFlag;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700183
184 // these are immutable once the object is created,
185 // they don't need to be protected by a lock
186 sp<ICameraClient> mCameraClient;
187 sp<CameraHardwareInterface> mHardware;
The Android Open Source Project27629322009-01-09 17:51:23 -0800188 pid_t mClientPid;
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800189 bool mUseOverlay;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700190 };
191
192// ----------------------------------------------------------------------------
193
194 CameraService();
195 virtual ~CameraService();
196
197 mutable Mutex mLock;
198 wp<Client> mClient;
199
200#if DEBUG_HEAP_LEAKS
201 wp<IMemoryHeap> gWeakHeap;
202#endif
203};
204
205// ----------------------------------------------------------------------------
206
207}; // namespace android
208
209#endif