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