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