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