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