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 | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 38 | #include <hardware/audio.h> |
| 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | status_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 | |
Jamie Gennis | 85cfdd0 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 71 | status_t MediaRecorderClient::setPreviewSurface(const sp<Surface>& surface) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 82 | status_t MediaRecorderClient::setVideoSource(int vs) |
| 83 | { |
| 84 | LOGV("setVideoSource(%d)", vs); |
Dave Sparks | 17612fc | 2009-03-27 19:56:11 -0700 | [diff] [blame] | 85 | if (!checkPermission(cameraPermission)) { |
| 86 | return PERMISSION_DENIED; |
| 87 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | Mutex::Autolock lock(mLock); |
| 89 | if (mRecorder == NULL) { |
| 90 | LOGE("recorder is not initialized"); |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 91 | return NO_INIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | } |
| 93 | return mRecorder->setVideoSource((video_source)vs); |
| 94 | } |
| 95 | |
| 96 | status_t MediaRecorderClient::setAudioSource(int as) |
| 97 | { |
| 98 | LOGV("setAudioSource(%d)", as); |
Dave Sparks | 6690dc5 | 2009-05-20 19:20:31 -0700 | [diff] [blame] | 99 | if (!checkPermission(recordAudioPermission)) { |
| 100 | return PERMISSION_DENIED; |
| 101 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | Mutex::Autolock lock(mLock); |
| 103 | if (mRecorder == NULL) { |
| 104 | LOGE("recorder is not initialized"); |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 105 | return NO_INIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 107 | return mRecorder->setAudioSource((audio_source_t)as); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | status_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 | |
| 121 | status_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 | |
| 132 | status_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 | |
| 143 | status_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 | |
| 154 | status_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 | |
Nipun Kwatra | b33a5ae | 2010-08-26 17:20:53 -0700 | [diff] [blame] | 165 | status_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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | status_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 | |
| 187 | status_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 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 198 | status_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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | status_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 | |
| 220 | status_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 | |
| 231 | status_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 | |
| 243 | status_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 | |
| 254 | status_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 | |
| 265 | status_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 | |
| 277 | status_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 | |
| 288 | status_t MediaRecorderClient::release() |
| 289 | { |
| 290 | LOGV("release"); |
| 291 | Mutex::Autolock lock(mLock); |
| 292 | if (mRecorder != NULL) { |
| 293 | delete mRecorder; |
| 294 | mRecorder = NULL; |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 295 | wp<MediaRecorderClient> client(this); |
| 296 | mMediaPlayerService->removeMediaRecorderClient(client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | } |
| 298 | return NO_ERROR; |
| 299 | } |
| 300 | |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 301 | MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | { |
| 303 | LOGV("Client constructor"); |
| 304 | mPid = pid; |
James Dong | 8ec2d9a | 2010-11-10 18:42:40 -0800 | [diff] [blame] | 305 | mRecorder = new StagefrightRecorder; |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 306 | mMediaPlayerService = service; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | MediaRecorderClient::~MediaRecorderClient() |
| 310 | { |
| 311 | LOGV("Client destructor"); |
| 312 | release(); |
| 313 | } |
| 314 | |
James Dong | fe1bafe | 2010-06-25 17:06:47 -0700 | [diff] [blame] | 315 | status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 326 | status_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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | }; // namespace android |