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