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 | |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 23 | #include <binder/IPCThreadState.h> |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 24 | #include <media/stagefright/AudioSource.h> |
| 25 | #include <media/stagefright/AMRWriter.h> |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 26 | #include <media/stagefright/CameraSource.h> |
| 27 | #include <media/stagefright/MPEG4Writer.h> |
| 28 | #include <media/stagefright/MediaDebug.h> |
| 29 | #include <media/stagefright/MediaDefs.h> |
| 30 | #include <media/stagefright/MetaData.h> |
| 31 | #include <media/stagefright/OMXClient.h> |
| 32 | #include <media/stagefright/OMXCodec.h> |
James Dong | 42a18c0 | 2010-06-16 17:27:46 -0700 | [diff] [blame] | 33 | #include <media/MediaProfiles.h> |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 34 | #include <camera/ICamera.h> |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 35 | #include <camera/Camera.h> |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 36 | #include <camera/CameraParameters.h> |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 37 | #include <surfaceflinger/ISurface.h> |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 38 | #include <utils/Errors.h> |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 39 | #include <sys/types.h> |
| 40 | #include <unistd.h> |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 41 | #include <ctype.h> |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 42 | |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 43 | #include "ARTPWriter.h" |
| 44 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 45 | namespace android { |
| 46 | |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 47 | StagefrightRecorder::StagefrightRecorder() |
| 48 | : mWriter(NULL), |
| 49 | mOutputFd(-1) { |
| 50 | |
| 51 | LOGV("Constructor"); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 52 | reset(); |
| 53 | } |
| 54 | |
| 55 | StagefrightRecorder::~StagefrightRecorder() { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 56 | LOGV("Destructor"); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 57 | stop(); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | status_t StagefrightRecorder::init() { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 61 | LOGV("init"); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 62 | return OK; |
| 63 | } |
| 64 | |
| 65 | status_t StagefrightRecorder::setAudioSource(audio_source as) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 66 | LOGV("setAudioSource: %d", as); |
| 67 | if (as < AUDIO_SOURCE_DEFAULT || |
| 68 | as >= AUDIO_SOURCE_LIST_END) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 69 | LOGE("Invalid audio source: %d", as); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 70 | return BAD_VALUE; |
| 71 | } |
| 72 | |
| 73 | if (as == AUDIO_SOURCE_DEFAULT) { |
| 74 | mAudioSource = AUDIO_SOURCE_MIC; |
| 75 | } else { |
| 76 | mAudioSource = as; |
| 77 | } |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 78 | |
| 79 | return OK; |
| 80 | } |
| 81 | |
| 82 | status_t StagefrightRecorder::setVideoSource(video_source vs) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 83 | LOGV("setVideoSource: %d", vs); |
| 84 | if (vs < VIDEO_SOURCE_DEFAULT || |
| 85 | vs >= VIDEO_SOURCE_LIST_END) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 86 | LOGE("Invalid video source: %d", vs); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 87 | return BAD_VALUE; |
| 88 | } |
| 89 | |
| 90 | if (vs == VIDEO_SOURCE_DEFAULT) { |
| 91 | mVideoSource = VIDEO_SOURCE_CAMERA; |
| 92 | } else { |
| 93 | mVideoSource = vs; |
| 94 | } |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 95 | |
| 96 | return OK; |
| 97 | } |
| 98 | |
| 99 | status_t StagefrightRecorder::setOutputFormat(output_format of) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 100 | LOGV("setOutputFormat: %d", of); |
| 101 | if (of < OUTPUT_FORMAT_DEFAULT || |
| 102 | of >= OUTPUT_FORMAT_LIST_END) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 103 | LOGE("Invalid output format: %d", of); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 104 | return BAD_VALUE; |
| 105 | } |
| 106 | |
| 107 | if (of == OUTPUT_FORMAT_DEFAULT) { |
| 108 | mOutputFormat = OUTPUT_FORMAT_THREE_GPP; |
| 109 | } else { |
| 110 | mOutputFormat = of; |
| 111 | } |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 112 | |
| 113 | return OK; |
| 114 | } |
| 115 | |
| 116 | status_t StagefrightRecorder::setAudioEncoder(audio_encoder ae) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 117 | LOGV("setAudioEncoder: %d", ae); |
| 118 | if (ae < AUDIO_ENCODER_DEFAULT || |
| 119 | ae >= AUDIO_ENCODER_LIST_END) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 120 | LOGE("Invalid audio encoder: %d", ae); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 121 | return BAD_VALUE; |
| 122 | } |
| 123 | |
| 124 | if (ae == AUDIO_ENCODER_DEFAULT) { |
| 125 | mAudioEncoder = AUDIO_ENCODER_AMR_NB; |
| 126 | } else { |
| 127 | mAudioEncoder = ae; |
| 128 | } |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 129 | |
| 130 | return OK; |
| 131 | } |
| 132 | |
| 133 | status_t StagefrightRecorder::setVideoEncoder(video_encoder ve) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 134 | LOGV("setVideoEncoder: %d", ve); |
| 135 | if (ve < VIDEO_ENCODER_DEFAULT || |
| 136 | ve >= VIDEO_ENCODER_LIST_END) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 137 | LOGE("Invalid video encoder: %d", ve); |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 138 | return BAD_VALUE; |
| 139 | } |
| 140 | |
| 141 | if (ve == VIDEO_ENCODER_DEFAULT) { |
| 142 | mVideoEncoder = VIDEO_ENCODER_H263; |
| 143 | } else { |
| 144 | mVideoEncoder = ve; |
| 145 | } |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 146 | |
| 147 | return OK; |
| 148 | } |
| 149 | |
| 150 | status_t StagefrightRecorder::setVideoSize(int width, int height) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 151 | LOGV("setVideoSize: %dx%d", width, height); |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 152 | if (width <= 0 || height <= 0) { |
| 153 | LOGE("Invalid video size: %dx%d", width, height); |
| 154 | return BAD_VALUE; |
| 155 | } |
| 156 | |
| 157 | // Additional check on the dimension will be performed later |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 158 | mVideoWidth = width; |
| 159 | mVideoHeight = height; |
| 160 | |
| 161 | return OK; |
| 162 | } |
| 163 | |
| 164 | status_t StagefrightRecorder::setVideoFrameRate(int frames_per_second) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 165 | LOGV("setVideoFrameRate: %d", frames_per_second); |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 166 | if (frames_per_second <= 0 || frames_per_second > 30) { |
| 167 | LOGE("Invalid video frame rate: %d", frames_per_second); |
| 168 | return BAD_VALUE; |
| 169 | } |
| 170 | |
| 171 | // Additional check on the frame rate will be performed later |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 172 | mFrameRate = frames_per_second; |
| 173 | |
| 174 | return OK; |
| 175 | } |
| 176 | |
| 177 | status_t StagefrightRecorder::setCamera(const sp<ICamera> &camera) { |
James Dong | 71d714c | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 178 | LOGV("setCamera"); |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 179 | if (camera == 0) { |
| 180 | LOGE("camera is NULL"); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 181 | return BAD_VALUE; |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 182 | } |
| 183 | |
James Dong | 71d714c | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 184 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 185 | mFlags &= ~FLAGS_HOT_CAMERA; |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 186 | mCamera = Camera::create(camera); |
| 187 | if (mCamera == 0) { |
| 188 | LOGE("Unable to connect to camera"); |
James Dong | 71d714c | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 189 | IPCThreadState::self()->restoreCallingIdentity(token); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 190 | return -EBUSY; |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | LOGV("Connected to camera"); |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 194 | if (mCamera->previewEnabled()) { |
| 195 | LOGV("camera is hot"); |
| 196 | mFlags |= FLAGS_HOT_CAMERA; |
| 197 | } |
James Dong | 71d714c | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 198 | IPCThreadState::self()->restoreCallingIdentity(token); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 199 | |
| 200 | return OK; |
| 201 | } |
| 202 | |
| 203 | status_t StagefrightRecorder::setPreviewSurface(const sp<ISurface> &surface) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 204 | LOGV("setPreviewSurface: %p", surface.get()); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 205 | mPreviewSurface = surface; |
| 206 | |
| 207 | return OK; |
| 208 | } |
| 209 | |
| 210 | status_t StagefrightRecorder::setOutputFile(const char *path) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 211 | LOGE("setOutputFile(const char*) must not be called"); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 212 | // We don't actually support this at all, as the media_server process |
| 213 | // no longer has permissions to create files. |
| 214 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 215 | return -EPERM; |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | status_t StagefrightRecorder::setOutputFile(int fd, int64_t offset, int64_t length) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 219 | LOGV("setOutputFile: %d, %lld, %lld", fd, offset, length); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 220 | // These don't make any sense, do they? |
| 221 | CHECK_EQ(offset, 0); |
| 222 | CHECK_EQ(length, 0); |
| 223 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 224 | if (fd < 0) { |
| 225 | LOGE("Invalid file descriptor: %d", fd); |
| 226 | return -EBADF; |
| 227 | } |
| 228 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 229 | if (mOutputFd >= 0) { |
| 230 | ::close(mOutputFd); |
| 231 | } |
| 232 | mOutputFd = dup(fd); |
| 233 | |
| 234 | return OK; |
| 235 | } |
| 236 | |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 237 | // Attempt to parse an int64 literal optionally surrounded by whitespace, |
| 238 | // returns true on success, false otherwise. |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 239 | static bool safe_strtoi64(const char *s, int64_t *val) { |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 240 | char *end; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 241 | *val = strtoll(s, &end, 10); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 242 | |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 243 | if (end == s || errno == ERANGE) { |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | // Skip trailing whitespace |
| 248 | while (isspace(*end)) { |
| 249 | ++end; |
| 250 | } |
| 251 | |
| 252 | // For a successful return, the string must contain nothing but a valid |
| 253 | // int64 literal optionally surrounded by whitespace. |
| 254 | |
| 255 | return *end == '\0'; |
| 256 | } |
| 257 | |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 258 | // Return true if the value is in [0, 0x007FFFFFFF] |
| 259 | static bool safe_strtoi32(const char *s, int32_t *val) { |
| 260 | int64_t temp; |
| 261 | if (safe_strtoi64(s, &temp)) { |
| 262 | if (temp >= 0 && temp <= 0x007FFFFFFF) { |
| 263 | *val = static_cast<int32_t>(temp); |
| 264 | return true; |
| 265 | } |
| 266 | } |
| 267 | return false; |
| 268 | } |
| 269 | |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 270 | // Trim both leading and trailing whitespace from the given string. |
| 271 | static void TrimString(String8 *s) { |
| 272 | size_t num_bytes = s->bytes(); |
| 273 | const char *data = s->string(); |
| 274 | |
| 275 | size_t leading_space = 0; |
| 276 | while (leading_space < num_bytes && isspace(data[leading_space])) { |
| 277 | ++leading_space; |
| 278 | } |
| 279 | |
| 280 | size_t i = num_bytes; |
| 281 | while (i > leading_space && isspace(data[i - 1])) { |
| 282 | --i; |
| 283 | } |
| 284 | |
| 285 | s->setTo(String8(&data[leading_space], i - leading_space)); |
| 286 | } |
| 287 | |
| 288 | status_t StagefrightRecorder::setParamAudioSamplingRate(int32_t sampleRate) { |
| 289 | LOGV("setParamAudioSamplingRate: %d", sampleRate); |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 290 | if (sampleRate <= 0) { |
| 291 | LOGE("Invalid audio sampling rate: %d", sampleRate); |
| 292 | return BAD_VALUE; |
| 293 | } |
| 294 | |
| 295 | // Additional check on the sample rate will be performed later. |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 296 | mSampleRate = sampleRate; |
| 297 | return OK; |
| 298 | } |
| 299 | |
| 300 | status_t StagefrightRecorder::setParamAudioNumberOfChannels(int32_t channels) { |
| 301 | LOGV("setParamAudioNumberOfChannels: %d", channels); |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 302 | if (channels <= 0 || channels >= 3) { |
| 303 | LOGE("Invalid number of audio channels: %d", channels); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 304 | return BAD_VALUE; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | // Additional check on the number of channels will be performed later. |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 308 | mAudioChannels = channels; |
| 309 | return OK; |
| 310 | } |
| 311 | |
| 312 | status_t StagefrightRecorder::setParamAudioEncodingBitRate(int32_t bitRate) { |
| 313 | LOGV("setParamAudioEncodingBitRate: %d", bitRate); |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 314 | if (bitRate <= 0) { |
| 315 | LOGE("Invalid audio encoding bit rate: %d", bitRate); |
| 316 | return BAD_VALUE; |
| 317 | } |
| 318 | |
| 319 | // The target bit rate may not be exactly the same as the requested. |
| 320 | // It depends on many factors, such as rate control, and the bit rate |
| 321 | // range that a specific encoder supports. The mismatch between the |
| 322 | // 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] | 323 | mAudioBitRate = bitRate; |
| 324 | return OK; |
| 325 | } |
| 326 | |
| 327 | status_t StagefrightRecorder::setParamVideoEncodingBitRate(int32_t bitRate) { |
| 328 | LOGV("setParamVideoEncodingBitRate: %d", bitRate); |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 329 | if (bitRate <= 0) { |
| 330 | LOGE("Invalid video encoding bit rate: %d", bitRate); |
| 331 | return BAD_VALUE; |
| 332 | } |
| 333 | |
| 334 | // The target bit rate may not be exactly the same as the requested. |
| 335 | // It depends on many factors, such as rate control, and the bit rate |
| 336 | // range that a specific encoder supports. The mismatch between the |
| 337 | // 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] | 338 | mVideoBitRate = bitRate; |
| 339 | return OK; |
| 340 | } |
| 341 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 342 | status_t StagefrightRecorder::setParamMaxFileDurationUs(int64_t timeUs) { |
| 343 | LOGV("setParamMaxFileDurationUs: %lld us", timeUs); |
Nipun Kwatra | fb45748 | 2010-08-18 15:19:19 -0700 | [diff] [blame] | 344 | if (timeUs <= 0) { |
| 345 | LOGW("Max file duration is not positive: %lld us. Disabling duration limit.", timeUs); |
| 346 | timeUs = 0; // Disable the duration limit for zero or negative values. |
| 347 | } else if (timeUs <= 100000LL) { // XXX: 100 milli-seconds |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 348 | LOGE("Max file duration is too short: %lld us", timeUs); |
| 349 | return BAD_VALUE; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 350 | } |
Nipun Kwatra | fb45748 | 2010-08-18 15:19:19 -0700 | [diff] [blame] | 351 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 352 | mMaxFileDurationUs = timeUs; |
| 353 | return OK; |
| 354 | } |
| 355 | |
| 356 | status_t StagefrightRecorder::setParamMaxFileSizeBytes(int64_t bytes) { |
| 357 | LOGV("setParamMaxFileSizeBytes: %lld bytes", bytes); |
| 358 | if (bytes <= 1024) { // XXX: 1 kB |
| 359 | LOGE("Max file size is too small: %lld bytes", bytes); |
| 360 | return BAD_VALUE; |
| 361 | } |
| 362 | mMaxFileSizeBytes = bytes; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 363 | return OK; |
| 364 | } |
| 365 | |
James Dong | 3300e96 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 366 | status_t StagefrightRecorder::setParamInterleaveDuration(int32_t durationUs) { |
| 367 | LOGV("setParamInterleaveDuration: %d", durationUs); |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 368 | if (durationUs <= 500000) { // 500 ms |
| 369 | // If interleave duration is too small, it is very inefficient to do |
| 370 | // interleaving since the metadata overhead will count for a significant |
| 371 | // portion of the saved contents |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 372 | LOGE("Audio/video interleave duration is too small: %d us", durationUs); |
| 373 | return BAD_VALUE; |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 374 | } else if (durationUs >= 10000000) { // 10 seconds |
| 375 | // If interleaving duration is too large, it can cause the recording |
| 376 | // session to use too much memory since we have to save the output |
| 377 | // data before we write them out |
| 378 | LOGE("Audio/video interleave duration is too large: %d us", durationUs); |
| 379 | return BAD_VALUE; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 380 | } |
James Dong | 3300e96 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 381 | mInterleaveDurationUs = durationUs; |
| 382 | return OK; |
| 383 | } |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 384 | |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 385 | // If seconds < 0, only the first frame is I frame, and rest are all P frames |
| 386 | // If seconds == 0, all frames are encoded as I frames. No P frames |
| 387 | // If seconds > 0, it is the time spacing (seconds) between 2 neighboring I frames |
| 388 | status_t StagefrightRecorder::setParamVideoIFramesInterval(int32_t seconds) { |
| 389 | LOGV("setParamVideoIFramesInterval: %d seconds", seconds); |
| 390 | mIFramesIntervalSec = seconds; |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 391 | return OK; |
| 392 | } |
| 393 | |
James Dong | 6feaa46 | 2010-06-20 08:20:54 -0700 | [diff] [blame] | 394 | status_t StagefrightRecorder::setParam64BitFileOffset(bool use64Bit) { |
| 395 | LOGV("setParam64BitFileOffset: %s", |
| 396 | use64Bit? "use 64 bit file offset": "use 32 bit file offset"); |
| 397 | mUse64BitFileOffset = use64Bit; |
| 398 | return OK; |
| 399 | } |
| 400 | |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 401 | status_t StagefrightRecorder::setParamVideoCameraId(int32_t cameraId) { |
| 402 | LOGV("setParamVideoCameraId: %d", cameraId); |
| 403 | if (cameraId < 0) { |
| 404 | return BAD_VALUE; |
| 405 | } |
| 406 | mCameraId = cameraId; |
| 407 | return OK; |
| 408 | } |
| 409 | |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 410 | status_t StagefrightRecorder::setParamTrackTimeStatus(int64_t timeDurationUs) { |
| 411 | LOGV("setParamTrackTimeStatus: %lld", timeDurationUs); |
| 412 | if (timeDurationUs < 20000) { // Infeasible if shorter than 20 ms? |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 413 | LOGE("Tracking time duration too short: %lld us", timeDurationUs); |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 414 | return BAD_VALUE; |
| 415 | } |
| 416 | mTrackEveryTimeDurationUs = timeDurationUs; |
| 417 | return OK; |
| 418 | } |
| 419 | |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 420 | status_t StagefrightRecorder::setParamVideoEncoderProfile(int32_t profile) { |
| 421 | LOGV("setParamVideoEncoderProfile: %d", profile); |
| 422 | |
| 423 | // Additional check will be done later when we load the encoder. |
| 424 | // For now, we are accepting values defined in OpenMAX IL. |
| 425 | mVideoEncoderProfile = profile; |
| 426 | return OK; |
| 427 | } |
| 428 | |
| 429 | status_t StagefrightRecorder::setParamVideoEncoderLevel(int32_t level) { |
| 430 | LOGV("setParamVideoEncoderLevel: %d", level); |
| 431 | |
| 432 | // Additional check will be done later when we load the encoder. |
| 433 | // For now, we are accepting values defined in OpenMAX IL. |
| 434 | mVideoEncoderLevel = level; |
| 435 | return OK; |
| 436 | } |
| 437 | |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 438 | status_t StagefrightRecorder::setParamMovieTimeScale(int32_t timeScale) { |
| 439 | LOGV("setParamMovieTimeScale: %d", timeScale); |
| 440 | |
| 441 | // The range is set to be the same as the audio's time scale range |
| 442 | // since audio's time scale has a wider range. |
| 443 | if (timeScale < 600 || timeScale > 96000) { |
| 444 | LOGE("Time scale (%d) for movie is out of range [600, 96000]", timeScale); |
| 445 | return BAD_VALUE; |
| 446 | } |
| 447 | mMovieTimeScale = timeScale; |
| 448 | return OK; |
| 449 | } |
| 450 | |
| 451 | status_t StagefrightRecorder::setParamVideoTimeScale(int32_t timeScale) { |
| 452 | LOGV("setParamVideoTimeScale: %d", timeScale); |
| 453 | |
| 454 | // 60000 is chosen to make sure that each video frame from a 60-fps |
| 455 | // video has 1000 ticks. |
| 456 | if (timeScale < 600 || timeScale > 60000) { |
| 457 | LOGE("Time scale (%d) for video is out of range [600, 60000]", timeScale); |
| 458 | return BAD_VALUE; |
| 459 | } |
| 460 | mVideoTimeScale = timeScale; |
| 461 | return OK; |
| 462 | } |
| 463 | |
| 464 | status_t StagefrightRecorder::setParamAudioTimeScale(int32_t timeScale) { |
| 465 | LOGV("setParamAudioTimeScale: %d", timeScale); |
| 466 | |
| 467 | // 96000 Hz is the highest sampling rate support in AAC. |
| 468 | if (timeScale < 600 || timeScale > 96000) { |
| 469 | LOGE("Time scale (%d) for audio is out of range [600, 96000]", timeScale); |
| 470 | return BAD_VALUE; |
| 471 | } |
| 472 | mAudioTimeScale = timeScale; |
| 473 | return OK; |
| 474 | } |
| 475 | |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 476 | status_t StagefrightRecorder::setParameter( |
| 477 | const String8 &key, const String8 &value) { |
| 478 | LOGV("setParameter: key (%s) => value (%s)", key.string(), value.string()); |
| 479 | if (key == "max-duration") { |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 480 | int64_t max_duration_ms; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 481 | if (safe_strtoi64(value.string(), &max_duration_ms)) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 482 | return setParamMaxFileDurationUs(1000LL * max_duration_ms); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 483 | } |
| 484 | } else if (key == "max-filesize") { |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 485 | int64_t max_filesize_bytes; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 486 | if (safe_strtoi64(value.string(), &max_filesize_bytes)) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 487 | return setParamMaxFileSizeBytes(max_filesize_bytes); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 488 | } |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 489 | } else if (key == "interleave-duration-us") { |
| 490 | int32_t durationUs; |
| 491 | if (safe_strtoi32(value.string(), &durationUs)) { |
| 492 | return setParamInterleaveDuration(durationUs); |
| 493 | } |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 494 | } else if (key == "param-movie-time-scale") { |
| 495 | int32_t timeScale; |
| 496 | if (safe_strtoi32(value.string(), &timeScale)) { |
| 497 | return setParamMovieTimeScale(timeScale); |
| 498 | } |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 499 | } else if (key == "param-use-64bit-offset") { |
| 500 | int32_t use64BitOffset; |
| 501 | if (safe_strtoi32(value.string(), &use64BitOffset)) { |
| 502 | return setParam64BitFileOffset(use64BitOffset != 0); |
| 503 | } |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 504 | } else if (key == "param-track-time-status") { |
| 505 | int64_t timeDurationUs; |
| 506 | if (safe_strtoi64(value.string(), &timeDurationUs)) { |
| 507 | return setParamTrackTimeStatus(timeDurationUs); |
| 508 | } |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 509 | } else if (key == "audio-param-sampling-rate") { |
| 510 | int32_t sampling_rate; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 511 | if (safe_strtoi32(value.string(), &sampling_rate)) { |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 512 | return setParamAudioSamplingRate(sampling_rate); |
| 513 | } |
| 514 | } else if (key == "audio-param-number-of-channels") { |
| 515 | int32_t number_of_channels; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 516 | if (safe_strtoi32(value.string(), &number_of_channels)) { |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 517 | return setParamAudioNumberOfChannels(number_of_channels); |
| 518 | } |
| 519 | } else if (key == "audio-param-encoding-bitrate") { |
| 520 | int32_t audio_bitrate; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 521 | if (safe_strtoi32(value.string(), &audio_bitrate)) { |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 522 | return setParamAudioEncodingBitRate(audio_bitrate); |
| 523 | } |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 524 | } else if (key == "audio-param-time-scale") { |
| 525 | int32_t timeScale; |
| 526 | if (safe_strtoi32(value.string(), &timeScale)) { |
| 527 | return setParamAudioTimeScale(timeScale); |
| 528 | } |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 529 | } else if (key == "video-param-encoding-bitrate") { |
| 530 | int32_t video_bitrate; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 531 | if (safe_strtoi32(value.string(), &video_bitrate)) { |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 532 | return setParamVideoEncodingBitRate(video_bitrate); |
| 533 | } |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 534 | } else if (key == "video-param-i-frames-interval") { |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 535 | int32_t seconds; |
| 536 | if (safe_strtoi32(value.string(), &seconds)) { |
| 537 | return setParamVideoIFramesInterval(seconds); |
James Dong | 1244eab | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 538 | } |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 539 | } else if (key == "video-param-encoder-profile") { |
| 540 | int32_t profile; |
| 541 | if (safe_strtoi32(value.string(), &profile)) { |
| 542 | return setParamVideoEncoderProfile(profile); |
| 543 | } |
| 544 | } else if (key == "video-param-encoder-level") { |
| 545 | int32_t level; |
| 546 | if (safe_strtoi32(value.string(), &level)) { |
| 547 | return setParamVideoEncoderLevel(level); |
| 548 | } |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 549 | } else if (key == "video-param-camera-id") { |
| 550 | int32_t cameraId; |
| 551 | if (safe_strtoi32(value.string(), &cameraId)) { |
| 552 | return setParamVideoCameraId(cameraId); |
James Dong | 6feaa46 | 2010-06-20 08:20:54 -0700 | [diff] [blame] | 553 | } |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 554 | } else if (key == "video-param-time-scale") { |
| 555 | int32_t timeScale; |
| 556 | if (safe_strtoi32(value.string(), &timeScale)) { |
| 557 | return setParamVideoTimeScale(timeScale); |
| 558 | } |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 559 | } else { |
| 560 | LOGE("setParameter: failed to find key %s", key.string()); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 561 | } |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 562 | return BAD_VALUE; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | status_t StagefrightRecorder::setParameters(const String8 ¶ms) { |
| 566 | LOGV("setParameters: %s", params.string()); |
| 567 | const char *cparams = params.string(); |
| 568 | const char *key_start = cparams; |
| 569 | for (;;) { |
| 570 | const char *equal_pos = strchr(key_start, '='); |
| 571 | if (equal_pos == NULL) { |
| 572 | LOGE("Parameters %s miss a value", cparams); |
| 573 | return BAD_VALUE; |
| 574 | } |
| 575 | String8 key(key_start, equal_pos - key_start); |
| 576 | TrimString(&key); |
| 577 | if (key.length() == 0) { |
| 578 | LOGE("Parameters %s contains an empty key", cparams); |
| 579 | return BAD_VALUE; |
| 580 | } |
| 581 | const char *value_start = equal_pos + 1; |
| 582 | const char *semicolon_pos = strchr(value_start, ';'); |
| 583 | String8 value; |
| 584 | if (semicolon_pos == NULL) { |
| 585 | value.setTo(value_start); |
| 586 | } else { |
| 587 | value.setTo(value_start, semicolon_pos - value_start); |
| 588 | } |
| 589 | if (setParameter(key, value) != OK) { |
| 590 | return BAD_VALUE; |
| 591 | } |
| 592 | if (semicolon_pos == NULL) { |
| 593 | break; // Reaches the end |
| 594 | } |
| 595 | key_start = semicolon_pos + 1; |
| 596 | } |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 597 | return OK; |
| 598 | } |
| 599 | |
James Dong | fe1bafe | 2010-06-25 17:06:47 -0700 | [diff] [blame] | 600 | status_t StagefrightRecorder::setListener(const sp<IMediaRecorderClient> &listener) { |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 601 | mListener = listener; |
| 602 | |
| 603 | return OK; |
| 604 | } |
| 605 | |
| 606 | status_t StagefrightRecorder::prepare() { |
| 607 | return OK; |
| 608 | } |
| 609 | |
| 610 | status_t StagefrightRecorder::start() { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 611 | CHECK(mOutputFd >= 0); |
| 612 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 613 | if (mWriter != NULL) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 614 | LOGE("File writer is not avaialble"); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 615 | return UNKNOWN_ERROR; |
| 616 | } |
| 617 | |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 618 | switch (mOutputFormat) { |
| 619 | case OUTPUT_FORMAT_DEFAULT: |
| 620 | case OUTPUT_FORMAT_THREE_GPP: |
| 621 | case OUTPUT_FORMAT_MPEG_4: |
| 622 | return startMPEG4Recording(); |
| 623 | |
| 624 | case OUTPUT_FORMAT_AMR_NB: |
| 625 | case OUTPUT_FORMAT_AMR_WB: |
| 626 | return startAMRRecording(); |
| 627 | |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 628 | case OUTPUT_FORMAT_AAC_ADIF: |
| 629 | case OUTPUT_FORMAT_AAC_ADTS: |
| 630 | return startAACRecording(); |
| 631 | |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 632 | case OUTPUT_FORMAT_RTP_AVP: |
| 633 | return startRTPRecording(); |
| 634 | |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 635 | default: |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 636 | LOGE("Unsupported output file format: %d", mOutputFormat); |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 637 | return UNKNOWN_ERROR; |
| 638 | } |
| 639 | } |
| 640 | |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 641 | sp<MediaSource> StagefrightRecorder::createAudioSource() { |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 642 | sp<AudioSource> audioSource = |
| 643 | new AudioSource( |
| 644 | mAudioSource, |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 645 | mSampleRate, |
James Dong | d77d2a9 | 2010-06-14 17:45:35 -0700 | [diff] [blame] | 646 | mAudioChannels); |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 647 | |
| 648 | status_t err = audioSource->initCheck(); |
| 649 | |
| 650 | if (err != OK) { |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 651 | LOGE("audio source is not initialized"); |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 652 | return NULL; |
| 653 | } |
| 654 | |
| 655 | sp<MetaData> encMeta = new MetaData; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 656 | const char *mime; |
| 657 | switch (mAudioEncoder) { |
| 658 | case AUDIO_ENCODER_AMR_NB: |
| 659 | case AUDIO_ENCODER_DEFAULT: |
| 660 | mime = MEDIA_MIMETYPE_AUDIO_AMR_NB; |
| 661 | break; |
| 662 | case AUDIO_ENCODER_AMR_WB: |
| 663 | mime = MEDIA_MIMETYPE_AUDIO_AMR_WB; |
| 664 | break; |
| 665 | case AUDIO_ENCODER_AAC: |
| 666 | mime = MEDIA_MIMETYPE_AUDIO_AAC; |
| 667 | break; |
| 668 | default: |
| 669 | LOGE("Unknown audio encoder: %d", mAudioEncoder); |
| 670 | return NULL; |
| 671 | } |
| 672 | encMeta->setCString(kKeyMIMEType, mime); |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 673 | |
Andreas Huber | 259b7c1 | 2010-02-10 15:04:31 -0800 | [diff] [blame] | 674 | int32_t maxInputSize; |
| 675 | CHECK(audioSource->getFormat()->findInt32( |
| 676 | kKeyMaxInputSize, &maxInputSize)); |
| 677 | |
| 678 | encMeta->setInt32(kKeyMaxInputSize, maxInputSize); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 679 | encMeta->setInt32(kKeyChannelCount, mAudioChannels); |
| 680 | encMeta->setInt32(kKeySampleRate, mSampleRate); |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 681 | encMeta->setInt32(kKeyBitRate, mAudioBitRate); |
James Dong | eff30e3 | 2010-08-13 14:16:26 -0700 | [diff] [blame] | 682 | if (mAudioTimeScale > 0) { |
| 683 | encMeta->setInt32(kKeyTimeScale, mAudioTimeScale); |
| 684 | } |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 685 | |
| 686 | OMXClient client; |
| 687 | CHECK_EQ(client.connect(), OK); |
| 688 | |
| 689 | sp<MediaSource> audioEncoder = |
| 690 | OMXCodec::Create(client.interface(), encMeta, |
| 691 | true /* createEncoder */, audioSource); |
James Dong | 57e7f83 | 2010-06-24 19:55:31 -0700 | [diff] [blame] | 692 | mAudioSourceNode = audioSource; |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 693 | |
| 694 | return audioEncoder; |
| 695 | } |
| 696 | |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 697 | status_t StagefrightRecorder::startAACRecording() { |
| 698 | CHECK(mOutputFormat == OUTPUT_FORMAT_AAC_ADIF || |
| 699 | mOutputFormat == OUTPUT_FORMAT_AAC_ADTS); |
| 700 | |
| 701 | CHECK(mAudioEncoder == AUDIO_ENCODER_AAC); |
| 702 | CHECK(mAudioSource != AUDIO_SOURCE_LIST_END); |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 703 | |
| 704 | CHECK(0 == "AACWriter is not implemented yet"); |
| 705 | |
| 706 | return OK; |
| 707 | } |
| 708 | |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 709 | status_t StagefrightRecorder::startAMRRecording() { |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 710 | CHECK(mOutputFormat == OUTPUT_FORMAT_AMR_NB || |
| 711 | mOutputFormat == OUTPUT_FORMAT_AMR_WB); |
| 712 | |
| 713 | if (mOutputFormat == OUTPUT_FORMAT_AMR_NB) { |
| 714 | if (mAudioEncoder != AUDIO_ENCODER_DEFAULT && |
| 715 | mAudioEncoder != AUDIO_ENCODER_AMR_NB) { |
| 716 | LOGE("Invalid encoder %d used for AMRNB recording", |
| 717 | mAudioEncoder); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 718 | return BAD_VALUE; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 719 | } |
| 720 | if (mSampleRate != 8000) { |
| 721 | LOGE("Invalid sampling rate %d used for AMRNB recording", |
| 722 | mSampleRate); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 723 | return BAD_VALUE; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 724 | } |
| 725 | } else { // mOutputFormat must be OUTPUT_FORMAT_AMR_WB |
| 726 | if (mAudioEncoder != AUDIO_ENCODER_AMR_WB) { |
| 727 | LOGE("Invlaid encoder %d used for AMRWB recording", |
| 728 | mAudioEncoder); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 729 | return BAD_VALUE; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 730 | } |
| 731 | if (mSampleRate != 16000) { |
| 732 | LOGE("Invalid sample rate %d used for AMRWB recording", |
| 733 | mSampleRate); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 734 | return BAD_VALUE; |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | if (mAudioChannels != 1) { |
| 738 | LOGE("Invalid number of audio channels %d used for amr recording", |
| 739 | mAudioChannels); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 740 | return BAD_VALUE; |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 741 | } |
| 742 | |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 743 | if (mAudioSource >= AUDIO_SOURCE_LIST_END) { |
| 744 | LOGE("Invalid audio source: %d", mAudioSource); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 745 | return BAD_VALUE; |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 746 | } |
| 747 | |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 748 | sp<MediaSource> audioEncoder = createAudioSource(); |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 749 | |
| 750 | if (audioEncoder == NULL) { |
| 751 | return UNKNOWN_ERROR; |
| 752 | } |
| 753 | |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 754 | mWriter = new AMRWriter(dup(mOutputFd)); |
| 755 | mWriter->addSource(audioEncoder); |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 756 | |
| 757 | if (mMaxFileDurationUs != 0) { |
| 758 | mWriter->setMaxFileDuration(mMaxFileDurationUs); |
| 759 | } |
| 760 | if (mMaxFileSizeBytes != 0) { |
| 761 | mWriter->setMaxFileSize(mMaxFileSizeBytes); |
| 762 | } |
| 763 | mWriter->setListener(mListener); |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 764 | mWriter->start(); |
| 765 | |
| 766 | return OK; |
| 767 | } |
| 768 | |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 769 | status_t StagefrightRecorder::startRTPRecording() { |
| 770 | CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_RTP_AVP); |
| 771 | |
| 772 | if ((mAudioSource != AUDIO_SOURCE_LIST_END |
| 773 | && mVideoSource != VIDEO_SOURCE_LIST_END) |
| 774 | || (mAudioSource == AUDIO_SOURCE_LIST_END |
| 775 | && mVideoSource == VIDEO_SOURCE_LIST_END)) { |
| 776 | // Must have exactly one source. |
| 777 | return BAD_VALUE; |
| 778 | } |
| 779 | |
| 780 | if (mOutputFd < 0) { |
| 781 | return BAD_VALUE; |
| 782 | } |
| 783 | |
| 784 | sp<MediaSource> source; |
| 785 | |
| 786 | if (mAudioSource != AUDIO_SOURCE_LIST_END) { |
| 787 | source = createAudioSource(); |
| 788 | } else { |
| 789 | status_t err = setupVideoEncoder(&source); |
| 790 | if (err != OK) { |
| 791 | return err; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | mWriter = new ARTPWriter(dup(mOutputFd)); |
| 796 | mWriter->addSource(source); |
| 797 | mWriter->setListener(mListener); |
| 798 | |
| 799 | return mWriter->start(); |
| 800 | } |
| 801 | |
James Dong | 42a18c0 | 2010-06-16 17:27:46 -0700 | [diff] [blame] | 802 | void StagefrightRecorder::clipVideoFrameRate() { |
| 803 | LOGV("clipVideoFrameRate: encoder %d", mVideoEncoder); |
| 804 | int minFrameRate = mEncoderProfiles->getVideoEncoderParamByName( |
| 805 | "enc.vid.fps.min", mVideoEncoder); |
| 806 | int maxFrameRate = mEncoderProfiles->getVideoEncoderParamByName( |
| 807 | "enc.vid.fps.max", mVideoEncoder); |
| 808 | if (mFrameRate < minFrameRate) { |
| 809 | LOGW("Intended video encoding frame rate (%d fps) is too small" |
| 810 | " and will be set to (%d fps)", mFrameRate, minFrameRate); |
| 811 | mFrameRate = minFrameRate; |
| 812 | } else if (mFrameRate > maxFrameRate) { |
| 813 | LOGW("Intended video encoding frame rate (%d fps) is too large" |
| 814 | " and will be set to (%d fps)", mFrameRate, maxFrameRate); |
| 815 | mFrameRate = maxFrameRate; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | void StagefrightRecorder::clipVideoBitRate() { |
| 820 | LOGV("clipVideoBitRate: encoder %d", mVideoEncoder); |
| 821 | int minBitRate = mEncoderProfiles->getVideoEncoderParamByName( |
| 822 | "enc.vid.bps.min", mVideoEncoder); |
| 823 | int maxBitRate = mEncoderProfiles->getVideoEncoderParamByName( |
| 824 | "enc.vid.bps.max", mVideoEncoder); |
| 825 | if (mVideoBitRate < minBitRate) { |
| 826 | LOGW("Intended video encoding bit rate (%d bps) is too small" |
| 827 | " and will be set to (%d bps)", mVideoBitRate, minBitRate); |
| 828 | mVideoBitRate = minBitRate; |
| 829 | } else if (mVideoBitRate > maxBitRate) { |
| 830 | LOGW("Intended video encoding bit rate (%d bps) is too large" |
| 831 | " and will be set to (%d bps)", mVideoBitRate, maxBitRate); |
| 832 | mVideoBitRate = maxBitRate; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | void StagefrightRecorder::clipVideoFrameWidth() { |
| 837 | LOGV("clipVideoFrameWidth: encoder %d", mVideoEncoder); |
| 838 | int minFrameWidth = mEncoderProfiles->getVideoEncoderParamByName( |
| 839 | "enc.vid.width.min", mVideoEncoder); |
| 840 | int maxFrameWidth = mEncoderProfiles->getVideoEncoderParamByName( |
| 841 | "enc.vid.width.max", mVideoEncoder); |
| 842 | if (mVideoWidth < minFrameWidth) { |
| 843 | LOGW("Intended video encoding frame width (%d) is too small" |
| 844 | " and will be set to (%d)", mVideoWidth, minFrameWidth); |
| 845 | mVideoWidth = minFrameWidth; |
| 846 | } else if (mVideoWidth > maxFrameWidth) { |
| 847 | LOGW("Intended video encoding frame width (%d) is too large" |
| 848 | " and will be set to (%d)", mVideoWidth, maxFrameWidth); |
| 849 | mVideoWidth = maxFrameWidth; |
| 850 | } |
| 851 | } |
| 852 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 853 | status_t StagefrightRecorder::setupCameraSource() { |
| 854 | clipVideoBitRate(); |
| 855 | clipVideoFrameRate(); |
| 856 | clipVideoFrameWidth(); |
| 857 | clipVideoFrameHeight(); |
| 858 | |
| 859 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 860 | if (mCamera == 0) { |
| 861 | mCamera = Camera::connect(mCameraId); |
| 862 | if (mCamera == 0) { |
| 863 | LOGE("Camera connection could not be established."); |
| 864 | return -EBUSY; |
| 865 | } |
| 866 | mFlags &= ~FLAGS_HOT_CAMERA; |
| 867 | mCamera->lock(); |
| 868 | } |
| 869 | |
| 870 | // Set the actual video recording frame size |
| 871 | CameraParameters params(mCamera->getParameters()); |
| 872 | params.setPreviewSize(mVideoWidth, mVideoHeight); |
| 873 | params.setPreviewFrameRate(mFrameRate); |
| 874 | String8 s = params.flatten(); |
James Dong | af3f29e | 2010-08-03 15:29:20 -0700 | [diff] [blame] | 875 | if (OK != mCamera->setParameters(s)) { |
| 876 | LOGE("Could not change settings." |
| 877 | " Someone else is using camera %d?", mCameraId); |
| 878 | return -EBUSY; |
| 879 | } |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 880 | CameraParameters newCameraParams(mCamera->getParameters()); |
| 881 | |
| 882 | // Check on video frame size |
| 883 | int frameWidth = 0, frameHeight = 0; |
| 884 | newCameraParams.getPreviewSize(&frameWidth, &frameHeight); |
| 885 | if (frameWidth < 0 || frameWidth != mVideoWidth || |
| 886 | frameHeight < 0 || frameHeight != mVideoHeight) { |
| 887 | LOGE("Failed to set the video frame size to %dx%d", |
| 888 | mVideoWidth, mVideoHeight); |
| 889 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 890 | return UNKNOWN_ERROR; |
| 891 | } |
| 892 | |
| 893 | // Check on video frame rate |
| 894 | int frameRate = newCameraParams.getPreviewFrameRate(); |
| 895 | if (frameRate < 0 || (frameRate - mFrameRate) != 0) { |
| 896 | LOGE("Failed to set frame rate to %d fps. The actual " |
| 897 | "frame rate is %d", mFrameRate, frameRate); |
| 898 | } |
| 899 | |
James Dong | af3f29e | 2010-08-03 15:29:20 -0700 | [diff] [blame] | 900 | // This CHECK is good, since we just passed the lock/unlock |
| 901 | // check earlier by calling mCamera->setParameters(). |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 902 | CHECK_EQ(OK, mCamera->setPreviewDisplay(mPreviewSurface)); |
| 903 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 904 | return OK; |
| 905 | } |
| 906 | |
James Dong | 42a18c0 | 2010-06-16 17:27:46 -0700 | [diff] [blame] | 907 | void StagefrightRecorder::clipVideoFrameHeight() { |
| 908 | LOGV("clipVideoFrameHeight: encoder %d", mVideoEncoder); |
| 909 | int minFrameHeight = mEncoderProfiles->getVideoEncoderParamByName( |
| 910 | "enc.vid.height.min", mVideoEncoder); |
| 911 | int maxFrameHeight = mEncoderProfiles->getVideoEncoderParamByName( |
| 912 | "enc.vid.height.max", mVideoEncoder); |
| 913 | if (mVideoHeight < minFrameHeight) { |
| 914 | LOGW("Intended video encoding frame height (%d) is too small" |
| 915 | " and will be set to (%d)", mVideoHeight, minFrameHeight); |
| 916 | mVideoHeight = minFrameHeight; |
| 917 | } else if (mVideoHeight > maxFrameHeight) { |
| 918 | LOGW("Intended video encoding frame height (%d) is too large" |
| 919 | " and will be set to (%d)", mVideoHeight, maxFrameHeight); |
| 920 | mVideoHeight = maxFrameHeight; |
| 921 | } |
| 922 | } |
| 923 | |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 924 | status_t StagefrightRecorder::setupVideoEncoder(sp<MediaSource> *source) { |
| 925 | source->clear(); |
| 926 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 927 | status_t err = setupCameraSource(); |
| 928 | if (err != OK) return err; |
| 929 | |
| 930 | sp<CameraSource> cameraSource = CameraSource::CreateFromCamera(mCamera); |
| 931 | CHECK(cameraSource != NULL); |
| 932 | |
| 933 | sp<MetaData> enc_meta = new MetaData; |
| 934 | enc_meta->setInt32(kKeyBitRate, mVideoBitRate); |
| 935 | enc_meta->setInt32(kKeySampleRate, mFrameRate); |
| 936 | |
| 937 | switch (mVideoEncoder) { |
| 938 | case VIDEO_ENCODER_H263: |
| 939 | enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263); |
| 940 | break; |
| 941 | |
| 942 | case VIDEO_ENCODER_MPEG_4_SP: |
| 943 | enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4); |
| 944 | break; |
| 945 | |
| 946 | case VIDEO_ENCODER_H264: |
| 947 | enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC); |
| 948 | break; |
| 949 | |
| 950 | default: |
| 951 | CHECK(!"Should not be here, unsupported video encoding."); |
| 952 | break; |
| 953 | } |
| 954 | |
| 955 | sp<MetaData> meta = cameraSource->getFormat(); |
| 956 | |
James Dong | 1cc31e6 | 2010-07-02 17:44:44 -0700 | [diff] [blame] | 957 | int32_t width, height, stride, sliceHeight, colorFormat; |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 958 | CHECK(meta->findInt32(kKeyWidth, &width)); |
| 959 | CHECK(meta->findInt32(kKeyHeight, &height)); |
| 960 | CHECK(meta->findInt32(kKeyStride, &stride)); |
| 961 | CHECK(meta->findInt32(kKeySliceHeight, &sliceHeight)); |
James Dong | 1cc31e6 | 2010-07-02 17:44:44 -0700 | [diff] [blame] | 962 | CHECK(meta->findInt32(kKeyColorFormat, &colorFormat)); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 963 | |
| 964 | enc_meta->setInt32(kKeyWidth, width); |
| 965 | enc_meta->setInt32(kKeyHeight, height); |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 966 | enc_meta->setInt32(kKeyIFramesInterval, mIFramesIntervalSec); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 967 | enc_meta->setInt32(kKeyStride, stride); |
| 968 | enc_meta->setInt32(kKeySliceHeight, sliceHeight); |
James Dong | 1cc31e6 | 2010-07-02 17:44:44 -0700 | [diff] [blame] | 969 | enc_meta->setInt32(kKeyColorFormat, colorFormat); |
James Dong | eff30e3 | 2010-08-13 14:16:26 -0700 | [diff] [blame] | 970 | if (mVideoTimeScale > 0) { |
| 971 | enc_meta->setInt32(kKeyTimeScale, mVideoTimeScale); |
| 972 | } |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 973 | if (mVideoEncoderProfile != -1) { |
| 974 | enc_meta->setInt32(kKeyVideoProfile, mVideoEncoderProfile); |
| 975 | } |
| 976 | if (mVideoEncoderLevel != -1) { |
| 977 | enc_meta->setInt32(kKeyVideoLevel, mVideoEncoderLevel); |
| 978 | } |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 979 | |
| 980 | OMXClient client; |
| 981 | CHECK_EQ(client.connect(), OK); |
| 982 | |
| 983 | sp<MediaSource> encoder = OMXCodec::Create( |
| 984 | client.interface(), enc_meta, |
| 985 | true /* createEncoder */, cameraSource); |
| 986 | if (encoder == NULL) { |
| 987 | return UNKNOWN_ERROR; |
| 988 | } |
| 989 | |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 990 | *source = encoder; |
| 991 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 992 | return OK; |
| 993 | } |
| 994 | |
| 995 | status_t StagefrightRecorder::setupAudioEncoder(const sp<MediaWriter>& writer) { |
| 996 | sp<MediaSource> audioEncoder; |
| 997 | switch(mAudioEncoder) { |
| 998 | case AUDIO_ENCODER_AMR_NB: |
| 999 | case AUDIO_ENCODER_AMR_WB: |
| 1000 | case AUDIO_ENCODER_AAC: |
| 1001 | audioEncoder = createAudioSource(); |
| 1002 | break; |
| 1003 | default: |
| 1004 | LOGE("Unsupported audio encoder: %d", mAudioEncoder); |
| 1005 | return UNKNOWN_ERROR; |
| 1006 | } |
| 1007 | |
| 1008 | if (audioEncoder == NULL) { |
| 1009 | return UNKNOWN_ERROR; |
| 1010 | } |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 1011 | |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1012 | writer->addSource(audioEncoder); |
| 1013 | return OK; |
| 1014 | } |
| 1015 | |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 1016 | status_t StagefrightRecorder::startMPEG4Recording() { |
James Dong | 6feaa46 | 2010-06-20 08:20:54 -0700 | [diff] [blame] | 1017 | int32_t totalBitRate = 0; |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1018 | status_t err = OK; |
| 1019 | sp<MediaWriter> writer = new MPEG4Writer(dup(mOutputFd)); |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 1020 | |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 1021 | // Add audio source first if it exists |
| 1022 | if (mAudioSource != AUDIO_SOURCE_LIST_END) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1023 | err = setupAudioEncoder(writer); |
| 1024 | if (err != OK) return err; |
James Dong | 6feaa46 | 2010-06-20 08:20:54 -0700 | [diff] [blame] | 1025 | totalBitRate += mAudioBitRate; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 1026 | } |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 1027 | if (mVideoSource == VIDEO_SOURCE_DEFAULT |
| 1028 | || mVideoSource == VIDEO_SOURCE_CAMERA) { |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 1029 | sp<MediaSource> encoder; |
| 1030 | err = setupVideoEncoder(&encoder); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1031 | if (err != OK) return err; |
Andreas Huber | 57648e4 | 2010-08-04 10:14:30 -0700 | [diff] [blame] | 1032 | writer->addSource(encoder); |
James Dong | 6feaa46 | 2010-06-20 08:20:54 -0700 | [diff] [blame] | 1033 | totalBitRate += mVideoBitRate; |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
James Dong | 63299c0 | 2010-07-28 10:08:03 -0700 | [diff] [blame] | 1036 | if (mInterleaveDurationUs > 0) { |
| 1037 | reinterpret_cast<MPEG4Writer *>(writer.get())-> |
| 1038 | setInterleaveDuration(mInterleaveDurationUs); |
| 1039 | } |
James Dong | 2cd841d | 2010-05-11 11:46:59 -0700 | [diff] [blame] | 1040 | |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 1041 | if (mMaxFileDurationUs != 0) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1042 | writer->setMaxFileDuration(mMaxFileDurationUs); |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 1043 | } |
| 1044 | if (mMaxFileSizeBytes != 0) { |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1045 | writer->setMaxFileSize(mMaxFileSizeBytes); |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 1046 | } |
James Dong | 6feaa46 | 2010-06-20 08:20:54 -0700 | [diff] [blame] | 1047 | sp<MetaData> meta = new MetaData; |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 1048 | meta->setInt64(kKeyTime, systemTime() / 1000); |
| 1049 | meta->setInt32(kKeyFileType, mOutputFormat); |
James Dong | 6feaa46 | 2010-06-20 08:20:54 -0700 | [diff] [blame] | 1050 | meta->setInt32(kKeyBitRate, totalBitRate); |
| 1051 | meta->setInt32(kKey64BitFileOffset, mUse64BitFileOffset); |
James Dong | eff30e3 | 2010-08-13 14:16:26 -0700 | [diff] [blame] | 1052 | if (mMovieTimeScale > 0) { |
| 1053 | meta->setInt32(kKeyTimeScale, mMovieTimeScale); |
| 1054 | } |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 1055 | if (mTrackEveryTimeDurationUs > 0) { |
| 1056 | meta->setInt64(kKeyTrackTimeStatus, mTrackEveryTimeDurationUs); |
| 1057 | } |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1058 | writer->setListener(mListener); |
| 1059 | mWriter = writer; |
| 1060 | return mWriter->start(meta.get()); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1061 | } |
| 1062 | |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 1063 | status_t StagefrightRecorder::pause() { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1064 | LOGV("pause"); |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 1065 | if (mWriter == NULL) { |
| 1066 | return UNKNOWN_ERROR; |
| 1067 | } |
| 1068 | mWriter->pause(); |
| 1069 | return OK; |
| 1070 | } |
| 1071 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1072 | status_t StagefrightRecorder::stop() { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1073 | LOGV("stop"); |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame] | 1074 | status_t err = OK; |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1075 | if (mWriter != NULL) { |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame] | 1076 | err = mWriter->stop(); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1077 | mWriter.clear(); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 1080 | if (mCamera != 0) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1081 | LOGV("Disconnect camera"); |
James Dong | 71d714c | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 1082 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 1083 | if ((mFlags & FLAGS_HOT_CAMERA) == 0) { |
| 1084 | LOGV("Camera was cold when we started, stopping preview"); |
| 1085 | mCamera->stopPreview(); |
| 1086 | } |
James Dong | 71d714c | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 1087 | mCamera->unlock(); |
James Dong | 7b06de6 | 2010-06-30 12:41:16 -0700 | [diff] [blame] | 1088 | mCamera.clear(); |
James Dong | 71d714c | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 1089 | IPCThreadState::self()->restoreCallingIdentity(token); |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 1090 | mFlags = 0; |
| 1091 | } |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1092 | |
James Dong | c6280bc | 2010-08-11 17:12:39 -0700 | [diff] [blame] | 1093 | if (mOutputFd >= 0) { |
| 1094 | ::close(mOutputFd); |
| 1095 | mOutputFd = -1; |
| 1096 | } |
| 1097 | |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame] | 1098 | return err; |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | status_t StagefrightRecorder::close() { |
| 1102 | LOGV("close"); |
| 1103 | stop(); |
| 1104 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1105 | return OK; |
| 1106 | } |
| 1107 | |
| 1108 | status_t StagefrightRecorder::reset() { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1109 | LOGV("reset"); |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1110 | stop(); |
| 1111 | |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 1112 | // No audio or video source by default |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1113 | mAudioSource = AUDIO_SOURCE_LIST_END; |
| 1114 | mVideoSource = VIDEO_SOURCE_LIST_END; |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 1115 | |
| 1116 | // Default parameters |
| 1117 | mOutputFormat = OUTPUT_FORMAT_THREE_GPP; |
| 1118 | mAudioEncoder = AUDIO_ENCODER_AMR_NB; |
| 1119 | mVideoEncoder = VIDEO_ENCODER_H263; |
| 1120 | mVideoWidth = 176; |
| 1121 | mVideoHeight = 144; |
| 1122 | mFrameRate = 20; |
| 1123 | mVideoBitRate = 192000; |
| 1124 | mSampleRate = 8000; |
| 1125 | mAudioChannels = 1; |
| 1126 | mAudioBitRate = 12200; |
James Dong | c616172 | 2010-05-20 17:55:52 -0700 | [diff] [blame] | 1127 | mInterleaveDurationUs = 0; |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 1128 | mIFramesIntervalSec = 1; |
James Dong | 57e7f83 | 2010-06-24 19:55:31 -0700 | [diff] [blame] | 1129 | mAudioSourceNode = 0; |
James Dong | 6feaa46 | 2010-06-20 08:20:54 -0700 | [diff] [blame] | 1130 | mUse64BitFileOffset = false; |
James Dong | eff30e3 | 2010-08-13 14:16:26 -0700 | [diff] [blame] | 1131 | mMovieTimeScale = -1; |
| 1132 | mAudioTimeScale = -1; |
| 1133 | mVideoTimeScale = -1; |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 1134 | mCameraId = 0; |
James Dong | 81c929a | 2010-07-01 15:02:14 -0700 | [diff] [blame] | 1135 | mVideoEncoderProfile = -1; |
| 1136 | mVideoEncoderLevel = -1; |
| 1137 | mMaxFileDurationUs = 0; |
| 1138 | mMaxFileSizeBytes = 0; |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 1139 | mTrackEveryTimeDurationUs = 0; |
James Dong | 42a18c0 | 2010-06-16 17:27:46 -0700 | [diff] [blame] | 1140 | mEncoderProfiles = MediaProfiles::getInstance(); |
James Dong | abed93a | 2010-04-22 17:27:04 -0700 | [diff] [blame] | 1141 | |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1142 | mOutputFd = -1; |
James Dong | b00e246 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 1143 | mFlags = 0; |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1144 | |
| 1145 | return OK; |
| 1146 | } |
| 1147 | |
| 1148 | status_t StagefrightRecorder::getMaxAmplitude(int *max) { |
James Dong | c0ab2a6 | 2010-06-29 16:29:19 -0700 | [diff] [blame] | 1149 | LOGV("getMaxAmplitude"); |
| 1150 | |
| 1151 | if (max == NULL) { |
| 1152 | LOGE("Null pointer argument"); |
| 1153 | return BAD_VALUE; |
| 1154 | } |
| 1155 | |
James Dong | 57e7f83 | 2010-06-24 19:55:31 -0700 | [diff] [blame] | 1156 | if (mAudioSourceNode != 0) { |
| 1157 | *max = mAudioSourceNode->getMaxAmplitude(); |
| 1158 | } else { |
| 1159 | *max = 0; |
| 1160 | } |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 1161 | |
| 1162 | return OK; |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1163 | } |
| 1164 | |
James Dong | 3f51fa7 | 2010-08-18 03:32:26 -0700 | [diff] [blame] | 1165 | status_t StagefrightRecorder::dump( |
| 1166 | int fd, const Vector<String16>& args) const { |
| 1167 | LOGV("dump"); |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 1168 | const size_t SIZE = 256; |
| 1169 | char buffer[SIZE]; |
| 1170 | String8 result; |
James Dong | 3f51fa7 | 2010-08-18 03:32:26 -0700 | [diff] [blame] | 1171 | if (mWriter != 0) { |
| 1172 | mWriter->dump(fd, args); |
| 1173 | } else { |
| 1174 | snprintf(buffer, SIZE, " No file writer\n"); |
| 1175 | result.append(buffer); |
| 1176 | } |
| 1177 | snprintf(buffer, SIZE, " Recorder: %p\n", this); |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 1178 | snprintf(buffer, SIZE, " Output file (fd %d):\n", mOutputFd); |
| 1179 | result.append(buffer); |
| 1180 | snprintf(buffer, SIZE, " File format: %d\n", mOutputFormat); |
| 1181 | result.append(buffer); |
| 1182 | snprintf(buffer, SIZE, " Max file size (bytes): %lld\n", mMaxFileSizeBytes); |
| 1183 | result.append(buffer); |
| 1184 | snprintf(buffer, SIZE, " Max file duration (us): %lld\n", mMaxFileDurationUs); |
| 1185 | result.append(buffer); |
| 1186 | snprintf(buffer, SIZE, " File offset length (bits): %d\n", mUse64BitFileOffset? 64: 32); |
| 1187 | result.append(buffer); |
| 1188 | snprintf(buffer, SIZE, " Interleave duration (us): %d\n", mInterleaveDurationUs); |
| 1189 | result.append(buffer); |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 1190 | snprintf(buffer, SIZE, " Progress notification: %lld us\n", mTrackEveryTimeDurationUs); |
| 1191 | result.append(buffer); |
| 1192 | snprintf(buffer, SIZE, " Audio\n"); |
| 1193 | result.append(buffer); |
| 1194 | snprintf(buffer, SIZE, " Source: %d\n", mAudioSource); |
| 1195 | result.append(buffer); |
| 1196 | snprintf(buffer, SIZE, " Encoder: %d\n", mAudioEncoder); |
| 1197 | result.append(buffer); |
| 1198 | snprintf(buffer, SIZE, " Bit rate (bps): %d\n", mAudioBitRate); |
| 1199 | result.append(buffer); |
| 1200 | snprintf(buffer, SIZE, " Sampling rate (hz): %d\n", mSampleRate); |
| 1201 | result.append(buffer); |
| 1202 | snprintf(buffer, SIZE, " Number of channels: %d\n", mAudioChannels); |
| 1203 | result.append(buffer); |
| 1204 | snprintf(buffer, SIZE, " Max amplitude: %d\n", mAudioSourceNode == 0? 0: mAudioSourceNode->getMaxAmplitude()); |
| 1205 | result.append(buffer); |
| 1206 | snprintf(buffer, SIZE, " Video\n"); |
| 1207 | result.append(buffer); |
| 1208 | snprintf(buffer, SIZE, " Source: %d\n", mVideoSource); |
| 1209 | result.append(buffer); |
| 1210 | snprintf(buffer, SIZE, " Camera Id: %d\n", mCameraId); |
| 1211 | result.append(buffer); |
| 1212 | snprintf(buffer, SIZE, " Camera flags: %d\n", mFlags); |
| 1213 | result.append(buffer); |
| 1214 | snprintf(buffer, SIZE, " Encoder: %d\n", mVideoEncoder); |
| 1215 | result.append(buffer); |
| 1216 | snprintf(buffer, SIZE, " Encoder profile: %d\n", mVideoEncoderProfile); |
| 1217 | result.append(buffer); |
| 1218 | snprintf(buffer, SIZE, " Encoder level: %d\n", mVideoEncoderLevel); |
| 1219 | result.append(buffer); |
James Dong | 52d13f0 | 2010-07-02 11:39:06 -0700 | [diff] [blame] | 1220 | snprintf(buffer, SIZE, " I frames interval (s): %d\n", mIFramesIntervalSec); |
James Dong | 929642e | 2010-07-08 11:16:11 -0700 | [diff] [blame] | 1221 | result.append(buffer); |
| 1222 | snprintf(buffer, SIZE, " Frame size (pixels): %dx%d\n", mVideoWidth, mVideoHeight); |
| 1223 | result.append(buffer); |
| 1224 | snprintf(buffer, SIZE, " Frame rate (fps): %d\n", mFrameRate); |
| 1225 | result.append(buffer); |
| 1226 | snprintf(buffer, SIZE, " Bit rate (bps): %d\n", mVideoBitRate); |
| 1227 | result.append(buffer); |
| 1228 | ::write(fd, result.string(), result.size()); |
| 1229 | return OK; |
| 1230 | } |
Andreas Huber | ea6a38c | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 1231 | } // namespace android |