The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [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 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "ICameraClient" |
| 20 | #include <utils/Log.h> |
| 21 | #include <stdint.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <ui/ICameraClient.h> |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | enum { |
| 28 | SHUTTER_CALLBACK = IBinder::FIRST_CALL_TRANSACTION, |
| 29 | RAW_CALLBACK, |
| 30 | JPEG_CALLBACK, |
| 31 | PREVIEW_CALLBACK, |
| 32 | ERROR_CALLBACK, |
| 33 | AUTOFOCUS_CALLBACK, |
| 34 | RECORDING_CALLBACK, |
| 35 | }; |
| 36 | |
| 37 | class BpCameraClient: public BpInterface<ICameraClient> |
| 38 | { |
| 39 | public: |
| 40 | BpCameraClient(const sp<IBinder>& impl) |
| 41 | : BpInterface<ICameraClient>(impl) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | // callback to let the app know the shutter has closed, ideal for playing the shutter sound |
| 46 | void shutterCallback() |
| 47 | { |
| 48 | LOGV("shutterCallback"); |
| 49 | Parcel data, reply; |
| 50 | data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor()); |
| 51 | remote()->transact(SHUTTER_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY); |
| 52 | } |
| 53 | |
| 54 | // callback from camera service to app with picture data |
| 55 | void rawCallback(const sp<IMemory>& picture) |
| 56 | { |
| 57 | LOGV("rawCallback"); |
| 58 | Parcel data, reply; |
| 59 | data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor()); |
| 60 | data.writeStrongBinder(picture->asBinder()); |
| 61 | remote()->transact(RAW_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY); |
| 62 | } |
| 63 | |
| 64 | // callback from camera service to app with picture data |
| 65 | void jpegCallback(const sp<IMemory>& picture) |
| 66 | { |
| 67 | LOGV("jpegCallback"); |
| 68 | Parcel data, reply; |
| 69 | data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor()); |
| 70 | data.writeStrongBinder(picture->asBinder()); |
| 71 | remote()->transact(JPEG_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY); |
| 72 | } |
| 73 | |
| 74 | // callback from camera service to app with preview frame data |
| 75 | void previewCallback(const sp<IMemory>& frame) |
| 76 | { |
| 77 | LOGV("previewCallback"); |
| 78 | Parcel data, reply; |
| 79 | data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor()); |
| 80 | data.writeStrongBinder(frame->asBinder()); |
| 81 | remote()->transact(PREVIEW_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY); |
| 82 | } |
| 83 | |
| 84 | // callback from camera service to app with recording frame data |
| 85 | void recordingCallback(const sp<IMemory>& frame) |
| 86 | { |
| 87 | LOGV("recordingCallback"); |
| 88 | Parcel data, reply; |
| 89 | data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor()); |
| 90 | data.writeStrongBinder(frame->asBinder()); |
| 91 | remote()->transact(RECORDING_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY); |
| 92 | } |
| 93 | |
| 94 | // callback from camera service to app to report error |
| 95 | void errorCallback(status_t error) |
| 96 | { |
| 97 | LOGV("errorCallback"); |
| 98 | Parcel data, reply; |
| 99 | data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor()); |
| 100 | data.writeInt32(error); |
| 101 | remote()->transact(ERROR_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY); |
| 102 | } |
| 103 | |
| 104 | // callback from camera service to app to report autofocus completion |
| 105 | void autoFocusCallback(bool focused) |
| 106 | { |
| 107 | LOGV("autoFocusCallback"); |
| 108 | Parcel data, reply; |
| 109 | data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor()); |
| 110 | data.writeInt32(focused); |
| 111 | remote()->transact(AUTOFOCUS_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY); |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | IMPLEMENT_META_INTERFACE(CameraClient, "android.hardware.ICameraClient"); |
| 116 | |
| 117 | // ---------------------------------------------------------------------- |
| 118 | |
| 119 | #define CHECK_INTERFACE(interface, data, reply) \ |
| 120 | do { if (!data.enforceInterface(interface::getInterfaceDescriptor())) { \ |
| 121 | LOGW("Call incorrectly routed to " #interface); \ |
| 122 | return PERMISSION_DENIED; \ |
| 123 | } } while (0) |
| 124 | |
| 125 | status_t BnCameraClient::onTransact( |
| 126 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 127 | { |
| 128 | switch(code) { |
| 129 | case SHUTTER_CALLBACK: { |
| 130 | LOGV("SHUTTER_CALLBACK"); |
| 131 | CHECK_INTERFACE(ICameraClient, data, reply); |
| 132 | shutterCallback(); |
| 133 | return NO_ERROR; |
| 134 | } break; |
| 135 | case RAW_CALLBACK: { |
| 136 | LOGV("RAW_CALLBACK"); |
| 137 | CHECK_INTERFACE(ICameraClient, data, reply); |
| 138 | sp<IMemory> picture = interface_cast<IMemory>(data.readStrongBinder()); |
| 139 | rawCallback(picture); |
| 140 | return NO_ERROR; |
| 141 | } break; |
| 142 | case JPEG_CALLBACK: { |
| 143 | LOGV("JPEG_CALLBACK"); |
| 144 | CHECK_INTERFACE(ICameraClient, data, reply); |
| 145 | sp<IMemory> picture = interface_cast<IMemory>(data.readStrongBinder()); |
| 146 | jpegCallback(picture); |
| 147 | return NO_ERROR; |
| 148 | } break; |
| 149 | case PREVIEW_CALLBACK: { |
| 150 | LOGV("PREVIEW_CALLBACK"); |
| 151 | CHECK_INTERFACE(ICameraClient, data, reply); |
| 152 | sp<IMemory> frame = interface_cast<IMemory>(data.readStrongBinder()); |
| 153 | previewCallback(frame); |
| 154 | return NO_ERROR; |
| 155 | } break; |
| 156 | case RECORDING_CALLBACK: { |
| 157 | LOGV("RECORDING_CALLBACK"); |
| 158 | CHECK_INTERFACE(ICameraClient, data, reply); |
| 159 | sp<IMemory> frame = interface_cast<IMemory>(data.readStrongBinder()); |
| 160 | recordingCallback(frame); |
| 161 | return NO_ERROR; |
| 162 | } break; |
| 163 | case ERROR_CALLBACK: { |
| 164 | LOGV("ERROR_CALLBACK"); |
| 165 | CHECK_INTERFACE(ICameraClient, data, reply); |
| 166 | status_t error = data.readInt32(); |
| 167 | errorCallback(error); |
| 168 | return NO_ERROR; |
| 169 | } break; |
| 170 | case AUTOFOCUS_CALLBACK: { |
| 171 | LOGV("AUTOFOCUS_CALLBACK"); |
| 172 | CHECK_INTERFACE(ICameraClient, data, reply); |
| 173 | bool focused = (bool)data.readInt32(); |
| 174 | autoFocusCallback(focused); |
| 175 | return NO_ERROR; |
| 176 | } break; |
| 177 | default: |
| 178 | return BBinder::onTransact(code, data, reply, flags); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // ---------------------------------------------------------------------------- |
| 183 | |
| 184 | }; // namespace android |
| 185 | |