StagefrightRecorder.cpp revision 25d83125cee222993673d3ba261ae1186bcad8c2
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 encMeta->setInt32(kKeyTimeScale, mAudioTimeScale); 679 680 OMXClient client; 681 CHECK_EQ(client.connect(), OK); 682 683 sp<MediaSource> audioEncoder = 684 OMXCodec::Create(client.interface(), encMeta, 685 true /* createEncoder */, audioSource); 686 mAudioSourceNode = audioSource; 687 688 return audioEncoder; 689} 690 691status_t StagefrightRecorder::startAACRecording() { 692 CHECK(mOutputFormat == OUTPUT_FORMAT_AAC_ADIF || 693 mOutputFormat == OUTPUT_FORMAT_AAC_ADTS); 694 695 CHECK(mAudioEncoder == AUDIO_ENCODER_AAC); 696 CHECK(mAudioSource != AUDIO_SOURCE_LIST_END); 697 698 CHECK(0 == "AACWriter is not implemented yet"); 699 700 return OK; 701} 702 703status_t StagefrightRecorder::startAMRRecording() { 704 CHECK(mOutputFormat == OUTPUT_FORMAT_AMR_NB || 705 mOutputFormat == OUTPUT_FORMAT_AMR_WB); 706 707 if (mOutputFormat == OUTPUT_FORMAT_AMR_NB) { 708 if (mAudioEncoder != AUDIO_ENCODER_DEFAULT && 709 mAudioEncoder != AUDIO_ENCODER_AMR_NB) { 710 LOGE("Invalid encoder %d used for AMRNB recording", 711 mAudioEncoder); 712 return BAD_VALUE; 713 } 714 if (mSampleRate != 8000) { 715 LOGE("Invalid sampling rate %d used for AMRNB recording", 716 mSampleRate); 717 return BAD_VALUE; 718 } 719 } else { // mOutputFormat must be OUTPUT_FORMAT_AMR_WB 720 if (mAudioEncoder != AUDIO_ENCODER_AMR_WB) { 721 LOGE("Invlaid encoder %d used for AMRWB recording", 722 mAudioEncoder); 723 return BAD_VALUE; 724 } 725 if (mSampleRate != 16000) { 726 LOGE("Invalid sample rate %d used for AMRWB recording", 727 mSampleRate); 728 return BAD_VALUE; 729 } 730 } 731 if (mAudioChannels != 1) { 732 LOGE("Invalid number of audio channels %d used for amr recording", 733 mAudioChannels); 734 return BAD_VALUE; 735 } 736 737 if (mAudioSource >= AUDIO_SOURCE_LIST_END) { 738 LOGE("Invalid audio source: %d", mAudioSource); 739 return BAD_VALUE; 740 } 741 742 sp<MediaSource> audioEncoder = createAudioSource(); 743 744 if (audioEncoder == NULL) { 745 return UNKNOWN_ERROR; 746 } 747 748 mWriter = new AMRWriter(dup(mOutputFd)); 749 mWriter->addSource(audioEncoder); 750 751 if (mMaxFileDurationUs != 0) { 752 mWriter->setMaxFileDuration(mMaxFileDurationUs); 753 } 754 if (mMaxFileSizeBytes != 0) { 755 mWriter->setMaxFileSize(mMaxFileSizeBytes); 756 } 757 mWriter->setListener(mListener); 758 mWriter->start(); 759 760 return OK; 761} 762 763status_t StagefrightRecorder::startRTPRecording() { 764 CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_RTP_AVP); 765 766 if ((mAudioSource != AUDIO_SOURCE_LIST_END 767 && mVideoSource != VIDEO_SOURCE_LIST_END) 768 || (mAudioSource == AUDIO_SOURCE_LIST_END 769 && mVideoSource == VIDEO_SOURCE_LIST_END)) { 770 // Must have exactly one source. 771 return BAD_VALUE; 772 } 773 774 if (mOutputFd < 0) { 775 return BAD_VALUE; 776 } 777 778 sp<MediaSource> source; 779 780 if (mAudioSource != AUDIO_SOURCE_LIST_END) { 781 source = createAudioSource(); 782 } else { 783 status_t err = setupVideoEncoder(&source); 784 if (err != OK) { 785 return err; 786 } 787 } 788 789 mWriter = new ARTPWriter(dup(mOutputFd)); 790 mWriter->addSource(source); 791 mWriter->setListener(mListener); 792 793 return mWriter->start(); 794} 795 796void StagefrightRecorder::clipVideoFrameRate() { 797 LOGV("clipVideoFrameRate: encoder %d", mVideoEncoder); 798 int minFrameRate = mEncoderProfiles->getVideoEncoderParamByName( 799 "enc.vid.fps.min", mVideoEncoder); 800 int maxFrameRate = mEncoderProfiles->getVideoEncoderParamByName( 801 "enc.vid.fps.max", mVideoEncoder); 802 if (mFrameRate < minFrameRate) { 803 LOGW("Intended video encoding frame rate (%d fps) is too small" 804 " and will be set to (%d fps)", mFrameRate, minFrameRate); 805 mFrameRate = minFrameRate; 806 } else if (mFrameRate > maxFrameRate) { 807 LOGW("Intended video encoding frame rate (%d fps) is too large" 808 " and will be set to (%d fps)", mFrameRate, maxFrameRate); 809 mFrameRate = maxFrameRate; 810 } 811} 812 813void StagefrightRecorder::clipVideoBitRate() { 814 LOGV("clipVideoBitRate: encoder %d", mVideoEncoder); 815 int minBitRate = mEncoderProfiles->getVideoEncoderParamByName( 816 "enc.vid.bps.min", mVideoEncoder); 817 int maxBitRate = mEncoderProfiles->getVideoEncoderParamByName( 818 "enc.vid.bps.max", mVideoEncoder); 819 if (mVideoBitRate < minBitRate) { 820 LOGW("Intended video encoding bit rate (%d bps) is too small" 821 " and will be set to (%d bps)", mVideoBitRate, minBitRate); 822 mVideoBitRate = minBitRate; 823 } else if (mVideoBitRate > maxBitRate) { 824 LOGW("Intended video encoding bit rate (%d bps) is too large" 825 " and will be set to (%d bps)", mVideoBitRate, maxBitRate); 826 mVideoBitRate = maxBitRate; 827 } 828} 829 830void StagefrightRecorder::clipVideoFrameWidth() { 831 LOGV("clipVideoFrameWidth: encoder %d", mVideoEncoder); 832 int minFrameWidth = mEncoderProfiles->getVideoEncoderParamByName( 833 "enc.vid.width.min", mVideoEncoder); 834 int maxFrameWidth = mEncoderProfiles->getVideoEncoderParamByName( 835 "enc.vid.width.max", mVideoEncoder); 836 if (mVideoWidth < minFrameWidth) { 837 LOGW("Intended video encoding frame width (%d) is too small" 838 " and will be set to (%d)", mVideoWidth, minFrameWidth); 839 mVideoWidth = minFrameWidth; 840 } else if (mVideoWidth > maxFrameWidth) { 841 LOGW("Intended video encoding frame width (%d) is too large" 842 " and will be set to (%d)", mVideoWidth, maxFrameWidth); 843 mVideoWidth = maxFrameWidth; 844 } 845} 846 847status_t StagefrightRecorder::setupCameraSource() { 848 clipVideoBitRate(); 849 clipVideoFrameRate(); 850 clipVideoFrameWidth(); 851 clipVideoFrameHeight(); 852 853 int64_t token = IPCThreadState::self()->clearCallingIdentity(); 854 if (mCamera == 0) { 855 mCamera = Camera::connect(mCameraId); 856 if (mCamera == 0) { 857 LOGE("Camera connection could not be established."); 858 return -EBUSY; 859 } 860 mFlags &= ~FLAGS_HOT_CAMERA; 861 mCamera->lock(); 862 } 863 864 // Set the actual video recording frame size 865 CameraParameters params(mCamera->getParameters()); 866 params.setPreviewSize(mVideoWidth, mVideoHeight); 867 params.setPreviewFrameRate(mFrameRate); 868 String8 s = params.flatten(); 869 if (OK != mCamera->setParameters(s)) { 870 LOGE("Could not change settings." 871 " Someone else is using camera %d?", mCameraId); 872 return -EBUSY; 873 } 874 CameraParameters newCameraParams(mCamera->getParameters()); 875 876 // Check on video frame size 877 int frameWidth = 0, frameHeight = 0; 878 newCameraParams.getPreviewSize(&frameWidth, &frameHeight); 879 if (frameWidth < 0 || frameWidth != mVideoWidth || 880 frameHeight < 0 || frameHeight != mVideoHeight) { 881 LOGE("Failed to set the video frame size to %dx%d", 882 mVideoWidth, mVideoHeight); 883 IPCThreadState::self()->restoreCallingIdentity(token); 884 return UNKNOWN_ERROR; 885 } 886 887 // Check on video frame rate 888 int frameRate = newCameraParams.getPreviewFrameRate(); 889 if (frameRate < 0 || (frameRate - mFrameRate) != 0) { 890 LOGE("Failed to set frame rate to %d fps. The actual " 891 "frame rate is %d", mFrameRate, frameRate); 892 } 893 894 // This CHECK is good, since we just passed the lock/unlock 895 // check earlier by calling mCamera->setParameters(). 896 CHECK_EQ(OK, mCamera->setPreviewDisplay(mPreviewSurface)); 897 IPCThreadState::self()->restoreCallingIdentity(token); 898 return OK; 899} 900 901void StagefrightRecorder::clipVideoFrameHeight() { 902 LOGV("clipVideoFrameHeight: encoder %d", mVideoEncoder); 903 int minFrameHeight = mEncoderProfiles->getVideoEncoderParamByName( 904 "enc.vid.height.min", mVideoEncoder); 905 int maxFrameHeight = mEncoderProfiles->getVideoEncoderParamByName( 906 "enc.vid.height.max", mVideoEncoder); 907 if (mVideoHeight < minFrameHeight) { 908 LOGW("Intended video encoding frame height (%d) is too small" 909 " and will be set to (%d)", mVideoHeight, minFrameHeight); 910 mVideoHeight = minFrameHeight; 911 } else if (mVideoHeight > maxFrameHeight) { 912 LOGW("Intended video encoding frame height (%d) is too large" 913 " and will be set to (%d)", mVideoHeight, maxFrameHeight); 914 mVideoHeight = maxFrameHeight; 915 } 916} 917 918status_t StagefrightRecorder::setupVideoEncoder(sp<MediaSource> *source) { 919 source->clear(); 920 921 status_t err = setupCameraSource(); 922 if (err != OK) return err; 923 924 sp<CameraSource> cameraSource = CameraSource::CreateFromCamera(mCamera); 925 CHECK(cameraSource != NULL); 926 927 sp<MetaData> enc_meta = new MetaData; 928 enc_meta->setInt32(kKeyBitRate, mVideoBitRate); 929 enc_meta->setInt32(kKeySampleRate, mFrameRate); 930 931 switch (mVideoEncoder) { 932 case VIDEO_ENCODER_H263: 933 enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263); 934 break; 935 936 case VIDEO_ENCODER_MPEG_4_SP: 937 enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4); 938 break; 939 940 case VIDEO_ENCODER_H264: 941 enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC); 942 break; 943 944 default: 945 CHECK(!"Should not be here, unsupported video encoding."); 946 break; 947 } 948 949 sp<MetaData> meta = cameraSource->getFormat(); 950 951 int32_t width, height, stride, sliceHeight, colorFormat; 952 CHECK(meta->findInt32(kKeyWidth, &width)); 953 CHECK(meta->findInt32(kKeyHeight, &height)); 954 CHECK(meta->findInt32(kKeyStride, &stride)); 955 CHECK(meta->findInt32(kKeySliceHeight, &sliceHeight)); 956 CHECK(meta->findInt32(kKeyColorFormat, &colorFormat)); 957 958 enc_meta->setInt32(kKeyWidth, width); 959 enc_meta->setInt32(kKeyHeight, height); 960 enc_meta->setInt32(kKeyIFramesInterval, mIFramesIntervalSec); 961 enc_meta->setInt32(kKeyStride, stride); 962 enc_meta->setInt32(kKeySliceHeight, sliceHeight); 963 enc_meta->setInt32(kKeyColorFormat, colorFormat); 964 enc_meta->setInt32(kKeyTimeScale, mVideoTimeScale); 965 if (mVideoEncoderProfile != -1) { 966 enc_meta->setInt32(kKeyVideoProfile, mVideoEncoderProfile); 967 } 968 if (mVideoEncoderLevel != -1) { 969 enc_meta->setInt32(kKeyVideoLevel, mVideoEncoderLevel); 970 } 971 972 OMXClient client; 973 CHECK_EQ(client.connect(), OK); 974 975 sp<MediaSource> encoder = OMXCodec::Create( 976 client.interface(), enc_meta, 977 true /* createEncoder */, cameraSource); 978 if (encoder == NULL) { 979 return UNKNOWN_ERROR; 980 } 981 982 *source = encoder; 983 984 return OK; 985} 986 987status_t StagefrightRecorder::setupAudioEncoder(const sp<MediaWriter>& writer) { 988 sp<MediaSource> audioEncoder; 989 switch(mAudioEncoder) { 990 case AUDIO_ENCODER_AMR_NB: 991 case AUDIO_ENCODER_AMR_WB: 992 case AUDIO_ENCODER_AAC: 993 audioEncoder = createAudioSource(); 994 break; 995 default: 996 LOGE("Unsupported audio encoder: %d", mAudioEncoder); 997 return UNKNOWN_ERROR; 998 } 999 1000 if (audioEncoder == NULL) { 1001 return UNKNOWN_ERROR; 1002 } 1003 1004 writer->addSource(audioEncoder); 1005 return OK; 1006} 1007 1008status_t StagefrightRecorder::startMPEG4Recording() { 1009 int32_t totalBitRate = 0; 1010 status_t err = OK; 1011 sp<MediaWriter> writer = new MPEG4Writer(dup(mOutputFd)); 1012 1013 // Add audio source first if it exists 1014 if (mAudioSource != AUDIO_SOURCE_LIST_END) { 1015 err = setupAudioEncoder(writer); 1016 if (err != OK) return err; 1017 totalBitRate += mAudioBitRate; 1018 } 1019 if (mVideoSource == VIDEO_SOURCE_DEFAULT 1020 || mVideoSource == VIDEO_SOURCE_CAMERA) { 1021 sp<MediaSource> encoder; 1022 err = setupVideoEncoder(&encoder); 1023 if (err != OK) return err; 1024 writer->addSource(encoder); 1025 totalBitRate += mVideoBitRate; 1026 } 1027 1028 if (mInterleaveDurationUs > 0) { 1029 reinterpret_cast<MPEG4Writer *>(writer.get())-> 1030 setInterleaveDuration(mInterleaveDurationUs); 1031 } 1032 1033 if (mMaxFileDurationUs != 0) { 1034 writer->setMaxFileDuration(mMaxFileDurationUs); 1035 } 1036 if (mMaxFileSizeBytes != 0) { 1037 writer->setMaxFileSize(mMaxFileSizeBytes); 1038 } 1039 sp<MetaData> meta = new MetaData; 1040 meta->setInt64(kKeyTime, systemTime() / 1000); 1041 meta->setInt32(kKeyFileType, mOutputFormat); 1042 meta->setInt32(kKeyBitRate, totalBitRate); 1043 meta->setInt32(kKey64BitFileOffset, mUse64BitFileOffset); 1044 meta->setInt32(kKeyTimeScale, mMovieTimeScale); 1045 if (mTrackEveryTimeDurationUs > 0) { 1046 meta->setInt64(kKeyTrackTimeStatus, mTrackEveryTimeDurationUs); 1047 } 1048 writer->setListener(mListener); 1049 mWriter = writer; 1050 return mWriter->start(meta.get()); 1051} 1052 1053status_t StagefrightRecorder::pause() { 1054 LOGV("pause"); 1055 if (mWriter == NULL) { 1056 return UNKNOWN_ERROR; 1057 } 1058 mWriter->pause(); 1059 return OK; 1060} 1061 1062status_t StagefrightRecorder::stop() { 1063 LOGV("stop"); 1064 if (mWriter != NULL) { 1065 mWriter->stop(); 1066 mWriter.clear(); 1067 } 1068 1069 if (mCamera != 0) { 1070 LOGV("Disconnect camera"); 1071 int64_t token = IPCThreadState::self()->clearCallingIdentity(); 1072 if ((mFlags & FLAGS_HOT_CAMERA) == 0) { 1073 LOGV("Camera was cold when we started, stopping preview"); 1074 mCamera->stopPreview(); 1075 } 1076 mCamera->unlock(); 1077 mCamera.clear(); 1078 IPCThreadState::self()->restoreCallingIdentity(token); 1079 mFlags = 0; 1080 } 1081 1082 if (mOutputFd >= 0) { 1083 ::close(mOutputFd); 1084 mOutputFd = -1; 1085 } 1086 1087 return OK; 1088} 1089 1090status_t StagefrightRecorder::close() { 1091 LOGV("close"); 1092 stop(); 1093 1094 return OK; 1095} 1096 1097status_t StagefrightRecorder::reset() { 1098 LOGV("reset"); 1099 stop(); 1100 1101 // No audio or video source by default 1102 mAudioSource = AUDIO_SOURCE_LIST_END; 1103 mVideoSource = VIDEO_SOURCE_LIST_END; 1104 1105 // Default parameters 1106 mOutputFormat = OUTPUT_FORMAT_THREE_GPP; 1107 mAudioEncoder = AUDIO_ENCODER_AMR_NB; 1108 mVideoEncoder = VIDEO_ENCODER_H263; 1109 mVideoWidth = 176; 1110 mVideoHeight = 144; 1111 mFrameRate = 20; 1112 mVideoBitRate = 192000; 1113 mSampleRate = 8000; 1114 mAudioChannels = 1; 1115 mAudioBitRate = 12200; 1116 mInterleaveDurationUs = 0; 1117 mIFramesIntervalSec = 1; 1118 mAudioSourceNode = 0; 1119 mUse64BitFileOffset = false; 1120 mMovieTimeScale = 1000; 1121 mAudioTimeScale = 1000; 1122 mVideoTimeScale = 1000; 1123 mCameraId = 0; 1124 mVideoEncoderProfile = -1; 1125 mVideoEncoderLevel = -1; 1126 mMaxFileDurationUs = 0; 1127 mMaxFileSizeBytes = 0; 1128 mTrackEveryTimeDurationUs = 0; 1129 mEncoderProfiles = MediaProfiles::getInstance(); 1130 1131 mOutputFd = -1; 1132 mFlags = 0; 1133 1134 return OK; 1135} 1136 1137status_t StagefrightRecorder::getMaxAmplitude(int *max) { 1138 LOGV("getMaxAmplitude"); 1139 1140 if (max == NULL) { 1141 LOGE("Null pointer argument"); 1142 return BAD_VALUE; 1143 } 1144 1145 if (mAudioSourceNode != 0) { 1146 *max = mAudioSourceNode->getMaxAmplitude(); 1147 } else { 1148 *max = 0; 1149 } 1150 1151 return OK; 1152} 1153 1154status_t StagefrightRecorder::dump(int fd, const Vector<String16>& args) const { 1155 const size_t SIZE = 256; 1156 char buffer[SIZE]; 1157 String8 result; 1158 snprintf(buffer, SIZE, " Recorder: %p", this); 1159 snprintf(buffer, SIZE, " Output file (fd %d):\n", mOutputFd); 1160 result.append(buffer); 1161 snprintf(buffer, SIZE, " File format: %d\n", mOutputFormat); 1162 result.append(buffer); 1163 snprintf(buffer, SIZE, " Max file size (bytes): %lld\n", mMaxFileSizeBytes); 1164 result.append(buffer); 1165 snprintf(buffer, SIZE, " Max file duration (us): %lld\n", mMaxFileDurationUs); 1166 result.append(buffer); 1167 snprintf(buffer, SIZE, " File offset length (bits): %d\n", mUse64BitFileOffset? 64: 32); 1168 result.append(buffer); 1169 snprintf(buffer, SIZE, " Interleave duration (us): %d\n", mInterleaveDurationUs); 1170 result.append(buffer); 1171 snprintf(buffer, SIZE, " Progress notification: %lld us\n", mTrackEveryTimeDurationUs); 1172 result.append(buffer); 1173 snprintf(buffer, SIZE, " Audio\n"); 1174 result.append(buffer); 1175 snprintf(buffer, SIZE, " Source: %d\n", mAudioSource); 1176 result.append(buffer); 1177 snprintf(buffer, SIZE, " Encoder: %d\n", mAudioEncoder); 1178 result.append(buffer); 1179 snprintf(buffer, SIZE, " Bit rate (bps): %d\n", mAudioBitRate); 1180 result.append(buffer); 1181 snprintf(buffer, SIZE, " Sampling rate (hz): %d\n", mSampleRate); 1182 result.append(buffer); 1183 snprintf(buffer, SIZE, " Number of channels: %d\n", mAudioChannels); 1184 result.append(buffer); 1185 snprintf(buffer, SIZE, " Max amplitude: %d\n", mAudioSourceNode == 0? 0: mAudioSourceNode->getMaxAmplitude()); 1186 result.append(buffer); 1187 snprintf(buffer, SIZE, " Video\n"); 1188 result.append(buffer); 1189 snprintf(buffer, SIZE, " Source: %d\n", mVideoSource); 1190 result.append(buffer); 1191 snprintf(buffer, SIZE, " Camera Id: %d\n", mCameraId); 1192 result.append(buffer); 1193 snprintf(buffer, SIZE, " Camera flags: %d\n", mFlags); 1194 result.append(buffer); 1195 snprintf(buffer, SIZE, " Encoder: %d\n", mVideoEncoder); 1196 result.append(buffer); 1197 snprintf(buffer, SIZE, " Encoder profile: %d\n", mVideoEncoderProfile); 1198 result.append(buffer); 1199 snprintf(buffer, SIZE, " Encoder level: %d\n", mVideoEncoderLevel); 1200 result.append(buffer); 1201 snprintf(buffer, SIZE, " I frames interval (s): %d\n", mIFramesIntervalSec); 1202 result.append(buffer); 1203 snprintf(buffer, SIZE, " Frame size (pixels): %dx%d\n", mVideoWidth, mVideoHeight); 1204 result.append(buffer); 1205 snprintf(buffer, SIZE, " Frame rate (fps): %d\n", mFrameRate); 1206 result.append(buffer); 1207 snprintf(buffer, SIZE, " Bit rate (bps): %d\n", mVideoBitRate); 1208 result.append(buffer); 1209 ::write(fd, result.string(), result.size()); 1210 return OK; 1211} 1212} // namespace android 1213