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