blob: 75e96c6dbc035ba9ddbe30ab5cf381436a635d73 [file] [log] [blame]
Mathias Agopianccaa4142010-07-22 15:27:48 -07001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
Mathias Agopianccaa4142010-07-22 15:27:48 -07004**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#ifndef ANDROID_SERVERS_CAMERA_CAMERASERVICE_H
19#define ANDROID_SERVERS_CAMERA_CAMERASERVICE_H
20
21#include <camera/ICameraService.h>
22#include <camera/CameraHardwareInterface.h>
23#include <camera/Camera.h>
24
25namespace android {
26
27class MemoryHeapBase;
28class MediaPlayer;
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
38// DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp. You just have to
39// 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 virtual status_t onTransact(
60 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags);
61
62private:
63
64// ----------------------------------------------------------------------------
65
66 class Client : public BnCamera {
67
68 public:
69 virtual void disconnect();
70
71 // connect new client with existing camera remote
72 virtual status_t connect(const sp<ICameraClient>& client);
73
74 // prevent other processes from using this ICamera interface
75 virtual status_t lock();
76
77 // allow other processes to use this ICamera interface
78 virtual status_t unlock();
79
80 // pass the buffered ISurface to the camera service
81 virtual status_t setPreviewDisplay(const sp<ISurface>& surface);
82
83 // set the preview callback flag to affect how the received frames from
84 // preview are handled.
85 virtual void setPreviewCallbackFlag(int callback_flag);
86
87 // start preview mode, must call setPreviewDisplay first
88 virtual status_t startPreview();
89
90 // stop preview mode
91 virtual void stopPreview();
92
93 // get preview state
94 virtual bool previewEnabled();
95
96 // start recording mode
97 virtual status_t startRecording();
98
99 // stop recording mode
100 virtual void stopRecording();
101
102 // get recording state
103 virtual bool recordingEnabled();
104
105 // release a recording frame
106 virtual void releaseRecordingFrame(const sp<IMemory>& mem);
107
108 // auto focus
109 virtual status_t autoFocus();
110
111 // cancel auto focus
112 virtual status_t cancelAutoFocus();
113
114 // take a picture - returns an IMemory (ref-counted mmap)
115 virtual status_t takePicture();
116
117 // set preview/capture parameters - key/value pairs
118 virtual status_t setParameters(const String8& params);
119
120 // get preview/capture parameters - key/value pairs
121 virtual String8 getParameters() const;
122
123 // send command to camera driver
124 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
125
126 // our client...
127 const sp<ICameraClient>& getCameraClient() const { return mCameraClient; }
128
129 private:
130 friend class CameraService;
131 Client(const sp<CameraService>& cameraService,
132 const sp<ICameraClient>& cameraClient,
133 pid_t clientPid);
134 Client();
135 virtual ~Client();
136
137 status_t checkPid();
138
139 static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user);
140 static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user);
141 static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType,
142 const sp<IMemory>& dataPtr, void* user);
143
144 static sp<Client> getClientFromCookie(void* user);
145
146 void handlePreviewData(const sp<IMemory>&);
147 void handleShutter(image_rect_type *image);
148 void handlePostview(const sp<IMemory>&);
149 void handleRawPicture(const sp<IMemory>&);
150 void handleCompressedPicture(const sp<IMemory>&);
151
152 void copyFrameAndPostCopiedFrame(const sp<ICameraClient>& client,
153 const sp<IMemoryHeap>& heap, size_t offset, size_t size);
154
155 // camera operation mode
156 enum camera_mode {
157 CAMERA_PREVIEW_MODE = 0, // frame automatically released
158 CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame()
159 };
160 status_t startCameraMode(camera_mode mode);
161 status_t startPreviewMode();
162 status_t startRecordingMode();
163 status_t setOverlay();
164 status_t registerPreviewBuffers();
165
166 // Ensures atomicity among the public methods
167 mutable Mutex mLock;
168
169 // mSurfaceLock synchronizes access to mSurface between
170 // setPreviewSurface() and postPreviewFrame(). Note that among
171 // the public methods, all accesses to mSurface are
172 // syncrhonized by mLock. However, postPreviewFrame() is called
173 // by the CameraHardwareInterface callback, and needs to
174 // access mSurface. It cannot hold mLock, however, because
175 // stopPreview() may be holding that lock while attempting
176 // to stop preview, and stopPreview itself will block waiting
177 // for a callback from CameraHardwareInterface. If this
178 // happens, it will cause a deadlock.
179 mutable Mutex mSurfaceLock;
180 mutable Condition mReady;
181 sp<CameraService> mCameraService;
182 sp<ISurface> mSurface;
183 int mPreviewCallbackFlag;
184 int mOrientation;
185
186 sp<MediaPlayer> mMediaPlayerClick;
187 sp<MediaPlayer> mMediaPlayerBeep;
188
189 // 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;
195
196 sp<OverlayRef> mOverlayRef;
197 int mOverlayW;
198 int mOverlayH;
199
200 mutable Mutex mPreviewLock;
201 sp<MemoryHeapBase> mPreviewBuffer;
202 };
203
204// ----------------------------------------------------------------------------
205
206 CameraService();
207 virtual ~CameraService();
208
209 // 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
214 mutable Mutex mServiceLock;
215 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