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