The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright (c) 2008 The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "MediaRecorder" |
| 20 | #include <utils/Log.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include <media/mediarecorder.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 22 | #include <binder/IServiceManager.h> |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 23 | #include <utils/String8.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <media/IMediaPlayerService.h> |
| 25 | #include <media/IMediaRecorder.h> |
James Dong | 34bbc22 | 2010-01-15 18:13:58 -0800 | [diff] [blame] | 26 | #include <media/mediaplayer.h> // for MEDIA_ERROR_SERVER_DIED |
Pannag Sanketi | 897e27b | 2011-07-01 17:39:39 -0700 | [diff] [blame] | 27 | #include <gui/ISurfaceTexture.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Wu-cheng Li | 42419ce | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 31 | status_t MediaRecorder::setCamera(const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 33 | ALOGV("setCamera(%p,%p)", camera.get(), proxy.get()); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 34 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 35 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | return INVALID_OPERATION; |
| 37 | } |
| 38 | if (!(mCurrentState & MEDIA_RECORDER_IDLE)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 39 | ALOGE("setCamera called in an invalid state(%d)", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | return INVALID_OPERATION; |
| 41 | } |
| 42 | |
Wu-cheng Li | 42419ce | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 43 | status_t ret = mMediaRecorder->setCamera(camera, proxy); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | if (OK != ret) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 45 | ALOGV("setCamera failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 47 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | } |
| 49 | return ret; |
| 50 | } |
| 51 | |
| 52 | status_t MediaRecorder::setPreviewSurface(const sp<Surface>& surface) |
| 53 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 54 | ALOGV("setPreviewSurface(%p)", surface.get()); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 55 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 56 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | return INVALID_OPERATION; |
| 58 | } |
| 59 | if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 60 | ALOGE("setPreviewSurface called in an invalid state(%d)", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | return INVALID_OPERATION; |
| 62 | } |
| 63 | if (!mIsVideoSourceSet) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 64 | ALOGE("try to set preview surface without setting the video source first"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | return INVALID_OPERATION; |
| 66 | } |
| 67 | |
Jamie Gennis | 85cfdd0 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 68 | status_t ret = mMediaRecorder->setPreviewSurface(surface); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | if (OK != ret) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 70 | ALOGV("setPreviewSurface failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 72 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | } |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | status_t MediaRecorder::init() |
| 78 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 79 | ALOGV("init"); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 80 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 81 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | return INVALID_OPERATION; |
| 83 | } |
| 84 | if (!(mCurrentState & MEDIA_RECORDER_IDLE)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 85 | ALOGE("init called in an invalid state(%d)", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | return INVALID_OPERATION; |
| 87 | } |
| 88 | |
| 89 | status_t ret = mMediaRecorder->init(); |
| 90 | if (OK != ret) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 91 | ALOGV("init failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 93 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | ret = mMediaRecorder->setListener(this); |
| 97 | if (OK != ret) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 98 | ALOGV("setListener failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 100 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | mCurrentState = MEDIA_RECORDER_INITIALIZED; |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | status_t MediaRecorder::setVideoSource(int vs) |
| 108 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 109 | ALOGV("setVideoSource(%d)", vs); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 110 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 111 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | return INVALID_OPERATION; |
| 113 | } |
| 114 | if (mIsVideoSourceSet) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 115 | ALOGE("video source has already been set"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | return INVALID_OPERATION; |
| 117 | } |
| 118 | if (mCurrentState & MEDIA_RECORDER_IDLE) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 119 | ALOGV("Call init() since the media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | status_t ret = init(); |
| 121 | if (OK != ret) { |
| 122 | return ret; |
| 123 | } |
| 124 | } |
| 125 | if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 126 | ALOGE("setVideoSource called in an invalid state(%d)", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | return INVALID_OPERATION; |
| 128 | } |
| 129 | |
Pannag Sanketi | 897e27b | 2011-07-01 17:39:39 -0700 | [diff] [blame] | 130 | // following call is made over the Binder Interface |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | status_t ret = mMediaRecorder->setVideoSource(vs); |
Pannag Sanketi | 897e27b | 2011-07-01 17:39:39 -0700 | [diff] [blame] | 132 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | if (OK != ret) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 134 | ALOGV("setVideoSource failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 136 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | } |
| 138 | mIsVideoSourceSet = true; |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | status_t MediaRecorder::setAudioSource(int as) |
| 143 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 144 | ALOGV("setAudioSource(%d)", as); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 145 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 146 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | return INVALID_OPERATION; |
| 148 | } |
| 149 | if (mCurrentState & MEDIA_RECORDER_IDLE) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 150 | ALOGV("Call init() since the media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | status_t ret = init(); |
| 152 | if (OK != ret) { |
| 153 | return ret; |
| 154 | } |
| 155 | } |
| 156 | if (mIsAudioSourceSet) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 157 | ALOGE("audio source has already been set"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | return INVALID_OPERATION; |
| 159 | } |
| 160 | if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 161 | ALOGE("setAudioSource called in an invalid state(%d)", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | return INVALID_OPERATION; |
| 163 | } |
| 164 | |
| 165 | status_t ret = mMediaRecorder->setAudioSource(as); |
| 166 | if (OK != ret) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 167 | ALOGV("setAudioSource failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 169 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | } |
| 171 | mIsAudioSourceSet = true; |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | status_t MediaRecorder::setOutputFormat(int of) |
| 176 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 177 | ALOGV("setOutputFormat(%d)", of); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 178 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 179 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | return INVALID_OPERATION; |
| 181 | } |
| 182 | if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 183 | ALOGE("setOutputFormat called in an invalid state: %d", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | return INVALID_OPERATION; |
| 185 | } |
Andreas Huber | 9adf466 | 2010-10-12 14:17:45 -0700 | [diff] [blame] | 186 | if (mIsVideoSourceSet && of >= OUTPUT_FORMAT_AUDIO_ONLY_START && of != OUTPUT_FORMAT_RTP_AVP && of != OUTPUT_FORMAT_MPEG2TS) { //first non-video output format |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 187 | ALOGE("output format (%d) is meant for audio recording only and incompatible with video recording", of); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | return INVALID_OPERATION; |
| 189 | } |
| 190 | |
| 191 | status_t ret = mMediaRecorder->setOutputFormat(of); |
| 192 | if (OK != ret) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 193 | ALOGE("setOutputFormat failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 195 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | } |
| 197 | mCurrentState = MEDIA_RECORDER_DATASOURCE_CONFIGURED; |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | status_t MediaRecorder::setVideoEncoder(int ve) |
| 202 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 203 | ALOGV("setVideoEncoder(%d)", ve); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 204 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 205 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | return INVALID_OPERATION; |
| 207 | } |
| 208 | if (!mIsVideoSourceSet) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 209 | ALOGE("try to set the video encoder without setting the video source first"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | return INVALID_OPERATION; |
| 211 | } |
| 212 | if (mIsVideoEncoderSet) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 213 | ALOGE("video encoder has already been set"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | return INVALID_OPERATION; |
| 215 | } |
| 216 | if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 217 | ALOGE("setVideoEncoder called in an invalid state(%d)", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | return INVALID_OPERATION; |
| 219 | } |
| 220 | |
| 221 | status_t ret = mMediaRecorder->setVideoEncoder(ve); |
| 222 | if (OK != ret) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 223 | ALOGV("setVideoEncoder failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 225 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | } |
| 227 | mIsVideoEncoderSet = true; |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | status_t MediaRecorder::setAudioEncoder(int ae) |
| 232 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 233 | ALOGV("setAudioEncoder(%d)", ae); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 234 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 235 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | return INVALID_OPERATION; |
| 237 | } |
| 238 | if (!mIsAudioSourceSet) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 239 | ALOGE("try to set the audio encoder without setting the audio source first"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | return INVALID_OPERATION; |
| 241 | } |
| 242 | if (mIsAudioEncoderSet) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 243 | ALOGE("audio encoder has already been set"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | return INVALID_OPERATION; |
| 245 | } |
| 246 | if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 247 | ALOGE("setAudioEncoder called in an invalid state(%d)", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | return INVALID_OPERATION; |
| 249 | } |
| 250 | |
| 251 | status_t ret = mMediaRecorder->setAudioEncoder(ae); |
| 252 | if (OK != ret) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 253 | ALOGV("setAudioEncoder failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 255 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | } |
| 257 | mIsAudioEncoderSet = true; |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | status_t MediaRecorder::setOutputFile(const char* path) |
| 262 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 263 | ALOGV("setOutputFile(%s)", path); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 264 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 265 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | return INVALID_OPERATION; |
| 267 | } |
| 268 | if (mIsOutputFileSet) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 269 | ALOGE("output file has already been set"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | return INVALID_OPERATION; |
| 271 | } |
| 272 | if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 273 | ALOGE("setOutputFile called in an invalid state(%d)", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | return INVALID_OPERATION; |
| 275 | } |
| 276 | |
| 277 | status_t ret = mMediaRecorder->setOutputFile(path); |
| 278 | if (OK != ret) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 279 | ALOGV("setOutputFile failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 281 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | } |
| 283 | mIsOutputFileSet = true; |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | status_t MediaRecorder::setOutputFile(int fd, int64_t offset, int64_t length) |
| 288 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 289 | ALOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 290 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 291 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | return INVALID_OPERATION; |
| 293 | } |
| 294 | if (mIsOutputFileSet) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 295 | ALOGE("output file has already been set"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | return INVALID_OPERATION; |
| 297 | } |
| 298 | if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 299 | ALOGE("setOutputFile called in an invalid state(%d)", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | return INVALID_OPERATION; |
| 301 | } |
| 302 | |
James Dong | bafa7ea | 2011-02-22 12:34:02 -0800 | [diff] [blame] | 303 | // It appears that if an invalid file descriptor is passed through |
| 304 | // binder calls, the server-side of the inter-process function call |
| 305 | // is skipped. As a result, the check at the server-side to catch |
| 306 | // the invalid file descritpor never gets invoked. This is to workaround |
| 307 | // this issue by checking the file descriptor first before passing |
| 308 | // it through binder call. |
| 309 | if (fd < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 310 | ALOGE("Invalid file descriptor: %d", fd); |
James Dong | bafa7ea | 2011-02-22 12:34:02 -0800 | [diff] [blame] | 311 | return BAD_VALUE; |
| 312 | } |
| 313 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | status_t ret = mMediaRecorder->setOutputFile(fd, offset, length); |
| 315 | if (OK != ret) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 316 | ALOGV("setOutputFile failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 318 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | } |
| 320 | mIsOutputFileSet = true; |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | status_t MediaRecorder::setVideoSize(int width, int height) |
| 325 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 326 | ALOGV("setVideoSize(%d, %d)", width, height); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 327 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 328 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | return INVALID_OPERATION; |
| 330 | } |
| 331 | if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 332 | ALOGE("setVideoSize called in an invalid state: %d", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | return INVALID_OPERATION; |
| 334 | } |
| 335 | if (!mIsVideoSourceSet) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 336 | ALOGE("Cannot set video size without setting video source first"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | return INVALID_OPERATION; |
| 338 | } |
| 339 | |
| 340 | status_t ret = mMediaRecorder->setVideoSize(width, height); |
| 341 | if (OK != ret) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 342 | ALOGE("setVideoSize failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 344 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | } |
Pannag Sanketi | 897e27b | 2011-07-01 17:39:39 -0700 | [diff] [blame] | 346 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | return ret; |
| 348 | } |
| 349 | |
Pannag Sanketi | 897e27b | 2011-07-01 17:39:39 -0700 | [diff] [blame] | 350 | // Query a SurfaceMediaSurface through the Mediaserver, over the |
| 351 | // binder interface. This is used by the Filter Framework (MeidaEncoder) |
| 352 | // to get an <ISurfaceTexture> object to hook up to ANativeWindow. |
| 353 | sp<ISurfaceTexture> MediaRecorder:: |
| 354 | querySurfaceMediaSourceFromMediaServer() |
| 355 | { |
| 356 | Mutex::Autolock _l(mLock); |
| 357 | mSurfaceMediaSource = |
| 358 | mMediaRecorder->querySurfaceMediaSource(); |
| 359 | if (mSurfaceMediaSource == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 360 | ALOGE("SurfaceMediaSource could not be initialized!"); |
Pannag Sanketi | 897e27b | 2011-07-01 17:39:39 -0700 | [diff] [blame] | 361 | } |
| 362 | return mSurfaceMediaSource; |
| 363 | } |
| 364 | |
| 365 | |
| 366 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | status_t MediaRecorder::setVideoFrameRate(int frames_per_second) |
| 368 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 369 | ALOGV("setVideoFrameRate(%d)", frames_per_second); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 370 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 371 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | return INVALID_OPERATION; |
| 373 | } |
| 374 | if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 375 | ALOGE("setVideoFrameRate called in an invalid state: %d", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | return INVALID_OPERATION; |
| 377 | } |
| 378 | if (!mIsVideoSourceSet) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 379 | ALOGE("Cannot set video frame rate without setting video source first"); |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 380 | return INVALID_OPERATION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | status_t ret = mMediaRecorder->setVideoFrameRate(frames_per_second); |
| 384 | if (OK != ret) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 385 | ALOGE("setVideoFrameRate failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 387 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | } |
| 389 | return ret; |
| 390 | } |
| 391 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 392 | status_t MediaRecorder::setParameters(const String8& params) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 393 | ALOGV("setParameters(%s)", params.string()); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 394 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 395 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 396 | return INVALID_OPERATION; |
| 397 | } |
| 398 | |
James Dong | 61701ce | 2010-02-25 18:41:46 -0800 | [diff] [blame] | 399 | bool isInvalidState = (mCurrentState & |
| 400 | (MEDIA_RECORDER_PREPARED | |
| 401 | MEDIA_RECORDER_RECORDING | |
| 402 | MEDIA_RECORDER_ERROR)); |
| 403 | if (isInvalidState) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 404 | ALOGE("setParameters is called in an invalid state: %d", mCurrentState); |
James Dong | 61701ce | 2010-02-25 18:41:46 -0800 | [diff] [blame] | 405 | return INVALID_OPERATION; |
| 406 | } |
| 407 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 408 | status_t ret = mMediaRecorder->setParameters(params); |
| 409 | if (OK != ret) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 410 | ALOGE("setParameters(%s) failed: %d", params.string(), ret); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 411 | // Do not change our current state to MEDIA_RECORDER_ERROR, failures |
| 412 | // of the only currently supported parameters, "max-duration" and |
| 413 | // "max-filesize" are _not_ fatal. |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | return ret; |
| 417 | } |
| 418 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | status_t MediaRecorder::prepare() |
| 420 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 421 | ALOGV("prepare"); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 422 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 423 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | return INVALID_OPERATION; |
| 425 | } |
| 426 | if (!(mCurrentState & MEDIA_RECORDER_DATASOURCE_CONFIGURED)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 427 | ALOGE("prepare called in an invalid state: %d", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | return INVALID_OPERATION; |
| 429 | } |
| 430 | if (mIsAudioSourceSet != mIsAudioEncoderSet) { |
| 431 | if (mIsAudioSourceSet) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 432 | ALOGE("audio source is set, but audio encoder is not set"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | } else { // must not happen, since setAudioEncoder checks this already |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 434 | ALOGE("audio encoder is set, but audio source is not set"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | } |
| 436 | return INVALID_OPERATION; |
| 437 | } |
| 438 | |
| 439 | if (mIsVideoSourceSet != mIsVideoEncoderSet) { |
| 440 | if (mIsVideoSourceSet) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 441 | ALOGE("video source is set, but video encoder is not set"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | } else { // must not happen, since setVideoEncoder checks this already |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 443 | ALOGE("video encoder is set, but video source is not set"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | } |
| 445 | return INVALID_OPERATION; |
| 446 | } |
| 447 | |
| 448 | status_t ret = mMediaRecorder->prepare(); |
| 449 | if (OK != ret) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 450 | ALOGE("prepare failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 452 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | } |
| 454 | mCurrentState = MEDIA_RECORDER_PREPARED; |
| 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | status_t MediaRecorder::getMaxAmplitude(int* max) |
| 459 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 460 | ALOGV("getMaxAmplitude"); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 461 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 462 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | return INVALID_OPERATION; |
| 464 | } |
| 465 | if (mCurrentState & MEDIA_RECORDER_ERROR) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 466 | ALOGE("getMaxAmplitude called in an invalid state: %d", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | return INVALID_OPERATION; |
| 468 | } |
| 469 | |
| 470 | status_t ret = mMediaRecorder->getMaxAmplitude(max); |
| 471 | if (OK != ret) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 472 | ALOGE("getMaxAmplitude failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 474 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | } |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | status_t MediaRecorder::start() |
| 480 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 481 | ALOGV("start"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 483 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | return INVALID_OPERATION; |
| 485 | } |
| 486 | if (!(mCurrentState & MEDIA_RECORDER_PREPARED)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 487 | ALOGE("start called in an invalid state: %d", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | return INVALID_OPERATION; |
| 489 | } |
| 490 | |
| 491 | status_t ret = mMediaRecorder->start(); |
| 492 | if (OK != ret) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 493 | ALOGE("start failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 495 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | } |
| 497 | mCurrentState = MEDIA_RECORDER_RECORDING; |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | status_t MediaRecorder::stop() |
| 502 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 503 | ALOGV("stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 505 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | return INVALID_OPERATION; |
| 507 | } |
| 508 | if (!(mCurrentState & MEDIA_RECORDER_RECORDING)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 509 | ALOGE("stop called in an invalid state: %d", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | return INVALID_OPERATION; |
| 511 | } |
| 512 | |
| 513 | status_t ret = mMediaRecorder->stop(); |
| 514 | if (OK != ret) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 515 | ALOGE("stop failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 517 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | } |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 519 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 520 | // FIXME: |
| 521 | // stop and reset are semantically different. |
| 522 | // We treat them the same for now, and will change this in the future. |
| 523 | doCleanUp(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | mCurrentState = MEDIA_RECORDER_IDLE; |
| 525 | return ret; |
| 526 | } |
| 527 | |
| 528 | // Reset should be OK in any state |
| 529 | status_t MediaRecorder::reset() |
| 530 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 531 | ALOGV("reset"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | if (mMediaRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 533 | ALOGE("media recorder is not initialized yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | return INVALID_OPERATION; |
| 535 | } |
Jianhong Jiang | 2bcda90 | 2009-06-08 08:50:42 -0700 | [diff] [blame] | 536 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | doCleanUp(); |
| 538 | status_t ret = UNKNOWN_ERROR; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 539 | switch (mCurrentState) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | case MEDIA_RECORDER_IDLE: |
| 541 | ret = OK; |
| 542 | break; |
| 543 | |
| 544 | case MEDIA_RECORDER_RECORDING: |
| 545 | case MEDIA_RECORDER_DATASOURCE_CONFIGURED: |
| 546 | case MEDIA_RECORDER_PREPARED: |
| 547 | case MEDIA_RECORDER_ERROR: { |
| 548 | ret = doReset(); |
| 549 | if (OK != ret) { |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame^] | 550 | return ret; // No need to continue |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | } |
| 552 | } // Intentional fall through |
| 553 | case MEDIA_RECORDER_INITIALIZED: |
| 554 | ret = close(); |
| 555 | break; |
| 556 | |
| 557 | default: { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 558 | ALOGE("Unexpected non-existing state: %d", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | break; |
| 560 | } |
| 561 | } |
| 562 | return ret; |
| 563 | } |
| 564 | |
| 565 | status_t MediaRecorder::close() |
| 566 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 567 | ALOGV("close"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | if (!(mCurrentState & MEDIA_RECORDER_INITIALIZED)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 569 | ALOGE("close called in an invalid state: %d", mCurrentState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | return INVALID_OPERATION; |
| 571 | } |
| 572 | status_t ret = mMediaRecorder->close(); |
| 573 | if (OK != ret) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 574 | ALOGE("close failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | mCurrentState = MEDIA_RECORDER_ERROR; |
| 576 | return UNKNOWN_ERROR; |
| 577 | } else { |
| 578 | mCurrentState = MEDIA_RECORDER_IDLE; |
| 579 | } |
| 580 | return ret; |
| 581 | } |
| 582 | |
| 583 | status_t MediaRecorder::doReset() |
| 584 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 585 | ALOGV("doReset"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | status_t ret = mMediaRecorder->reset(); |
| 587 | if (OK != ret) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 588 | ALOGE("doReset failed: %d", ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | mCurrentState = MEDIA_RECORDER_ERROR; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 590 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | } else { |
| 592 | mCurrentState = MEDIA_RECORDER_INITIALIZED; |
| 593 | } |
| 594 | return ret; |
| 595 | } |
| 596 | |
| 597 | void MediaRecorder::doCleanUp() |
| 598 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 599 | ALOGV("doCleanUp"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | mIsAudioSourceSet = false; |
| 601 | mIsVideoSourceSet = false; |
| 602 | mIsAudioEncoderSet = false; |
| 603 | mIsVideoEncoderSet = false; |
| 604 | mIsOutputFileSet = false; |
| 605 | } |
| 606 | |
| 607 | // Release should be OK in any state |
| 608 | status_t MediaRecorder::release() |
| 609 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 610 | ALOGV("release"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | if (mMediaRecorder != NULL) { |
| 612 | return mMediaRecorder->release(); |
| 613 | } |
| 614 | return INVALID_OPERATION; |
| 615 | } |
| 616 | |
Pannag Sanketi | 897e27b | 2011-07-01 17:39:39 -0700 | [diff] [blame] | 617 | MediaRecorder::MediaRecorder() : mSurfaceMediaSource(NULL) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 619 | ALOGV("constructor"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 620 | |
James Dong | 34bbc22 | 2010-01-15 18:13:58 -0800 | [diff] [blame] | 621 | const sp<IMediaPlayerService>& service(getMediaPlayerService()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | if (service != NULL) { |
| 623 | mMediaRecorder = service->createMediaRecorder(getpid()); |
| 624 | } |
| 625 | if (mMediaRecorder != NULL) { |
| 626 | mCurrentState = MEDIA_RECORDER_IDLE; |
| 627 | } |
Pannag Sanketi | 897e27b | 2011-07-01 17:39:39 -0700 | [diff] [blame] | 628 | |
| 629 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | doCleanUp(); |
| 631 | } |
| 632 | |
| 633 | status_t MediaRecorder::initCheck() |
| 634 | { |
| 635 | return mMediaRecorder != 0 ? NO_ERROR : NO_INIT; |
| 636 | } |
| 637 | |
| 638 | MediaRecorder::~MediaRecorder() |
| 639 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 640 | ALOGV("destructor"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | if (mMediaRecorder != NULL) { |
| 642 | mMediaRecorder.clear(); |
| 643 | } |
Pannag Sanketi | 897e27b | 2011-07-01 17:39:39 -0700 | [diff] [blame] | 644 | |
| 645 | if (mSurfaceMediaSource != NULL) { |
| 646 | mSurfaceMediaSource.clear(); |
| 647 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | status_t MediaRecorder::setListener(const sp<MediaRecorderListener>& listener) |
| 651 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 652 | ALOGV("setListener"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 653 | Mutex::Autolock _l(mLock); |
| 654 | mListener = listener; |
| 655 | |
| 656 | return NO_ERROR; |
| 657 | } |
| 658 | |
| 659 | void MediaRecorder::notify(int msg, int ext1, int ext2) |
| 660 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 661 | ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 662 | |
| 663 | sp<MediaRecorderListener> listener; |
| 664 | mLock.lock(); |
| 665 | listener = mListener; |
| 666 | mLock.unlock(); |
| 667 | |
| 668 | if (listener != NULL) { |
| 669 | Mutex::Autolock _l(mNotifyLock); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 670 | ALOGV("callback application"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 671 | listener->notify(msg, ext1, ext2); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 672 | ALOGV("back from callback"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
James Dong | 34bbc22 | 2010-01-15 18:13:58 -0800 | [diff] [blame] | 676 | void MediaRecorder::died() |
| 677 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 678 | ALOGV("died"); |
James Dong | 34bbc22 | 2010-01-15 18:13:58 -0800 | [diff] [blame] | 679 | notify(MEDIA_RECORDER_EVENT_ERROR, MEDIA_ERROR_SERVER_DIED, 0); |
| 680 | } |
| 681 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | }; // namespace android |