StagefrightRecorder.cpp revision 73c3e6363b31fb27882c4666453ad8ef050b3cc1
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 mTimeBetweenCaptureUs = -1; 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 constexpr int64_t k1E12 = 1000000000000ll; 713 int64_t fpsx1e12 = k1E12 * fps; 714 if (fpsx1e12 == 0) { 715 ALOGE("FPS is zero or too small"); 716 return BAD_VALUE; 717 } 718 719 // This does not overflow since 10^6 * 10^12 < 2^63 720 int64_t timeUs = 1000000ll * k1E12 / fpsx1e12; 721 722 // Not allowing time more than a day and a millisecond for error margin. 723 // Note: 1e12 / 86400 = 11574074.(074) and 1e18 / 11574074 = 86400000553; 724 // therefore 1 ms of margin should be sufficient. 725 if (timeUs <= 0 || timeUs > 86400001000ll) { 726 ALOGE("Time between frame capture (%lld) is out of range [0, 1 Day]", (long long)timeUs); 727 return BAD_VALUE; 728 } 729 730 mCaptureFps = fps; 731 mTimeBetweenCaptureUs = timeUs; 732 return OK; 733} 734 735status_t StagefrightRecorder::setParamGeoDataLongitude( 736 int64_t longitudex10000) { 737 738 if (longitudex10000 > 1800000 || longitudex10000 < -1800000) { 739 return BAD_VALUE; 740 } 741 mLongitudex10000 = longitudex10000; 742 return OK; 743} 744 745status_t StagefrightRecorder::setParamGeoDataLatitude( 746 int64_t latitudex10000) { 747 748 if (latitudex10000 > 900000 || latitudex10000 < -900000) { 749 return BAD_VALUE; 750 } 751 mLatitudex10000 = latitudex10000; 752 return OK; 753} 754 755status_t StagefrightRecorder::setParameter( 756 const String8 &key, const String8 &value) { 757 ALOGV("setParameter: key (%s) => value (%s)", key.string(), value.string()); 758 if (key == "max-duration") { 759 int64_t max_duration_ms; 760 if (safe_strtoi64(value.string(), &max_duration_ms)) { 761 return setParamMaxFileDurationUs(1000LL * max_duration_ms); 762 } 763 } else if (key == "max-filesize") { 764 int64_t max_filesize_bytes; 765 if (safe_strtoi64(value.string(), &max_filesize_bytes)) { 766 return setParamMaxFileSizeBytes(max_filesize_bytes); 767 } 768 } else if (key == "interleave-duration-us") { 769 int32_t durationUs; 770 if (safe_strtoi32(value.string(), &durationUs)) { 771 return setParamInterleaveDuration(durationUs); 772 } 773 } else if (key == "param-movie-time-scale") { 774 int32_t timeScale; 775 if (safe_strtoi32(value.string(), &timeScale)) { 776 return setParamMovieTimeScale(timeScale); 777 } 778 } else if (key == "param-use-64bit-offset") { 779 int32_t use64BitOffset; 780 if (safe_strtoi32(value.string(), &use64BitOffset)) { 781 return setParam64BitFileOffset(use64BitOffset != 0); 782 } 783 } else if (key == "param-geotag-longitude") { 784 int64_t longitudex10000; 785 if (safe_strtoi64(value.string(), &longitudex10000)) { 786 return setParamGeoDataLongitude(longitudex10000); 787 } 788 } else if (key == "param-geotag-latitude") { 789 int64_t latitudex10000; 790 if (safe_strtoi64(value.string(), &latitudex10000)) { 791 return setParamGeoDataLatitude(latitudex10000); 792 } 793 } else if (key == "param-track-time-status") { 794 int64_t timeDurationUs; 795 if (safe_strtoi64(value.string(), &timeDurationUs)) { 796 return setParamTrackTimeStatus(timeDurationUs); 797 } 798 } else if (key == "audio-param-sampling-rate") { 799 int32_t sampling_rate; 800 if (safe_strtoi32(value.string(), &sampling_rate)) { 801 return setParamAudioSamplingRate(sampling_rate); 802 } 803 } else if (key == "audio-param-number-of-channels") { 804 int32_t number_of_channels; 805 if (safe_strtoi32(value.string(), &number_of_channels)) { 806 return setParamAudioNumberOfChannels(number_of_channels); 807 } 808 } else if (key == "audio-param-encoding-bitrate") { 809 int32_t audio_bitrate; 810 if (safe_strtoi32(value.string(), &audio_bitrate)) { 811 return setParamAudioEncodingBitRate(audio_bitrate); 812 } 813 } else if (key == "audio-param-time-scale") { 814 int32_t timeScale; 815 if (safe_strtoi32(value.string(), &timeScale)) { 816 return setParamAudioTimeScale(timeScale); 817 } 818 } else if (key == "video-param-encoding-bitrate") { 819 int32_t video_bitrate; 820 if (safe_strtoi32(value.string(), &video_bitrate)) { 821 return setParamVideoEncodingBitRate(video_bitrate); 822 } 823 } else if (key == "video-param-rotation-angle-degrees") { 824 int32_t degrees; 825 if (safe_strtoi32(value.string(), °rees)) { 826 return setParamVideoRotation(degrees); 827 } 828 } else if (key == "video-param-i-frames-interval") { 829 int32_t seconds; 830 if (safe_strtoi32(value.string(), &seconds)) { 831 return setParamVideoIFramesInterval(seconds); 832 } 833 } else if (key == "video-param-encoder-profile") { 834 int32_t profile; 835 if (safe_strtoi32(value.string(), &profile)) { 836 return setParamVideoEncoderProfile(profile); 837 } 838 } else if (key == "video-param-encoder-level") { 839 int32_t level; 840 if (safe_strtoi32(value.string(), &level)) { 841 return setParamVideoEncoderLevel(level); 842 } 843 } else if (key == "video-param-camera-id") { 844 int32_t cameraId; 845 if (safe_strtoi32(value.string(), &cameraId)) { 846 return setParamVideoCameraId(cameraId); 847 } 848 } else if (key == "video-param-time-scale") { 849 int32_t timeScale; 850 if (safe_strtoi32(value.string(), &timeScale)) { 851 return setParamVideoTimeScale(timeScale); 852 } 853 } else if (key == "time-lapse-enable") { 854 int32_t captureFpsEnable; 855 if (safe_strtoi32(value.string(), &captureFpsEnable)) { 856 return setParamCaptureFpsEnable(captureFpsEnable); 857 } 858 } else if (key == "time-lapse-fps") { 859 double fps; 860 if (safe_strtod(value.string(), &fps)) { 861 return setParamCaptureFps(fps); 862 } 863 } else { 864 ALOGE("setParameter: failed to find key %s", key.string()); 865 } 866 return BAD_VALUE; 867} 868 869status_t StagefrightRecorder::setParameters(const String8 ¶ms) { 870 ALOGV("setParameters: %s", params.string()); 871 const char *cparams = params.string(); 872 const char *key_start = cparams; 873 for (;;) { 874 const char *equal_pos = strchr(key_start, '='); 875 if (equal_pos == NULL) { 876 ALOGE("Parameters %s miss a value", cparams); 877 return BAD_VALUE; 878 } 879 String8 key(key_start, equal_pos - key_start); 880 TrimString(&key); 881 if (key.length() == 0) { 882 ALOGE("Parameters %s contains an empty key", cparams); 883 return BAD_VALUE; 884 } 885 const char *value_start = equal_pos + 1; 886 const char *semicolon_pos = strchr(value_start, ';'); 887 String8 value; 888 if (semicolon_pos == NULL) { 889 value.setTo(value_start); 890 } else { 891 value.setTo(value_start, semicolon_pos - value_start); 892 } 893 if (setParameter(key, value) != OK) { 894 return BAD_VALUE; 895 } 896 if (semicolon_pos == NULL) { 897 break; // Reaches the end 898 } 899 key_start = semicolon_pos + 1; 900 } 901 return OK; 902} 903 904status_t StagefrightRecorder::setListener(const sp<IMediaRecorderClient> &listener) { 905 mListener = listener; 906 907 return OK; 908} 909 910status_t StagefrightRecorder::setClientName(const String16& clientName) { 911 mClientName = clientName; 912 913 return OK; 914} 915 916status_t StagefrightRecorder::prepareInternal() { 917 ALOGV("prepare"); 918 if (mOutputFd < 0) { 919 ALOGE("Output file descriptor is invalid"); 920 return INVALID_OPERATION; 921 } 922 923 // Get UID and PID here for permission checking 924 mClientUid = IPCThreadState::self()->getCallingUid(); 925 mClientPid = IPCThreadState::self()->getCallingPid(); 926 927 status_t status = OK; 928 929 switch (mOutputFormat) { 930 case OUTPUT_FORMAT_DEFAULT: 931 case OUTPUT_FORMAT_THREE_GPP: 932 case OUTPUT_FORMAT_MPEG_4: 933 case OUTPUT_FORMAT_WEBM: 934 status = setupMPEG4orWEBMRecording(); 935 break; 936 937 case OUTPUT_FORMAT_AMR_NB: 938 case OUTPUT_FORMAT_AMR_WB: 939 status = setupAMRRecording(); 940 break; 941 942 case OUTPUT_FORMAT_AAC_ADIF: 943 case OUTPUT_FORMAT_AAC_ADTS: 944 status = setupAACRecording(); 945 break; 946 947 case OUTPUT_FORMAT_RTP_AVP: 948 status = setupRTPRecording(); 949 break; 950 951 case OUTPUT_FORMAT_MPEG2TS: 952 status = setupMPEG2TSRecording(); 953 break; 954 955 default: 956 ALOGE("Unsupported output file format: %d", mOutputFormat); 957 status = UNKNOWN_ERROR; 958 break; 959 } 960 961 ALOGV("Recording frameRate: %d captureFps: %f", 962 mFrameRate, mCaptureFps); 963 964 return status; 965} 966 967status_t StagefrightRecorder::prepare() { 968 ALOGV("prepare"); 969 Mutex::Autolock autolock(mLock); 970 if (mVideoSource == VIDEO_SOURCE_SURFACE) { 971 return prepareInternal(); 972 } 973 return OK; 974} 975 976status_t StagefrightRecorder::start() { 977 ALOGV("start"); 978 Mutex::Autolock autolock(mLock); 979 if (mOutputFd < 0) { 980 ALOGE("Output file descriptor is invalid"); 981 return INVALID_OPERATION; 982 } 983 984 status_t status = OK; 985 986 if (mVideoSource != VIDEO_SOURCE_SURFACE) { 987 status = prepareInternal(); 988 if (status != OK) { 989 return status; 990 } 991 } 992 993 if (mWriter == NULL) { 994 ALOGE("File writer is not avaialble"); 995 return UNKNOWN_ERROR; 996 } 997 998 switch (mOutputFormat) { 999 case OUTPUT_FORMAT_DEFAULT: 1000 case OUTPUT_FORMAT_THREE_GPP: 1001 case OUTPUT_FORMAT_MPEG_4: 1002 case OUTPUT_FORMAT_WEBM: 1003 { 1004 bool isMPEG4 = true; 1005 if (mOutputFormat == OUTPUT_FORMAT_WEBM) { 1006 isMPEG4 = false; 1007 } 1008 sp<MetaData> meta = new MetaData; 1009 setupMPEG4orWEBMMetaData(&meta); 1010 status = mWriter->start(meta.get()); 1011 break; 1012 } 1013 1014 case OUTPUT_FORMAT_AMR_NB: 1015 case OUTPUT_FORMAT_AMR_WB: 1016 case OUTPUT_FORMAT_AAC_ADIF: 1017 case OUTPUT_FORMAT_AAC_ADTS: 1018 case OUTPUT_FORMAT_RTP_AVP: 1019 case OUTPUT_FORMAT_MPEG2TS: 1020 { 1021 sp<MetaData> meta = new MetaData; 1022 int64_t startTimeUs = systemTime() / 1000; 1023 meta->setInt64(kKeyTime, startTimeUs); 1024 status = mWriter->start(meta.get()); 1025 break; 1026 } 1027 1028 default: 1029 { 1030 ALOGE("Unsupported output file format: %d", mOutputFormat); 1031 status = UNKNOWN_ERROR; 1032 break; 1033 } 1034 } 1035 1036 if (status != OK) { 1037 mWriter.clear(); 1038 mWriter = NULL; 1039 } 1040 1041 if ((status == OK) && (!mStarted)) { 1042 mAnalyticsDirty = true; 1043 mStarted = true; 1044 1045 uint32_t params = IMediaPlayerService::kBatteryDataCodecStarted; 1046 if (mAudioSource != AUDIO_SOURCE_CNT) { 1047 params |= IMediaPlayerService::kBatteryDataTrackAudio; 1048 } 1049 if (mVideoSource != VIDEO_SOURCE_LIST_END) { 1050 params |= IMediaPlayerService::kBatteryDataTrackVideo; 1051 } 1052 1053 addBatteryData(params); 1054 } 1055 1056 return status; 1057} 1058 1059sp<MediaCodecSource> StagefrightRecorder::createAudioSource() { 1060 int32_t sourceSampleRate = mSampleRate; 1061 1062 if (mCaptureFpsEnable && mCaptureFps >= mFrameRate) { 1063 // Upscale the sample rate for slow motion recording. 1064 // Fail audio source creation if source sample rate is too high, as it could 1065 // cause out-of-memory due to large input buffer size. And audio recording 1066 // probably doesn't make sense in the scenario, since the slow-down factor 1067 // is probably huge (eg. mSampleRate=48K, mCaptureFps=240, mFrameRate=1). 1068 const static int32_t SAMPLE_RATE_HZ_MAX = 192000; 1069 sourceSampleRate = 1070 (mSampleRate * mCaptureFps + mFrameRate / 2) / mFrameRate; 1071 if (sourceSampleRate < mSampleRate || sourceSampleRate > SAMPLE_RATE_HZ_MAX) { 1072 ALOGE("source sample rate out of range! " 1073 "(mSampleRate %d, mCaptureFps %.2f, mFrameRate %d", 1074 mSampleRate, mCaptureFps, mFrameRate); 1075 return NULL; 1076 } 1077 } 1078 1079 sp<AudioSource> audioSource = 1080 new AudioSource( 1081 mAudioSource, 1082 mOpPackageName, 1083 sourceSampleRate, 1084 mAudioChannels, 1085 mSampleRate, 1086 mClientUid, 1087 mClientPid); 1088 1089 status_t err = audioSource->initCheck(); 1090 1091 if (err != OK) { 1092 ALOGE("audio source is not initialized"); 1093 return NULL; 1094 } 1095 1096 sp<AMessage> format = new AMessage; 1097 switch (mAudioEncoder) { 1098 case AUDIO_ENCODER_AMR_NB: 1099 case AUDIO_ENCODER_DEFAULT: 1100 format->setString("mime", MEDIA_MIMETYPE_AUDIO_AMR_NB); 1101 break; 1102 case AUDIO_ENCODER_AMR_WB: 1103 format->setString("mime", MEDIA_MIMETYPE_AUDIO_AMR_WB); 1104 break; 1105 case AUDIO_ENCODER_AAC: 1106 format->setString("mime", MEDIA_MIMETYPE_AUDIO_AAC); 1107 format->setInt32("aac-profile", OMX_AUDIO_AACObjectLC); 1108 break; 1109 case AUDIO_ENCODER_HE_AAC: 1110 format->setString("mime", MEDIA_MIMETYPE_AUDIO_AAC); 1111 format->setInt32("aac-profile", OMX_AUDIO_AACObjectHE); 1112 break; 1113 case AUDIO_ENCODER_AAC_ELD: 1114 format->setString("mime", MEDIA_MIMETYPE_AUDIO_AAC); 1115 format->setInt32("aac-profile", OMX_AUDIO_AACObjectELD); 1116 break; 1117 1118 default: 1119 ALOGE("Unknown audio encoder: %d", mAudioEncoder); 1120 return NULL; 1121 } 1122 1123 int32_t maxInputSize; 1124 CHECK(audioSource->getFormat()->findInt32( 1125 kKeyMaxInputSize, &maxInputSize)); 1126 1127 format->setInt32("max-input-size", maxInputSize); 1128 format->setInt32("channel-count", mAudioChannels); 1129 format->setInt32("sample-rate", mSampleRate); 1130 format->setInt32("bitrate", mAudioBitRate); 1131 if (mAudioTimeScale > 0) { 1132 format->setInt32("time-scale", mAudioTimeScale); 1133 } 1134 format->setInt32("priority", 0 /* realtime */); 1135 1136 sp<MediaCodecSource> audioEncoder = 1137 MediaCodecSource::Create(mLooper, format, audioSource); 1138 mAudioSourceNode = audioSource; 1139 1140 if (audioEncoder == NULL) { 1141 ALOGE("Failed to create audio encoder"); 1142 } 1143 1144 return audioEncoder; 1145} 1146 1147status_t StagefrightRecorder::setupAACRecording() { 1148 // FIXME: 1149 // Add support for OUTPUT_FORMAT_AAC_ADIF 1150 CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_AAC_ADTS); 1151 1152 CHECK(mAudioEncoder == AUDIO_ENCODER_AAC || 1153 mAudioEncoder == AUDIO_ENCODER_HE_AAC || 1154 mAudioEncoder == AUDIO_ENCODER_AAC_ELD); 1155 CHECK(mAudioSource != AUDIO_SOURCE_CNT); 1156 1157 mWriter = new AACWriter(mOutputFd); 1158 return setupRawAudioRecording(); 1159} 1160 1161status_t StagefrightRecorder::setupAMRRecording() { 1162 CHECK(mOutputFormat == OUTPUT_FORMAT_AMR_NB || 1163 mOutputFormat == OUTPUT_FORMAT_AMR_WB); 1164 1165 if (mOutputFormat == OUTPUT_FORMAT_AMR_NB) { 1166 if (mAudioEncoder != AUDIO_ENCODER_DEFAULT && 1167 mAudioEncoder != AUDIO_ENCODER_AMR_NB) { 1168 ALOGE("Invalid encoder %d used for AMRNB recording", 1169 mAudioEncoder); 1170 return BAD_VALUE; 1171 } 1172 } else { // mOutputFormat must be OUTPUT_FORMAT_AMR_WB 1173 if (mAudioEncoder != AUDIO_ENCODER_AMR_WB) { 1174 ALOGE("Invlaid encoder %d used for AMRWB recording", 1175 mAudioEncoder); 1176 return BAD_VALUE; 1177 } 1178 } 1179 1180 mWriter = new AMRWriter(mOutputFd); 1181 return setupRawAudioRecording(); 1182} 1183 1184status_t StagefrightRecorder::setupRawAudioRecording() { 1185 if (mAudioSource >= AUDIO_SOURCE_CNT && mAudioSource != AUDIO_SOURCE_FM_TUNER) { 1186 ALOGE("Invalid audio source: %d", mAudioSource); 1187 return BAD_VALUE; 1188 } 1189 1190 status_t status = BAD_VALUE; 1191 if (OK != (status = checkAudioEncoderCapabilities())) { 1192 return status; 1193 } 1194 1195 sp<MediaCodecSource> audioEncoder = createAudioSource(); 1196 if (audioEncoder == NULL) { 1197 return UNKNOWN_ERROR; 1198 } 1199 1200 CHECK(mWriter != 0); 1201 mWriter->addSource(audioEncoder); 1202 mAudioEncoderSource = audioEncoder; 1203 1204 if (mMaxFileDurationUs != 0) { 1205 mWriter->setMaxFileDuration(mMaxFileDurationUs); 1206 } 1207 if (mMaxFileSizeBytes != 0) { 1208 mWriter->setMaxFileSize(mMaxFileSizeBytes); 1209 } 1210 mWriter->setListener(mListener); 1211 1212 return OK; 1213} 1214 1215status_t StagefrightRecorder::setupRTPRecording() { 1216 CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_RTP_AVP); 1217 1218 if ((mAudioSource != AUDIO_SOURCE_CNT 1219 && mVideoSource != VIDEO_SOURCE_LIST_END) 1220 || (mAudioSource == AUDIO_SOURCE_CNT 1221 && mVideoSource == VIDEO_SOURCE_LIST_END)) { 1222 // Must have exactly one source. 1223 return BAD_VALUE; 1224 } 1225 1226 if (mOutputFd < 0) { 1227 return BAD_VALUE; 1228 } 1229 1230 sp<MediaCodecSource> source; 1231 1232 if (mAudioSource != AUDIO_SOURCE_CNT) { 1233 source = createAudioSource(); 1234 mAudioEncoderSource = source; 1235 } else { 1236 setDefaultVideoEncoderIfNecessary(); 1237 1238 sp<MediaSource> mediaSource; 1239 status_t err = setupMediaSource(&mediaSource); 1240 if (err != OK) { 1241 return err; 1242 } 1243 1244 err = setupVideoEncoder(mediaSource, &source); 1245 if (err != OK) { 1246 return err; 1247 } 1248 mVideoEncoderSource = source; 1249 } 1250 1251 mWriter = new ARTPWriter(mOutputFd); 1252 mWriter->addSource(source); 1253 mWriter->setListener(mListener); 1254 1255 return OK; 1256} 1257 1258status_t StagefrightRecorder::setupMPEG2TSRecording() { 1259 CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_MPEG2TS); 1260 1261 sp<MediaWriter> writer = new MPEG2TSWriter(mOutputFd); 1262 1263 if (mAudioSource != AUDIO_SOURCE_CNT) { 1264 if (mAudioEncoder != AUDIO_ENCODER_AAC && 1265 mAudioEncoder != AUDIO_ENCODER_HE_AAC && 1266 mAudioEncoder != AUDIO_ENCODER_AAC_ELD) { 1267 return ERROR_UNSUPPORTED; 1268 } 1269 1270 status_t err = setupAudioEncoder(writer); 1271 1272 if (err != OK) { 1273 return err; 1274 } 1275 } 1276 1277 if (mVideoSource < VIDEO_SOURCE_LIST_END) { 1278 if (mVideoEncoder != VIDEO_ENCODER_H264) { 1279 ALOGE("MPEG2TS recording only supports H.264 encoding!"); 1280 return ERROR_UNSUPPORTED; 1281 } 1282 1283 sp<MediaSource> mediaSource; 1284 status_t err = setupMediaSource(&mediaSource); 1285 if (err != OK) { 1286 return err; 1287 } 1288 1289 sp<MediaCodecSource> encoder; 1290 err = setupVideoEncoder(mediaSource, &encoder); 1291 1292 if (err != OK) { 1293 return err; 1294 } 1295 1296 writer->addSource(encoder); 1297 mVideoEncoderSource = encoder; 1298 } 1299 1300 if (mMaxFileDurationUs != 0) { 1301 writer->setMaxFileDuration(mMaxFileDurationUs); 1302 } 1303 1304 if (mMaxFileSizeBytes != 0) { 1305 writer->setMaxFileSize(mMaxFileSizeBytes); 1306 } 1307 1308 mWriter = writer; 1309 1310 return OK; 1311} 1312 1313void StagefrightRecorder::clipVideoFrameRate() { 1314 ALOGV("clipVideoFrameRate: encoder %d", mVideoEncoder); 1315 if (mFrameRate == -1) { 1316 mFrameRate = mEncoderProfiles->getCamcorderProfileParamByName( 1317 "vid.fps", mCameraId, CAMCORDER_QUALITY_LOW); 1318 ALOGW("Using default video fps %d", mFrameRate); 1319 } 1320 1321 int minFrameRate = mEncoderProfiles->getVideoEncoderParamByName( 1322 "enc.vid.fps.min", mVideoEncoder); 1323 int maxFrameRate = mEncoderProfiles->getVideoEncoderParamByName( 1324 "enc.vid.fps.max", mVideoEncoder); 1325 if (mFrameRate < minFrameRate && minFrameRate != -1) { 1326 ALOGW("Intended video encoding frame rate (%d fps) is too small" 1327 " and will be set to (%d fps)", mFrameRate, minFrameRate); 1328 mFrameRate = minFrameRate; 1329 } else if (mFrameRate > maxFrameRate && maxFrameRate != -1) { 1330 ALOGW("Intended video encoding frame rate (%d fps) is too large" 1331 " and will be set to (%d fps)", mFrameRate, maxFrameRate); 1332 mFrameRate = maxFrameRate; 1333 } 1334} 1335 1336void StagefrightRecorder::clipVideoBitRate() { 1337 ALOGV("clipVideoBitRate: encoder %d", mVideoEncoder); 1338 int minBitRate = mEncoderProfiles->getVideoEncoderParamByName( 1339 "enc.vid.bps.min", mVideoEncoder); 1340 int maxBitRate = mEncoderProfiles->getVideoEncoderParamByName( 1341 "enc.vid.bps.max", mVideoEncoder); 1342 if (mVideoBitRate < minBitRate && minBitRate != -1) { 1343 ALOGW("Intended video encoding bit rate (%d bps) is too small" 1344 " and will be set to (%d bps)", mVideoBitRate, minBitRate); 1345 mVideoBitRate = minBitRate; 1346 } else if (mVideoBitRate > maxBitRate && maxBitRate != -1) { 1347 ALOGW("Intended video encoding bit rate (%d bps) is too large" 1348 " and will be set to (%d bps)", mVideoBitRate, maxBitRate); 1349 mVideoBitRate = maxBitRate; 1350 } 1351} 1352 1353void StagefrightRecorder::clipVideoFrameWidth() { 1354 ALOGV("clipVideoFrameWidth: encoder %d", mVideoEncoder); 1355 int minFrameWidth = mEncoderProfiles->getVideoEncoderParamByName( 1356 "enc.vid.width.min", mVideoEncoder); 1357 int maxFrameWidth = mEncoderProfiles->getVideoEncoderParamByName( 1358 "enc.vid.width.max", mVideoEncoder); 1359 if (mVideoWidth < minFrameWidth && minFrameWidth != -1) { 1360 ALOGW("Intended video encoding frame width (%d) is too small" 1361 " and will be set to (%d)", mVideoWidth, minFrameWidth); 1362 mVideoWidth = minFrameWidth; 1363 } else if (mVideoWidth > maxFrameWidth && maxFrameWidth != -1) { 1364 ALOGW("Intended video encoding frame width (%d) is too large" 1365 " and will be set to (%d)", mVideoWidth, maxFrameWidth); 1366 mVideoWidth = maxFrameWidth; 1367 } 1368} 1369 1370status_t StagefrightRecorder::checkVideoEncoderCapabilities() { 1371 if (!mCaptureFpsEnable) { 1372 // Dont clip for time lapse capture as encoder will have enough 1373 // time to encode because of slow capture rate of time lapse. 1374 clipVideoBitRate(); 1375 clipVideoFrameRate(); 1376 clipVideoFrameWidth(); 1377 clipVideoFrameHeight(); 1378 setDefaultProfileIfNecessary(); 1379 } 1380 return OK; 1381} 1382 1383// Set to use AVC baseline profile if the encoding parameters matches 1384// CAMCORDER_QUALITY_LOW profile; this is for the sake of MMS service. 1385void StagefrightRecorder::setDefaultProfileIfNecessary() { 1386 ALOGV("setDefaultProfileIfNecessary"); 1387 1388 camcorder_quality quality = CAMCORDER_QUALITY_LOW; 1389 1390 int64_t durationUs = mEncoderProfiles->getCamcorderProfileParamByName( 1391 "duration", mCameraId, quality) * 1000000LL; 1392 1393 int fileFormat = mEncoderProfiles->getCamcorderProfileParamByName( 1394 "file.format", mCameraId, quality); 1395 1396 int videoCodec = mEncoderProfiles->getCamcorderProfileParamByName( 1397 "vid.codec", mCameraId, quality); 1398 1399 int videoBitRate = mEncoderProfiles->getCamcorderProfileParamByName( 1400 "vid.bps", mCameraId, quality); 1401 1402 int videoFrameRate = mEncoderProfiles->getCamcorderProfileParamByName( 1403 "vid.fps", mCameraId, quality); 1404 1405 int videoFrameWidth = mEncoderProfiles->getCamcorderProfileParamByName( 1406 "vid.width", mCameraId, quality); 1407 1408 int videoFrameHeight = mEncoderProfiles->getCamcorderProfileParamByName( 1409 "vid.height", mCameraId, quality); 1410 1411 int audioCodec = mEncoderProfiles->getCamcorderProfileParamByName( 1412 "aud.codec", mCameraId, quality); 1413 1414 int audioBitRate = mEncoderProfiles->getCamcorderProfileParamByName( 1415 "aud.bps", mCameraId, quality); 1416 1417 int audioSampleRate = mEncoderProfiles->getCamcorderProfileParamByName( 1418 "aud.hz", mCameraId, quality); 1419 1420 int audioChannels = mEncoderProfiles->getCamcorderProfileParamByName( 1421 "aud.ch", mCameraId, quality); 1422 1423 if (durationUs == mMaxFileDurationUs && 1424 fileFormat == mOutputFormat && 1425 videoCodec == mVideoEncoder && 1426 videoBitRate == mVideoBitRate && 1427 videoFrameRate == mFrameRate && 1428 videoFrameWidth == mVideoWidth && 1429 videoFrameHeight == mVideoHeight && 1430 audioCodec == mAudioEncoder && 1431 audioBitRate == mAudioBitRate && 1432 audioSampleRate == mSampleRate && 1433 audioChannels == mAudioChannels) { 1434 if (videoCodec == VIDEO_ENCODER_H264) { 1435 ALOGI("Force to use AVC baseline profile"); 1436 setParamVideoEncoderProfile(OMX_VIDEO_AVCProfileBaseline); 1437 // set 0 for invalid levels - this will be rejected by the 1438 // codec if it cannot handle it during configure 1439 setParamVideoEncoderLevel(ACodec::getAVCLevelFor( 1440 videoFrameWidth, videoFrameHeight, videoFrameRate, videoBitRate)); 1441 } 1442 } 1443} 1444 1445void StagefrightRecorder::setDefaultVideoEncoderIfNecessary() { 1446 if (mVideoEncoder == VIDEO_ENCODER_DEFAULT) { 1447 if (mOutputFormat == OUTPUT_FORMAT_WEBM) { 1448 // default to VP8 for WEBM recording 1449 mVideoEncoder = VIDEO_ENCODER_VP8; 1450 } else { 1451 // pick the default encoder for CAMCORDER_QUALITY_LOW 1452 int videoCodec = mEncoderProfiles->getCamcorderProfileParamByName( 1453 "vid.codec", mCameraId, CAMCORDER_QUALITY_LOW); 1454 1455 if (videoCodec > VIDEO_ENCODER_DEFAULT && 1456 videoCodec < VIDEO_ENCODER_LIST_END) { 1457 mVideoEncoder = (video_encoder)videoCodec; 1458 } else { 1459 // default to H.264 if camcorder profile not available 1460 mVideoEncoder = VIDEO_ENCODER_H264; 1461 } 1462 } 1463 } 1464} 1465 1466status_t StagefrightRecorder::checkAudioEncoderCapabilities() { 1467 clipAudioBitRate(); 1468 clipAudioSampleRate(); 1469 clipNumberOfAudioChannels(); 1470 return OK; 1471} 1472 1473void StagefrightRecorder::clipAudioBitRate() { 1474 ALOGV("clipAudioBitRate: encoder %d", mAudioEncoder); 1475 1476 int minAudioBitRate = 1477 mEncoderProfiles->getAudioEncoderParamByName( 1478 "enc.aud.bps.min", mAudioEncoder); 1479 if (minAudioBitRate != -1 && mAudioBitRate < minAudioBitRate) { 1480 ALOGW("Intended audio encoding bit rate (%d) is too small" 1481 " and will be set to (%d)", mAudioBitRate, minAudioBitRate); 1482 mAudioBitRate = minAudioBitRate; 1483 } 1484 1485 int maxAudioBitRate = 1486 mEncoderProfiles->getAudioEncoderParamByName( 1487 "enc.aud.bps.max", mAudioEncoder); 1488 if (maxAudioBitRate != -1 && mAudioBitRate > maxAudioBitRate) { 1489 ALOGW("Intended audio encoding bit rate (%d) is too large" 1490 " and will be set to (%d)", mAudioBitRate, maxAudioBitRate); 1491 mAudioBitRate = maxAudioBitRate; 1492 } 1493} 1494 1495void StagefrightRecorder::clipAudioSampleRate() { 1496 ALOGV("clipAudioSampleRate: encoder %d", mAudioEncoder); 1497 1498 int minSampleRate = 1499 mEncoderProfiles->getAudioEncoderParamByName( 1500 "enc.aud.hz.min", mAudioEncoder); 1501 if (minSampleRate != -1 && mSampleRate < minSampleRate) { 1502 ALOGW("Intended audio sample rate (%d) is too small" 1503 " and will be set to (%d)", mSampleRate, minSampleRate); 1504 mSampleRate = minSampleRate; 1505 } 1506 1507 int maxSampleRate = 1508 mEncoderProfiles->getAudioEncoderParamByName( 1509 "enc.aud.hz.max", mAudioEncoder); 1510 if (maxSampleRate != -1 && mSampleRate > maxSampleRate) { 1511 ALOGW("Intended audio sample rate (%d) is too large" 1512 " and will be set to (%d)", mSampleRate, maxSampleRate); 1513 mSampleRate = maxSampleRate; 1514 } 1515} 1516 1517void StagefrightRecorder::clipNumberOfAudioChannels() { 1518 ALOGV("clipNumberOfAudioChannels: encoder %d", mAudioEncoder); 1519 1520 int minChannels = 1521 mEncoderProfiles->getAudioEncoderParamByName( 1522 "enc.aud.ch.min", mAudioEncoder); 1523 if (minChannels != -1 && mAudioChannels < minChannels) { 1524 ALOGW("Intended number of audio channels (%d) is too small" 1525 " and will be set to (%d)", mAudioChannels, minChannels); 1526 mAudioChannels = minChannels; 1527 } 1528 1529 int maxChannels = 1530 mEncoderProfiles->getAudioEncoderParamByName( 1531 "enc.aud.ch.max", mAudioEncoder); 1532 if (maxChannels != -1 && mAudioChannels > maxChannels) { 1533 ALOGW("Intended number of audio channels (%d) is too large" 1534 " and will be set to (%d)", mAudioChannels, maxChannels); 1535 mAudioChannels = maxChannels; 1536 } 1537} 1538 1539void StagefrightRecorder::clipVideoFrameHeight() { 1540 ALOGV("clipVideoFrameHeight: encoder %d", mVideoEncoder); 1541 int minFrameHeight = mEncoderProfiles->getVideoEncoderParamByName( 1542 "enc.vid.height.min", mVideoEncoder); 1543 int maxFrameHeight = mEncoderProfiles->getVideoEncoderParamByName( 1544 "enc.vid.height.max", mVideoEncoder); 1545 if (minFrameHeight != -1 && mVideoHeight < minFrameHeight) { 1546 ALOGW("Intended video encoding frame height (%d) is too small" 1547 " and will be set to (%d)", mVideoHeight, minFrameHeight); 1548 mVideoHeight = minFrameHeight; 1549 } else if (maxFrameHeight != -1 && mVideoHeight > maxFrameHeight) { 1550 ALOGW("Intended video encoding frame height (%d) is too large" 1551 " and will be set to (%d)", mVideoHeight, maxFrameHeight); 1552 mVideoHeight = maxFrameHeight; 1553 } 1554} 1555 1556// Set up the appropriate MediaSource depending on the chosen option 1557status_t StagefrightRecorder::setupMediaSource( 1558 sp<MediaSource> *mediaSource) { 1559 if (mVideoSource == VIDEO_SOURCE_DEFAULT 1560 || mVideoSource == VIDEO_SOURCE_CAMERA) { 1561 sp<CameraSource> cameraSource; 1562 status_t err = setupCameraSource(&cameraSource); 1563 if (err != OK) { 1564 return err; 1565 } 1566 *mediaSource = cameraSource; 1567 } else if (mVideoSource == VIDEO_SOURCE_SURFACE) { 1568 *mediaSource = NULL; 1569 } else { 1570 return INVALID_OPERATION; 1571 } 1572 return OK; 1573} 1574 1575status_t StagefrightRecorder::setupCameraSource( 1576 sp<CameraSource> *cameraSource) { 1577 status_t err = OK; 1578 if ((err = checkVideoEncoderCapabilities()) != OK) { 1579 return err; 1580 } 1581 Size videoSize; 1582 videoSize.width = mVideoWidth; 1583 videoSize.height = mVideoHeight; 1584 if (mCaptureFpsEnable) { 1585 if (mTimeBetweenCaptureUs < 0) { 1586 ALOGE("Invalid mTimeBetweenTimeLapseFrameCaptureUs value: %lld", 1587 (long long)mTimeBetweenCaptureUs); 1588 return BAD_VALUE; 1589 } 1590 1591 mCameraSourceTimeLapse = CameraSourceTimeLapse::CreateFromCamera( 1592 mCamera, mCameraProxy, mCameraId, mClientName, mClientUid, mClientPid, 1593 videoSize, mFrameRate, mPreviewSurface, 1594 mTimeBetweenCaptureUs); 1595 *cameraSource = mCameraSourceTimeLapse; 1596 } else { 1597 *cameraSource = CameraSource::CreateFromCamera( 1598 mCamera, mCameraProxy, mCameraId, mClientName, mClientUid, mClientPid, 1599 videoSize, mFrameRate, 1600 mPreviewSurface); 1601 } 1602 mCamera.clear(); 1603 mCameraProxy.clear(); 1604 if (*cameraSource == NULL) { 1605 return UNKNOWN_ERROR; 1606 } 1607 1608 if ((*cameraSource)->initCheck() != OK) { 1609 (*cameraSource).clear(); 1610 *cameraSource = NULL; 1611 return NO_INIT; 1612 } 1613 1614 // When frame rate is not set, the actual frame rate will be set to 1615 // the current frame rate being used. 1616 if (mFrameRate == -1) { 1617 int32_t frameRate = 0; 1618 CHECK ((*cameraSource)->getFormat()->findInt32( 1619 kKeyFrameRate, &frameRate)); 1620 ALOGI("Frame rate is not explicitly set. Use the current frame " 1621 "rate (%d fps)", frameRate); 1622 mFrameRate = frameRate; 1623 } 1624 1625 CHECK(mFrameRate != -1); 1626 1627 mMetaDataStoredInVideoBuffers = 1628 (*cameraSource)->metaDataStoredInVideoBuffers(); 1629 1630 return OK; 1631} 1632 1633status_t StagefrightRecorder::setupVideoEncoder( 1634 const sp<MediaSource> &cameraSource, 1635 sp<MediaCodecSource> *source) { 1636 source->clear(); 1637 1638 sp<AMessage> format = new AMessage(); 1639 1640 switch (mVideoEncoder) { 1641 case VIDEO_ENCODER_H263: 1642 format->setString("mime", MEDIA_MIMETYPE_VIDEO_H263); 1643 break; 1644 1645 case VIDEO_ENCODER_MPEG_4_SP: 1646 format->setString("mime", MEDIA_MIMETYPE_VIDEO_MPEG4); 1647 break; 1648 1649 case VIDEO_ENCODER_H264: 1650 format->setString("mime", MEDIA_MIMETYPE_VIDEO_AVC); 1651 break; 1652 1653 case VIDEO_ENCODER_VP8: 1654 format->setString("mime", MEDIA_MIMETYPE_VIDEO_VP8); 1655 break; 1656 1657 case VIDEO_ENCODER_HEVC: 1658 format->setString("mime", MEDIA_MIMETYPE_VIDEO_HEVC); 1659 break; 1660 1661 default: 1662 CHECK(!"Should not be here, unsupported video encoding."); 1663 break; 1664 } 1665 1666 if (cameraSource != NULL) { 1667 sp<MetaData> meta = cameraSource->getFormat(); 1668 1669 int32_t width, height, stride, sliceHeight, colorFormat; 1670 CHECK(meta->findInt32(kKeyWidth, &width)); 1671 CHECK(meta->findInt32(kKeyHeight, &height)); 1672 CHECK(meta->findInt32(kKeyStride, &stride)); 1673 CHECK(meta->findInt32(kKeySliceHeight, &sliceHeight)); 1674 CHECK(meta->findInt32(kKeyColorFormat, &colorFormat)); 1675 1676 format->setInt32("width", width); 1677 format->setInt32("height", height); 1678 format->setInt32("stride", stride); 1679 format->setInt32("slice-height", sliceHeight); 1680 format->setInt32("color-format", colorFormat); 1681 } else { 1682 format->setInt32("width", mVideoWidth); 1683 format->setInt32("height", mVideoHeight); 1684 format->setInt32("stride", mVideoWidth); 1685 format->setInt32("slice-height", mVideoHeight); 1686 format->setInt32("color-format", OMX_COLOR_FormatAndroidOpaque); 1687 1688 // set up time lapse/slow motion for surface source 1689 if (mCaptureFpsEnable) { 1690 if (mTimeBetweenCaptureUs <= 0) { 1691 ALOGE("Invalid mTimeBetweenCaptureUs value: %lld", 1692 (long long)mTimeBetweenCaptureUs); 1693 return BAD_VALUE; 1694 } 1695 format->setInt64("time-lapse", mTimeBetweenCaptureUs); 1696 } 1697 } 1698 1699 format->setInt32("bitrate", mVideoBitRate); 1700 format->setInt32("frame-rate", mFrameRate); 1701 format->setInt32("i-frame-interval", mIFramesIntervalSec); 1702 1703 if (mVideoTimeScale > 0) { 1704 format->setInt32("time-scale", mVideoTimeScale); 1705 } 1706 if (mVideoEncoderProfile != -1) { 1707 format->setInt32("profile", mVideoEncoderProfile); 1708 } 1709 if (mVideoEncoderLevel != -1) { 1710 format->setInt32("level", mVideoEncoderLevel); 1711 } 1712 1713 uint32_t tsLayers = 1; 1714 bool preferBFrames = true; // we like B-frames as it produces better quality per bitrate 1715 format->setInt32("priority", 0 /* realtime */); 1716 float maxPlaybackFps = mFrameRate; // assume video is only played back at normal speed 1717 1718 if (mCaptureFpsEnable) { 1719 format->setFloat("operating-rate", mCaptureFps); 1720 1721 // enable layering for all time lapse and high frame rate recordings 1722 if (mFrameRate / mCaptureFps >= 1.9) { // time lapse 1723 preferBFrames = false; 1724 tsLayers = 2; // use at least two layers as resulting video will likely be sped up 1725 } else if (mCaptureFps > maxPlaybackFps) { // slow-mo 1726 maxPlaybackFps = mCaptureFps; // assume video will be played back at full capture speed 1727 preferBFrames = false; 1728 } 1729 } 1730 1731 for (uint32_t tryLayers = 1; tryLayers <= kMaxNumVideoTemporalLayers; ++tryLayers) { 1732 if (tryLayers > tsLayers) { 1733 tsLayers = tryLayers; 1734 } 1735 // keep going until the base layer fps falls below the typical display refresh rate 1736 float baseLayerFps = maxPlaybackFps / (1 << (tryLayers - 1)); 1737 if (baseLayerFps < kMinTypicalDisplayRefreshingRate / 0.9) { 1738 break; 1739 } 1740 } 1741 1742 if (tsLayers > 1) { 1743 uint32_t bLayers = std::min(2u, tsLayers - 1); // use up-to 2 B-layers 1744 uint32_t pLayers = tsLayers - bLayers; 1745 format->setString( 1746 "ts-schema", AStringPrintf("android.generic.%u+%u", pLayers, bLayers)); 1747 1748 // TODO: some encoders do not support B-frames with temporal layering, and we have a 1749 // different preference based on use-case. We could move this into camera profiles. 1750 format->setInt32("android._prefer-b-frames", preferBFrames); 1751 } 1752 1753 if (mMetaDataStoredInVideoBuffers != kMetadataBufferTypeInvalid) { 1754 format->setInt32("android._input-metadata-buffer-type", mMetaDataStoredInVideoBuffers); 1755 } 1756 1757 uint32_t flags = 0; 1758 if (cameraSource == NULL) { 1759 flags |= MediaCodecSource::FLAG_USE_SURFACE_INPUT; 1760 } else { 1761 // require dataspace setup even if not using surface input 1762 format->setInt32("android._using-recorder", 1); 1763 } 1764 1765 sp<MediaCodecSource> encoder = MediaCodecSource::Create( 1766 mLooper, format, cameraSource, mPersistentSurface, flags); 1767 if (encoder == NULL) { 1768 ALOGE("Failed to create video encoder"); 1769 // When the encoder fails to be created, we need 1770 // release the camera source due to the camera's lock 1771 // and unlock mechanism. 1772 if (cameraSource != NULL) { 1773 cameraSource->stop(); 1774 } 1775 return UNKNOWN_ERROR; 1776 } 1777 1778 if (cameraSource == NULL) { 1779 mGraphicBufferProducer = encoder->getGraphicBufferProducer(); 1780 } 1781 1782 *source = encoder; 1783 1784 return OK; 1785} 1786 1787status_t StagefrightRecorder::setupAudioEncoder(const sp<MediaWriter>& writer) { 1788 status_t status = BAD_VALUE; 1789 if (OK != (status = checkAudioEncoderCapabilities())) { 1790 return status; 1791 } 1792 1793 switch(mAudioEncoder) { 1794 case AUDIO_ENCODER_AMR_NB: 1795 case AUDIO_ENCODER_AMR_WB: 1796 case AUDIO_ENCODER_AAC: 1797 case AUDIO_ENCODER_HE_AAC: 1798 case AUDIO_ENCODER_AAC_ELD: 1799 break; 1800 1801 default: 1802 ALOGE("Unsupported audio encoder: %d", mAudioEncoder); 1803 return UNKNOWN_ERROR; 1804 } 1805 1806 sp<MediaCodecSource> audioEncoder = createAudioSource(); 1807 if (audioEncoder == NULL) { 1808 return UNKNOWN_ERROR; 1809 } 1810 1811 writer->addSource(audioEncoder); 1812 mAudioEncoderSource = audioEncoder; 1813 return OK; 1814} 1815 1816status_t StagefrightRecorder::setupMPEG4orWEBMRecording() { 1817 mWriter.clear(); 1818 mTotalBitRate = 0; 1819 1820 status_t err = OK; 1821 sp<MediaWriter> writer; 1822 sp<MPEG4Writer> mp4writer; 1823 if (mOutputFormat == OUTPUT_FORMAT_WEBM) { 1824 writer = new WebmWriter(mOutputFd); 1825 } else { 1826 writer = mp4writer = new MPEG4Writer(mOutputFd); 1827 } 1828 1829 if (mVideoSource < VIDEO_SOURCE_LIST_END) { 1830 setDefaultVideoEncoderIfNecessary(); 1831 1832 sp<MediaSource> mediaSource; 1833 err = setupMediaSource(&mediaSource); 1834 if (err != OK) { 1835 return err; 1836 } 1837 1838 sp<MediaCodecSource> encoder; 1839 err = setupVideoEncoder(mediaSource, &encoder); 1840 if (err != OK) { 1841 return err; 1842 } 1843 1844 writer->addSource(encoder); 1845 mVideoEncoderSource = encoder; 1846 mTotalBitRate += mVideoBitRate; 1847 } 1848 1849 if (mOutputFormat != OUTPUT_FORMAT_WEBM) { 1850 // Audio source is added at the end if it exists. 1851 // This help make sure that the "recoding" sound is suppressed for 1852 // camcorder applications in the recorded files. 1853 // TODO Audio source is currently unsupported for webm output; vorbis encoder needed. 1854 // disable audio for time lapse recording 1855 bool disableAudio = mCaptureFpsEnable && mCaptureFps < mFrameRate; 1856 if (!disableAudio && mAudioSource != AUDIO_SOURCE_CNT) { 1857 err = setupAudioEncoder(writer); 1858 if (err != OK) return err; 1859 mTotalBitRate += mAudioBitRate; 1860 } 1861 1862 if (mCaptureFpsEnable) { 1863 mp4writer->setCaptureRate(mCaptureFps); 1864 } 1865 1866 if (mInterleaveDurationUs > 0) { 1867 mp4writer->setInterleaveDuration(mInterleaveDurationUs); 1868 } 1869 if (mLongitudex10000 > -3600000 && mLatitudex10000 > -3600000) { 1870 mp4writer->setGeoData(mLatitudex10000, mLongitudex10000); 1871 } 1872 } 1873 if (mMaxFileDurationUs != 0) { 1874 writer->setMaxFileDuration(mMaxFileDurationUs); 1875 } 1876 if (mMaxFileSizeBytes != 0) { 1877 writer->setMaxFileSize(mMaxFileSizeBytes); 1878 } 1879 if (mVideoSource == VIDEO_SOURCE_DEFAULT 1880 || mVideoSource == VIDEO_SOURCE_CAMERA) { 1881 mStartTimeOffsetMs = mEncoderProfiles->getStartTimeOffsetMs(mCameraId); 1882 } else if (mVideoSource == VIDEO_SOURCE_SURFACE) { 1883 // surface source doesn't need large initial delay 1884 mStartTimeOffsetMs = 200; 1885 } 1886 if (mStartTimeOffsetMs > 0) { 1887 writer->setStartTimeOffsetMs(mStartTimeOffsetMs); 1888 } 1889 1890 writer->setListener(mListener); 1891 mWriter = writer; 1892 return OK; 1893} 1894 1895void StagefrightRecorder::setupMPEG4orWEBMMetaData(sp<MetaData> *meta) { 1896 int64_t startTimeUs = systemTime() / 1000; 1897 (*meta)->setInt64(kKeyTime, startTimeUs); 1898 (*meta)->setInt32(kKeyFileType, mOutputFormat); 1899 (*meta)->setInt32(kKeyBitRate, mTotalBitRate); 1900 if (mMovieTimeScale > 0) { 1901 (*meta)->setInt32(kKeyTimeScale, mMovieTimeScale); 1902 } 1903 if (mOutputFormat != OUTPUT_FORMAT_WEBM) { 1904 (*meta)->setInt32(kKey64BitFileOffset, mUse64BitFileOffset); 1905 if (mTrackEveryTimeDurationUs > 0) { 1906 (*meta)->setInt64(kKeyTrackTimeStatus, mTrackEveryTimeDurationUs); 1907 } 1908 if (mRotationDegrees != 0) { 1909 (*meta)->setInt32(kKeyRotation, mRotationDegrees); 1910 } 1911 } 1912} 1913 1914status_t StagefrightRecorder::pause() { 1915 ALOGV("pause"); 1916 if (!mStarted) { 1917 return INVALID_OPERATION; 1918 } 1919 1920 // Already paused --- no-op. 1921 if (mPauseStartTimeUs != 0) { 1922 return OK; 1923 } 1924 1925 mPauseStartTimeUs = systemTime() / 1000; 1926 sp<MetaData> meta = new MetaData; 1927 meta->setInt64(kKeyTime, mPauseStartTimeUs); 1928 1929 if (mAudioEncoderSource != NULL) { 1930 mAudioEncoderSource->pause(); 1931 } 1932 if (mVideoEncoderSource != NULL) { 1933 mVideoEncoderSource->pause(meta.get()); 1934 } 1935 1936 return OK; 1937} 1938 1939status_t StagefrightRecorder::resume() { 1940 ALOGV("resume"); 1941 if (!mStarted) { 1942 return INVALID_OPERATION; 1943 } 1944 1945 // Not paused --- no-op. 1946 if (mPauseStartTimeUs == 0) { 1947 return OK; 1948 } 1949 1950 int64_t resumeStartTimeUs = systemTime() / 1000; 1951 1952 int64_t bufferStartTimeUs = 0; 1953 bool allSourcesStarted = true; 1954 for (const auto &source : { mAudioEncoderSource, mVideoEncoderSource }) { 1955 if (source == nullptr) { 1956 continue; 1957 } 1958 int64_t timeUs = source->getFirstSampleSystemTimeUs(); 1959 if (timeUs < 0) { 1960 allSourcesStarted = false; 1961 } 1962 if (bufferStartTimeUs < timeUs) { 1963 bufferStartTimeUs = timeUs; 1964 } 1965 } 1966 1967 if (allSourcesStarted) { 1968 if (mPauseStartTimeUs < bufferStartTimeUs) { 1969 mPauseStartTimeUs = bufferStartTimeUs; 1970 } 1971 // 30 ms buffer to avoid timestamp overlap 1972 mTotalPausedDurationUs += resumeStartTimeUs - mPauseStartTimeUs - 30000; 1973 } 1974 double timeOffset = -mTotalPausedDurationUs; 1975 if (mCaptureFpsEnable) { 1976 timeOffset *= mCaptureFps / mFrameRate; 1977 } 1978 sp<MetaData> meta = new MetaData; 1979 meta->setInt64(kKeyTime, resumeStartTimeUs); 1980 for (const auto &source : { mAudioEncoderSource, mVideoEncoderSource }) { 1981 if (source == nullptr) { 1982 continue; 1983 } 1984 source->setInputBufferTimeOffset((int64_t)timeOffset); 1985 source->start(meta.get()); 1986 } 1987 mPauseStartTimeUs = 0; 1988 1989 return OK; 1990} 1991 1992status_t StagefrightRecorder::stop() { 1993 ALOGV("stop"); 1994 Mutex::Autolock autolock(mLock); 1995 status_t err = OK; 1996 1997 if (mCaptureFpsEnable && mCameraSourceTimeLapse != NULL) { 1998 mCameraSourceTimeLapse->startQuickReadReturns(); 1999 mCameraSourceTimeLapse = NULL; 2000 } 2001 2002 if (mVideoEncoderSource != NULL) { 2003 int64_t stopTimeUs = systemTime() / 1000; 2004 sp<MetaData> meta = new MetaData; 2005 err = mVideoEncoderSource->setStopStimeUs(stopTimeUs); 2006 } 2007 2008 if (mWriter != NULL) { 2009 err = mWriter->stop(); 2010 mWriter.clear(); 2011 } 2012 2013 resetMetrics(); 2014 2015 mTotalPausedDurationUs = 0; 2016 mPauseStartTimeUs = 0; 2017 2018 mGraphicBufferProducer.clear(); 2019 mPersistentSurface.clear(); 2020 mAudioEncoderSource.clear(); 2021 mVideoEncoderSource.clear(); 2022 2023 if (mOutputFd >= 0) { 2024 ::close(mOutputFd); 2025 mOutputFd = -1; 2026 } 2027 2028 if (mStarted) { 2029 mStarted = false; 2030 2031 uint32_t params = 0; 2032 if (mAudioSource != AUDIO_SOURCE_CNT) { 2033 params |= IMediaPlayerService::kBatteryDataTrackAudio; 2034 } 2035 if (mVideoSource != VIDEO_SOURCE_LIST_END) { 2036 params |= IMediaPlayerService::kBatteryDataTrackVideo; 2037 } 2038 2039 addBatteryData(params); 2040 } 2041 2042 return err; 2043} 2044 2045status_t StagefrightRecorder::close() { 2046 ALOGV("close"); 2047 stop(); 2048 2049 return OK; 2050} 2051 2052status_t StagefrightRecorder::reset() { 2053 ALOGV("reset"); 2054 stop(); 2055 2056 // No audio or video source by default 2057 mAudioSource = AUDIO_SOURCE_CNT; 2058 mVideoSource = VIDEO_SOURCE_LIST_END; 2059 2060 // Default parameters 2061 mOutputFormat = OUTPUT_FORMAT_THREE_GPP; 2062 mAudioEncoder = AUDIO_ENCODER_AMR_NB; 2063 mVideoEncoder = VIDEO_ENCODER_DEFAULT; 2064 mVideoWidth = 176; 2065 mVideoHeight = 144; 2066 mFrameRate = -1; 2067 mVideoBitRate = 192000; 2068 mSampleRate = 8000; 2069 mAudioChannels = 1; 2070 mAudioBitRate = 12200; 2071 mInterleaveDurationUs = 0; 2072 mIFramesIntervalSec = 1; 2073 mAudioSourceNode = 0; 2074 mUse64BitFileOffset = false; 2075 mMovieTimeScale = -1; 2076 mAudioTimeScale = -1; 2077 mVideoTimeScale = -1; 2078 mCameraId = 0; 2079 mStartTimeOffsetMs = -1; 2080 mVideoEncoderProfile = -1; 2081 mVideoEncoderLevel = -1; 2082 mMaxFileDurationUs = 0; 2083 mMaxFileSizeBytes = 0; 2084 mTrackEveryTimeDurationUs = 0; 2085 mCaptureFpsEnable = false; 2086 mCaptureFps = 0.0; 2087 mTimeBetweenCaptureUs = -1; 2088 mCameraSourceTimeLapse = NULL; 2089 mMetaDataStoredInVideoBuffers = kMetadataBufferTypeInvalid; 2090 mEncoderProfiles = MediaProfiles::getInstance(); 2091 mRotationDegrees = 0; 2092 mLatitudex10000 = -3600000; 2093 mLongitudex10000 = -3600000; 2094 mTotalBitRate = 0; 2095 2096 mOutputFd = -1; 2097 2098 return OK; 2099} 2100 2101status_t StagefrightRecorder::getMaxAmplitude(int *max) { 2102 ALOGV("getMaxAmplitude"); 2103 2104 if (max == NULL) { 2105 ALOGE("Null pointer argument"); 2106 return BAD_VALUE; 2107 } 2108 2109 if (mAudioSourceNode != 0) { 2110 *max = mAudioSourceNode->getMaxAmplitude(); 2111 } else { 2112 *max = 0; 2113 } 2114 2115 return OK; 2116} 2117 2118status_t StagefrightRecorder::getMetrics(Parcel *reply) { 2119 ALOGD("StagefrightRecorder::getMetrics"); 2120 2121 if (reply == NULL) { 2122 ALOGE("Null pointer argument"); 2123 return BAD_VALUE; 2124 } 2125 2126 if (mAnalyticsItem == NULL) { 2127 return UNKNOWN_ERROR; 2128 } 2129 2130 updateMetrics(); 2131 mAnalyticsItem->writeToParcel(reply); 2132 return OK; 2133} 2134 2135status_t StagefrightRecorder::dump( 2136 int fd, const Vector<String16>& args) const { 2137 ALOGV("dump"); 2138 Mutex::Autolock autolock(mLock); 2139 const size_t SIZE = 256; 2140 char buffer[SIZE]; 2141 String8 result; 2142 if (mWriter != 0) { 2143 mWriter->dump(fd, args); 2144 } else { 2145 snprintf(buffer, SIZE, " No file writer\n"); 2146 result.append(buffer); 2147 } 2148 snprintf(buffer, SIZE, " Recorder: %p\n", this); 2149 snprintf(buffer, SIZE, " Output file (fd %d):\n", mOutputFd); 2150 result.append(buffer); 2151 snprintf(buffer, SIZE, " File format: %d\n", mOutputFormat); 2152 result.append(buffer); 2153 snprintf(buffer, SIZE, " Max file size (bytes): %" PRId64 "\n", mMaxFileSizeBytes); 2154 result.append(buffer); 2155 snprintf(buffer, SIZE, " Max file duration (us): %" PRId64 "\n", mMaxFileDurationUs); 2156 result.append(buffer); 2157 snprintf(buffer, SIZE, " File offset length (bits): %d\n", mUse64BitFileOffset? 64: 32); 2158 result.append(buffer); 2159 snprintf(buffer, SIZE, " Interleave duration (us): %d\n", mInterleaveDurationUs); 2160 result.append(buffer); 2161 snprintf(buffer, SIZE, " Progress notification: %" PRId64 " us\n", mTrackEveryTimeDurationUs); 2162 result.append(buffer); 2163 snprintf(buffer, SIZE, " Audio\n"); 2164 result.append(buffer); 2165 snprintf(buffer, SIZE, " Source: %d\n", mAudioSource); 2166 result.append(buffer); 2167 snprintf(buffer, SIZE, " Encoder: %d\n", mAudioEncoder); 2168 result.append(buffer); 2169 snprintf(buffer, SIZE, " Bit rate (bps): %d\n", mAudioBitRate); 2170 result.append(buffer); 2171 snprintf(buffer, SIZE, " Sampling rate (hz): %d\n", mSampleRate); 2172 result.append(buffer); 2173 snprintf(buffer, SIZE, " Number of channels: %d\n", mAudioChannels); 2174 result.append(buffer); 2175 snprintf(buffer, SIZE, " Max amplitude: %d\n", mAudioSourceNode == 0? 0: mAudioSourceNode->getMaxAmplitude()); 2176 result.append(buffer); 2177 snprintf(buffer, SIZE, " Video\n"); 2178 result.append(buffer); 2179 snprintf(buffer, SIZE, " Source: %d\n", mVideoSource); 2180 result.append(buffer); 2181 snprintf(buffer, SIZE, " Camera Id: %d\n", mCameraId); 2182 result.append(buffer); 2183 snprintf(buffer, SIZE, " Start time offset (ms): %d\n", mStartTimeOffsetMs); 2184 result.append(buffer); 2185 snprintf(buffer, SIZE, " Encoder: %d\n", mVideoEncoder); 2186 result.append(buffer); 2187 snprintf(buffer, SIZE, " Encoder profile: %d\n", mVideoEncoderProfile); 2188 result.append(buffer); 2189 snprintf(buffer, SIZE, " Encoder level: %d\n", mVideoEncoderLevel); 2190 result.append(buffer); 2191 snprintf(buffer, SIZE, " I frames interval (s): %d\n", mIFramesIntervalSec); 2192 result.append(buffer); 2193 snprintf(buffer, SIZE, " Frame size (pixels): %dx%d\n", mVideoWidth, mVideoHeight); 2194 result.append(buffer); 2195 snprintf(buffer, SIZE, " Frame rate (fps): %d\n", mFrameRate); 2196 result.append(buffer); 2197 snprintf(buffer, SIZE, " Bit rate (bps): %d\n", mVideoBitRate); 2198 result.append(buffer); 2199 ::write(fd, result.string(), result.size()); 2200 return OK; 2201} 2202} // namespace android 2203