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