MediaRecorderClient.cpp revision 30ab66297501757d745b9ae10da61adcd891f497
1/* 2 ** Copyright 2008, HTC Inc. 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#include <media/PVMediaRecorder.h> 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<ISurface>& 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::setVideoSize(int width, int height) 164{ 165 LOGV("setVideoSize(%dx%d)", width, height); 166 Mutex::Autolock lock(mLock); 167 if (mRecorder == NULL) { 168 LOGE("recorder is not initialized"); 169 return NO_INIT; 170 } 171 return mRecorder->setVideoSize(width, height); 172} 173 174status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second) 175{ 176 LOGV("setVideoFrameRate(%d)", frames_per_second); 177 Mutex::Autolock lock(mLock); 178 if (mRecorder == NULL) { 179 LOGE("recorder is not initialized"); 180 return NO_INIT; 181 } 182 return mRecorder->setVideoFrameRate(frames_per_second); 183} 184 185status_t MediaRecorderClient::setParameters(const String8& params) { 186 LOGV("setParameters(%s)", params.string()); 187 Mutex::Autolock lock(mLock); 188 if (mRecorder == NULL) { 189 LOGE("recorder is not initialized"); 190 return NO_INIT; 191 } 192 return mRecorder->setParameters(params); 193} 194 195status_t MediaRecorderClient::prepare() 196{ 197 LOGV("prepare"); 198 Mutex::Autolock lock(mLock); 199 if (mRecorder == NULL) { 200 LOGE("recorder is not initialized"); 201 return NO_INIT; 202 } 203 return mRecorder->prepare(); 204} 205 206 207status_t MediaRecorderClient::getMaxAmplitude(int* max) 208{ 209 LOGV("getMaxAmplitude"); 210 Mutex::Autolock lock(mLock); 211 if (mRecorder == NULL) { 212 LOGE("recorder is not initialized"); 213 return NO_INIT; 214 } 215 return mRecorder->getMaxAmplitude(max); 216} 217 218status_t MediaRecorderClient::start() 219{ 220 LOGV("start"); 221 Mutex::Autolock lock(mLock); 222 if (mRecorder == NULL) { 223 LOGE("recorder is not initialized"); 224 return NO_INIT; 225 } 226 return mRecorder->start(); 227 228} 229 230status_t MediaRecorderClient::stop() 231{ 232 LOGV("stop"); 233 Mutex::Autolock lock(mLock); 234 if (mRecorder == NULL) { 235 LOGE("recorder is not initialized"); 236 return NO_INIT; 237 } 238 return mRecorder->stop(); 239} 240 241status_t MediaRecorderClient::init() 242{ 243 LOGV("init"); 244 Mutex::Autolock lock(mLock); 245 if (mRecorder == NULL) { 246 LOGE("recorder is not initialized"); 247 return NO_INIT; 248 } 249 return mRecorder->init(); 250} 251 252status_t MediaRecorderClient::close() 253{ 254 LOGV("close"); 255 Mutex::Autolock lock(mLock); 256 if (mRecorder == NULL) { 257 LOGE("recorder is not initialized"); 258 return NO_INIT; 259 } 260 return mRecorder->close(); 261} 262 263 264status_t MediaRecorderClient::reset() 265{ 266 LOGV("reset"); 267 Mutex::Autolock lock(mLock); 268 if (mRecorder == NULL) { 269 LOGE("recorder is not initialized"); 270 return NO_INIT; 271 } 272 return mRecorder->reset(); 273} 274 275status_t MediaRecorderClient::release() 276{ 277 LOGV("release"); 278 Mutex::Autolock lock(mLock); 279 if (mRecorder != NULL) { 280 delete mRecorder; 281 mRecorder = NULL; 282 wp<MediaRecorderClient> client(this); 283 mMediaPlayerService->removeMediaRecorderClient(client); 284 } 285 return NO_ERROR; 286} 287 288MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid) 289{ 290 LOGV("Client constructor"); 291 mPid = pid; 292 293#if BUILD_WITH_FULL_STAGEFRIGHT 294 char value[PROPERTY_VALUE_MAX]; 295 if (property_get("media.stagefright.enable-record", value, NULL) 296 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) { 297 mRecorder = new StagefrightRecorder; 298 } else 299#endif 300 { 301 mRecorder = new PVMediaRecorder(); 302 } 303 304 mMediaPlayerService = service; 305} 306 307MediaRecorderClient::~MediaRecorderClient() 308{ 309 LOGV("Client destructor"); 310 release(); 311} 312 313status_t MediaRecorderClient::setListener(const sp<IMediaPlayerClient>& 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 324}; // namespace android 325 326