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