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