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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | #include "MediaRecorderClient.h" |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 39 | #include "MediaPlayerService.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 41 | #include "StagefrightRecorder.h" |
| 42 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | namespace android { |
| 44 | |
Dave Sparks | 17612fc | 2009-03-27 19:56:11 -0700 | [diff] [blame] | 45 | const char* cameraPermission = "android.permission.CAMERA"; |
Dave Sparks | 6690dc5 | 2009-05-20 19:20:31 -0700 | [diff] [blame] | 46 | const char* recordAudioPermission = "android.permission.RECORD_AUDIO"; |
Dave Sparks | 17612fc | 2009-03-27 19:56:11 -0700 | [diff] [blame] | 47 | |
| 48 | static 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | status_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 | |
Jamie Gennis | 85cfdd0 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 69 | status_t MediaRecorderClient::setPreviewSurface(const sp<Surface>& surface) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 80 | status_t MediaRecorderClient::setVideoSource(int vs) |
| 81 | { |
| 82 | LOGV("setVideoSource(%d)", vs); |
Dave Sparks | 17612fc | 2009-03-27 19:56:11 -0700 | [diff] [blame] | 83 | if (!checkPermission(cameraPermission)) { |
| 84 | return PERMISSION_DENIED; |
| 85 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | Mutex::Autolock lock(mLock); |
| 87 | if (mRecorder == NULL) { |
| 88 | LOGE("recorder is not initialized"); |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 89 | return NO_INIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | } |
| 91 | return mRecorder->setVideoSource((video_source)vs); |
| 92 | } |
| 93 | |
| 94 | status_t MediaRecorderClient::setAudioSource(int as) |
| 95 | { |
| 96 | LOGV("setAudioSource(%d)", as); |
Dave Sparks | 6690dc5 | 2009-05-20 19:20:31 -0700 | [diff] [blame] | 97 | if (!checkPermission(recordAudioPermission)) { |
| 98 | return PERMISSION_DENIED; |
| 99 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | Mutex::Autolock lock(mLock); |
| 101 | if (mRecorder == NULL) { |
| 102 | LOGE("recorder is not initialized"); |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 103 | return NO_INIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | } |
| 105 | return mRecorder->setAudioSource((audio_source)as); |
| 106 | } |
| 107 | |
| 108 | status_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 | |
| 119 | status_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 | |
| 130 | status_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 | |
| 141 | status_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 | |
| 152 | status_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 | |
Nipun Kwatra | b33a5ae | 2010-08-26 17:20:53 -0700 | [diff] [blame] | 163 | status_t MediaRecorderClient::setOutputFileAuxiliary(int fd) |
| 164 | { |
| 165 | LOGV("setOutputFileAuxiliary(%d)", fd); |
| 166 | Mutex::Autolock lock(mLock); |
| 167 | if (mRecorder == NULL) { |
| 168 | LOGE("recorder is not initialized"); |
| 169 | return NO_INIT; |
| 170 | } |
| 171 | return mRecorder->setOutputFileAuxiliary(fd); |
| 172 | } |
| 173 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | status_t MediaRecorderClient::setVideoSize(int width, int height) |
| 175 | { |
| 176 | LOGV("setVideoSize(%dx%d)", width, height); |
| 177 | Mutex::Autolock lock(mLock); |
| 178 | if (mRecorder == NULL) { |
| 179 | LOGE("recorder is not initialized"); |
| 180 | return NO_INIT; |
| 181 | } |
| 182 | return mRecorder->setVideoSize(width, height); |
| 183 | } |
| 184 | |
| 185 | status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second) |
| 186 | { |
| 187 | LOGV("setVideoFrameRate(%d)", frames_per_second); |
| 188 | Mutex::Autolock lock(mLock); |
| 189 | if (mRecorder == NULL) { |
| 190 | LOGE("recorder is not initialized"); |
| 191 | return NO_INIT; |
| 192 | } |
| 193 | return mRecorder->setVideoFrameRate(frames_per_second); |
| 194 | } |
| 195 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 196 | status_t MediaRecorderClient::setParameters(const String8& params) { |
| 197 | LOGV("setParameters(%s)", params.string()); |
| 198 | Mutex::Autolock lock(mLock); |
| 199 | if (mRecorder == NULL) { |
| 200 | LOGE("recorder is not initialized"); |
| 201 | return NO_INIT; |
| 202 | } |
| 203 | return mRecorder->setParameters(params); |
| 204 | } |
| 205 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | status_t MediaRecorderClient::prepare() |
| 207 | { |
| 208 | LOGV("prepare"); |
| 209 | Mutex::Autolock lock(mLock); |
| 210 | if (mRecorder == NULL) { |
| 211 | LOGE("recorder is not initialized"); |
| 212 | return NO_INIT; |
| 213 | } |
| 214 | return mRecorder->prepare(); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | status_t MediaRecorderClient::getMaxAmplitude(int* max) |
| 219 | { |
| 220 | LOGV("getMaxAmplitude"); |
| 221 | Mutex::Autolock lock(mLock); |
| 222 | if (mRecorder == NULL) { |
| 223 | LOGE("recorder is not initialized"); |
| 224 | return NO_INIT; |
| 225 | } |
| 226 | return mRecorder->getMaxAmplitude(max); |
| 227 | } |
| 228 | |
| 229 | status_t MediaRecorderClient::start() |
| 230 | { |
| 231 | LOGV("start"); |
| 232 | Mutex::Autolock lock(mLock); |
| 233 | if (mRecorder == NULL) { |
| 234 | LOGE("recorder is not initialized"); |
| 235 | return NO_INIT; |
| 236 | } |
| 237 | return mRecorder->start(); |
| 238 | |
| 239 | } |
| 240 | |
| 241 | status_t MediaRecorderClient::stop() |
| 242 | { |
| 243 | LOGV("stop"); |
| 244 | Mutex::Autolock lock(mLock); |
| 245 | if (mRecorder == NULL) { |
| 246 | LOGE("recorder is not initialized"); |
| 247 | return NO_INIT; |
| 248 | } |
| 249 | return mRecorder->stop(); |
| 250 | } |
| 251 | |
| 252 | status_t MediaRecorderClient::init() |
| 253 | { |
| 254 | LOGV("init"); |
| 255 | Mutex::Autolock lock(mLock); |
| 256 | if (mRecorder == NULL) { |
| 257 | LOGE("recorder is not initialized"); |
| 258 | return NO_INIT; |
| 259 | } |
| 260 | return mRecorder->init(); |
| 261 | } |
| 262 | |
| 263 | status_t MediaRecorderClient::close() |
| 264 | { |
| 265 | LOGV("close"); |
| 266 | Mutex::Autolock lock(mLock); |
| 267 | if (mRecorder == NULL) { |
| 268 | LOGE("recorder is not initialized"); |
| 269 | return NO_INIT; |
| 270 | } |
| 271 | return mRecorder->close(); |
| 272 | } |
| 273 | |
| 274 | |
| 275 | status_t MediaRecorderClient::reset() |
| 276 | { |
| 277 | LOGV("reset"); |
| 278 | Mutex::Autolock lock(mLock); |
| 279 | if (mRecorder == NULL) { |
| 280 | LOGE("recorder is not initialized"); |
| 281 | return NO_INIT; |
| 282 | } |
| 283 | return mRecorder->reset(); |
| 284 | } |
| 285 | |
| 286 | status_t MediaRecorderClient::release() |
| 287 | { |
| 288 | LOGV("release"); |
| 289 | Mutex::Autolock lock(mLock); |
| 290 | if (mRecorder != NULL) { |
| 291 | delete mRecorder; |
| 292 | mRecorder = NULL; |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 293 | wp<MediaRecorderClient> client(this); |
| 294 | mMediaPlayerService->removeMediaRecorderClient(client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | } |
| 296 | return NO_ERROR; |
| 297 | } |
| 298 | |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 299 | MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | { |
| 301 | LOGV("Client constructor"); |
| 302 | mPid = pid; |
James Dong | 8ec2d9a | 2010-11-10 18:42:40 -0800 | [diff] [blame] | 303 | mRecorder = new StagefrightRecorder; |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 304 | mMediaPlayerService = service; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | MediaRecorderClient::~MediaRecorderClient() |
| 308 | { |
| 309 | LOGV("Client destructor"); |
| 310 | release(); |
| 311 | } |
| 312 | |
James Dong | fe1bafe | 2010-06-25 17:06:47 -0700 | [diff] [blame] | 313 | status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 324 | status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) const { |
| 325 | if (mRecorder != NULL) { |
| 326 | return mRecorder->dump(fd, args); |
| 327 | } |
| 328 | return OK; |
| 329 | } |
| 330 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | }; // namespace android |