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