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