blob: 77ccf41000d0f5ecb94484a2beaa4e20c2474cda [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
Mathias Agopian65ab4712010-07-14 17:59:35 -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
Mathias Agopian5462fc92010-07-14 18:41:18 -070021#include <binder/BinderService.h>
22
Mathias Agopian65ab4712010-07-14 17:59:35 -070023#include <camera/ICameraService.h>
24#include <camera/CameraHardwareInterface.h>
25
26/* This needs to be increased if we can have more cameras */
27#define MAX_CAMERAS 2
28
29namespace android {
30
31class MemoryHeapBase;
32class MediaPlayer;
33
Mathias Agopian5462fc92010-07-14 18:41:18 -070034class CameraService :
35 public BinderService<CameraService>,
36 public BnCameraService
Mathias Agopian65ab4712010-07-14 17:59:35 -070037{
38 class Client;
Mathias Agopian5462fc92010-07-14 18:41:18 -070039 friend class BinderService<CameraService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070040public:
Mathias Agopian5462fc92010-07-14 18:41:18 -070041 static char const* getServiceName() { return "media.camera"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070042
43 CameraService();
44 virtual ~CameraService();
45
46 virtual int32_t getNumberOfCameras();
47 virtual status_t getCameraInfo(int cameraId,
48 struct CameraInfo* cameraInfo);
49 virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId);
50 virtual void removeClient(const sp<ICameraClient>& cameraClient);
51 virtual sp<Client> getClientById(int cameraId);
52
53 virtual status_t dump(int fd, const Vector<String16>& args);
54 virtual status_t onTransact(uint32_t code, const Parcel& data,
55 Parcel* reply, uint32_t flags);
56
57 enum sound_kind {
58 SOUND_SHUTTER = 0,
59 SOUND_RECORDING = 1,
60 NUM_SOUNDS
61 };
62
63 void loadSound();
64 void playSound(sound_kind kind);
65 void releaseSound();
66
67private:
68 Mutex mServiceLock;
69 wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock
70 int mNumberOfCameras;
71
72 // atomics to record whether the hardware is allocated to some client.
73 volatile int32_t mBusy[MAX_CAMERAS];
74 void setCameraBusy(int cameraId);
75 void setCameraFree(int cameraId);
76
77 // sounds
78 Mutex mSoundLock;
79 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
80 int mSoundRef; // reference count (release all MediaPlayer when 0)
81
82 class Client : public BnCamera
83 {
84 public:
85 // ICamera interface (see ICamera for details)
86 virtual void disconnect();
87 virtual status_t connect(const sp<ICameraClient>& client);
88 virtual status_t lock();
89 virtual status_t unlock();
90 virtual status_t setPreviewDisplay(const sp<ISurface>& surface);
91 virtual void setPreviewCallbackFlag(int flag);
92 virtual status_t startPreview();
93 virtual void stopPreview();
94 virtual bool previewEnabled();
95 virtual status_t startRecording();
96 virtual void stopRecording();
97 virtual bool recordingEnabled();
98 virtual void releaseRecordingFrame(const sp<IMemory>& mem);
99 virtual status_t autoFocus();
100 virtual status_t cancelAutoFocus();
101 virtual status_t takePicture();
102 virtual status_t setParameters(const String8& params);
103 virtual String8 getParameters() const;
104 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
105 private:
106 friend class CameraService;
107 Client(const sp<CameraService>& cameraService,
108 const sp<ICameraClient>& cameraClient,
109 int cameraId,
110 int clientPid);
111 ~Client();
112
113 // return our camera client
114 const sp<ICameraClient>& getCameraClient() { return mCameraClient; }
115
116 // check whether the calling process matches mClientPid.
117 status_t checkPid() const;
118 status_t checkPidAndHardware() const; // also check mHardware != 0
119
120 // these are internal functions used to set up preview buffers
121 status_t registerPreviewBuffers();
122 status_t setOverlay();
123
124 // camera operation mode
125 enum camera_mode {
126 CAMERA_PREVIEW_MODE = 0, // frame automatically released
127 CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame()
128 };
129 // these are internal functions used for preview/recording
130 status_t startCameraMode(camera_mode mode);
131 status_t startPreviewMode();
132 status_t startRecordingMode();
133
134 // these are static callback functions
135 static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user);
136 static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user);
137 static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user);
138 // convert client from cookie
139 static sp<Client> getClientFromCookie(void* user);
140 // handlers for messages
141 void handleShutter(image_rect_type *size);
142 void handlePreviewData(const sp<IMemory>& mem);
143 void handlePostview(const sp<IMemory>& mem);
144 void handleRawPicture(const sp<IMemory>& mem);
145 void handleCompressedPicture(const sp<IMemory>& mem);
146 void handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2);
147 void handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr);
148 void handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
149
150 void copyFrameAndPostCopiedFrame(
151 const sp<ICameraClient>& client,
152 const sp<IMemoryHeap>& heap,
153 size_t offset, size_t size);
154
155 // these are initialized in the constructor.
156 sp<CameraService> mCameraService; // immutable after constructor
157 sp<ICameraClient> mCameraClient;
158 int mCameraId; // immutable after constructor
159 pid_t mClientPid;
160 sp<CameraHardwareInterface> mHardware; // cleared after disconnect()
161 bool mUseOverlay; // immutable after constructor
162 sp<OverlayRef> mOverlayRef;
163 int mOverlayW;
164 int mOverlayH;
165 int mPreviewCallbackFlag;
166 int mOrientation;
167
168 // Ensures atomicity among the public methods
169 mutable Mutex mLock;
170 sp<ISurface> mSurface;
171
172 // If the user want us to return a copy of the preview frame (instead
173 // of the original one), we allocate mPreviewBuffer and reuse it if possible.
174 sp<MemoryHeapBase> mPreviewBuffer;
175
176 // We need to avoid the deadlock when the incoming command thread and
177 // the CameraHardwareInterface callback thread both want to grab mLock.
178 // An extra flag is used to tell the callback thread that it should stop
179 // trying to deliver the callback messages if the client is not
180 // interested in it anymore. For example, if the client is calling
181 // stopPreview(), the preview frame messages do not need to be delivered
182 // anymore.
183
184 // This function takes the same parameter as the enableMsgType() and
185 // disableMsgType() functions in CameraHardwareInterface.
186 void enableMsgType(int32_t msgType);
187 void disableMsgType(int32_t msgType);
188 volatile int32_t mMsgEnabled;
189
190 // This function keeps trying to grab mLock, or give up if the message
191 // is found to be disabled. It returns true if mLock is grabbed.
192 bool lockIfMessageWanted(int32_t msgType);
193 };
194};
195
196} // namespace android
197
198#endif