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 <inttypes.h> 20#include <utils/Log.h> 21 22#include "WebmWriter.h" 23#include "StagefrightRecorder.h" 24 25#include <algorithm> 26 27#include <android/hardware/ICamera.h> 28 29#include <binder/IPCThreadState.h> 30#include <binder/IServiceManager.h> 31 32#include <media/IMediaPlayerService.h> 33#include <media/MediaAnalyticsItem.h> 34#include <media/stagefright/foundation/ABuffer.h> 35#include <media/stagefright/foundation/ADebug.h> 36#include <media/stagefright/foundation/AMessage.h> 37#include <media/stagefright/foundation/ALooper.h> 38#include <media/stagefright/ACodec.h> 39#include <media/stagefright/AudioSource.h> 40#include <media/stagefright/AMRWriter.h> 41#include <media/stagefright/AACWriter.h> 42#include <media/stagefright/CameraSource.h> 43#include <media/stagefright/CameraSourceTimeLapse.h> 44#include <media/stagefright/MPEG2TSWriter.h> 45#include <media/stagefright/MPEG4Writer.h> 46#include <media/stagefright/MediaDefs.h> 47#include <media/stagefright/MetaData.h> 48#include <media/stagefright/MediaCodecSource.h> 49#include <media/stagefright/PersistentSurface.h> 50#include <media/MediaProfiles.h> 51#include <camera/CameraParameters.h> 52 53#include <utils/Errors.h> 54#include <sys/types.h> 55#include <ctype.h> 56#include <unistd.h> 57 58#include <system/audio.h> 59 60#include "ARTPWriter.h" 61 62namespace android { 63 64static const float kTypicalDisplayRefreshingRate = 60.f; 65// display refresh rate drops on battery saver 66static const float kMinTypicalDisplayRefreshingRate = kTypicalDisplayRefreshingRate / 2; 67static const int kMaxNumVideoTemporalLayers = 8; 68 69// key for media statistics 70static const char *kKeyRecorder = "recorder"; 71// attrs for media statistics 72static const char *kRecorderHeight = "android.media.mediarecorder.height"; 73static const char *kRecorderWidth = "android.media.mediarecorder.width"; 74static const char *kRecorderFrameRate = "android.media.mediarecorder.frame-rate"; 75static const char *kRecorderVideoBitrate = "android.media.mediarecorder.video-bitrate"; 76static const char *kRecorderAudioSampleRate = "android.media.mediarecorder.audio-samplerate"; 77static const char *kRecorderAudioChannels = "android.media.mediarecorder.audio-channels"; 78static const char *kRecorderAudioBitrate = "android.media.mediarecorder.audio-bitrate"; 79static const char *kRecorderVideoIframeInterval = "android.media.mediarecorder.video-iframe-interval"; 80static const char *kRecorderMovieTimescale = "android.media.mediarecorder.movie-timescale"; 81static const char *kRecorderAudioTimescale = "android.media.mediarecorder.audio-timescale"; 82static const char *kRecorderVideoTimescale = "android.media.mediarecorder.video-timescale"; 83static const char *kRecorderVideoProfile = "android.media.mediarecorder.video-encoder-profile"; 84static const char *kRecorderVideoLevel = "android.media.mediarecorder.video-encoder-level"; 85static const char *kRecorderCaptureFpsEnable = "android.media.mediarecorder.capture-fpsenable"; 86static const char *kRecorderCaptureFps = "android.media.mediarecorder.capture-fps"; 87static const char *kRecorderRotation = "android.media.mediarecorder.rotation"; 88 89// To collect the encoder usage for the battery app 90static void addBatteryData(uint32_t params) { 91 sp<IBinder> binder = 92 defaultServiceManager()->getService(String16("media.player")); 93 sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder); 94 CHECK(service.get() != NULL); 95 96 service->addBatteryData(params); 97} 98 99 100StagefrightRecorder::StagefrightRecorder(const String16 &opPackageName) 101 : MediaRecorderBase(opPackageName), 102 mWriter(NULL), 103 mOutputFd(-1), 104 mAudioSource(AUDIO_SOURCE_CNT), 105 mVideoSource(VIDEO_SOURCE_LIST_END), 106 mStarted(false) { 107 108 ALOGV("Constructor"); 109 110 mAnalyticsDirty = false; 111 reset(); 112} 113 114StagefrightRecorder::~StagefrightRecorder() { 115 ALOGV("Destructor"); 116 stop(); 117 118 if (mLooper != NULL) { 119 mLooper->stop(); 120 } 121 122 // log the current record, provided it has some information worth recording 123 if (mAnalyticsDirty && mAnalyticsItem != NULL) { 124 updateMetrics(); 125 if (mAnalyticsItem->count() > 0) { 126 mAnalyticsItem->setFinalized(true); 127 mAnalyticsItem->selfrecord(); 128 } 129 delete mAnalyticsItem; 130 mAnalyticsItem = NULL; 131 } 132} 133 134void StagefrightRecorder::updateMetrics() { 135 ALOGV("updateMetrics"); 136 137 // we'll populate the values from the raw fields. 138 // (NOT going to populate as we go through the various set* ops) 139 140 // TBD mOutputFormat = OUTPUT_FORMAT_THREE_GPP; 141 // TBD mAudioEncoder = AUDIO_ENCODER_AMR_NB; 142 // TBD mVideoEncoder = VIDEO_ENCODER_DEFAULT; 143 mAnalyticsItem->setInt32(kRecorderHeight, mVideoHeight); 144 mAnalyticsItem->setInt32(kRecorderWidth, mVideoWidth); 145 mAnalyticsItem->setInt32(kRecorderFrameRate, mFrameRate); 146 mAnalyticsItem->setInt32(kRecorderVideoBitrate, mVideoBitRate); 147 mAnalyticsItem->setInt32(kRecorderAudioSampleRate, mSampleRate); 148 mAnalyticsItem->setInt32(kRecorderAudioChannels, mAudioChannels); 149 mAnalyticsItem->setInt32(kRecorderAudioBitrate, mAudioBitRate); 150 // TBD mInterleaveDurationUs = 0; 151 mAnalyticsItem->setInt32(kRecorderVideoIframeInterval, mIFramesIntervalSec); 152 // TBD mAudioSourceNode = 0; 153 // TBD mUse64BitFileOffset = false; 154 mAnalyticsItem->setInt32(kRecorderMovieTimescale, mMovieTimeScale); 155 mAnalyticsItem->setInt32(kRecorderAudioTimescale, mAudioTimeScale); 156 mAnalyticsItem->setInt32(kRecorderVideoTimescale, mVideoTimeScale); 157 // TBD mCameraId = 0; 158 // TBD mStartTimeOffsetMs = -1; 159 mAnalyticsItem->setInt32(kRecorderVideoProfile, mVideoEncoderProfile); 160 mAnalyticsItem->setInt32(kRecorderVideoLevel, mVideoEncoderLevel); 161 // TBD mMaxFileDurationUs = 0; 162 // TBD mMaxFileSizeBytes = 0; 163 // TBD mTrackEveryTimeDurationUs = 0; 164 mAnalyticsItem->setInt32(kRecorderCaptureFpsEnable, mCaptureFpsEnable); 165 mAnalyticsItem->setDouble(kRecorderCaptureFps, mCaptureFps); 166 // TBD mCaptureFps = -1.0; 167 // TBD mCameraSourceTimeLapse = NULL; 168 // TBD mMetaDataStoredInVideoBuffers = kMetadataBufferTypeInvalid; 169 // TBD mEncoderProfiles = MediaProfiles::getInstance(); 170 mAnalyticsItem->setInt32(kRecorderRotation, mRotationDegrees); 171 // PII mLatitudex10000 = -3600000; 172 // PII mLongitudex10000 = -3600000; 173 // TBD mTotalBitRate = 0; 174 175 // TBD: some duration information (capture, paused) 176 // 177 178} 179 180void StagefrightRecorder::resetMetrics() { 181 ALOGV("resetMetrics"); 182 // flush anything we have, restart the record 183 if (mAnalyticsDirty && mAnalyticsItem != NULL) { 184 updateMetrics(); 185 if (mAnalyticsItem->count() > 0) { 186 mAnalyticsItem->setFinalized(true); 187 mAnalyticsItem->selfrecord(); 188 } 189 delete mAnalyticsItem; 190 mAnalyticsItem = NULL; 191 } 192 mAnalyticsItem = new MediaAnalyticsItem(kKeyRecorder); 193 (void) mAnalyticsItem->generateSessionID(); 194 mAnalyticsDirty = false; 195} 196 197status_t StagefrightRecorder::init() { 198 ALOGV("init"); 199 200 mLooper = new ALooper; 201 mLooper->setName("recorder_looper"); 202 mLooper->start(); 203 204 return OK; 205} 206 207// The client side of mediaserver asks it to creat a SurfaceMediaSource 208// and return a interface reference. The client side will use that 209// while encoding GL Frames 210sp<IGraphicBufferProducer> StagefrightRecorder::querySurfaceMediaSource() const { 211 ALOGV("Get SurfaceMediaSource"); 212 return mGraphicBufferProducer; 213} 214 215status_t StagefrightRecorder::setAudioSource(audio_source_t as) { 216 ALOGV("setAudioSource: %d", as); 217 if (as < AUDIO_SOURCE_DEFAULT || 218 (as >= AUDIO_SOURCE_CNT && as != AUDIO_SOURCE_FM_TUNER)) { 219 ALOGE("Invalid audio source: %d", as); 220 return BAD_VALUE; 221 } 222 223 if (as == AUDIO_SOURCE_DEFAULT) { 224 mAudioSource = AUDIO_SOURCE_MIC; 225 } else { 226 mAudioSource = as; 227 } 228 229 return OK; 230} 231 232status_t StagefrightRecorder::setVideoSource(video_source vs) { 233 ALOGV("setVideoSource: %d", vs); 234 if (vs < VIDEO_SOURCE_DEFAULT || 235 vs >= VIDEO_SOURCE_LIST_END) { 236 ALOGE("Invalid video source: %d", vs); 237 return BAD_VALUE; 238 } 239 240 if (vs == VIDEO_SOURCE_DEFAULT) { 241 mVideoSource = VIDEO_SOURCE_CAMERA; 242 } else { 243 mVideoSource = vs; 244 } 245 246 return OK; 247} 248 249status_t StagefrightRecorder::setOutputFormat(output_format of) { 250 ALOGV("setOutputFormat: %d", of); 251 if (of < OUTPUT_FORMAT_DEFAULT || 252 of >= OUTPUT_FORMAT_LIST_END) { 253 ALOGE("Invalid output format: %d", of); 254 return BAD_VALUE; 255 } 256 257 if (of == OUTPUT_FORMAT_DEFAULT) { 258 mOutputFormat = OUTPUT_FORMAT_THREE_GPP; 259 } else { 260 mOutputFormat = of; 261 } 262 263 return OK; 264} 265 266status_t StagefrightRecorder::setAudioEncoder(audio_encoder ae) { 267 ALOGV("setAudioEncoder: %d", ae); 268 if (ae < AUDIO_ENCODER_DEFAULT || 269 ae >= AUDIO_ENCODER_LIST_END) { 270 ALOGE("Invalid audio encoder: %d", ae); 271 return BAD_VALUE; 272 } 273 274 if (ae == AUDIO_ENCODER_DEFAULT) { 275 mAudioEncoder = AUDIO_ENCODER_AMR_NB; 276 } else { 277 mAudioEncoder = ae; 278 } 279 280 return OK; 281} 282 283status_t StagefrightRecorder::setVideoEncoder(video_encoder ve) { 284 ALOGV("setVideoEncoder: %d", ve); 285 if (ve < VIDEO_ENCODER_DEFAULT || 286 ve >= VIDEO_ENCODER_LIST_END) { 287 ALOGE("Invalid video encoder: %d", ve); 288 return BAD_VALUE; 289 } 290 291 mVideoEncoder = ve; 292 293 return OK; 294} 295 296status_t StagefrightRecorder::setVideoSize(int width, int height) { 297 ALOGV("setVideoSize: %dx%d", width, height); 298 if (width <= 0 || height <= 0) { 299 ALOGE("Invalid video size: %dx%d", width, height); 300 return BAD_VALUE; 301 } 302 303 // Additional check on the dimension will be performed later 304 mVideoWidth = width; 305 mVideoHeight = height; 306 307 return OK; 308} 309 310status_t StagefrightRecorder::setVideoFrameRate(int frames_per_second) { 311 ALOGV("setVideoFrameRate: %d", frames_per_second); 312 if ((frames_per_second <= 0 && frames_per_second != -1) || 313 frames_per_second > kMaxHighSpeedFps) { 314 ALOGE("Invalid video frame rate: %d", frames_per_second); 315 return BAD_VALUE; 316 } 317 318 // Additional check on the frame rate will be performed later 319 mFrameRate = frames_per_second; 320 321 return OK; 322} 323 324status_t StagefrightRecorder::setCamera(const sp<hardware::ICamera> &camera, 325 const sp<ICameraRecordingProxy> &proxy) { 326 ALOGV("setCamera"); 327 if (camera == 0) { 328 ALOGE("camera is NULL"); 329 return BAD_VALUE; 330 } 331 if (proxy == 0) { 332 ALOGE("camera proxy is NULL"); 333 return BAD_VALUE; 334 } 335 336 mCamera = camera; 337 mCameraProxy = proxy; 338 return OK; 339} 340 341status_t StagefrightRecorder::setPreviewSurface(const sp<IGraphicBufferProducer> &surface) { 342 ALOGV("setPreviewSurface: %p", surface.get()); 343 mPreviewSurface = surface; 344 345 return OK; 346} 347 348status_t StagefrightRecorder::setInputSurface( 349 const sp<PersistentSurface>& surface) { 350 mPersistentSurface = surface; 351 352 return OK; 353} 354 355status_t StagefrightRecorder::setOutputFile(int fd) { 356 ALOGV("setOutputFile: %d", fd); 357 358 if (fd < 0) { 359 ALOGE("Invalid file descriptor: %d", fd); 360 return -EBADF; 361 } 362 363 // start with a clean, empty file 364 ftruncate(fd, 0); 365 366 if (mOutputFd >= 0) { 367 ::close(mOutputFd); 368 } 369 mOutputFd = dup(fd); 370 371 return OK; 372} 373 374status_t StagefrightRecorder::setNextOutputFile(int fd) { 375 Mutex::Autolock autolock(mLock); 376 // Only support MPEG4 377 if (mOutputFormat != OUTPUT_FORMAT_MPEG_4) { 378 ALOGE("Only MP4 file format supports setting next output file"); 379 return INVALID_OPERATION; 380 } 381 ALOGV("setNextOutputFile: %d", fd); 382 383 if (fd < 0) { 384 ALOGE("Invalid file descriptor: %d", fd); 385 return -EBADF; 386 } 387 388 // start with a clean, empty file 389 ftruncate(fd, 0); 390 int nextFd = dup(fd); 391 if (mWriter == NULL) { 392 ALOGE("setNextOutputFile failed. Writer has been freed"); 393 return INVALID_OPERATION; 394 } 395 return mWriter->setNextFd(nextFd); 396} 397 398// Attempt to parse an float literal optionally surrounded by whitespace, 399// returns true on success, false otherwise. 400static bool safe_strtod(const char *s, double *val) { 401 char *end; 402 403 // It is lame, but according to man page, we have to set errno to 0 404 // before calling strtod(). 405 errno = 0; 406 *val = strtod(s, &end); 407 408 if (end == s || errno == ERANGE) { 409 return false; 410 } 411 412 // Skip trailing whitespace 413 while (isspace(*end)) { 414 ++end; 415 } 416 417 // For a successful return, the string must contain nothing but a valid 418 // float literal optionally surrounded by whitespace. 419 420 return *end == '\0'; 421} 422 423// Attempt to parse an int64 literal optionally surrounded by whitespace, 424// returns true on success, false otherwise. 425static bool safe_strtoi64(const char *s, int64_t *val) { 426 char *end; 427 428 // It is lame, but according to man page, we have to set errno to 0 429 // before calling strtoll(). 430 errno = 0; 431 *val = strtoll(s, &end, 10); 432 433 if (end == s || errno == ERANGE) { 434 return false; 435 } 436 437 // Skip trailing whitespace 438 while (isspace(*end)) { 439 ++end; 440 } 441 442 // For a successful return, the string must contain nothing but a valid 443 // int64 literal optionally surrounded by whitespace. 444 445 return *end == '\0'; 446} 447 448// Return true if the value is in [0, 0x007FFFFFFF] 449static bool safe_strtoi32(const char *s, int32_t *val) { 450 int64_t temp; 451 if (safe_strtoi64(s, &temp)) { 452 if (temp >= 0 && temp <= 0x007FFFFFFF) { 453 *val = static_cast<int32_t>(temp); 454 return true; 455 } 456 } 457 return false; 458} 459 460// Trim both leading and trailing whitespace from the given string. 461static void TrimString(String8 *s) { 462 size_t num_bytes = s->bytes(); 463 const char *data = s->string(); 464 465 size_t leading_space = 0; 466 while (leading_space < num_bytes && isspace(data[leading_space])) { 467 ++leading_space; 468 } 469 470 size_t i = num_bytes; 471 while (i > leading_space && isspace(data[i - 1])) { 472 --i; 473 } 474 475 s->setTo(String8(&data[leading_space], i - leading_space)); 476} 477 478status_t StagefrightRecorder::setParamAudioSamplingRate(int32_t sampleRate) { 479 ALOGV("setParamAudioSamplingRate: %d", sampleRate); 480 if (sampleRate <= 0) { 481 ALOGE("Invalid audio sampling rate: %d", sampleRate); 482 return BAD_VALUE; 483 } 484 485 // Additional check on the sample rate will be performed later. 486 mSampleRate = sampleRate; 487 488 return OK; 489} 490 491status_t StagefrightRecorder::setParamAudioNumberOfChannels(int32_t channels) { 492 ALOGV("setParamAudioNumberOfChannels: %d", channels); 493 if (channels <= 0 || channels >= 3) { 494 ALOGE("Invalid number of audio channels: %d", channels); 495 return BAD_VALUE; 496 } 497 498 // Additional check on the number of channels will be performed later. 499 mAudioChannels = channels; 500 501 return OK; 502} 503 504status_t StagefrightRecorder::setParamAudioEncodingBitRate(int32_t bitRate) { 505 ALOGV("setParamAudioEncodingBitRate: %d", bitRate); 506 if (bitRate <= 0) { 507 ALOGE("Invalid audio encoding bit rate: %d", bitRate); 508 return BAD_VALUE; 509 } 510 511 // The target bit rate may not be exactly the same as the requested. 512 // It depends on many factors, such as rate control, and the bit rate 513 // range that a specific encoder supports. The mismatch between the 514 // the target and requested bit rate will NOT be treated as an error. 515 mAudioBitRate = bitRate; 516 return OK; 517} 518 519status_t StagefrightRecorder::setParamVideoEncodingBitRate(int32_t bitRate) { 520 ALOGV("setParamVideoEncodingBitRate: %d", bitRate); 521 if (bitRate <= 0) { 522 ALOGE("Invalid video encoding bit rate: %d", bitRate); 523 return BAD_VALUE; 524 } 525 526 // The target bit rate may not be exactly the same as the requested. 527 // It depends on many factors, such as rate control, and the bit rate 528 // range that a specific encoder supports. The mismatch between the 529 // the target and requested bit rate will NOT be treated as an error. 530 mVideoBitRate = bitRate; 531 return OK; 532} 533 534// Always rotate clockwise, and only support 0, 90, 180 and 270 for now. 535status_t StagefrightRecorder::setParamVideoRotation(int32_t degrees) { 536 ALOGV("setParamVideoRotation: %d", degrees); 537 if (degrees < 0 || degrees % 90 != 0) { 538 ALOGE("Unsupported video rotation angle: %d", degrees); 539 return BAD_VALUE; 540 } 541 mRotationDegrees = degrees % 360; 542 return OK; 543} 544 545status_t StagefrightRecorder::setParamMaxFileDurationUs(int64_t timeUs) { 546 ALOGV("setParamMaxFileDurationUs: %lld us", (long long)timeUs); 547 548 // This is meant for backward compatibility for MediaRecorder.java 549 if (timeUs <= 0) { 550 ALOGW("Max file duration is not positive: %lld us. Disabling duration limit.", 551 (long long)timeUs); 552 timeUs = 0; // Disable the duration limit for zero or negative values. 553 } else if (timeUs <= 100000LL) { // XXX: 100 milli-seconds 554 ALOGE("Max file duration is too short: %lld us", (long long)timeUs); 555 return BAD_VALUE; 556 } 557 558 if (timeUs <= 15 * 1000000LL) { 559 ALOGW("Target duration (%lld us) too short to be respected", (long long)timeUs); 560 } 561 mMaxFileDurationUs = timeUs; 562 return OK; 563} 564 565status_t StagefrightRecorder::setParamMaxFileSizeBytes(int64_t bytes) { 566 ALOGV("setParamMaxFileSizeBytes: %lld bytes", (long long)bytes); 567 568 // This is meant for backward compatibility for MediaRecorder.java 569 if (bytes <= 0) { 570 ALOGW("Max file size is not positive: %lld bytes. " 571 "Disabling file size limit.", (long long)bytes); 572 bytes = 0; // Disable the file size limit for zero or negative values. 573 } else if (bytes <= 1024) { // XXX: 1 kB 574 ALOGE("Max file size is too small: %lld bytes", (long long)bytes); 575 return BAD_VALUE; 576 } 577 578 if (bytes <= 100 * 1024) { 579 ALOGW("Target file size (%lld bytes) is too small to be respected", (long long)bytes); 580 } 581 582 mMaxFileSizeBytes = bytes; 583 return OK; 584} 585 586status_t StagefrightRecorder::setParamInterleaveDuration(int32_t durationUs) { 587 ALOGV("setParamInterleaveDuration: %d", durationUs); 588 if (durationUs <= 500000) { // 500 ms 589 // If interleave duration is too small, it is very inefficient to do 590 // interleaving since the metadata overhead will count for a significant 591 // portion of the saved contents 592 ALOGE("Audio/video interleave duration is too small: %d us", durationUs); 593 return BAD_VALUE; 594 } else if (durationUs >= 10000000) { // 10 seconds 595 // If interleaving duration is too large, it can cause the recording 596 // session to use too much memory since we have to save the output 597 // data before we write them out 598 ALOGE("Audio/video interleave duration is too large: %d us", durationUs); 599 return BAD_VALUE; 600 } 601 mInterleaveDurationUs = durationUs; 602 return OK; 603} 604 605// If seconds < 0, only the first frame is I frame, and rest are all P frames 606// If seconds == 0, all frames are encoded as I frames. No P frames 607// If seconds > 0, it is the time spacing (seconds) between 2 neighboring I frames 608status_t StagefrightRecorder::setParamVideoIFramesInterval(int32_t seconds) { 609 ALOGV("setParamVideoIFramesInterval: %d seconds", seconds); 610 mIFramesIntervalSec = seconds; 611 return OK; 612} 613 614status_t StagefrightRecorder::setParam64BitFileOffset(bool use64Bit) { 615 ALOGV("setParam64BitFileOffset: %s", 616 use64Bit? "use 64 bit file offset": "use 32 bit file offset"); 617 mUse64BitFileOffset = use64Bit; 618 return OK; 619} 620 621status_t StagefrightRecorder::setParamVideoCameraId(int32_t cameraId) { 622 ALOGV("setParamVideoCameraId: %d", cameraId); 623 if (cameraId < 0) { 624 return BAD_VALUE; 625 } 626 mCameraId = cameraId; 627 return OK; 628} 629 630status_t StagefrightRecorder::setParamTrackTimeStatus(int64_t timeDurationUs) { 631 ALOGV("setParamTrackTimeStatus: %lld", (long long)timeDurationUs); 632 if (timeDurationUs < 20000) { // Infeasible if shorter than 20 ms? 633 ALOGE("Tracking time duration too short: %lld us", (long long)timeDurationUs); 634 return BAD_VALUE; 635 } 636 mTrackEveryTimeDurationUs = timeDurationUs; 637 return OK; 638} 639 640status_t StagefrightRecorder::setParamVideoEncoderProfile(int32_t profile) { 641 ALOGV("setParamVideoEncoderProfile: %d", profile); 642 643 // Additional check will be done later when we load the encoder. 644 // For now, we are accepting values defined in OpenMAX IL. 645 mVideoEncoderProfile = profile; 646 return OK; 647} 648 649status_t StagefrightRecorder::setParamVideoEncoderLevel(int32_t level) { 650 ALOGV("setParamVideoEncoderLevel: %d", level); 651 652 // Additional check will be done later when we load the encoder. 653 // For now, we are accepting values defined in OpenMAX IL. 654 mVideoEncoderLevel = level; 655 return OK; 656} 657 658status_t StagefrightRecorder::setParamMovieTimeScale(int32_t timeScale) { 659 ALOGV("setParamMovieTimeScale: %d", timeScale); 660 661 // The range is set to be the same as the audio's time scale range 662 // since audio's time scale has a wider range. 663 if (timeScale < 600 || timeScale > 96000) { 664 ALOGE("Time scale (%d) for movie is out of range [600, 96000]", timeScale); 665 return BAD_VALUE; 666 } 667 mMovieTimeScale = timeScale; 668 return OK; 669} 670 671status_t StagefrightRecorder::setParamVideoTimeScale(int32_t timeScale) { 672 ALOGV("setParamVideoTimeScale: %d", timeScale); 673 674 // 60000 is chosen to make sure that each video frame from a 60-fps 675 // video has 1000 ticks. 676 if (timeScale < 600 || timeScale > 60000) { 677 ALOGE("Time scale (%d) for video is out of range [600, 60000]", timeScale); 678 return BAD_VALUE; 679 } 680 mVideoTimeScale = timeScale; 681 return OK; 682} 683 684status_t StagefrightRecorder::setParamAudioTimeScale(int32_t timeScale) { 685 ALOGV("setParamAudioTimeScale: %d", timeScale); 686 687 // 96000 Hz is the highest sampling rate support in AAC. 688 if (timeScale < 600 || timeScale > 96000) { 689 ALOGE("Time scale (%d) for audio is out of range [600, 96000]", timeScale); 690 return BAD_VALUE; 691 } 692 mAudioTimeScale = timeScale; 693 return OK; 694} 695 696status_t StagefrightRecorder::setParamCaptureFpsEnable(int32_t captureFpsEnable) { 697 ALOGV("setParamCaptureFpsEnable: %d", captureFpsEnable); 698 699 if(captureFpsEnable == 0) { 700 mCaptureFpsEnable = false; 701 } else if (captureFpsEnable == 1) { 702 mCaptureFpsEnable = true; 703 } else { 704 return BAD_VALUE; 705 } 706 return OK; 707} 708 709status_t StagefrightRecorder::setParamCaptureFps(double fps) { 710 ALOGV("setParamCaptureFps: %.2f", fps); 711 712 if (!(fps >= 1.0 / 86400)) { 713 ALOGE("FPS is too small"); 714 return BAD_VALUE; 715 } 716 mCaptureFps = fps; 717 return OK; 718} 719 720status_t StagefrightRecorder::setParamGeoDataLongitude( 721 int64_t longitudex10000) { 722 723 if (longitudex10000 > 1800000 || longitudex10000 < -1800000) { 724 return BAD_VALUE; 725 } 726 mLongitudex10000 = longitudex10000; 727 return OK; 728} 729 730status_t StagefrightRecorder::setParamGeoDataLatitude( 731 int64_t latitudex10000) { 732 733 if (latitudex10000 > 900000 || latitudex10000 < -900000) { 734 return BAD_VALUE; 735 } 736 mLatitudex10000 = latitudex10000; 737 return OK; 738} 739 740status_t StagefrightRecorder::setParameter( 741 const String8 &key, const String8 &value) { 742 ALOGV("setParameter: key (%s) => value (%s)", key.string(), value.string()); 743 if (key == "max-duration") { 744 int64_t max_duration_ms; 745 if (safe_strtoi64(value.string(), &max_duration_ms)) { 746 return setParamMaxFileDurationUs(1000LL * max_duration_ms); 747 } 748 } else if (key == "max-filesize") { 749 int64_t max_filesize_bytes; 750 if (safe_strtoi64(value.string(), &max_filesize_bytes)) { 751 return setParamMaxFileSizeBytes(max_filesize_bytes); 752 } 753 } else if (key == "interleave-duration-us") { 754 int32_t durationUs; 755 if (safe_strtoi32(value.string(), &durationUs)) { 756 return setParamInterleaveDuration(durationUs); 757 } 758 } else if (key == "param-movie-time-scale") { 759 int32_t timeScale; 760 if (safe_strtoi32(value.string(), &timeScale)) { 761 return setParamMovieTimeScale(timeScale); 762 } 763 } else if (key == "param-use-64bit-offset") { 764 int32_t use64BitOffset; 765 if (safe_strtoi32(value.string(), &use64BitOffset)) { 766 return setParam64BitFileOffset(use64BitOffset != 0); 767 } 768 } else if (key == "param-geotag-longitude") { 769 int64_t longitudex10000; 770 if (safe_strtoi64(value.string(), &longitudex10000)) { 771 return setParamGeoDataLongitude(longitudex10000); 772 } 773 } else if (key == "param-geotag-latitude") { 774 int64_t latitudex10000; 775 if (safe_strtoi64(value.string(), &latitudex10000)) { 776 return setParamGeoDataLatitude(latitudex10000); 777 } 778 } else if (key == "param-track-time-status") { 779 int64_t timeDurationUs; 780 if (safe_strtoi64(value.string(), &timeDurationUs)) { 781 return setParamTrackTimeStatus(timeDurationUs); 782 } 783 } else if (key == "audio-param-sampling-rate") { 784 int32_t sampling_rate; 785 if (safe_strtoi32(value.string(), &sampling_rate)) { 786 return setParamAudioSamplingRate(sampling_rate); 787 } 788 } else if (key == "audio-param-number-of-channels") { 789 int32_t number_of_channels; 790 if (safe_strtoi32(value.string(), &number_of_channels)) { 791 return setParamAudioNumberOfChannels(number_of_channels); 792 } 793 } else if (key == "audio-param-encoding-bitrate") { 794 int32_t audio_bitrate; 795 if (safe_strtoi32(value.string(), &audio_bitrate)) { 796 return setParamAudioEncodingBitRate(audio_bitrate); 797 } 798 } else if (key == "audio-param-time-scale") { 799 int32_t timeScale; 800 if (safe_strtoi32(value.string(), &timeScale)) { 801 return setParamAudioTimeScale(timeScale); 802 } 803 } else if (key == "video-param-encoding-bitrate") { 804 int32_t video_bitrate; 805 if (safe_strtoi32(value.string(), &video_bitrate)) { 806 return setParamVideoEncodingBitRate(video_bitrate); 807 } 808 } else if (key == "video-param-rotation-angle-degrees") { 809 int32_t degrees; 810 if (safe_strtoi32(value.string(), °rees)) { 811 return setParamVideoRotation(degrees); 812 } 813 } else if (key == "video-param-i-frames-interval") { 814 int32_t seconds; 815 if (safe_strtoi32(value.string(), &seconds)) { 816 return setParamVideoIFramesInterval(seconds); 817 } 818 } else if (key == "video-param-encoder-profile") { 819 int32_t profile; 820 if (safe_strtoi32(value.string(), &profile)) { 821 return setParamVideoEncoderProfile(profile); 822 } 823 } else if (key == "video-param-encoder-level") { 824 int32_t level; 825 if (safe_strtoi32(value.string(), &level)) { 826 return setParamVideoEncoderLevel(level); 827 } 828 } else if (key == "video-param-camera-id") { 829 int32_t cameraId; 830 if (safe_strtoi32(value.string(), &cameraId)) { 831 return setParamVideoCameraId(cameraId); 832 } 833 } else if (key == "video-param-time-scale") { 834 int32_t timeScale; 835 if (safe_strtoi32(value.string(), &timeScale)) { 836 return setParamVideoTimeScale(timeScale); 837 } 838 } else if (key == "time-lapse-enable") { 839 int32_t captureFpsEnable; 840 if (safe_strtoi32(value.string(), &captureFpsEnable)) { 841 return setParamCaptureFpsEnable(captureFpsEnable); 842 } 843 } else if (key == "time-lapse-fps") { 844 double fps; 845 if (safe_strtod(value.string(), &fps)) { 846 return setParamCaptureFps(fps); 847 } 848 } else { 849 ALOGE("setParameter: failed to find key %s", key.string()); 850 } 851 return BAD_VALUE; 852} 853 854status_t StagefrightRecorder::setParameters(const String8 ¶ms) { 855 ALOGV("setParameters: %s", params.string()); 856 const char *cparams = params.string(); 857 const char *key_start = cparams; 858 for (;;) { 859 const char *equal_pos = strchr(key_start, '='); 860 if (equal_pos == NULL) { 861 ALOGE("Parameters %s miss a value", cparams); 862 return BAD_VALUE; 863 } 864 String8 key(key_start, equal_pos - key_start); 865 TrimString(&key); 866 if (key.length() == 0) { 867 ALOGE("Parameters %s contains an empty key", cparams); 868 return BAD_VALUE; 869 } 870 const char *value_start = equal_pos + 1; 871 const char *semicolon_pos = strchr(value_start, ';'); 872 String8 value; 873 if (semicolon_pos == NULL) { 874 value.setTo(value_start); 875 } else { 876 value.setTo(value_start, semicolon_pos - value_start); 877 } 878 if (setParameter(key, value) != OK) { 879 return BAD_VALUE; 880 } 881 if (semicolon_pos == NULL) { 882 break; // Reaches the end 883 } 884 key_start = semicolon_pos + 1; 885 } 886 return OK; 887} 888 889status_t StagefrightRecorder::setListener(const sp<IMediaRecorderClient> &listener) { 890 mListener = listener; 891 892 return OK; 893} 894 895status_t StagefrightRecorder::setClientName(const String16& clientName) { 896 mClientName = clientName; 897 898 return OK; 899} 900 901status_t StagefrightRecorder::prepareInternal() { 902 ALOGV("prepare"); 903 if (mOutputFd < 0) { 904 ALOGE("Output file descriptor is invalid"); 905 return INVALID_OPERATION; 906 } 907 908 // Get UID and PID here for permission checking 909 mClientUid = IPCThreadState::self()->getCallingUid(); 910 mClientPid = IPCThreadState::self()->getCallingPid(); 911 912 status_t status = OK; 913 914 switch (mOutputFormat) { 915 case OUTPUT_FORMAT_DEFAULT: 916 case OUTPUT_FORMAT_THREE_GPP: 917 case OUTPUT_FORMAT_MPEG_4: 918 case OUTPUT_FORMAT_WEBM: 919 status = setupMPEG4orWEBMRecording(); 920 break; 921 922 case OUTPUT_FORMAT_AMR_NB: 923 case OUTPUT_FORMAT_AMR_WB: 924 status = setupAMRRecording(); 925 break; 926 927 case OUTPUT_FORMAT_AAC_ADIF: 928 case OUTPUT_FORMAT_AAC_ADTS: 929 status = setupAACRecording(); 930 break; 931 932 case OUTPUT_FORMAT_RTP_AVP: 933 status = setupRTPRecording(); 934 break; 935 936 case OUTPUT_FORMAT_MPEG2TS: 937 status = setupMPEG2TSRecording(); 938 break; 939 940 default: 941 ALOGE("Unsupported output file format: %d", mOutputFormat); 942 status = UNKNOWN_ERROR; 943 break; 944 } 945 946 ALOGV("Recording frameRate: %d captureFps: %f", 947 mFrameRate, mCaptureFps); 948 949 return status; 950} 951 952status_t StagefrightRecorder::prepare() { 953 ALOGV("prepare"); 954 Mutex::Autolock autolock(mLock); 955 if (mVideoSource == VIDEO_SOURCE_SURFACE) { 956 return prepareInternal(); 957 } 958 return OK; 959} 960 961status_t StagefrightRecorder::start() { 962 ALOGV("start"); 963 Mutex::Autolock autolock(mLock); 964 if (mOutputFd < 0) { 965 ALOGE("Output file descriptor is invalid"); 966 return INVALID_OPERATION; 967 } 968 969 status_t status = OK; 970 971 if (mVideoSource != VIDEO_SOURCE_SURFACE) { 972 status = prepareInternal(); 973 if (status != OK) { 974 return status; 975 } 976 } 977 978 if (mWriter == NULL) { 979 ALOGE("File writer is not avaialble"); 980 return UNKNOWN_ERROR; 981 } 982 983 switch (mOutputFormat) { 984 case OUTPUT_FORMAT_DEFAULT: 985 case OUTPUT_FORMAT_THREE_GPP: 986 case OUTPUT_FORMAT_MPEG_4: 987 case OUTPUT_FORMAT_WEBM: 988 { 989 bool isMPEG4 = true; 990 if (mOutputFormat == OUTPUT_FORMAT_WEBM) { 991 isMPEG4 = false; 992 } 993 sp<MetaData> meta = new MetaData; 994 setupMPEG4orWEBMMetaData(&meta); 995 status = mWriter->start(meta.get()); 996 break; 997 } 998 999 case OUTPUT_FORMAT_AMR_NB: 1000 case OUTPUT_FORMAT_AMR_WB: 1001 case OUTPUT_FORMAT_AAC_ADIF: 1002 case OUTPUT_FORMAT_AAC_ADTS: 1003 case OUTPUT_FORMAT_RTP_AVP: 1004 case OUTPUT_FORMAT_MPEG2TS: 1005 { 1006 sp<MetaData> meta = new MetaData; 1007 int64_t startTimeUs = systemTime() / 1000; 1008 meta->setInt64(kKeyTime, startTimeUs); 1009 status = mWriter->start(meta.get()); 1010 break; 1011 } 1012 1013 default: 1014 { 1015 ALOGE("Unsupported output file format: %d", mOutputFormat); 1016 status = UNKNOWN_ERROR; 1017 break; 1018 } 1019 } 1020 1021 if (status != OK) { 1022 mWriter.clear(); 1023 mWriter = NULL; 1024 } 1025 1026 if ((status == OK) && (!mStarted)) { 1027 mAnalyticsDirty = true; 1028 mStarted = true; 1029 1030 uint32_t params = IMediaPlayerService::kBatteryDataCodecStarted; 1031 if (mAudioSource != AUDIO_SOURCE_CNT) { 1032 params |= IMediaPlayerService::kBatteryDataTrackAudio; 1033 } 1034 if (mVideoSource != VIDEO_SOURCE_LIST_END) { 1035 params |= IMediaPlayerService::kBatteryDataTrackVideo; 1036 } 1037 1038 addBatteryData(params); 1039 } 1040 1041 return status; 1042} 1043 1044sp<MediaCodecSource> StagefrightRecorder::createAudioSource() { 1045 int32_t sourceSampleRate = mSampleRate; 1046 1047 if (mCaptureFpsEnable && mCaptureFps >= mFrameRate) { 1048 // Upscale the sample rate for slow motion recording. 1049 // Fail audio source creation if source sample rate is too high, as it could 1050 // cause out-of-memory due to large input buffer size. And audio recording 1051 // probably doesn't make sense in the scenario, since the slow-down factor 1052 // is probably huge (eg. mSampleRate=48K, mCaptureFps=240, mFrameRate=1). 1053 const static int32_t SAMPLE_RATE_HZ_MAX = 192000; 1054 sourceSampleRate = 1055 (mSampleRate * mCaptureFps + mFrameRate / 2) / mFrameRate; 1056 if (sourceSampleRate < mSampleRate || sourceSampleRate > SAMPLE_RATE_HZ_MAX) { 1057 ALOGE("source sample rate out of range! " 1058 "(mSampleRate %d, mCaptureFps %.2f, mFrameRate %d", 1059 mSampleRate, mCaptureFps, mFrameRate); 1060 return NULL; 1061 } 1062 } 1063 1064 sp<AudioSource> audioSource = 1065 new AudioSource( 1066 mAudioSource, 1067 mOpPackageName, 1068 sourceSampleRate, 1069 mAudioChannels, 1070 mSampleRate, 1071 mClientUid, 1072 mClientPid); 1073 1074 status_t err = audioSource->initCheck(); 1075 1076 if (err != OK) { 1077 ALOGE("audio source is not initialized"); 1078 return NULL; 1079 } 1080 1081 sp<AMessage> format = new AMessage; 1082 switch (mAudioEncoder) { 1083 case AUDIO_ENCODER_AMR_NB: 1084 case AUDIO_ENCODER_DEFAULT: 1085 format->setString("mime", MEDIA_MIMETYPE_AUDIO_AMR_NB); 1086 break; 1087 case AUDIO_ENCODER_AMR_WB: 1088 format->setString("mime", MEDIA_MIMETYPE_AUDIO_AMR_WB); 1089 break; 1090 case AUDIO_ENCODER_AAC: 1091 format->setString("mime", MEDIA_MIMETYPE_AUDIO_AAC); 1092 format->setInt32("aac-profile", OMX_AUDIO_AACObjectLC); 1093 break; 1094 case AUDIO_ENCODER_HE_AAC: 1095 format->setString("mime", MEDIA_MIMETYPE_AUDIO_AAC); 1096 format->setInt32("aac-profile", OMX_AUDIO_AACObjectHE); 1097 break; 1098 case AUDIO_ENCODER_AAC_ELD: 1099 format->setString("mime", MEDIA_MIMETYPE_AUDIO_AAC); 1100 format->setInt32("aac-profile", OMX_AUDIO_AACObjectELD); 1101 break; 1102 1103 default: 1104 ALOGE("Unknown audio encoder: %d", mAudioEncoder); 1105 return NULL; 1106 } 1107 1108 int32_t maxInputSize; 1109 CHECK(audioSource->getFormat()->findInt32( 1110 kKeyMaxInputSize, &maxInputSize)); 1111 1112 format->setInt32("max-input-size", maxInputSize); 1113 format->setInt32("channel-count", mAudioChannels); 1114 format->setInt32("sample-rate", mSampleRate); 1115 format->setInt32("bitrate", mAudioBitRate); 1116 if (mAudioTimeScale > 0) { 1117 format->setInt32("time-scale", mAudioTimeScale); 1118 } 1119 format->setInt32("priority", 0 /* realtime */); 1120 1121 sp<MediaCodecSource> audioEncoder = 1122 MediaCodecSource::Create(mLooper, format, audioSource); 1123 mAudioSourceNode = audioSource; 1124 1125 if (audioEncoder == NULL) { 1126 ALOGE("Failed to create audio encoder"); 1127 } 1128 1129 return audioEncoder; 1130} 1131 1132status_t StagefrightRecorder::setupAACRecording() { 1133 // FIXME: 1134 // Add support for OUTPUT_FORMAT_AAC_ADIF 1135 CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_AAC_ADTS); 1136 1137 CHECK(mAudioEncoder == AUDIO_ENCODER_AAC || 1138 mAudioEncoder == AUDIO_ENCODER_HE_AAC || 1139 mAudioEncoder == AUDIO_ENCODER_AAC_ELD); 1140 CHECK(mAudioSource != AUDIO_SOURCE_CNT); 1141 1142 mWriter = new AACWriter(mOutputFd); 1143 return setupRawAudioRecording(); 1144} 1145 1146status_t StagefrightRecorder::setupAMRRecording() { 1147 CHECK(mOutputFormat == OUTPUT_FORMAT_AMR_NB || 1148 mOutputFormat == OUTPUT_FORMAT_AMR_WB); 1149 1150 if (mOutputFormat == OUTPUT_FORMAT_AMR_NB) { 1151 if (mAudioEncoder != AUDIO_ENCODER_DEFAULT && 1152 mAudioEncoder != AUDIO_ENCODER_AMR_NB) { 1153 ALOGE("Invalid encoder %d used for AMRNB recording", 1154 mAudioEncoder); 1155 return BAD_VALUE; 1156 } 1157 } else { // mOutputFormat must be OUTPUT_FORMAT_AMR_WB 1158 if (mAudioEncoder != AUDIO_ENCODER_AMR_WB) { 1159 ALOGE("Invlaid encoder %d used for AMRWB recording", 1160 mAudioEncoder); 1161 return BAD_VALUE; 1162 } 1163 } 1164 1165 mWriter = new AMRWriter(mOutputFd); 1166 return setupRawAudioRecording(); 1167} 1168 1169status_t StagefrightRecorder::setupRawAudioRecording() { 1170 if (mAudioSource >= AUDIO_SOURCE_CNT && mAudioSource != AUDIO_SOURCE_FM_TUNER) { 1171 ALOGE("Invalid audio source: %d", mAudioSource); 1172 return BAD_VALUE; 1173 } 1174 1175 status_t status = BAD_VALUE; 1176 if (OK != (status = checkAudioEncoderCapabilities())) { 1177 return status; 1178 } 1179 1180 sp<MediaCodecSource> audioEncoder = createAudioSource(); 1181 if (audioEncoder == NULL) { 1182 return UNKNOWN_ERROR; 1183 } 1184 1185 CHECK(mWriter != 0); 1186 mWriter->addSource(audioEncoder); 1187 mAudioEncoderSource = audioEncoder; 1188 1189 if (mMaxFileDurationUs != 0) { 1190 mWriter->setMaxFileDuration(mMaxFileDurationUs); 1191 } 1192 if (mMaxFileSizeBytes != 0) { 1193 mWriter->setMaxFileSize(mMaxFileSizeBytes); 1194 } 1195 mWriter->setListener(mListener); 1196 1197 return OK; 1198} 1199 1200status_t StagefrightRecorder::setupRTPRecording() { 1201 CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_RTP_AVP); 1202 1203 if ((mAudioSource != AUDIO_SOURCE_CNT 1204 && mVideoSource != VIDEO_SOURCE_LIST_END) 1205 || (mAudioSource == AUDIO_SOURCE_CNT 1206 && mVideoSource == VIDEO_SOURCE_LIST_END)) { 1207 // Must have exactly one source. 1208 return BAD_VALUE; 1209 } 1210 1211 if (mOutputFd < 0) { 1212 return BAD_VALUE; 1213 } 1214 1215 sp<MediaCodecSource> source; 1216 1217 if (mAudioSource != AUDIO_SOURCE_CNT) { 1218 source = createAudioSource(); 1219 mAudioEncoderSource = source; 1220 } else { 1221 setDefaultVideoEncoderIfNecessary(); 1222 1223 sp<MediaSource> mediaSource; 1224 status_t err = setupMediaSource(&mediaSource); 1225 if (err != OK) { 1226 return err; 1227 } 1228 1229 err = setupVideoEncoder(mediaSource, &source); 1230 if (err != OK) { 1231 return err; 1232 } 1233 mVideoEncoderSource = source; 1234 } 1235 1236 mWriter = new ARTPWriter(mOutputFd); 1237 mWriter->addSource(source); 1238 mWriter->setListener(mListener); 1239 1240 return OK; 1241} 1242 1243status_t StagefrightRecorder::setupMPEG2TSRecording() { 1244 CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_MPEG2TS); 1245 1246 sp<MediaWriter> writer = new MPEG2TSWriter(mOutputFd); 1247 1248 if (mAudioSource != AUDIO_SOURCE_CNT) { 1249 if (mAudioEncoder != AUDIO_ENCODER_AAC && 1250 mAudioEncoder != AUDIO_ENCODER_HE_AAC && 1251 mAudioEncoder != AUDIO_ENCODER_AAC_ELD) { 1252 return ERROR_UNSUPPORTED; 1253 } 1254 1255 status_t err = setupAudioEncoder(writer); 1256 1257 if (err != OK) { 1258 return err; 1259 } 1260 } 1261 1262 if (mVideoSource < VIDEO_SOURCE_LIST_END) { 1263 if (mVideoEncoder != VIDEO_ENCODER_H264) { 1264 ALOGE("MPEG2TS recording only supports H.264 encoding!"); 1265 return ERROR_UNSUPPORTED; 1266 } 1267 1268 sp<MediaSource> mediaSource; 1269 status_t err = setupMediaSource(&mediaSource); 1270 if (err != OK) { 1271 return err; 1272 } 1273 1274 sp<MediaCodecSource> encoder; 1275 err = setupVideoEncoder(mediaSource, &encoder); 1276 1277 if (err != OK) { 1278 return err; 1279 } 1280 1281 writer->addSource(encoder); 1282 mVideoEncoderSource = encoder; 1283 } 1284 1285 if (mMaxFileDurationUs != 0) { 1286 writer->setMaxFileDuration(mMaxFileDurationUs); 1287 } 1288 1289 if (mMaxFileSizeBytes != 0) { 1290 writer->setMaxFileSize(mMaxFileSizeBytes); 1291 } 1292 1293 mWriter = writer; 1294 1295 return OK; 1296} 1297 1298void StagefrightRecorder::clipVideoFrameRate() { 1299 ALOGV("clipVideoFrameRate: encoder %d", mVideoEncoder); 1300 if (mFrameRate == -1) { 1301 mFrameRate = mEncoderProfiles->getCamcorderProfileParamByName( 1302 "vid.fps", mCameraId, CAMCORDER_QUALITY_LOW); 1303 ALOGW("Using default video fps %d", mFrameRate); 1304 } 1305 1306 int minFrameRate = mEncoderProfiles->getVideoEncoderParamByName( 1307 "enc.vid.fps.min", mVideoEncoder); 1308 int maxFrameRate = mEncoderProfiles->getVideoEncoderParamByName( 1309 "enc.vid.fps.max", mVideoEncoder); 1310 if (mFrameRate < minFrameRate && minFrameRate != -1) { 1311 ALOGW("Intended video encoding frame rate (%d fps) is too small" 1312 " and will be set to (%d fps)", mFrameRate, minFrameRate); 1313 mFrameRate = minFrameRate; 1314 } else if (mFrameRate > maxFrameRate && maxFrameRate != -1) { 1315 ALOGW("Intended video encoding frame rate (%d fps) is too large" 1316 " and will be set to (%d fps)", mFrameRate, maxFrameRate); 1317 mFrameRate = maxFrameRate; 1318 } 1319} 1320 1321void StagefrightRecorder::clipVideoBitRate() { 1322 ALOGV("clipVideoBitRate: encoder %d", mVideoEncoder); 1323 int minBitRate = mEncoderProfiles->getVideoEncoderParamByName( 1324 "enc.vid.bps.min", mVideoEncoder); 1325 int maxBitRate = mEncoderProfiles->getVideoEncoderParamByName( 1326 "enc.vid.bps.max", mVideoEncoder); 1327 if (mVideoBitRate < minBitRate && minBitRate != -1) { 1328 ALOGW("Intended video encoding bit rate (%d bps) is too small" 1329 " and will be set to (%d bps)", mVideoBitRate, minBitRate); 1330 mVideoBitRate = minBitRate; 1331 } else if (mVideoBitRate > maxBitRate && maxBitRate != -1) { 1332 ALOGW("Intended video encoding bit rate (%d bps) is too large" 1333 " and will be set to (%d bps)", mVideoBitRate, maxBitRate); 1334 mVideoBitRate = maxBitRate; 1335 } 1336} 1337 1338void StagefrightRecorder::clipVideoFrameWidth() { 1339 ALOGV("clipVideoFrameWidth: encoder %d", mVideoEncoder); 1340 int minFrameWidth = mEncoderProfiles->getVideoEncoderParamByName( 1341 "enc.vid.width.min", mVideoEncoder); 1342 int maxFrameWidth = mEncoderProfiles->getVideoEncoderParamByName( 1343 "enc.vid.width.max", mVideoEncoder); 1344 if (mVideoWidth < minFrameWidth && minFrameWidth != -1) { 1345 ALOGW("Intended video encoding frame width (%d) is too small" 1346 " and will be set to (%d)", mVideoWidth, minFrameWidth); 1347 mVideoWidth = minFrameWidth; 1348 } else if (mVideoWidth > maxFrameWidth && maxFrameWidth != -1) { 1349 ALOGW("Intended video encoding frame width (%d) is too large" 1350 " and will be set to (%d)", mVideoWidth, maxFrameWidth); 1351 mVideoWidth = maxFrameWidth; 1352 } 1353} 1354 1355status_t StagefrightRecorder::checkVideoEncoderCapabilities() { 1356 if (!mCaptureFpsEnable) { 1357 // Dont clip for time lapse capture as encoder will have enough 1358 // time to encode because of slow capture rate of time lapse. 1359 clipVideoBitRate(); 1360 clipVideoFrameRate(); 1361 clipVideoFrameWidth(); 1362 clipVideoFrameHeight(); 1363 setDefaultProfileIfNecessary(); 1364 } 1365 return OK; 1366} 1367 1368// Set to use AVC baseline profile if the encoding parameters matches 1369// CAMCORDER_QUALITY_LOW profile; this is for the sake of MMS service. 1370void StagefrightRecorder::setDefaultProfileIfNecessary() { 1371 ALOGV("setDefaultProfileIfNecessary"); 1372 1373 camcorder_quality quality = CAMCORDER_QUALITY_LOW; 1374 1375 int64_t durationUs = mEncoderProfiles->getCamcorderProfileParamByName( 1376 "duration", mCameraId, quality) * 1000000LL; 1377 1378 int fileFormat = mEncoderProfiles->getCamcorderProfileParamByName( 1379 "file.format", mCameraId, quality); 1380 1381 int videoCodec = mEncoderProfiles->getCamcorderProfileParamByName( 1382 "vid.codec", mCameraId, quality); 1383 1384 int videoBitRate = mEncoderProfiles->getCamcorderProfileParamByName( 1385 "vid.bps", mCameraId, quality); 1386 1387 int videoFrameRate = mEncoderProfiles->getCamcorderProfileParamByName( 1388 "vid.fps", mCameraId, quality); 1389 1390 int videoFrameWidth = mEncoderProfiles->getCamcorderProfileParamByName( 1391 "vid.width", mCameraId, quality); 1392 1393 int videoFrameHeight = mEncoderProfiles->getCamcorderProfileParamByName( 1394 "vid.height", mCameraId, quality); 1395 1396 int audioCodec = mEncoderProfiles->getCamcorderProfileParamByName( 1397 "aud.codec", mCameraId, quality); 1398 1399 int audioBitRate = mEncoderProfiles->getCamcorderProfileParamByName( 1400 "aud.bps", mCameraId, quality); 1401 1402 int audioSampleRate = mEncoderProfiles->getCamcorderProfileParamByName( 1403 "aud.hz", mCameraId, quality); 1404 1405 int audioChannels = mEncoderProfiles->getCamcorderProfileParamByName( 1406 "aud.ch", mCameraId, quality); 1407 1408 if (durationUs == mMaxFileDurationUs && 1409 fileFormat == mOutputFormat && 1410 videoCodec == mVideoEncoder && 1411 videoBitRate == mVideoBitRate && 1412 videoFrameRate == mFrameRate && 1413 videoFrameWidth == mVideoWidth && 1414 videoFrameHeight == mVideoHeight && 1415 audioCodec == mAudioEncoder && 1416 audioBitRate == mAudioBitRate && 1417 audioSampleRate == mSampleRate && 1418 audioChannels == mAudioChannels) { 1419 if (videoCodec == VIDEO_ENCODER_H264) { 1420 ALOGI("Force to use AVC baseline profile"); 1421 setParamVideoEncoderProfile(OMX_VIDEO_AVCProfileBaseline); 1422 // set 0 for invalid levels - this will be rejected by the 1423 // codec if it cannot handle it during configure 1424 setParamVideoEncoderLevel(ACodec::getAVCLevelFor( 1425 videoFrameWidth, videoFrameHeight, videoFrameRate, videoBitRate)); 1426 } 1427 } 1428} 1429 1430void StagefrightRecorder::setDefaultVideoEncoderIfNecessary() { 1431 if (mVideoEncoder == VIDEO_ENCODER_DEFAULT) { 1432 if (mOutputFormat == OUTPUT_FORMAT_WEBM) { 1433 // default to VP8 for WEBM recording 1434 mVideoEncoder = VIDEO_ENCODER_VP8; 1435 } else { 1436 // pick the default encoder for CAMCORDER_QUALITY_LOW 1437 int videoCodec = mEncoderProfiles->getCamcorderProfileParamByName( 1438 "vid.codec", mCameraId, CAMCORDER_QUALITY_LOW); 1439 1440 if (videoCodec > VIDEO_ENCODER_DEFAULT && 1441 videoCodec < VIDEO_ENCODER_LIST_END) { 1442 mVideoEncoder = (video_encoder)videoCodec; 1443 } else { 1444 // default to H.264 if camcorder profile not available 1445 mVideoEncoder = VIDEO_ENCODER_H264; 1446 } 1447 } 1448 } 1449} 1450 1451status_t StagefrightRecorder::checkAudioEncoderCapabilities() { 1452 clipAudioBitRate(); 1453 clipAudioSampleRate(); 1454 clipNumberOfAudioChannels(); 1455 return OK; 1456} 1457 1458void StagefrightRecorder::clipAudioBitRate() { 1459 ALOGV("clipAudioBitRate: encoder %d", mAudioEncoder); 1460 1461 int minAudioBitRate = 1462 mEncoderProfiles->getAudioEncoderParamByName( 1463 "enc.aud.bps.min", mAudioEncoder); 1464 if (minAudioBitRate != -1 && mAudioBitRate < minAudioBitRate) { 1465 ALOGW("Intended audio encoding bit rate (%d) is too small" 1466 " and will be set to (%d)", mAudioBitRate, minAudioBitRate); 1467 mAudioBitRate = minAudioBitRate; 1468 } 1469 1470 int maxAudioBitRate = 1471 mEncoderProfiles->getAudioEncoderParamByName( 1472 "enc.aud.bps.max", mAudioEncoder); 1473 if (maxAudioBitRate != -1 && mAudioBitRate > maxAudioBitRate) { 1474 ALOGW("Intended audio encoding bit rate (%d) is too large" 1475 " and will be set to (%d)", mAudioBitRate, maxAudioBitRate); 1476 mAudioBitRate = maxAudioBitRate; 1477 } 1478} 1479 1480void StagefrightRecorder::clipAudioSampleRate() { 1481 ALOGV("clipAudioSampleRate: encoder %d", mAudioEncoder); 1482 1483 int minSampleRate = 1484 mEncoderProfiles->getAudioEncoderParamByName( 1485 "enc.aud.hz.min", mAudioEncoder); 1486 if (minSampleRate != -1 && mSampleRate < minSampleRate) { 1487 ALOGW("Intended audio sample rate (%d) is too small" 1488 " and will be set to (%d)", mSampleRate, minSampleRate); 1489 mSampleRate = minSampleRate; 1490 } 1491 1492 int maxSampleRate = 1493 mEncoderProfiles->getAudioEncoderParamByName( 1494 "enc.aud.hz.max", mAudioEncoder); 1495 if (maxSampleRate != -1 && mSampleRate > maxSampleRate) { 1496 ALOGW("Intended audio sample rate (%d) is too large" 1497 " and will be set to (%d)", mSampleRate, maxSampleRate); 1498 mSampleRate = maxSampleRate; 1499 } 1500} 1501 1502void StagefrightRecorder::clipNumberOfAudioChannels() { 1503 ALOGV("clipNumberOfAudioChannels: encoder %d", mAudioEncoder); 1504 1505 int minChannels = 1506 mEncoderProfiles->getAudioEncoderParamByName( 1507 "enc.aud.ch.min", mAudioEncoder); 1508 if (minChannels != -1 && mAudioChannels < minChannels) { 1509 ALOGW("Intended number of audio channels (%d) is too small" 1510 " and will be set to (%d)", mAudioChannels, minChannels); 1511 mAudioChannels = minChannels; 1512 } 1513 1514 int maxChannels = 1515 mEncoderProfiles->getAudioEncoderParamByName( 1516 "enc.aud.ch.max", mAudioEncoder); 1517 if (maxChannels != -1 && mAudioChannels > maxChannels) { 1518 ALOGW("Intended number of audio channels (%d) is too large" 1519 " and will be set to (%d)", mAudioChannels, maxChannels); 1520 mAudioChannels = maxChannels; 1521 } 1522} 1523 1524void StagefrightRecorder::clipVideoFrameHeight() { 1525 ALOGV("clipVideoFrameHeight: encoder %d", mVideoEncoder); 1526 int minFrameHeight = mEncoderProfiles->getVideoEncoderParamByName( 1527 "enc.vid.height.min", mVideoEncoder); 1528 int maxFrameHeight = mEncoderProfiles->getVideoEncoderParamByName( 1529 "enc.vid.height.max", mVideoEncoder); 1530 if (minFrameHeight != -1 && mVideoHeight < minFrameHeight) { 1531 ALOGW("Intended video encoding frame height (%d) is too small" 1532 " and will be set to (%d)", mVideoHeight, minFrameHeight); 1533 mVideoHeight = minFrameHeight; 1534 } else if (maxFrameHeight != -1 && mVideoHeight > maxFrameHeight) { 1535 ALOGW("Intended video encoding frame height (%d) is too large" 1536 " and will be set to (%d)", mVideoHeight, maxFrameHeight); 1537 mVideoHeight = maxFrameHeight; 1538 } 1539} 1540 1541// Set up the appropriate MediaSource depending on the chosen option 1542status_t StagefrightRecorder::setupMediaSource( 1543 sp<MediaSource> *mediaSource) { 1544 if (mVideoSource == VIDEO_SOURCE_DEFAULT 1545 || mVideoSource == VIDEO_SOURCE_CAMERA) { 1546 sp<CameraSource> cameraSource; 1547 status_t err = setupCameraSource(&cameraSource); 1548 if (err != OK) { 1549 return err; 1550 } 1551 *mediaSource = cameraSource; 1552 } else if (mVideoSource == VIDEO_SOURCE_SURFACE) { 1553 *mediaSource = NULL; 1554 } else { 1555 return INVALID_OPERATION; 1556 } 1557 return OK; 1558} 1559 1560status_t StagefrightRecorder::setupCameraSource( 1561 sp<CameraSource> *cameraSource) { 1562 status_t err = OK; 1563 if ((err = checkVideoEncoderCapabilities()) != OK) { 1564 return err; 1565 } 1566 Size videoSize; 1567 videoSize.width = mVideoWidth; 1568 videoSize.height = mVideoHeight; 1569 if (mCaptureFpsEnable) { 1570 if (!(mCaptureFps > 0.)) { 1571 ALOGE("Invalid mCaptureFps value: %lf", mCaptureFps); 1572 return BAD_VALUE; 1573 } 1574 1575 mCameraSourceTimeLapse = CameraSourceTimeLapse::CreateFromCamera( 1576 mCamera, mCameraProxy, mCameraId, mClientName, mClientUid, mClientPid, 1577 videoSize, mFrameRate, mPreviewSurface, 1578 std::llround(1e6 / mCaptureFps)); 1579 *cameraSource = mCameraSourceTimeLapse; 1580 } else { 1581 *cameraSource = CameraSource::CreateFromCamera( 1582 mCamera, mCameraProxy, mCameraId, mClientName, mClientUid, mClientPid, 1583 videoSize, mFrameRate, 1584 mPreviewSurface); 1585 } 1586 mCamera.clear(); 1587 mCameraProxy.clear(); 1588 if (*cameraSource == NULL) { 1589 return UNKNOWN_ERROR; 1590 } 1591 1592 if ((*cameraSource)->initCheck() != OK) { 1593 (*cameraSource).clear(); 1594 *cameraSource = NULL; 1595 return NO_INIT; 1596 } 1597 1598 // When frame rate is not set, the actual frame rate will be set to 1599 // the current frame rate being used. 1600 if (mFrameRate == -1) { 1601 int32_t frameRate = 0; 1602 CHECK ((*cameraSource)->getFormat()->findInt32( 1603 kKeyFrameRate, &frameRate)); 1604 ALOGI("Frame rate is not explicitly set. Use the current frame " 1605 "rate (%d fps)", frameRate); 1606 mFrameRate = frameRate; 1607 } 1608 1609 CHECK(mFrameRate != -1); 1610 1611 mMetaDataStoredInVideoBuffers = 1612 (*cameraSource)->metaDataStoredInVideoBuffers(); 1613 1614 return OK; 1615} 1616 1617status_t StagefrightRecorder::setupVideoEncoder( 1618 const sp<MediaSource> &cameraSource, 1619 sp<MediaCodecSource> *source) { 1620 source->clear(); 1621 1622 sp<AMessage> format = new AMessage(); 1623 1624 switch (mVideoEncoder) { 1625 case VIDEO_ENCODER_H263: 1626 format->setString("mime", MEDIA_MIMETYPE_VIDEO_H263); 1627 break; 1628 1629 case VIDEO_ENCODER_MPEG_4_SP: 1630 format->setString("mime", MEDIA_MIMETYPE_VIDEO_MPEG4); 1631 break; 1632 1633 case VIDEO_ENCODER_H264: 1634 format->setString("mime", MEDIA_MIMETYPE_VIDEO_AVC); 1635 break; 1636 1637 case VIDEO_ENCODER_VP8: 1638 format->setString("mime", MEDIA_MIMETYPE_VIDEO_VP8); 1639 break; 1640 1641 case VIDEO_ENCODER_HEVC: 1642 format->setString("mime", MEDIA_MIMETYPE_VIDEO_HEVC); 1643 break; 1644 1645 default: 1646 CHECK(!"Should not be here, unsupported video encoding."); 1647 break; 1648 } 1649 1650 if (cameraSource != NULL) { 1651 sp<MetaData> meta = cameraSource->getFormat(); 1652 1653 int32_t width, height, stride, sliceHeight, colorFormat; 1654 CHECK(meta->findInt32(kKeyWidth, &width)); 1655 CHECK(meta->findInt32(kKeyHeight, &height)); 1656 CHECK(meta->findInt32(kKeyStride, &stride)); 1657 CHECK(meta->findInt32(kKeySliceHeight, &sliceHeight)); 1658 CHECK(meta->findInt32(kKeyColorFormat, &colorFormat)); 1659 1660 format->setInt32("width", width); 1661 format->setInt32("height", height); 1662 format->setInt32("stride", stride); 1663 format->setInt32("slice-height", sliceHeight); 1664 format->setInt32("color-format", colorFormat); 1665 } else { 1666 format->setInt32("width", mVideoWidth); 1667 format->setInt32("height", mVideoHeight); 1668 format->setInt32("stride", mVideoWidth); 1669 format->setInt32("slice-height", mVideoHeight); 1670 format->setInt32("color-format", OMX_COLOR_FormatAndroidOpaque); 1671 1672 // set up time lapse/slow motion for surface source 1673 if (mCaptureFpsEnable) { 1674 if (!(mCaptureFps > 0.)) { 1675 ALOGE("Invalid mCaptureFps value: %lf", mCaptureFps); 1676 return BAD_VALUE; 1677 } 1678 format->setDouble("time-lapse-fps", mCaptureFps); 1679 } 1680 } 1681 1682 format->setInt32("bitrate", mVideoBitRate); 1683 format->setInt32("frame-rate", mFrameRate); 1684 format->setInt32("i-frame-interval", mIFramesIntervalSec); 1685 1686 if (mVideoTimeScale > 0) { 1687 format->setInt32("time-scale", mVideoTimeScale); 1688 } 1689 if (mVideoEncoderProfile != -1) { 1690 format->setInt32("profile", mVideoEncoderProfile); 1691 } 1692 if (mVideoEncoderLevel != -1) { 1693 format->setInt32("level", mVideoEncoderLevel); 1694 } 1695 1696 uint32_t tsLayers = 1; 1697 bool preferBFrames = true; // we like B-frames as it produces better quality per bitrate 1698 format->setInt32("priority", 0 /* realtime */); 1699 float maxPlaybackFps = mFrameRate; // assume video is only played back at normal speed 1700 1701 if (mCaptureFpsEnable) { 1702 format->setFloat("operating-rate", mCaptureFps); 1703 1704 // enable layering for all time lapse and high frame rate recordings 1705 if (mFrameRate / mCaptureFps >= 1.9) { // time lapse 1706 preferBFrames = false; 1707 tsLayers = 2; // use at least two layers as resulting video will likely be sped up 1708 } else if (mCaptureFps > maxPlaybackFps) { // slow-mo 1709 maxPlaybackFps = mCaptureFps; // assume video will be played back at full capture speed 1710 preferBFrames = false; 1711 } 1712 } 1713 1714 for (uint32_t tryLayers = 1; tryLayers <= kMaxNumVideoTemporalLayers; ++tryLayers) { 1715 if (tryLayers > tsLayers) { 1716 tsLayers = tryLayers; 1717 } 1718 // keep going until the base layer fps falls below the typical display refresh rate 1719 float baseLayerFps = maxPlaybackFps / (1 << (tryLayers - 1)); 1720 if (baseLayerFps < kMinTypicalDisplayRefreshingRate / 0.9) { 1721 break; 1722 } 1723 } 1724 1725 if (tsLayers > 1) { 1726 uint32_t bLayers = std::min(2u, tsLayers - 1); // use up-to 2 B-layers 1727 uint32_t pLayers = tsLayers - bLayers; 1728 format->setString( 1729 "ts-schema", AStringPrintf("android.generic.%u+%u", pLayers, bLayers)); 1730 1731 // TODO: some encoders do not support B-frames with temporal layering, and we have a 1732 // different preference based on use-case. We could move this into camera profiles. 1733 format->setInt32("android._prefer-b-frames", preferBFrames); 1734 } 1735 1736 if (mMetaDataStoredInVideoBuffers != kMetadataBufferTypeInvalid) { 1737 format->setInt32("android._input-metadata-buffer-type", mMetaDataStoredInVideoBuffers); 1738 } 1739 1740 uint32_t flags = 0; 1741 if (cameraSource == NULL) { 1742 flags |= MediaCodecSource::FLAG_USE_SURFACE_INPUT; 1743 } else { 1744 // require dataspace setup even if not using surface input 1745 format->setInt32("android._using-recorder", 1); 1746 } 1747 1748 sp<MediaCodecSource> encoder = MediaCodecSource::Create( 1749 mLooper, format, cameraSource, mPersistentSurface, flags); 1750 if (encoder == NULL) { 1751 ALOGE("Failed to create video encoder"); 1752 // When the encoder fails to be created, we need 1753 // release the camera source due to the camera's lock 1754 // and unlock mechanism. 1755 if (cameraSource != NULL) { 1756 cameraSource->stop(); 1757 } 1758 return UNKNOWN_ERROR; 1759 } 1760 1761 if (cameraSource == NULL) { 1762 mGraphicBufferProducer = encoder->getGraphicBufferProducer(); 1763 } 1764 1765 *source = encoder; 1766 1767 return OK; 1768} 1769 1770status_t StagefrightRecorder::setupAudioEncoder(const sp<MediaWriter>& writer) { 1771 status_t status = BAD_VALUE; 1772 if (OK != (status = checkAudioEncoderCapabilities())) { 1773 return status; 1774 } 1775 1776 switch(mAudioEncoder) { 1777 case AUDIO_ENCODER_AMR_NB: 1778 case AUDIO_ENCODER_AMR_WB: 1779 case AUDIO_ENCODER_AAC: 1780 case AUDIO_ENCODER_HE_AAC: 1781 case AUDIO_ENCODER_AAC_ELD: 1782 break; 1783 1784 default: 1785 ALOGE("Unsupported audio encoder: %d", mAudioEncoder); 1786 return UNKNOWN_ERROR; 1787 } 1788 1789 sp<MediaCodecSource> audioEncoder = createAudioSource(); 1790 if (audioEncoder == NULL) { 1791 return UNKNOWN_ERROR; 1792 } 1793 1794 writer->addSource(audioEncoder); 1795 mAudioEncoderSource = audioEncoder; 1796 return OK; 1797} 1798 1799status_t StagefrightRecorder::setupMPEG4orWEBMRecording() { 1800 mWriter.clear(); 1801 mTotalBitRate = 0; 1802 1803 status_t err = OK; 1804 sp<MediaWriter> writer; 1805 sp<MPEG4Writer> mp4writer; 1806 if (mOutputFormat == OUTPUT_FORMAT_WEBM) { 1807 writer = new WebmWriter(mOutputFd); 1808 } else { 1809 writer = mp4writer = new MPEG4Writer(mOutputFd); 1810 } 1811 1812 if (mVideoSource < VIDEO_SOURCE_LIST_END) { 1813 setDefaultVideoEncoderIfNecessary(); 1814 1815 sp<MediaSource> mediaSource; 1816 err = setupMediaSource(&mediaSource); 1817 if (err != OK) { 1818 return err; 1819 } 1820 1821 sp<MediaCodecSource> encoder; 1822 err = setupVideoEncoder(mediaSource, &encoder); 1823 if (err != OK) { 1824 return err; 1825 } 1826 1827 writer->addSource(encoder); 1828 mVideoEncoderSource = encoder; 1829 mTotalBitRate += mVideoBitRate; 1830 } 1831 1832 if (mOutputFormat != OUTPUT_FORMAT_WEBM) { 1833 // Audio source is added at the end if it exists. 1834 // This help make sure that the "recoding" sound is suppressed for 1835 // camcorder applications in the recorded files. 1836 // TODO Audio source is currently unsupported for webm output; vorbis encoder needed. 1837 // disable audio for time lapse recording 1838 bool disableAudio = mCaptureFpsEnable && mCaptureFps < mFrameRate; 1839 if (!disableAudio && mAudioSource != AUDIO_SOURCE_CNT) { 1840 err = setupAudioEncoder(writer); 1841 if (err != OK) return err; 1842 mTotalBitRate += mAudioBitRate; 1843 } 1844 1845 if (mCaptureFpsEnable) { 1846 mp4writer->setCaptureRate(mCaptureFps); 1847 } 1848 1849 if (mInterleaveDurationUs > 0) { 1850 mp4writer->setInterleaveDuration(mInterleaveDurationUs); 1851 } 1852 if (mLongitudex10000 > -3600000 && mLatitudex10000 > -3600000) { 1853 mp4writer->setGeoData(mLatitudex10000, mLongitudex10000); 1854 } 1855 } 1856 if (mMaxFileDurationUs != 0) { 1857 writer->setMaxFileDuration(mMaxFileDurationUs); 1858 } 1859 if (mMaxFileSizeBytes != 0) { 1860 writer->setMaxFileSize(mMaxFileSizeBytes); 1861 } 1862 if (mVideoSource == VIDEO_SOURCE_DEFAULT 1863 || mVideoSource == VIDEO_SOURCE_CAMERA) { 1864 mStartTimeOffsetMs = mEncoderProfiles->getStartTimeOffsetMs(mCameraId); 1865 } else if (mVideoSource == VIDEO_SOURCE_SURFACE) { 1866 // surface source doesn't need large initial delay 1867 mStartTimeOffsetMs = 200; 1868 } 1869 if (mStartTimeOffsetMs > 0) { 1870 writer->setStartTimeOffsetMs(mStartTimeOffsetMs); 1871 } 1872 1873 writer->setListener(mListener); 1874 mWriter = writer; 1875 return OK; 1876} 1877 1878void StagefrightRecorder::setupMPEG4orWEBMMetaData(sp<MetaData> *meta) { 1879 int64_t startTimeUs = systemTime() / 1000; 1880 (*meta)->setInt64(kKeyTime, startTimeUs); 1881 (*meta)->setInt32(kKeyFileType, mOutputFormat); 1882 (*meta)->setInt32(kKeyBitRate, mTotalBitRate); 1883 if (mMovieTimeScale > 0) { 1884 (*meta)->setInt32(kKeyTimeScale, mMovieTimeScale); 1885 } 1886 if (mOutputFormat != OUTPUT_FORMAT_WEBM) { 1887 (*meta)->setInt32(kKey64BitFileOffset, mUse64BitFileOffset); 1888 if (mTrackEveryTimeDurationUs > 0) { 1889 (*meta)->setInt64(kKeyTrackTimeStatus, mTrackEveryTimeDurationUs); 1890 } 1891 if (mRotationDegrees != 0) { 1892 (*meta)->setInt32(kKeyRotation, mRotationDegrees); 1893 } 1894 } 1895} 1896 1897status_t StagefrightRecorder::pause() { 1898 ALOGV("pause"); 1899 if (!mStarted) { 1900 return INVALID_OPERATION; 1901 } 1902 1903 // Already paused --- no-op. 1904 if (mPauseStartTimeUs != 0) { 1905 return OK; 1906 } 1907 1908 mPauseStartTimeUs = systemTime() / 1000; 1909 sp<MetaData> meta = new MetaData; 1910 meta->setInt64(kKeyTime, mPauseStartTimeUs); 1911 1912 if (mAudioEncoderSource != NULL) { 1913 mAudioEncoderSource->pause(); 1914 } 1915 if (mVideoEncoderSource != NULL) { 1916 mVideoEncoderSource->pause(meta.get()); 1917 } 1918 1919 return OK; 1920} 1921 1922status_t StagefrightRecorder::resume() { 1923 ALOGV("resume"); 1924 if (!mStarted) { 1925 return INVALID_OPERATION; 1926 } 1927 1928 // Not paused --- no-op. 1929 if (mPauseStartTimeUs == 0) { 1930 return OK; 1931 } 1932 1933 int64_t resumeStartTimeUs = systemTime() / 1000; 1934 1935 int64_t bufferStartTimeUs = 0; 1936 bool allSourcesStarted = true; 1937 for (const auto &source : { mAudioEncoderSource, mVideoEncoderSource }) { 1938 if (source == nullptr) { 1939 continue; 1940 } 1941 int64_t timeUs = source->getFirstSampleSystemTimeUs(); 1942 if (timeUs < 0) { 1943 allSourcesStarted = false; 1944 } 1945 if (bufferStartTimeUs < timeUs) { 1946 bufferStartTimeUs = timeUs; 1947 } 1948 } 1949 1950 if (allSourcesStarted) { 1951 if (mPauseStartTimeUs < bufferStartTimeUs) { 1952 mPauseStartTimeUs = bufferStartTimeUs; 1953 } 1954 // 30 ms buffer to avoid timestamp overlap 1955 mTotalPausedDurationUs += resumeStartTimeUs - mPauseStartTimeUs - 30000; 1956 } 1957 double timeOffset = -mTotalPausedDurationUs; 1958 if (mCaptureFpsEnable) { 1959 timeOffset *= mCaptureFps / mFrameRate; 1960 } 1961 sp<MetaData> meta = new MetaData; 1962 meta->setInt64(kKeyTime, resumeStartTimeUs); 1963 for (const auto &source : { mAudioEncoderSource, mVideoEncoderSource }) { 1964 if (source == nullptr) { 1965 continue; 1966 } 1967 source->setInputBufferTimeOffset((int64_t)timeOffset); 1968 source->start(meta.get()); 1969 } 1970 mPauseStartTimeUs = 0; 1971 1972 return OK; 1973} 1974 1975status_t StagefrightRecorder::stop() { 1976 ALOGV("stop"); 1977 Mutex::Autolock autolock(mLock); 1978 status_t err = OK; 1979 1980 if (mCaptureFpsEnable && mCameraSourceTimeLapse != NULL) { 1981 mCameraSourceTimeLapse->startQuickReadReturns(); 1982 mCameraSourceTimeLapse = NULL; 1983 } 1984 1985 if (mVideoEncoderSource != NULL) { 1986 int64_t stopTimeUs = systemTime() / 1000; 1987 sp<MetaData> meta = new MetaData; 1988 err = mVideoEncoderSource->setStopStimeUs(stopTimeUs); 1989 } 1990 1991 if (mWriter != NULL) { 1992 err = mWriter->stop(); 1993 mWriter.clear(); 1994 } 1995 1996 resetMetrics(); 1997 1998 mTotalPausedDurationUs = 0; 1999 mPauseStartTimeUs = 0; 2000 2001 mGraphicBufferProducer.clear(); 2002 mPersistentSurface.clear(); 2003 mAudioEncoderSource.clear(); 2004 mVideoEncoderSource.clear(); 2005 2006 if (mOutputFd >= 0) { 2007 ::close(mOutputFd); 2008 mOutputFd = -1; 2009 } 2010 2011 if (mStarted) { 2012 mStarted = false; 2013 2014 uint32_t params = 0; 2015 if (mAudioSource != AUDIO_SOURCE_CNT) { 2016 params |= IMediaPlayerService::kBatteryDataTrackAudio; 2017 } 2018 if (mVideoSource != VIDEO_SOURCE_LIST_END) { 2019 params |= IMediaPlayerService::kBatteryDataTrackVideo; 2020 } 2021 2022 addBatteryData(params); 2023 } 2024 2025 return err; 2026} 2027 2028status_t StagefrightRecorder::close() { 2029 ALOGV("close"); 2030 stop(); 2031 2032 return OK; 2033} 2034 2035status_t StagefrightRecorder::reset() { 2036 ALOGV("reset"); 2037 stop(); 2038 2039 // No audio or video source by default 2040 mAudioSource = AUDIO_SOURCE_CNT; 2041 mVideoSource = VIDEO_SOURCE_LIST_END; 2042 2043 // Default parameters 2044 mOutputFormat = OUTPUT_FORMAT_THREE_GPP; 2045 mAudioEncoder = AUDIO_ENCODER_AMR_NB; 2046 mVideoEncoder = VIDEO_ENCODER_DEFAULT; 2047 mVideoWidth = 176; 2048 mVideoHeight = 144; 2049 mFrameRate = -1; 2050 mVideoBitRate = 192000; 2051 mSampleRate = 8000; 2052 mAudioChannels = 1; 2053 mAudioBitRate = 12200; 2054 mInterleaveDurationUs = 0; 2055 mIFramesIntervalSec = 1; 2056 mAudioSourceNode = 0; 2057 mUse64BitFileOffset = false; 2058 mMovieTimeScale = -1; 2059 mAudioTimeScale = -1; 2060 mVideoTimeScale = -1; 2061 mCameraId = 0; 2062 mStartTimeOffsetMs = -1; 2063 mVideoEncoderProfile = -1; 2064 mVideoEncoderLevel = -1; 2065 mMaxFileDurationUs = 0; 2066 mMaxFileSizeBytes = 0; 2067 mTrackEveryTimeDurationUs = 0; 2068 mCaptureFpsEnable = false; 2069 mCaptureFps = -1.0; 2070 mCameraSourceTimeLapse = NULL; 2071 mMetaDataStoredInVideoBuffers = kMetadataBufferTypeInvalid; 2072 mEncoderProfiles = MediaProfiles::getInstance(); 2073 mRotationDegrees = 0; 2074 mLatitudex10000 = -3600000; 2075 mLongitudex10000 = -3600000; 2076 mTotalBitRate = 0; 2077 2078 mOutputFd = -1; 2079 2080 return OK; 2081} 2082 2083status_t StagefrightRecorder::getMaxAmplitude(int *max) { 2084 ALOGV("getMaxAmplitude"); 2085 2086 if (max == NULL) { 2087 ALOGE("Null pointer argument"); 2088 return BAD_VALUE; 2089 } 2090 2091 if (mAudioSourceNode != 0) { 2092 *max = mAudioSourceNode->getMaxAmplitude(); 2093 } else { 2094 *max = 0; 2095 } 2096 2097 return OK; 2098} 2099 2100status_t StagefrightRecorder::getMetrics(Parcel *reply) { 2101 ALOGD("StagefrightRecorder::getMetrics"); 2102 2103 if (reply == NULL) { 2104 ALOGE("Null pointer argument"); 2105 return BAD_VALUE; 2106 } 2107 2108 if (mAnalyticsItem == NULL) { 2109 return UNKNOWN_ERROR; 2110 } 2111 2112 updateMetrics(); 2113 mAnalyticsItem->writeToParcel(reply); 2114 return OK; 2115} 2116 2117status_t StagefrightRecorder::dump( 2118 int fd, const Vector<String16>& args) const { 2119 ALOGV("dump"); 2120 Mutex::Autolock autolock(mLock); 2121 const size_t SIZE = 256; 2122 char buffer[SIZE]; 2123 String8 result; 2124 if (mWriter != 0) { 2125 mWriter->dump(fd, args); 2126 } else { 2127 snprintf(buffer, SIZE, " No file writer\n"); 2128 result.append(buffer); 2129 } 2130 snprintf(buffer, SIZE, " Recorder: %p\n", this); 2131 snprintf(buffer, SIZE, " Output file (fd %d):\n", mOutputFd); 2132 result.append(buffer); 2133 snprintf(buffer, SIZE, " File format: %d\n", mOutputFormat); 2134 result.append(buffer); 2135 snprintf(buffer, SIZE, " Max file size (bytes): %" PRId64 "\n", mMaxFileSizeBytes); 2136 result.append(buffer); 2137 snprintf(buffer, SIZE, " Max file duration (us): %" PRId64 "\n", mMaxFileDurationUs); 2138 result.append(buffer); 2139 snprintf(buffer, SIZE, " File offset length (bits): %d\n", mUse64BitFileOffset? 64: 32); 2140 result.append(buffer); 2141 snprintf(buffer, SIZE, " Interleave duration (us): %d\n", mInterleaveDurationUs); 2142 result.append(buffer); 2143 snprintf(buffer, SIZE, " Progress notification: %" PRId64 " us\n", mTrackEveryTimeDurationUs); 2144 result.append(buffer); 2145 snprintf(buffer, SIZE, " Audio\n"); 2146 result.append(buffer); 2147 snprintf(buffer, SIZE, " Source: %d\n", mAudioSource); 2148 result.append(buffer); 2149 snprintf(buffer, SIZE, " Encoder: %d\n", mAudioEncoder); 2150 result.append(buffer); 2151 snprintf(buffer, SIZE, " Bit rate (bps): %d\n", mAudioBitRate); 2152 result.append(buffer); 2153 snprintf(buffer, SIZE, " Sampling rate (hz): %d\n", mSampleRate); 2154 result.append(buffer); 2155 snprintf(buffer, SIZE, " Number of channels: %d\n", mAudioChannels); 2156 result.append(buffer); 2157 snprintf(buffer, SIZE, " Max amplitude: %d\n", mAudioSourceNode == 0? 0: mAudioSourceNode->getMaxAmplitude()); 2158 result.append(buffer); 2159 snprintf(buffer, SIZE, " Video\n"); 2160 result.append(buffer); 2161 snprintf(buffer, SIZE, " Source: %d\n", mVideoSource); 2162 result.append(buffer); 2163 snprintf(buffer, SIZE, " Camera Id: %d\n", mCameraId); 2164 result.append(buffer); 2165 snprintf(buffer, SIZE, " Start time offset (ms): %d\n", mStartTimeOffsetMs); 2166 result.append(buffer); 2167 snprintf(buffer, SIZE, " Encoder: %d\n", mVideoEncoder); 2168 result.append(buffer); 2169 snprintf(buffer, SIZE, " Encoder profile: %d\n", mVideoEncoderProfile); 2170 result.append(buffer); 2171 snprintf(buffer, SIZE, " Encoder level: %d\n", mVideoEncoderLevel); 2172 result.append(buffer); 2173 snprintf(buffer, SIZE, " I frames interval (s): %d\n", mIFramesIntervalSec); 2174 result.append(buffer); 2175 snprintf(buffer, SIZE, " Frame size (pixels): %dx%d\n", mVideoWidth, mVideoHeight); 2176 result.append(buffer); 2177 snprintf(buffer, SIZE, " Frame rate (fps): %d\n", mFrameRate); 2178 result.append(buffer); 2179 snprintf(buffer, SIZE, " Bit rate (bps): %d\n", mVideoBitRate); 2180 result.append(buffer); 2181 ::write(fd, result.string(), result.size()); 2182 return OK; 2183} 2184} // namespace android 2185