blob: 3e3e54f0a950de3fe90b31cc307e15c4fd9fc6a5 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026namespace android {
27
Dave Sparksdd158c92009-10-15 10:02:22 -070028class MemoryHeapBase;
Jason Samsb18b6912009-03-24 20:21:36 -070029class MediaPlayer;
30
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031// ----------------------------------------------------------------------------
32
33#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
34#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
35
36// When enabled, this feature allows you to send an event to the CameraService
37// so that you can cause all references to the heap object gWeakHeap, defined
38// below, to be printed. You will also need to set DEBUG_REFS=1 and
39// DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp. You just have to
40// set gWeakHeap to the appropriate heap you want to track.
41
42#define DEBUG_HEAP_LEAKS 0
43
44// ----------------------------------------------------------------------------
45
46class CameraService : public BnCameraService
47{
48 class Client;
49
50public:
51 static void instantiate();
52
53 // ICameraService interface
54 virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient);
55
56 virtual status_t dump(int fd, const Vector<String16>& args);
57
58 void removeClient(const sp<ICameraClient>& cameraClient);
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 virtual status_t onTransact(
61 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
63private:
64
65// ----------------------------------------------------------------------------
66
67 class Client : public BnCamera {
68
69 public:
70 virtual void disconnect();
71
72 // connect new client with existing camera remote
73 virtual status_t connect(const sp<ICameraClient>& client);
74
75 // prevent other processes from using this ICamera interface
76 virtual status_t lock();
77
78 // allow other processes to use this ICamera interface
79 virtual status_t unlock();
80
81 // pass the buffered ISurface to the camera service
82 virtual status_t setPreviewDisplay(const sp<ISurface>& surface);
83
84 // set the preview callback flag to affect how the received frames from
85 // preview are handled.
86 virtual void setPreviewCallbackFlag(int callback_flag);
87
88 // start preview mode, must call setPreviewDisplay first
89 virtual status_t startPreview();
90
91 // stop preview mode
92 virtual void stopPreview();
93
94 // get preview state
95 virtual bool previewEnabled();
96
97 // start recording mode
98 virtual status_t startRecording();
99
100 // stop recording mode
101 virtual void stopRecording();
102
103 // get recording state
104 virtual bool recordingEnabled();
105
106 // release a recording frame
107 virtual void releaseRecordingFrame(const sp<IMemory>& mem);
108
109 // auto focus
110 virtual status_t autoFocus();
111
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800112 // cancel auto focus
113 virtual status_t cancelAutoFocus();
114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 // 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
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700124 // send command to camera driver
125 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 // our client...
128 const sp<ICameraClient>& getCameraClient() const { return mCameraClient; }
129
130 private:
131 friend class CameraService;
132 Client(const sp<CameraService>& cameraService,
133 const sp<ICameraClient>& cameraClient,
134 pid_t clientPid);
135 Client();
136 virtual ~Client();
137
138 status_t checkPid();
139
Benny Wongda83f462009-08-12 12:01:27 -0500140 static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user);
141 static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user);
142 static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType,
143 const sp<IMemory>& dataPtr, void* user);
144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 static sp<Client> getClientFromCookie(void* user);
146
Benny Wongda83f462009-08-12 12:01:27 -0500147 void handlePreviewData(const sp<IMemory>&);
Wu-cheng Li4cb04c42009-10-23 17:39:46 +0800148 void handleShutter(image_rect_type *image);
Benny Wongda83f462009-08-12 12:01:27 -0500149 void handlePostview(const sp<IMemory>&);
150 void handleRawPicture(const sp<IMemory>&);
151 void handleCompressedPicture(const sp<IMemory>&);
152
Dave Sparksdd158c92009-10-15 10:02:22 -0700153 void copyFrameAndPostCopiedFrame(const sp<ICameraClient>& client,
154 const sp<IMemoryHeap>& heap, size_t offset, size_t size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155
156 // camera operation mode
157 enum camera_mode {
158 CAMERA_PREVIEW_MODE = 0, // frame automatically released
159 CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame()
160 };
161 status_t startCameraMode(camera_mode mode);
162 status_t startPreviewMode();
163 status_t startRecordingMode();
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800164 status_t setOverlay();
165 status_t registerPreviewBuffers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166
167 // Ensures atomicity among the public methods
168 mutable Mutex mLock;
169
170 // mSurfaceLock synchronizes access to mSurface between
171 // setPreviewSurface() and postPreviewFrame(). Note that among
172 // the public methods, all accesses to mSurface are
173 // syncrhonized by mLock. However, postPreviewFrame() is called
174 // by the CameraHardwareInterface callback, and needs to
175 // access mSurface. It cannot hold mLock, however, because
176 // stopPreview() may be holding that lock while attempting
177 // to stop preview, and stopPreview itself will block waiting
178 // for a callback from CameraHardwareInterface. If this
179 // happens, it will cause a deadlock.
180 mutable Mutex mSurfaceLock;
181 mutable Condition mReady;
182 sp<CameraService> mCameraService;
183 sp<ISurface> mSurface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 int mPreviewCallbackFlag;
185
Jason Samsb18b6912009-03-24 20:21:36 -0700186 sp<MediaPlayer> mMediaPlayerClick;
187 sp<MediaPlayer> mMediaPlayerBeep;
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 // these are immutable once the object is created,
190 // they don't need to be protected by a lock
191 sp<ICameraClient> mCameraClient;
192 sp<CameraHardwareInterface> mHardware;
193 pid_t mClientPid;
194 bool mUseOverlay;
Benny Wong6d2090e2009-07-15 18:44:27 -0500195
196 sp<OverlayRef> mOverlayRef;
197 int mOverlayW;
198 int mOverlayH;
Dave Sparks05fd0df2009-11-10 17:08:08 -0800199
200 mutable Mutex mPreviewLock;
201 sp<MemoryHeapBase> mPreviewBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 };
203
204// ----------------------------------------------------------------------------
205
206 CameraService();
207 virtual ~CameraService();
208
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800209 // We use a count for number of clients (shoule only be 0 or 1).
210 volatile int32_t mUsers;
211 virtual void incUsers();
212 virtual void decUsers();
213
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800214 mutable Mutex mServiceLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 wp<Client> mClient;
216
217#if DEBUG_HEAP_LEAKS
218 wp<IMemoryHeap> gWeakHeap;
219#endif
220};
221
222// ----------------------------------------------------------------------------
223
224}; // namespace android
225
226#endif