Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "StagefrightRecorder" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include "StagefrightRecorder.h" |
| 22 | |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 23 | #include <binder/IPCThreadState.h> |
| 24 | #include <binder/IServiceManager.h> |
| 25 | |
| 26 | #include <media/IMediaPlayerService.h> |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 27 | #include <media/stagefright/AudioSource.h> |
| 28 | #include <media/stagefright/AMRWriter.h> |
James Dong | f84bfab | 2011-03-21 14:29:38 -0700 | [diff] [blame] | 29 | #include <media/stagefright/AACWriter.h> |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 30 | #include <media/stagefright/CameraSource.h> |
Nipun Kwatra | 239f2e5 | 2010-08-31 15:35:44 -0700 | [diff] [blame] | 31 | #include <media/stagefright/VideoSourceDownSampler.h> |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 32 | #include <media/stagefright/CameraSourceTimeLapse.h> |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 33 | #include <media/stagefright/MediaSourceSplitter.h> |
Andreas Huber | 9adf466 | 2010-10-12 14:17:45 -0700 | [diff] [blame] | 34 | #include <media/stagefright/MPEG2TSWriter.h> |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 35 | #include <media/stagefright/MPEG4Writer.h> |
| 36 | #include <media/stagefright/MediaDebug.h> |
| 37 | #include <media/stagefright/MediaDefs.h> |
| 38 | #include <media/stagefright/MetaData.h> |
| 39 | #include <media/stagefright/OMXClient.h> |
| 40 | #include <media/stagefright/OMXCodec.h> |
James Dong | 42a18c0 | 2010-06-16 17:27:46 -0700 | [diff] [blame] | 41 | #include <media/MediaProfiles.h> |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 42 | #include <camera/ICamera.h> |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 43 | #include <camera/CameraParameters.h> |
Jamie Gennis | 85cfdd0 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 44 | #include <surfaceflinger/Surface.h> |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 45 | #include <utils/Errors.h> |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 46 | #include <sys/types.h> |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 47 | #include <ctype.h> |
Nipun Kwatra | 453e92f | 2010-08-04 14:26:45 -0700 | [diff] [blame] | 48 | #include <unistd.h> |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 49 | |
Dima Zavin | 34bb419 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 50 | #include <system/audio.h> |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 51 | |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 52 | #include "ARTPWriter.h" |
| 53 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 54 | namespace android { |
| 55 | |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 56 | // To collect the encoder usage for the battery app |
| 57 | static void addBatteryData(uint32_t params) { |
| 58 | sp<IBinder> binder = |
| 59 | defaultServiceManager()->getService(String16("media.player")); |
| 60 | sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder); |
| 61 | CHECK(service.get() != NULL); |
| 62 | |
| 63 | service->addBatteryData(params); |
| 64 | } |
| 65 | |
| 66 | |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 67 | StagefrightRecorder::StagefrightRecorder() |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 68 | : mWriter(NULL), mWriterAux(NULL), |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 69 | mOutputFd(-1), mOutputFdAux(-1), |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 70 | mAudioSource(AUDIO_SOURCE_CNT), |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 71 | mVideoSource(VIDEO_SOURCE_LIST_END), |
| 72 | mStarted(false) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 73 | |
| 74 | LOGV("Constructor"); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 75 | reset(); |
| 76 | } |
| 77 | |
| 78 | StagefrightRecorder::~StagefrightRecorder() { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 79 | LOGV("Destructor"); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 80 | stop(); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | status_t StagefrightRecorder::init() { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 84 | LOGV("init"); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 85 | return OK; |
| 86 | } |
| 87 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 88 | status_t StagefrightRecorder::setAudioSource(audio_source_t as) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 89 | LOGV("setAudioSource: %d", as); |
| 90 | if (as < AUDIO_SOURCE_DEFAULT || |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 91 | as >= AUDIO_SOURCE_CNT) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 92 | LOGE("Invalid audio source: %d", as); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 93 | return BAD_VALUE; |
| 94 | } |
| 95 | |
| 96 | if (as == AUDIO_SOURCE_DEFAULT) { |
| 97 | mAudioSource = AUDIO_SOURCE_MIC; |
| 98 | } else { |
| 99 | mAudioSource = as; |
| 100 | } |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 101 | |
| 102 | return OK; |
| 103 | } |
| 104 | |
| 105 | status_t StagefrightRecorder::setVideoSource(video_source vs) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 106 | LOGV("setVideoSource: %d", vs); |
| 107 | if (vs < VIDEO_SOURCE_DEFAULT || |
| 108 | vs >= VIDEO_SOURCE_LIST_END) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 109 | LOGE("Invalid video source: %d", vs); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 110 | return BAD_VALUE; |
| 111 | } |
| 112 | |
| 113 | if (vs == VIDEO_SOURCE_DEFAULT) { |
| 114 | mVideoSource = VIDEO_SOURCE_CAMERA; |
| 115 | } else { |
| 116 | mVideoSource = vs; |
| 117 | } |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 118 | |
| 119 | return OK; |
| 120 | } |
| 121 | |
| 122 | status_t StagefrightRecorder::setOutputFormat(output_format of) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 123 | LOGV("setOutputFormat: %d", of); |
| 124 | if (of < OUTPUT_FORMAT_DEFAULT || |
| 125 | of >= OUTPUT_FORMAT_LIST_END) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 126 | LOGE("Invalid output format: %d", of); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 127 | return BAD_VALUE; |
| 128 | } |
| 129 | |
| 130 | if (of == OUTPUT_FORMAT_DEFAULT) { |
| 131 | mOutputFormat = OUTPUT_FORMAT_THREE_GPP; |
| 132 | } else { |
| 133 | mOutputFormat = of; |
| 134 | } |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 135 | |
| 136 | return OK; |
| 137 | } |
| 138 | |
| 139 | status_t StagefrightRecorder::setAudioEncoder(audio_encoder ae) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 140 | LOGV("setAudioEncoder: %d", ae); |
| 141 | if (ae < AUDIO_ENCODER_DEFAULT || |
| 142 | ae >= AUDIO_ENCODER_LIST_END) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 143 | LOGE("Invalid audio encoder: %d", ae); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 144 | return BAD_VALUE; |
| 145 | } |
| 146 | |
| 147 | if (ae == AUDIO_ENCODER_DEFAULT) { |
| 148 | mAudioEncoder = AUDIO_ENCODER_AMR_NB; |
| 149 | } else { |
| 150 | mAudioEncoder = ae; |
| 151 | } |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 152 | |
| 153 | return OK; |
| 154 | } |
| 155 | |
| 156 | status_t StagefrightRecorder::setVideoEncoder(video_encoder ve) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 157 | LOGV("setVideoEncoder: %d", ve); |
| 158 | if (ve < VIDEO_ENCODER_DEFAULT || |
| 159 | ve >= VIDEO_ENCODER_LIST_END) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 160 | LOGE("Invalid video encoder: %d", ve); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 161 | return BAD_VALUE; |
| 162 | } |
| 163 | |
| 164 | if (ve == VIDEO_ENCODER_DEFAULT) { |
| 165 | mVideoEncoder = VIDEO_ENCODER_H263; |
| 166 | } else { |
| 167 | mVideoEncoder = ve; |
| 168 | } |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 169 | |
| 170 | return OK; |
| 171 | } |
| 172 | |
| 173 | status_t StagefrightRecorder::setVideoSize(int width, int height) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 174 | LOGV("setVideoSize: %dx%d", width, height); |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 175 | if (width <= 0 || height <= 0) { |
| 176 | LOGE("Invalid video size: %dx%d", width, height); |
| 177 | return BAD_VALUE; |
| 178 | } |
| 179 | |
| 180 | // Additional check on the dimension will be performed later |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 181 | mVideoWidth = width; |
| 182 | mVideoHeight = height; |
| 183 | |
| 184 | return OK; |
| 185 | } |
| 186 | |
| 187 | status_t StagefrightRecorder::setVideoFrameRate(int frames_per_second) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 188 | LOGV("setVideoFrameRate: %d", frames_per_second); |
James Dong | b15f2ea | 2010-10-24 09:32:39 -0700 | [diff] [blame] | 189 | if ((frames_per_second <= 0 && frames_per_second != -1) || |
| 190 | frames_per_second > 120) { |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 191 | LOGE("Invalid video frame rate: %d", frames_per_second); |
| 192 | return BAD_VALUE; |
| 193 | } |
| 194 | |
| 195 | // Additional check on the frame rate will be performed later |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 196 | mFrameRate = frames_per_second; |
| 197 | |
| 198 | return OK; |
| 199 | } |
| 200 | |
Wu-cheng Li | 42419ce | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 201 | status_t StagefrightRecorder::setCamera(const sp<ICamera> &camera, |
| 202 | const sp<ICameraRecordingProxy> &proxy) { |
James Dong | 71d714c | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 203 | LOGV("setCamera"); |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 204 | if (camera == 0) { |
| 205 | LOGE("camera is NULL"); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 206 | return BAD_VALUE; |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 207 | } |
Wu-cheng Li | 42419ce | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 208 | if (proxy == 0) { |
| 209 | LOGE("camera proxy is NULL"); |
| 210 | return BAD_VALUE; |
| 211 | } |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 212 | |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 213 | mCamera = camera; |
Wu-cheng Li | 42419ce | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 214 | mCameraProxy = proxy; |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 215 | return OK; |
| 216 | } |
| 217 | |
Jamie Gennis | 85cfdd0 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 218 | status_t StagefrightRecorder::setPreviewSurface(const sp<Surface> &surface) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 219 | LOGV("setPreviewSurface: %p", surface.get()); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 220 | mPreviewSurface = surface; |
| 221 | |
| 222 | return OK; |
| 223 | } |
| 224 | |
| 225 | status_t StagefrightRecorder::setOutputFile(const char *path) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 226 | LOGE("setOutputFile(const char*) must not be called"); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 227 | // We don't actually support this at all, as the media_server process |
| 228 | // no longer has permissions to create files. |
| 229 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 230 | return -EPERM; |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | status_t StagefrightRecorder::setOutputFile(int fd, int64_t offset, int64_t length) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 234 | LOGV("setOutputFile: %d, %lld, %lld", fd, offset, length); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 235 | // These don't make any sense, do they? |
| 236 | CHECK_EQ(offset, 0); |
| 237 | CHECK_EQ(length, 0); |
| 238 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 239 | if (fd < 0) { |
| 240 | LOGE("Invalid file descriptor: %d", fd); |
| 241 | return -EBADF; |
| 242 | } |
| 243 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 244 | if (mOutputFd >= 0) { |
| 245 | ::close(mOutputFd); |
| 246 | } |
| 247 | mOutputFd = dup(fd); |
| 248 | |
| 249 | return OK; |
| 250 | } |
| 251 | |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 252 | status_t StagefrightRecorder::setOutputFileAuxiliary(int fd) { |
| 253 | LOGV("setOutputFileAuxiliary: %d", fd); |
| 254 | |
| 255 | if (fd < 0) { |
| 256 | LOGE("Invalid file descriptor: %d", fd); |
| 257 | return -EBADF; |
| 258 | } |
| 259 | |
| 260 | mCaptureAuxVideo = true; |
| 261 | |
| 262 | if (mOutputFdAux >= 0) { |
| 263 | ::close(mOutputFdAux); |
| 264 | } |
| 265 | mOutputFdAux = dup(fd); |
| 266 | |
| 267 | return OK; |
| 268 | } |
| 269 | |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 270 | // Attempt to parse an int64 literal optionally surrounded by whitespace, |
| 271 | // returns true on success, false otherwise. |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 272 | static bool safe_strtoi64(const char *s, int64_t *val) { |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 273 | char *end; |
James Dong | 75fb238 | 2011-02-08 15:41:58 -0800 | [diff] [blame] | 274 | |
| 275 | // It is lame, but according to man page, we have to set errno to 0 |
| 276 | // before calling strtoll(). |
| 277 | errno = 0; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 278 | *val = strtoll(s, &end, 10); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 279 | |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 280 | if (end == s || errno == ERANGE) { |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | // Skip trailing whitespace |
| 285 | while (isspace(*end)) { |
| 286 | ++end; |
| 287 | } |
| 288 | |
| 289 | // For a successful return, the string must contain nothing but a valid |
| 290 | // int64 literal optionally surrounded by whitespace. |
| 291 | |
| 292 | return *end == '\0'; |
| 293 | } |
| 294 | |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 295 | // Return true if the value is in [0, 0x007FFFFFFF] |
| 296 | static bool safe_strtoi32(const char *s, int32_t *val) { |
| 297 | int64_t temp; |
| 298 | if (safe_strtoi64(s, &temp)) { |
| 299 | if (temp >= 0 && temp <= 0x007FFFFFFF) { |
| 300 | *val = static_cast<int32_t>(temp); |
| 301 | return true; |
| 302 | } |
| 303 | } |
| 304 | return false; |
| 305 | } |
| 306 | |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 307 | // Trim both leading and trailing whitespace from the given string. |
| 308 | static void TrimString(String8 *s) { |
| 309 | size_t num_bytes = s->bytes(); |
| 310 | const char *data = s->string(); |
| 311 | |
| 312 | size_t leading_space = 0; |
| 313 | while (leading_space < num_bytes && isspace(data[leading_space])) { |
| 314 | ++leading_space; |
| 315 | } |
| 316 | |
| 317 | size_t i = num_bytes; |
| 318 | while (i > leading_space && isspace(data[i - 1])) { |
| 319 | --i; |
| 320 | } |
| 321 | |
| 322 | s->setTo(String8(&data[leading_space], i - leading_space)); |
| 323 | } |
| 324 | |
| 325 | status_t StagefrightRecorder::setParamAudioSamplingRate(int32_t sampleRate) { |
| 326 | LOGV("setParamAudioSamplingRate: %d", sampleRate); |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 327 | if (sampleRate <= 0) { |
| 328 | LOGE("Invalid audio sampling rate: %d", sampleRate); |
| 329 | return BAD_VALUE; |
| 330 | } |
| 331 | |
| 332 | // Additional check on the sample rate will be performed later. |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 333 | mSampleRate = sampleRate; |
| 334 | return OK; |
| 335 | } |
| 336 | |
| 337 | status_t StagefrightRecorder::setParamAudioNumberOfChannels(int32_t channels) { |
| 338 | LOGV("setParamAudioNumberOfChannels: %d", channels); |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 339 | if (channels <= 0 || channels >= 3) { |
| 340 | LOGE("Invalid number of audio channels: %d", channels); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 341 | return BAD_VALUE; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | // Additional check on the number of channels will be performed later. |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 345 | mAudioChannels = channels; |
| 346 | return OK; |
| 347 | } |
| 348 | |
| 349 | status_t StagefrightRecorder::setParamAudioEncodingBitRate(int32_t bitRate) { |
| 350 | LOGV("setParamAudioEncodingBitRate: %d", bitRate); |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 351 | if (bitRate <= 0) { |
| 352 | LOGE("Invalid audio encoding bit rate: %d", bitRate); |
| 353 | return BAD_VALUE; |
| 354 | } |
| 355 | |
| 356 | // The target bit rate may not be exactly the same as the requested. |
| 357 | // It depends on many factors, such as rate control, and the bit rate |
| 358 | // range that a specific encoder supports. The mismatch between the |
| 359 | // the target and requested bit rate will NOT be treated as an error. |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 360 | mAudioBitRate = bitRate; |
| 361 | return OK; |
| 362 | } |
| 363 | |
| 364 | status_t StagefrightRecorder::setParamVideoEncodingBitRate(int32_t bitRate) { |
| 365 | LOGV("setParamVideoEncodingBitRate: %d", bitRate); |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 366 | if (bitRate <= 0) { |
| 367 | LOGE("Invalid video encoding bit rate: %d", bitRate); |
| 368 | return BAD_VALUE; |
| 369 | } |
| 370 | |
| 371 | // The target bit rate may not be exactly the same as the requested. |
| 372 | // It depends on many factors, such as rate control, and the bit rate |
| 373 | // range that a specific encoder supports. The mismatch between the |
| 374 | // the target and requested bit rate will NOT be treated as an error. |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 375 | mVideoBitRate = bitRate; |
| 376 | return OK; |
| 377 | } |
| 378 | |
James Dong | b9d7e01 | 2010-11-09 11:15:47 -0800 | [diff] [blame] | 379 | // Always rotate clockwise, and only support 0, 90, 180 and 270 for now. |
| 380 | status_t StagefrightRecorder::setParamVideoRotation(int32_t degrees) { |
| 381 | LOGV("setParamVideoRotation: %d", degrees); |
| 382 | if (degrees < 0 || degrees % 90 != 0) { |
| 383 | LOGE("Unsupported video rotation angle: %d", degrees); |
| 384 | return BAD_VALUE; |
| 385 | } |
| 386 | mRotationDegrees = degrees % 360; |
| 387 | return OK; |
| 388 | } |
| 389 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 390 | status_t StagefrightRecorder::setParamMaxFileDurationUs(int64_t timeUs) { |
| 391 | LOGV("setParamMaxFileDurationUs: %lld us", timeUs); |
James Dong | 26ea64c | 2010-12-20 11:39:38 -0800 | [diff] [blame] | 392 | |
| 393 | // This is meant for backward compatibility for MediaRecorder.java |
Nipun Kwatra | fb45748 | 2010-08-18 15:19:19 -0700 | [diff] [blame] | 394 | if (timeUs <= 0) { |
| 395 | LOGW("Max file duration is not positive: %lld us. Disabling duration limit.", timeUs); |
| 396 | timeUs = 0; // Disable the duration limit for zero or negative values. |
| 397 | } else if (timeUs <= 100000LL) { // XXX: 100 milli-seconds |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 398 | LOGE("Max file duration is too short: %lld us", timeUs); |
| 399 | return BAD_VALUE; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 400 | } |
Nipun Kwatra | fb45748 | 2010-08-18 15:19:19 -0700 | [diff] [blame] | 401 | |
James Dong | 5cdcf16 | 2010-11-30 18:18:08 -0800 | [diff] [blame] | 402 | if (timeUs <= 15 * 1000000LL) { |
| 403 | LOGW("Target duration (%lld us) too short to be respected", timeUs); |
| 404 | } |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 405 | mMaxFileDurationUs = timeUs; |
| 406 | return OK; |
| 407 | } |
| 408 | |
| 409 | status_t StagefrightRecorder::setParamMaxFileSizeBytes(int64_t bytes) { |
| 410 | LOGV("setParamMaxFileSizeBytes: %lld bytes", bytes); |
James Dong | 26ea64c | 2010-12-20 11:39:38 -0800 | [diff] [blame] | 411 | |
| 412 | // This is meant for backward compatibility for MediaRecorder.java |
| 413 | if (bytes <= 0) { |
| 414 | LOGW("Max file size is not positive: %lld bytes. " |
| 415 | "Disabling file size limit.", bytes); |
| 416 | bytes = 0; // Disable the file size limit for zero or negative values. |
| 417 | } else if (bytes <= 1024) { // XXX: 1 kB |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 418 | LOGE("Max file size is too small: %lld bytes", bytes); |
| 419 | return BAD_VALUE; |
| 420 | } |
James Dong | 5cdcf16 | 2010-11-30 18:18:08 -0800 | [diff] [blame] | 421 | |
| 422 | if (bytes <= 100 * 1024) { |
| 423 | LOGW("Target file size (%lld bytes) is too small to be respected", bytes); |
| 424 | } |
| 425 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 426 | mMaxFileSizeBytes = bytes; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 427 | return OK; |
| 428 | } |
| 429 | |
James Dong | 3300e96 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 430 | status_t StagefrightRecorder::setParamInterleaveDuration(int32_t durationUs) { |
| 431 | LOGV("setParamInterleaveDuration: %d", durationUs); |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 432 | if (durationUs <= 500000) { // 500 ms |
| 433 | // If interleave duration is too small, it is very inefficient to do |
| 434 | // interleaving since the metadata overhead will count for a significant |
| 435 | // portion of the saved contents |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 436 | LOGE("Audio/video interleave duration is too small: %d us", durationUs); |
| 437 | return BAD_VALUE; |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 438 | } else if (durationUs >= 10000000) { // 10 seconds |
| 439 | // If interleaving duration is too large, it can cause the recording |
| 440 | // session to use too much memory since we have to save the output |
| 441 | // data before we write them out |
| 442 | LOGE("Audio/video interleave duration is too large: %d us", durationUs); |
| 443 | return BAD_VALUE; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 444 | } |
James Dong | 3300e96 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 445 | mInterleaveDurationUs = durationUs; |
| 446 | return OK; |
| 447 | } |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 448 | |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 449 | // If seconds < 0, only the first frame is I frame, and rest are all P frames |
| 450 | // If seconds == 0, all frames are encoded as I frames. No P frames |
| 451 | // If seconds > 0, it is the time spacing (seconds) between 2 neighboring I frames |
| 452 | status_t StagefrightRecorder::setParamVideoIFramesInterval(int32_t seconds) { |
| 453 | LOGV("setParamVideoIFramesInterval: %d seconds", seconds); |
| 454 | mIFramesIntervalSec = seconds; |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 455 | return OK; |
| 456 | } |
| 457 | |
James Dong | 6feaa46 | 2010-06-20 08:20:54 -0700 | [diff] [blame] | 458 | status_t StagefrightRecorder::setParam64BitFileOffset(bool use64Bit) { |
| 459 | LOGV("setParam64BitFileOffset: %s", |
| 460 | use64Bit? "use 64 bit file offset": "use 32 bit file offset"); |
| 461 | mUse64BitFileOffset = use64Bit; |
| 462 | return OK; |
| 463 | } |
| 464 | |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 465 | status_t StagefrightRecorder::setParamVideoCameraId(int32_t cameraId) { |
| 466 | LOGV("setParamVideoCameraId: %d", cameraId); |
| 467 | if (cameraId < 0) { |
| 468 | return BAD_VALUE; |
| 469 | } |
| 470 | mCameraId = cameraId; |
| 471 | return OK; |
| 472 | } |
| 473 | |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 474 | status_t StagefrightRecorder::setParamTrackTimeStatus(int64_t timeDurationUs) { |
| 475 | LOGV("setParamTrackTimeStatus: %lld", timeDurationUs); |
| 476 | if (timeDurationUs < 20000) { // Infeasible if shorter than 20 ms? |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 477 | LOGE("Tracking time duration too short: %lld us", timeDurationUs); |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 478 | return BAD_VALUE; |
| 479 | } |
| 480 | mTrackEveryTimeDurationUs = timeDurationUs; |
| 481 | return OK; |
| 482 | } |
| 483 | |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 484 | status_t StagefrightRecorder::setParamVideoEncoderProfile(int32_t profile) { |
| 485 | LOGV("setParamVideoEncoderProfile: %d", profile); |
| 486 | |
| 487 | // Additional check will be done later when we load the encoder. |
| 488 | // For now, we are accepting values defined in OpenMAX IL. |
| 489 | mVideoEncoderProfile = profile; |
| 490 | return OK; |
| 491 | } |
| 492 | |
| 493 | status_t StagefrightRecorder::setParamVideoEncoderLevel(int32_t level) { |
| 494 | LOGV("setParamVideoEncoderLevel: %d", level); |
| 495 | |
| 496 | // Additional check will be done later when we load the encoder. |
| 497 | // For now, we are accepting values defined in OpenMAX IL. |
| 498 | mVideoEncoderLevel = level; |
| 499 | return OK; |
| 500 | } |
| 501 | |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 502 | status_t StagefrightRecorder::setParamMovieTimeScale(int32_t timeScale) { |
| 503 | LOGV("setParamMovieTimeScale: %d", timeScale); |
| 504 | |
| 505 | // The range is set to be the same as the audio's time scale range |
| 506 | // since audio's time scale has a wider range. |
| 507 | if (timeScale < 600 || timeScale > 96000) { |
| 508 | LOGE("Time scale (%d) for movie is out of range [600, 96000]", timeScale); |
| 509 | return BAD_VALUE; |
| 510 | } |
| 511 | mMovieTimeScale = timeScale; |
| 512 | return OK; |
| 513 | } |
| 514 | |
| 515 | status_t StagefrightRecorder::setParamVideoTimeScale(int32_t timeScale) { |
| 516 | LOGV("setParamVideoTimeScale: %d", timeScale); |
| 517 | |
| 518 | // 60000 is chosen to make sure that each video frame from a 60-fps |
| 519 | // video has 1000 ticks. |
| 520 | if (timeScale < 600 || timeScale > 60000) { |
| 521 | LOGE("Time scale (%d) for video is out of range [600, 60000]", timeScale); |
| 522 | return BAD_VALUE; |
| 523 | } |
| 524 | mVideoTimeScale = timeScale; |
| 525 | return OK; |
| 526 | } |
| 527 | |
| 528 | status_t StagefrightRecorder::setParamAudioTimeScale(int32_t timeScale) { |
| 529 | LOGV("setParamAudioTimeScale: %d", timeScale); |
| 530 | |
| 531 | // 96000 Hz is the highest sampling rate support in AAC. |
| 532 | if (timeScale < 600 || timeScale > 96000) { |
| 533 | LOGE("Time scale (%d) for audio is out of range [600, 96000]", timeScale); |
| 534 | return BAD_VALUE; |
| 535 | } |
| 536 | mAudioTimeScale = timeScale; |
| 537 | return OK; |
| 538 | } |
| 539 | |
Nipun Kwatra | d01371b | 2010-07-20 21:33:31 -0700 | [diff] [blame] | 540 | status_t StagefrightRecorder::setParamTimeLapseEnable(int32_t timeLapseEnable) { |
| 541 | LOGV("setParamTimeLapseEnable: %d", timeLapseEnable); |
| 542 | |
| 543 | if(timeLapseEnable == 0) { |
| 544 | mCaptureTimeLapse = false; |
| 545 | } else if (timeLapseEnable == 1) { |
| 546 | mCaptureTimeLapse = true; |
| 547 | } else { |
| 548 | return BAD_VALUE; |
| 549 | } |
| 550 | return OK; |
| 551 | } |
| 552 | |
| 553 | status_t StagefrightRecorder::setParamTimeBetweenTimeLapseFrameCapture(int64_t timeUs) { |
| 554 | LOGV("setParamTimeBetweenTimeLapseFrameCapture: %lld us", timeUs); |
| 555 | |
| 556 | // Not allowing time more than a day |
| 557 | if (timeUs <= 0 || timeUs > 86400*1E6) { |
| 558 | LOGE("Time between time lapse frame capture (%lld) is out of range [0, 1 Day]", timeUs); |
| 559 | return BAD_VALUE; |
| 560 | } |
| 561 | |
| 562 | mTimeBetweenTimeLapseFrameCaptureUs = timeUs; |
| 563 | return OK; |
| 564 | } |
| 565 | |
Nipun Kwatra | 239f2e5 | 2010-08-31 15:35:44 -0700 | [diff] [blame] | 566 | status_t StagefrightRecorder::setParamAuxVideoWidth(int32_t width) { |
| 567 | LOGV("setParamAuxVideoWidth : %d", width); |
| 568 | |
| 569 | if (width <= 0) { |
| 570 | LOGE("Width (%d) is not positive", width); |
| 571 | return BAD_VALUE; |
| 572 | } |
| 573 | |
| 574 | mAuxVideoWidth = width; |
| 575 | return OK; |
| 576 | } |
| 577 | |
| 578 | status_t StagefrightRecorder::setParamAuxVideoHeight(int32_t height) { |
| 579 | LOGV("setParamAuxVideoHeight : %d", height); |
| 580 | |
| 581 | if (height <= 0) { |
| 582 | LOGE("Height (%d) is not positive", height); |
| 583 | return BAD_VALUE; |
| 584 | } |
| 585 | |
| 586 | mAuxVideoHeight = height; |
| 587 | return OK; |
| 588 | } |
| 589 | |
| 590 | status_t StagefrightRecorder::setParamAuxVideoEncodingBitRate(int32_t bitRate) { |
| 591 | LOGV("StagefrightRecorder::setParamAuxVideoEncodingBitRate: %d", bitRate); |
| 592 | |
| 593 | if (bitRate <= 0) { |
| 594 | LOGE("Invalid video encoding bit rate: %d", bitRate); |
| 595 | return BAD_VALUE; |
| 596 | } |
| 597 | |
| 598 | mAuxVideoBitRate = bitRate; |
| 599 | return OK; |
| 600 | } |
| 601 | |
James Dong | 987ab48 | 2011-05-11 19:09:25 -0700 | [diff] [blame] | 602 | status_t StagefrightRecorder::setParamGeoDataLongitude( |
| 603 | int32_t longitudex10000) { |
| 604 | |
| 605 | if (longitudex10000 > 1800000 || longitudex10000 < -1800000) { |
| 606 | return BAD_VALUE; |
| 607 | } |
| 608 | mLongitudex10000 = longitudex10000; |
| 609 | return OK; |
| 610 | } |
| 611 | |
| 612 | status_t StagefrightRecorder::setParamGeoDataLatitude( |
| 613 | int32_t latitudex10000) { |
| 614 | |
| 615 | if (latitudex10000 > 900000 || latitudex10000 < -900000) { |
| 616 | return BAD_VALUE; |
| 617 | } |
| 618 | mLatitudex10000 = latitudex10000; |
| 619 | return OK; |
| 620 | } |
| 621 | |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 622 | status_t StagefrightRecorder::setParameter( |
| 623 | const String8 &key, const String8 &value) { |
| 624 | LOGV("setParameter: key (%s) => value (%s)", key.string(), value.string()); |
| 625 | if (key == "max-duration") { |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 626 | int64_t max_duration_ms; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 627 | if (safe_strtoi64(value.string(), &max_duration_ms)) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 628 | return setParamMaxFileDurationUs(1000LL * max_duration_ms); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 629 | } |
| 630 | } else if (key == "max-filesize") { |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 631 | int64_t max_filesize_bytes; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 632 | if (safe_strtoi64(value.string(), &max_filesize_bytes)) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 633 | return setParamMaxFileSizeBytes(max_filesize_bytes); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 634 | } |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 635 | } else if (key == "interleave-duration-us") { |
| 636 | int32_t durationUs; |
| 637 | if (safe_strtoi32(value.string(), &durationUs)) { |
| 638 | return setParamInterleaveDuration(durationUs); |
| 639 | } |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 640 | } else if (key == "param-movie-time-scale") { |
| 641 | int32_t timeScale; |
| 642 | if (safe_strtoi32(value.string(), &timeScale)) { |
| 643 | return setParamMovieTimeScale(timeScale); |
| 644 | } |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 645 | } else if (key == "param-use-64bit-offset") { |
| 646 | int32_t use64BitOffset; |
| 647 | if (safe_strtoi32(value.string(), &use64BitOffset)) { |
| 648 | return setParam64BitFileOffset(use64BitOffset != 0); |
| 649 | } |
James Dong | 987ab48 | 2011-05-11 19:09:25 -0700 | [diff] [blame] | 650 | } else if (key == "param-geotag-longitude") { |
| 651 | int32_t longitudex10000; |
| 652 | if (safe_strtoi32(value.string(), &longitudex10000)) { |
| 653 | return setParamGeoDataLongitude(longitudex10000); |
| 654 | } |
| 655 | } else if (key == "param-geotag-latitude") { |
| 656 | int32_t latitudex10000; |
| 657 | if (safe_strtoi32(value.string(), &latitudex10000)) { |
| 658 | return setParamGeoDataLatitude(latitudex10000); |
| 659 | } |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 660 | } else if (key == "param-track-time-status") { |
| 661 | int64_t timeDurationUs; |
| 662 | if (safe_strtoi64(value.string(), &timeDurationUs)) { |
| 663 | return setParamTrackTimeStatus(timeDurationUs); |
| 664 | } |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 665 | } else if (key == "audio-param-sampling-rate") { |
| 666 | int32_t sampling_rate; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 667 | if (safe_strtoi32(value.string(), &sampling_rate)) { |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 668 | return setParamAudioSamplingRate(sampling_rate); |
| 669 | } |
| 670 | } else if (key == "audio-param-number-of-channels") { |
| 671 | int32_t number_of_channels; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 672 | if (safe_strtoi32(value.string(), &number_of_channels)) { |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 673 | return setParamAudioNumberOfChannels(number_of_channels); |
| 674 | } |
| 675 | } else if (key == "audio-param-encoding-bitrate") { |
| 676 | int32_t audio_bitrate; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 677 | if (safe_strtoi32(value.string(), &audio_bitrate)) { |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 678 | return setParamAudioEncodingBitRate(audio_bitrate); |
| 679 | } |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 680 | } else if (key == "audio-param-time-scale") { |
| 681 | int32_t timeScale; |
| 682 | if (safe_strtoi32(value.string(), &timeScale)) { |
| 683 | return setParamAudioTimeScale(timeScale); |
| 684 | } |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 685 | } else if (key == "video-param-encoding-bitrate") { |
| 686 | int32_t video_bitrate; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 687 | if (safe_strtoi32(value.string(), &video_bitrate)) { |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 688 | return setParamVideoEncodingBitRate(video_bitrate); |
| 689 | } |
James Dong | b9d7e01 | 2010-11-09 11:15:47 -0800 | [diff] [blame] | 690 | } else if (key == "video-param-rotation-angle-degrees") { |
| 691 | int32_t degrees; |
| 692 | if (safe_strtoi32(value.string(), °rees)) { |
| 693 | return setParamVideoRotation(degrees); |
| 694 | } |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 695 | } else if (key == "video-param-i-frames-interval") { |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 696 | int32_t seconds; |
| 697 | if (safe_strtoi32(value.string(), &seconds)) { |
| 698 | return setParamVideoIFramesInterval(seconds); |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 699 | } |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 700 | } else if (key == "video-param-encoder-profile") { |
| 701 | int32_t profile; |
| 702 | if (safe_strtoi32(value.string(), &profile)) { |
| 703 | return setParamVideoEncoderProfile(profile); |
| 704 | } |
| 705 | } else if (key == "video-param-encoder-level") { |
| 706 | int32_t level; |
| 707 | if (safe_strtoi32(value.string(), &level)) { |
| 708 | return setParamVideoEncoderLevel(level); |
| 709 | } |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 710 | } else if (key == "video-param-camera-id") { |
| 711 | int32_t cameraId; |
| 712 | if (safe_strtoi32(value.string(), &cameraId)) { |
| 713 | return setParamVideoCameraId(cameraId); |
James Dong | 6feaa46 | 2010-06-20 08:20:54 -0700 | [diff] [blame] | 714 | } |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 715 | } else if (key == "video-param-time-scale") { |
| 716 | int32_t timeScale; |
| 717 | if (safe_strtoi32(value.string(), &timeScale)) { |
| 718 | return setParamVideoTimeScale(timeScale); |
| 719 | } |
Nipun Kwatra | d01371b | 2010-07-20 21:33:31 -0700 | [diff] [blame] | 720 | } else if (key == "time-lapse-enable") { |
| 721 | int32_t timeLapseEnable; |
| 722 | if (safe_strtoi32(value.string(), &timeLapseEnable)) { |
| 723 | return setParamTimeLapseEnable(timeLapseEnable); |
| 724 | } |
| 725 | } else if (key == "time-between-time-lapse-frame-capture") { |
| 726 | int64_t timeBetweenTimeLapseFrameCaptureMs; |
| 727 | if (safe_strtoi64(value.string(), &timeBetweenTimeLapseFrameCaptureMs)) { |
| 728 | return setParamTimeBetweenTimeLapseFrameCapture( |
| 729 | 1000LL * timeBetweenTimeLapseFrameCaptureMs); |
| 730 | } |
Nipun Kwatra | 239f2e5 | 2010-08-31 15:35:44 -0700 | [diff] [blame] | 731 | } else if (key == "video-aux-param-width") { |
| 732 | int32_t auxWidth; |
| 733 | if (safe_strtoi32(value.string(), &auxWidth)) { |
| 734 | return setParamAuxVideoWidth(auxWidth); |
| 735 | } |
| 736 | } else if (key == "video-aux-param-height") { |
| 737 | int32_t auxHeight; |
| 738 | if (safe_strtoi32(value.string(), &auxHeight)) { |
| 739 | return setParamAuxVideoHeight(auxHeight); |
| 740 | } |
| 741 | } else if (key == "video-aux-param-encoding-bitrate") { |
| 742 | int32_t auxVideoBitRate; |
| 743 | if (safe_strtoi32(value.string(), &auxVideoBitRate)) { |
| 744 | return setParamAuxVideoEncodingBitRate(auxVideoBitRate); |
| 745 | } |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 746 | } else { |
| 747 | LOGE("setParameter: failed to find key %s", key.string()); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 748 | } |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 749 | return BAD_VALUE; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | status_t StagefrightRecorder::setParameters(const String8 ¶ms) { |
| 753 | LOGV("setParameters: %s", params.string()); |
| 754 | const char *cparams = params.string(); |
| 755 | const char *key_start = cparams; |
| 756 | for (;;) { |
| 757 | const char *equal_pos = strchr(key_start, '='); |
| 758 | if (equal_pos == NULL) { |
| 759 | LOGE("Parameters %s miss a value", cparams); |
| 760 | return BAD_VALUE; |
| 761 | } |
| 762 | String8 key(key_start, equal_pos - key_start); |
| 763 | TrimString(&key); |
| 764 | if (key.length() == 0) { |
| 765 | LOGE("Parameters %s contains an empty key", cparams); |
| 766 | return BAD_VALUE; |
| 767 | } |
| 768 | const char *value_start = equal_pos + 1; |
| 769 | const char *semicolon_pos = strchr(value_start, ';'); |
| 770 | String8 value; |
| 771 | if (semicolon_pos == NULL) { |
| 772 | value.setTo(value_start); |
| 773 | } else { |
| 774 | value.setTo(value_start, semicolon_pos - value_start); |
| 775 | } |
| 776 | if (setParameter(key, value) != OK) { |
| 777 | return BAD_VALUE; |
| 778 | } |
| 779 | if (semicolon_pos == NULL) { |
| 780 | break; // Reaches the end |
| 781 | } |
| 782 | key_start = semicolon_pos + 1; |
| 783 | } |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 784 | return OK; |
| 785 | } |
| 786 | |
James Dong | fe1bafe | 2010-06-25 17:06:47 -0700 | [diff] [blame] | 787 | status_t StagefrightRecorder::setListener(const sp<IMediaRecorderClient> &listener) { |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 788 | mListener = listener; |
| 789 | |
| 790 | return OK; |
| 791 | } |
| 792 | |
| 793 | status_t StagefrightRecorder::prepare() { |
| 794 | return OK; |
| 795 | } |
| 796 | |
| 797 | status_t StagefrightRecorder::start() { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 798 | CHECK(mOutputFd >= 0); |
| 799 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 800 | if (mWriter != NULL) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 801 | LOGE("File writer is not avaialble"); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 802 | return UNKNOWN_ERROR; |
| 803 | } |
| 804 | |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 805 | status_t status = OK; |
| 806 | |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 807 | switch (mOutputFormat) { |
| 808 | case OUTPUT_FORMAT_DEFAULT: |
| 809 | case OUTPUT_FORMAT_THREE_GPP: |
| 810 | case OUTPUT_FORMAT_MPEG_4: |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 811 | status = startMPEG4Recording(); |
| 812 | break; |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 813 | |
| 814 | case OUTPUT_FORMAT_AMR_NB: |
| 815 | case OUTPUT_FORMAT_AMR_WB: |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 816 | status = startAMRRecording(); |
| 817 | break; |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 818 | |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 819 | case OUTPUT_FORMAT_AAC_ADIF: |
| 820 | case OUTPUT_FORMAT_AAC_ADTS: |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 821 | status = startAACRecording(); |
| 822 | break; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 823 | |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 824 | case OUTPUT_FORMAT_RTP_AVP: |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 825 | status = startRTPRecording(); |
| 826 | break; |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 827 | |
Andreas Huber | 9adf466 | 2010-10-12 14:17:45 -0700 | [diff] [blame] | 828 | case OUTPUT_FORMAT_MPEG2TS: |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 829 | status = startMPEG2TSRecording(); |
| 830 | break; |
Andreas Huber | 9adf466 | 2010-10-12 14:17:45 -0700 | [diff] [blame] | 831 | |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 832 | default: |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 833 | LOGE("Unsupported output file format: %d", mOutputFormat); |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 834 | status = UNKNOWN_ERROR; |
| 835 | break; |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 836 | } |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 837 | |
| 838 | if ((status == OK) && (!mStarted)) { |
| 839 | mStarted = true; |
| 840 | |
| 841 | uint32_t params = IMediaPlayerService::kBatteryDataCodecStarted; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 842 | if (mAudioSource != AUDIO_SOURCE_CNT) { |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 843 | params |= IMediaPlayerService::kBatteryDataTrackAudio; |
| 844 | } |
| 845 | if (mVideoSource != VIDEO_SOURCE_LIST_END) { |
| 846 | params |= IMediaPlayerService::kBatteryDataTrackVideo; |
| 847 | } |
| 848 | |
| 849 | addBatteryData(params); |
| 850 | } |
| 851 | |
| 852 | return status; |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 853 | } |
| 854 | |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 855 | sp<MediaSource> StagefrightRecorder::createAudioSource() { |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 856 | sp<AudioSource> audioSource = |
| 857 | new AudioSource( |
| 858 | mAudioSource, |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 859 | mSampleRate, |
James Dong | d77d2a9 | 2010-06-14 17:45:35 -0700 | [diff] [blame] | 860 | mAudioChannels); |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 861 | |
| 862 | status_t err = audioSource->initCheck(); |
| 863 | |
| 864 | if (err != OK) { |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 865 | LOGE("audio source is not initialized"); |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 866 | return NULL; |
| 867 | } |
| 868 | |
| 869 | sp<MetaData> encMeta = new MetaData; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 870 | const char *mime; |
| 871 | switch (mAudioEncoder) { |
| 872 | case AUDIO_ENCODER_AMR_NB: |
| 873 | case AUDIO_ENCODER_DEFAULT: |
| 874 | mime = MEDIA_MIMETYPE_AUDIO_AMR_NB; |
| 875 | break; |
| 876 | case AUDIO_ENCODER_AMR_WB: |
| 877 | mime = MEDIA_MIMETYPE_AUDIO_AMR_WB; |
| 878 | break; |
| 879 | case AUDIO_ENCODER_AAC: |
| 880 | mime = MEDIA_MIMETYPE_AUDIO_AAC; |
| 881 | break; |
| 882 | default: |
| 883 | LOGE("Unknown audio encoder: %d", mAudioEncoder); |
| 884 | return NULL; |
| 885 | } |
| 886 | encMeta->setCString(kKeyMIMEType, mime); |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 887 | |
Andreas Huber | 259b7c1 | 2010-02-10 15:04:31 -0800 | [diff] [blame] | 888 | int32_t maxInputSize; |
| 889 | CHECK(audioSource->getFormat()->findInt32( |
| 890 | kKeyMaxInputSize, &maxInputSize)); |
| 891 | |
| 892 | encMeta->setInt32(kKeyMaxInputSize, maxInputSize); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 893 | encMeta->setInt32(kKeyChannelCount, mAudioChannels); |
| 894 | encMeta->setInt32(kKeySampleRate, mSampleRate); |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 895 | encMeta->setInt32(kKeyBitRate, mAudioBitRate); |
James Dong | eff30e3 | 2010-08-13 14:16:26 -0700 | [diff] [blame] | 896 | if (mAudioTimeScale > 0) { |
| 897 | encMeta->setInt32(kKeyTimeScale, mAudioTimeScale); |
| 898 | } |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 899 | |
| 900 | OMXClient client; |
| 901 | CHECK_EQ(client.connect(), OK); |
| 902 | |
| 903 | sp<MediaSource> audioEncoder = |
| 904 | OMXCodec::Create(client.interface(), encMeta, |
| 905 | true /* createEncoder */, audioSource); |
James Dong | 57e7f83 | 2010-06-24 19:55:31 -0700 | [diff] [blame] | 906 | mAudioSourceNode = audioSource; |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 907 | |
| 908 | return audioEncoder; |
| 909 | } |
| 910 | |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 911 | status_t StagefrightRecorder::startAACRecording() { |
James Dong | f84bfab | 2011-03-21 14:29:38 -0700 | [diff] [blame] | 912 | // FIXME: |
| 913 | // Add support for OUTPUT_FORMAT_AAC_ADIF |
| 914 | CHECK(mOutputFormat == OUTPUT_FORMAT_AAC_ADTS); |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 915 | |
| 916 | CHECK(mAudioEncoder == AUDIO_ENCODER_AAC); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 917 | CHECK(mAudioSource != AUDIO_SOURCE_CNT); |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 918 | |
James Dong | f84bfab | 2011-03-21 14:29:38 -0700 | [diff] [blame] | 919 | mWriter = new AACWriter(mOutputFd); |
| 920 | status_t status = startRawAudioRecording(); |
| 921 | if (status != OK) { |
| 922 | mWriter.clear(); |
| 923 | mWriter = NULL; |
| 924 | } |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 925 | |
James Dong | f84bfab | 2011-03-21 14:29:38 -0700 | [diff] [blame] | 926 | return status; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 927 | } |
| 928 | |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 929 | status_t StagefrightRecorder::startAMRRecording() { |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 930 | CHECK(mOutputFormat == OUTPUT_FORMAT_AMR_NB || |
| 931 | mOutputFormat == OUTPUT_FORMAT_AMR_WB); |
| 932 | |
| 933 | if (mOutputFormat == OUTPUT_FORMAT_AMR_NB) { |
| 934 | if (mAudioEncoder != AUDIO_ENCODER_DEFAULT && |
| 935 | mAudioEncoder != AUDIO_ENCODER_AMR_NB) { |
| 936 | LOGE("Invalid encoder %d used for AMRNB recording", |
| 937 | mAudioEncoder); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 938 | return BAD_VALUE; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 939 | } |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 940 | } else { // mOutputFormat must be OUTPUT_FORMAT_AMR_WB |
| 941 | if (mAudioEncoder != AUDIO_ENCODER_AMR_WB) { |
| 942 | LOGE("Invlaid encoder %d used for AMRWB recording", |
| 943 | mAudioEncoder); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 944 | return BAD_VALUE; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 945 | } |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 946 | } |
| 947 | |
James Dong | f84bfab | 2011-03-21 14:29:38 -0700 | [diff] [blame] | 948 | mWriter = new AMRWriter(mOutputFd); |
| 949 | status_t status = startRawAudioRecording(); |
| 950 | if (status != OK) { |
| 951 | mWriter.clear(); |
| 952 | mWriter = NULL; |
| 953 | } |
| 954 | return status; |
| 955 | } |
| 956 | |
| 957 | status_t StagefrightRecorder::startRawAudioRecording() { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 958 | if (mAudioSource >= AUDIO_SOURCE_CNT) { |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 959 | LOGE("Invalid audio source: %d", mAudioSource); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 960 | return BAD_VALUE; |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 961 | } |
| 962 | |
James Dong | 54815a7 | 2011-01-12 20:45:16 -0800 | [diff] [blame] | 963 | status_t status = BAD_VALUE; |
| 964 | if (OK != (status = checkAudioEncoderCapabilities())) { |
| 965 | return status; |
| 966 | } |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 967 | |
James Dong | 54815a7 | 2011-01-12 20:45:16 -0800 | [diff] [blame] | 968 | sp<MediaSource> audioEncoder = createAudioSource(); |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 969 | if (audioEncoder == NULL) { |
| 970 | return UNKNOWN_ERROR; |
| 971 | } |
| 972 | |
James Dong | f84bfab | 2011-03-21 14:29:38 -0700 | [diff] [blame] | 973 | CHECK(mWriter != 0); |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 974 | mWriter->addSource(audioEncoder); |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 975 | |
| 976 | if (mMaxFileDurationUs != 0) { |
| 977 | mWriter->setMaxFileDuration(mMaxFileDurationUs); |
| 978 | } |
| 979 | if (mMaxFileSizeBytes != 0) { |
| 980 | mWriter->setMaxFileSize(mMaxFileSizeBytes); |
| 981 | } |
| 982 | mWriter->setListener(mListener); |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 983 | mWriter->start(); |
| 984 | |
| 985 | return OK; |
| 986 | } |
| 987 | |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 988 | status_t StagefrightRecorder::startRTPRecording() { |
| 989 | CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_RTP_AVP); |
| 990 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 991 | if ((mAudioSource != AUDIO_SOURCE_CNT |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 992 | && mVideoSource != VIDEO_SOURCE_LIST_END) |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 993 | || (mAudioSource == AUDIO_SOURCE_CNT |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 994 | && mVideoSource == VIDEO_SOURCE_LIST_END)) { |
| 995 | // Must have exactly one source. |
| 996 | return BAD_VALUE; |
| 997 | } |
| 998 | |
| 999 | if (mOutputFd < 0) { |
| 1000 | return BAD_VALUE; |
| 1001 | } |
| 1002 | |
| 1003 | sp<MediaSource> source; |
| 1004 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1005 | if (mAudioSource != AUDIO_SOURCE_CNT) { |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 1006 | source = createAudioSource(); |
| 1007 | } else { |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1008 | |
| 1009 | sp<CameraSource> cameraSource; |
| 1010 | status_t err = setupCameraSource(&cameraSource); |
| 1011 | if (err != OK) { |
| 1012 | return err; |
| 1013 | } |
| 1014 | |
| 1015 | err = setupVideoEncoder(cameraSource, mVideoBitRate, &source); |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 1016 | if (err != OK) { |
| 1017 | return err; |
| 1018 | } |
| 1019 | } |
| 1020 | |
James Dong | 2747e0e | 2010-11-18 20:59:13 -0800 | [diff] [blame] | 1021 | mWriter = new ARTPWriter(mOutputFd); |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 1022 | mWriter->addSource(source); |
| 1023 | mWriter->setListener(mListener); |
| 1024 | |
| 1025 | return mWriter->start(); |
| 1026 | } |
| 1027 | |
Andreas Huber | 9adf466 | 2010-10-12 14:17:45 -0700 | [diff] [blame] | 1028 | status_t StagefrightRecorder::startMPEG2TSRecording() { |
| 1029 | CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_MPEG2TS); |
| 1030 | |
James Dong | 2747e0e | 2010-11-18 20:59:13 -0800 | [diff] [blame] | 1031 | sp<MediaWriter> writer = new MPEG2TSWriter(mOutputFd); |
Andreas Huber | 9adf466 | 2010-10-12 14:17:45 -0700 | [diff] [blame] | 1032 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1033 | if (mAudioSource != AUDIO_SOURCE_CNT) { |
Andreas Huber | 9adf466 | 2010-10-12 14:17:45 -0700 | [diff] [blame] | 1034 | if (mAudioEncoder != AUDIO_ENCODER_AAC) { |
| 1035 | return ERROR_UNSUPPORTED; |
| 1036 | } |
| 1037 | |
| 1038 | status_t err = setupAudioEncoder(writer); |
| 1039 | |
| 1040 | if (err != OK) { |
| 1041 | return err; |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | if (mVideoSource == VIDEO_SOURCE_DEFAULT |
| 1046 | || mVideoSource == VIDEO_SOURCE_CAMERA) { |
| 1047 | if (mVideoEncoder != VIDEO_ENCODER_H264) { |
| 1048 | return ERROR_UNSUPPORTED; |
| 1049 | } |
| 1050 | |
Kenny Root | 4a90f93 | 2010-10-14 23:58:41 -0700 | [diff] [blame] | 1051 | sp<CameraSource> cameraSource; |
| 1052 | status_t err = setupCameraSource(&cameraSource); |
| 1053 | if (err != OK) { |
| 1054 | return err; |
| 1055 | } |
| 1056 | |
Andreas Huber | 9adf466 | 2010-10-12 14:17:45 -0700 | [diff] [blame] | 1057 | sp<MediaSource> encoder; |
Kenny Root | 4a90f93 | 2010-10-14 23:58:41 -0700 | [diff] [blame] | 1058 | err = setupVideoEncoder(cameraSource, mVideoBitRate, &encoder); |
Andreas Huber | 9adf466 | 2010-10-12 14:17:45 -0700 | [diff] [blame] | 1059 | |
| 1060 | if (err != OK) { |
| 1061 | return err; |
| 1062 | } |
| 1063 | |
| 1064 | writer->addSource(encoder); |
| 1065 | } |
| 1066 | |
| 1067 | if (mMaxFileDurationUs != 0) { |
| 1068 | writer->setMaxFileDuration(mMaxFileDurationUs); |
| 1069 | } |
| 1070 | |
| 1071 | if (mMaxFileSizeBytes != 0) { |
| 1072 | writer->setMaxFileSize(mMaxFileSizeBytes); |
| 1073 | } |
| 1074 | |
| 1075 | mWriter = writer; |
| 1076 | |
| 1077 | return mWriter->start(); |
| 1078 | } |
| 1079 | |
James Dong | 42a18c0 | 2010-06-16 17:27:46 -0700 | [diff] [blame] | 1080 | void StagefrightRecorder::clipVideoFrameRate() { |
| 1081 | LOGV("clipVideoFrameRate: encoder %d", mVideoEncoder); |
| 1082 | int minFrameRate = mEncoderProfiles->getVideoEncoderParamByName( |
| 1083 | "enc.vid.fps.min", mVideoEncoder); |
| 1084 | int maxFrameRate = mEncoderProfiles->getVideoEncoderParamByName( |
| 1085 | "enc.vid.fps.max", mVideoEncoder); |
James Dong | b15f2ea | 2010-10-24 09:32:39 -0700 | [diff] [blame] | 1086 | if (mFrameRate < minFrameRate && mFrameRate != -1) { |
James Dong | 42a18c0 | 2010-06-16 17:27:46 -0700 | [diff] [blame] | 1087 | LOGW("Intended video encoding frame rate (%d fps) is too small" |
| 1088 | " and will be set to (%d fps)", mFrameRate, minFrameRate); |
| 1089 | mFrameRate = minFrameRate; |
| 1090 | } else if (mFrameRate > maxFrameRate) { |
| 1091 | LOGW("Intended video encoding frame rate (%d fps) is too large" |
| 1092 | " and will be set to (%d fps)", mFrameRate, maxFrameRate); |
| 1093 | mFrameRate = maxFrameRate; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | void StagefrightRecorder::clipVideoBitRate() { |
| 1098 | LOGV("clipVideoBitRate: encoder %d", mVideoEncoder); |
| 1099 | int minBitRate = mEncoderProfiles->getVideoEncoderParamByName( |
| 1100 | "enc.vid.bps.min", mVideoEncoder); |
| 1101 | int maxBitRate = mEncoderProfiles->getVideoEncoderParamByName( |
| 1102 | "enc.vid.bps.max", mVideoEncoder); |
| 1103 | if (mVideoBitRate < minBitRate) { |
| 1104 | LOGW("Intended video encoding bit rate (%d bps) is too small" |
| 1105 | " and will be set to (%d bps)", mVideoBitRate, minBitRate); |
| 1106 | mVideoBitRate = minBitRate; |
| 1107 | } else if (mVideoBitRate > maxBitRate) { |
| 1108 | LOGW("Intended video encoding bit rate (%d bps) is too large" |
| 1109 | " and will be set to (%d bps)", mVideoBitRate, maxBitRate); |
| 1110 | mVideoBitRate = maxBitRate; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | void StagefrightRecorder::clipVideoFrameWidth() { |
| 1115 | LOGV("clipVideoFrameWidth: encoder %d", mVideoEncoder); |
| 1116 | int minFrameWidth = mEncoderProfiles->getVideoEncoderParamByName( |
| 1117 | "enc.vid.width.min", mVideoEncoder); |
| 1118 | int maxFrameWidth = mEncoderProfiles->getVideoEncoderParamByName( |
| 1119 | "enc.vid.width.max", mVideoEncoder); |
| 1120 | if (mVideoWidth < minFrameWidth) { |
| 1121 | LOGW("Intended video encoding frame width (%d) is too small" |
| 1122 | " and will be set to (%d)", mVideoWidth, minFrameWidth); |
| 1123 | mVideoWidth = minFrameWidth; |
| 1124 | } else if (mVideoWidth > maxFrameWidth) { |
| 1125 | LOGW("Intended video encoding frame width (%d) is too large" |
| 1126 | " and will be set to (%d)", mVideoWidth, maxFrameWidth); |
| 1127 | mVideoWidth = maxFrameWidth; |
| 1128 | } |
| 1129 | } |
| 1130 | |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 1131 | status_t StagefrightRecorder::checkVideoEncoderCapabilities() { |
Nipun Kwatra | 453e92f | 2010-08-04 14:26:45 -0700 | [diff] [blame] | 1132 | if (!mCaptureTimeLapse) { |
Nipun Kwatra | d01371b | 2010-07-20 21:33:31 -0700 | [diff] [blame] | 1133 | // Dont clip for time lapse capture as encoder will have enough |
| 1134 | // time to encode because of slow capture rate of time lapse. |
| 1135 | clipVideoBitRate(); |
| 1136 | clipVideoFrameRate(); |
| 1137 | clipVideoFrameWidth(); |
| 1138 | clipVideoFrameHeight(); |
James Dong | be65087 | 2011-07-07 16:41:25 -0700 | [diff] [blame] | 1139 | setDefaultProfileIfNecessary(); |
Nipun Kwatra | d01371b | 2010-07-20 21:33:31 -0700 | [diff] [blame] | 1140 | } |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1141 | return OK; |
| 1142 | } |
| 1143 | |
James Dong | be65087 | 2011-07-07 16:41:25 -0700 | [diff] [blame] | 1144 | // Set to use AVC baseline profile if the encoding parameters matches |
| 1145 | // CAMCORDER_QUALITY_LOW profile; this is for the sake of MMS service. |
| 1146 | void StagefrightRecorder::setDefaultProfileIfNecessary() { |
| 1147 | LOGV("setDefaultProfileIfNecessary"); |
| 1148 | |
| 1149 | camcorder_quality quality = CAMCORDER_QUALITY_LOW; |
| 1150 | |
| 1151 | int64_t durationUs = mEncoderProfiles->getCamcorderProfileParamByName( |
| 1152 | "duration", mCameraId, quality) * 1000000LL; |
| 1153 | |
| 1154 | int fileFormat = mEncoderProfiles->getCamcorderProfileParamByName( |
| 1155 | "file.format", mCameraId, quality); |
| 1156 | |
| 1157 | int videoCodec = mEncoderProfiles->getCamcorderProfileParamByName( |
| 1158 | "vid.codec", mCameraId, quality); |
| 1159 | |
| 1160 | int videoBitRate = mEncoderProfiles->getCamcorderProfileParamByName( |
| 1161 | "vid.bps", mCameraId, quality); |
| 1162 | |
| 1163 | int videoFrameRate = mEncoderProfiles->getCamcorderProfileParamByName( |
| 1164 | "vid.fps", mCameraId, quality); |
| 1165 | |
| 1166 | int videoFrameWidth = mEncoderProfiles->getCamcorderProfileParamByName( |
| 1167 | "vid.width", mCameraId, quality); |
| 1168 | |
| 1169 | int videoFrameHeight = mEncoderProfiles->getCamcorderProfileParamByName( |
| 1170 | "vid.height", mCameraId, quality); |
| 1171 | |
| 1172 | int audioCodec = mEncoderProfiles->getCamcorderProfileParamByName( |
| 1173 | "aud.codec", mCameraId, quality); |
| 1174 | |
| 1175 | int audioBitRate = mEncoderProfiles->getCamcorderProfileParamByName( |
| 1176 | "aud.bps", mCameraId, quality); |
| 1177 | |
| 1178 | int audioSampleRate = mEncoderProfiles->getCamcorderProfileParamByName( |
| 1179 | "aud.hz", mCameraId, quality); |
| 1180 | |
| 1181 | int audioChannels = mEncoderProfiles->getCamcorderProfileParamByName( |
| 1182 | "aud.ch", mCameraId, quality); |
| 1183 | |
| 1184 | if (durationUs == mMaxFileDurationUs && |
| 1185 | fileFormat == mOutputFormat && |
| 1186 | videoCodec == mVideoEncoder && |
| 1187 | videoBitRate == mVideoBitRate && |
| 1188 | videoFrameRate == mFrameRate && |
| 1189 | videoFrameWidth == mVideoWidth && |
| 1190 | videoFrameHeight == mVideoHeight && |
| 1191 | audioCodec == mAudioEncoder && |
| 1192 | audioBitRate == mAudioBitRate && |
| 1193 | audioSampleRate == mSampleRate && |
| 1194 | audioChannels == mAudioChannels) { |
| 1195 | if (videoCodec == VIDEO_ENCODER_H264) { |
| 1196 | LOGI("Force to use AVC baseline profile"); |
| 1197 | setParamVideoEncoderProfile(OMX_VIDEO_AVCProfileBaseline); |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | |
James Dong | 54815a7 | 2011-01-12 20:45:16 -0800 | [diff] [blame] | 1202 | status_t StagefrightRecorder::checkAudioEncoderCapabilities() { |
| 1203 | clipAudioBitRate(); |
| 1204 | clipAudioSampleRate(); |
| 1205 | clipNumberOfAudioChannels(); |
| 1206 | return OK; |
| 1207 | } |
| 1208 | |
| 1209 | void StagefrightRecorder::clipAudioBitRate() { |
| 1210 | LOGV("clipAudioBitRate: encoder %d", mAudioEncoder); |
| 1211 | |
| 1212 | int minAudioBitRate = |
| 1213 | mEncoderProfiles->getAudioEncoderParamByName( |
| 1214 | "enc.aud.bps.min", mAudioEncoder); |
| 1215 | if (mAudioBitRate < minAudioBitRate) { |
| 1216 | LOGW("Intended audio encoding bit rate (%d) is too small" |
| 1217 | " and will be set to (%d)", mAudioBitRate, minAudioBitRate); |
| 1218 | mAudioBitRate = minAudioBitRate; |
| 1219 | } |
| 1220 | |
| 1221 | int maxAudioBitRate = |
| 1222 | mEncoderProfiles->getAudioEncoderParamByName( |
| 1223 | "enc.aud.bps.max", mAudioEncoder); |
| 1224 | if (mAudioBitRate > maxAudioBitRate) { |
| 1225 | LOGW("Intended audio encoding bit rate (%d) is too large" |
| 1226 | " and will be set to (%d)", mAudioBitRate, maxAudioBitRate); |
| 1227 | mAudioBitRate = maxAudioBitRate; |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | void StagefrightRecorder::clipAudioSampleRate() { |
| 1232 | LOGV("clipAudioSampleRate: encoder %d", mAudioEncoder); |
| 1233 | |
| 1234 | int minSampleRate = |
| 1235 | mEncoderProfiles->getAudioEncoderParamByName( |
| 1236 | "enc.aud.hz.min", mAudioEncoder); |
| 1237 | if (mSampleRate < minSampleRate) { |
| 1238 | LOGW("Intended audio sample rate (%d) is too small" |
| 1239 | " and will be set to (%d)", mSampleRate, minSampleRate); |
| 1240 | mSampleRate = minSampleRate; |
| 1241 | } |
| 1242 | |
| 1243 | int maxSampleRate = |
| 1244 | mEncoderProfiles->getAudioEncoderParamByName( |
| 1245 | "enc.aud.hz.max", mAudioEncoder); |
| 1246 | if (mSampleRate > maxSampleRate) { |
| 1247 | LOGW("Intended audio sample rate (%d) is too large" |
| 1248 | " and will be set to (%d)", mSampleRate, maxSampleRate); |
| 1249 | mSampleRate = maxSampleRate; |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | void StagefrightRecorder::clipNumberOfAudioChannels() { |
| 1254 | LOGV("clipNumberOfAudioChannels: encoder %d", mAudioEncoder); |
| 1255 | |
| 1256 | int minChannels = |
| 1257 | mEncoderProfiles->getAudioEncoderParamByName( |
| 1258 | "enc.aud.ch.min", mAudioEncoder); |
| 1259 | if (mAudioChannels < minChannels) { |
| 1260 | LOGW("Intended number of audio channels (%d) is too small" |
| 1261 | " and will be set to (%d)", mAudioChannels, minChannels); |
| 1262 | mAudioChannels = minChannels; |
| 1263 | } |
| 1264 | |
| 1265 | int maxChannels = |
| 1266 | mEncoderProfiles->getAudioEncoderParamByName( |
| 1267 | "enc.aud.ch.max", mAudioEncoder); |
| 1268 | if (mAudioChannels > maxChannels) { |
| 1269 | LOGW("Intended number of audio channels (%d) is too large" |
| 1270 | " and will be set to (%d)", mAudioChannels, maxChannels); |
| 1271 | mAudioChannels = maxChannels; |
| 1272 | } |
| 1273 | } |
| 1274 | |
James Dong | 42a18c0 | 2010-06-16 17:27:46 -0700 | [diff] [blame] | 1275 | void StagefrightRecorder::clipVideoFrameHeight() { |
| 1276 | LOGV("clipVideoFrameHeight: encoder %d", mVideoEncoder); |
| 1277 | int minFrameHeight = mEncoderProfiles->getVideoEncoderParamByName( |
| 1278 | "enc.vid.height.min", mVideoEncoder); |
| 1279 | int maxFrameHeight = mEncoderProfiles->getVideoEncoderParamByName( |
| 1280 | "enc.vid.height.max", mVideoEncoder); |
| 1281 | if (mVideoHeight < minFrameHeight) { |
| 1282 | LOGW("Intended video encoding frame height (%d) is too small" |
| 1283 | " and will be set to (%d)", mVideoHeight, minFrameHeight); |
| 1284 | mVideoHeight = minFrameHeight; |
| 1285 | } else if (mVideoHeight > maxFrameHeight) { |
| 1286 | LOGW("Intended video encoding frame height (%d) is too large" |
| 1287 | " and will be set to (%d)", mVideoHeight, maxFrameHeight); |
| 1288 | mVideoHeight = maxFrameHeight; |
| 1289 | } |
| 1290 | } |
| 1291 | |
James Dong | 05c2fd5 | 2010-11-02 13:20:11 -0700 | [diff] [blame] | 1292 | status_t StagefrightRecorder::setupCameraSource( |
| 1293 | sp<CameraSource> *cameraSource) { |
James Dong | b15f2ea | 2010-10-24 09:32:39 -0700 | [diff] [blame] | 1294 | status_t err = OK; |
| 1295 | if ((err = checkVideoEncoderCapabilities()) != OK) { |
| 1296 | return err; |
| 1297 | } |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 1298 | Size videoSize; |
| 1299 | videoSize.width = mVideoWidth; |
| 1300 | videoSize.height = mVideoHeight; |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 1301 | if (mCaptureTimeLapse) { |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 1302 | mCameraSourceTimeLapse = CameraSourceTimeLapse::CreateFromCamera( |
Wu-cheng Li | 42419ce | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 1303 | mCamera, mCameraProxy, mCameraId, |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 1304 | videoSize, mFrameRate, mPreviewSurface, |
| 1305 | mTimeBetweenTimeLapseFrameCaptureUs); |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 1306 | *cameraSource = mCameraSourceTimeLapse; |
| 1307 | } else { |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 1308 | *cameraSource = CameraSource::CreateFromCamera( |
Wu-cheng Li | 42419ce | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 1309 | mCamera, mCameraProxy, mCameraId, videoSize, mFrameRate, |
James Dong | 05c2fd5 | 2010-11-02 13:20:11 -0700 | [diff] [blame] | 1310 | mPreviewSurface, true /*storeMetaDataInVideoBuffers*/); |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 1311 | } |
Wu-cheng Li | 42419ce | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 1312 | mCamera.clear(); |
| 1313 | mCameraProxy.clear(); |
James Dong | 5df53fe | 2010-12-05 14:25:34 -0800 | [diff] [blame] | 1314 | if (*cameraSource == NULL) { |
| 1315 | return UNKNOWN_ERROR; |
| 1316 | } |
| 1317 | |
| 1318 | if ((*cameraSource)->initCheck() != OK) { |
| 1319 | (*cameraSource).clear(); |
| 1320 | *cameraSource = NULL; |
| 1321 | return NO_INIT; |
| 1322 | } |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1323 | |
James Dong | b15f2ea | 2010-10-24 09:32:39 -0700 | [diff] [blame] | 1324 | // When frame rate is not set, the actual frame rate will be set to |
| 1325 | // the current frame rate being used. |
| 1326 | if (mFrameRate == -1) { |
| 1327 | int32_t frameRate = 0; |
| 1328 | CHECK ((*cameraSource)->getFormat()->findInt32( |
James Dong | aac193c | 2010-11-10 20:43:53 -0800 | [diff] [blame] | 1329 | kKeyFrameRate, &frameRate)); |
James Dong | b15f2ea | 2010-10-24 09:32:39 -0700 | [diff] [blame] | 1330 | LOGI("Frame rate is not explicitly set. Use the current frame " |
| 1331 | "rate (%d fps)", frameRate); |
| 1332 | mFrameRate = frameRate; |
| 1333 | } |
| 1334 | |
| 1335 | CHECK(mFrameRate != -1); |
James Dong | 05c2fd5 | 2010-11-02 13:20:11 -0700 | [diff] [blame] | 1336 | |
| 1337 | mIsMetaDataStoredInVideoBuffers = |
| 1338 | (*cameraSource)->isMetaDataStoredInVideoBuffers(); |
| 1339 | |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1340 | return OK; |
| 1341 | } |
| 1342 | |
| 1343 | status_t StagefrightRecorder::setupVideoEncoder( |
| 1344 | sp<MediaSource> cameraSource, |
| 1345 | int32_t videoBitRate, |
| 1346 | sp<MediaSource> *source) { |
| 1347 | source->clear(); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1348 | |
| 1349 | sp<MetaData> enc_meta = new MetaData; |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1350 | enc_meta->setInt32(kKeyBitRate, videoBitRate); |
James Dong | aac193c | 2010-11-10 20:43:53 -0800 | [diff] [blame] | 1351 | enc_meta->setInt32(kKeyFrameRate, mFrameRate); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1352 | |
| 1353 | switch (mVideoEncoder) { |
| 1354 | case VIDEO_ENCODER_H263: |
| 1355 | enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263); |
| 1356 | break; |
| 1357 | |
| 1358 | case VIDEO_ENCODER_MPEG_4_SP: |
| 1359 | enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4); |
| 1360 | break; |
| 1361 | |
| 1362 | case VIDEO_ENCODER_H264: |
| 1363 | enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC); |
| 1364 | break; |
| 1365 | |
| 1366 | default: |
| 1367 | CHECK(!"Should not be here, unsupported video encoding."); |
| 1368 | break; |
| 1369 | } |
| 1370 | |
| 1371 | sp<MetaData> meta = cameraSource->getFormat(); |
| 1372 | |
James Dong | 1cc31e6 | 2010-07-02 17:44:44 -0700 | [diff] [blame] | 1373 | int32_t width, height, stride, sliceHeight, colorFormat; |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1374 | CHECK(meta->findInt32(kKeyWidth, &width)); |
| 1375 | CHECK(meta->findInt32(kKeyHeight, &height)); |
| 1376 | CHECK(meta->findInt32(kKeyStride, &stride)); |
| 1377 | CHECK(meta->findInt32(kKeySliceHeight, &sliceHeight)); |
James Dong | 1cc31e6 | 2010-07-02 17:44:44 -0700 | [diff] [blame] | 1378 | CHECK(meta->findInt32(kKeyColorFormat, &colorFormat)); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1379 | |
| 1380 | enc_meta->setInt32(kKeyWidth, width); |
| 1381 | enc_meta->setInt32(kKeyHeight, height); |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 1382 | enc_meta->setInt32(kKeyIFramesInterval, mIFramesIntervalSec); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1383 | enc_meta->setInt32(kKeyStride, stride); |
| 1384 | enc_meta->setInt32(kKeySliceHeight, sliceHeight); |
James Dong | 1cc31e6 | 2010-07-02 17:44:44 -0700 | [diff] [blame] | 1385 | enc_meta->setInt32(kKeyColorFormat, colorFormat); |
James Dong | eff30e3 | 2010-08-13 14:16:26 -0700 | [diff] [blame] | 1386 | if (mVideoTimeScale > 0) { |
| 1387 | enc_meta->setInt32(kKeyTimeScale, mVideoTimeScale); |
| 1388 | } |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 1389 | if (mVideoEncoderProfile != -1) { |
| 1390 | enc_meta->setInt32(kKeyVideoProfile, mVideoEncoderProfile); |
| 1391 | } |
| 1392 | if (mVideoEncoderLevel != -1) { |
| 1393 | enc_meta->setInt32(kKeyVideoLevel, mVideoEncoderLevel); |
| 1394 | } |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1395 | |
| 1396 | OMXClient client; |
| 1397 | CHECK_EQ(client.connect(), OK); |
| 1398 | |
James Dong | 6312dd6 | 2010-12-02 14:48:23 -0800 | [diff] [blame] | 1399 | uint32_t encoder_flags = 0; |
James Dong | 5fb60c7 | 2011-01-18 21:12:31 -0800 | [diff] [blame] | 1400 | if (mIsMetaDataStoredInVideoBuffers) { |
James Dong | 6312dd6 | 2010-12-02 14:48:23 -0800 | [diff] [blame] | 1401 | encoder_flags |= OMXCodec::kHardwareCodecsOnly; |
James Dong | 05c2fd5 | 2010-11-02 13:20:11 -0700 | [diff] [blame] | 1402 | encoder_flags |= OMXCodec::kStoreMetaDataInVideoBuffers; |
| 1403 | } |
James Dong | 5f3ab06 | 2011-01-25 16:31:28 -0800 | [diff] [blame] | 1404 | |
| 1405 | // Do not wait for all the input buffers to become available. |
| 1406 | // This give timelapse video recording faster response in |
| 1407 | // receiving output from video encoder component. |
| 1408 | if (mCaptureTimeLapse) { |
| 1409 | encoder_flags |= OMXCodec::kOnlySubmitOneInputBufferAtOneTime; |
| 1410 | } |
| 1411 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1412 | sp<MediaSource> encoder = OMXCodec::Create( |
| 1413 | client.interface(), enc_meta, |
Nipun Kwatra | 077cba4 | 2010-07-20 11:37:17 -0700 | [diff] [blame] | 1414 | true /* createEncoder */, cameraSource, |
| 1415 | NULL, encoder_flags); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1416 | if (encoder == NULL) { |
James Dong | 6312dd6 | 2010-12-02 14:48:23 -0800 | [diff] [blame] | 1417 | LOGW("Failed to create the encoder"); |
| 1418 | // When the encoder fails to be created, we need |
| 1419 | // release the camera source due to the camera's lock |
| 1420 | // and unlock mechanism. |
| 1421 | cameraSource->stop(); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1422 | return UNKNOWN_ERROR; |
| 1423 | } |
| 1424 | |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 1425 | *source = encoder; |
| 1426 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1427 | return OK; |
| 1428 | } |
| 1429 | |
| 1430 | status_t StagefrightRecorder::setupAudioEncoder(const sp<MediaWriter>& writer) { |
James Dong | 54815a7 | 2011-01-12 20:45:16 -0800 | [diff] [blame] | 1431 | status_t status = BAD_VALUE; |
| 1432 | if (OK != (status = checkAudioEncoderCapabilities())) { |
| 1433 | return status; |
| 1434 | } |
| 1435 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1436 | switch(mAudioEncoder) { |
| 1437 | case AUDIO_ENCODER_AMR_NB: |
| 1438 | case AUDIO_ENCODER_AMR_WB: |
| 1439 | case AUDIO_ENCODER_AAC: |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1440 | break; |
James Dong | 54815a7 | 2011-01-12 20:45:16 -0800 | [diff] [blame] | 1441 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1442 | default: |
| 1443 | LOGE("Unsupported audio encoder: %d", mAudioEncoder); |
| 1444 | return UNKNOWN_ERROR; |
| 1445 | } |
| 1446 | |
James Dong | 54815a7 | 2011-01-12 20:45:16 -0800 | [diff] [blame] | 1447 | sp<MediaSource> audioEncoder = createAudioSource(); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1448 | if (audioEncoder == NULL) { |
| 1449 | return UNKNOWN_ERROR; |
| 1450 | } |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 1451 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1452 | writer->addSource(audioEncoder); |
| 1453 | return OK; |
| 1454 | } |
| 1455 | |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1456 | status_t StagefrightRecorder::setupMPEG4Recording( |
| 1457 | bool useSplitCameraSource, |
Nipun Kwatra | 239f2e5 | 2010-08-31 15:35:44 -0700 | [diff] [blame] | 1458 | int outputFd, |
| 1459 | int32_t videoWidth, int32_t videoHeight, |
| 1460 | int32_t videoBitRate, |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1461 | int32_t *totalBitRate, |
| 1462 | sp<MediaWriter> *mediaWriter) { |
| 1463 | mediaWriter->clear(); |
| 1464 | *totalBitRate = 0; |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1465 | status_t err = OK; |
James Dong | 2747e0e | 2010-11-18 20:59:13 -0800 | [diff] [blame] | 1466 | sp<MediaWriter> writer = new MPEG4Writer(outputFd); |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 1467 | |
| 1468 | if (mVideoSource == VIDEO_SOURCE_DEFAULT |
| 1469 | || mVideoSource == VIDEO_SOURCE_CAMERA) { |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1470 | |
| 1471 | sp<MediaSource> cameraMediaSource; |
| 1472 | if (useSplitCameraSource) { |
| 1473 | LOGV("Using Split camera source"); |
| 1474 | cameraMediaSource = mCameraSourceSplitter->createClient(); |
| 1475 | } else { |
| 1476 | sp<CameraSource> cameraSource; |
| 1477 | err = setupCameraSource(&cameraSource); |
| 1478 | cameraMediaSource = cameraSource; |
| 1479 | } |
Nipun Kwatra | 239f2e5 | 2010-08-31 15:35:44 -0700 | [diff] [blame] | 1480 | if ((videoWidth != mVideoWidth) || (videoHeight != mVideoHeight)) { |
| 1481 | // Use downsampling from the original source. |
Nipun Kwatra | 239f2e5 | 2010-08-31 15:35:44 -0700 | [diff] [blame] | 1482 | cameraMediaSource = |
| 1483 | new VideoSourceDownSampler(cameraMediaSource, videoWidth, videoHeight); |
| 1484 | } |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1485 | if (err != OK) { |
| 1486 | return err; |
| 1487 | } |
| 1488 | |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 1489 | sp<MediaSource> encoder; |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1490 | err = setupVideoEncoder(cameraMediaSource, videoBitRate, &encoder); |
| 1491 | if (err != OK) { |
| 1492 | return err; |
| 1493 | } |
| 1494 | |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 1495 | writer->addSource(encoder); |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1496 | *totalBitRate += videoBitRate; |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1497 | } |
| 1498 | |
James Dong | 44b004b | 2011-01-19 20:42:19 -0800 | [diff] [blame] | 1499 | // Audio source is added at the end if it exists. |
| 1500 | // This help make sure that the "recoding" sound is suppressed for |
| 1501 | // camcorder applications in the recorded files. |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1502 | if (!mCaptureTimeLapse && (mAudioSource != AUDIO_SOURCE_CNT)) { |
James Dong | 44b004b | 2011-01-19 20:42:19 -0800 | [diff] [blame] | 1503 | err = setupAudioEncoder(writer); |
| 1504 | if (err != OK) return err; |
| 1505 | *totalBitRate += mAudioBitRate; |
| 1506 | } |
| 1507 | |
James Dong | 63299c0 | 2010-07-28 10:08:03 -0700 | [diff] [blame] | 1508 | if (mInterleaveDurationUs > 0) { |
| 1509 | reinterpret_cast<MPEG4Writer *>(writer.get())-> |
| 1510 | setInterleaveDuration(mInterleaveDurationUs); |
| 1511 | } |
James Dong | 987ab48 | 2011-05-11 19:09:25 -0700 | [diff] [blame] | 1512 | if (mLongitudex10000 > -3600000 && mLatitudex10000 > -3600000) { |
| 1513 | reinterpret_cast<MPEG4Writer *>(writer.get())-> |
| 1514 | setGeoData(mLatitudex10000, mLongitudex10000); |
| 1515 | } |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 1516 | if (mMaxFileDurationUs != 0) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1517 | writer->setMaxFileDuration(mMaxFileDurationUs); |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 1518 | } |
| 1519 | if (mMaxFileSizeBytes != 0) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1520 | writer->setMaxFileSize(mMaxFileSizeBytes); |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 1521 | } |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1522 | |
James Dong | 13a3316 | 2011-05-09 16:56:25 -0700 | [diff] [blame] | 1523 | mStartTimeOffsetMs = mEncoderProfiles->getStartTimeOffsetMs(mCameraId); |
| 1524 | if (mStartTimeOffsetMs > 0) { |
| 1525 | reinterpret_cast<MPEG4Writer *>(writer.get())-> |
| 1526 | setStartTimeOffsetMs(mStartTimeOffsetMs); |
| 1527 | } |
| 1528 | |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1529 | writer->setListener(mListener); |
| 1530 | *mediaWriter = writer; |
| 1531 | return OK; |
| 1532 | } |
| 1533 | |
| 1534 | void StagefrightRecorder::setupMPEG4MetaData(int64_t startTimeUs, int32_t totalBitRate, |
| 1535 | sp<MetaData> *meta) { |
| 1536 | (*meta)->setInt64(kKeyTime, startTimeUs); |
| 1537 | (*meta)->setInt32(kKeyFileType, mOutputFormat); |
| 1538 | (*meta)->setInt32(kKeyBitRate, totalBitRate); |
| 1539 | (*meta)->setInt32(kKey64BitFileOffset, mUse64BitFileOffset); |
James Dong | eff30e3 | 2010-08-13 14:16:26 -0700 | [diff] [blame] | 1540 | if (mMovieTimeScale > 0) { |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1541 | (*meta)->setInt32(kKeyTimeScale, mMovieTimeScale); |
James Dong | eff30e3 | 2010-08-13 14:16:26 -0700 | [diff] [blame] | 1542 | } |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 1543 | if (mTrackEveryTimeDurationUs > 0) { |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1544 | (*meta)->setInt64(kKeyTrackTimeStatus, mTrackEveryTimeDurationUs); |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 1545 | } |
James Dong | b9d7e01 | 2010-11-09 11:15:47 -0800 | [diff] [blame] | 1546 | if (mRotationDegrees != 0) { |
| 1547 | (*meta)->setInt32(kKeyRotation, mRotationDegrees); |
| 1548 | } |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | status_t StagefrightRecorder::startMPEG4Recording() { |
| 1552 | if (mCaptureAuxVideo) { |
Nipun Kwatra | fb5a2d7 | 2010-09-13 18:52:55 -0700 | [diff] [blame] | 1553 | if (!mCaptureTimeLapse) { |
| 1554 | LOGE("Auxiliary video can be captured only in time lapse mode"); |
| 1555 | return UNKNOWN_ERROR; |
| 1556 | } |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1557 | LOGV("Creating MediaSourceSplitter"); |
| 1558 | sp<CameraSource> cameraSource; |
| 1559 | status_t err = setupCameraSource(&cameraSource); |
| 1560 | if (err != OK) { |
| 1561 | return err; |
| 1562 | } |
| 1563 | mCameraSourceSplitter = new MediaSourceSplitter(cameraSource); |
| 1564 | } else { |
| 1565 | mCameraSourceSplitter = NULL; |
| 1566 | } |
| 1567 | |
| 1568 | int32_t totalBitRate; |
| 1569 | status_t err = setupMPEG4Recording(mCaptureAuxVideo, |
Nipun Kwatra | 239f2e5 | 2010-08-31 15:35:44 -0700 | [diff] [blame] | 1570 | mOutputFd, mVideoWidth, mVideoHeight, |
| 1571 | mVideoBitRate, &totalBitRate, &mWriter); |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1572 | if (err != OK) { |
| 1573 | return err; |
| 1574 | } |
| 1575 | |
| 1576 | int64_t startTimeUs = systemTime() / 1000; |
| 1577 | sp<MetaData> meta = new MetaData; |
| 1578 | setupMPEG4MetaData(startTimeUs, totalBitRate, &meta); |
| 1579 | |
| 1580 | err = mWriter->start(meta.get()); |
| 1581 | if (err != OK) { |
| 1582 | return err; |
| 1583 | } |
| 1584 | |
| 1585 | if (mCaptureAuxVideo) { |
| 1586 | CHECK(mOutputFdAux >= 0); |
| 1587 | if (mWriterAux != NULL) { |
| 1588 | LOGE("Auxiliary File writer is not avaialble"); |
| 1589 | return UNKNOWN_ERROR; |
| 1590 | } |
Nipun Kwatra | fb5a2d7 | 2010-09-13 18:52:55 -0700 | [diff] [blame] | 1591 | if ((mAuxVideoWidth > mVideoWidth) || (mAuxVideoHeight > mVideoHeight) || |
| 1592 | ((mAuxVideoWidth == mVideoWidth) && mAuxVideoHeight == mVideoHeight)) { |
| 1593 | LOGE("Auxiliary video size (%d x %d) same or larger than the main video size (%d x %d)", |
| 1594 | mAuxVideoWidth, mAuxVideoHeight, mVideoWidth, mVideoHeight); |
| 1595 | return UNKNOWN_ERROR; |
| 1596 | } |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1597 | |
| 1598 | int32_t totalBitrateAux; |
| 1599 | err = setupMPEG4Recording(mCaptureAuxVideo, |
Nipun Kwatra | 239f2e5 | 2010-08-31 15:35:44 -0700 | [diff] [blame] | 1600 | mOutputFdAux, mAuxVideoWidth, mAuxVideoHeight, |
| 1601 | mAuxVideoBitRate, &totalBitrateAux, &mWriterAux); |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1602 | if (err != OK) { |
| 1603 | return err; |
| 1604 | } |
| 1605 | |
| 1606 | sp<MetaData> metaAux = new MetaData; |
| 1607 | setupMPEG4MetaData(startTimeUs, totalBitrateAux, &metaAux); |
| 1608 | |
| 1609 | return mWriterAux->start(metaAux.get()); |
| 1610 | } |
| 1611 | |
| 1612 | return OK; |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1613 | } |
| 1614 | |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 1615 | status_t StagefrightRecorder::pause() { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1616 | LOGV("pause"); |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 1617 | if (mWriter == NULL) { |
| 1618 | return UNKNOWN_ERROR; |
| 1619 | } |
| 1620 | mWriter->pause(); |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1621 | |
| 1622 | if (mCaptureAuxVideo) { |
| 1623 | if (mWriterAux == NULL) { |
| 1624 | return UNKNOWN_ERROR; |
| 1625 | } |
| 1626 | mWriterAux->pause(); |
| 1627 | } |
| 1628 | |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 1629 | if (mStarted) { |
| 1630 | mStarted = false; |
| 1631 | |
| 1632 | uint32_t params = 0; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1633 | if (mAudioSource != AUDIO_SOURCE_CNT) { |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 1634 | params |= IMediaPlayerService::kBatteryDataTrackAudio; |
| 1635 | } |
| 1636 | if (mVideoSource != VIDEO_SOURCE_LIST_END) { |
| 1637 | params |= IMediaPlayerService::kBatteryDataTrackVideo; |
| 1638 | } |
| 1639 | |
| 1640 | addBatteryData(params); |
| 1641 | } |
| 1642 | |
| 1643 | |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 1644 | return OK; |
| 1645 | } |
| 1646 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1647 | status_t StagefrightRecorder::stop() { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1648 | LOGV("stop"); |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame] | 1649 | status_t err = OK; |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1650 | |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 1651 | if (mCaptureTimeLapse && mCameraSourceTimeLapse != NULL) { |
| 1652 | mCameraSourceTimeLapse->startQuickReadReturns(); |
| 1653 | mCameraSourceTimeLapse = NULL; |
| 1654 | } |
| 1655 | |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1656 | if (mCaptureAuxVideo) { |
| 1657 | if (mWriterAux != NULL) { |
| 1658 | mWriterAux->stop(); |
| 1659 | mWriterAux.clear(); |
| 1660 | } |
| 1661 | } |
| 1662 | |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1663 | if (mWriter != NULL) { |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame] | 1664 | err = mWriter->stop(); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1665 | mWriter.clear(); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1666 | } |
| 1667 | |
James Dong | c6280bc | 2010-08-11 17:12:39 -0700 | [diff] [blame] | 1668 | if (mOutputFd >= 0) { |
| 1669 | ::close(mOutputFd); |
| 1670 | mOutputFd = -1; |
| 1671 | } |
| 1672 | |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1673 | if (mCaptureAuxVideo) { |
| 1674 | if (mOutputFdAux >= 0) { |
| 1675 | ::close(mOutputFdAux); |
| 1676 | mOutputFdAux = -1; |
| 1677 | } |
| 1678 | } |
| 1679 | |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 1680 | if (mStarted) { |
| 1681 | mStarted = false; |
| 1682 | |
| 1683 | uint32_t params = 0; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1684 | if (mAudioSource != AUDIO_SOURCE_CNT) { |
Gloria Wang | 62e05a6 | 2011-02-23 11:47:34 -0800 | [diff] [blame] | 1685 | params |= IMediaPlayerService::kBatteryDataTrackAudio; |
| 1686 | } |
| 1687 | if (mVideoSource != VIDEO_SOURCE_LIST_END) { |
| 1688 | params |= IMediaPlayerService::kBatteryDataTrackVideo; |
| 1689 | } |
| 1690 | |
| 1691 | addBatteryData(params); |
| 1692 | } |
| 1693 | |
| 1694 | |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame] | 1695 | return err; |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | status_t StagefrightRecorder::close() { |
| 1699 | LOGV("close"); |
| 1700 | stop(); |
| 1701 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1702 | return OK; |
| 1703 | } |
| 1704 | |
| 1705 | status_t StagefrightRecorder::reset() { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1706 | LOGV("reset"); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1707 | stop(); |
| 1708 | |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 1709 | // No audio or video source by default |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1710 | mAudioSource = AUDIO_SOURCE_CNT; |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1711 | mVideoSource = VIDEO_SOURCE_LIST_END; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 1712 | |
| 1713 | // Default parameters |
| 1714 | mOutputFormat = OUTPUT_FORMAT_THREE_GPP; |
| 1715 | mAudioEncoder = AUDIO_ENCODER_AMR_NB; |
| 1716 | mVideoEncoder = VIDEO_ENCODER_H263; |
| 1717 | mVideoWidth = 176; |
| 1718 | mVideoHeight = 144; |
Nipun Kwatra | 239f2e5 | 2010-08-31 15:35:44 -0700 | [diff] [blame] | 1719 | mAuxVideoWidth = 176; |
| 1720 | mAuxVideoHeight = 144; |
James Dong | b15f2ea | 2010-10-24 09:32:39 -0700 | [diff] [blame] | 1721 | mFrameRate = -1; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 1722 | mVideoBitRate = 192000; |
Nipun Kwatra | 239f2e5 | 2010-08-31 15:35:44 -0700 | [diff] [blame] | 1723 | mAuxVideoBitRate = 192000; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 1724 | mSampleRate = 8000; |
| 1725 | mAudioChannels = 1; |
| 1726 | mAudioBitRate = 12200; |
James Dong | c616172 | 2010-05-20 17:55:52 -0700 | [diff] [blame] | 1727 | mInterleaveDurationUs = 0; |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 1728 | mIFramesIntervalSec = 1; |
James Dong | 57e7f83 | 2010-06-24 19:55:31 -0700 | [diff] [blame] | 1729 | mAudioSourceNode = 0; |
James Dong | 6feaa46 | 2010-06-20 08:20:54 -0700 | [diff] [blame] | 1730 | mUse64BitFileOffset = false; |
James Dong | eff30e3 | 2010-08-13 14:16:26 -0700 | [diff] [blame] | 1731 | mMovieTimeScale = -1; |
| 1732 | mAudioTimeScale = -1; |
| 1733 | mVideoTimeScale = -1; |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 1734 | mCameraId = 0; |
James Dong | 13a3316 | 2011-05-09 16:56:25 -0700 | [diff] [blame] | 1735 | mStartTimeOffsetMs = -1; |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 1736 | mVideoEncoderProfile = -1; |
| 1737 | mVideoEncoderLevel = -1; |
| 1738 | mMaxFileDurationUs = 0; |
| 1739 | mMaxFileSizeBytes = 0; |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 1740 | mTrackEveryTimeDurationUs = 0; |
Nipun Kwatra | d26920a | 2010-06-30 18:51:31 -0700 | [diff] [blame] | 1741 | mCaptureTimeLapse = false; |
Nipun Kwatra | d01371b | 2010-07-20 21:33:31 -0700 | [diff] [blame] | 1742 | mTimeBetweenTimeLapseFrameCaptureUs = -1; |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1743 | mCaptureAuxVideo = false; |
| 1744 | mCameraSourceSplitter = NULL; |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 1745 | mCameraSourceTimeLapse = NULL; |
James Dong | 05c2fd5 | 2010-11-02 13:20:11 -0700 | [diff] [blame] | 1746 | mIsMetaDataStoredInVideoBuffers = false; |
James Dong | 42a18c0 | 2010-06-16 17:27:46 -0700 | [diff] [blame] | 1747 | mEncoderProfiles = MediaProfiles::getInstance(); |
James Dong | 5d0b783 | 2010-11-10 12:26:58 -0800 | [diff] [blame] | 1748 | mRotationDegrees = 0; |
James Dong | 987ab48 | 2011-05-11 19:09:25 -0700 | [diff] [blame] | 1749 | mLatitudex10000 = -3600000; |
| 1750 | mLongitudex10000 = -3600000; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 1751 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1752 | mOutputFd = -1; |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1753 | mOutputFdAux = -1; |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1754 | |
| 1755 | return OK; |
| 1756 | } |
| 1757 | |
| 1758 | status_t StagefrightRecorder::getMaxAmplitude(int *max) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1759 | LOGV("getMaxAmplitude"); |
| 1760 | |
| 1761 | if (max == NULL) { |
| 1762 | LOGE("Null pointer argument"); |
| 1763 | return BAD_VALUE; |
| 1764 | } |
| 1765 | |
James Dong | 57e7f83 | 2010-06-24 19:55:31 -0700 | [diff] [blame] | 1766 | if (mAudioSourceNode != 0) { |
| 1767 | *max = mAudioSourceNode->getMaxAmplitude(); |
| 1768 | } else { |
| 1769 | *max = 0; |
| 1770 | } |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 1771 | |
| 1772 | return OK; |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1773 | } |
| 1774 | |
James Dong | 3f51fa7 | 2010-08-18 03:32:26 -0700 | [diff] [blame] | 1775 | status_t StagefrightRecorder::dump( |
| 1776 | int fd, const Vector<String16>& args) const { |
| 1777 | LOGV("dump"); |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 1778 | const size_t SIZE = 256; |
| 1779 | char buffer[SIZE]; |
| 1780 | String8 result; |
James Dong | 3f51fa7 | 2010-08-18 03:32:26 -0700 | [diff] [blame] | 1781 | if (mWriter != 0) { |
| 1782 | mWriter->dump(fd, args); |
| 1783 | } else { |
| 1784 | snprintf(buffer, SIZE, " No file writer\n"); |
| 1785 | result.append(buffer); |
| 1786 | } |
| 1787 | snprintf(buffer, SIZE, " Recorder: %p\n", this); |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 1788 | snprintf(buffer, SIZE, " Output file (fd %d):\n", mOutputFd); |
| 1789 | result.append(buffer); |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1790 | snprintf(buffer, SIZE, " Output file Auxiliary (fd %d):\n", mOutputFdAux); |
| 1791 | result.append(buffer); |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 1792 | snprintf(buffer, SIZE, " File format: %d\n", mOutputFormat); |
| 1793 | result.append(buffer); |
| 1794 | snprintf(buffer, SIZE, " Max file size (bytes): %lld\n", mMaxFileSizeBytes); |
| 1795 | result.append(buffer); |
| 1796 | snprintf(buffer, SIZE, " Max file duration (us): %lld\n", mMaxFileDurationUs); |
| 1797 | result.append(buffer); |
| 1798 | snprintf(buffer, SIZE, " File offset length (bits): %d\n", mUse64BitFileOffset? 64: 32); |
| 1799 | result.append(buffer); |
| 1800 | snprintf(buffer, SIZE, " Interleave duration (us): %d\n", mInterleaveDurationUs); |
| 1801 | result.append(buffer); |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 1802 | snprintf(buffer, SIZE, " Progress notification: %lld us\n", mTrackEveryTimeDurationUs); |
| 1803 | result.append(buffer); |
| 1804 | snprintf(buffer, SIZE, " Audio\n"); |
| 1805 | result.append(buffer); |
| 1806 | snprintf(buffer, SIZE, " Source: %d\n", mAudioSource); |
| 1807 | result.append(buffer); |
| 1808 | snprintf(buffer, SIZE, " Encoder: %d\n", mAudioEncoder); |
| 1809 | result.append(buffer); |
| 1810 | snprintf(buffer, SIZE, " Bit rate (bps): %d\n", mAudioBitRate); |
| 1811 | result.append(buffer); |
| 1812 | snprintf(buffer, SIZE, " Sampling rate (hz): %d\n", mSampleRate); |
| 1813 | result.append(buffer); |
| 1814 | snprintf(buffer, SIZE, " Number of channels: %d\n", mAudioChannels); |
| 1815 | result.append(buffer); |
| 1816 | snprintf(buffer, SIZE, " Max amplitude: %d\n", mAudioSourceNode == 0? 0: mAudioSourceNode->getMaxAmplitude()); |
| 1817 | result.append(buffer); |
| 1818 | snprintf(buffer, SIZE, " Video\n"); |
| 1819 | result.append(buffer); |
| 1820 | snprintf(buffer, SIZE, " Source: %d\n", mVideoSource); |
| 1821 | result.append(buffer); |
| 1822 | snprintf(buffer, SIZE, " Camera Id: %d\n", mCameraId); |
| 1823 | result.append(buffer); |
James Dong | 13a3316 | 2011-05-09 16:56:25 -0700 | [diff] [blame] | 1824 | snprintf(buffer, SIZE, " Start time offset (ms): %d\n", mStartTimeOffsetMs); |
| 1825 | result.append(buffer); |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 1826 | snprintf(buffer, SIZE, " Encoder: %d\n", mVideoEncoder); |
| 1827 | result.append(buffer); |
| 1828 | snprintf(buffer, SIZE, " Encoder profile: %d\n", mVideoEncoderProfile); |
| 1829 | result.append(buffer); |
| 1830 | snprintf(buffer, SIZE, " Encoder level: %d\n", mVideoEncoderLevel); |
| 1831 | result.append(buffer); |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 1832 | snprintf(buffer, SIZE, " I frames interval (s): %d\n", mIFramesIntervalSec); |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 1833 | result.append(buffer); |
| 1834 | snprintf(buffer, SIZE, " Frame size (pixels): %dx%d\n", mVideoWidth, mVideoHeight); |
| 1835 | result.append(buffer); |
Nipun Kwatra | 239f2e5 | 2010-08-31 15:35:44 -0700 | [diff] [blame] | 1836 | snprintf(buffer, SIZE, " Aux Frame size (pixels): %dx%d\n", mAuxVideoWidth, mAuxVideoHeight); |
| 1837 | result.append(buffer); |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 1838 | snprintf(buffer, SIZE, " Frame rate (fps): %d\n", mFrameRate); |
| 1839 | result.append(buffer); |
| 1840 | snprintf(buffer, SIZE, " Bit rate (bps): %d\n", mVideoBitRate); |
| 1841 | result.append(buffer); |
Nipun Kwatra | 239f2e5 | 2010-08-31 15:35:44 -0700 | [diff] [blame] | 1842 | snprintf(buffer, SIZE, " Aux Bit rate (bps): %d\n", mAuxVideoBitRate); |
Nipun Kwatra | d7e7a3f | 2010-08-26 17:05:18 -0700 | [diff] [blame] | 1843 | result.append(buffer); |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 1844 | ::write(fd, result.string(), result.size()); |
| 1845 | return OK; |
| 1846 | } |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1847 | } // namespace android |