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