blob: bf9a6bc2cdcf9ebeff39ca1002368d51fcdf5ffa [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 */
26#define MAX_CAMERAS 2
27
28namespace android {
29
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070030extern volatile int32_t gLogLevel;
31
Mathias Agopian65ab4712010-07-14 17:59:35 -070032class MemoryHeapBase;
33class MediaPlayer;
Iliyan Malchev8951a972011-04-14 16:55:59 -070034class CameraHardwareInterface;
Mathias Agopian65ab4712010-07-14 17:59:35 -070035
Mathias Agopian5462fc92010-07-14 18:41:18 -070036class CameraService :
37 public BinderService<CameraService>,
38 public BnCameraService
Mathias Agopian65ab4712010-07-14 17:59:35 -070039{
Mathias Agopian5462fc92010-07-14 18:41:18 -070040 friend class BinderService<CameraService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070041public:
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070042 class Client;
Mathias Agopian5462fc92010-07-14 18:41:18 -070043 static char const* getServiceName() { return "media.camera"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070044
45 CameraService();
46 virtual ~CameraService();
47
48 virtual int32_t getNumberOfCameras();
49 virtual status_t getCameraInfo(int cameraId,
50 struct CameraInfo* cameraInfo);
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +080051 virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -070052 virtual void removeClient(const sp<ICameraClient>& cameraClient);
Keun young Parkd8973a72012-03-28 14:13:09 -070053 // returns plain pointer of client. Note that mClientLock should be acquired to
54 // prevent the client from destruction. The result can be NULL.
55 virtual Client* getClientByIdUnsafe(int cameraId);
56 virtual Mutex* getClientLockById(int cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -070057
58 virtual status_t dump(int fd, const Vector<String16>& args);
59 virtual status_t onTransact(uint32_t code, const Parcel& data,
60 Parcel* reply, uint32_t flags);
Iliyan Malchev8951a972011-04-14 16:55:59 -070061 virtual void onFirstRef();
Mathias Agopian65ab4712010-07-14 17:59:35 -070062
63 enum sound_kind {
64 SOUND_SHUTTER = 0,
65 SOUND_RECORDING = 1,
66 NUM_SOUNDS
67 };
68
69 void loadSound();
70 void playSound(sound_kind kind);
71 void releaseSound();
72
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070073 class Client : public BnCamera
74 {
75 public:
76 // ICamera interface (see ICamera for details)
77 virtual void disconnect();
78 virtual status_t connect(const sp<ICameraClient>& client) = 0;
79 virtual status_t lock() = 0;
80 virtual status_t unlock() = 0;
81 virtual status_t setPreviewDisplay(const sp<Surface>& surface) = 0;
82 virtual status_t setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture) = 0;
83 virtual void setPreviewCallbackFlag(int flag) = 0;
84 virtual status_t startPreview() = 0;
85 virtual void stopPreview() = 0;
86 virtual bool previewEnabled() = 0;
87 virtual status_t storeMetaDataInBuffers(bool enabled) = 0;
88 virtual status_t startRecording() = 0;
89 virtual void stopRecording() = 0;
90 virtual bool recordingEnabled() = 0;
91 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
92 virtual status_t autoFocus() = 0;
93 virtual status_t cancelAutoFocus() = 0;
94 virtual status_t takePicture(int msgType) = 0;
95 virtual status_t setParameters(const String8& params) = 0;
96 virtual String8 getParameters() const = 0;
97 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
98
99 // Interface used by CameraService
100 Client(const sp<CameraService>& cameraService,
101 const sp<ICameraClient>& cameraClient,
102 int cameraId,
103 int cameraFacing,
104 int clientPid);
105 ~Client();
106
107 // return our camera client
108 const sp<ICameraClient>& getCameraClient() {
109 return mCameraClient;
110 }
111
112 virtual status_t dump(int fd, const Vector<String16>& args) = 0;
113
114 protected:
115 static Mutex* getClientLockFromCookie(void* user);
116 // convert client from cookie. Client lock should be acquired before getting Client.
117 static Client* getClientFromCookie(void* user);
118
119 // the instance is in the middle of destruction. When this is set,
120 // the instance should not be accessed from callback.
121 // CameraService's mClientLock should be acquired to access this.
122 bool mDestructionStarted;
123
124 // these are initialized in the constructor.
125 sp<CameraService> mCameraService; // immutable after constructor
126 sp<ICameraClient> mCameraClient;
127 int mCameraId; // immutable after constructor
128 int mCameraFacing; // immutable after constructor
129 pid_t mClientPid;
130
131 };
132
Mathias Agopian65ab4712010-07-14 17:59:35 -0700133private:
134 Mutex mServiceLock;
135 wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock
Keun young Parkd8973a72012-03-28 14:13:09 -0700136 Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700137 int mNumberOfCameras;
138
139 // atomics to record whether the hardware is allocated to some client.
140 volatile int32_t mBusy[MAX_CAMERAS];
141 void setCameraBusy(int cameraId);
142 void setCameraFree(int cameraId);
143
144 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800145 MediaPlayer* newMediaPlayer(const char *file);
146
Mathias Agopian65ab4712010-07-14 17:59:35 -0700147 Mutex mSoundLock;
148 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
149 int mSoundRef; // reference count (release all MediaPlayer when 0)
150
Iliyan Malchev8951a972011-04-14 16:55:59 -0700151 camera_module_t *mModule;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152};
153
154} // namespace android
155
156#endif