blob: 7844047ac079496ed5da2cb25075116e38fea4c1 [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>
Mathias Agopian65ab4712010-07-14 17:59:35 -070022#include <camera/ICameraService.h>
Iliyan Malchev8951a972011-04-14 16:55:59 -070023#include <hardware/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024
25/* This needs to be increased if we can have more cameras */
Dejan Ivanovskia3b58da2011-10-11 12:03:40 +030026#ifdef OMAP_ENHANCEMENT
27#define MAX_CAMERAS 3
28#else
Mathias Agopian65ab4712010-07-14 17:59:35 -070029#define MAX_CAMERAS 2
Dejan Ivanovskia3b58da2011-10-11 12:03:40 +030030#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -070031
32namespace android {
33
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070034extern volatile int32_t gLogLevel;
35
Mathias Agopian65ab4712010-07-14 17:59:35 -070036class MemoryHeapBase;
37class MediaPlayer;
38
Mathias Agopian5462fc92010-07-14 18:41:18 -070039class CameraService :
40 public BinderService<CameraService>,
Igor Murashkinecf17e82012-10-02 16:05:11 -070041 public BnCameraService,
42 public IBinder::DeathRecipient
Mathias Agopian65ab4712010-07-14 17:59:35 -070043{
Mathias Agopian5462fc92010-07-14 18:41:18 -070044 friend class BinderService<CameraService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070045public:
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070046 class Client;
Mathias Agopian5462fc92010-07-14 18:41:18 -070047 static char const* getServiceName() { return "media.camera"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
49 CameraService();
50 virtual ~CameraService();
51
52 virtual int32_t getNumberOfCameras();
53 virtual status_t getCameraInfo(int cameraId,
54 struct CameraInfo* cameraInfo);
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +080055 virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -070056 virtual void removeClient(const sp<ICameraClient>& cameraClient);
Keun young Parkd8973a72012-03-28 14:13:09 -070057 // returns plain pointer of client. Note that mClientLock should be acquired to
58 // prevent the client from destruction. The result can be NULL.
59 virtual Client* getClientByIdUnsafe(int cameraId);
60 virtual Mutex* getClientLockById(int cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -070061
Igor Murashkin294d0ec2012-10-05 10:44:57 -070062 virtual sp<Client> getClientByRemote(const wp<IBinder>& cameraClient);
Igor Murashkinecf17e82012-10-02 16:05:11 -070063
Mathias Agopian65ab4712010-07-14 17:59:35 -070064 virtual status_t dump(int fd, const Vector<String16>& args);
65 virtual status_t onTransact(uint32_t code, const Parcel& data,
66 Parcel* reply, uint32_t flags);
Iliyan Malchev8951a972011-04-14 16:55:59 -070067 virtual void onFirstRef();
Mathias Agopian65ab4712010-07-14 17:59:35 -070068
69 enum sound_kind {
70 SOUND_SHUTTER = 0,
71 SOUND_RECORDING = 1,
72 NUM_SOUNDS
73 };
74
75 void loadSound();
76 void playSound(sound_kind kind);
77 void releaseSound();
78
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070079 class Client : public BnCamera
80 {
81 public:
82 // ICamera interface (see ICamera for details)
83 virtual void disconnect();
84 virtual status_t connect(const sp<ICameraClient>& client) = 0;
85 virtual status_t lock() = 0;
86 virtual status_t unlock() = 0;
87 virtual status_t setPreviewDisplay(const sp<Surface>& surface) = 0;
88 virtual status_t setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture) = 0;
89 virtual void setPreviewCallbackFlag(int flag) = 0;
90 virtual status_t startPreview() = 0;
91 virtual void stopPreview() = 0;
92 virtual bool previewEnabled() = 0;
93 virtual status_t storeMetaDataInBuffers(bool enabled) = 0;
94 virtual status_t startRecording() = 0;
95 virtual void stopRecording() = 0;
96 virtual bool recordingEnabled() = 0;
97 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
98 virtual status_t autoFocus() = 0;
99 virtual status_t cancelAutoFocus() = 0;
100 virtual status_t takePicture(int msgType) = 0;
101 virtual status_t setParameters(const String8& params) = 0;
102 virtual String8 getParameters() const = 0;
103 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
104
105 // Interface used by CameraService
106 Client(const sp<CameraService>& cameraService,
107 const sp<ICameraClient>& cameraClient,
108 int cameraId,
109 int cameraFacing,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700110 int clientPid,
111 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700112 ~Client();
113
114 // return our camera client
115 const sp<ICameraClient>& getCameraClient() {
116 return mCameraClient;
117 }
118
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700119 virtual status_t initialize(camera_module_t *module) = 0;
120
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700121 virtual status_t dump(int fd, const Vector<String16>& args) = 0;
122
123 protected:
124 static Mutex* getClientLockFromCookie(void* user);
125 // convert client from cookie. Client lock should be acquired before getting Client.
126 static Client* getClientFromCookie(void* user);
127
128 // the instance is in the middle of destruction. When this is set,
129 // the instance should not be accessed from callback.
130 // CameraService's mClientLock should be acquired to access this.
131 bool mDestructionStarted;
132
133 // these are initialized in the constructor.
134 sp<CameraService> mCameraService; // immutable after constructor
135 sp<ICameraClient> mCameraClient;
136 int mCameraId; // immutable after constructor
137 int mCameraFacing; // immutable after constructor
138 pid_t mClientPid;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700139 pid_t mServicePid; // immutable after constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700140
141 };
142
Mathias Agopian65ab4712010-07-14 17:59:35 -0700143private:
144 Mutex mServiceLock;
145 wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock
Keun young Parkd8973a72012-03-28 14:13:09 -0700146 Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700147 int mNumberOfCameras;
148
Igor Murashkinecf17e82012-10-02 16:05:11 -0700149 // needs to be called with mServiceLock held
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700150 sp<Client> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700151
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152 // atomics to record whether the hardware is allocated to some client.
153 volatile int32_t mBusy[MAX_CAMERAS];
154 void setCameraBusy(int cameraId);
155 void setCameraFree(int cameraId);
156
157 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800158 MediaPlayer* newMediaPlayer(const char *file);
159
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160 Mutex mSoundLock;
161 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
162 int mSoundRef; // reference count (release all MediaPlayer when 0)
163
Iliyan Malchev8951a972011-04-14 16:55:59 -0700164 camera_module_t *mModule;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700165
166 // IBinder::DeathRecipient implementation
167 virtual void binderDied(const wp<IBinder> &who);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168};
169
170} // namespace android
171
172#endif