The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
Jean-Baptiste Queru | 4506c62 | 2010-07-29 17:35:37 -0700 | [diff] [blame] | 2 | ** Copyright 2008, The Android Open Source Project |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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> |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 27 | #include <cutils/properties.h> // for property_get |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include <android_runtime/ActivityManager.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 29 | #include <binder/IPCThreadState.h> |
| 30 | #include <binder/IServiceManager.h> |
| 31 | #include <binder/MemoryHeapBase.h> |
| 32 | #include <binder/MemoryBase.h> |
Andreas Huber | bfb9fb1 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 33 | |
| 34 | #ifndef NO_OPENCORE |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | #include <media/PVMediaRecorder.h> |
Andreas Huber | bfb9fb1 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 36 | #endif |
| 37 | |
Dave Sparks | 17612fc | 2009-03-27 19:56:11 -0700 | [diff] [blame] | 38 | #include <utils/String16.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 40 | #include <media/AudioTrack.h> |
| 41 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | #include "MediaRecorderClient.h" |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 43 | #include "MediaPlayerService.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 45 | #include "StagefrightRecorder.h" |
| 46 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | namespace android { |
| 48 | |
Dave Sparks | 17612fc | 2009-03-27 19:56:11 -0700 | [diff] [blame] | 49 | const char* cameraPermission = "android.permission.CAMERA"; |
Dave Sparks | 6690dc5 | 2009-05-20 19:20:31 -0700 | [diff] [blame] | 50 | const char* recordAudioPermission = "android.permission.RECORD_AUDIO"; |
Dave Sparks | 17612fc | 2009-03-27 19:56:11 -0700 | [diff] [blame] | 51 | |
| 52 | static bool checkPermission(const char* permissionString) { |
| 53 | #ifndef HAVE_ANDROID_OS |
| 54 | return true; |
| 55 | #endif |
| 56 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; |
| 57 | bool ok = checkCallingPermission(String16(permissionString)); |
| 58 | if (!ok) LOGE("Request requires %s", permissionString); |
| 59 | return ok; |
| 60 | } |
| 61 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | status_t MediaRecorderClient::setCamera(const sp<ICamera>& camera) |
| 63 | { |
| 64 | LOGV("setCamera"); |
| 65 | Mutex::Autolock lock(mLock); |
| 66 | if (mRecorder == NULL) { |
| 67 | LOGE("recorder is not initialized"); |
| 68 | return NO_INIT; |
| 69 | } |
| 70 | return mRecorder->setCamera(camera); |
| 71 | } |
| 72 | |
Jamie Gennis | 85cfdd0 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 73 | status_t MediaRecorderClient::setPreviewSurface(const sp<Surface>& surface) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | { |
| 75 | LOGV("setPreviewSurface"); |
| 76 | Mutex::Autolock lock(mLock); |
| 77 | if (mRecorder == NULL) { |
| 78 | LOGE("recorder is not initialized"); |
| 79 | return NO_INIT; |
| 80 | } |
| 81 | return mRecorder->setPreviewSurface(surface); |
| 82 | } |
| 83 | |
| 84 | status_t MediaRecorderClient::setVideoSource(int vs) |
| 85 | { |
| 86 | LOGV("setVideoSource(%d)", vs); |
Dave Sparks | 17612fc | 2009-03-27 19:56:11 -0700 | [diff] [blame] | 87 | if (!checkPermission(cameraPermission)) { |
| 88 | return PERMISSION_DENIED; |
| 89 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | Mutex::Autolock lock(mLock); |
| 91 | if (mRecorder == NULL) { |
| 92 | LOGE("recorder is not initialized"); |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 93 | return NO_INIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | } |
| 95 | return mRecorder->setVideoSource((video_source)vs); |
| 96 | } |
| 97 | |
| 98 | status_t MediaRecorderClient::setAudioSource(int as) |
| 99 | { |
| 100 | LOGV("setAudioSource(%d)", as); |
Dave Sparks | 6690dc5 | 2009-05-20 19:20:31 -0700 | [diff] [blame] | 101 | if (!checkPermission(recordAudioPermission)) { |
| 102 | return PERMISSION_DENIED; |
| 103 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | Mutex::Autolock lock(mLock); |
| 105 | if (mRecorder == NULL) { |
| 106 | LOGE("recorder is not initialized"); |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 107 | return NO_INIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | } |
| 109 | return mRecorder->setAudioSource((audio_source)as); |
| 110 | } |
| 111 | |
| 112 | status_t MediaRecorderClient::setOutputFormat(int of) |
| 113 | { |
| 114 | LOGV("setOutputFormat(%d)", of); |
| 115 | Mutex::Autolock lock(mLock); |
| 116 | if (mRecorder == NULL) { |
| 117 | LOGE("recorder is not initialized"); |
| 118 | return NO_INIT; |
| 119 | } |
| 120 | return mRecorder->setOutputFormat((output_format)of); |
| 121 | } |
| 122 | |
| 123 | status_t MediaRecorderClient::setVideoEncoder(int ve) |
| 124 | { |
| 125 | LOGV("setVideoEncoder(%d)", ve); |
| 126 | Mutex::Autolock lock(mLock); |
| 127 | if (mRecorder == NULL) { |
| 128 | LOGE("recorder is not initialized"); |
| 129 | return NO_INIT; |
| 130 | } |
| 131 | return mRecorder->setVideoEncoder((video_encoder)ve); |
| 132 | } |
| 133 | |
| 134 | status_t MediaRecorderClient::setAudioEncoder(int ae) |
| 135 | { |
| 136 | LOGV("setAudioEncoder(%d)", ae); |
| 137 | Mutex::Autolock lock(mLock); |
| 138 | if (mRecorder == NULL) { |
| 139 | LOGE("recorder is not initialized"); |
| 140 | return NO_INIT; |
| 141 | } |
| 142 | return mRecorder->setAudioEncoder((audio_encoder)ae); |
| 143 | } |
| 144 | |
| 145 | status_t MediaRecorderClient::setOutputFile(const char* path) |
| 146 | { |
| 147 | LOGV("setOutputFile(%s)", path); |
| 148 | Mutex::Autolock lock(mLock); |
| 149 | if (mRecorder == NULL) { |
| 150 | LOGE("recorder is not initialized"); |
| 151 | return NO_INIT; |
| 152 | } |
| 153 | return mRecorder->setOutputFile(path); |
| 154 | } |
| 155 | |
| 156 | status_t MediaRecorderClient::setOutputFile(int fd, int64_t offset, int64_t length) |
| 157 | { |
| 158 | LOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length); |
| 159 | Mutex::Autolock lock(mLock); |
| 160 | if (mRecorder == NULL) { |
| 161 | LOGE("recorder is not initialized"); |
| 162 | return NO_INIT; |
| 163 | } |
| 164 | return mRecorder->setOutputFile(fd, offset, length); |
| 165 | } |
| 166 | |
Nipun Kwatra | b33a5ae | 2010-08-26 17:20:53 -0700 | [diff] [blame^] | 167 | status_t MediaRecorderClient::setOutputFileAuxiliary(int fd) |
| 168 | { |
| 169 | LOGV("setOutputFileAuxiliary(%d)", fd); |
| 170 | Mutex::Autolock lock(mLock); |
| 171 | if (mRecorder == NULL) { |
| 172 | LOGE("recorder is not initialized"); |
| 173 | return NO_INIT; |
| 174 | } |
| 175 | return mRecorder->setOutputFileAuxiliary(fd); |
| 176 | } |
| 177 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | status_t MediaRecorderClient::setVideoSize(int width, int height) |
| 179 | { |
| 180 | LOGV("setVideoSize(%dx%d)", width, height); |
| 181 | Mutex::Autolock lock(mLock); |
| 182 | if (mRecorder == NULL) { |
| 183 | LOGE("recorder is not initialized"); |
| 184 | return NO_INIT; |
| 185 | } |
| 186 | return mRecorder->setVideoSize(width, height); |
| 187 | } |
| 188 | |
| 189 | status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second) |
| 190 | { |
| 191 | LOGV("setVideoFrameRate(%d)", frames_per_second); |
| 192 | Mutex::Autolock lock(mLock); |
| 193 | if (mRecorder == NULL) { |
| 194 | LOGE("recorder is not initialized"); |
| 195 | return NO_INIT; |
| 196 | } |
| 197 | return mRecorder->setVideoFrameRate(frames_per_second); |
| 198 | } |
| 199 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 200 | status_t MediaRecorderClient::setParameters(const String8& params) { |
| 201 | LOGV("setParameters(%s)", params.string()); |
| 202 | Mutex::Autolock lock(mLock); |
| 203 | if (mRecorder == NULL) { |
| 204 | LOGE("recorder is not initialized"); |
| 205 | return NO_INIT; |
| 206 | } |
| 207 | return mRecorder->setParameters(params); |
| 208 | } |
| 209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | status_t MediaRecorderClient::prepare() |
| 211 | { |
| 212 | LOGV("prepare"); |
| 213 | Mutex::Autolock lock(mLock); |
| 214 | if (mRecorder == NULL) { |
| 215 | LOGE("recorder is not initialized"); |
| 216 | return NO_INIT; |
| 217 | } |
| 218 | return mRecorder->prepare(); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | status_t MediaRecorderClient::getMaxAmplitude(int* max) |
| 223 | { |
| 224 | LOGV("getMaxAmplitude"); |
| 225 | Mutex::Autolock lock(mLock); |
| 226 | if (mRecorder == NULL) { |
| 227 | LOGE("recorder is not initialized"); |
| 228 | return NO_INIT; |
| 229 | } |
| 230 | return mRecorder->getMaxAmplitude(max); |
| 231 | } |
| 232 | |
| 233 | status_t MediaRecorderClient::start() |
| 234 | { |
| 235 | LOGV("start"); |
| 236 | Mutex::Autolock lock(mLock); |
| 237 | if (mRecorder == NULL) { |
| 238 | LOGE("recorder is not initialized"); |
| 239 | return NO_INIT; |
| 240 | } |
| 241 | return mRecorder->start(); |
| 242 | |
| 243 | } |
| 244 | |
| 245 | status_t MediaRecorderClient::stop() |
| 246 | { |
| 247 | LOGV("stop"); |
| 248 | Mutex::Autolock lock(mLock); |
| 249 | if (mRecorder == NULL) { |
| 250 | LOGE("recorder is not initialized"); |
| 251 | return NO_INIT; |
| 252 | } |
| 253 | return mRecorder->stop(); |
| 254 | } |
| 255 | |
| 256 | status_t MediaRecorderClient::init() |
| 257 | { |
| 258 | LOGV("init"); |
| 259 | Mutex::Autolock lock(mLock); |
| 260 | if (mRecorder == NULL) { |
| 261 | LOGE("recorder is not initialized"); |
| 262 | return NO_INIT; |
| 263 | } |
| 264 | return mRecorder->init(); |
| 265 | } |
| 266 | |
| 267 | status_t MediaRecorderClient::close() |
| 268 | { |
| 269 | LOGV("close"); |
| 270 | Mutex::Autolock lock(mLock); |
| 271 | if (mRecorder == NULL) { |
| 272 | LOGE("recorder is not initialized"); |
| 273 | return NO_INIT; |
| 274 | } |
| 275 | return mRecorder->close(); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | status_t MediaRecorderClient::reset() |
| 280 | { |
| 281 | LOGV("reset"); |
| 282 | Mutex::Autolock lock(mLock); |
| 283 | if (mRecorder == NULL) { |
| 284 | LOGE("recorder is not initialized"); |
| 285 | return NO_INIT; |
| 286 | } |
| 287 | return mRecorder->reset(); |
| 288 | } |
| 289 | |
| 290 | status_t MediaRecorderClient::release() |
| 291 | { |
| 292 | LOGV("release"); |
| 293 | Mutex::Autolock lock(mLock); |
| 294 | if (mRecorder != NULL) { |
| 295 | delete mRecorder; |
| 296 | mRecorder = NULL; |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 297 | wp<MediaRecorderClient> client(this); |
| 298 | mMediaPlayerService->removeMediaRecorderClient(client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | } |
| 300 | return NO_ERROR; |
| 301 | } |
| 302 | |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 303 | MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | { |
| 305 | LOGV("Client constructor"); |
| 306 | mPid = pid; |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 307 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 308 | char value[PROPERTY_VALUE_MAX]; |
Andreas Huber | 30cfa20 | 2010-08-30 12:46:12 -0700 | [diff] [blame] | 309 | if (!property_get("media.stagefright.enable-record", value, NULL) |
| 310 | || !strcmp(value, "1") || !strcasecmp(value, "true")) { |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 311 | mRecorder = new StagefrightRecorder; |
| 312 | } else |
Andreas Huber | bfb9fb1 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 313 | #ifndef NO_OPENCORE |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 314 | { |
| 315 | mRecorder = new PVMediaRecorder(); |
| 316 | } |
Andreas Huber | bfb9fb1 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 317 | #else |
| 318 | { |
| 319 | mRecorder = NULL; |
| 320 | } |
| 321 | #endif |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 322 | |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 323 | mMediaPlayerService = service; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | MediaRecorderClient::~MediaRecorderClient() |
| 327 | { |
| 328 | LOGV("Client destructor"); |
| 329 | release(); |
| 330 | } |
| 331 | |
James Dong | fe1bafe | 2010-06-25 17:06:47 -0700 | [diff] [blame] | 332 | status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | { |
| 334 | LOGV("setListener"); |
| 335 | Mutex::Autolock lock(mLock); |
| 336 | if (mRecorder == NULL) { |
| 337 | LOGE("recorder is not initialized"); |
| 338 | return NO_INIT; |
| 339 | } |
| 340 | return mRecorder->setListener(listener); |
| 341 | } |
| 342 | |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 343 | status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) const { |
| 344 | if (mRecorder != NULL) { |
| 345 | return mRecorder->dump(fd, args); |
| 346 | } |
| 347 | return OK; |
| 348 | } |
| 349 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | }; // namespace android |