MediaRecorderClient.cpp revision be71aa29a3c86d2e01cd17839d2a72ab09a1bce5
1/* 2 ** Copyright 2008, The Android Open Source Project 3 ** 4 ** Licensed under the Apache License, Version 2.0 (the "License"); 5 ** you may not use this file except in compliance with the License. 6 ** You may obtain a copy of the License at 7 ** 8 ** http://www.apache.org/licenses/LICENSE-2.0 9 ** 10 ** Unless required by applicable law or agreed to in writing, software 11 ** distributed under the License is distributed on an "AS IS" BASIS, 12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 ** See the License for the specific language governing permissions and 14 ** limitations under the License. 15 */ 16 17//#define LOG_NDEBUG 0 18#define LOG_TAG "MediaRecorderService" 19#include <utils/Log.h> 20 21#include <sys/types.h> 22#include <sys/stat.h> 23#include <dirent.h> 24#include <unistd.h> 25#include <string.h> 26#include <cutils/atomic.h> 27#include <cutils/properties.h> // for property_get 28#include <binder/IPCThreadState.h> 29#include <binder/IServiceManager.h> 30#include <binder/MemoryHeapBase.h> 31#include <binder/MemoryBase.h> 32 33#include <utils/String16.h> 34 35#include <system/audio.h> 36 37#include "MediaRecorderClient.h" 38#include "MediaPlayerService.h" 39 40#include "StagefrightRecorder.h" 41#include <gui/IGraphicBufferProducer.h> 42 43namespace android { 44 45const char* cameraPermission = "android.permission.CAMERA"; 46const char* recordAudioPermission = "android.permission.RECORD_AUDIO"; 47 48static bool checkPermission(const char* permissionString) { 49#ifndef HAVE_ANDROID_OS 50 return true; 51#endif 52 if (getpid() == IPCThreadState::self()->getCallingPid()) return true; 53 bool ok = checkCallingPermission(String16(permissionString)); 54 if (!ok) ALOGE("Request requires %s", permissionString); 55 return ok; 56} 57 58 59sp<IGraphicBufferProducer> MediaRecorderClient::querySurfaceMediaSource() 60{ 61 ALOGV("Query SurfaceMediaSource"); 62 Mutex::Autolock lock(mLock); 63 if (mRecorder == NULL) { 64 ALOGE("recorder is not initialized"); 65 return NULL; 66 } 67 return mRecorder->querySurfaceMediaSource(); 68} 69 70 71 72status_t MediaRecorderClient::setCamera(const sp<ICamera>& camera, 73 const sp<ICameraRecordingProxy>& proxy) 74{ 75 ALOGV("setCamera"); 76 Mutex::Autolock lock(mLock); 77 if (mRecorder == NULL) { 78 ALOGE("recorder is not initialized"); 79 return NO_INIT; 80 } 81 return mRecorder->setCamera(camera, proxy); 82} 83 84status_t MediaRecorderClient::setPreviewSurface(const sp<IGraphicBufferProducer>& surface) 85{ 86 ALOGV("setPreviewSurface"); 87 Mutex::Autolock lock(mLock); 88 if (mRecorder == NULL) { 89 ALOGE("recorder is not initialized"); 90 return NO_INIT; 91 } 92 return mRecorder->setPreviewSurface(surface); 93} 94 95status_t MediaRecorderClient::setVideoSource(int vs) 96{ 97 ALOGV("setVideoSource(%d)", vs); 98 // Check camera permission for sources other than SURFACE 99 if (vs != VIDEO_SOURCE_SURFACE && !checkPermission(cameraPermission)) { 100 return PERMISSION_DENIED; 101 } 102 Mutex::Autolock lock(mLock); 103 if (mRecorder == NULL) { 104 ALOGE("recorder is not initialized"); 105 return NO_INIT; 106 } 107 return mRecorder->setVideoSource((video_source)vs); 108} 109 110status_t MediaRecorderClient::setAudioSource(int as) 111{ 112 ALOGV("setAudioSource(%d)", as); 113 if (!checkPermission(recordAudioPermission)) { 114 return PERMISSION_DENIED; 115 } 116 Mutex::Autolock lock(mLock); 117 if (mRecorder == NULL) { 118 ALOGE("recorder is not initialized"); 119 return NO_INIT; 120 } 121 return mRecorder->setAudioSource((audio_source_t)as); 122} 123 124status_t MediaRecorderClient::setOutputFormat(int of) 125{ 126 ALOGV("setOutputFormat(%d)", of); 127 Mutex::Autolock lock(mLock); 128 if (mRecorder == NULL) { 129 ALOGE("recorder is not initialized"); 130 return NO_INIT; 131 } 132 return mRecorder->setOutputFormat((output_format)of); 133} 134 135status_t MediaRecorderClient::setVideoEncoder(int ve) 136{ 137 ALOGV("setVideoEncoder(%d)", ve); 138 Mutex::Autolock lock(mLock); 139 if (mRecorder == NULL) { 140 ALOGE("recorder is not initialized"); 141 return NO_INIT; 142 } 143 return mRecorder->setVideoEncoder((video_encoder)ve); 144} 145 146status_t MediaRecorderClient::setAudioEncoder(int ae) 147{ 148 ALOGV("setAudioEncoder(%d)", ae); 149 Mutex::Autolock lock(mLock); 150 if (mRecorder == NULL) { 151 ALOGE("recorder is not initialized"); 152 return NO_INIT; 153 } 154 return mRecorder->setAudioEncoder((audio_encoder)ae); 155} 156 157status_t MediaRecorderClient::setOutputFile(int fd, int64_t offset, int64_t length) 158{ 159 ALOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length); 160 Mutex::Autolock lock(mLock); 161 if (mRecorder == NULL) { 162 ALOGE("recorder is not initialized"); 163 return NO_INIT; 164 } 165 return mRecorder->setOutputFile(fd, offset, length); 166} 167 168status_t MediaRecorderClient::setVideoSize(int width, int height) 169{ 170 ALOGV("setVideoSize(%dx%d)", width, height); 171 Mutex::Autolock lock(mLock); 172 if (mRecorder == NULL) { 173 ALOGE("recorder is not initialized"); 174 return NO_INIT; 175 } 176 return mRecorder->setVideoSize(width, height); 177} 178 179status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second) 180{ 181 ALOGV("setVideoFrameRate(%d)", frames_per_second); 182 Mutex::Autolock lock(mLock); 183 if (mRecorder == NULL) { 184 ALOGE("recorder is not initialized"); 185 return NO_INIT; 186 } 187 return mRecorder->setVideoFrameRate(frames_per_second); 188} 189 190status_t MediaRecorderClient::setParameters(const String8& params) { 191 ALOGV("setParameters(%s)", params.string()); 192 Mutex::Autolock lock(mLock); 193 if (mRecorder == NULL) { 194 ALOGE("recorder is not initialized"); 195 return NO_INIT; 196 } 197 return mRecorder->setParameters(params); 198} 199 200status_t MediaRecorderClient::prepare() 201{ 202 ALOGV("prepare"); 203 Mutex::Autolock lock(mLock); 204 if (mRecorder == NULL) { 205 ALOGE("recorder is not initialized"); 206 return NO_INIT; 207 } 208 return mRecorder->prepare(); 209} 210 211 212status_t MediaRecorderClient::getMaxAmplitude(int* max) 213{ 214 ALOGV("getMaxAmplitude"); 215 Mutex::Autolock lock(mLock); 216 if (mRecorder == NULL) { 217 ALOGE("recorder is not initialized"); 218 return NO_INIT; 219 } 220 return mRecorder->getMaxAmplitude(max); 221} 222 223status_t MediaRecorderClient::start() 224{ 225 ALOGV("start"); 226 Mutex::Autolock lock(mLock); 227 if (mRecorder == NULL) { 228 ALOGE("recorder is not initialized"); 229 return NO_INIT; 230 } 231 return mRecorder->start(); 232 233} 234 235status_t MediaRecorderClient::stop() 236{ 237 ALOGV("stop"); 238 Mutex::Autolock lock(mLock); 239 if (mRecorder == NULL) { 240 ALOGE("recorder is not initialized"); 241 return NO_INIT; 242 } 243 return mRecorder->stop(); 244} 245 246status_t MediaRecorderClient::init() 247{ 248 ALOGV("init"); 249 Mutex::Autolock lock(mLock); 250 if (mRecorder == NULL) { 251 ALOGE("recorder is not initialized"); 252 return NO_INIT; 253 } 254 return mRecorder->init(); 255} 256 257status_t MediaRecorderClient::close() 258{ 259 ALOGV("close"); 260 Mutex::Autolock lock(mLock); 261 if (mRecorder == NULL) { 262 ALOGE("recorder is not initialized"); 263 return NO_INIT; 264 } 265 return mRecorder->close(); 266} 267 268 269status_t MediaRecorderClient::reset() 270{ 271 ALOGV("reset"); 272 Mutex::Autolock lock(mLock); 273 if (mRecorder == NULL) { 274 ALOGE("recorder is not initialized"); 275 return NO_INIT; 276 } 277 return mRecorder->reset(); 278} 279 280status_t MediaRecorderClient::release() 281{ 282 ALOGV("release"); 283 Mutex::Autolock lock(mLock); 284 if (mRecorder != NULL) { 285 delete mRecorder; 286 mRecorder = NULL; 287 wp<MediaRecorderClient> client(this); 288 mMediaPlayerService->removeMediaRecorderClient(client); 289 } 290 return NO_ERROR; 291} 292 293MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid, 294 const String16& opPackageName) 295{ 296 ALOGV("Client constructor"); 297 mPid = pid; 298 mRecorder = new StagefrightRecorder(opPackageName); 299 mMediaPlayerService = service; 300} 301 302MediaRecorderClient::~MediaRecorderClient() 303{ 304 ALOGV("Client destructor"); 305 release(); 306} 307 308status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener) 309{ 310 ALOGV("setListener"); 311 Mutex::Autolock lock(mLock); 312 if (mRecorder == NULL) { 313 ALOGE("recorder is not initialized"); 314 return NO_INIT; 315 } 316 return mRecorder->setListener(listener); 317} 318 319status_t MediaRecorderClient::setClientName(const String16& clientName) { 320 ALOGV("setClientName(%s)", String8(clientName).string()); 321 Mutex::Autolock lock(mLock); 322 if (mRecorder == NULL) { 323 ALOGE("recorder is not initialized"); 324 return NO_INIT; 325 } 326 return mRecorder->setClientName(clientName); 327} 328 329status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) { 330 if (mRecorder != NULL) { 331 return mRecorder->dump(fd, args); 332 } 333 return OK; 334} 335 336}; // namespace android 337