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 | |
Dave Sparks | 17612fc | 2009-03-27 19:56:11 -0700 | [diff] [blame] | 34 | #include <utils/String16.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 36 | #include <media/AudioTrack.h> |
| 37 | |
Dima Zavin | 34bb419 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 38 | #include <system/audio.h> |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 39 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | #include "MediaRecorderClient.h" |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 41 | #include "MediaPlayerService.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 43 | #include "StagefrightRecorder.h" |
| 44 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | namespace android { |
| 46 | |
Dave Sparks | 17612fc | 2009-03-27 19:56:11 -0700 | [diff] [blame] | 47 | const char* cameraPermission = "android.permission.CAMERA"; |
Dave Sparks | 6690dc5 | 2009-05-20 19:20:31 -0700 | [diff] [blame] | 48 | const char* recordAudioPermission = "android.permission.RECORD_AUDIO"; |
Dave Sparks | 17612fc | 2009-03-27 19:56:11 -0700 | [diff] [blame] | 49 | |
| 50 | static 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 | |
Wu-cheng Li | 42419ce | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 60 | status_t MediaRecorderClient::setCamera(const sp<ICamera>& camera, |
| 61 | const sp<ICameraRecordingProxy>& proxy) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | { |
| 63 | LOGV("setCamera"); |
| 64 | Mutex::Autolock lock(mLock); |
| 65 | if (mRecorder == NULL) { |
| 66 | LOGE("recorder is not initialized"); |
| 67 | return NO_INIT; |
| 68 | } |
Wu-cheng Li | 42419ce | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 69 | return mRecorder->setCamera(camera, proxy); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Jamie Gennis | 85cfdd0 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 72 | status_t MediaRecorderClient::setPreviewSurface(const sp<Surface>& surface) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 83 | status_t MediaRecorderClient::setVideoSource(int vs) |
| 84 | { |
| 85 | LOGV("setVideoSource(%d)", vs); |
Dave Sparks | 17612fc | 2009-03-27 19:56:11 -0700 | [diff] [blame] | 86 | if (!checkPermission(cameraPermission)) { |
| 87 | return PERMISSION_DENIED; |
| 88 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | Mutex::Autolock lock(mLock); |
| 90 | if (mRecorder == NULL) { |
| 91 | LOGE("recorder is not initialized"); |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 92 | return NO_INIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | } |
| 94 | return mRecorder->setVideoSource((video_source)vs); |
| 95 | } |
| 96 | |
| 97 | status_t MediaRecorderClient::setAudioSource(int as) |
| 98 | { |
| 99 | LOGV("setAudioSource(%d)", as); |
Dave Sparks | 6690dc5 | 2009-05-20 19:20:31 -0700 | [diff] [blame] | 100 | if (!checkPermission(recordAudioPermission)) { |
| 101 | return PERMISSION_DENIED; |
| 102 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | Mutex::Autolock lock(mLock); |
| 104 | if (mRecorder == NULL) { |
| 105 | LOGE("recorder is not initialized"); |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 106 | return NO_INIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 108 | return mRecorder->setAudioSource((audio_source_t)as); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | status_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 | |
| 122 | status_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 | |
| 133 | status_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 | |
| 144 | status_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 | |
| 155 | status_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 | |
Nipun Kwatra | b33a5ae | 2010-08-26 17:20:53 -0700 | [diff] [blame] | 166 | status_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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | status_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 | |
| 188 | status_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 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 199 | status_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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | status_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 | |
| 221 | status_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 | |
| 232 | status_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 | |
| 244 | status_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 | |
| 255 | status_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 | |
| 266 | status_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 | |
| 278 | status_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 | |
| 289 | status_t MediaRecorderClient::release() |
| 290 | { |
| 291 | LOGV("release"); |
| 292 | Mutex::Autolock lock(mLock); |
| 293 | if (mRecorder != NULL) { |
| 294 | delete mRecorder; |
| 295 | mRecorder = NULL; |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 296 | wp<MediaRecorderClient> client(this); |
| 297 | mMediaPlayerService->removeMediaRecorderClient(client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | } |
| 299 | return NO_ERROR; |
| 300 | } |
| 301 | |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 302 | MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | { |
| 304 | LOGV("Client constructor"); |
| 305 | mPid = pid; |
James Dong | 8ec2d9a | 2010-11-10 18:42:40 -0800 | [diff] [blame] | 306 | mRecorder = new StagefrightRecorder; |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 307 | mMediaPlayerService = service; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | MediaRecorderClient::~MediaRecorderClient() |
| 311 | { |
| 312 | LOGV("Client destructor"); |
| 313 | release(); |
| 314 | } |
| 315 | |
James Dong | fe1bafe | 2010-06-25 17:06:47 -0700 | [diff] [blame] | 316 | status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 327 | status_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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | }; // namespace android |