StagefrightRecorder.cpp revision 13f6284305e4b27395a23db7882d670bdb1bcae1
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 23#include <media/stagefright/AudioSource.h> 24#include <media/stagefright/AMRWriter.h> 25#include <media/stagefright/CameraSource.h> 26#include <media/stagefright/VideoSourceDownSampler.h> 27#include <media/stagefright/CameraSourceTimeLapse.h> 28#include <media/stagefright/MediaSourceSplitter.h> 29#include <media/stagefright/MPEG2TSWriter.h> 30#include <media/stagefright/MPEG4Writer.h> 31#include <media/stagefright/MediaDebug.h> 32#include <media/stagefright/MediaDefs.h> 33#include <media/stagefright/MetaData.h> 34#include <media/stagefright/OMXClient.h> 35#include <media/stagefright/OMXCodec.h> 36#include <media/MediaProfiles.h> 37#include <camera/ICamera.h> 38#include <camera/CameraParameters.h> 39#include <surfaceflinger/Surface.h> 40#include <utils/Errors.h> 41#include <sys/types.h> 42#include <ctype.h> 43#include <unistd.h> 44 45#include "ARTPWriter.h" 46 47namespace android { 48 49StagefrightRecorder::StagefrightRecorder() 50 : mWriter(NULL), mWriterAux(NULL), 51 mOutputFd(-1), mOutputFdAux(-1) { 52 53 LOGV("Constructor"); 54 reset(); 55} 56 57StagefrightRecorder::~StagefrightRecorder() { 58 LOGV("Destructor"); 59 stop(); 60} 61 62status_t StagefrightRecorder::init() { 63 LOGV("init"); 64 return OK; 65} 66 67status_t StagefrightRecorder::setAudioSource(audio_source as) { 68 LOGV("setAudioSource: %d", as); 69 if (as < AUDIO_SOURCE_DEFAULT || 70 as >= AUDIO_SOURCE_LIST_END) { 71 LOGE("Invalid audio source: %d", as); 72 return BAD_VALUE; 73 } 74 75 if (as == AUDIO_SOURCE_DEFAULT) { 76 mAudioSource = AUDIO_SOURCE_MIC; 77 } else { 78 mAudioSource = as; 79 } 80 81 return OK; 82} 83 84status_t StagefrightRecorder::setVideoSource(video_source vs) { 85 LOGV("setVideoSource: %d", vs); 86 if (vs < VIDEO_SOURCE_DEFAULT || 87 vs >= VIDEO_SOURCE_LIST_END) { 88 LOGE("Invalid video source: %d", vs); 89 return BAD_VALUE; 90 } 91 92 if (vs == VIDEO_SOURCE_DEFAULT) { 93 mVideoSource = VIDEO_SOURCE_CAMERA; 94 } else { 95 mVideoSource = vs; 96 } 97 98 return OK; 99} 100 101status_t StagefrightRecorder::setOutputFormat(output_format of) { 102 LOGV("setOutputFormat: %d", of); 103 if (of < OUTPUT_FORMAT_DEFAULT || 104 of >= OUTPUT_FORMAT_LIST_END) { 105 LOGE("Invalid output format: %d", of); 106 return BAD_VALUE; 107 } 108 109 if (of == OUTPUT_FORMAT_DEFAULT) { 110 mOutputFormat = OUTPUT_FORMAT_THREE_GPP; 111 } else { 112 mOutputFormat = of; 113 } 114 115 return OK; 116} 117 118status_t StagefrightRecorder::setAudioEncoder(audio_encoder ae) { 119 LOGV("setAudioEncoder: %d", ae); 120 if (ae < AUDIO_ENCODER_DEFAULT || 121 ae >= AUDIO_ENCODER_LIST_END) { 122 LOGE("Invalid audio encoder: %d", ae); 123 return BAD_VALUE; 124 } 125 126 if (ae == AUDIO_ENCODER_DEFAULT) { 127 mAudioEncoder = AUDIO_ENCODER_AMR_NB; 128 } else { 129 mAudioEncoder = ae; 130 } 131 132 return OK; 133} 134 135status_t StagefrightRecorder::setVideoEncoder(video_encoder ve) { 136 LOGV("setVideoEncoder: %d", ve); 137 if (ve < VIDEO_ENCODER_DEFAULT || 138 ve >= VIDEO_ENCODER_LIST_END) { 139 LOGE("Invalid video encoder: %d", ve); 140 return BAD_VALUE; 141 } 142 143 if (ve == VIDEO_ENCODER_DEFAULT) { 144 mVideoEncoder = VIDEO_ENCODER_H263; 145 } else { 146 mVideoEncoder = ve; 147 } 148 149 return OK; 150} 151 152status_t StagefrightRecorder::setVideoSize(int width, int height) { 153 LOGV("setVideoSize: %dx%d", width, height); 154 if (width <= 0 || height <= 0) { 155 LOGE("Invalid video size: %dx%d", width, height); 156 return BAD_VALUE; 157 } 158 159 // Additional check on the dimension will be performed later 160 mVideoWidth = width; 161 mVideoHeight = height; 162 163 return OK; 164} 165 166status_t StagefrightRecorder::setVideoFrameRate(int frames_per_second) { 167 LOGV("setVideoFrameRate: %d", frames_per_second); 168 if ((frames_per_second <= 0 && frames_per_second != -1) || 169 frames_per_second > 120) { 170 LOGE("Invalid video frame rate: %d", frames_per_second); 171 return BAD_VALUE; 172 } 173 174 // Additional check on the frame rate will be performed later 175 mFrameRate = frames_per_second; 176 177 return OK; 178} 179 180status_t StagefrightRecorder::setCamera(const sp<ICamera> &camera) { 181 LOGV("setCamera"); 182 if (camera == 0) { 183 LOGE("camera is NULL"); 184 return BAD_VALUE; 185 } 186 187 mCamera = camera; 188 return OK; 189} 190 191status_t StagefrightRecorder::setPreviewSurface(const sp<Surface> &surface) { 192 LOGV("setPreviewSurface: %p", surface.get()); 193 mPreviewSurface = surface; 194 195 return OK; 196} 197 198status_t StagefrightRecorder::setOutputFile(const char *path) { 199 LOGE("setOutputFile(const char*) must not be called"); 200 // We don't actually support this at all, as the media_server process 201 // no longer has permissions to create files. 202 203 return -EPERM; 204} 205 206status_t StagefrightRecorder::setOutputFile(int fd, int64_t offset, int64_t length) { 207 LOGV("setOutputFile: %d, %lld, %lld", fd, offset, length); 208 // These don't make any sense, do they? 209 CHECK_EQ(offset, 0); 210 CHECK_EQ(length, 0); 211 212 if (fd < 0) { 213 LOGE("Invalid file descriptor: %d", fd); 214 return -EBADF; 215 } 216 217 if (mOutputFd >= 0) { 218 ::close(mOutputFd); 219 } 220 mOutputFd = dup(fd); 221 222 return OK; 223} 224 225status_t StagefrightRecorder::setOutputFileAuxiliary(int fd) { 226 LOGV("setOutputFileAuxiliary: %d", fd); 227 228 if (fd < 0) { 229 LOGE("Invalid file descriptor: %d", fd); 230 return -EBADF; 231 } 232 233 mCaptureAuxVideo = true; 234 235 if (mOutputFdAux >= 0) { 236 ::close(mOutputFdAux); 237 } 238 mOutputFdAux = dup(fd); 239 240 return OK; 241} 242 243// Attempt to parse an int64 literal optionally surrounded by whitespace, 244// returns true on success, false otherwise. 245static bool safe_strtoi64(const char *s, int64_t *val) { 246 char *end; 247 *val = strtoll(s, &end, 10); 248 249 if (end == s || errno == ERANGE) { 250 return false; 251 } 252 253 // Skip trailing whitespace 254 while (isspace(*end)) { 255 ++end; 256 } 257 258 // For a successful return, the string must contain nothing but a valid 259 // int64 literal optionally surrounded by whitespace. 260 261 return *end == '\0'; 262} 263 264// Return true if the value is in [0, 0x007FFFFFFF] 265static bool safe_strtoi32(const char *s, int32_t *val) { 266 int64_t temp; 267 if (safe_strtoi64(s, &temp)) { 268 if (temp >= 0 && temp <= 0x007FFFFFFF) { 269 *val = static_cast<int32_t>(temp); 270 return true; 271 } 272 } 273 return false; 274} 275 276// Trim both leading and trailing whitespace from the given string. 277static void TrimString(String8 *s) { 278 size_t num_bytes = s->bytes(); 279 const char *data = s->string(); 280 281 size_t leading_space = 0; 282 while (leading_space < num_bytes && isspace(data[leading_space])) { 283 ++leading_space; 284 } 285 286 size_t i = num_bytes; 287 while (i > leading_space && isspace(data[i - 1])) { 288 --i; 289 } 290 291 s->setTo(String8(&data[leading_space], i - leading_space)); 292} 293 294status_t StagefrightRecorder::setParamAudioSamplingRate(int32_t sampleRate) { 295 LOGV("setParamAudioSamplingRate: %d", sampleRate); 296 if (sampleRate <= 0) { 297 LOGE("Invalid audio sampling rate: %d", sampleRate); 298 return BAD_VALUE; 299 } 300 301 // Additional check on the sample rate will be performed later. 302 mSampleRate = sampleRate; 303 return OK; 304} 305 306status_t StagefrightRecorder::setParamAudioNumberOfChannels(int32_t channels) { 307 LOGV("setParamAudioNumberOfChannels: %d", channels); 308 if (channels <= 0 || channels >= 3) { 309 LOGE("Invalid number of audio channels: %d", channels); 310 return BAD_VALUE; 311 } 312 313 // Additional check on the number of channels will be performed later. 314 mAudioChannels = channels; 315 return OK; 316} 317 318status_t StagefrightRecorder::setParamAudioEncodingBitRate(int32_t bitRate) { 319 LOGV("setParamAudioEncodingBitRate: %d", bitRate); 320 if (bitRate <= 0) { 321 LOGE("Invalid audio encoding bit rate: %d", bitRate); 322 return BAD_VALUE; 323 } 324 325 // The target bit rate may not be exactly the same as the requested. 326 // It depends on many factors, such as rate control, and the bit rate 327 // range that a specific encoder supports. The mismatch between the 328 // the target and requested bit rate will NOT be treated as an error. 329 mAudioBitRate = bitRate; 330 return OK; 331} 332 333status_t StagefrightRecorder::setParamVideoEncodingBitRate(int32_t bitRate) { 334 LOGV("setParamVideoEncodingBitRate: %d", bitRate); 335 if (bitRate <= 0) { 336 LOGE("Invalid video encoding bit rate: %d", bitRate); 337 return BAD_VALUE; 338 } 339 340 // The target bit rate may not be exactly the same as the requested. 341 // It depends on many factors, such as rate control, and the bit rate 342 // range that a specific encoder supports. The mismatch between the 343 // the target and requested bit rate will NOT be treated as an error. 344 mVideoBitRate = bitRate; 345 return OK; 346} 347 348// Always rotate clockwise, and only support 0, 90, 180 and 270 for now. 349status_t StagefrightRecorder::setParamVideoRotation(int32_t degrees) { 350 LOGV("setParamVideoRotation: %d", degrees); 351 if (degrees < 0 || degrees % 90 != 0) { 352 LOGE("Unsupported video rotation angle: %d", degrees); 353 return BAD_VALUE; 354 } 355 mRotationDegrees = degrees % 360; 356 return OK; 357} 358 359status_t StagefrightRecorder::setParamMaxFileDurationUs(int64_t timeUs) { 360 LOGV("setParamMaxFileDurationUs: %lld us", timeUs); 361 if (timeUs <= 0) { 362 LOGW("Max file duration is not positive: %lld us. Disabling duration limit.", timeUs); 363 timeUs = 0; // Disable the duration limit for zero or negative values. 364 } else if (timeUs <= 100000LL) { // XXX: 100 milli-seconds 365 LOGE("Max file duration is too short: %lld us", timeUs); 366 return BAD_VALUE; 367 } 368 369 mMaxFileDurationUs = timeUs; 370 return OK; 371} 372 373status_t StagefrightRecorder::setParamMaxFileSizeBytes(int64_t bytes) { 374 LOGV("setParamMaxFileSizeBytes: %lld bytes", bytes); 375 if (bytes <= 1024) { // XXX: 1 kB 376 LOGE("Max file size is too small: %lld bytes", bytes); 377 return BAD_VALUE; 378 } 379 mMaxFileSizeBytes = bytes; 380 return OK; 381} 382 383status_t StagefrightRecorder::setParamInterleaveDuration(int32_t durationUs) { 384 LOGV("setParamInterleaveDuration: %d", durationUs); 385 if (durationUs <= 500000) { // 500 ms 386 // If interleave duration is too small, it is very inefficient to do 387 // interleaving since the metadata overhead will count for a significant 388 // portion of the saved contents 389 LOGE("Audio/video interleave duration is too small: %d us", durationUs); 390 return BAD_VALUE; 391 } else if (durationUs >= 10000000) { // 10 seconds 392 // If interleaving duration is too large, it can cause the recording 393 // session to use too much memory since we have to save the output 394 // data before we write them out 395 LOGE("Audio/video interleave duration is too large: %d us", durationUs); 396 return BAD_VALUE; 397 } 398 mInterleaveDurationUs = durationUs; 399 return OK; 400} 401 402// If seconds < 0, only the first frame is I frame, and rest are all P frames 403// If seconds == 0, all frames are encoded as I frames. No P frames 404// If seconds > 0, it is the time spacing (seconds) between 2 neighboring I frames 405status_t StagefrightRecorder::setParamVideoIFramesInterval(int32_t seconds) { 406 LOGV("setParamVideoIFramesInterval: %d seconds", seconds); 407 mIFramesIntervalSec = seconds; 408 return OK; 409} 410 411status_t StagefrightRecorder::setParam64BitFileOffset(bool use64Bit) { 412 LOGV("setParam64BitFileOffset: %s", 413 use64Bit? "use 64 bit file offset": "use 32 bit file offset"); 414 mUse64BitFileOffset = use64Bit; 415 return OK; 416} 417 418status_t StagefrightRecorder::setParamVideoCameraId(int32_t cameraId) { 419 LOGV("setParamVideoCameraId: %d", cameraId); 420 if (cameraId < 0) { 421 return BAD_VALUE; 422 } 423 mCameraId = cameraId; 424 return OK; 425} 426 427status_t StagefrightRecorder::setParamTrackTimeStatus(int64_t timeDurationUs) { 428 LOGV("setParamTrackTimeStatus: %lld", timeDurationUs); 429 if (timeDurationUs < 20000) { // Infeasible if shorter than 20 ms? 430 LOGE("Tracking time duration too short: %lld us", timeDurationUs); 431 return BAD_VALUE; 432 } 433 mTrackEveryTimeDurationUs = timeDurationUs; 434 return OK; 435} 436 437status_t StagefrightRecorder::setParamVideoEncoderProfile(int32_t profile) { 438 LOGV("setParamVideoEncoderProfile: %d", profile); 439 440 // Additional check will be done later when we load the encoder. 441 // For now, we are accepting values defined in OpenMAX IL. 442 mVideoEncoderProfile = profile; 443 return OK; 444} 445 446status_t StagefrightRecorder::setParamVideoEncoderLevel(int32_t level) { 447 LOGV("setParamVideoEncoderLevel: %d", level); 448 449 // Additional check will be done later when we load the encoder. 450 // For now, we are accepting values defined in OpenMAX IL. 451 mVideoEncoderLevel = level; 452 return OK; 453} 454 455status_t StagefrightRecorder::setParamMovieTimeScale(int32_t timeScale) { 456 LOGV("setParamMovieTimeScale: %d", timeScale); 457 458 // The range is set to be the same as the audio's time scale range 459 // since audio's time scale has a wider range. 460 if (timeScale < 600 || timeScale > 96000) { 461 LOGE("Time scale (%d) for movie is out of range [600, 96000]", timeScale); 462 return BAD_VALUE; 463 } 464 mMovieTimeScale = timeScale; 465 return OK; 466} 467 468status_t StagefrightRecorder::setParamVideoTimeScale(int32_t timeScale) { 469 LOGV("setParamVideoTimeScale: %d", timeScale); 470 471 // 60000 is chosen to make sure that each video frame from a 60-fps 472 // video has 1000 ticks. 473 if (timeScale < 600 || timeScale > 60000) { 474 LOGE("Time scale (%d) for video is out of range [600, 60000]", timeScale); 475 return BAD_VALUE; 476 } 477 mVideoTimeScale = timeScale; 478 return OK; 479} 480 481status_t StagefrightRecorder::setParamAudioTimeScale(int32_t timeScale) { 482 LOGV("setParamAudioTimeScale: %d", timeScale); 483 484 // 96000 Hz is the highest sampling rate support in AAC. 485 if (timeScale < 600 || timeScale > 96000) { 486 LOGE("Time scale (%d) for audio is out of range [600, 96000]", timeScale); 487 return BAD_VALUE; 488 } 489 mAudioTimeScale = timeScale; 490 return OK; 491} 492 493status_t StagefrightRecorder::setParamTimeLapseEnable(int32_t timeLapseEnable) { 494 LOGV("setParamTimeLapseEnable: %d", timeLapseEnable); 495 496 if(timeLapseEnable == 0) { 497 mCaptureTimeLapse = false; 498 } else if (timeLapseEnable == 1) { 499 mCaptureTimeLapse = true; 500 } else { 501 return BAD_VALUE; 502 } 503 return OK; 504} 505 506status_t StagefrightRecorder::setParamTimeBetweenTimeLapseFrameCapture(int64_t timeUs) { 507 LOGV("setParamTimeBetweenTimeLapseFrameCapture: %lld us", timeUs); 508 509 // Not allowing time more than a day 510 if (timeUs <= 0 || timeUs > 86400*1E6) { 511 LOGE("Time between time lapse frame capture (%lld) is out of range [0, 1 Day]", timeUs); 512 return BAD_VALUE; 513 } 514 515 mTimeBetweenTimeLapseFrameCaptureUs = timeUs; 516 return OK; 517} 518 519status_t StagefrightRecorder::setParamAuxVideoWidth(int32_t width) { 520 LOGV("setParamAuxVideoWidth : %d", width); 521 522 if (width <= 0) { 523 LOGE("Width (%d) is not positive", width); 524 return BAD_VALUE; 525 } 526 527 mAuxVideoWidth = width; 528 return OK; 529} 530 531status_t StagefrightRecorder::setParamAuxVideoHeight(int32_t height) { 532 LOGV("setParamAuxVideoHeight : %d", height); 533 534 if (height <= 0) { 535 LOGE("Height (%d) is not positive", height); 536 return BAD_VALUE; 537 } 538 539 mAuxVideoHeight = height; 540 return OK; 541} 542 543status_t StagefrightRecorder::setParamAuxVideoEncodingBitRate(int32_t bitRate) { 544 LOGV("StagefrightRecorder::setParamAuxVideoEncodingBitRate: %d", bitRate); 545 546 if (bitRate <= 0) { 547 LOGE("Invalid video encoding bit rate: %d", bitRate); 548 return BAD_VALUE; 549 } 550 551 mAuxVideoBitRate = bitRate; 552 return OK; 553} 554 555status_t StagefrightRecorder::setParameter( 556 const String8 &key, const String8 &value) { 557 LOGV("setParameter: key (%s) => value (%s)", key.string(), value.string()); 558 if (key == "max-duration") { 559 int64_t max_duration_ms; 560 if (safe_strtoi64(value.string(), &max_duration_ms)) { 561 return setParamMaxFileDurationUs(1000LL * max_duration_ms); 562 } 563 } else if (key == "max-filesize") { 564 int64_t max_filesize_bytes; 565 if (safe_strtoi64(value.string(), &max_filesize_bytes)) { 566 return setParamMaxFileSizeBytes(max_filesize_bytes); 567 } 568 } else if (key == "interleave-duration-us") { 569 int32_t durationUs; 570 if (safe_strtoi32(value.string(), &durationUs)) { 571 return setParamInterleaveDuration(durationUs); 572 } 573 } else if (key == "param-movie-time-scale") { 574 int32_t timeScale; 575 if (safe_strtoi32(value.string(), &timeScale)) { 576 return setParamMovieTimeScale(timeScale); 577 } 578 } else if (key == "param-use-64bit-offset") { 579 int32_t use64BitOffset; 580 if (safe_strtoi32(value.string(), &use64BitOffset)) { 581 return setParam64BitFileOffset(use64BitOffset != 0); 582 } 583 } else if (key == "param-track-time-status") { 584 int64_t timeDurationUs; 585 if (safe_strtoi64(value.string(), &timeDurationUs)) { 586 return setParamTrackTimeStatus(timeDurationUs); 587 } 588 } else if (key == "audio-param-sampling-rate") { 589 int32_t sampling_rate; 590 if (safe_strtoi32(value.string(), &sampling_rate)) { 591 return setParamAudioSamplingRate(sampling_rate); 592 } 593 } else if (key == "audio-param-number-of-channels") { 594 int32_t number_of_channels; 595 if (safe_strtoi32(value.string(), &number_of_channels)) { 596 return setParamAudioNumberOfChannels(number_of_channels); 597 } 598 } else if (key == "audio-param-encoding-bitrate") { 599 int32_t audio_bitrate; 600 if (safe_strtoi32(value.string(), &audio_bitrate)) { 601 return setParamAudioEncodingBitRate(audio_bitrate); 602 } 603 } else if (key == "audio-param-time-scale") { 604 int32_t timeScale; 605 if (safe_strtoi32(value.string(), &timeScale)) { 606 return setParamAudioTimeScale(timeScale); 607 } 608 } else if (key == "video-param-encoding-bitrate") { 609 int32_t video_bitrate; 610 if (safe_strtoi32(value.string(), &video_bitrate)) { 611 return setParamVideoEncodingBitRate(video_bitrate); 612 } 613 } else if (key == "video-param-rotation-angle-degrees") { 614 int32_t degrees; 615 if (safe_strtoi32(value.string(), °rees)) { 616 return setParamVideoRotation(degrees); 617 } 618 } else if (key == "video-param-i-frames-interval") { 619 int32_t seconds; 620 if (safe_strtoi32(value.string(), &seconds)) { 621 return setParamVideoIFramesInterval(seconds); 622 } 623 } else if (key == "video-param-encoder-profile") { 624 int32_t profile; 625 if (safe_strtoi32(value.string(), &profile)) { 626 return setParamVideoEncoderProfile(profile); 627 } 628 } else if (key == "video-param-encoder-level") { 629 int32_t level; 630 if (safe_strtoi32(value.string(), &level)) { 631 return setParamVideoEncoderLevel(level); 632 } 633 } else if (key == "video-param-camera-id") { 634 int32_t cameraId; 635 if (safe_strtoi32(value.string(), &cameraId)) { 636 return setParamVideoCameraId(cameraId); 637 } 638 } else if (key == "video-param-time-scale") { 639 int32_t timeScale; 640 if (safe_strtoi32(value.string(), &timeScale)) { 641 return setParamVideoTimeScale(timeScale); 642 } 643 } else if (key == "time-lapse-enable") { 644 int32_t timeLapseEnable; 645 if (safe_strtoi32(value.string(), &timeLapseEnable)) { 646 return setParamTimeLapseEnable(timeLapseEnable); 647 } 648 } else if (key == "time-between-time-lapse-frame-capture") { 649 int64_t timeBetweenTimeLapseFrameCaptureMs; 650 if (safe_strtoi64(value.string(), &timeBetweenTimeLapseFrameCaptureMs)) { 651 return setParamTimeBetweenTimeLapseFrameCapture( 652 1000LL * timeBetweenTimeLapseFrameCaptureMs); 653 } 654 } else if (key == "video-aux-param-width") { 655 int32_t auxWidth; 656 if (safe_strtoi32(value.string(), &auxWidth)) { 657 return setParamAuxVideoWidth(auxWidth); 658 } 659 } else if (key == "video-aux-param-height") { 660 int32_t auxHeight; 661 if (safe_strtoi32(value.string(), &auxHeight)) { 662 return setParamAuxVideoHeight(auxHeight); 663 } 664 } else if (key == "video-aux-param-encoding-bitrate") { 665 int32_t auxVideoBitRate; 666 if (safe_strtoi32(value.string(), &auxVideoBitRate)) { 667 return setParamAuxVideoEncodingBitRate(auxVideoBitRate); 668 } 669 } else { 670 LOGE("setParameter: failed to find key %s", key.string()); 671 } 672 return BAD_VALUE; 673} 674 675status_t StagefrightRecorder::setParameters(const String8 ¶ms) { 676 LOGV("setParameters: %s", params.string()); 677 const char *cparams = params.string(); 678 const char *key_start = cparams; 679 for (;;) { 680 const char *equal_pos = strchr(key_start, '='); 681 if (equal_pos == NULL) { 682 LOGE("Parameters %s miss a value", cparams); 683 return BAD_VALUE; 684 } 685 String8 key(key_start, equal_pos - key_start); 686 TrimString(&key); 687 if (key.length() == 0) { 688 LOGE("Parameters %s contains an empty key", cparams); 689 return BAD_VALUE; 690 } 691 const char *value_start = equal_pos + 1; 692 const char *semicolon_pos = strchr(value_start, ';'); 693 String8 value; 694 if (semicolon_pos == NULL) { 695 value.setTo(value_start); 696 } else { 697 value.setTo(value_start, semicolon_pos - value_start); 698 } 699 if (setParameter(key, value) != OK) { 700 return BAD_VALUE; 701 } 702 if (semicolon_pos == NULL) { 703 break; // Reaches the end 704 } 705 key_start = semicolon_pos + 1; 706 } 707 return OK; 708} 709 710status_t StagefrightRecorder::setListener(const sp<IMediaRecorderClient> &listener) { 711 mListener = listener; 712 713 return OK; 714} 715 716status_t StagefrightRecorder::prepare() { 717 return OK; 718} 719 720status_t StagefrightRecorder::start() { 721 CHECK(mOutputFd >= 0); 722 723 if (mWriter != NULL) { 724 LOGE("File writer is not avaialble"); 725 return UNKNOWN_ERROR; 726 } 727 728 switch (mOutputFormat) { 729 case OUTPUT_FORMAT_DEFAULT: 730 case OUTPUT_FORMAT_THREE_GPP: 731 case OUTPUT_FORMAT_MPEG_4: 732 return startMPEG4Recording(); 733 734 case OUTPUT_FORMAT_AMR_NB: 735 case OUTPUT_FORMAT_AMR_WB: 736 return startAMRRecording(); 737 738 case OUTPUT_FORMAT_AAC_ADIF: 739 case OUTPUT_FORMAT_AAC_ADTS: 740 return startAACRecording(); 741 742 case OUTPUT_FORMAT_RTP_AVP: 743 return startRTPRecording(); 744 745 case OUTPUT_FORMAT_MPEG2TS: 746 return startMPEG2TSRecording(); 747 748 default: 749 LOGE("Unsupported output file format: %d", mOutputFormat); 750 return UNKNOWN_ERROR; 751 } 752} 753 754sp<MediaSource> StagefrightRecorder::createAudioSource() { 755 sp<AudioSource> audioSource = 756 new AudioSource( 757 mAudioSource, 758 mSampleRate, 759 mAudioChannels); 760 761 status_t err = audioSource->initCheck(); 762 763 if (err != OK) { 764 LOGE("audio source is not initialized"); 765 return NULL; 766 } 767 768 sp<MetaData> encMeta = new MetaData; 769 const char *mime; 770 switch (mAudioEncoder) { 771 case AUDIO_ENCODER_AMR_NB: 772 case AUDIO_ENCODER_DEFAULT: 773 mime = MEDIA_MIMETYPE_AUDIO_AMR_NB; 774 break; 775 case AUDIO_ENCODER_AMR_WB: 776 mime = MEDIA_MIMETYPE_AUDIO_AMR_WB; 777 break; 778 case AUDIO_ENCODER_AAC: 779 mime = MEDIA_MIMETYPE_AUDIO_AAC; 780 break; 781 default: 782 LOGE("Unknown audio encoder: %d", mAudioEncoder); 783 return NULL; 784 } 785 encMeta->setCString(kKeyMIMEType, mime); 786 787 int32_t maxInputSize; 788 CHECK(audioSource->getFormat()->findInt32( 789 kKeyMaxInputSize, &maxInputSize)); 790 791 encMeta->setInt32(kKeyMaxInputSize, maxInputSize); 792 encMeta->setInt32(kKeyChannelCount, mAudioChannels); 793 encMeta->setInt32(kKeySampleRate, mSampleRate); 794 encMeta->setInt32(kKeyBitRate, mAudioBitRate); 795 if (mAudioTimeScale > 0) { 796 encMeta->setInt32(kKeyTimeScale, mAudioTimeScale); 797 } 798 799 OMXClient client; 800 CHECK_EQ(client.connect(), OK); 801 802 sp<MediaSource> audioEncoder = 803 OMXCodec::Create(client.interface(), encMeta, 804 true /* createEncoder */, audioSource); 805 mAudioSourceNode = audioSource; 806 807 return audioEncoder; 808} 809 810status_t StagefrightRecorder::startAACRecording() { 811 CHECK(mOutputFormat == OUTPUT_FORMAT_AAC_ADIF || 812 mOutputFormat == OUTPUT_FORMAT_AAC_ADTS); 813 814 CHECK(mAudioEncoder == AUDIO_ENCODER_AAC); 815 CHECK(mAudioSource != AUDIO_SOURCE_LIST_END); 816 817 CHECK(0 == "AACWriter is not implemented yet"); 818 819 return OK; 820} 821 822status_t StagefrightRecorder::startAMRRecording() { 823 CHECK(mOutputFormat == OUTPUT_FORMAT_AMR_NB || 824 mOutputFormat == OUTPUT_FORMAT_AMR_WB); 825 826 if (mOutputFormat == OUTPUT_FORMAT_AMR_NB) { 827 if (mAudioEncoder != AUDIO_ENCODER_DEFAULT && 828 mAudioEncoder != AUDIO_ENCODER_AMR_NB) { 829 LOGE("Invalid encoder %d used for AMRNB recording", 830 mAudioEncoder); 831 return BAD_VALUE; 832 } 833 if (mSampleRate != 8000) { 834 LOGE("Invalid sampling rate %d used for AMRNB recording", 835 mSampleRate); 836 return BAD_VALUE; 837 } 838 } else { // mOutputFormat must be OUTPUT_FORMAT_AMR_WB 839 if (mAudioEncoder != AUDIO_ENCODER_AMR_WB) { 840 LOGE("Invlaid encoder %d used for AMRWB recording", 841 mAudioEncoder); 842 return BAD_VALUE; 843 } 844 if (mSampleRate != 16000) { 845 LOGE("Invalid sample rate %d used for AMRWB recording", 846 mSampleRate); 847 return BAD_VALUE; 848 } 849 } 850 if (mAudioChannels != 1) { 851 LOGE("Invalid number of audio channels %d used for amr recording", 852 mAudioChannels); 853 return BAD_VALUE; 854 } 855 856 if (mAudioSource >= AUDIO_SOURCE_LIST_END) { 857 LOGE("Invalid audio source: %d", mAudioSource); 858 return BAD_VALUE; 859 } 860 861 sp<MediaSource> audioEncoder = createAudioSource(); 862 863 if (audioEncoder == NULL) { 864 return UNKNOWN_ERROR; 865 } 866 867 mWriter = new AMRWriter(dup(mOutputFd)); 868 mWriter->addSource(audioEncoder); 869 870 if (mMaxFileDurationUs != 0) { 871 mWriter->setMaxFileDuration(mMaxFileDurationUs); 872 } 873 if (mMaxFileSizeBytes != 0) { 874 mWriter->setMaxFileSize(mMaxFileSizeBytes); 875 } 876 mWriter->setListener(mListener); 877 mWriter->start(); 878 879 return OK; 880} 881 882status_t StagefrightRecorder::startRTPRecording() { 883 CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_RTP_AVP); 884 885 if ((mAudioSource != AUDIO_SOURCE_LIST_END 886 && mVideoSource != VIDEO_SOURCE_LIST_END) 887 || (mAudioSource == AUDIO_SOURCE_LIST_END 888 && mVideoSource == VIDEO_SOURCE_LIST_END)) { 889 // Must have exactly one source. 890 return BAD_VALUE; 891 } 892 893 if (mOutputFd < 0) { 894 return BAD_VALUE; 895 } 896 897 sp<MediaSource> source; 898 899 if (mAudioSource != AUDIO_SOURCE_LIST_END) { 900 source = createAudioSource(); 901 } else { 902 903 sp<CameraSource> cameraSource; 904 status_t err = setupCameraSource(&cameraSource); 905 if (err != OK) { 906 return err; 907 } 908 909 err = setupVideoEncoder(cameraSource, mVideoBitRate, &source); 910 if (err != OK) { 911 return err; 912 } 913 } 914 915 mWriter = new ARTPWriter(dup(mOutputFd)); 916 mWriter->addSource(source); 917 mWriter->setListener(mListener); 918 919 return mWriter->start(); 920} 921 922status_t StagefrightRecorder::startMPEG2TSRecording() { 923 CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_MPEG2TS); 924 925 sp<MediaWriter> writer = new MPEG2TSWriter(dup(mOutputFd)); 926 927 if (mAudioSource != AUDIO_SOURCE_LIST_END) { 928 if (mAudioEncoder != AUDIO_ENCODER_AAC) { 929 return ERROR_UNSUPPORTED; 930 } 931 932 status_t err = setupAudioEncoder(writer); 933 934 if (err != OK) { 935 return err; 936 } 937 } 938 939 if (mVideoSource == VIDEO_SOURCE_DEFAULT 940 || mVideoSource == VIDEO_SOURCE_CAMERA) { 941 if (mVideoEncoder != VIDEO_ENCODER_H264) { 942 return ERROR_UNSUPPORTED; 943 } 944 945 sp<CameraSource> cameraSource; 946 status_t err = setupCameraSource(&cameraSource); 947 if (err != OK) { 948 return err; 949 } 950 951 sp<MediaSource> encoder; 952 err = setupVideoEncoder(cameraSource, mVideoBitRate, &encoder); 953 954 if (err != OK) { 955 return err; 956 } 957 958 writer->addSource(encoder); 959 } 960 961 if (mMaxFileDurationUs != 0) { 962 writer->setMaxFileDuration(mMaxFileDurationUs); 963 } 964 965 if (mMaxFileSizeBytes != 0) { 966 writer->setMaxFileSize(mMaxFileSizeBytes); 967 } 968 969 mWriter = writer; 970 971 return mWriter->start(); 972} 973 974void StagefrightRecorder::clipVideoFrameRate() { 975 LOGV("clipVideoFrameRate: encoder %d", mVideoEncoder); 976 int minFrameRate = mEncoderProfiles->getVideoEncoderParamByName( 977 "enc.vid.fps.min", mVideoEncoder); 978 int maxFrameRate = mEncoderProfiles->getVideoEncoderParamByName( 979 "enc.vid.fps.max", mVideoEncoder); 980 if (mFrameRate < minFrameRate && mFrameRate != -1) { 981 LOGW("Intended video encoding frame rate (%d fps) is too small" 982 " and will be set to (%d fps)", mFrameRate, minFrameRate); 983 mFrameRate = minFrameRate; 984 } else if (mFrameRate > maxFrameRate) { 985 LOGW("Intended video encoding frame rate (%d fps) is too large" 986 " and will be set to (%d fps)", mFrameRate, maxFrameRate); 987 mFrameRate = maxFrameRate; 988 } 989} 990 991void StagefrightRecorder::clipVideoBitRate() { 992 LOGV("clipVideoBitRate: encoder %d", mVideoEncoder); 993 int minBitRate = mEncoderProfiles->getVideoEncoderParamByName( 994 "enc.vid.bps.min", mVideoEncoder); 995 int maxBitRate = mEncoderProfiles->getVideoEncoderParamByName( 996 "enc.vid.bps.max", mVideoEncoder); 997 if (mVideoBitRate < minBitRate) { 998 LOGW("Intended video encoding bit rate (%d bps) is too small" 999 " and will be set to (%d bps)", mVideoBitRate, minBitRate); 1000 mVideoBitRate = minBitRate; 1001 } else if (mVideoBitRate > maxBitRate) { 1002 LOGW("Intended video encoding bit rate (%d bps) is too large" 1003 " and will be set to (%d bps)", mVideoBitRate, maxBitRate); 1004 mVideoBitRate = maxBitRate; 1005 } 1006} 1007 1008void StagefrightRecorder::clipVideoFrameWidth() { 1009 LOGV("clipVideoFrameWidth: encoder %d", mVideoEncoder); 1010 int minFrameWidth = mEncoderProfiles->getVideoEncoderParamByName( 1011 "enc.vid.width.min", mVideoEncoder); 1012 int maxFrameWidth = mEncoderProfiles->getVideoEncoderParamByName( 1013 "enc.vid.width.max", mVideoEncoder); 1014 if (mVideoWidth < minFrameWidth) { 1015 LOGW("Intended video encoding frame width (%d) is too small" 1016 " and will be set to (%d)", mVideoWidth, minFrameWidth); 1017 mVideoWidth = minFrameWidth; 1018 } else if (mVideoWidth > maxFrameWidth) { 1019 LOGW("Intended video encoding frame width (%d) is too large" 1020 " and will be set to (%d)", mVideoWidth, maxFrameWidth); 1021 mVideoWidth = maxFrameWidth; 1022 } 1023} 1024 1025status_t StagefrightRecorder::checkVideoEncoderCapabilities() { 1026 if (!mCaptureTimeLapse) { 1027 // Dont clip for time lapse capture as encoder will have enough 1028 // time to encode because of slow capture rate of time lapse. 1029 clipVideoBitRate(); 1030 clipVideoFrameRate(); 1031 clipVideoFrameWidth(); 1032 clipVideoFrameHeight(); 1033 } 1034 return OK; 1035} 1036 1037void StagefrightRecorder::clipVideoFrameHeight() { 1038 LOGV("clipVideoFrameHeight: encoder %d", mVideoEncoder); 1039 int minFrameHeight = mEncoderProfiles->getVideoEncoderParamByName( 1040 "enc.vid.height.min", mVideoEncoder); 1041 int maxFrameHeight = mEncoderProfiles->getVideoEncoderParamByName( 1042 "enc.vid.height.max", mVideoEncoder); 1043 if (mVideoHeight < minFrameHeight) { 1044 LOGW("Intended video encoding frame height (%d) is too small" 1045 " and will be set to (%d)", mVideoHeight, minFrameHeight); 1046 mVideoHeight = minFrameHeight; 1047 } else if (mVideoHeight > maxFrameHeight) { 1048 LOGW("Intended video encoding frame height (%d) is too large" 1049 " and will be set to (%d)", mVideoHeight, maxFrameHeight); 1050 mVideoHeight = maxFrameHeight; 1051 } 1052} 1053 1054status_t StagefrightRecorder::setupCameraSource( 1055 sp<CameraSource> *cameraSource) { 1056 status_t err = OK; 1057 if ((err = checkVideoEncoderCapabilities()) != OK) { 1058 return err; 1059 } 1060 Size videoSize; 1061 videoSize.width = mVideoWidth; 1062 videoSize.height = mVideoHeight; 1063 if (mCaptureTimeLapse) { 1064 mCameraSourceTimeLapse = CameraSourceTimeLapse::CreateFromCamera( 1065 mCamera, mCameraId, 1066 videoSize, mFrameRate, mPreviewSurface, 1067 mTimeBetweenTimeLapseFrameCaptureUs); 1068 *cameraSource = mCameraSourceTimeLapse; 1069 } else { 1070 *cameraSource = CameraSource::CreateFromCamera( 1071 mCamera, mCameraId, videoSize, mFrameRate, 1072 mPreviewSurface, true /*storeMetaDataInVideoBuffers*/); 1073 } 1074 CHECK(*cameraSource != NULL); 1075 1076 // When frame rate is not set, the actual frame rate will be set to 1077 // the current frame rate being used. 1078 if (mFrameRate == -1) { 1079 int32_t frameRate = 0; 1080 CHECK ((*cameraSource)->getFormat()->findInt32( 1081 kKeySampleRate, &frameRate)); 1082 LOGI("Frame rate is not explicitly set. Use the current frame " 1083 "rate (%d fps)", frameRate); 1084 mFrameRate = frameRate; 1085 } 1086 1087 CHECK(mFrameRate != -1); 1088 1089 mIsMetaDataStoredInVideoBuffers = 1090 (*cameraSource)->isMetaDataStoredInVideoBuffers(); 1091 1092 return OK; 1093} 1094 1095status_t StagefrightRecorder::setupVideoEncoder( 1096 sp<MediaSource> cameraSource, 1097 int32_t videoBitRate, 1098 sp<MediaSource> *source) { 1099 source->clear(); 1100 1101 sp<MetaData> enc_meta = new MetaData; 1102 enc_meta->setInt32(kKeyBitRate, videoBitRate); 1103 enc_meta->setInt32(kKeySampleRate, mFrameRate); 1104 1105 switch (mVideoEncoder) { 1106 case VIDEO_ENCODER_H263: 1107 enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263); 1108 break; 1109 1110 case VIDEO_ENCODER_MPEG_4_SP: 1111 enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4); 1112 break; 1113 1114 case VIDEO_ENCODER_H264: 1115 enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC); 1116 break; 1117 1118 default: 1119 CHECK(!"Should not be here, unsupported video encoding."); 1120 break; 1121 } 1122 1123 sp<MetaData> meta = cameraSource->getFormat(); 1124 1125 int32_t width, height, stride, sliceHeight, colorFormat; 1126 CHECK(meta->findInt32(kKeyWidth, &width)); 1127 CHECK(meta->findInt32(kKeyHeight, &height)); 1128 CHECK(meta->findInt32(kKeyStride, &stride)); 1129 CHECK(meta->findInt32(kKeySliceHeight, &sliceHeight)); 1130 CHECK(meta->findInt32(kKeyColorFormat, &colorFormat)); 1131 1132 enc_meta->setInt32(kKeyWidth, width); 1133 enc_meta->setInt32(kKeyHeight, height); 1134 enc_meta->setInt32(kKeyIFramesInterval, mIFramesIntervalSec); 1135 enc_meta->setInt32(kKeyStride, stride); 1136 enc_meta->setInt32(kKeySliceHeight, sliceHeight); 1137 enc_meta->setInt32(kKeyColorFormat, colorFormat); 1138 if (mVideoTimeScale > 0) { 1139 enc_meta->setInt32(kKeyTimeScale, mVideoTimeScale); 1140 } 1141 if (mVideoEncoderProfile != -1) { 1142 enc_meta->setInt32(kKeyVideoProfile, mVideoEncoderProfile); 1143 } 1144 if (mVideoEncoderLevel != -1) { 1145 enc_meta->setInt32(kKeyVideoLevel, mVideoEncoderLevel); 1146 } else if (mCaptureTimeLapse) { 1147 // Check if we are using high resolution and/or high bitrate and 1148 // set appropriate level for the software AVCEncoder. 1149 if ((width * height >= 921600) // 720p 1150 || (videoBitRate >= 20000000)) { 1151 enc_meta->setInt32(kKeyVideoLevel, 50); 1152 } 1153 } 1154 1155 OMXClient client; 1156 CHECK_EQ(client.connect(), OK); 1157 1158 // Use software codec for time lapse 1159 uint32_t encoder_flags = (mCaptureTimeLapse) ? OMXCodec::kPreferSoftwareCodecs : 0; 1160 if (mIsMetaDataStoredInVideoBuffers) { 1161 encoder_flags |= OMXCodec::kStoreMetaDataInVideoBuffers; 1162 } 1163 sp<MediaSource> encoder = OMXCodec::Create( 1164 client.interface(), enc_meta, 1165 true /* createEncoder */, cameraSource, 1166 NULL, encoder_flags); 1167 if (encoder == NULL) { 1168 return UNKNOWN_ERROR; 1169 } 1170 1171 *source = encoder; 1172 1173 return OK; 1174} 1175 1176status_t StagefrightRecorder::setupAudioEncoder(const sp<MediaWriter>& writer) { 1177 sp<MediaSource> audioEncoder; 1178 switch(mAudioEncoder) { 1179 case AUDIO_ENCODER_AMR_NB: 1180 case AUDIO_ENCODER_AMR_WB: 1181 case AUDIO_ENCODER_AAC: 1182 audioEncoder = createAudioSource(); 1183 break; 1184 default: 1185 LOGE("Unsupported audio encoder: %d", mAudioEncoder); 1186 return UNKNOWN_ERROR; 1187 } 1188 1189 if (audioEncoder == NULL) { 1190 return UNKNOWN_ERROR; 1191 } 1192 1193 writer->addSource(audioEncoder); 1194 return OK; 1195} 1196 1197status_t StagefrightRecorder::setupMPEG4Recording( 1198 bool useSplitCameraSource, 1199 int outputFd, 1200 int32_t videoWidth, int32_t videoHeight, 1201 int32_t videoBitRate, 1202 int32_t *totalBitRate, 1203 sp<MediaWriter> *mediaWriter) { 1204 mediaWriter->clear(); 1205 *totalBitRate = 0; 1206 status_t err = OK; 1207 sp<MediaWriter> writer = new MPEG4Writer(dup(outputFd)); 1208 1209 // Add audio source first if it exists 1210 if (!mCaptureTimeLapse && (mAudioSource != AUDIO_SOURCE_LIST_END)) { 1211 err = setupAudioEncoder(writer); 1212 if (err != OK) return err; 1213 *totalBitRate += mAudioBitRate; 1214 } 1215 if (mVideoSource == VIDEO_SOURCE_DEFAULT 1216 || mVideoSource == VIDEO_SOURCE_CAMERA) { 1217 1218 sp<MediaSource> cameraMediaSource; 1219 if (useSplitCameraSource) { 1220 LOGV("Using Split camera source"); 1221 cameraMediaSource = mCameraSourceSplitter->createClient(); 1222 } else { 1223 sp<CameraSource> cameraSource; 1224 err = setupCameraSource(&cameraSource); 1225 cameraMediaSource = cameraSource; 1226 } 1227 if ((videoWidth != mVideoWidth) || (videoHeight != mVideoHeight)) { 1228 // Use downsampling from the original source. 1229 cameraMediaSource = 1230 new VideoSourceDownSampler(cameraMediaSource, videoWidth, videoHeight); 1231 } 1232 if (err != OK) { 1233 return err; 1234 } 1235 1236 sp<MediaSource> encoder; 1237 err = setupVideoEncoder(cameraMediaSource, videoBitRate, &encoder); 1238 if (err != OK) { 1239 return err; 1240 } 1241 1242 writer->addSource(encoder); 1243 *totalBitRate += videoBitRate; 1244 } 1245 1246 if (mInterleaveDurationUs > 0) { 1247 reinterpret_cast<MPEG4Writer *>(writer.get())-> 1248 setInterleaveDuration(mInterleaveDurationUs); 1249 } 1250 if (mMaxFileDurationUs != 0) { 1251 writer->setMaxFileDuration(mMaxFileDurationUs); 1252 } 1253 if (mMaxFileSizeBytes != 0) { 1254 writer->setMaxFileSize(mMaxFileSizeBytes); 1255 } 1256 1257 writer->setListener(mListener); 1258 *mediaWriter = writer; 1259 return OK; 1260} 1261 1262void StagefrightRecorder::setupMPEG4MetaData(int64_t startTimeUs, int32_t totalBitRate, 1263 sp<MetaData> *meta) { 1264 (*meta)->setInt64(kKeyTime, startTimeUs); 1265 (*meta)->setInt32(kKeyFileType, mOutputFormat); 1266 (*meta)->setInt32(kKeyBitRate, totalBitRate); 1267 (*meta)->setInt32(kKey64BitFileOffset, mUse64BitFileOffset); 1268 if (mMovieTimeScale > 0) { 1269 (*meta)->setInt32(kKeyTimeScale, mMovieTimeScale); 1270 } 1271 if (mTrackEveryTimeDurationUs > 0) { 1272 (*meta)->setInt64(kKeyTrackTimeStatus, mTrackEveryTimeDurationUs); 1273 } 1274 if (mRotationDegrees != 0) { 1275 (*meta)->setInt32(kKeyRotation, mRotationDegrees); 1276 } 1277} 1278 1279status_t StagefrightRecorder::startMPEG4Recording() { 1280 if (mCaptureAuxVideo) { 1281 if (!mCaptureTimeLapse) { 1282 LOGE("Auxiliary video can be captured only in time lapse mode"); 1283 return UNKNOWN_ERROR; 1284 } 1285 LOGV("Creating MediaSourceSplitter"); 1286 sp<CameraSource> cameraSource; 1287 status_t err = setupCameraSource(&cameraSource); 1288 if (err != OK) { 1289 return err; 1290 } 1291 mCameraSourceSplitter = new MediaSourceSplitter(cameraSource); 1292 } else { 1293 mCameraSourceSplitter = NULL; 1294 } 1295 1296 int32_t totalBitRate; 1297 status_t err = setupMPEG4Recording(mCaptureAuxVideo, 1298 mOutputFd, mVideoWidth, mVideoHeight, 1299 mVideoBitRate, &totalBitRate, &mWriter); 1300 if (err != OK) { 1301 return err; 1302 } 1303 1304 int64_t startTimeUs = systemTime() / 1000; 1305 sp<MetaData> meta = new MetaData; 1306 setupMPEG4MetaData(startTimeUs, totalBitRate, &meta); 1307 1308 err = mWriter->start(meta.get()); 1309 if (err != OK) { 1310 return err; 1311 } 1312 1313 if (mCaptureAuxVideo) { 1314 CHECK(mOutputFdAux >= 0); 1315 if (mWriterAux != NULL) { 1316 LOGE("Auxiliary File writer is not avaialble"); 1317 return UNKNOWN_ERROR; 1318 } 1319 if ((mAuxVideoWidth > mVideoWidth) || (mAuxVideoHeight > mVideoHeight) || 1320 ((mAuxVideoWidth == mVideoWidth) && mAuxVideoHeight == mVideoHeight)) { 1321 LOGE("Auxiliary video size (%d x %d) same or larger than the main video size (%d x %d)", 1322 mAuxVideoWidth, mAuxVideoHeight, mVideoWidth, mVideoHeight); 1323 return UNKNOWN_ERROR; 1324 } 1325 1326 int32_t totalBitrateAux; 1327 err = setupMPEG4Recording(mCaptureAuxVideo, 1328 mOutputFdAux, mAuxVideoWidth, mAuxVideoHeight, 1329 mAuxVideoBitRate, &totalBitrateAux, &mWriterAux); 1330 if (err != OK) { 1331 return err; 1332 } 1333 1334 sp<MetaData> metaAux = new MetaData; 1335 setupMPEG4MetaData(startTimeUs, totalBitrateAux, &metaAux); 1336 1337 return mWriterAux->start(metaAux.get()); 1338 } 1339 1340 return OK; 1341} 1342 1343status_t StagefrightRecorder::pause() { 1344 LOGV("pause"); 1345 if (mWriter == NULL) { 1346 return UNKNOWN_ERROR; 1347 } 1348 mWriter->pause(); 1349 1350 if (mCaptureAuxVideo) { 1351 if (mWriterAux == NULL) { 1352 return UNKNOWN_ERROR; 1353 } 1354 mWriterAux->pause(); 1355 } 1356 1357 return OK; 1358} 1359 1360status_t StagefrightRecorder::stop() { 1361 LOGV("stop"); 1362 status_t err = OK; 1363 1364 if (mCaptureTimeLapse && mCameraSourceTimeLapse != NULL) { 1365 mCameraSourceTimeLapse->startQuickReadReturns(); 1366 mCameraSourceTimeLapse = NULL; 1367 } 1368 1369 if (mCaptureAuxVideo) { 1370 if (mWriterAux != NULL) { 1371 mWriterAux->stop(); 1372 mWriterAux.clear(); 1373 } 1374 } 1375 1376 if (mWriter != NULL) { 1377 err = mWriter->stop(); 1378 mWriter.clear(); 1379 } 1380 1381 if (mOutputFd >= 0) { 1382 ::close(mOutputFd); 1383 mOutputFd = -1; 1384 } 1385 1386 if (mCaptureAuxVideo) { 1387 if (mOutputFdAux >= 0) { 1388 ::close(mOutputFdAux); 1389 mOutputFdAux = -1; 1390 } 1391 } 1392 1393 return err; 1394} 1395 1396status_t StagefrightRecorder::close() { 1397 LOGV("close"); 1398 stop(); 1399 1400 return OK; 1401} 1402 1403status_t StagefrightRecorder::reset() { 1404 LOGV("reset"); 1405 stop(); 1406 1407 // No audio or video source by default 1408 mAudioSource = AUDIO_SOURCE_LIST_END; 1409 mVideoSource = VIDEO_SOURCE_LIST_END; 1410 1411 // Default parameters 1412 mOutputFormat = OUTPUT_FORMAT_THREE_GPP; 1413 mAudioEncoder = AUDIO_ENCODER_AMR_NB; 1414 mVideoEncoder = VIDEO_ENCODER_H263; 1415 mVideoWidth = 176; 1416 mVideoHeight = 144; 1417 mAuxVideoWidth = 176; 1418 mAuxVideoHeight = 144; 1419 mFrameRate = -1; 1420 mVideoBitRate = 192000; 1421 mAuxVideoBitRate = 192000; 1422 mSampleRate = 8000; 1423 mAudioChannels = 1; 1424 mAudioBitRate = 12200; 1425 mInterleaveDurationUs = 0; 1426 mIFramesIntervalSec = 1; 1427 mAudioSourceNode = 0; 1428 mUse64BitFileOffset = false; 1429 mMovieTimeScale = -1; 1430 mAudioTimeScale = -1; 1431 mVideoTimeScale = -1; 1432 mCameraId = 0; 1433 mVideoEncoderProfile = -1; 1434 mVideoEncoderLevel = -1; 1435 mMaxFileDurationUs = 0; 1436 mMaxFileSizeBytes = 0; 1437 mTrackEveryTimeDurationUs = 0; 1438 mCaptureTimeLapse = false; 1439 mTimeBetweenTimeLapseFrameCaptureUs = -1; 1440 mCaptureAuxVideo = false; 1441 mCameraSourceSplitter = NULL; 1442 mCameraSourceTimeLapse = NULL; 1443 mIsMetaDataStoredInVideoBuffers = false; 1444 mEncoderProfiles = MediaProfiles::getInstance(); 1445 1446 mOutputFd = -1; 1447 mOutputFdAux = -1; 1448 1449 return OK; 1450} 1451 1452status_t StagefrightRecorder::getMaxAmplitude(int *max) { 1453 LOGV("getMaxAmplitude"); 1454 1455 if (max == NULL) { 1456 LOGE("Null pointer argument"); 1457 return BAD_VALUE; 1458 } 1459 1460 if (mAudioSourceNode != 0) { 1461 *max = mAudioSourceNode->getMaxAmplitude(); 1462 } else { 1463 *max = 0; 1464 } 1465 1466 return OK; 1467} 1468 1469status_t StagefrightRecorder::dump( 1470 int fd, const Vector<String16>& args) const { 1471 LOGV("dump"); 1472 const size_t SIZE = 256; 1473 char buffer[SIZE]; 1474 String8 result; 1475 if (mWriter != 0) { 1476 mWriter->dump(fd, args); 1477 } else { 1478 snprintf(buffer, SIZE, " No file writer\n"); 1479 result.append(buffer); 1480 } 1481 snprintf(buffer, SIZE, " Recorder: %p\n", this); 1482 snprintf(buffer, SIZE, " Output file (fd %d):\n", mOutputFd); 1483 result.append(buffer); 1484 snprintf(buffer, SIZE, " Output file Auxiliary (fd %d):\n", mOutputFdAux); 1485 result.append(buffer); 1486 snprintf(buffer, SIZE, " File format: %d\n", mOutputFormat); 1487 result.append(buffer); 1488 snprintf(buffer, SIZE, " Max file size (bytes): %lld\n", mMaxFileSizeBytes); 1489 result.append(buffer); 1490 snprintf(buffer, SIZE, " Max file duration (us): %lld\n", mMaxFileDurationUs); 1491 result.append(buffer); 1492 snprintf(buffer, SIZE, " File offset length (bits): %d\n", mUse64BitFileOffset? 64: 32); 1493 result.append(buffer); 1494 snprintf(buffer, SIZE, " Interleave duration (us): %d\n", mInterleaveDurationUs); 1495 result.append(buffer); 1496 snprintf(buffer, SIZE, " Progress notification: %lld us\n", mTrackEveryTimeDurationUs); 1497 result.append(buffer); 1498 snprintf(buffer, SIZE, " Audio\n"); 1499 result.append(buffer); 1500 snprintf(buffer, SIZE, " Source: %d\n", mAudioSource); 1501 result.append(buffer); 1502 snprintf(buffer, SIZE, " Encoder: %d\n", mAudioEncoder); 1503 result.append(buffer); 1504 snprintf(buffer, SIZE, " Bit rate (bps): %d\n", mAudioBitRate); 1505 result.append(buffer); 1506 snprintf(buffer, SIZE, " Sampling rate (hz): %d\n", mSampleRate); 1507 result.append(buffer); 1508 snprintf(buffer, SIZE, " Number of channels: %d\n", mAudioChannels); 1509 result.append(buffer); 1510 snprintf(buffer, SIZE, " Max amplitude: %d\n", mAudioSourceNode == 0? 0: mAudioSourceNode->getMaxAmplitude()); 1511 result.append(buffer); 1512 snprintf(buffer, SIZE, " Video\n"); 1513 result.append(buffer); 1514 snprintf(buffer, SIZE, " Source: %d\n", mVideoSource); 1515 result.append(buffer); 1516 snprintf(buffer, SIZE, " Camera Id: %d\n", mCameraId); 1517 result.append(buffer); 1518 snprintf(buffer, SIZE, " Encoder: %d\n", mVideoEncoder); 1519 result.append(buffer); 1520 snprintf(buffer, SIZE, " Encoder profile: %d\n", mVideoEncoderProfile); 1521 result.append(buffer); 1522 snprintf(buffer, SIZE, " Encoder level: %d\n", mVideoEncoderLevel); 1523 result.append(buffer); 1524 snprintf(buffer, SIZE, " I frames interval (s): %d\n", mIFramesIntervalSec); 1525 result.append(buffer); 1526 snprintf(buffer, SIZE, " Frame size (pixels): %dx%d\n", mVideoWidth, mVideoHeight); 1527 result.append(buffer); 1528 snprintf(buffer, SIZE, " Aux Frame size (pixels): %dx%d\n", mAuxVideoWidth, mAuxVideoHeight); 1529 result.append(buffer); 1530 snprintf(buffer, SIZE, " Frame rate (fps): %d\n", mFrameRate); 1531 result.append(buffer); 1532 snprintf(buffer, SIZE, " Bit rate (bps): %d\n", mVideoBitRate); 1533 result.append(buffer); 1534 snprintf(buffer, SIZE, " Aux Bit rate (bps): %d\n", mAuxVideoBitRate); 1535 result.append(buffer); 1536 ::write(fd, result.string(), result.size()); 1537 return OK; 1538} 1539} // namespace android 1540