blob: 2e3597f113fee8c47a5763d1cb6aeaf140a6998a [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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
Jason Sams78b877e2009-03-24 20:21:36 -070030class MediaPlayer;
31
The Android Open Source Projectedbf3b62009-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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061 virtual status_t onTransact(
62 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
64private:
65
66// ----------------------------------------------------------------------------
67
68 class Client : public BnCamera {
69
70 public:
71 virtual void disconnect();
72
73 // connect new client with existing camera remote
74 virtual status_t connect(const sp<ICameraClient>& client);
75
76 // 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
82 // pass the buffered ISurface to the camera service
83 virtual status_t setPreviewDisplay(const sp<ISurface>& surface);
84
85 // set the preview callback flag to affect how the received frames from
86 // preview are handled.
87 virtual void setPreviewCallbackFlag(int callback_flag);
88
89 // start preview mode, must call setPreviewDisplay first
90 virtual status_t startPreview();
91
92 // stop preview mode
93 virtual void stopPreview();
94
95 // get preview state
96 virtual bool previewEnabled();
97
98 // 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
110 // auto focus
111 virtual status_t autoFocus();
112
Chih-Chung Chang00900eb2009-09-15 14:51:56 +0800113 // cancel auto focus
114 virtual status_t cancelAutoFocus();
115
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 // take a picture - returns an IMemory (ref-counted mmap)
117 virtual status_t takePicture();
118
119 // set preview/capture parameters - key/value pairs
120 virtual status_t setParameters(const String8& params);
121
122 // get preview/capture parameters - key/value pairs
123 virtual String8 getParameters() const;
124
Wu-cheng Lie6a550d2009-09-28 16:14:58 -0700125 // send command to camera driver
126 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 // our client...
129 const sp<ICameraClient>& getCameraClient() const { return mCameraClient; }
130
131 private:
132 friend class CameraService;
133 Client(const sp<CameraService>& cameraService,
134 const sp<ICameraClient>& cameraClient,
135 pid_t clientPid);
136 Client();
137 virtual ~Client();
138
139 status_t checkPid();
140
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500141 static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user);
142 static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user);
143 static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType,
144 const sp<IMemory>& dataPtr, void* user);
145
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 static sp<Client> getClientFromCookie(void* user);
147
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500148 void handlePreviewData(const sp<IMemory>&);
149 void handleShutter();
150 void handlePostview(const sp<IMemory>&);
151 void handleRawPicture(const sp<IMemory>&);
152 void handleCompressedPicture(const sp<IMemory>&);
153
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 void copyFrameAndPostCopiedFrame(sp<IMemoryHeap> heap, size_t offset, size_t size);
The Android Open Source Projectedbf3b62009-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 Li988fb622009-06-23 23:37:36 +0800164 status_t setOverlay();
165 status_t registerPreviewBuffers();
The Android Open Source Projectedbf3b62009-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;
184 sp<MemoryHeapBase> mPreviewBuffer;
185 int mPreviewCallbackFlag;
186
Jason Sams78b877e2009-03-24 20:21:36 -0700187 sp<MediaPlayer> mMediaPlayerClick;
188 sp<MediaPlayer> mMediaPlayerBeep;
189
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190 // these are immutable once the object is created,
191 // they don't need to be protected by a lock
192 sp<ICameraClient> mCameraClient;
193 sp<CameraHardwareInterface> mHardware;
194 pid_t mClientPid;
195 bool mUseOverlay;
Benny Wong71f77152009-07-15 18:44:27 -0500196
197 sp<OverlayRef> mOverlayRef;
198 int mOverlayW;
199 int mOverlayH;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200 };
201
202// ----------------------------------------------------------------------------
203
204 CameraService();
205 virtual ~CameraService();
206
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800207 // We use a count for number of clients (shoule only be 0 or 1).
208 volatile int32_t mUsers;
209 virtual void incUsers();
210 virtual void decUsers();
211
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800212 mutable Mutex mServiceLock;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 wp<Client> mClient;
214
215#if DEBUG_HEAP_LEAKS
216 wp<IMemoryHeap> gWeakHeap;
217#endif
218};
219
220// ----------------------------------------------------------------------------
221
222}; // namespace android
223
224#endif