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