blob: 6752f265dabbfaab658d994a1e7180221b7daeb8 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
4** Copyright (C) 2008 HTC Inc.
5**
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>
24#include <ui/Camera.h>
25
26class android::MemoryHeapBase;
27
28namespace android {
29
The Android Open Source Projectc2ad2412009-03-19 23:08:54 -070030class MediaPlayer;
31
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032// ----------------------------------------------------------------------------
33
34#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
35#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
36
37// When enabled, this feature allows you to send an event to the CameraService
38// so that you can cause all references to the heap object gWeakHeap, defined
39// below, to be printed. You will also need to set DEBUG_REFS=1 and
40// DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp. You just have to
41// set gWeakHeap to the appropriate heap you want to track.
42
43#define DEBUG_HEAP_LEAKS 0
44
45// ----------------------------------------------------------------------------
46
47class CameraService : public BnCameraService
48{
49 class Client;
50
51public:
52 static void instantiate();
53
54 // ICameraService interface
55 virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient);
56
57 virtual status_t dump(int fd, const Vector<String16>& args);
58
59 void removeClient(const sp<ICameraClient>& cameraClient);
60
61#if DEBUG_HEAP_LEAKS
62 virtual status_t onTransact(
63 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
64#endif
65
66private:
67
68// ----------------------------------------------------------------------------
69
70 class Client : public BnCamera {
71
72 public:
73 virtual void disconnect();
74
75 // connect new client with existing camera remote
76 virtual status_t connect(const sp<ICameraClient>& client);
77
78 // prevent other processes from using this ICamera interface
79 virtual status_t lock();
80
81 // allow other processes to use this ICamera interface
82 virtual status_t unlock();
83
84 // pass the buffered ISurface to the camera service
85 virtual status_t setPreviewDisplay(const sp<ISurface>& surface);
86
87 // set the preview callback flag to affect how the received frames from
88 // preview are handled.
89 virtual void setPreviewCallbackFlag(int callback_flag);
90
91 // start preview mode, must call setPreviewDisplay first
92 virtual status_t startPreview();
93
94 // stop preview mode
95 virtual void stopPreview();
96
97 // get preview state
98 virtual bool previewEnabled();
99
100 // start recording mode
101 virtual status_t startRecording();
102
103 // stop recording mode
104 virtual void stopRecording();
105
106 // get recording state
107 virtual bool recordingEnabled();
108
109 // release a recording frame
110 virtual void releaseRecordingFrame(const sp<IMemory>& mem);
111
112 // auto focus
113 virtual status_t autoFocus();
114
115 // take a picture - returns an IMemory (ref-counted mmap)
116 virtual status_t takePicture();
117
118 // set preview/capture parameters - key/value pairs
119 virtual status_t setParameters(const String8& params);
120
121 // get preview/capture parameters - key/value pairs
122 virtual String8 getParameters() const;
123
124 // our client...
125 const sp<ICameraClient>& getCameraClient() const { return mCameraClient; }
126
127 private:
128 friend class CameraService;
129 Client(const sp<CameraService>& cameraService,
130 const sp<ICameraClient>& cameraClient,
131 pid_t clientPid);
132 Client();
133 virtual ~Client();
134
135 status_t checkPid();
136
137 static void recordingCallback(const sp<IMemory>& mem, void* user);
138 static void previewCallback(const sp<IMemory>& mem, void* user);
139 static void shutterCallback(void *user);
140 static void yuvPictureCallback(const sp<IMemory>& mem, void* user);
141 static void jpegPictureCallback(const sp<IMemory>& mem, void* user);
142 static void autoFocusCallback(bool focused, void* user);
143 static sp<Client> getClientFromCookie(void* user);
144
145 void postShutter();
146 void postRaw(const sp<IMemory>& mem);
147 void postJpeg(const sp<IMemory>& mem);
148 void postPreviewFrame(const sp<IMemory>& mem);
149 void postRecordingFrame(const sp<IMemory>& frame);
150 void copyFrameAndPostCopiedFrame(sp<IMemoryHeap> heap, size_t offset, size_t size);
151 void postError(status_t error);
152 void postAutoFocus(bool focused);
153
154 // camera operation mode
155 enum camera_mode {
156 CAMERA_PREVIEW_MODE = 0, // frame automatically released
157 CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame()
158 };
159 status_t startCameraMode(camera_mode mode);
160 status_t startPreviewMode();
161 status_t startRecordingMode();
162
163 // Ensures atomicity among the public methods
164 mutable Mutex mLock;
165
166 // mSurfaceLock synchronizes access to mSurface between
167 // setPreviewSurface() and postPreviewFrame(). Note that among
168 // the public methods, all accesses to mSurface are
169 // syncrhonized by mLock. However, postPreviewFrame() is called
170 // by the CameraHardwareInterface callback, and needs to
171 // access mSurface. It cannot hold mLock, however, because
172 // stopPreview() may be holding that lock while attempting
173 // to stop preview, and stopPreview itself will block waiting
174 // for a callback from CameraHardwareInterface. If this
175 // happens, it will cause a deadlock.
176 mutable Mutex mSurfaceLock;
177 mutable Condition mReady;
178 sp<CameraService> mCameraService;
179 sp<ISurface> mSurface;
180 sp<MemoryHeapBase> mPreviewBuffer;
181 int mPreviewCallbackFlag;
182
The Android Open Source Projectc2ad2412009-03-19 23:08:54 -0700183 sp<MediaPlayer> mMediaPlayerClick;
184 sp<MediaPlayer> mMediaPlayerBeep;
185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 // these are immutable once the object is created,
187 // they don't need to be protected by a lock
188 sp<ICameraClient> mCameraClient;
189 sp<CameraHardwareInterface> mHardware;
190 pid_t mClientPid;
191 bool mUseOverlay;
192 };
193
194// ----------------------------------------------------------------------------
195
196 CameraService();
197 virtual ~CameraService();
198
199 mutable Mutex mLock;
200 wp<Client> mClient;
201
202#if DEBUG_HEAP_LEAKS
203 wp<IMemoryHeap> gWeakHeap;
204#endif
205};
206
207// ----------------------------------------------------------------------------
208
209}; // namespace android
210
211#endif