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