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