The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3 | ** Copyright (C) 2008, The Android Open Source Project |
| 4 | ** Copyright (C) 2008 HTC Inc. |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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> |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 24 | #include <ui/Camera.h> |
| 25 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | class android::MemoryHeapBase; |
| 27 | |
| 28 | namespace android { |
| 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 |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 38 | // DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp. You just have to |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 39 | // set gWeakHeap to the appropriate heap you want to track. |
| 40 | |
| 41 | #define DEBUG_HEAP_LEAKS 0 |
| 42 | |
| 43 | // ---------------------------------------------------------------------------- |
| 44 | |
| 45 | class CameraService : public BnCameraService |
| 46 | { |
| 47 | class Client; |
| 48 | |
| 49 | public: |
| 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 | #if DEBUG_HEAP_LEAKS |
| 60 | virtual status_t onTransact( |
| 61 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags); |
| 62 | #endif |
| 63 | |
| 64 | private: |
| 65 | |
| 66 | // ---------------------------------------------------------------------------- |
| 67 | |
| 68 | class Client : public BnCamera { |
| 69 | |
| 70 | public: |
| 71 | virtual void disconnect(); |
| 72 | |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 73 | // connect new client with existing camera remote |
| 74 | virtual status_t connect(const sp<ICameraClient>& client); |
| 75 | |
The Android Open Source Project | 2762932 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 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 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 82 | // pass the buffered ISurface to the camera service |
| 83 | virtual status_t setPreviewDisplay(const sp<ISurface>& surface); |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 84 | |
| 85 | // set the frame callback flag to affect how the received frames from |
| 86 | // preview are handled. |
| 87 | virtual void setFrameCallbackFlag(int frame_callback_flag); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 88 | |
| 89 | // start preview mode, must call setPreviewDisplay first |
| 90 | virtual status_t startPreview(); |
| 91 | |
| 92 | // stop preview mode |
| 93 | virtual void stopPreview(); |
| 94 | |
The Android Open Source Project | 2762932 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 95 | // get preview state |
| 96 | virtual bool previewEnabled(); |
| 97 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 98 | // auto focus |
| 99 | virtual status_t autoFocus(); |
| 100 | |
| 101 | // take a picture - returns an IMemory (ref-counted mmap) |
| 102 | virtual status_t takePicture(); |
| 103 | |
| 104 | // set preview/capture parameters - key/value pairs |
| 105 | virtual status_t setParameters(const String8& params); |
| 106 | |
| 107 | // get preview/capture parameters - key/value pairs |
| 108 | virtual String8 getParameters() const; |
| 109 | |
| 110 | // our client... |
| 111 | const sp<ICameraClient>& getCameraClient() const { return mCameraClient; } |
| 112 | |
| 113 | private: |
| 114 | friend class CameraService; |
The Android Open Source Project | 2762932 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 115 | Client(const sp<CameraService>& cameraService, |
| 116 | const sp<ICameraClient>& cameraClient, |
| 117 | pid_t clientPid); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 118 | Client(); |
| 119 | virtual ~Client(); |
| 120 | |
The Android Open Source Project | 2762932 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 121 | status_t checkPid(); |
| 122 | |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 123 | static void previewCallback(const sp<IMemory>& mem, void* user); |
| 124 | static void shutterCallback(void *user); |
| 125 | static void yuvPictureCallback(const sp<IMemory>& mem, void* user); |
| 126 | static void jpegPictureCallback(const sp<IMemory>& mem, void* user); |
| 127 | static void autoFocusCallback(bool focused, void* user); |
| 128 | static sp<Client> getClientFromCookie(void* user); |
| 129 | |
| 130 | void postShutter(); |
| 131 | void postRaw(const sp<IMemory>& mem); |
| 132 | void postJpeg(const sp<IMemory>& mem); |
| 133 | void postFrame(const sp<IMemory>& mem); |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 134 | void copyFrameAndPostCopiedFrame(sp<IMemoryHeap> heap, size_t offset, size_t size); |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 135 | void postError(status_t error); |
| 136 | void postAutoFocus(bool focused); |
| 137 | |
| 138 | // Ensures atomicity among the public methods |
| 139 | mutable Mutex mLock; |
| 140 | // mSurfaceLock synchronizes access to mSurface between |
| 141 | // setPreviewSurface() and postFrame(). Note that among |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 142 | // the public methods, all accesses to mSurface are |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 143 | // syncrhonized by mLock. However, postFrame() is called |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 144 | // by the CameraHardwareInterface callback, and needs to |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 145 | // access mSurface. It cannot hold mLock, however, because |
| 146 | // stopPreview() may be holding that lock while attempting |
The Android Open Source Project | 2762932 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 147 | // to stop preview, and stopPreview itself will block waiting |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 148 | // for a callback from CameraHardwareInterface. If this |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 149 | // happens, it will cause a deadlock. |
| 150 | mutable Mutex mSurfaceLock; |
| 151 | mutable Condition mReady; |
| 152 | sp<CameraService> mCameraService; |
| 153 | sp<ISurface> mSurface; |
| 154 | sp<MemoryHeapBase> mPreviewBuffer; |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 155 | int mFrameCallbackFlag; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 156 | |
| 157 | // these are immutable once the object is created, |
| 158 | // they don't need to be protected by a lock |
| 159 | sp<ICameraClient> mCameraClient; |
| 160 | sp<CameraHardwareInterface> mHardware; |
The Android Open Source Project | 2762932 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 161 | pid_t mClientPid; |
The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | // ---------------------------------------------------------------------------- |
| 165 | |
| 166 | CameraService(); |
| 167 | virtual ~CameraService(); |
| 168 | |
| 169 | mutable Mutex mLock; |
| 170 | wp<Client> mClient; |
| 171 | |
| 172 | #if DEBUG_HEAP_LEAKS |
| 173 | wp<IMemoryHeap> gWeakHeap; |
| 174 | #endif |
| 175 | }; |
| 176 | |
| 177 | // ---------------------------------------------------------------------------- |
| 178 | |
| 179 | }; // namespace android |
| 180 | |
| 181 | #endif |