MediaRecorderClient.cpp revision 308f3927dee3861586c17df267265ae0c86d79f7
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 if (getpid() == IPCThreadState::self()->getCallingPid()) return true; 50 bool ok = checkCallingPermission(String16(permissionString)); 51 if (!ok) ALOGE("Request requires %s", permissionString); 52 return ok; 53} 54 55 56sp<IGraphicBufferProducer> MediaRecorderClient::querySurfaceMediaSource() 57{ 58 ALOGV("Query SurfaceMediaSource"); 59 Mutex::Autolock lock(mLock); 60 if (mRecorder == NULL) { 61 ALOGE("recorder is not initialized"); 62 return NULL; 63 } 64 return mRecorder->querySurfaceMediaSource(); 65} 66 67 68 69status_t MediaRecorderClient::setCamera(const sp<ICamera>& camera, 70 const sp<ICameraRecordingProxy>& proxy) 71{ 72 ALOGV("setCamera"); 73 Mutex::Autolock lock(mLock); 74 if (mRecorder == NULL) { 75 ALOGE("recorder is not initialized"); 76 return NO_INIT; 77 } 78 return mRecorder->setCamera(camera, proxy); 79} 80 81status_t MediaRecorderClient::setPreviewSurface(const sp<IGraphicBufferProducer>& surface) 82{ 83 ALOGV("setPreviewSurface"); 84 Mutex::Autolock lock(mLock); 85 if (mRecorder == NULL) { 86 ALOGE("recorder is not initialized"); 87 return NO_INIT; 88 } 89 return mRecorder->setPreviewSurface(surface); 90} 91 92status_t MediaRecorderClient::setVideoSource(int vs) 93{ 94 ALOGV("setVideoSource(%d)", vs); 95 // Check camera permission for sources other than SURFACE 96 if (vs != VIDEO_SOURCE_SURFACE && !checkPermission(cameraPermission)) { 97 return PERMISSION_DENIED; 98 } 99 Mutex::Autolock lock(mLock); 100 if (mRecorder == NULL) { 101 ALOGE("recorder is not initialized"); 102 return NO_INIT; 103 } 104 return mRecorder->setVideoSource((video_source)vs); 105} 106 107status_t MediaRecorderClient::setAudioSource(int as) 108{ 109 ALOGV("setAudioSource(%d)", as); 110 if (!checkPermission(recordAudioPermission)) { 111 return PERMISSION_DENIED; 112 } 113 Mutex::Autolock lock(mLock); 114 if (mRecorder == NULL) { 115 ALOGE("recorder is not initialized"); 116 return NO_INIT; 117 } 118 return mRecorder->setAudioSource((audio_source_t)as); 119} 120 121status_t MediaRecorderClient::setOutputFormat(int of) 122{ 123 ALOGV("setOutputFormat(%d)", of); 124 Mutex::Autolock lock(mLock); 125 if (mRecorder == NULL) { 126 ALOGE("recorder is not initialized"); 127 return NO_INIT; 128 } 129 return mRecorder->setOutputFormat((output_format)of); 130} 131 132status_t MediaRecorderClient::setVideoEncoder(int ve) 133{ 134 ALOGV("setVideoEncoder(%d)", ve); 135 Mutex::Autolock lock(mLock); 136 if (mRecorder == NULL) { 137 ALOGE("recorder is not initialized"); 138 return NO_INIT; 139 } 140 return mRecorder->setVideoEncoder((video_encoder)ve); 141} 142 143status_t MediaRecorderClient::setAudioEncoder(int ae) 144{ 145 ALOGV("setAudioEncoder(%d)", ae); 146 Mutex::Autolock lock(mLock); 147 if (mRecorder == NULL) { 148 ALOGE("recorder is not initialized"); 149 return NO_INIT; 150 } 151 return mRecorder->setAudioEncoder((audio_encoder)ae); 152} 153 154status_t MediaRecorderClient::setOutputFile(const char* path) 155{ 156 ALOGV("setOutputFile(%s)", path); 157 Mutex::Autolock lock(mLock); 158 if (mRecorder == NULL) { 159 ALOGE("recorder is not initialized"); 160 return NO_INIT; 161 } 162 return mRecorder->setOutputFile(path); 163} 164 165status_t MediaRecorderClient::setOutputFile(int fd, int64_t offset, int64_t length) 166{ 167 ALOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length); 168 Mutex::Autolock lock(mLock); 169 if (mRecorder == NULL) { 170 ALOGE("recorder is not initialized"); 171 return NO_INIT; 172 } 173 return mRecorder->setOutputFile(fd, offset, length); 174} 175 176status_t MediaRecorderClient::setVideoSize(int width, int height) 177{ 178 ALOGV("setVideoSize(%dx%d)", width, height); 179 Mutex::Autolock lock(mLock); 180 if (mRecorder == NULL) { 181 ALOGE("recorder is not initialized"); 182 return NO_INIT; 183 } 184 return mRecorder->setVideoSize(width, height); 185} 186 187status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second) 188{ 189 ALOGV("setVideoFrameRate(%d)", frames_per_second); 190 Mutex::Autolock lock(mLock); 191 if (mRecorder == NULL) { 192 ALOGE("recorder is not initialized"); 193 return NO_INIT; 194 } 195 return mRecorder->setVideoFrameRate(frames_per_second); 196} 197 198status_t MediaRecorderClient::setParameters(const String8& params) { 199 ALOGV("setParameters(%s)", params.string()); 200 Mutex::Autolock lock(mLock); 201 if (mRecorder == NULL) { 202 ALOGE("recorder is not initialized"); 203 return NO_INIT; 204 } 205 return mRecorder->setParameters(params); 206} 207 208status_t MediaRecorderClient::prepare() 209{ 210 ALOGV("prepare"); 211 Mutex::Autolock lock(mLock); 212 if (mRecorder == NULL) { 213 ALOGE("recorder is not initialized"); 214 return NO_INIT; 215 } 216 return mRecorder->prepare(); 217} 218 219 220status_t MediaRecorderClient::getMaxAmplitude(int* max) 221{ 222 ALOGV("getMaxAmplitude"); 223 Mutex::Autolock lock(mLock); 224 if (mRecorder == NULL) { 225 ALOGE("recorder is not initialized"); 226 return NO_INIT; 227 } 228 return mRecorder->getMaxAmplitude(max); 229} 230 231status_t MediaRecorderClient::start() 232{ 233 ALOGV("start"); 234 Mutex::Autolock lock(mLock); 235 if (mRecorder == NULL) { 236 ALOGE("recorder is not initialized"); 237 return NO_INIT; 238 } 239 return mRecorder->start(); 240 241} 242 243status_t MediaRecorderClient::stop() 244{ 245 ALOGV("stop"); 246 Mutex::Autolock lock(mLock); 247 if (mRecorder == NULL) { 248 ALOGE("recorder is not initialized"); 249 return NO_INIT; 250 } 251 return mRecorder->stop(); 252} 253 254status_t MediaRecorderClient::init() 255{ 256 ALOGV("init"); 257 Mutex::Autolock lock(mLock); 258 if (mRecorder == NULL) { 259 ALOGE("recorder is not initialized"); 260 return NO_INIT; 261 } 262 return mRecorder->init(); 263} 264 265status_t MediaRecorderClient::close() 266{ 267 ALOGV("close"); 268 Mutex::Autolock lock(mLock); 269 if (mRecorder == NULL) { 270 ALOGE("recorder is not initialized"); 271 return NO_INIT; 272 } 273 return mRecorder->close(); 274} 275 276 277status_t MediaRecorderClient::reset() 278{ 279 ALOGV("reset"); 280 Mutex::Autolock lock(mLock); 281 if (mRecorder == NULL) { 282 ALOGE("recorder is not initialized"); 283 return NO_INIT; 284 } 285 return mRecorder->reset(); 286} 287 288status_t MediaRecorderClient::release() 289{ 290 ALOGV("release"); 291 Mutex::Autolock lock(mLock); 292 if (mRecorder != NULL) { 293 delete mRecorder; 294 mRecorder = NULL; 295 wp<MediaRecorderClient> client(this); 296 mMediaPlayerService->removeMediaRecorderClient(client); 297 } 298 return NO_ERROR; 299} 300 301MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid) 302{ 303 ALOGV("Client constructor"); 304 mPid = pid; 305 mRecorder = new StagefrightRecorder; 306 mMediaPlayerService = service; 307} 308 309MediaRecorderClient::~MediaRecorderClient() 310{ 311 ALOGV("Client destructor"); 312 release(); 313} 314 315status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener) 316{ 317 ALOGV("setListener"); 318 Mutex::Autolock lock(mLock); 319 if (mRecorder == NULL) { 320 ALOGE("recorder is not initialized"); 321 return NO_INIT; 322 } 323 return mRecorder->setListener(listener); 324} 325 326status_t MediaRecorderClient::setClientName(const String16& clientName) { 327 ALOGV("setClientName(%s)", String8(clientName).string()); 328 Mutex::Autolock lock(mLock); 329 if (mRecorder == NULL) { 330 ALOGE("recorder is not initialized"); 331 return NO_INIT; 332 } 333 return mRecorder->setClientName(clientName); 334} 335 336status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) const { 337 if (mRecorder != NULL) { 338 return mRecorder->dump(fd, args); 339 } 340 return OK; 341} 342 343}; // namespace android 344