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     mMaxFileDurationUs = timeUs;
    365     return OK;
    366 }
    367 
    368 status_t StagefrightRecorder::setParamMaxFileSizeBytes(int64_t bytes) {
    369     LOGV("setParamMaxFileSizeBytes: %lld bytes", bytes);
    370     if (bytes <= 1024) {  // XXX: 1 kB
    371         LOGE("Max file size is too small: %lld bytes", bytes);
    372         return BAD_VALUE;
    373     }
    374     mMaxFileSizeBytes = bytes;
    375     return OK;
    376 }
    377 
    378 status_t StagefrightRecorder::setParamInterleaveDuration(int32_t durationUs) {
    379     LOGV("setParamInterleaveDuration: %d", durationUs);
    380     if (durationUs <= 500000) {           //  500 ms
    381         // If interleave duration is too small, it is very inefficient to do
    382         // interleaving since the metadata overhead will count for a significant
    383         // portion of the saved contents
    384         LOGE("Audio/video interleave duration is too small: %d us", durationUs);
    385         return BAD_VALUE;
    386     } else if (durationUs >= 10000000) {  // 10 seconds
    387         // If interleaving duration is too large, it can cause the recording
    388         // session to use too much memory since we have to save the output
    389         // data before we write them out
    390         LOGE("Audio/video interleave duration is too large: %d us", durationUs);
    391         return BAD_VALUE;
    392     }
    393     mInterleaveDurationUs = durationUs;
    394     return OK;
    395 }
    396 
    397 // If seconds <  0, only the first frame is I frame, and rest are all P frames
    398 // If seconds == 0, all frames are encoded as I frames. No P frames
    399 // If seconds >  0, it is the time spacing (seconds) between 2 neighboring I frames
    400 status_t StagefrightRecorder::setParamVideoIFramesInterval(int32_t seconds) {
    401     LOGV("setParamVideoIFramesInterval: %d seconds", seconds);
    402     mIFramesIntervalSec = seconds;
    403     return OK;
    404 }
    405 
    406 status_t StagefrightRecorder::setParam64BitFileOffset(bool use64Bit) {
    407     LOGV("setParam64BitFileOffset: %s",
    408         use64Bit? "use 64 bit file offset": "use 32 bit file offset");
    409     mUse64BitFileOffset = use64Bit;
    410     return OK;
    411 }
    412 
    413 status_t StagefrightRecorder::setParamVideoCameraId(int32_t cameraId) {
    414     LOGV("setParamVideoCameraId: %d", cameraId);
    415     if (cameraId < 0) {
    416         return BAD_VALUE;
    417     }
    418     mCameraId = cameraId;
    419     return OK;
    420 }
    421 
    422 status_t StagefrightRecorder::setParamTrackTimeStatus(int64_t timeDurationUs) {
    423     LOGV("setParamTrackTimeStatus: %lld", timeDurationUs);
    424     if (timeDurationUs < 20000) {  // Infeasible if shorter than 20 ms?
    425         LOGE("Tracking time duration too short: %lld us", timeDurationUs);
    426         return BAD_VALUE;
    427     }
    428     mTrackEveryTimeDurationUs = timeDurationUs;
    429     return OK;
    430 }
    431 
    432 status_t StagefrightRecorder::setParamVideoEncoderProfile(int32_t profile) {
    433     LOGV("setParamVideoEncoderProfile: %d", profile);
    434 
    435     // Additional check will be done later when we load the encoder.
    436     // For now, we are accepting values defined in OpenMAX IL.
    437     mVideoEncoderProfile = profile;
    438     return OK;
    439 }
    440 
    441 status_t StagefrightRecorder::setParamVideoEncoderLevel(int32_t level) {
    442     LOGV("setParamVideoEncoderLevel: %d", level);
    443 
    444     // Additional check will be done later when we load the encoder.
    445     // For now, we are accepting values defined in OpenMAX IL.
    446     mVideoEncoderLevel = level;
    447     return OK;
    448 }
    449 
    450 status_t StagefrightRecorder::setParamMovieTimeScale(int32_t timeScale) {
    451     LOGV("setParamMovieTimeScale: %d", timeScale);
    452 
    453     // The range is set to be the same as the audio's time scale range
    454     // since audio's time scale has a wider range.
    455     if (timeScale < 600 || timeScale > 96000) {
    456         LOGE("Time scale (%d) for movie is out of range [600, 96000]", timeScale);
    457         return BAD_VALUE;
    458     }
    459     mMovieTimeScale = timeScale;
    460     return OK;
    461 }
    462 
    463 status_t StagefrightRecorder::setParamVideoTimeScale(int32_t timeScale) {
    464     LOGV("setParamVideoTimeScale: %d", timeScale);
    465 
    466     // 60000 is chosen to make sure that each video frame from a 60-fps
    467     // video has 1000 ticks.
    468     if (timeScale < 600 || timeScale > 60000) {
    469         LOGE("Time scale (%d) for video is out of range [600, 60000]", timeScale);
    470         return BAD_VALUE;
    471     }
    472     mVideoTimeScale = timeScale;
    473     return OK;
    474 }
    475 
    476 status_t StagefrightRecorder::setParamAudioTimeScale(int32_t timeScale) {
    477     LOGV("setParamAudioTimeScale: %d", timeScale);
    478 
    479     // 96000 Hz is the highest sampling rate support in AAC.
    480     if (timeScale < 600 || timeScale > 96000) {
    481         LOGE("Time scale (%d) for audio is out of range [600, 96000]", timeScale);
    482         return BAD_VALUE;
    483     }
    484     mAudioTimeScale = timeScale;
    485     return OK;
    486 }
    487 
    488 status_t StagefrightRecorder::setParameter(
    489         const String8 &key, const String8 &value) {
    490     LOGV("setParameter: key (%s) => value (%s)", key.string(), value.string());
    491     if (key == "max-duration") {
    492         int64_t max_duration_ms;
    493         if (safe_strtoi64(value.string(), &max_duration_ms)) {
    494             return setParamMaxFileDurationUs(1000LL * max_duration_ms);
    495         }
    496     } else if (key == "max-filesize") {
    497         int64_t max_filesize_bytes;
    498         if (safe_strtoi64(value.string(), &max_filesize_bytes)) {
    499             return setParamMaxFileSizeBytes(max_filesize_bytes);
    500         }
    501     } else if (key == "interleave-duration-us") {
    502         int32_t durationUs;
    503         if (safe_strtoi32(value.string(), &durationUs)) {
    504             return setParamInterleaveDuration(durationUs);
    505         }
    506     } else if (key == "param-movie-time-scale") {
    507         int32_t timeScale;
    508         if (safe_strtoi32(value.string(), &timeScale)) {
    509             return setParamMovieTimeScale(timeScale);
    510         }
    511     } else if (key == "param-use-64bit-offset") {
    512         int32_t use64BitOffset;
    513         if (safe_strtoi32(value.string(), &use64BitOffset)) {
    514             return setParam64BitFileOffset(use64BitOffset != 0);
    515         }
    516     } else if (key == "param-track-time-status") {
    517         int64_t timeDurationUs;
    518         if (safe_strtoi64(value.string(), &timeDurationUs)) {
    519             return setParamTrackTimeStatus(timeDurationUs);
    520         }
    521     } else if (key == "audio-param-sampling-rate") {
    522         int32_t sampling_rate;
    523         if (safe_strtoi32(value.string(), &sampling_rate)) {
    524             return setParamAudioSamplingRate(sampling_rate);
    525         }
    526     } else if (key == "audio-param-number-of-channels") {
    527         int32_t number_of_channels;
    528         if (safe_strtoi32(value.string(), &number_of_channels)) {
    529             return setParamAudioNumberOfChannels(number_of_channels);
    530         }
    531     } else if (key == "audio-param-encoding-bitrate") {
    532         int32_t audio_bitrate;
    533         if (safe_strtoi32(value.string(), &audio_bitrate)) {
    534             return setParamAudioEncodingBitRate(audio_bitrate);
    535         }
    536     } else if (key == "audio-param-time-scale") {
    537         int32_t timeScale;
    538         if (safe_strtoi32(value.string(), &timeScale)) {
    539             return setParamAudioTimeScale(timeScale);
    540         }
    541     } else if (key == "video-param-encoding-bitrate") {
    542         int32_t video_bitrate;
    543         if (safe_strtoi32(value.string(), &video_bitrate)) {
    544             return setParamVideoEncodingBitRate(video_bitrate);
    545         }
    546     } else if (key == "video-param-rotation-angle-degrees") {
    547         int32_t degrees;
    548         if (safe_strtoi32(value.string(), &degrees)) {
    549             return setParamVideoRotation(degrees);
    550         }
    551     } else if (key == "video-param-i-frames-interval") {
    552         int32_t seconds;
    553         if (safe_strtoi32(value.string(), &seconds)) {
    554             return setParamVideoIFramesInterval(seconds);
    555         }
    556     } else if (key == "video-param-encoder-profile") {
    557         int32_t profile;
    558         if (safe_strtoi32(value.string(), &profile)) {
    559             return setParamVideoEncoderProfile(profile);
    560         }
    561     } else if (key == "video-param-encoder-level") {
    562         int32_t level;
    563         if (safe_strtoi32(value.string(), &level)) {
    564             return setParamVideoEncoderLevel(level);
    565         }
    566     } else if (key == "video-param-camera-id") {
    567         int32_t cameraId;
    568         if (safe_strtoi32(value.string(), &cameraId)) {
    569             return setParamVideoCameraId(cameraId);
    570         }
    571     } else if (key == "video-param-time-scale") {
    572         int32_t timeScale;
    573         if (safe_strtoi32(value.string(), &timeScale)) {
    574             return setParamVideoTimeScale(timeScale);
    575         }
    576     } else {
    577         LOGE("setParameter: failed to find key %s", key.string());
    578     }
    579     return BAD_VALUE;
    580 }
    581 
    582 status_t StagefrightRecorder::setParameters(const String8 &params) {
    583     LOGV("setParameters: %s", params.string());
    584     const char *cparams = params.string();
    585     const char *key_start = cparams;
    586     for (;;) {
    587         const char *equal_pos = strchr(key_start, '=');
    588         if (equal_pos == NULL) {
    589             LOGE("Parameters %s miss a value", cparams);
    590             return BAD_VALUE;
    591         }
    592         String8 key(key_start, equal_pos - key_start);
    593         TrimString(&key);
    594         if (key.length() == 0) {
    595             LOGE("Parameters %s contains an empty key", cparams);
    596             return BAD_VALUE;
    597         }
    598         const char *value_start = equal_pos + 1;
    599         const char *semicolon_pos = strchr(value_start, ';');
    600         String8 value;
    601         if (semicolon_pos == NULL) {
    602             value.setTo(value_start);
    603         } else {
    604             value.setTo(value_start, semicolon_pos - value_start);
    605         }
    606         if (setParameter(key, value) != OK) {
    607             return BAD_VALUE;
    608         }
    609         if (semicolon_pos == NULL) {
    610             break;  // Reaches the end
    611         }
    612         key_start = semicolon_pos + 1;
    613     }
    614     return OK;
    615 }
    616 
    617 status_t StagefrightRecorder::setListener(const sp<IMediaRecorderClient> &listener) {
    618     mListener = listener;
    619 
    620     return OK;
    621 }
    622 
    623 status_t StagefrightRecorder::prepare() {
    624     return OK;
    625 }
    626 
    627 status_t StagefrightRecorder::start() {
    628     CHECK(mOutputFd >= 0);
    629 
    630     if (mWriter != NULL) {
    631         LOGE("File writer is not avaialble");
    632         return UNKNOWN_ERROR;
    633     }
    634 
    635     switch (mOutputFormat) {
    636         case OUTPUT_FORMAT_DEFAULT:
    637         case OUTPUT_FORMAT_THREE_GPP:
    638         case OUTPUT_FORMAT_MPEG_4:
    639             return startMPEG4Recording();
    640 
    641         case OUTPUT_FORMAT_AMR_NB:
    642         case OUTPUT_FORMAT_AMR_WB:
    643             return startAMRRecording();
    644 
    645         case OUTPUT_FORMAT_AAC_ADIF:
    646         case OUTPUT_FORMAT_AAC_ADTS:
    647             return startAACRecording();
    648 
    649         case OUTPUT_FORMAT_RTP_AVP:
    650             return startRTPRecording();
    651 
    652         case OUTPUT_FORMAT_MPEG2TS:
    653             return startMPEG2TSRecording();
    654 
    655         default:
    656             LOGE("Unsupported output file format: %d", mOutputFormat);
    657             return UNKNOWN_ERROR;
    658     }
    659 }
    660 
    661 sp<MediaSource> StagefrightRecorder::createAudioSource() {
    662     sp<AudioSource> audioSource =
    663         new AudioSource(
    664                 mAudioSource,
    665                 mSampleRate,
    666                 mAudioChannels);
    667 
    668     status_t err = audioSource->initCheck();
    669 
    670     if (err != OK) {
    671         LOGE("audio source is not initialized");
    672         return NULL;
    673     }
    674 
    675     sp<MetaData> encMeta = new MetaData;
    676     const char *mime;
    677     switch (mAudioEncoder) {
    678         case AUDIO_ENCODER_AMR_NB:
    679         case AUDIO_ENCODER_DEFAULT:
    680             mime = MEDIA_MIMETYPE_AUDIO_AMR_NB;
    681             break;
    682         case AUDIO_ENCODER_AMR_WB:
    683             mime = MEDIA_MIMETYPE_AUDIO_AMR_WB;
    684             break;
    685         case AUDIO_ENCODER_AAC:
    686             mime = MEDIA_MIMETYPE_AUDIO_AAC;
    687             break;
    688         default:
    689             LOGE("Unknown audio encoder: %d", mAudioEncoder);
    690             return NULL;
    691     }
    692     encMeta->setCString(kKeyMIMEType, mime);
    693 
    694     int32_t maxInputSize;
    695     CHECK(audioSource->getFormat()->findInt32(
    696                 kKeyMaxInputSize, &maxInputSize));
    697 
    698     encMeta->setInt32(kKeyMaxInputSize, maxInputSize);
    699     encMeta->setInt32(kKeyChannelCount, mAudioChannels);
    700     encMeta->setInt32(kKeySampleRate, mSampleRate);
    701     encMeta->setInt32(kKeyBitRate, mAudioBitRate);
    702     if (mAudioTimeScale > 0) {
    703         encMeta->setInt32(kKeyTimeScale, mAudioTimeScale);
    704     }
    705 
    706     OMXClient client;
    707     CHECK_EQ(client.connect(), OK);
    708 
    709     sp<MediaSource> audioEncoder =
    710         OMXCodec::Create(client.interface(), encMeta,
    711                          true /* createEncoder */, audioSource);
    712     mAudioSourceNode = audioSource;
    713 
    714     return audioEncoder;
    715 }
    716 
    717 status_t StagefrightRecorder::startAACRecording() {
    718     CHECK(mOutputFormat == OUTPUT_FORMAT_AAC_ADIF ||
    719           mOutputFormat == OUTPUT_FORMAT_AAC_ADTS);
    720 
    721     CHECK(mAudioEncoder == AUDIO_ENCODER_AAC);
    722     CHECK(mAudioSource != AUDIO_SOURCE_LIST_END);
    723 
    724     CHECK(0 == "AACWriter is not implemented yet");
    725 
    726     return OK;
    727 }
    728 
    729 status_t StagefrightRecorder::startAMRRecording() {
    730     CHECK(mOutputFormat == OUTPUT_FORMAT_AMR_NB ||
    731           mOutputFormat == OUTPUT_FORMAT_AMR_WB);
    732 
    733     if (mOutputFormat == OUTPUT_FORMAT_AMR_NB) {
    734         if (mAudioEncoder != AUDIO_ENCODER_DEFAULT &&
    735             mAudioEncoder != AUDIO_ENCODER_AMR_NB) {
    736             LOGE("Invalid encoder %d used for AMRNB recording",
    737                     mAudioEncoder);
    738             return BAD_VALUE;
    739         }
    740         if (mSampleRate != 8000) {
    741             LOGE("Invalid sampling rate %d used for AMRNB recording",
    742                     mSampleRate);
    743             return BAD_VALUE;
    744         }
    745     } else {  // mOutputFormat must be OUTPUT_FORMAT_AMR_WB
    746         if (mAudioEncoder != AUDIO_ENCODER_AMR_WB) {
    747             LOGE("Invlaid encoder %d used for AMRWB recording",
    748                     mAudioEncoder);
    749             return BAD_VALUE;
    750         }
    751         if (mSampleRate != 16000) {
    752             LOGE("Invalid sample rate %d used for AMRWB recording",
    753                     mSampleRate);
    754             return BAD_VALUE;
    755         }
    756     }
    757     if (mAudioChannels != 1) {
    758         LOGE("Invalid number of audio channels %d used for amr recording",
    759                 mAudioChannels);
    760         return BAD_VALUE;
    761     }
    762 
    763     if (mAudioSource >= AUDIO_SOURCE_LIST_END) {
    764         LOGE("Invalid audio source: %d", mAudioSource);
    765         return BAD_VALUE;
    766     }
    767 
    768     sp<MediaSource> audioEncoder = createAudioSource();
    769 
    770     if (audioEncoder == NULL) {
    771         return UNKNOWN_ERROR;
    772     }
    773 
    774     mWriter = new AMRWriter(dup(mOutputFd));
    775     mWriter->addSource(audioEncoder);
    776 
    777     if (mMaxFileDurationUs != 0) {
    778         mWriter->setMaxFileDuration(mMaxFileDurationUs);
    779     }
    780     if (mMaxFileSizeBytes != 0) {
    781         mWriter->setMaxFileSize(mMaxFileSizeBytes);
    782     }
    783     mWriter->setListener(mListener);
    784     mWriter->start();
    785 
    786     return OK;
    787 }
    788 
    789 status_t StagefrightRecorder::startRTPRecording() {
    790     CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_RTP_AVP);
    791 
    792     if ((mAudioSource != AUDIO_SOURCE_LIST_END
    793                 && mVideoSource != VIDEO_SOURCE_LIST_END)
    794             || (mAudioSource == AUDIO_SOURCE_LIST_END
    795                 && mVideoSource == VIDEO_SOURCE_LIST_END)) {
    796         // Must have exactly one source.
    797         return BAD_VALUE;
    798     }
    799 
    800     if (mOutputFd < 0) {
    801         return BAD_VALUE;
    802     }
    803 
    804     sp<MediaSource> source;
    805 
    806     if (mAudioSource != AUDIO_SOURCE_LIST_END) {
    807         source = createAudioSource();
    808     } else {
    809         status_t err = setupVideoEncoder(&source);
    810         if (err != OK) {
    811             return err;
    812         }
    813     }
    814 
    815     mWriter = new ARTPWriter(dup(mOutputFd));
    816     mWriter->addSource(source);
    817     mWriter->setListener(mListener);
    818 
    819     return mWriter->start();
    820 }
    821 
    822 status_t StagefrightRecorder::startMPEG2TSRecording() {
    823     CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_MPEG2TS);
    824 
    825     sp<MediaWriter> writer = new MPEG2TSWriter(dup(mOutputFd));
    826 
    827     if (mAudioSource != AUDIO_SOURCE_LIST_END) {
    828         if (mAudioEncoder != AUDIO_ENCODER_AAC) {
    829             return ERROR_UNSUPPORTED;
    830         }
    831 
    832         status_t err = setupAudioEncoder(writer);
    833 
    834         if (err != OK) {
    835             return err;
    836         }
    837     }
    838 
    839     if (mVideoSource == VIDEO_SOURCE_DEFAULT
    840             || mVideoSource == VIDEO_SOURCE_CAMERA) {
    841         if (mVideoEncoder != VIDEO_ENCODER_H264) {
    842             return ERROR_UNSUPPORTED;
    843         }
    844 
    845         sp<MediaSource> encoder;
    846         status_t err = setupVideoEncoder(&encoder);
    847 
    848         if (err != OK) {
    849             return err;
    850         }
    851 
    852         writer->addSource(encoder);
    853     }
    854 
    855     if (mMaxFileDurationUs != 0) {
    856         writer->setMaxFileDuration(mMaxFileDurationUs);
    857     }
    858 
    859     if (mMaxFileSizeBytes != 0) {
    860         writer->setMaxFileSize(mMaxFileSizeBytes);
    861     }
    862 
    863     mWriter = writer;
    864 
    865     return mWriter->start();
    866 }
    867 
    868 void StagefrightRecorder::clipVideoFrameRate() {
    869     LOGV("clipVideoFrameRate: encoder %d", mVideoEncoder);
    870     int minFrameRate = mEncoderProfiles->getVideoEncoderParamByName(
    871                         "enc.vid.fps.min", mVideoEncoder);
    872     int maxFrameRate = mEncoderProfiles->getVideoEncoderParamByName(
    873                         "enc.vid.fps.max", mVideoEncoder);
    874     if (mFrameRate < minFrameRate) {
    875         LOGW("Intended video encoding frame rate (%d fps) is too small"
    876              " and will be set to (%d fps)", mFrameRate, minFrameRate);
    877         mFrameRate = minFrameRate;
    878     } else if (mFrameRate > maxFrameRate) {
    879         LOGW("Intended video encoding frame rate (%d fps) is too large"
    880              " and will be set to (%d fps)", mFrameRate, maxFrameRate);
    881         mFrameRate = maxFrameRate;
    882     }
    883 }
    884 
    885 void StagefrightRecorder::clipVideoBitRate() {
    886     LOGV("clipVideoBitRate: encoder %d", mVideoEncoder);
    887     int minBitRate = mEncoderProfiles->getVideoEncoderParamByName(
    888                         "enc.vid.bps.min", mVideoEncoder);
    889     int maxBitRate = mEncoderProfiles->getVideoEncoderParamByName(
    890                         "enc.vid.bps.max", mVideoEncoder);
    891     if (mVideoBitRate < minBitRate) {
    892         LOGW("Intended video encoding bit rate (%d bps) is too small"
    893              " and will be set to (%d bps)", mVideoBitRate, minBitRate);
    894         mVideoBitRate = minBitRate;
    895     } else if (mVideoBitRate > maxBitRate) {
    896         LOGW("Intended video encoding bit rate (%d bps) is too large"
    897              " and will be set to (%d bps)", mVideoBitRate, maxBitRate);
    898         mVideoBitRate = maxBitRate;
    899     }
    900 }
    901 
    902 void StagefrightRecorder::clipVideoFrameWidth() {
    903     LOGV("clipVideoFrameWidth: encoder %d", mVideoEncoder);
    904     int minFrameWidth = mEncoderProfiles->getVideoEncoderParamByName(
    905                         "enc.vid.width.min", mVideoEncoder);
    906     int maxFrameWidth = mEncoderProfiles->getVideoEncoderParamByName(
    907                         "enc.vid.width.max", mVideoEncoder);
    908     if (mVideoWidth < minFrameWidth) {
    909         LOGW("Intended video encoding frame width (%d) is too small"
    910              " and will be set to (%d)", mVideoWidth, minFrameWidth);
    911         mVideoWidth = minFrameWidth;
    912     } else if (mVideoWidth > maxFrameWidth) {
    913         LOGW("Intended video encoding frame width (%d) is too large"
    914              " and will be set to (%d)", mVideoWidth, maxFrameWidth);
    915         mVideoWidth = maxFrameWidth;
    916     }
    917 }
    918 
    919 status_t StagefrightRecorder::setupCameraSource() {
    920     clipVideoBitRate();
    921     clipVideoFrameRate();
    922     clipVideoFrameWidth();
    923     clipVideoFrameHeight();
    924 
    925     int64_t token = IPCThreadState::self()->clearCallingIdentity();
    926     if (mCamera == 0) {
    927         mCamera = Camera::connect(mCameraId);
    928         if (mCamera == 0) {
    929             LOGE("Camera connection could not be established.");
    930             return -EBUSY;
    931         }
    932         mFlags &= ~FLAGS_HOT_CAMERA;
    933         mCamera->lock();
    934     }
    935 
    936     // Set the actual video recording frame size
    937     CameraParameters params(mCamera->getParameters());
    938     params.setPreviewSize(mVideoWidth, mVideoHeight);
    939     params.setPreviewFrameRate(mFrameRate);
    940     String8 s = params.flatten();
    941     if (OK != mCamera->setParameters(s)) {
    942         LOGE("Could not change settings."
    943              " Someone else is using camera %d?", mCameraId);
    944         return -EBUSY;
    945     }
    946     CameraParameters newCameraParams(mCamera->getParameters());
    947 
    948     // Check on video frame size
    949     int frameWidth = 0, frameHeight = 0;
    950     newCameraParams.getPreviewSize(&frameWidth, &frameHeight);
    951     if (frameWidth  < 0 || frameWidth  != mVideoWidth ||
    952         frameHeight < 0 || frameHeight != mVideoHeight) {
    953         LOGE("Failed to set the video frame size to %dx%d",
    954                 mVideoWidth, mVideoHeight);
    955         IPCThreadState::self()->restoreCallingIdentity(token);
    956         return UNKNOWN_ERROR;
    957     }
    958 
    959     // Check on video frame rate
    960     int frameRate = newCameraParams.getPreviewFrameRate();
    961     if (frameRate < 0 || (frameRate - mFrameRate) != 0) {
    962         LOGE("Failed to set frame rate to %d fps. The actual "
    963              "frame rate is %d", mFrameRate, frameRate);
    964     }
    965 
    966     // This CHECK is good, since we just passed the lock/unlock
    967     // check earlier by calling mCamera->setParameters().
    968     CHECK_EQ(OK, mCamera->setPreviewDisplay(mPreviewSurface));
    969     IPCThreadState::self()->restoreCallingIdentity(token);
    970     return OK;
    971 }
    972 
    973 void StagefrightRecorder::clipVideoFrameHeight() {
    974     LOGV("clipVideoFrameHeight: encoder %d", mVideoEncoder);
    975     int minFrameHeight = mEncoderProfiles->getVideoEncoderParamByName(
    976                         "enc.vid.height.min", mVideoEncoder);
    977     int maxFrameHeight = mEncoderProfiles->getVideoEncoderParamByName(
    978                         "enc.vid.height.max", mVideoEncoder);
    979     if (mVideoHeight < minFrameHeight) {
    980         LOGW("Intended video encoding frame height (%d) is too small"
    981              " and will be set to (%d)", mVideoHeight, minFrameHeight);
    982         mVideoHeight = minFrameHeight;
    983     } else if (mVideoHeight > maxFrameHeight) {
    984         LOGW("Intended video encoding frame height (%d) is too large"
    985              " and will be set to (%d)", mVideoHeight, maxFrameHeight);
    986         mVideoHeight = maxFrameHeight;
    987     }
    988 }
    989 
    990 status_t StagefrightRecorder::setupVideoEncoder(sp<MediaSource> *source) {
    991     source->clear();
    992 
    993     status_t err = setupCameraSource();
    994     if (err != OK) return err;
    995 
    996     sp<CameraSource> cameraSource = CameraSource::CreateFromCamera(mCamera);
    997     CHECK(cameraSource != NULL);
    998 
    999     sp<MetaData> enc_meta = new MetaData;
   1000     enc_meta->setInt32(kKeyBitRate, mVideoBitRate);
   1001     enc_meta->setInt32(kKeySampleRate, mFrameRate);
   1002 
   1003     switch (mVideoEncoder) {
   1004         case VIDEO_ENCODER_H263:
   1005             enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
   1006             break;
   1007 
   1008         case VIDEO_ENCODER_MPEG_4_SP:
   1009             enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
   1010             break;
   1011 
   1012         case VIDEO_ENCODER_H264:
   1013             enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
   1014             break;
   1015 
   1016         default:
   1017             CHECK(!"Should not be here, unsupported video encoding.");
   1018             break;
   1019     }
   1020 
   1021     sp<MetaData> meta = cameraSource->getFormat();
   1022 
   1023     int32_t width, height, stride, sliceHeight, colorFormat;
   1024     CHECK(meta->findInt32(kKeyWidth, &width));
   1025     CHECK(meta->findInt32(kKeyHeight, &height));
   1026     CHECK(meta->findInt32(kKeyStride, &stride));
   1027     CHECK(meta->findInt32(kKeySliceHeight, &sliceHeight));
   1028     CHECK(meta->findInt32(kKeyColorFormat, &colorFormat));
   1029 
   1030     enc_meta->setInt32(kKeyWidth, width);
   1031     enc_meta->setInt32(kKeyHeight, height);
   1032     enc_meta->setInt32(kKeyIFramesInterval, mIFramesIntervalSec);
   1033     enc_meta->setInt32(kKeyStride, stride);
   1034     enc_meta->setInt32(kKeySliceHeight, sliceHeight);
   1035     enc_meta->setInt32(kKeyColorFormat, colorFormat);
   1036     if (mVideoTimeScale > 0) {
   1037         enc_meta->setInt32(kKeyTimeScale, mVideoTimeScale);
   1038     }
   1039     if (mVideoEncoderProfile != -1) {
   1040         enc_meta->setInt32(kKeyVideoProfile, mVideoEncoderProfile);
   1041     }
   1042     if (mVideoEncoderLevel != -1) {
   1043         enc_meta->setInt32(kKeyVideoLevel, mVideoEncoderLevel);
   1044     }
   1045 
   1046     OMXClient client;
   1047     CHECK_EQ(client.connect(), OK);
   1048 
   1049     sp<MediaSource> encoder = OMXCodec::Create(
   1050             client.interface(), enc_meta,
   1051             true /* createEncoder */, cameraSource);
   1052     if (encoder == NULL) {
   1053         return UNKNOWN_ERROR;
   1054     }
   1055 
   1056     *source = encoder;
   1057 
   1058     return OK;
   1059 }
   1060 
   1061 status_t StagefrightRecorder::setupAudioEncoder(const sp<MediaWriter>& writer) {
   1062     sp<MediaSource> audioEncoder;
   1063     switch(mAudioEncoder) {
   1064         case AUDIO_ENCODER_AMR_NB:
   1065         case AUDIO_ENCODER_AMR_WB:
   1066         case AUDIO_ENCODER_AAC:
   1067             audioEncoder = createAudioSource();
   1068             break;
   1069         default:
   1070             LOGE("Unsupported audio encoder: %d", mAudioEncoder);
   1071             return UNKNOWN_ERROR;
   1072     }
   1073 
   1074     if (audioEncoder == NULL) {
   1075         return UNKNOWN_ERROR;
   1076     }
   1077 
   1078     writer->addSource(audioEncoder);
   1079     return OK;
   1080 }
   1081 
   1082 status_t StagefrightRecorder::startMPEG4Recording() {
   1083     int32_t totalBitRate = 0;
   1084     status_t err = OK;
   1085     sp<MediaWriter> writer = new MPEG4Writer(dup(mOutputFd));
   1086 
   1087     // Add audio source first if it exists
   1088     if (mAudioSource != AUDIO_SOURCE_LIST_END) {
   1089         err = setupAudioEncoder(writer);
   1090         if (err != OK) return err;
   1091         totalBitRate += mAudioBitRate;
   1092     }
   1093     if (mVideoSource == VIDEO_SOURCE_DEFAULT
   1094             || mVideoSource == VIDEO_SOURCE_CAMERA) {
   1095         sp<MediaSource> encoder;
   1096         err = setupVideoEncoder(&encoder);
   1097         if (err != OK) return err;
   1098         writer->addSource(encoder);
   1099         totalBitRate += mVideoBitRate;
   1100     }
   1101 
   1102     if (mInterleaveDurationUs > 0) {
   1103         reinterpret_cast<MPEG4Writer *>(writer.get())->
   1104             setInterleaveDuration(mInterleaveDurationUs);
   1105     }
   1106 
   1107     if (mMaxFileDurationUs != 0) {
   1108         writer->setMaxFileDuration(mMaxFileDurationUs);
   1109     }
   1110     if (mMaxFileSizeBytes != 0) {
   1111         writer->setMaxFileSize(mMaxFileSizeBytes);
   1112     }
   1113     sp<MetaData> meta = new MetaData;
   1114     meta->setInt64(kKeyTime, systemTime() / 1000);
   1115     meta->setInt32(kKeyFileType, mOutputFormat);
   1116     meta->setInt32(kKeyBitRate, totalBitRate);
   1117     meta->setInt32(kKey64BitFileOffset, mUse64BitFileOffset);
   1118     if (mMovieTimeScale > 0) {
   1119         meta->setInt32(kKeyTimeScale, mMovieTimeScale);
   1120     }
   1121     if (mTrackEveryTimeDurationUs > 0) {
   1122         meta->setInt64(kKeyTrackTimeStatus, mTrackEveryTimeDurationUs);
   1123     }
   1124     if (mRotationDegrees != 0) {
   1125         meta->setInt32(kKeyRotationDegree, mRotationDegrees);
   1126     }
   1127     writer->setListener(mListener);
   1128     mWriter = writer;
   1129     return mWriter->start(meta.get());
   1130 }
   1131 
   1132 status_t StagefrightRecorder::pause() {
   1133     LOGV("pause");
   1134     if (mWriter == NULL) {
   1135         return UNKNOWN_ERROR;
   1136     }
   1137     mWriter->pause();
   1138     return OK;
   1139 }
   1140 
   1141 status_t StagefrightRecorder::stop() {
   1142     LOGV("stop");
   1143     status_t err = OK;
   1144     if (mWriter != NULL) {
   1145         err = mWriter->stop();
   1146         mWriter.clear();
   1147     }
   1148 
   1149     if (mCamera != 0) {
   1150         LOGV("Disconnect camera");
   1151         int64_t token = IPCThreadState::self()->clearCallingIdentity();
   1152         if ((mFlags & FLAGS_HOT_CAMERA) == 0) {
   1153             LOGV("Camera was cold when we started, stopping preview");
   1154             mCamera->stopPreview();
   1155         }
   1156         mCamera->unlock();
   1157         mCamera.clear();
   1158         IPCThreadState::self()->restoreCallingIdentity(token);
   1159         mFlags = 0;
   1160     }
   1161 
   1162     if (mOutputFd >= 0) {
   1163         ::close(mOutputFd);
   1164         mOutputFd = -1;
   1165     }
   1166 
   1167     return err;
   1168 }
   1169 
   1170 status_t StagefrightRecorder::close() {
   1171     LOGV("close");
   1172     stop();
   1173 
   1174     return OK;
   1175 }
   1176 
   1177 status_t StagefrightRecorder::reset() {
   1178     LOGV("reset");
   1179     stop();
   1180 
   1181     // No audio or video source by default
   1182     mAudioSource = AUDIO_SOURCE_LIST_END;
   1183     mVideoSource = VIDEO_SOURCE_LIST_END;
   1184 
   1185     // Default parameters
   1186     mOutputFormat  = OUTPUT_FORMAT_THREE_GPP;
   1187     mAudioEncoder  = AUDIO_ENCODER_AMR_NB;
   1188     mVideoEncoder  = VIDEO_ENCODER_H263;
   1189     mVideoWidth    = 176;
   1190     mVideoHeight   = 144;
   1191     mFrameRate     = 20;
   1192     mVideoBitRate  = 192000;
   1193     mSampleRate    = 8000;
   1194     mAudioChannels = 1;
   1195     mAudioBitRate  = 12200;
   1196     mInterleaveDurationUs = 0;
   1197     mIFramesIntervalSec = 1;
   1198     mAudioSourceNode = 0;
   1199     mUse64BitFileOffset = false;
   1200     mMovieTimeScale  = -1;
   1201     mAudioTimeScale  = -1;
   1202     mVideoTimeScale  = -1;
   1203     mCameraId        = 0;
   1204     mVideoEncoderProfile = -1;
   1205     mVideoEncoderLevel   = -1;
   1206     mMaxFileDurationUs = 0;
   1207     mMaxFileSizeBytes = 0;
   1208     mTrackEveryTimeDurationUs = 0;
   1209     mRotationDegrees = 0;
   1210     mEncoderProfiles = MediaProfiles::getInstance();
   1211 
   1212     mOutputFd = -1;
   1213     mFlags = 0;
   1214 
   1215     return OK;
   1216 }
   1217 
   1218 status_t StagefrightRecorder::getMaxAmplitude(int *max) {
   1219     LOGV("getMaxAmplitude");
   1220 
   1221     if (max == NULL) {
   1222         LOGE("Null pointer argument");
   1223         return BAD_VALUE;
   1224     }
   1225 
   1226     if (mAudioSourceNode != 0) {
   1227         *max = mAudioSourceNode->getMaxAmplitude();
   1228     } else {
   1229         *max = 0;
   1230     }
   1231 
   1232     return OK;
   1233 }
   1234 
   1235 status_t StagefrightRecorder::dump(
   1236         int fd, const Vector<String16>& args) const {
   1237     LOGV("dump");
   1238     const size_t SIZE = 256;
   1239     char buffer[SIZE];
   1240     String8 result;
   1241     if (mWriter != 0) {
   1242         mWriter->dump(fd, args);
   1243     } else {
   1244         snprintf(buffer, SIZE, "   No file writer\n");
   1245         result.append(buffer);
   1246     }
   1247     snprintf(buffer, SIZE, "   Recorder: %p\n", this);
   1248     snprintf(buffer, SIZE, "   Output file (fd %d):\n", mOutputFd);
   1249     result.append(buffer);
   1250     snprintf(buffer, SIZE, "     File format: %d\n", mOutputFormat);
   1251     result.append(buffer);
   1252     snprintf(buffer, SIZE, "     Max file size (bytes): %lld\n", mMaxFileSizeBytes);
   1253     result.append(buffer);
   1254     snprintf(buffer, SIZE, "     Max file duration (us): %lld\n", mMaxFileDurationUs);
   1255     result.append(buffer);
   1256     snprintf(buffer, SIZE, "     File offset length (bits): %d\n", mUse64BitFileOffset? 64: 32);
   1257     result.append(buffer);
   1258     snprintf(buffer, SIZE, "     Interleave duration (us): %d\n", mInterleaveDurationUs);
   1259     result.append(buffer);
   1260     snprintf(buffer, SIZE, "     Progress notification: %lld us\n", mTrackEveryTimeDurationUs);
   1261     result.append(buffer);
   1262     snprintf(buffer, SIZE, "   Audio\n");
   1263     result.append(buffer);
   1264     snprintf(buffer, SIZE, "     Source: %d\n", mAudioSource);
   1265     result.append(buffer);
   1266     snprintf(buffer, SIZE, "     Encoder: %d\n", mAudioEncoder);
   1267     result.append(buffer);
   1268     snprintf(buffer, SIZE, "     Bit rate (bps): %d\n", mAudioBitRate);
   1269     result.append(buffer);
   1270     snprintf(buffer, SIZE, "     Sampling rate (hz): %d\n", mSampleRate);
   1271     result.append(buffer);
   1272     snprintf(buffer, SIZE, "     Number of channels: %d\n", mAudioChannels);
   1273     result.append(buffer);
   1274     snprintf(buffer, SIZE, "     Max amplitude: %d\n", mAudioSourceNode == 0? 0: mAudioSourceNode->getMaxAmplitude());
   1275     result.append(buffer);
   1276     snprintf(buffer, SIZE, "   Video\n");
   1277     result.append(buffer);
   1278     snprintf(buffer, SIZE, "     Source: %d\n", mVideoSource);
   1279     result.append(buffer);
   1280     snprintf(buffer, SIZE, "     Camera Id: %d\n", mCameraId);
   1281     result.append(buffer);
   1282     snprintf(buffer, SIZE, "     Camera flags: %d\n", mFlags);
   1283     result.append(buffer);
   1284     snprintf(buffer, SIZE, "     Encoder: %d\n", mVideoEncoder);
   1285     result.append(buffer);
   1286     snprintf(buffer, SIZE, "     Encoder profile: %d\n", mVideoEncoderProfile);
   1287     result.append(buffer);
   1288     snprintf(buffer, SIZE, "     Encoder level: %d\n", mVideoEncoderLevel);
   1289     result.append(buffer);
   1290     snprintf(buffer, SIZE, "     I frames interval (s): %d\n", mIFramesIntervalSec);
   1291     result.append(buffer);
   1292     snprintf(buffer, SIZE, "     Frame size (pixels): %dx%d\n", mVideoWidth, mVideoHeight);
   1293     result.append(buffer);
   1294     snprintf(buffer, SIZE, "     Frame rate (fps): %d\n", mFrameRate);
   1295     result.append(buffer);
   1296     snprintf(buffer, SIZE, "     Bit rate (bps): %d\n", mVideoBitRate);
   1297     result.append(buffer);
   1298     ::write(fd, result.string(), result.size());
   1299     return OK;
   1300 }
   1301 }  // namespace android
   1302