StagefrightRecorder.cpp revision afb43f76821e6a63e17e6484289a40430ada6978
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 <inttypes.h>
20#include <utils/Log.h>
21
22#include "WebmWriter.h"
23#include "StagefrightRecorder.h"
24
25#include <algorithm>
26
27#include <android/hardware/ICamera.h>
28
29#include <binder/IPCThreadState.h>
30#include <binder/IServiceManager.h>
31
32#include <media/IMediaPlayerService.h>
33#include <media/MediaAnalyticsItem.h>
34#include <media/stagefright/foundation/ABuffer.h>
35#include <media/stagefright/foundation/ADebug.h>
36#include <media/stagefright/foundation/AMessage.h>
37#include <media/stagefright/foundation/ALooper.h>
38#include <media/stagefright/ACodec.h>
39#include <media/stagefright/AudioSource.h>
40#include <media/stagefright/AMRWriter.h>
41#include <media/stagefright/AACWriter.h>
42#include <media/stagefright/CameraSource.h>
43#include <media/stagefright/CameraSourceTimeLapse.h>
44#include <media/stagefright/MPEG2TSWriter.h>
45#include <media/stagefright/MPEG4Writer.h>
46#include <media/stagefright/MediaDefs.h>
47#include <media/stagefright/MetaData.h>
48#include <media/stagefright/MediaCodecSource.h>
49#include <media/stagefright/PersistentSurface.h>
50#include <media/MediaProfiles.h>
51#include <camera/CameraParameters.h>
52
53#include <utils/Errors.h>
54#include <sys/types.h>
55#include <ctype.h>
56#include <unistd.h>
57
58#include <system/audio.h>
59
60#include "ARTPWriter.h"
61
62namespace android {
63
64static const float kTypicalDisplayRefreshingRate = 60.f;
65// display refresh rate drops on battery saver
66static const float kMinTypicalDisplayRefreshingRate = kTypicalDisplayRefreshingRate / 2;
67static const int kMaxNumVideoTemporalLayers = 8;
68
69// key for media statistics
70static const char *kKeyRecorder = "recorder";
71// attrs for media statistics
72static const char *kRecorderHeight = "android.media.mediarecorder.height";
73static const char *kRecorderWidth = "android.media.mediarecorder.width";
74static const char *kRecorderFrameRate = "android.media.mediarecorder.frame-rate";
75static const char *kRecorderVideoBitrate = "android.media.mediarecorder.video-bitrate";
76static const char *kRecorderAudioSampleRate = "android.media.mediarecorder.audio-samplerate";
77static const char *kRecorderAudioChannels = "android.media.mediarecorder.audio-channels";
78static const char *kRecorderAudioBitrate = "android.media.mediarecorder.audio-bitrate";
79static const char *kRecorderVideoIframeInterval = "android.media.mediarecorder.video-iframe-interval";
80static const char *kRecorderMovieTimescale = "android.media.mediarecorder.movie-timescale";
81static const char *kRecorderAudioTimescale = "android.media.mediarecorder.audio-timescale";
82static const char *kRecorderVideoTimescale = "android.media.mediarecorder.video-timescale";
83static const char *kRecorderVideoProfile = "android.media.mediarecorder.video-encoder-profile";
84static const char *kRecorderVideoLevel = "android.media.mediarecorder.video-encoder-level";
85static const char *kRecorderCaptureFpsEnable = "android.media.mediarecorder.capture-fpsenable";
86static const char *kRecorderCaptureFps = "android.media.mediarecorder.capture-fps";
87static const char *kRecorderRotation = "android.media.mediarecorder.rotation";
88
89// To collect the encoder usage for the battery app
90static void addBatteryData(uint32_t params) {
91    sp<IBinder> binder =
92        defaultServiceManager()->getService(String16("media.player"));
93    sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
94    CHECK(service.get() != NULL);
95
96    service->addBatteryData(params);
97}
98
99
100StagefrightRecorder::StagefrightRecorder(const String16 &opPackageName)
101    : MediaRecorderBase(opPackageName),
102      mWriter(NULL),
103      mOutputFd(-1),
104      mAudioSource(AUDIO_SOURCE_CNT),
105      mVideoSource(VIDEO_SOURCE_LIST_END),
106      mStarted(false) {
107
108    ALOGV("Constructor");
109
110    mAnalyticsDirty = false;
111    reset();
112}
113
114StagefrightRecorder::~StagefrightRecorder() {
115    ALOGV("Destructor");
116    stop();
117
118    if (mLooper != NULL) {
119        mLooper->stop();
120    }
121
122    // log the current record, provided it has some information worth recording
123    if (mAnalyticsDirty && mAnalyticsItem != NULL) {
124        updateMetrics();
125        if (mAnalyticsItem->count() > 0) {
126            mAnalyticsItem->setFinalized(true);
127            mAnalyticsItem->selfrecord();
128        }
129        delete mAnalyticsItem;
130        mAnalyticsItem = NULL;
131    }
132}
133
134void StagefrightRecorder::updateMetrics() {
135    ALOGV("updateMetrics");
136
137    // we'll populate the values from the raw fields.
138    // (NOT going to populate as we go through the various set* ops)
139
140    // TBD mOutputFormat  = OUTPUT_FORMAT_THREE_GPP;
141    // TBD mAudioEncoder  = AUDIO_ENCODER_AMR_NB;
142    // TBD mVideoEncoder  = VIDEO_ENCODER_DEFAULT;
143    mAnalyticsItem->setInt32(kRecorderHeight, mVideoHeight);
144    mAnalyticsItem->setInt32(kRecorderWidth, mVideoWidth);
145    mAnalyticsItem->setInt32(kRecorderFrameRate, mFrameRate);
146    mAnalyticsItem->setInt32(kRecorderVideoBitrate, mVideoBitRate);
147    mAnalyticsItem->setInt32(kRecorderAudioSampleRate, mSampleRate);
148    mAnalyticsItem->setInt32(kRecorderAudioChannels, mAudioChannels);
149    mAnalyticsItem->setInt32(kRecorderAudioBitrate, mAudioBitRate);
150    // TBD mInterleaveDurationUs = 0;
151    mAnalyticsItem->setInt32(kRecorderVideoIframeInterval, mIFramesIntervalSec);
152    // TBD mAudioSourceNode = 0;
153    // TBD mUse64BitFileOffset = false;
154    mAnalyticsItem->setInt32(kRecorderMovieTimescale, mMovieTimeScale);
155    mAnalyticsItem->setInt32(kRecorderAudioTimescale, mAudioTimeScale);
156    mAnalyticsItem->setInt32(kRecorderVideoTimescale, mVideoTimeScale);
157    // TBD mCameraId        = 0;
158    // TBD mStartTimeOffsetMs = -1;
159    mAnalyticsItem->setInt32(kRecorderVideoProfile, mVideoEncoderProfile);
160    mAnalyticsItem->setInt32(kRecorderVideoLevel, mVideoEncoderLevel);
161    // TBD mMaxFileDurationUs = 0;
162    // TBD mMaxFileSizeBytes = 0;
163    // TBD mTrackEveryTimeDurationUs = 0;
164    mAnalyticsItem->setInt32(kRecorderCaptureFpsEnable, mCaptureFpsEnable);
165    mAnalyticsItem->setDouble(kRecorderCaptureFps, mCaptureFps);
166    // TBD mTimeBetweenCaptureUs = -1;
167    // TBD mCameraSourceTimeLapse = NULL;
168    // TBD mMetaDataStoredInVideoBuffers = kMetadataBufferTypeInvalid;
169    // TBD mEncoderProfiles = MediaProfiles::getInstance();
170    mAnalyticsItem->setInt32(kRecorderRotation, mRotationDegrees);
171    // PII mLatitudex10000 = -3600000;
172    // PII mLongitudex10000 = -3600000;
173    // TBD mTotalBitRate = 0;
174
175    // TBD: some duration information (capture, paused)
176    //
177
178}
179
180void StagefrightRecorder::resetMetrics() {
181    ALOGV("resetMetrics");
182    // flush anything we have, restart the record
183    if (mAnalyticsDirty && mAnalyticsItem != NULL) {
184        updateMetrics();
185        if (mAnalyticsItem->count() > 0) {
186            mAnalyticsItem->setFinalized(true);
187            mAnalyticsItem->selfrecord();
188        }
189        delete mAnalyticsItem;
190        mAnalyticsItem = NULL;
191    }
192    mAnalyticsItem = new MediaAnalyticsItem(kKeyRecorder);
193    (void) mAnalyticsItem->generateSessionID();
194    mAnalyticsDirty = false;
195}
196
197status_t StagefrightRecorder::init() {
198    ALOGV("init");
199
200    mLooper = new ALooper;
201    mLooper->setName("recorder_looper");
202    mLooper->start();
203
204    return OK;
205}
206
207// The client side of mediaserver asks it to creat a SurfaceMediaSource
208// and return a interface reference. The client side will use that
209// while encoding GL Frames
210sp<IGraphicBufferProducer> StagefrightRecorder::querySurfaceMediaSource() const {
211    ALOGV("Get SurfaceMediaSource");
212    return mGraphicBufferProducer;
213}
214
215status_t StagefrightRecorder::setAudioSource(audio_source_t as) {
216    ALOGV("setAudioSource: %d", as);
217    if (as < AUDIO_SOURCE_DEFAULT ||
218        (as >= AUDIO_SOURCE_CNT && as != AUDIO_SOURCE_FM_TUNER)) {
219        ALOGE("Invalid audio source: %d", as);
220        return BAD_VALUE;
221    }
222
223    if (as == AUDIO_SOURCE_DEFAULT) {
224        mAudioSource = AUDIO_SOURCE_MIC;
225    } else {
226        mAudioSource = as;
227    }
228
229    return OK;
230}
231
232status_t StagefrightRecorder::setVideoSource(video_source vs) {
233    ALOGV("setVideoSource: %d", vs);
234    if (vs < VIDEO_SOURCE_DEFAULT ||
235        vs >= VIDEO_SOURCE_LIST_END) {
236        ALOGE("Invalid video source: %d", vs);
237        return BAD_VALUE;
238    }
239
240    if (vs == VIDEO_SOURCE_DEFAULT) {
241        mVideoSource = VIDEO_SOURCE_CAMERA;
242    } else {
243        mVideoSource = vs;
244    }
245
246    return OK;
247}
248
249status_t StagefrightRecorder::setOutputFormat(output_format of) {
250    ALOGV("setOutputFormat: %d", of);
251    if (of < OUTPUT_FORMAT_DEFAULT ||
252        of >= OUTPUT_FORMAT_LIST_END) {
253        ALOGE("Invalid output format: %d", of);
254        return BAD_VALUE;
255    }
256
257    if (of == OUTPUT_FORMAT_DEFAULT) {
258        mOutputFormat = OUTPUT_FORMAT_THREE_GPP;
259    } else {
260        mOutputFormat = of;
261    }
262
263    return OK;
264}
265
266status_t StagefrightRecorder::setAudioEncoder(audio_encoder ae) {
267    ALOGV("setAudioEncoder: %d", ae);
268    if (ae < AUDIO_ENCODER_DEFAULT ||
269        ae >= AUDIO_ENCODER_LIST_END) {
270        ALOGE("Invalid audio encoder: %d", ae);
271        return BAD_VALUE;
272    }
273
274    if (ae == AUDIO_ENCODER_DEFAULT) {
275        mAudioEncoder = AUDIO_ENCODER_AMR_NB;
276    } else {
277        mAudioEncoder = ae;
278    }
279
280    return OK;
281}
282
283status_t StagefrightRecorder::setVideoEncoder(video_encoder ve) {
284    ALOGV("setVideoEncoder: %d", ve);
285    if (ve < VIDEO_ENCODER_DEFAULT ||
286        ve >= VIDEO_ENCODER_LIST_END) {
287        ALOGE("Invalid video encoder: %d", ve);
288        return BAD_VALUE;
289    }
290
291    mVideoEncoder = ve;
292
293    return OK;
294}
295
296status_t StagefrightRecorder::setVideoSize(int width, int height) {
297    ALOGV("setVideoSize: %dx%d", width, height);
298    if (width <= 0 || height <= 0) {
299        ALOGE("Invalid video size: %dx%d", width, height);
300        return BAD_VALUE;
301    }
302
303    // Additional check on the dimension will be performed later
304    mVideoWidth = width;
305    mVideoHeight = height;
306
307    return OK;
308}
309
310status_t StagefrightRecorder::setVideoFrameRate(int frames_per_second) {
311    ALOGV("setVideoFrameRate: %d", frames_per_second);
312    if ((frames_per_second <= 0 && frames_per_second != -1) ||
313        frames_per_second > kMaxHighSpeedFps) {
314        ALOGE("Invalid video frame rate: %d", frames_per_second);
315        return BAD_VALUE;
316    }
317
318    // Additional check on the frame rate will be performed later
319    mFrameRate = frames_per_second;
320
321    return OK;
322}
323
324status_t StagefrightRecorder::setCamera(const sp<hardware::ICamera> &camera,
325                                        const sp<ICameraRecordingProxy> &proxy) {
326    ALOGV("setCamera");
327    if (camera == 0) {
328        ALOGE("camera is NULL");
329        return BAD_VALUE;
330    }
331    if (proxy == 0) {
332        ALOGE("camera proxy is NULL");
333        return BAD_VALUE;
334    }
335
336    mCamera = camera;
337    mCameraProxy = proxy;
338    return OK;
339}
340
341status_t StagefrightRecorder::setPreviewSurface(const sp<IGraphicBufferProducer> &surface) {
342    ALOGV("setPreviewSurface: %p", surface.get());
343    mPreviewSurface = surface;
344
345    return OK;
346}
347
348status_t StagefrightRecorder::setInputSurface(
349        const sp<PersistentSurface>& surface) {
350    mPersistentSurface = surface;
351
352    return OK;
353}
354
355status_t StagefrightRecorder::setOutputFile(int fd) {
356    ALOGV("setOutputFile: %d", fd);
357
358    if (fd < 0) {
359        ALOGE("Invalid file descriptor: %d", fd);
360        return -EBADF;
361    }
362
363    // start with a clean, empty file
364    ftruncate(fd, 0);
365
366    if (mOutputFd >= 0) {
367        ::close(mOutputFd);
368    }
369    mOutputFd = dup(fd);
370
371    return OK;
372}
373
374status_t StagefrightRecorder::setNextOutputFile(int fd) {
375    Mutex::Autolock autolock(mLock);
376    // Only support MPEG4
377    if (mOutputFormat != OUTPUT_FORMAT_MPEG_4) {
378        ALOGE("Only MP4 file format supports setting next output file");
379        return INVALID_OPERATION;
380    }
381    ALOGV("setNextOutputFile: %d", fd);
382
383    if (fd < 0) {
384        ALOGE("Invalid file descriptor: %d", fd);
385        return -EBADF;
386    }
387
388    // start with a clean, empty file
389    ftruncate(fd, 0);
390    int nextFd = dup(fd);
391    if (mWriter == NULL) {
392        ALOGE("setNextOutputFile failed. Writer has been freed");
393        return INVALID_OPERATION;
394    }
395    return mWriter->setNextFd(nextFd);
396}
397
398// Attempt to parse an float literal optionally surrounded by whitespace,
399// returns true on success, false otherwise.
400static bool safe_strtof(const char *s, float *val) {
401    char *end;
402
403    // It is lame, but according to man page, we have to set errno to 0
404    // before calling strtof().
405    errno = 0;
406    *val = strtof(s, &end);
407
408    if (end == s || errno == ERANGE) {
409        return false;
410    }
411
412    // Skip trailing whitespace
413    while (isspace(*end)) {
414        ++end;
415    }
416
417    // For a successful return, the string must contain nothing but a valid
418    // float literal optionally surrounded by whitespace.
419
420    return *end == '\0';
421}
422
423// Attempt to parse an int64 literal optionally surrounded by whitespace,
424// returns true on success, false otherwise.
425static bool safe_strtoi64(const char *s, int64_t *val) {
426    char *end;
427
428    // It is lame, but according to man page, we have to set errno to 0
429    // before calling strtoll().
430    errno = 0;
431    *val = strtoll(s, &end, 10);
432
433    if (end == s || errno == ERANGE) {
434        return false;
435    }
436
437    // Skip trailing whitespace
438    while (isspace(*end)) {
439        ++end;
440    }
441
442    // For a successful return, the string must contain nothing but a valid
443    // int64 literal optionally surrounded by whitespace.
444
445    return *end == '\0';
446}
447
448// Return true if the value is in [0, 0x007FFFFFFF]
449static bool safe_strtoi32(const char *s, int32_t *val) {
450    int64_t temp;
451    if (safe_strtoi64(s, &temp)) {
452        if (temp >= 0 && temp <= 0x007FFFFFFF) {
453            *val = static_cast<int32_t>(temp);
454            return true;
455        }
456    }
457    return false;
458}
459
460// Trim both leading and trailing whitespace from the given string.
461static void TrimString(String8 *s) {
462    size_t num_bytes = s->bytes();
463    const char *data = s->string();
464
465    size_t leading_space = 0;
466    while (leading_space < num_bytes && isspace(data[leading_space])) {
467        ++leading_space;
468    }
469
470    size_t i = num_bytes;
471    while (i > leading_space && isspace(data[i - 1])) {
472        --i;
473    }
474
475    s->setTo(String8(&data[leading_space], i - leading_space));
476}
477
478status_t StagefrightRecorder::setParamAudioSamplingRate(int32_t sampleRate) {
479    ALOGV("setParamAudioSamplingRate: %d", sampleRate);
480    if (sampleRate <= 0) {
481        ALOGE("Invalid audio sampling rate: %d", sampleRate);
482        return BAD_VALUE;
483    }
484
485    // Additional check on the sample rate will be performed later.
486    mSampleRate = sampleRate;
487
488    return OK;
489}
490
491status_t StagefrightRecorder::setParamAudioNumberOfChannels(int32_t channels) {
492    ALOGV("setParamAudioNumberOfChannels: %d", channels);
493    if (channels <= 0 || channels >= 3) {
494        ALOGE("Invalid number of audio channels: %d", channels);
495        return BAD_VALUE;
496    }
497
498    // Additional check on the number of channels will be performed later.
499    mAudioChannels = channels;
500
501    return OK;
502}
503
504status_t StagefrightRecorder::setParamAudioEncodingBitRate(int32_t bitRate) {
505    ALOGV("setParamAudioEncodingBitRate: %d", bitRate);
506    if (bitRate <= 0) {
507        ALOGE("Invalid audio encoding bit rate: %d", bitRate);
508        return BAD_VALUE;
509    }
510
511    // The target bit rate may not be exactly the same as the requested.
512    // It depends on many factors, such as rate control, and the bit rate
513    // range that a specific encoder supports. The mismatch between the
514    // the target and requested bit rate will NOT be treated as an error.
515    mAudioBitRate = bitRate;
516    return OK;
517}
518
519status_t StagefrightRecorder::setParamVideoEncodingBitRate(int32_t bitRate) {
520    ALOGV("setParamVideoEncodingBitRate: %d", bitRate);
521    if (bitRate <= 0) {
522        ALOGE("Invalid video encoding bit rate: %d", bitRate);
523        return BAD_VALUE;
524    }
525
526    // The target bit rate may not be exactly the same as the requested.
527    // It depends on many factors, such as rate control, and the bit rate
528    // range that a specific encoder supports. The mismatch between the
529    // the target and requested bit rate will NOT be treated as an error.
530    mVideoBitRate = bitRate;
531    return OK;
532}
533
534// Always rotate clockwise, and only support 0, 90, 180 and 270 for now.
535status_t StagefrightRecorder::setParamVideoRotation(int32_t degrees) {
536    ALOGV("setParamVideoRotation: %d", degrees);
537    if (degrees < 0 || degrees % 90 != 0) {
538        ALOGE("Unsupported video rotation angle: %d", degrees);
539        return BAD_VALUE;
540    }
541    mRotationDegrees = degrees % 360;
542    return OK;
543}
544
545status_t StagefrightRecorder::setParamMaxFileDurationUs(int64_t timeUs) {
546    ALOGV("setParamMaxFileDurationUs: %lld us", (long long)timeUs);
547
548    // This is meant for backward compatibility for MediaRecorder.java
549    if (timeUs <= 0) {
550        ALOGW("Max file duration is not positive: %lld us. Disabling duration limit.",
551                (long long)timeUs);
552        timeUs = 0; // Disable the duration limit for zero or negative values.
553    } else if (timeUs <= 100000LL) {  // XXX: 100 milli-seconds
554        ALOGE("Max file duration is too short: %lld us", (long long)timeUs);
555        return BAD_VALUE;
556    }
557
558    if (timeUs <= 15 * 1000000LL) {
559        ALOGW("Target duration (%lld us) too short to be respected", (long long)timeUs);
560    }
561    mMaxFileDurationUs = timeUs;
562    return OK;
563}
564
565status_t StagefrightRecorder::setParamMaxFileSizeBytes(int64_t bytes) {
566    ALOGV("setParamMaxFileSizeBytes: %lld bytes", (long long)bytes);
567
568    // This is meant for backward compatibility for MediaRecorder.java
569    if (bytes <= 0) {
570        ALOGW("Max file size is not positive: %lld bytes. "
571             "Disabling file size limit.", (long long)bytes);
572        bytes = 0; // Disable the file size limit for zero or negative values.
573    } else if (bytes <= 1024) {  // XXX: 1 kB
574        ALOGE("Max file size is too small: %lld bytes", (long long)bytes);
575        return BAD_VALUE;
576    }
577
578    if (bytes <= 100 * 1024) {
579        ALOGW("Target file size (%lld bytes) is too small to be respected", (long long)bytes);
580    }
581
582    mMaxFileSizeBytes = bytes;
583    return OK;
584}
585
586status_t StagefrightRecorder::setParamInterleaveDuration(int32_t durationUs) {
587    ALOGV("setParamInterleaveDuration: %d", durationUs);
588    if (durationUs <= 500000) {           //  500 ms
589        // If interleave duration is too small, it is very inefficient to do
590        // interleaving since the metadata overhead will count for a significant
591        // portion of the saved contents
592        ALOGE("Audio/video interleave duration is too small: %d us", durationUs);
593        return BAD_VALUE;
594    } else if (durationUs >= 10000000) {  // 10 seconds
595        // If interleaving duration is too large, it can cause the recording
596        // session to use too much memory since we have to save the output
597        // data before we write them out
598        ALOGE("Audio/video interleave duration is too large: %d us", durationUs);
599        return BAD_VALUE;
600    }
601    mInterleaveDurationUs = durationUs;
602    return OK;
603}
604
605// If seconds <  0, only the first frame is I frame, and rest are all P frames
606// If seconds == 0, all frames are encoded as I frames. No P frames
607// If seconds >  0, it is the time spacing (seconds) between 2 neighboring I frames
608status_t StagefrightRecorder::setParamVideoIFramesInterval(int32_t seconds) {
609    ALOGV("setParamVideoIFramesInterval: %d seconds", seconds);
610    mIFramesIntervalSec = seconds;
611    return OK;
612}
613
614status_t StagefrightRecorder::setParam64BitFileOffset(bool use64Bit) {
615    ALOGV("setParam64BitFileOffset: %s",
616        use64Bit? "use 64 bit file offset": "use 32 bit file offset");
617    mUse64BitFileOffset = use64Bit;
618    return OK;
619}
620
621status_t StagefrightRecorder::setParamVideoCameraId(int32_t cameraId) {
622    ALOGV("setParamVideoCameraId: %d", cameraId);
623    if (cameraId < 0) {
624        return BAD_VALUE;
625    }
626    mCameraId = cameraId;
627    return OK;
628}
629
630status_t StagefrightRecorder::setParamTrackTimeStatus(int64_t timeDurationUs) {
631    ALOGV("setParamTrackTimeStatus: %lld", (long long)timeDurationUs);
632    if (timeDurationUs < 20000) {  // Infeasible if shorter than 20 ms?
633        ALOGE("Tracking time duration too short: %lld us", (long long)timeDurationUs);
634        return BAD_VALUE;
635    }
636    mTrackEveryTimeDurationUs = timeDurationUs;
637    return OK;
638}
639
640status_t StagefrightRecorder::setParamVideoEncoderProfile(int32_t profile) {
641    ALOGV("setParamVideoEncoderProfile: %d", profile);
642
643    // Additional check will be done later when we load the encoder.
644    // For now, we are accepting values defined in OpenMAX IL.
645    mVideoEncoderProfile = profile;
646    return OK;
647}
648
649status_t StagefrightRecorder::setParamVideoEncoderLevel(int32_t level) {
650    ALOGV("setParamVideoEncoderLevel: %d", level);
651
652    // Additional check will be done later when we load the encoder.
653    // For now, we are accepting values defined in OpenMAX IL.
654    mVideoEncoderLevel = level;
655    return OK;
656}
657
658status_t StagefrightRecorder::setParamMovieTimeScale(int32_t timeScale) {
659    ALOGV("setParamMovieTimeScale: %d", timeScale);
660
661    // The range is set to be the same as the audio's time scale range
662    // since audio's time scale has a wider range.
663    if (timeScale < 600 || timeScale > 96000) {
664        ALOGE("Time scale (%d) for movie is out of range [600, 96000]", timeScale);
665        return BAD_VALUE;
666    }
667    mMovieTimeScale = timeScale;
668    return OK;
669}
670
671status_t StagefrightRecorder::setParamVideoTimeScale(int32_t timeScale) {
672    ALOGV("setParamVideoTimeScale: %d", timeScale);
673
674    // 60000 is chosen to make sure that each video frame from a 60-fps
675    // video has 1000 ticks.
676    if (timeScale < 600 || timeScale > 60000) {
677        ALOGE("Time scale (%d) for video is out of range [600, 60000]", timeScale);
678        return BAD_VALUE;
679    }
680    mVideoTimeScale = timeScale;
681    return OK;
682}
683
684status_t StagefrightRecorder::setParamAudioTimeScale(int32_t timeScale) {
685    ALOGV("setParamAudioTimeScale: %d", timeScale);
686
687    // 96000 Hz is the highest sampling rate support in AAC.
688    if (timeScale < 600 || timeScale > 96000) {
689        ALOGE("Time scale (%d) for audio is out of range [600, 96000]", timeScale);
690        return BAD_VALUE;
691    }
692    mAudioTimeScale = timeScale;
693    return OK;
694}
695
696status_t StagefrightRecorder::setParamCaptureFpsEnable(int32_t captureFpsEnable) {
697    ALOGV("setParamCaptureFpsEnable: %d", captureFpsEnable);
698
699    if(captureFpsEnable == 0) {
700        mCaptureFpsEnable = false;
701    } else if (captureFpsEnable == 1) {
702        mCaptureFpsEnable = true;
703    } else {
704        return BAD_VALUE;
705    }
706    return OK;
707}
708
709status_t StagefrightRecorder::setParamCaptureFps(float fps) {
710    ALOGV("setParamCaptureFps: %.2f", fps);
711
712    int64_t timeUs = (int64_t) (1000000.0 / fps + 0.5f);
713
714    // Not allowing time more than a day
715    if (timeUs <= 0 || timeUs > 86400*1E6) {
716        ALOGE("Time between frame capture (%lld) is out of range [0, 1 Day]", (long long)timeUs);
717        return BAD_VALUE;
718    }
719
720    mCaptureFps = fps;
721    mTimeBetweenCaptureUs = timeUs;
722    return OK;
723}
724
725status_t StagefrightRecorder::setParamGeoDataLongitude(
726    int64_t longitudex10000) {
727
728    if (longitudex10000 > 1800000 || longitudex10000 < -1800000) {
729        return BAD_VALUE;
730    }
731    mLongitudex10000 = longitudex10000;
732    return OK;
733}
734
735status_t StagefrightRecorder::setParamGeoDataLatitude(
736    int64_t latitudex10000) {
737
738    if (latitudex10000 > 900000 || latitudex10000 < -900000) {
739        return BAD_VALUE;
740    }
741    mLatitudex10000 = latitudex10000;
742    return OK;
743}
744
745status_t StagefrightRecorder::setParameter(
746        const String8 &key, const String8 &value) {
747    ALOGV("setParameter: key (%s) => value (%s)", key.string(), value.string());
748    if (key == "max-duration") {
749        int64_t max_duration_ms;
750        if (safe_strtoi64(value.string(), &max_duration_ms)) {
751            return setParamMaxFileDurationUs(1000LL * max_duration_ms);
752        }
753    } else if (key == "max-filesize") {
754        int64_t max_filesize_bytes;
755        if (safe_strtoi64(value.string(), &max_filesize_bytes)) {
756            return setParamMaxFileSizeBytes(max_filesize_bytes);
757        }
758    } else if (key == "interleave-duration-us") {
759        int32_t durationUs;
760        if (safe_strtoi32(value.string(), &durationUs)) {
761            return setParamInterleaveDuration(durationUs);
762        }
763    } else if (key == "param-movie-time-scale") {
764        int32_t timeScale;
765        if (safe_strtoi32(value.string(), &timeScale)) {
766            return setParamMovieTimeScale(timeScale);
767        }
768    } else if (key == "param-use-64bit-offset") {
769        int32_t use64BitOffset;
770        if (safe_strtoi32(value.string(), &use64BitOffset)) {
771            return setParam64BitFileOffset(use64BitOffset != 0);
772        }
773    } else if (key == "param-geotag-longitude") {
774        int64_t longitudex10000;
775        if (safe_strtoi64(value.string(), &longitudex10000)) {
776            return setParamGeoDataLongitude(longitudex10000);
777        }
778    } else if (key == "param-geotag-latitude") {
779        int64_t latitudex10000;
780        if (safe_strtoi64(value.string(), &latitudex10000)) {
781            return setParamGeoDataLatitude(latitudex10000);
782        }
783    } else if (key == "param-track-time-status") {
784        int64_t timeDurationUs;
785        if (safe_strtoi64(value.string(), &timeDurationUs)) {
786            return setParamTrackTimeStatus(timeDurationUs);
787        }
788    } else if (key == "audio-param-sampling-rate") {
789        int32_t sampling_rate;
790        if (safe_strtoi32(value.string(), &sampling_rate)) {
791            return setParamAudioSamplingRate(sampling_rate);
792        }
793    } else if (key == "audio-param-number-of-channels") {
794        int32_t number_of_channels;
795        if (safe_strtoi32(value.string(), &number_of_channels)) {
796            return setParamAudioNumberOfChannels(number_of_channels);
797        }
798    } else if (key == "audio-param-encoding-bitrate") {
799        int32_t audio_bitrate;
800        if (safe_strtoi32(value.string(), &audio_bitrate)) {
801            return setParamAudioEncodingBitRate(audio_bitrate);
802        }
803    } else if (key == "audio-param-time-scale") {
804        int32_t timeScale;
805        if (safe_strtoi32(value.string(), &timeScale)) {
806            return setParamAudioTimeScale(timeScale);
807        }
808    } else if (key == "video-param-encoding-bitrate") {
809        int32_t video_bitrate;
810        if (safe_strtoi32(value.string(), &video_bitrate)) {
811            return setParamVideoEncodingBitRate(video_bitrate);
812        }
813    } else if (key == "video-param-rotation-angle-degrees") {
814        int32_t degrees;
815        if (safe_strtoi32(value.string(), &degrees)) {
816            return setParamVideoRotation(degrees);
817        }
818    } else if (key == "video-param-i-frames-interval") {
819        int32_t seconds;
820        if (safe_strtoi32(value.string(), &seconds)) {
821            return setParamVideoIFramesInterval(seconds);
822        }
823    } else if (key == "video-param-encoder-profile") {
824        int32_t profile;
825        if (safe_strtoi32(value.string(), &profile)) {
826            return setParamVideoEncoderProfile(profile);
827        }
828    } else if (key == "video-param-encoder-level") {
829        int32_t level;
830        if (safe_strtoi32(value.string(), &level)) {
831            return setParamVideoEncoderLevel(level);
832        }
833    } else if (key == "video-param-camera-id") {
834        int32_t cameraId;
835        if (safe_strtoi32(value.string(), &cameraId)) {
836            return setParamVideoCameraId(cameraId);
837        }
838    } else if (key == "video-param-time-scale") {
839        int32_t timeScale;
840        if (safe_strtoi32(value.string(), &timeScale)) {
841            return setParamVideoTimeScale(timeScale);
842        }
843    } else if (key == "time-lapse-enable") {
844        int32_t captureFpsEnable;
845        if (safe_strtoi32(value.string(), &captureFpsEnable)) {
846            return setParamCaptureFpsEnable(captureFpsEnable);
847        }
848    } else if (key == "time-lapse-fps") {
849        float fps;
850        if (safe_strtof(value.string(), &fps)) {
851            return setParamCaptureFps(fps);
852        }
853    } else {
854        ALOGE("setParameter: failed to find key %s", key.string());
855    }
856    return BAD_VALUE;
857}
858
859status_t StagefrightRecorder::setParameters(const String8 &params) {
860    ALOGV("setParameters: %s", params.string());
861    const char *cparams = params.string();
862    const char *key_start = cparams;
863    for (;;) {
864        const char *equal_pos = strchr(key_start, '=');
865        if (equal_pos == NULL) {
866            ALOGE("Parameters %s miss a value", cparams);
867            return BAD_VALUE;
868        }
869        String8 key(key_start, equal_pos - key_start);
870        TrimString(&key);
871        if (key.length() == 0) {
872            ALOGE("Parameters %s contains an empty key", cparams);
873            return BAD_VALUE;
874        }
875        const char *value_start = equal_pos + 1;
876        const char *semicolon_pos = strchr(value_start, ';');
877        String8 value;
878        if (semicolon_pos == NULL) {
879            value.setTo(value_start);
880        } else {
881            value.setTo(value_start, semicolon_pos - value_start);
882        }
883        if (setParameter(key, value) != OK) {
884            return BAD_VALUE;
885        }
886        if (semicolon_pos == NULL) {
887            break;  // Reaches the end
888        }
889        key_start = semicolon_pos + 1;
890    }
891    return OK;
892}
893
894status_t StagefrightRecorder::setListener(const sp<IMediaRecorderClient> &listener) {
895    mListener = listener;
896
897    return OK;
898}
899
900status_t StagefrightRecorder::setClientName(const String16& clientName) {
901    mClientName = clientName;
902
903    return OK;
904}
905
906status_t StagefrightRecorder::prepareInternal() {
907    ALOGV("prepare");
908    if (mOutputFd < 0) {
909        ALOGE("Output file descriptor is invalid");
910        return INVALID_OPERATION;
911    }
912
913    // Get UID and PID here for permission checking
914    mClientUid = IPCThreadState::self()->getCallingUid();
915    mClientPid = IPCThreadState::self()->getCallingPid();
916
917    status_t status = OK;
918
919    switch (mOutputFormat) {
920        case OUTPUT_FORMAT_DEFAULT:
921        case OUTPUT_FORMAT_THREE_GPP:
922        case OUTPUT_FORMAT_MPEG_4:
923        case OUTPUT_FORMAT_WEBM:
924            status = setupMPEG4orWEBMRecording();
925            break;
926
927        case OUTPUT_FORMAT_AMR_NB:
928        case OUTPUT_FORMAT_AMR_WB:
929            status = setupAMRRecording();
930            break;
931
932        case OUTPUT_FORMAT_AAC_ADIF:
933        case OUTPUT_FORMAT_AAC_ADTS:
934            status = setupAACRecording();
935            break;
936
937        case OUTPUT_FORMAT_RTP_AVP:
938            status = setupRTPRecording();
939            break;
940
941        case OUTPUT_FORMAT_MPEG2TS:
942            status = setupMPEG2TSRecording();
943            break;
944
945        default:
946            ALOGE("Unsupported output file format: %d", mOutputFormat);
947            status = UNKNOWN_ERROR;
948            break;
949    }
950
951    ALOGV("Recording frameRate: %d captureFps: %f",
952            mFrameRate, mCaptureFps);
953
954    return status;
955}
956
957status_t StagefrightRecorder::prepare() {
958    ALOGV("prepare");
959    Mutex::Autolock autolock(mLock);
960    if (mVideoSource == VIDEO_SOURCE_SURFACE) {
961        return prepareInternal();
962    }
963    return OK;
964}
965
966status_t StagefrightRecorder::start() {
967    ALOGV("start");
968    Mutex::Autolock autolock(mLock);
969    if (mOutputFd < 0) {
970        ALOGE("Output file descriptor is invalid");
971        return INVALID_OPERATION;
972    }
973
974    status_t status = OK;
975
976    if (mVideoSource != VIDEO_SOURCE_SURFACE) {
977        status = prepareInternal();
978        if (status != OK) {
979            return status;
980        }
981    }
982
983    if (mWriter == NULL) {
984        ALOGE("File writer is not avaialble");
985        return UNKNOWN_ERROR;
986    }
987
988    switch (mOutputFormat) {
989        case OUTPUT_FORMAT_DEFAULT:
990        case OUTPUT_FORMAT_THREE_GPP:
991        case OUTPUT_FORMAT_MPEG_4:
992        case OUTPUT_FORMAT_WEBM:
993        {
994            bool isMPEG4 = true;
995            if (mOutputFormat == OUTPUT_FORMAT_WEBM) {
996                isMPEG4 = false;
997            }
998            sp<MetaData> meta = new MetaData;
999            setupMPEG4orWEBMMetaData(&meta);
1000            status = mWriter->start(meta.get());
1001            break;
1002        }
1003
1004        case OUTPUT_FORMAT_AMR_NB:
1005        case OUTPUT_FORMAT_AMR_WB:
1006        case OUTPUT_FORMAT_AAC_ADIF:
1007        case OUTPUT_FORMAT_AAC_ADTS:
1008        case OUTPUT_FORMAT_RTP_AVP:
1009        case OUTPUT_FORMAT_MPEG2TS:
1010        {
1011            sp<MetaData> meta = new MetaData;
1012            int64_t startTimeUs = systemTime() / 1000;
1013            meta->setInt64(kKeyTime, startTimeUs);
1014            status = mWriter->start(meta.get());
1015            break;
1016        }
1017
1018        default:
1019        {
1020            ALOGE("Unsupported output file format: %d", mOutputFormat);
1021            status = UNKNOWN_ERROR;
1022            break;
1023        }
1024    }
1025
1026    if (status != OK) {
1027        mWriter.clear();
1028        mWriter = NULL;
1029    }
1030
1031    if ((status == OK) && (!mStarted)) {
1032        mAnalyticsDirty = true;
1033        mStarted = true;
1034
1035        uint32_t params = IMediaPlayerService::kBatteryDataCodecStarted;
1036        if (mAudioSource != AUDIO_SOURCE_CNT) {
1037            params |= IMediaPlayerService::kBatteryDataTrackAudio;
1038        }
1039        if (mVideoSource != VIDEO_SOURCE_LIST_END) {
1040            params |= IMediaPlayerService::kBatteryDataTrackVideo;
1041        }
1042
1043        addBatteryData(params);
1044    }
1045
1046    return status;
1047}
1048
1049sp<MediaCodecSource> StagefrightRecorder::createAudioSource() {
1050    int32_t sourceSampleRate = mSampleRate;
1051
1052    if (mCaptureFpsEnable && mCaptureFps >= mFrameRate) {
1053        // Upscale the sample rate for slow motion recording.
1054        // Fail audio source creation if source sample rate is too high, as it could
1055        // cause out-of-memory due to large input buffer size. And audio recording
1056        // probably doesn't make sense in the scenario, since the slow-down factor
1057        // is probably huge (eg. mSampleRate=48K, mCaptureFps=240, mFrameRate=1).
1058        const static int32_t SAMPLE_RATE_HZ_MAX = 192000;
1059        sourceSampleRate =
1060                (mSampleRate * mCaptureFps + mFrameRate / 2) / mFrameRate;
1061        if (sourceSampleRate < mSampleRate || sourceSampleRate > SAMPLE_RATE_HZ_MAX) {
1062            ALOGE("source sample rate out of range! "
1063                    "(mSampleRate %d, mCaptureFps %.2f, mFrameRate %d",
1064                    mSampleRate, mCaptureFps, mFrameRate);
1065            return NULL;
1066        }
1067    }
1068
1069    sp<AudioSource> audioSource =
1070        new AudioSource(
1071                mAudioSource,
1072                mOpPackageName,
1073                sourceSampleRate,
1074                mAudioChannels,
1075                mSampleRate,
1076                mClientUid,
1077                mClientPid);
1078
1079    status_t err = audioSource->initCheck();
1080
1081    if (err != OK) {
1082        ALOGE("audio source is not initialized");
1083        return NULL;
1084    }
1085
1086    sp<AMessage> format = new AMessage;
1087    switch (mAudioEncoder) {
1088        case AUDIO_ENCODER_AMR_NB:
1089        case AUDIO_ENCODER_DEFAULT:
1090            format->setString("mime", MEDIA_MIMETYPE_AUDIO_AMR_NB);
1091            break;
1092        case AUDIO_ENCODER_AMR_WB:
1093            format->setString("mime", MEDIA_MIMETYPE_AUDIO_AMR_WB);
1094            break;
1095        case AUDIO_ENCODER_AAC:
1096            format->setString("mime", MEDIA_MIMETYPE_AUDIO_AAC);
1097            format->setInt32("aac-profile", OMX_AUDIO_AACObjectLC);
1098            break;
1099        case AUDIO_ENCODER_HE_AAC:
1100            format->setString("mime", MEDIA_MIMETYPE_AUDIO_AAC);
1101            format->setInt32("aac-profile", OMX_AUDIO_AACObjectHE);
1102            break;
1103        case AUDIO_ENCODER_AAC_ELD:
1104            format->setString("mime", MEDIA_MIMETYPE_AUDIO_AAC);
1105            format->setInt32("aac-profile", OMX_AUDIO_AACObjectELD);
1106            break;
1107
1108        default:
1109            ALOGE("Unknown audio encoder: %d", mAudioEncoder);
1110            return NULL;
1111    }
1112
1113    int32_t maxInputSize;
1114    CHECK(audioSource->getFormat()->findInt32(
1115                kKeyMaxInputSize, &maxInputSize));
1116
1117    format->setInt32("max-input-size", maxInputSize);
1118    format->setInt32("channel-count", mAudioChannels);
1119    format->setInt32("sample-rate", mSampleRate);
1120    format->setInt32("bitrate", mAudioBitRate);
1121    if (mAudioTimeScale > 0) {
1122        format->setInt32("time-scale", mAudioTimeScale);
1123    }
1124    format->setInt32("priority", 0 /* realtime */);
1125
1126    sp<MediaCodecSource> audioEncoder =
1127            MediaCodecSource::Create(mLooper, format, audioSource);
1128    mAudioSourceNode = audioSource;
1129
1130    if (audioEncoder == NULL) {
1131        ALOGE("Failed to create audio encoder");
1132    }
1133
1134    return audioEncoder;
1135}
1136
1137status_t StagefrightRecorder::setupAACRecording() {
1138    // FIXME:
1139    // Add support for OUTPUT_FORMAT_AAC_ADIF
1140    CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_AAC_ADTS);
1141
1142    CHECK(mAudioEncoder == AUDIO_ENCODER_AAC ||
1143          mAudioEncoder == AUDIO_ENCODER_HE_AAC ||
1144          mAudioEncoder == AUDIO_ENCODER_AAC_ELD);
1145    CHECK(mAudioSource != AUDIO_SOURCE_CNT);
1146
1147    mWriter = new AACWriter(mOutputFd);
1148    return setupRawAudioRecording();
1149}
1150
1151status_t StagefrightRecorder::setupAMRRecording() {
1152    CHECK(mOutputFormat == OUTPUT_FORMAT_AMR_NB ||
1153          mOutputFormat == OUTPUT_FORMAT_AMR_WB);
1154
1155    if (mOutputFormat == OUTPUT_FORMAT_AMR_NB) {
1156        if (mAudioEncoder != AUDIO_ENCODER_DEFAULT &&
1157            mAudioEncoder != AUDIO_ENCODER_AMR_NB) {
1158            ALOGE("Invalid encoder %d used for AMRNB recording",
1159                    mAudioEncoder);
1160            return BAD_VALUE;
1161        }
1162    } else {  // mOutputFormat must be OUTPUT_FORMAT_AMR_WB
1163        if (mAudioEncoder != AUDIO_ENCODER_AMR_WB) {
1164            ALOGE("Invlaid encoder %d used for AMRWB recording",
1165                    mAudioEncoder);
1166            return BAD_VALUE;
1167        }
1168    }
1169
1170    mWriter = new AMRWriter(mOutputFd);
1171    return setupRawAudioRecording();
1172}
1173
1174status_t StagefrightRecorder::setupRawAudioRecording() {
1175    if (mAudioSource >= AUDIO_SOURCE_CNT && mAudioSource != AUDIO_SOURCE_FM_TUNER) {
1176        ALOGE("Invalid audio source: %d", mAudioSource);
1177        return BAD_VALUE;
1178    }
1179
1180    status_t status = BAD_VALUE;
1181    if (OK != (status = checkAudioEncoderCapabilities())) {
1182        return status;
1183    }
1184
1185    sp<MediaCodecSource> audioEncoder = createAudioSource();
1186    if (audioEncoder == NULL) {
1187        return UNKNOWN_ERROR;
1188    }
1189
1190    CHECK(mWriter != 0);
1191    mWriter->addSource(audioEncoder);
1192    mAudioEncoderSource = audioEncoder;
1193
1194    if (mMaxFileDurationUs != 0) {
1195        mWriter->setMaxFileDuration(mMaxFileDurationUs);
1196    }
1197    if (mMaxFileSizeBytes != 0) {
1198        mWriter->setMaxFileSize(mMaxFileSizeBytes);
1199    }
1200    mWriter->setListener(mListener);
1201
1202    return OK;
1203}
1204
1205status_t StagefrightRecorder::setupRTPRecording() {
1206    CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_RTP_AVP);
1207
1208    if ((mAudioSource != AUDIO_SOURCE_CNT
1209                && mVideoSource != VIDEO_SOURCE_LIST_END)
1210            || (mAudioSource == AUDIO_SOURCE_CNT
1211                && mVideoSource == VIDEO_SOURCE_LIST_END)) {
1212        // Must have exactly one source.
1213        return BAD_VALUE;
1214    }
1215
1216    if (mOutputFd < 0) {
1217        return BAD_VALUE;
1218    }
1219
1220    sp<MediaCodecSource> source;
1221
1222    if (mAudioSource != AUDIO_SOURCE_CNT) {
1223        source = createAudioSource();
1224        mAudioEncoderSource = source;
1225    } else {
1226        setDefaultVideoEncoderIfNecessary();
1227
1228        sp<MediaSource> mediaSource;
1229        status_t err = setupMediaSource(&mediaSource);
1230        if (err != OK) {
1231            return err;
1232        }
1233
1234        err = setupVideoEncoder(mediaSource, &source);
1235        if (err != OK) {
1236            return err;
1237        }
1238        mVideoEncoderSource = source;
1239    }
1240
1241    mWriter = new ARTPWriter(mOutputFd);
1242    mWriter->addSource(source);
1243    mWriter->setListener(mListener);
1244
1245    return OK;
1246}
1247
1248status_t StagefrightRecorder::setupMPEG2TSRecording() {
1249    CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_MPEG2TS);
1250
1251    sp<MediaWriter> writer = new MPEG2TSWriter(mOutputFd);
1252
1253    if (mAudioSource != AUDIO_SOURCE_CNT) {
1254        if (mAudioEncoder != AUDIO_ENCODER_AAC &&
1255            mAudioEncoder != AUDIO_ENCODER_HE_AAC &&
1256            mAudioEncoder != AUDIO_ENCODER_AAC_ELD) {
1257            return ERROR_UNSUPPORTED;
1258        }
1259
1260        status_t err = setupAudioEncoder(writer);
1261
1262        if (err != OK) {
1263            return err;
1264        }
1265    }
1266
1267    if (mVideoSource < VIDEO_SOURCE_LIST_END) {
1268        if (mVideoEncoder != VIDEO_ENCODER_H264) {
1269            ALOGE("MPEG2TS recording only supports H.264 encoding!");
1270            return ERROR_UNSUPPORTED;
1271        }
1272
1273        sp<MediaSource> mediaSource;
1274        status_t err = setupMediaSource(&mediaSource);
1275        if (err != OK) {
1276            return err;
1277        }
1278
1279        sp<MediaCodecSource> encoder;
1280        err = setupVideoEncoder(mediaSource, &encoder);
1281
1282        if (err != OK) {
1283            return err;
1284        }
1285
1286        writer->addSource(encoder);
1287        mVideoEncoderSource = encoder;
1288    }
1289
1290    if (mMaxFileDurationUs != 0) {
1291        writer->setMaxFileDuration(mMaxFileDurationUs);
1292    }
1293
1294    if (mMaxFileSizeBytes != 0) {
1295        writer->setMaxFileSize(mMaxFileSizeBytes);
1296    }
1297
1298    mWriter = writer;
1299
1300    return OK;
1301}
1302
1303void StagefrightRecorder::clipVideoFrameRate() {
1304    ALOGV("clipVideoFrameRate: encoder %d", mVideoEncoder);
1305    if (mFrameRate == -1) {
1306        mFrameRate = mEncoderProfiles->getCamcorderProfileParamByName(
1307                "vid.fps", mCameraId, CAMCORDER_QUALITY_LOW);
1308        ALOGW("Using default video fps %d", mFrameRate);
1309    }
1310
1311    int minFrameRate = mEncoderProfiles->getVideoEncoderParamByName(
1312                        "enc.vid.fps.min", mVideoEncoder);
1313    int maxFrameRate = mEncoderProfiles->getVideoEncoderParamByName(
1314                        "enc.vid.fps.max", mVideoEncoder);
1315    if (mFrameRate < minFrameRate && minFrameRate != -1) {
1316        ALOGW("Intended video encoding frame rate (%d fps) is too small"
1317             " and will be set to (%d fps)", mFrameRate, minFrameRate);
1318        mFrameRate = minFrameRate;
1319    } else if (mFrameRate > maxFrameRate && maxFrameRate != -1) {
1320        ALOGW("Intended video encoding frame rate (%d fps) is too large"
1321             " and will be set to (%d fps)", mFrameRate, maxFrameRate);
1322        mFrameRate = maxFrameRate;
1323    }
1324}
1325
1326void StagefrightRecorder::clipVideoBitRate() {
1327    ALOGV("clipVideoBitRate: encoder %d", mVideoEncoder);
1328    int minBitRate = mEncoderProfiles->getVideoEncoderParamByName(
1329                        "enc.vid.bps.min", mVideoEncoder);
1330    int maxBitRate = mEncoderProfiles->getVideoEncoderParamByName(
1331                        "enc.vid.bps.max", mVideoEncoder);
1332    if (mVideoBitRate < minBitRate && minBitRate != -1) {
1333        ALOGW("Intended video encoding bit rate (%d bps) is too small"
1334             " and will be set to (%d bps)", mVideoBitRate, minBitRate);
1335        mVideoBitRate = minBitRate;
1336    } else if (mVideoBitRate > maxBitRate && maxBitRate != -1) {
1337        ALOGW("Intended video encoding bit rate (%d bps) is too large"
1338             " and will be set to (%d bps)", mVideoBitRate, maxBitRate);
1339        mVideoBitRate = maxBitRate;
1340    }
1341}
1342
1343void StagefrightRecorder::clipVideoFrameWidth() {
1344    ALOGV("clipVideoFrameWidth: encoder %d", mVideoEncoder);
1345    int minFrameWidth = mEncoderProfiles->getVideoEncoderParamByName(
1346                        "enc.vid.width.min", mVideoEncoder);
1347    int maxFrameWidth = mEncoderProfiles->getVideoEncoderParamByName(
1348                        "enc.vid.width.max", mVideoEncoder);
1349    if (mVideoWidth < minFrameWidth && minFrameWidth != -1) {
1350        ALOGW("Intended video encoding frame width (%d) is too small"
1351             " and will be set to (%d)", mVideoWidth, minFrameWidth);
1352        mVideoWidth = minFrameWidth;
1353    } else if (mVideoWidth > maxFrameWidth && maxFrameWidth != -1) {
1354        ALOGW("Intended video encoding frame width (%d) is too large"
1355             " and will be set to (%d)", mVideoWidth, maxFrameWidth);
1356        mVideoWidth = maxFrameWidth;
1357    }
1358}
1359
1360status_t StagefrightRecorder::checkVideoEncoderCapabilities() {
1361    if (!mCaptureFpsEnable) {
1362        // Dont clip for time lapse capture as encoder will have enough
1363        // time to encode because of slow capture rate of time lapse.
1364        clipVideoBitRate();
1365        clipVideoFrameRate();
1366        clipVideoFrameWidth();
1367        clipVideoFrameHeight();
1368        setDefaultProfileIfNecessary();
1369    }
1370    return OK;
1371}
1372
1373// Set to use AVC baseline profile if the encoding parameters matches
1374// CAMCORDER_QUALITY_LOW profile; this is for the sake of MMS service.
1375void StagefrightRecorder::setDefaultProfileIfNecessary() {
1376    ALOGV("setDefaultProfileIfNecessary");
1377
1378    camcorder_quality quality = CAMCORDER_QUALITY_LOW;
1379
1380    int64_t durationUs   = mEncoderProfiles->getCamcorderProfileParamByName(
1381                                "duration", mCameraId, quality) * 1000000LL;
1382
1383    int fileFormat       = mEncoderProfiles->getCamcorderProfileParamByName(
1384                                "file.format", mCameraId, quality);
1385
1386    int videoCodec       = mEncoderProfiles->getCamcorderProfileParamByName(
1387                                "vid.codec", mCameraId, quality);
1388
1389    int videoBitRate     = mEncoderProfiles->getCamcorderProfileParamByName(
1390                                "vid.bps", mCameraId, quality);
1391
1392    int videoFrameRate   = mEncoderProfiles->getCamcorderProfileParamByName(
1393                                "vid.fps", mCameraId, quality);
1394
1395    int videoFrameWidth  = mEncoderProfiles->getCamcorderProfileParamByName(
1396                                "vid.width", mCameraId, quality);
1397
1398    int videoFrameHeight = mEncoderProfiles->getCamcorderProfileParamByName(
1399                                "vid.height", mCameraId, quality);
1400
1401    int audioCodec       = mEncoderProfiles->getCamcorderProfileParamByName(
1402                                "aud.codec", mCameraId, quality);
1403
1404    int audioBitRate     = mEncoderProfiles->getCamcorderProfileParamByName(
1405                                "aud.bps", mCameraId, quality);
1406
1407    int audioSampleRate  = mEncoderProfiles->getCamcorderProfileParamByName(
1408                                "aud.hz", mCameraId, quality);
1409
1410    int audioChannels    = mEncoderProfiles->getCamcorderProfileParamByName(
1411                                "aud.ch", mCameraId, quality);
1412
1413    if (durationUs == mMaxFileDurationUs &&
1414        fileFormat == mOutputFormat &&
1415        videoCodec == mVideoEncoder &&
1416        videoBitRate == mVideoBitRate &&
1417        videoFrameRate == mFrameRate &&
1418        videoFrameWidth == mVideoWidth &&
1419        videoFrameHeight == mVideoHeight &&
1420        audioCodec == mAudioEncoder &&
1421        audioBitRate == mAudioBitRate &&
1422        audioSampleRate == mSampleRate &&
1423        audioChannels == mAudioChannels) {
1424        if (videoCodec == VIDEO_ENCODER_H264) {
1425            ALOGI("Force to use AVC baseline profile");
1426            setParamVideoEncoderProfile(OMX_VIDEO_AVCProfileBaseline);
1427            // set 0 for invalid levels - this will be rejected by the
1428            // codec if it cannot handle it during configure
1429            setParamVideoEncoderLevel(ACodec::getAVCLevelFor(
1430                    videoFrameWidth, videoFrameHeight, videoFrameRate, videoBitRate));
1431        }
1432    }
1433}
1434
1435void StagefrightRecorder::setDefaultVideoEncoderIfNecessary() {
1436    if (mVideoEncoder == VIDEO_ENCODER_DEFAULT) {
1437        if (mOutputFormat == OUTPUT_FORMAT_WEBM) {
1438            // default to VP8 for WEBM recording
1439            mVideoEncoder = VIDEO_ENCODER_VP8;
1440        } else {
1441            // pick the default encoder for CAMCORDER_QUALITY_LOW
1442            int videoCodec = mEncoderProfiles->getCamcorderProfileParamByName(
1443                    "vid.codec", mCameraId, CAMCORDER_QUALITY_LOW);
1444
1445            if (videoCodec > VIDEO_ENCODER_DEFAULT &&
1446                videoCodec < VIDEO_ENCODER_LIST_END) {
1447                mVideoEncoder = (video_encoder)videoCodec;
1448            } else {
1449                // default to H.264 if camcorder profile not available
1450                mVideoEncoder = VIDEO_ENCODER_H264;
1451            }
1452        }
1453    }
1454}
1455
1456status_t StagefrightRecorder::checkAudioEncoderCapabilities() {
1457    clipAudioBitRate();
1458    clipAudioSampleRate();
1459    clipNumberOfAudioChannels();
1460    return OK;
1461}
1462
1463void StagefrightRecorder::clipAudioBitRate() {
1464    ALOGV("clipAudioBitRate: encoder %d", mAudioEncoder);
1465
1466    int minAudioBitRate =
1467            mEncoderProfiles->getAudioEncoderParamByName(
1468                "enc.aud.bps.min", mAudioEncoder);
1469    if (minAudioBitRate != -1 && mAudioBitRate < minAudioBitRate) {
1470        ALOGW("Intended audio encoding bit rate (%d) is too small"
1471            " and will be set to (%d)", mAudioBitRate, minAudioBitRate);
1472        mAudioBitRate = minAudioBitRate;
1473    }
1474
1475    int maxAudioBitRate =
1476            mEncoderProfiles->getAudioEncoderParamByName(
1477                "enc.aud.bps.max", mAudioEncoder);
1478    if (maxAudioBitRate != -1 && mAudioBitRate > maxAudioBitRate) {
1479        ALOGW("Intended audio encoding bit rate (%d) is too large"
1480            " and will be set to (%d)", mAudioBitRate, maxAudioBitRate);
1481        mAudioBitRate = maxAudioBitRate;
1482    }
1483}
1484
1485void StagefrightRecorder::clipAudioSampleRate() {
1486    ALOGV("clipAudioSampleRate: encoder %d", mAudioEncoder);
1487
1488    int minSampleRate =
1489            mEncoderProfiles->getAudioEncoderParamByName(
1490                "enc.aud.hz.min", mAudioEncoder);
1491    if (minSampleRate != -1 && mSampleRate < minSampleRate) {
1492        ALOGW("Intended audio sample rate (%d) is too small"
1493            " and will be set to (%d)", mSampleRate, minSampleRate);
1494        mSampleRate = minSampleRate;
1495    }
1496
1497    int maxSampleRate =
1498            mEncoderProfiles->getAudioEncoderParamByName(
1499                "enc.aud.hz.max", mAudioEncoder);
1500    if (maxSampleRate != -1 && mSampleRate > maxSampleRate) {
1501        ALOGW("Intended audio sample rate (%d) is too large"
1502            " and will be set to (%d)", mSampleRate, maxSampleRate);
1503        mSampleRate = maxSampleRate;
1504    }
1505}
1506
1507void StagefrightRecorder::clipNumberOfAudioChannels() {
1508    ALOGV("clipNumberOfAudioChannels: encoder %d", mAudioEncoder);
1509
1510    int minChannels =
1511            mEncoderProfiles->getAudioEncoderParamByName(
1512                "enc.aud.ch.min", mAudioEncoder);
1513    if (minChannels != -1 && mAudioChannels < minChannels) {
1514        ALOGW("Intended number of audio channels (%d) is too small"
1515            " and will be set to (%d)", mAudioChannels, minChannels);
1516        mAudioChannels = minChannels;
1517    }
1518
1519    int maxChannels =
1520            mEncoderProfiles->getAudioEncoderParamByName(
1521                "enc.aud.ch.max", mAudioEncoder);
1522    if (maxChannels != -1 && mAudioChannels > maxChannels) {
1523        ALOGW("Intended number of audio channels (%d) is too large"
1524            " and will be set to (%d)", mAudioChannels, maxChannels);
1525        mAudioChannels = maxChannels;
1526    }
1527}
1528
1529void StagefrightRecorder::clipVideoFrameHeight() {
1530    ALOGV("clipVideoFrameHeight: encoder %d", mVideoEncoder);
1531    int minFrameHeight = mEncoderProfiles->getVideoEncoderParamByName(
1532                        "enc.vid.height.min", mVideoEncoder);
1533    int maxFrameHeight = mEncoderProfiles->getVideoEncoderParamByName(
1534                        "enc.vid.height.max", mVideoEncoder);
1535    if (minFrameHeight != -1 && mVideoHeight < minFrameHeight) {
1536        ALOGW("Intended video encoding frame height (%d) is too small"
1537             " and will be set to (%d)", mVideoHeight, minFrameHeight);
1538        mVideoHeight = minFrameHeight;
1539    } else if (maxFrameHeight != -1 && mVideoHeight > maxFrameHeight) {
1540        ALOGW("Intended video encoding frame height (%d) is too large"
1541             " and will be set to (%d)", mVideoHeight, maxFrameHeight);
1542        mVideoHeight = maxFrameHeight;
1543    }
1544}
1545
1546// Set up the appropriate MediaSource depending on the chosen option
1547status_t StagefrightRecorder::setupMediaSource(
1548                      sp<MediaSource> *mediaSource) {
1549    if (mVideoSource == VIDEO_SOURCE_DEFAULT
1550            || mVideoSource == VIDEO_SOURCE_CAMERA) {
1551        sp<CameraSource> cameraSource;
1552        status_t err = setupCameraSource(&cameraSource);
1553        if (err != OK) {
1554            return err;
1555        }
1556        *mediaSource = cameraSource;
1557    } else if (mVideoSource == VIDEO_SOURCE_SURFACE) {
1558        *mediaSource = NULL;
1559    } else {
1560        return INVALID_OPERATION;
1561    }
1562    return OK;
1563}
1564
1565status_t StagefrightRecorder::setupCameraSource(
1566        sp<CameraSource> *cameraSource) {
1567    status_t err = OK;
1568    if ((err = checkVideoEncoderCapabilities()) != OK) {
1569        return err;
1570    }
1571    Size videoSize;
1572    videoSize.width = mVideoWidth;
1573    videoSize.height = mVideoHeight;
1574    if (mCaptureFpsEnable) {
1575        if (mTimeBetweenCaptureUs < 0) {
1576            ALOGE("Invalid mTimeBetweenTimeLapseFrameCaptureUs value: %lld",
1577                    (long long)mTimeBetweenCaptureUs);
1578            return BAD_VALUE;
1579        }
1580
1581        mCameraSourceTimeLapse = CameraSourceTimeLapse::CreateFromCamera(
1582                mCamera, mCameraProxy, mCameraId, mClientName, mClientUid, mClientPid,
1583                videoSize, mFrameRate, mPreviewSurface,
1584                mTimeBetweenCaptureUs);
1585        *cameraSource = mCameraSourceTimeLapse;
1586    } else {
1587        *cameraSource = CameraSource::CreateFromCamera(
1588                mCamera, mCameraProxy, mCameraId, mClientName, mClientUid, mClientPid,
1589                videoSize, mFrameRate,
1590                mPreviewSurface);
1591    }
1592    mCamera.clear();
1593    mCameraProxy.clear();
1594    if (*cameraSource == NULL) {
1595        return UNKNOWN_ERROR;
1596    }
1597
1598    if ((*cameraSource)->initCheck() != OK) {
1599        (*cameraSource).clear();
1600        *cameraSource = NULL;
1601        return NO_INIT;
1602    }
1603
1604    // When frame rate is not set, the actual frame rate will be set to
1605    // the current frame rate being used.
1606    if (mFrameRate == -1) {
1607        int32_t frameRate = 0;
1608        CHECK ((*cameraSource)->getFormat()->findInt32(
1609                    kKeyFrameRate, &frameRate));
1610        ALOGI("Frame rate is not explicitly set. Use the current frame "
1611             "rate (%d fps)", frameRate);
1612        mFrameRate = frameRate;
1613    }
1614
1615    CHECK(mFrameRate != -1);
1616
1617    mMetaDataStoredInVideoBuffers =
1618        (*cameraSource)->metaDataStoredInVideoBuffers();
1619
1620    return OK;
1621}
1622
1623status_t StagefrightRecorder::setupVideoEncoder(
1624        const sp<MediaSource> &cameraSource,
1625        sp<MediaCodecSource> *source) {
1626    source->clear();
1627
1628    sp<AMessage> format = new AMessage();
1629
1630    switch (mVideoEncoder) {
1631        case VIDEO_ENCODER_H263:
1632            format->setString("mime", MEDIA_MIMETYPE_VIDEO_H263);
1633            break;
1634
1635        case VIDEO_ENCODER_MPEG_4_SP:
1636            format->setString("mime", MEDIA_MIMETYPE_VIDEO_MPEG4);
1637            break;
1638
1639        case VIDEO_ENCODER_H264:
1640            format->setString("mime", MEDIA_MIMETYPE_VIDEO_AVC);
1641            break;
1642
1643        case VIDEO_ENCODER_VP8:
1644            format->setString("mime", MEDIA_MIMETYPE_VIDEO_VP8);
1645            break;
1646
1647        case VIDEO_ENCODER_HEVC:
1648            format->setString("mime", MEDIA_MIMETYPE_VIDEO_HEVC);
1649            break;
1650
1651        default:
1652            CHECK(!"Should not be here, unsupported video encoding.");
1653            break;
1654    }
1655
1656    if (cameraSource != NULL) {
1657        sp<MetaData> meta = cameraSource->getFormat();
1658
1659        int32_t width, height, stride, sliceHeight, colorFormat;
1660        CHECK(meta->findInt32(kKeyWidth, &width));
1661        CHECK(meta->findInt32(kKeyHeight, &height));
1662        CHECK(meta->findInt32(kKeyStride, &stride));
1663        CHECK(meta->findInt32(kKeySliceHeight, &sliceHeight));
1664        CHECK(meta->findInt32(kKeyColorFormat, &colorFormat));
1665
1666        format->setInt32("width", width);
1667        format->setInt32("height", height);
1668        format->setInt32("stride", stride);
1669        format->setInt32("slice-height", sliceHeight);
1670        format->setInt32("color-format", colorFormat);
1671    } else {
1672        format->setInt32("width", mVideoWidth);
1673        format->setInt32("height", mVideoHeight);
1674        format->setInt32("stride", mVideoWidth);
1675        format->setInt32("slice-height", mVideoHeight);
1676        format->setInt32("color-format", OMX_COLOR_FormatAndroidOpaque);
1677
1678        // set up time lapse/slow motion for surface source
1679        if (mCaptureFpsEnable) {
1680            if (mTimeBetweenCaptureUs <= 0) {
1681                ALOGE("Invalid mTimeBetweenCaptureUs value: %lld",
1682                        (long long)mTimeBetweenCaptureUs);
1683                return BAD_VALUE;
1684            }
1685            format->setInt64("time-lapse", mTimeBetweenCaptureUs);
1686        }
1687    }
1688
1689    format->setInt32("bitrate", mVideoBitRate);
1690    format->setInt32("frame-rate", mFrameRate);
1691    format->setInt32("i-frame-interval", mIFramesIntervalSec);
1692
1693    if (mVideoTimeScale > 0) {
1694        format->setInt32("time-scale", mVideoTimeScale);
1695    }
1696    if (mVideoEncoderProfile != -1) {
1697        format->setInt32("profile", mVideoEncoderProfile);
1698    }
1699    if (mVideoEncoderLevel != -1) {
1700        format->setInt32("level", mVideoEncoderLevel);
1701    }
1702
1703    uint32_t tsLayers = 1;
1704    bool preferBFrames = true; // we like B-frames as it produces better quality per bitrate
1705    format->setInt32("priority", 0 /* realtime */);
1706    float maxPlaybackFps = mFrameRate; // assume video is only played back at normal speed
1707
1708    if (mCaptureFpsEnable) {
1709        format->setFloat("operating-rate", mCaptureFps);
1710
1711        // enable layering for all time lapse and high frame rate recordings
1712        if (mFrameRate / mCaptureFps >= 1.9) { // time lapse
1713            preferBFrames = false;
1714            tsLayers = 2; // use at least two layers as resulting video will likely be sped up
1715        } else if (mCaptureFps > maxPlaybackFps) { // slow-mo
1716            maxPlaybackFps = mCaptureFps; // assume video will be played back at full capture speed
1717            preferBFrames = false;
1718        }
1719    }
1720
1721    for (uint32_t tryLayers = 1; tryLayers <= kMaxNumVideoTemporalLayers; ++tryLayers) {
1722        if (tryLayers > tsLayers) {
1723            tsLayers = tryLayers;
1724        }
1725        // keep going until the base layer fps falls below the typical display refresh rate
1726        float baseLayerFps = maxPlaybackFps / (1 << (tryLayers - 1));
1727        if (baseLayerFps < kMinTypicalDisplayRefreshingRate / 0.9) {
1728            break;
1729        }
1730    }
1731
1732    if (tsLayers > 1) {
1733        uint32_t bLayers = std::min(2u, tsLayers - 1); // use up-to 2 B-layers
1734        uint32_t pLayers = tsLayers - bLayers;
1735        format->setString(
1736                "ts-schema", AStringPrintf("android.generic.%u+%u", pLayers, bLayers));
1737
1738        // TODO: some encoders do not support B-frames with temporal layering, and we have a
1739        // different preference based on use-case. We could move this into camera profiles.
1740        format->setInt32("android._prefer-b-frames", preferBFrames);
1741    }
1742
1743    if (mMetaDataStoredInVideoBuffers != kMetadataBufferTypeInvalid) {
1744        format->setInt32("android._input-metadata-buffer-type", mMetaDataStoredInVideoBuffers);
1745    }
1746
1747    uint32_t flags = 0;
1748    if (cameraSource == NULL) {
1749        flags |= MediaCodecSource::FLAG_USE_SURFACE_INPUT;
1750    } else {
1751        // require dataspace setup even if not using surface input
1752        format->setInt32("android._using-recorder", 1);
1753    }
1754
1755    sp<MediaCodecSource> encoder = MediaCodecSource::Create(
1756            mLooper, format, cameraSource, mPersistentSurface, flags);
1757    if (encoder == NULL) {
1758        ALOGE("Failed to create video encoder");
1759        // When the encoder fails to be created, we need
1760        // release the camera source due to the camera's lock
1761        // and unlock mechanism.
1762        if (cameraSource != NULL) {
1763            cameraSource->stop();
1764        }
1765        return UNKNOWN_ERROR;
1766    }
1767
1768    if (cameraSource == NULL) {
1769        mGraphicBufferProducer = encoder->getGraphicBufferProducer();
1770    }
1771
1772    *source = encoder;
1773
1774    return OK;
1775}
1776
1777status_t StagefrightRecorder::setupAudioEncoder(const sp<MediaWriter>& writer) {
1778    status_t status = BAD_VALUE;
1779    if (OK != (status = checkAudioEncoderCapabilities())) {
1780        return status;
1781    }
1782
1783    switch(mAudioEncoder) {
1784        case AUDIO_ENCODER_AMR_NB:
1785        case AUDIO_ENCODER_AMR_WB:
1786        case AUDIO_ENCODER_AAC:
1787        case AUDIO_ENCODER_HE_AAC:
1788        case AUDIO_ENCODER_AAC_ELD:
1789            break;
1790
1791        default:
1792            ALOGE("Unsupported audio encoder: %d", mAudioEncoder);
1793            return UNKNOWN_ERROR;
1794    }
1795
1796    sp<MediaCodecSource> audioEncoder = createAudioSource();
1797    if (audioEncoder == NULL) {
1798        return UNKNOWN_ERROR;
1799    }
1800
1801    writer->addSource(audioEncoder);
1802    mAudioEncoderSource = audioEncoder;
1803    return OK;
1804}
1805
1806status_t StagefrightRecorder::setupMPEG4orWEBMRecording() {
1807    mWriter.clear();
1808    mTotalBitRate = 0;
1809
1810    status_t err = OK;
1811    sp<MediaWriter> writer;
1812    sp<MPEG4Writer> mp4writer;
1813    if (mOutputFormat == OUTPUT_FORMAT_WEBM) {
1814        writer = new WebmWriter(mOutputFd);
1815    } else {
1816        writer = mp4writer = new MPEG4Writer(mOutputFd);
1817    }
1818
1819    if (mVideoSource < VIDEO_SOURCE_LIST_END) {
1820        setDefaultVideoEncoderIfNecessary();
1821
1822        sp<MediaSource> mediaSource;
1823        err = setupMediaSource(&mediaSource);
1824        if (err != OK) {
1825            return err;
1826        }
1827
1828        sp<MediaCodecSource> encoder;
1829        err = setupVideoEncoder(mediaSource, &encoder);
1830        if (err != OK) {
1831            return err;
1832        }
1833
1834        writer->addSource(encoder);
1835        mVideoEncoderSource = encoder;
1836        mTotalBitRate += mVideoBitRate;
1837    }
1838
1839    if (mOutputFormat != OUTPUT_FORMAT_WEBM) {
1840        // Audio source is added at the end if it exists.
1841        // This help make sure that the "recoding" sound is suppressed for
1842        // camcorder applications in the recorded files.
1843        // TODO Audio source is currently unsupported for webm output; vorbis encoder needed.
1844        // disable audio for time lapse recording
1845        bool disableAudio = mCaptureFpsEnable && mCaptureFps < mFrameRate;
1846        if (!disableAudio && mAudioSource != AUDIO_SOURCE_CNT) {
1847            err = setupAudioEncoder(writer);
1848            if (err != OK) return err;
1849            mTotalBitRate += mAudioBitRate;
1850        }
1851
1852        if (mCaptureFpsEnable) {
1853            mp4writer->setCaptureRate(mCaptureFps);
1854        }
1855
1856        if (mInterleaveDurationUs > 0) {
1857            mp4writer->setInterleaveDuration(mInterleaveDurationUs);
1858        }
1859        if (mLongitudex10000 > -3600000 && mLatitudex10000 > -3600000) {
1860            mp4writer->setGeoData(mLatitudex10000, mLongitudex10000);
1861        }
1862    }
1863    if (mMaxFileDurationUs != 0) {
1864        writer->setMaxFileDuration(mMaxFileDurationUs);
1865    }
1866    if (mMaxFileSizeBytes != 0) {
1867        writer->setMaxFileSize(mMaxFileSizeBytes);
1868    }
1869    if (mVideoSource == VIDEO_SOURCE_DEFAULT
1870            || mVideoSource == VIDEO_SOURCE_CAMERA) {
1871        mStartTimeOffsetMs = mEncoderProfiles->getStartTimeOffsetMs(mCameraId);
1872    } else if (mVideoSource == VIDEO_SOURCE_SURFACE) {
1873        // surface source doesn't need large initial delay
1874        mStartTimeOffsetMs = 200;
1875    }
1876    if (mStartTimeOffsetMs > 0) {
1877        writer->setStartTimeOffsetMs(mStartTimeOffsetMs);
1878    }
1879
1880    writer->setListener(mListener);
1881    mWriter = writer;
1882    return OK;
1883}
1884
1885void StagefrightRecorder::setupMPEG4orWEBMMetaData(sp<MetaData> *meta) {
1886    int64_t startTimeUs = systemTime() / 1000;
1887    (*meta)->setInt64(kKeyTime, startTimeUs);
1888    (*meta)->setInt32(kKeyFileType, mOutputFormat);
1889    (*meta)->setInt32(kKeyBitRate, mTotalBitRate);
1890    if (mMovieTimeScale > 0) {
1891        (*meta)->setInt32(kKeyTimeScale, mMovieTimeScale);
1892    }
1893    if (mOutputFormat != OUTPUT_FORMAT_WEBM) {
1894        (*meta)->setInt32(kKey64BitFileOffset, mUse64BitFileOffset);
1895        if (mTrackEveryTimeDurationUs > 0) {
1896            (*meta)->setInt64(kKeyTrackTimeStatus, mTrackEveryTimeDurationUs);
1897        }
1898        if (mRotationDegrees != 0) {
1899            (*meta)->setInt32(kKeyRotation, mRotationDegrees);
1900        }
1901    }
1902}
1903
1904status_t StagefrightRecorder::pause() {
1905    ALOGV("pause");
1906    if (!mStarted) {
1907        return INVALID_OPERATION;
1908    }
1909
1910    // Already paused --- no-op.
1911    if (mPauseStartTimeUs != 0) {
1912        return OK;
1913    }
1914
1915    mPauseStartTimeUs = systemTime() / 1000;
1916    sp<MetaData> meta = new MetaData;
1917    meta->setInt64(kKeyTime, mPauseStartTimeUs);
1918
1919    if (mAudioEncoderSource != NULL) {
1920        mAudioEncoderSource->pause();
1921    }
1922    if (mVideoEncoderSource != NULL) {
1923        mVideoEncoderSource->pause(meta.get());
1924    }
1925
1926    return OK;
1927}
1928
1929status_t StagefrightRecorder::resume() {
1930    ALOGV("resume");
1931    if (!mStarted) {
1932        return INVALID_OPERATION;
1933    }
1934
1935    // Not paused --- no-op.
1936    if (mPauseStartTimeUs == 0) {
1937        return OK;
1938    }
1939
1940    int64_t resumeStartTimeUs = systemTime() / 1000;
1941
1942    int64_t bufferStartTimeUs = 0;
1943    bool allSourcesStarted = true;
1944    for (const auto &source : { mAudioEncoderSource, mVideoEncoderSource }) {
1945        if (source == nullptr) {
1946            continue;
1947        }
1948        int64_t timeUs = source->getFirstSampleSystemTimeUs();
1949        if (timeUs < 0) {
1950            allSourcesStarted = false;
1951        }
1952        if (bufferStartTimeUs < timeUs) {
1953            bufferStartTimeUs = timeUs;
1954        }
1955    }
1956
1957    if (allSourcesStarted) {
1958        if (mPauseStartTimeUs < bufferStartTimeUs) {
1959            mPauseStartTimeUs = bufferStartTimeUs;
1960        }
1961        // 30 ms buffer to avoid timestamp overlap
1962        mTotalPausedDurationUs += resumeStartTimeUs - mPauseStartTimeUs - 30000;
1963    }
1964    double timeOffset = -mTotalPausedDurationUs;
1965    if (mCaptureFpsEnable) {
1966        timeOffset *= mCaptureFps / mFrameRate;
1967    }
1968    sp<MetaData> meta = new MetaData;
1969    meta->setInt64(kKeyTime, resumeStartTimeUs);
1970    for (const auto &source : { mAudioEncoderSource, mVideoEncoderSource }) {
1971        if (source == nullptr) {
1972            continue;
1973        }
1974        source->setInputBufferTimeOffset((int64_t)timeOffset);
1975        source->start(meta.get());
1976    }
1977    mPauseStartTimeUs = 0;
1978
1979    return OK;
1980}
1981
1982status_t StagefrightRecorder::stop() {
1983    ALOGV("stop");
1984    Mutex::Autolock autolock(mLock);
1985    status_t err = OK;
1986
1987    if (mCaptureFpsEnable && mCameraSourceTimeLapse != NULL) {
1988        mCameraSourceTimeLapse->startQuickReadReturns();
1989        mCameraSourceTimeLapse = NULL;
1990    }
1991
1992    if (mVideoEncoderSource != NULL) {
1993        int64_t stopTimeUs = systemTime() / 1000;
1994        sp<MetaData> meta = new MetaData;
1995        err = mVideoEncoderSource->setStopStimeUs(stopTimeUs);
1996    }
1997
1998    if (mWriter != NULL) {
1999        err = mWriter->stop();
2000        mWriter.clear();
2001    }
2002
2003    resetMetrics();
2004
2005    mTotalPausedDurationUs = 0;
2006    mPauseStartTimeUs = 0;
2007
2008    mGraphicBufferProducer.clear();
2009    mPersistentSurface.clear();
2010    mAudioEncoderSource.clear();
2011    mVideoEncoderSource.clear();
2012
2013    if (mOutputFd >= 0) {
2014        ::close(mOutputFd);
2015        mOutputFd = -1;
2016    }
2017
2018    if (mStarted) {
2019        mStarted = false;
2020
2021        uint32_t params = 0;
2022        if (mAudioSource != AUDIO_SOURCE_CNT) {
2023            params |= IMediaPlayerService::kBatteryDataTrackAudio;
2024        }
2025        if (mVideoSource != VIDEO_SOURCE_LIST_END) {
2026            params |= IMediaPlayerService::kBatteryDataTrackVideo;
2027        }
2028
2029        addBatteryData(params);
2030    }
2031
2032    return err;
2033}
2034
2035status_t StagefrightRecorder::close() {
2036    ALOGV("close");
2037    stop();
2038
2039    return OK;
2040}
2041
2042status_t StagefrightRecorder::reset() {
2043    ALOGV("reset");
2044    stop();
2045
2046    // No audio or video source by default
2047    mAudioSource = AUDIO_SOURCE_CNT;
2048    mVideoSource = VIDEO_SOURCE_LIST_END;
2049
2050    // Default parameters
2051    mOutputFormat  = OUTPUT_FORMAT_THREE_GPP;
2052    mAudioEncoder  = AUDIO_ENCODER_AMR_NB;
2053    mVideoEncoder  = VIDEO_ENCODER_DEFAULT;
2054    mVideoWidth    = 176;
2055    mVideoHeight   = 144;
2056    mFrameRate     = -1;
2057    mVideoBitRate  = 192000;
2058    mSampleRate    = 8000;
2059    mAudioChannels = 1;
2060    mAudioBitRate  = 12200;
2061    mInterleaveDurationUs = 0;
2062    mIFramesIntervalSec = 1;
2063    mAudioSourceNode = 0;
2064    mUse64BitFileOffset = false;
2065    mMovieTimeScale  = -1;
2066    mAudioTimeScale  = -1;
2067    mVideoTimeScale  = -1;
2068    mCameraId        = 0;
2069    mStartTimeOffsetMs = -1;
2070    mVideoEncoderProfile = -1;
2071    mVideoEncoderLevel   = -1;
2072    mMaxFileDurationUs = 0;
2073    mMaxFileSizeBytes = 0;
2074    mTrackEveryTimeDurationUs = 0;
2075    mCaptureFpsEnable = false;
2076    mCaptureFps = 0.0f;
2077    mTimeBetweenCaptureUs = -1;
2078    mCameraSourceTimeLapse = NULL;
2079    mMetaDataStoredInVideoBuffers = kMetadataBufferTypeInvalid;
2080    mEncoderProfiles = MediaProfiles::getInstance();
2081    mRotationDegrees = 0;
2082    mLatitudex10000 = -3600000;
2083    mLongitudex10000 = -3600000;
2084    mTotalBitRate = 0;
2085
2086    mOutputFd = -1;
2087
2088    return OK;
2089}
2090
2091status_t StagefrightRecorder::getMaxAmplitude(int *max) {
2092    ALOGV("getMaxAmplitude");
2093
2094    if (max == NULL) {
2095        ALOGE("Null pointer argument");
2096        return BAD_VALUE;
2097    }
2098
2099    if (mAudioSourceNode != 0) {
2100        *max = mAudioSourceNode->getMaxAmplitude();
2101    } else {
2102        *max = 0;
2103    }
2104
2105    return OK;
2106}
2107
2108status_t StagefrightRecorder::getMetrics(Parcel *reply) {
2109    ALOGD("StagefrightRecorder::getMetrics");
2110
2111    if (reply == NULL) {
2112        ALOGE("Null pointer argument");
2113        return BAD_VALUE;
2114    }
2115
2116    if (mAnalyticsItem == NULL) {
2117        return UNKNOWN_ERROR;
2118    }
2119
2120    updateMetrics();
2121    mAnalyticsItem->writeToParcel(reply);
2122    return OK;
2123}
2124
2125status_t StagefrightRecorder::dump(
2126        int fd, const Vector<String16>& args) const {
2127    ALOGV("dump");
2128    Mutex::Autolock autolock(mLock);
2129    const size_t SIZE = 256;
2130    char buffer[SIZE];
2131    String8 result;
2132    if (mWriter != 0) {
2133        mWriter->dump(fd, args);
2134    } else {
2135        snprintf(buffer, SIZE, "   No file writer\n");
2136        result.append(buffer);
2137    }
2138    snprintf(buffer, SIZE, "   Recorder: %p\n", this);
2139    snprintf(buffer, SIZE, "   Output file (fd %d):\n", mOutputFd);
2140    result.append(buffer);
2141    snprintf(buffer, SIZE, "     File format: %d\n", mOutputFormat);
2142    result.append(buffer);
2143    snprintf(buffer, SIZE, "     Max file size (bytes): %" PRId64 "\n", mMaxFileSizeBytes);
2144    result.append(buffer);
2145    snprintf(buffer, SIZE, "     Max file duration (us): %" PRId64 "\n", mMaxFileDurationUs);
2146    result.append(buffer);
2147    snprintf(buffer, SIZE, "     File offset length (bits): %d\n", mUse64BitFileOffset? 64: 32);
2148    result.append(buffer);
2149    snprintf(buffer, SIZE, "     Interleave duration (us): %d\n", mInterleaveDurationUs);
2150    result.append(buffer);
2151    snprintf(buffer, SIZE, "     Progress notification: %" PRId64 " us\n", mTrackEveryTimeDurationUs);
2152    result.append(buffer);
2153    snprintf(buffer, SIZE, "   Audio\n");
2154    result.append(buffer);
2155    snprintf(buffer, SIZE, "     Source: %d\n", mAudioSource);
2156    result.append(buffer);
2157    snprintf(buffer, SIZE, "     Encoder: %d\n", mAudioEncoder);
2158    result.append(buffer);
2159    snprintf(buffer, SIZE, "     Bit rate (bps): %d\n", mAudioBitRate);
2160    result.append(buffer);
2161    snprintf(buffer, SIZE, "     Sampling rate (hz): %d\n", mSampleRate);
2162    result.append(buffer);
2163    snprintf(buffer, SIZE, "     Number of channels: %d\n", mAudioChannels);
2164    result.append(buffer);
2165    snprintf(buffer, SIZE, "     Max amplitude: %d\n", mAudioSourceNode == 0? 0: mAudioSourceNode->getMaxAmplitude());
2166    result.append(buffer);
2167    snprintf(buffer, SIZE, "   Video\n");
2168    result.append(buffer);
2169    snprintf(buffer, SIZE, "     Source: %d\n", mVideoSource);
2170    result.append(buffer);
2171    snprintf(buffer, SIZE, "     Camera Id: %d\n", mCameraId);
2172    result.append(buffer);
2173    snprintf(buffer, SIZE, "     Start time offset (ms): %d\n", mStartTimeOffsetMs);
2174    result.append(buffer);
2175    snprintf(buffer, SIZE, "     Encoder: %d\n", mVideoEncoder);
2176    result.append(buffer);
2177    snprintf(buffer, SIZE, "     Encoder profile: %d\n", mVideoEncoderProfile);
2178    result.append(buffer);
2179    snprintf(buffer, SIZE, "     Encoder level: %d\n", mVideoEncoderLevel);
2180    result.append(buffer);
2181    snprintf(buffer, SIZE, "     I frames interval (s): %d\n", mIFramesIntervalSec);
2182    result.append(buffer);
2183    snprintf(buffer, SIZE, "     Frame size (pixels): %dx%d\n", mVideoWidth, mVideoHeight);
2184    result.append(buffer);
2185    snprintf(buffer, SIZE, "     Frame rate (fps): %d\n", mFrameRate);
2186    result.append(buffer);
2187    snprintf(buffer, SIZE, "     Bit rate (bps): %d\n", mVideoBitRate);
2188    result.append(buffer);
2189    ::write(fd, result.string(), result.size());
2190    return OK;
2191}
2192}  // namespace android
2193