| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2008, HTC Inc. |
| 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 "IMediaRecorder" |
| 20 | #include <utils/Log.h> |
| Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 21 | #include <binder/Parcel.h> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <ui/ISurface.h> |
| 23 | #include <ui/ICamera.h> |
| 24 | #include <media/IMediaPlayerClient.h> |
| 25 | #include <media/IMediaRecorder.h> |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | enum { |
| 30 | RELEASE = IBinder::FIRST_CALL_TRANSACTION, |
| 31 | INIT, |
| 32 | CLOSE, |
| 33 | RESET, |
| 34 | STOP, |
| 35 | START, |
| 36 | PREPARE, |
| 37 | GET_MAX_AMPLITUDE, |
| 38 | SET_VIDEO_SOURCE, |
| 39 | SET_AUDIO_SOURCE, |
| 40 | SET_OUTPUT_FORMAT, |
| 41 | SET_VIDEO_ENCODER, |
| 42 | SET_AUDIO_ENCODER, |
| 43 | SET_OUTPUT_FILE_PATH, |
| 44 | SET_OUTPUT_FILE_FD, |
| 45 | SET_VIDEO_SIZE, |
| 46 | SET_VIDEO_FRAMERATE, |
| The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 47 | SET_PARAMETERS, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | SET_PREVIEW_SURFACE, |
| 49 | SET_CAMERA, |
| 50 | SET_LISTENER |
| 51 | }; |
| 52 | |
| 53 | class BpMediaRecorder: public BpInterface<IMediaRecorder> |
| 54 | { |
| 55 | public: |
| 56 | BpMediaRecorder(const sp<IBinder>& impl) |
| 57 | : BpInterface<IMediaRecorder>(impl) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | status_t setCamera(const sp<ICamera>& camera) |
| 62 | { |
| 63 | LOGV("setCamera(%p)", camera.get()); |
| 64 | Parcel data, reply; |
| 65 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 66 | data.writeStrongBinder(camera->asBinder()); |
| 67 | remote()->transact(SET_CAMERA, data, &reply); |
| 68 | return reply.readInt32(); |
| 69 | } |
| 70 | |
| 71 | status_t setPreviewSurface(const sp<ISurface>& surface) |
| 72 | { |
| 73 | LOGV("setPreviewSurface(%p)", surface.get()); |
| 74 | Parcel data, reply; |
| 75 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 76 | data.writeStrongBinder(surface->asBinder()); |
| 77 | remote()->transact(SET_PREVIEW_SURFACE, data, &reply); |
| 78 | return reply.readInt32(); |
| 79 | } |
| 80 | |
| 81 | status_t init() |
| 82 | { |
| 83 | LOGV("init"); |
| 84 | Parcel data, reply; |
| 85 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 86 | remote()->transact(INIT, data, &reply); |
| 87 | return reply.readInt32(); |
| 88 | } |
| 89 | |
| 90 | status_t setVideoSource(int vs) |
| 91 | { |
| 92 | LOGV("setVideoSource(%d)", vs); |
| 93 | Parcel data, reply; |
| 94 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 95 | data.writeInt32(vs); |
| 96 | remote()->transact(SET_VIDEO_SOURCE, data, &reply); |
| 97 | return reply.readInt32(); |
| 98 | } |
| 99 | |
| 100 | status_t setAudioSource(int as) |
| 101 | { |
| 102 | LOGV("setAudioSource(%d)", as); |
| 103 | Parcel data, reply; |
| 104 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 105 | data.writeInt32(as); |
| 106 | remote()->transact(SET_AUDIO_SOURCE, data, &reply); |
| 107 | return reply.readInt32(); |
| 108 | } |
| 109 | |
| 110 | status_t setOutputFormat(int of) |
| 111 | { |
| 112 | LOGV("setOutputFormat(%d)", of); |
| 113 | Parcel data, reply; |
| 114 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 115 | data.writeInt32(of); |
| 116 | remote()->transact(SET_OUTPUT_FORMAT, data, &reply); |
| 117 | return reply.readInt32(); |
| 118 | } |
| 119 | |
| 120 | status_t setVideoEncoder(int ve) |
| 121 | { |
| 122 | LOGV("setVideoEncoder(%d)", ve); |
| 123 | Parcel data, reply; |
| 124 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 125 | data.writeInt32(ve); |
| 126 | remote()->transact(SET_VIDEO_ENCODER, data, &reply); |
| 127 | return reply.readInt32(); |
| 128 | } |
| 129 | |
| 130 | status_t setAudioEncoder(int ae) |
| 131 | { |
| 132 | LOGV("setAudioEncoder(%d)", ae); |
| 133 | Parcel data, reply; |
| 134 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 135 | data.writeInt32(ae); |
| 136 | remote()->transact(SET_AUDIO_ENCODER, data, &reply); |
| 137 | return reply.readInt32(); |
| 138 | } |
| 139 | |
| 140 | status_t setOutputFile(const char* path) |
| 141 | { |
| 142 | LOGV("setOutputFile(%s)", path); |
| 143 | Parcel data, reply; |
| 144 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 145 | data.writeCString(path); |
| 146 | remote()->transact(SET_OUTPUT_FILE_PATH, data, &reply); |
| 147 | return reply.readInt32(); |
| 148 | } |
| 149 | |
| 150 | status_t setOutputFile(int fd, int64_t offset, int64_t length) { |
| 151 | LOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length); |
| 152 | Parcel data, reply; |
| 153 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 154 | data.writeFileDescriptor(fd); |
| 155 | data.writeInt64(offset); |
| 156 | data.writeInt64(length); |
| 157 | remote()->transact(SET_OUTPUT_FILE_FD, data, &reply); |
| 158 | return reply.readInt32(); |
| 159 | } |
| 160 | |
| 161 | status_t setVideoSize(int width, int height) |
| 162 | { |
| 163 | LOGV("setVideoSize(%dx%d)", width, height); |
| 164 | Parcel data, reply; |
| 165 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 166 | data.writeInt32(width); |
| 167 | data.writeInt32(height); |
| 168 | remote()->transact(SET_VIDEO_SIZE, data, &reply); |
| 169 | return reply.readInt32(); |
| 170 | } |
| 171 | |
| 172 | status_t setVideoFrameRate(int frames_per_second) |
| 173 | { |
| 174 | LOGV("setVideoFrameRate(%d)", frames_per_second); |
| 175 | Parcel data, reply; |
| 176 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 177 | data.writeInt32(frames_per_second); |
| 178 | remote()->transact(SET_VIDEO_FRAMERATE, data, &reply); |
| 179 | return reply.readInt32(); |
| 180 | } |
| 181 | |
| The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 182 | status_t setParameters(const String8& params) |
| 183 | { |
| 184 | LOGV("setParameter(%s)", params.string()); |
| 185 | Parcel data, reply; |
| 186 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 187 | data.writeString8(params); |
| 188 | remote()->transact(SET_PARAMETERS, data, &reply); |
| 189 | return reply.readInt32(); |
| 190 | } |
| 191 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | status_t setListener(const sp<IMediaPlayerClient>& listener) |
| 193 | { |
| 194 | LOGV("setListener(%p)", listener.get()); |
| 195 | Parcel data, reply; |
| 196 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 197 | data.writeStrongBinder(listener->asBinder()); |
| 198 | remote()->transact(SET_LISTENER, data, &reply); |
| 199 | return reply.readInt32(); |
| 200 | } |
| 201 | |
| 202 | status_t prepare() |
| 203 | { |
| 204 | LOGV("prepare"); |
| 205 | Parcel data, reply; |
| 206 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 207 | remote()->transact(PREPARE, data, &reply); |
| 208 | return reply.readInt32(); |
| 209 | } |
| 210 | |
| 211 | status_t getMaxAmplitude(int* max) |
| 212 | { |
| 213 | LOGV("getMaxAmplitude"); |
| 214 | Parcel data, reply; |
| 215 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 216 | remote()->transact(GET_MAX_AMPLITUDE, data, &reply); |
| 217 | *max = reply.readInt32(); |
| 218 | return reply.readInt32(); |
| 219 | } |
| 220 | |
| 221 | status_t start() |
| 222 | { |
| 223 | LOGV("start"); |
| 224 | Parcel data, reply; |
| 225 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 226 | remote()->transact(START, data, &reply); |
| 227 | return reply.readInt32(); |
| 228 | } |
| 229 | |
| 230 | status_t stop() |
| 231 | { |
| 232 | LOGV("stop"); |
| 233 | Parcel data, reply; |
| 234 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 235 | remote()->transact(STOP, data, &reply); |
| 236 | return reply.readInt32(); |
| 237 | } |
| 238 | |
| 239 | status_t reset() |
| 240 | { |
| 241 | LOGV("reset"); |
| 242 | Parcel data, reply; |
| 243 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 244 | remote()->transact(RESET, data, &reply); |
| 245 | return reply.readInt32(); |
| 246 | } |
| 247 | |
| 248 | status_t close() |
| 249 | { |
| 250 | LOGV("close"); |
| 251 | Parcel data, reply; |
| 252 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 253 | remote()->transact(CLOSE, data, &reply); |
| 254 | return reply.readInt32(); |
| 255 | } |
| 256 | |
| 257 | status_t release() |
| 258 | { |
| 259 | LOGV("release"); |
| 260 | Parcel data, reply; |
| 261 | data.writeInterfaceToken(IMediaRecorder::getInterfaceDescriptor()); |
| 262 | remote()->transact(RELEASE, data, &reply); |
| 263 | return reply.readInt32(); |
| 264 | } |
| 265 | }; |
| 266 | |
| 267 | IMPLEMENT_META_INTERFACE(MediaRecorder, "android.hardware.IMediaRecorder"); |
| 268 | |
| 269 | // ---------------------------------------------------------------------- |
| 270 | |
| 271 | #define CHECK_INTERFACE(interface, data, reply) \ |
| 272 | do { if (!data.enforceInterface(interface::getInterfaceDescriptor())) { \ |
| 273 | LOGW("Call incorrectly routed to " #interface); \ |
| 274 | return PERMISSION_DENIED; \ |
| 275 | } } while (0) |
| 276 | |
| 277 | status_t BnMediaRecorder::onTransact( |
| 278 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 279 | { |
| 280 | switch(code) { |
| 281 | case RELEASE: { |
| 282 | LOGV("RELEASE"); |
| 283 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 284 | reply->writeInt32(release()); |
| 285 | return NO_ERROR; |
| 286 | } break; |
| 287 | case INIT: { |
| 288 | LOGV("INIT"); |
| 289 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 290 | reply->writeInt32(init()); |
| 291 | return NO_ERROR; |
| 292 | } break; |
| 293 | case CLOSE: { |
| 294 | LOGV("CLOSE"); |
| 295 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 296 | reply->writeInt32(close()); |
| 297 | return NO_ERROR; |
| 298 | } break; |
| 299 | case RESET: { |
| 300 | LOGV("RESET"); |
| 301 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 302 | reply->writeInt32(reset()); |
| 303 | return NO_ERROR; |
| 304 | } break; |
| 305 | case STOP: { |
| 306 | LOGV("STOP"); |
| 307 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 308 | reply->writeInt32(stop()); |
| 309 | return NO_ERROR; |
| 310 | } break; |
| 311 | case START: { |
| 312 | LOGV("START"); |
| 313 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 314 | reply->writeInt32(start()); |
| 315 | return NO_ERROR; |
| 316 | } break; |
| 317 | case PREPARE: { |
| 318 | LOGV("PREPARE"); |
| 319 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 320 | reply->writeInt32(prepare()); |
| 321 | return NO_ERROR; |
| 322 | } break; |
| 323 | case GET_MAX_AMPLITUDE: { |
| 324 | LOGV("GET_MAX_AMPLITUDE"); |
| 325 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 326 | int max = 0; |
| 327 | status_t ret = getMaxAmplitude(&max); |
| 328 | reply->writeInt32(max); |
| 329 | reply->writeInt32(ret); |
| 330 | return NO_ERROR; |
| 331 | } break; |
| 332 | case SET_VIDEO_SOURCE: { |
| 333 | LOGV("SET_VIDEO_SOURCE"); |
| 334 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 335 | int vs = data.readInt32(); |
| 336 | reply->writeInt32(setVideoSource(vs)); |
| 337 | return NO_ERROR; |
| 338 | } break; |
| 339 | case SET_AUDIO_SOURCE: { |
| 340 | LOGV("SET_AUDIO_SOURCE"); |
| 341 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 342 | int as = data.readInt32(); |
| 343 | reply->writeInt32(setAudioSource(as)); |
| 344 | return NO_ERROR; |
| 345 | } break; |
| 346 | case SET_OUTPUT_FORMAT: { |
| 347 | LOGV("SET_OUTPUT_FORMAT"); |
| 348 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 349 | int of = data.readInt32(); |
| 350 | reply->writeInt32(setOutputFormat(of)); |
| 351 | return NO_ERROR; |
| 352 | } break; |
| 353 | case SET_VIDEO_ENCODER: { |
| 354 | LOGV("SET_VIDEO_ENCODER"); |
| 355 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 356 | int ve = data.readInt32(); |
| 357 | reply->writeInt32(setVideoEncoder(ve)); |
| 358 | return NO_ERROR; |
| 359 | } break; |
| 360 | case SET_AUDIO_ENCODER: { |
| 361 | LOGV("SET_AUDIO_ENCODER"); |
| 362 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 363 | int ae = data.readInt32(); |
| 364 | reply->writeInt32(setAudioEncoder(ae)); |
| 365 | return NO_ERROR; |
| 366 | |
| 367 | } break; |
| 368 | case SET_OUTPUT_FILE_PATH: { |
| 369 | LOGV("SET_OUTPUT_FILE_PATH"); |
| 370 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 371 | const char* path = data.readCString(); |
| 372 | reply->writeInt32(setOutputFile(path)); |
| 373 | return NO_ERROR; |
| 374 | } break; |
| 375 | case SET_OUTPUT_FILE_FD: { |
| 376 | LOGV("SET_OUTPUT_FILE_FD"); |
| 377 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 378 | int fd = dup(data.readFileDescriptor()); |
| 379 | int64_t offset = data.readInt64(); |
| 380 | int64_t length = data.readInt64(); |
| 381 | reply->writeInt32(setOutputFile(fd, offset, length)); |
| 382 | return NO_ERROR; |
| 383 | } break; |
| 384 | case SET_VIDEO_SIZE: { |
| 385 | LOGV("SET_VIDEO_SIZE"); |
| 386 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 387 | int width = data.readInt32(); |
| 388 | int height = data.readInt32(); |
| 389 | reply->writeInt32(setVideoSize(width, height)); |
| 390 | return NO_ERROR; |
| 391 | } break; |
| 392 | case SET_VIDEO_FRAMERATE: { |
| 393 | LOGV("SET_VIDEO_FRAMERATE"); |
| 394 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 395 | int frames_per_second = data.readInt32(); |
| 396 | reply->writeInt32(setVideoFrameRate(frames_per_second)); |
| 397 | return NO_ERROR; |
| 398 | } break; |
| The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 399 | case SET_PARAMETERS: { |
| 400 | LOGV("SET_PARAMETER"); |
| 401 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 402 | reply->writeInt32(setParameters(data.readString8())); |
| 403 | return NO_ERROR; |
| 404 | } break; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | case SET_LISTENER: { |
| 406 | LOGV("SET_LISTENER"); |
| 407 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 408 | sp<IMediaPlayerClient> listener = |
| 409 | interface_cast<IMediaPlayerClient>(data.readStrongBinder()); |
| 410 | reply->writeInt32(setListener(listener)); |
| 411 | return NO_ERROR; |
| 412 | } break; |
| 413 | case SET_PREVIEW_SURFACE: { |
| 414 | LOGV("SET_PREVIEW_SURFACE"); |
| 415 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 416 | sp<ISurface> surface = interface_cast<ISurface>(data.readStrongBinder()); |
| 417 | reply->writeInt32(setPreviewSurface(surface)); |
| 418 | return NO_ERROR; |
| 419 | } break; |
| 420 | case SET_CAMERA: { |
| 421 | LOGV("SET_CAMERA"); |
| 422 | CHECK_INTERFACE(IMediaRecorder, data, reply); |
| 423 | sp<ICamera> camera = interface_cast<ICamera>(data.readStrongBinder()); |
| 424 | reply->writeInt32(setCamera(camera)); |
| 425 | return NO_ERROR; |
| 426 | } break; |
| 427 | default: |
| 428 | return BBinder::onTransact(code, data, reply, flags); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | // ---------------------------------------------------------------------------- |
| 433 | |
| 434 | }; // namespace android |