The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2008, The Android Open Source Project |
| 4 | ** |
| 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 <ui/ICameraService.h> |
| 22 | #include <ui/CameraHardwareInterface.h> |
| 23 | class android::MemoryHeapBase; |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | // ---------------------------------------------------------------------------- |
| 28 | |
| 29 | #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) |
| 30 | #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) |
| 31 | |
| 32 | // When enabled, this feature allows you to send an event to the CameraService |
| 33 | // so that you can cause all references to the heap object gWeakHeap, defined |
| 34 | // below, to be printed. You will also need to set DEBUG_REFS=1 and |
| 35 | // DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp. You just have to |
| 36 | // set gWeakHeap to the appropriate heap you want to track. |
| 37 | |
| 38 | #define DEBUG_HEAP_LEAKS 0 |
| 39 | |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | |
| 42 | class CameraService : public BnCameraService |
| 43 | { |
| 44 | class Client; |
| 45 | |
| 46 | public: |
| 47 | static void instantiate(); |
| 48 | |
| 49 | // ICameraService interface |
| 50 | virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient); |
| 51 | |
| 52 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 53 | |
| 54 | void removeClient(const sp<ICameraClient>& cameraClient); |
| 55 | |
| 56 | #if DEBUG_HEAP_LEAKS |
| 57 | virtual status_t onTransact( |
| 58 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags); |
| 59 | #endif |
| 60 | |
| 61 | private: |
| 62 | |
| 63 | // ---------------------------------------------------------------------------- |
| 64 | |
| 65 | class Client : public BnCamera { |
| 66 | |
| 67 | public: |
| 68 | virtual void disconnect(); |
| 69 | |
| 70 | // pass the buffered ISurface to the camera service |
| 71 | virtual status_t setPreviewDisplay(const sp<ISurface>& surface); |
| 72 | |
| 73 | // tell the service whether to callback with each preview frame |
| 74 | virtual void setHasFrameCallback(bool installed); |
| 75 | |
| 76 | // start preview mode, must call setPreviewDisplay first |
| 77 | virtual status_t startPreview(); |
| 78 | |
| 79 | // stop preview mode |
| 80 | virtual void stopPreview(); |
| 81 | |
| 82 | // auto focus |
| 83 | virtual status_t autoFocus(); |
| 84 | |
| 85 | // take a picture - returns an IMemory (ref-counted mmap) |
| 86 | virtual status_t takePicture(); |
| 87 | |
| 88 | // set preview/capture parameters - key/value pairs |
| 89 | virtual status_t setParameters(const String8& params); |
| 90 | |
| 91 | // get preview/capture parameters - key/value pairs |
| 92 | virtual String8 getParameters() const; |
| 93 | |
| 94 | // our client... |
| 95 | const sp<ICameraClient>& getCameraClient() const { return mCameraClient; } |
| 96 | |
| 97 | private: |
| 98 | friend class CameraService; |
| 99 | Client( const sp<CameraService>& cameraService, |
| 100 | const sp<ICameraClient>& cameraClient); |
| 101 | Client(); |
| 102 | virtual ~Client(); |
| 103 | |
| 104 | static void previewCallback(const sp<IMemory>& mem, void* user); |
| 105 | static void shutterCallback(void *user); |
| 106 | static void yuvPictureCallback(const sp<IMemory>& mem, void* user); |
| 107 | static void jpegPictureCallback(const sp<IMemory>& mem, void* user); |
| 108 | static void autoFocusCallback(bool focused, void* user); |
| 109 | static sp<Client> getClientFromCookie(void* user); |
| 110 | |
| 111 | void postShutter(); |
| 112 | void postRaw(const sp<IMemory>& mem); |
| 113 | void postJpeg(const sp<IMemory>& mem); |
| 114 | void postFrame(const sp<IMemory>& mem); |
| 115 | void postError(status_t error); |
| 116 | void postAutoFocus(bool focused); |
| 117 | |
| 118 | // Ensures atomicity among the public methods |
| 119 | mutable Mutex mLock; |
| 120 | // mSurfaceLock synchronizes access to mSurface between |
| 121 | // setPreviewSurface() and postFrame(). Note that among |
| 122 | // the public methods, all accesses to mSurface are |
| 123 | // syncrhonized by mLock. However, postFrame() is called |
| 124 | // by the CameraHardwareInterface callback, and needs to |
| 125 | // access mSurface. It cannot hold mLock, however, because |
| 126 | // stopPreview() may be holding that lock while attempting |
| 127 | // top stop preview, and stopPreview itself will block waiting |
| 128 | // for a callback from CameraHardwareInterface. If this |
| 129 | // happens, it will cause a deadlock. |
| 130 | mutable Mutex mSurfaceLock; |
| 131 | mutable Condition mReady; |
| 132 | sp<CameraService> mCameraService; |
| 133 | sp<ISurface> mSurface; |
| 134 | sp<MemoryHeapBase> mPreviewBuffer; |
| 135 | bool mHasFrameCallback; |
| 136 | |
| 137 | // these are immutable once the object is created, |
| 138 | // they don't need to be protected by a lock |
| 139 | sp<ICameraClient> mCameraClient; |
| 140 | sp<CameraHardwareInterface> mHardware; |
| 141 | }; |
| 142 | |
| 143 | // ---------------------------------------------------------------------------- |
| 144 | |
| 145 | CameraService(); |
| 146 | virtual ~CameraService(); |
| 147 | |
| 148 | mutable Mutex mLock; |
| 149 | wp<Client> mClient; |
| 150 | |
| 151 | #if DEBUG_HEAP_LEAKS |
| 152 | wp<IMemoryHeap> gWeakHeap; |
| 153 | #endif |
| 154 | }; |
| 155 | |
| 156 | // ---------------------------------------------------------------------------- |
| 157 | |
| 158 | }; // namespace android |
| 159 | |
| 160 | #endif |