StagefrightRecorder.cpp revision 9d7f58a7da8502a4174a17ac49fcba6efa35a457
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "StagefrightRecorder"
19#include <utils/Log.h>
20
21#include "StagefrightRecorder.h"
22
23#include <binder/IPCThreadState.h>
24#include <media/stagefright/AudioSource.h>
25#include <media/stagefright/AMRWriter.h>
26#include <media/stagefright/CameraSource.h>
27#include <media/stagefright/MPEG4Writer.h>
28#include <media/stagefright/MediaDebug.h>
29#include <media/stagefright/MediaDefs.h>
30#include <media/stagefright/MetaData.h>
31#include <media/stagefright/OMXClient.h>
32#include <media/stagefright/OMXCodec.h>
33#include <camera/ICamera.h>
34#include <camera/Camera.h>
35#include <camera/CameraParameters.h>
36#include <surfaceflinger/ISurface.h>
37#include <utils/Errors.h>
38#include <sys/types.h>
39#include <unistd.h>
40#include <ctype.h>
41
42namespace android {
43
44StagefrightRecorder::StagefrightRecorder() {
45    reset();
46}
47
48StagefrightRecorder::~StagefrightRecorder() {
49    stop();
50
51    if (mOutputFd >= 0) {
52        ::close(mOutputFd);
53        mOutputFd = -1;
54    }
55}
56
57status_t StagefrightRecorder::init() {
58    return OK;
59}
60
61status_t StagefrightRecorder::setAudioSource(audio_source as) {
62    mAudioSource = as;
63
64    return OK;
65}
66
67status_t StagefrightRecorder::setVideoSource(video_source vs) {
68    mVideoSource = vs;
69
70    return OK;
71}
72
73status_t StagefrightRecorder::setOutputFormat(output_format of) {
74    mOutputFormat = of;
75
76    return OK;
77}
78
79status_t StagefrightRecorder::setAudioEncoder(audio_encoder ae) {
80    mAudioEncoder = ae;
81
82    return OK;
83}
84
85status_t StagefrightRecorder::setVideoEncoder(video_encoder ve) {
86    mVideoEncoder = ve;
87
88    return OK;
89}
90
91status_t StagefrightRecorder::setVideoSize(int width, int height) {
92    if (width <= 0 || height <= 0) {
93        LOGE("Invalid video size: %dx%d", width, height);
94        return BAD_VALUE;
95    }
96
97    // Additional check on the dimension will be performed later
98    mVideoWidth = width;
99    mVideoHeight = height;
100
101    return OK;
102}
103
104status_t StagefrightRecorder::setVideoFrameRate(int frames_per_second) {
105    if (frames_per_second <= 0 || frames_per_second > 30) {
106        LOGE("Invalid video frame rate: %d", frames_per_second);
107        return BAD_VALUE;
108    }
109
110    // Additional check on the frame rate will be performed later
111    mFrameRate = frames_per_second;
112
113    return OK;
114}
115
116status_t StagefrightRecorder::setCamera(const sp<ICamera> &camera) {
117    LOGV("setCamera");
118    if (camera == 0) {
119        LOGE("camera is NULL");
120        return UNKNOWN_ERROR;
121    }
122
123    int64_t token = IPCThreadState::self()->clearCallingIdentity();
124    mFlags &= ~FLAGS_HOT_CAMERA;
125    mCamera = Camera::create(camera);
126    if (mCamera == 0) {
127        LOGE("Unable to connect to camera");
128        IPCThreadState::self()->restoreCallingIdentity(token);
129        return UNKNOWN_ERROR;
130    }
131
132    LOGV("Connected to camera");
133    if (mCamera->previewEnabled()) {
134        LOGV("camera is hot");
135        mFlags |= FLAGS_HOT_CAMERA;
136    }
137    IPCThreadState::self()->restoreCallingIdentity(token);
138
139    return OK;
140}
141
142status_t StagefrightRecorder::setPreviewSurface(const sp<ISurface> &surface) {
143    mPreviewSurface = surface;
144
145    return OK;
146}
147
148status_t StagefrightRecorder::setOutputFile(const char *path) {
149    // We don't actually support this at all, as the media_server process
150    // no longer has permissions to create files.
151
152    return UNKNOWN_ERROR;
153}
154
155status_t StagefrightRecorder::setOutputFile(int fd, int64_t offset, int64_t length) {
156    // These don't make any sense, do they?
157    CHECK_EQ(offset, 0);
158    CHECK_EQ(length, 0);
159
160    if (mOutputFd >= 0) {
161        ::close(mOutputFd);
162    }
163    mOutputFd = dup(fd);
164
165    return OK;
166}
167
168// Attempt to parse an int64 literal optionally surrounded by whitespace,
169// returns true on success, false otherwise.
170static bool safe_strtoi64(const char *s, int64_t *val) {
171    char *end;
172    *val = strtoll(s, &end, 10);
173
174    if (end == s || errno == ERANGE) {
175        return false;
176    }
177
178    // Skip trailing whitespace
179    while (isspace(*end)) {
180        ++end;
181    }
182
183    // For a successful return, the string must contain nothing but a valid
184    // int64 literal optionally surrounded by whitespace.
185
186    return *end == '\0';
187}
188
189// Return true if the value is in [0, 0x007FFFFFFF]
190static bool safe_strtoi32(const char *s, int32_t *val) {
191    int64_t temp;
192    if (safe_strtoi64(s, &temp)) {
193        if (temp >= 0 && temp <= 0x007FFFFFFF) {
194            *val = static_cast<int32_t>(temp);
195            return true;
196        }
197    }
198    return false;
199}
200
201// Trim both leading and trailing whitespace from the given string.
202static void TrimString(String8 *s) {
203    size_t num_bytes = s->bytes();
204    const char *data = s->string();
205
206    size_t leading_space = 0;
207    while (leading_space < num_bytes && isspace(data[leading_space])) {
208        ++leading_space;
209    }
210
211    size_t i = num_bytes;
212    while (i > leading_space && isspace(data[i - 1])) {
213        --i;
214    }
215
216    s->setTo(String8(&data[leading_space], i - leading_space));
217}
218
219status_t StagefrightRecorder::setParamAudioSamplingRate(int32_t sampleRate) {
220    LOGV("setParamAudioSamplingRate: %d", sampleRate);
221    if (sampleRate <= 0) {
222        LOGE("Invalid audio sampling rate: %d", sampleRate);
223        return BAD_VALUE;
224    }
225
226    // Additional check on the sample rate will be performed later.
227    mSampleRate = sampleRate;
228    return OK;
229}
230
231status_t StagefrightRecorder::setParamAudioNumberOfChannels(int32_t channels) {
232    LOGV("setParamAudioNumberOfChannels: %d", channels);
233    if (channels <= 0 || channels >= 3) {
234        LOGE("Invalid number of audio channels: %d", channels);
235    }
236
237    // Additional check on the number of channels will be performed later.
238    mAudioChannels = channels;
239    return OK;
240}
241
242status_t StagefrightRecorder::setParamAudioEncodingBitRate(int32_t bitRate) {
243    LOGV("setParamAudioEncodingBitRate: %d", bitRate);
244    if (bitRate <= 0) {
245        LOGE("Invalid audio encoding bit rate: %d", bitRate);
246        return BAD_VALUE;
247    }
248
249    // The target bit rate may not be exactly the same as the requested.
250    // It depends on many factors, such as rate control, and the bit rate
251    // range that a specific encoder supports. The mismatch between the
252    // the target and requested bit rate will NOT be treated as an error.
253    mAudioBitRate = bitRate;
254    return OK;
255}
256
257status_t StagefrightRecorder::setParamVideoEncodingBitRate(int32_t bitRate) {
258    LOGV("setParamVideoEncodingBitRate: %d", bitRate);
259    if (bitRate <= 0) {
260        LOGE("Invalid video encoding bit rate: %d", bitRate);
261        return BAD_VALUE;
262    }
263
264    // The target bit rate may not be exactly the same as the requested.
265    // It depends on many factors, such as rate control, and the bit rate
266    // range that a specific encoder supports. The mismatch between the
267    // the target and requested bit rate will NOT be treated as an error.
268    mVideoBitRate = bitRate;
269    return OK;
270}
271
272status_t StagefrightRecorder::setParamMaxDurationOrFileSize(int64_t limit,
273        bool limit_is_duration) {
274    LOGV("setParamMaxDurationOrFileSize: limit (%lld) for %s",
275            limit, limit_is_duration?"duration":"size");
276    if (limit_is_duration) {  // limit is in ms
277        if (limit <= 1000) {  // XXX: 1 second
278            LOGE("Max file duration is too short: %lld us", limit);
279        }
280        mMaxFileDurationUs = limit * 1000LL;
281    } else {
282        if (limit <= 1024) {  // XXX: 1 kB
283            LOGE("Max file size is too small: %lld bytes", limit);
284        }
285        mMaxFileSizeBytes = limit;
286    }
287    return OK;
288}
289
290status_t StagefrightRecorder::setParamInterleaveDuration(int32_t durationUs) {
291    LOGV("setParamInterleaveDuration: %d", durationUs);
292    if (durationUs <= 500000) {           //  500 ms
293        // If interleave duration is too small, it is very inefficient to do
294        // interleaving since the metadata overhead will count for a significant
295        // portion of the saved contents
296        LOGE("Audio/video interleave duration is too small: %d us", durationUs);
297        return BAD_VALUE;
298    } else if (durationUs >= 10000000) {  // 10 seconds
299        // If interleaving duration is too large, it can cause the recording
300        // session to use too much memory since we have to save the output
301        // data before we write them out
302        LOGE("Audio/video interleave duration is too large: %d us", durationUs);
303        return BAD_VALUE;
304    }
305    mInterleaveDurationUs = durationUs;
306    return OK;
307}
308
309// If interval <  0, only the first frame is I frame, and rest are all P frames
310// If interval == 0, all frames are encoded as I frames. No P frames
311// If interval >  0, it is the time spacing between 2 neighboring I frames
312status_t StagefrightRecorder::setParamIFramesInterval(int32_t interval) {
313    LOGV("setParamIFramesInterval: %d seconds", interval);
314    mIFramesInterval = interval;
315    return OK;
316}
317
318status_t StagefrightRecorder::setParameter(
319        const String8 &key, const String8 &value) {
320    LOGV("setParameter: key (%s) => value (%s)", key.string(), value.string());
321    if (key == "max-duration") {
322        int64_t max_duration_ms;
323        if (safe_strtoi64(value.string(), &max_duration_ms)) {
324            return setParamMaxDurationOrFileSize(
325                    max_duration_ms, true /* limit_is_duration */);
326        }
327    } else if (key == "max-filesize") {
328        int64_t max_filesize_bytes;
329        if (safe_strtoi64(value.string(), &max_filesize_bytes)) {
330            return setParamMaxDurationOrFileSize(
331                    max_filesize_bytes, false /* limit is filesize */);
332        }
333    } else if (key == "audio-param-sampling-rate") {
334        int32_t sampling_rate;
335        if (safe_strtoi32(value.string(), &sampling_rate)) {
336            return setParamAudioSamplingRate(sampling_rate);
337        }
338    } else if (key == "audio-param-number-of-channels") {
339        int32_t number_of_channels;
340        if (safe_strtoi32(value.string(), &number_of_channels)) {
341            return setParamAudioNumberOfChannels(number_of_channels);
342        }
343    } else if (key == "audio-param-encoding-bitrate") {
344        int32_t audio_bitrate;
345        if (safe_strtoi32(value.string(), &audio_bitrate)) {
346            return setParamAudioEncodingBitRate(audio_bitrate);
347        }
348    } else if (key == "video-param-encoding-bitrate") {
349        int32_t video_bitrate;
350        if (safe_strtoi32(value.string(), &video_bitrate)) {
351            return setParamVideoEncodingBitRate(video_bitrate);
352        }
353    } else if (key == "param-interleave-duration-us") {
354        int32_t durationUs;
355        if (safe_strtoi32(value.string(), &durationUs)) {
356            return setParamInterleaveDuration(durationUs);
357        }
358    } else if (key == "param-i-frames-interval") {
359        int32_t interval;
360        if (safe_strtoi32(value.string(), &interval)) {
361            return setParamIFramesInterval(interval);
362        }
363    } else {
364        LOGE("setParameter: failed to find key %s", key.string());
365    }
366    return BAD_VALUE;
367}
368
369status_t StagefrightRecorder::setParameters(const String8 &params) {
370    LOGV("setParameters: %s", params.string());
371    const char *cparams = params.string();
372    const char *key_start = cparams;
373    for (;;) {
374        const char *equal_pos = strchr(key_start, '=');
375        if (equal_pos == NULL) {
376            LOGE("Parameters %s miss a value", cparams);
377            return BAD_VALUE;
378        }
379        String8 key(key_start, equal_pos - key_start);
380        TrimString(&key);
381        if (key.length() == 0) {
382            LOGE("Parameters %s contains an empty key", cparams);
383            return BAD_VALUE;
384        }
385        const char *value_start = equal_pos + 1;
386        const char *semicolon_pos = strchr(value_start, ';');
387        String8 value;
388        if (semicolon_pos == NULL) {
389            value.setTo(value_start);
390        } else {
391            value.setTo(value_start, semicolon_pos - value_start);
392        }
393        if (setParameter(key, value) != OK) {
394            return BAD_VALUE;
395        }
396        if (semicolon_pos == NULL) {
397            break;  // Reaches the end
398        }
399        key_start = semicolon_pos + 1;
400    }
401    return OK;
402}
403
404status_t StagefrightRecorder::setListener(const sp<IMediaPlayerClient> &listener) {
405    mListener = listener;
406
407    return OK;
408}
409
410status_t StagefrightRecorder::prepare() {
411    return OK;
412}
413
414status_t StagefrightRecorder::start() {
415    if (mWriter != NULL) {
416        return UNKNOWN_ERROR;
417    }
418
419    switch (mOutputFormat) {
420        case OUTPUT_FORMAT_DEFAULT:
421        case OUTPUT_FORMAT_THREE_GPP:
422        case OUTPUT_FORMAT_MPEG_4:
423            return startMPEG4Recording();
424
425        case OUTPUT_FORMAT_AMR_NB:
426        case OUTPUT_FORMAT_AMR_WB:
427            return startAMRRecording();
428
429        case OUTPUT_FORMAT_AAC_ADIF:
430        case OUTPUT_FORMAT_AAC_ADTS:
431            return startAACRecording();
432
433        default:
434            return UNKNOWN_ERROR;
435    }
436}
437
438sp<MediaSource> StagefrightRecorder::createAudioSource() {
439    sp<AudioSource> audioSource =
440        new AudioSource(
441                mAudioSource,
442                mSampleRate,
443                AudioSystem::CHANNEL_IN_MONO);
444
445    status_t err = audioSource->initCheck();
446
447    if (err != OK) {
448        LOGE("audio source is not initialized");
449        return NULL;
450    }
451
452    sp<MetaData> encMeta = new MetaData;
453    const char *mime;
454    switch (mAudioEncoder) {
455        case AUDIO_ENCODER_AMR_NB:
456        case AUDIO_ENCODER_DEFAULT:
457            mime = MEDIA_MIMETYPE_AUDIO_AMR_NB;
458            break;
459        case AUDIO_ENCODER_AMR_WB:
460            mime = MEDIA_MIMETYPE_AUDIO_AMR_WB;
461            break;
462        case AUDIO_ENCODER_AAC:
463            mime = MEDIA_MIMETYPE_AUDIO_AAC;
464            break;
465        default:
466            LOGE("Unknown audio encoder: %d", mAudioEncoder);
467            return NULL;
468    }
469    encMeta->setCString(kKeyMIMEType, mime);
470
471    int32_t maxInputSize;
472    CHECK(audioSource->getFormat()->findInt32(
473                kKeyMaxInputSize, &maxInputSize));
474
475    encMeta->setInt32(kKeyMaxInputSize, maxInputSize);
476    encMeta->setInt32(kKeyChannelCount, mAudioChannels);
477    encMeta->setInt32(kKeySampleRate, mSampleRate);
478    encMeta->setInt32(kKeyBitRate, mAudioBitRate);
479
480    OMXClient client;
481    CHECK_EQ(client.connect(), OK);
482
483    sp<MediaSource> audioEncoder =
484        OMXCodec::Create(client.interface(), encMeta,
485                         true /* createEncoder */, audioSource);
486
487    return audioEncoder;
488}
489
490status_t StagefrightRecorder::startAACRecording() {
491    CHECK(mOutputFormat == OUTPUT_FORMAT_AAC_ADIF ||
492          mOutputFormat == OUTPUT_FORMAT_AAC_ADTS);
493
494    CHECK(mAudioEncoder == AUDIO_ENCODER_AAC);
495    CHECK(mAudioSource != AUDIO_SOURCE_LIST_END);
496    CHECK(mOutputFd >= 0);
497
498    CHECK(0 == "AACWriter is not implemented yet");
499
500    return OK;
501}
502
503status_t StagefrightRecorder::startAMRRecording() {
504    CHECK(mOutputFormat == OUTPUT_FORMAT_AMR_NB ||
505          mOutputFormat == OUTPUT_FORMAT_AMR_WB);
506
507    if (mOutputFormat == OUTPUT_FORMAT_AMR_NB) {
508        if (mAudioEncoder != AUDIO_ENCODER_DEFAULT &&
509            mAudioEncoder != AUDIO_ENCODER_AMR_NB) {
510            LOGE("Invalid encoder %d used for AMRNB recording",
511                    mAudioEncoder);
512            return UNKNOWN_ERROR;
513        }
514        if (mSampleRate != 8000) {
515            LOGE("Invalid sampling rate %d used for AMRNB recording",
516                    mSampleRate);
517            return UNKNOWN_ERROR;
518        }
519    } else {  // mOutputFormat must be OUTPUT_FORMAT_AMR_WB
520        if (mAudioEncoder != AUDIO_ENCODER_AMR_WB) {
521            LOGE("Invlaid encoder %d used for AMRWB recording",
522                    mAudioEncoder);
523            return UNKNOWN_ERROR;
524        }
525        if (mSampleRate != 16000) {
526            LOGE("Invalid sample rate %d used for AMRWB recording",
527                    mSampleRate);
528            return UNKNOWN_ERROR;
529        }
530    }
531    if (mAudioChannels != 1) {
532        LOGE("Invalid number of audio channels %d used for amr recording",
533                mAudioChannels);
534        return UNKNOWN_ERROR;
535    }
536
537    if (mAudioSource >= AUDIO_SOURCE_LIST_END) {
538        LOGE("Invalid audio source: %d", mAudioSource);
539        return UNKNOWN_ERROR;
540    }
541
542    sp<MediaSource> audioEncoder = createAudioSource();
543
544    if (audioEncoder == NULL) {
545        return UNKNOWN_ERROR;
546    }
547
548    CHECK(mOutputFd >= 0);
549    mWriter = new AMRWriter(dup(mOutputFd));
550    mWriter->addSource(audioEncoder);
551
552    if (mMaxFileDurationUs != 0) {
553        mWriter->setMaxFileDuration(mMaxFileDurationUs);
554    }
555    if (mMaxFileSizeBytes != 0) {
556        mWriter->setMaxFileSize(mMaxFileSizeBytes);
557    }
558    mWriter->setListener(mListener);
559    mWriter->start();
560
561    return OK;
562}
563
564status_t StagefrightRecorder::startMPEG4Recording() {
565    mWriter = new MPEG4Writer(dup(mOutputFd));
566
567    // Add audio source first if it exists
568    if (mAudioSource != AUDIO_SOURCE_LIST_END) {
569        sp<MediaSource> audioEncoder;
570        switch(mAudioEncoder) {
571            case AUDIO_ENCODER_AMR_NB:
572            case AUDIO_ENCODER_AMR_WB:
573            case AUDIO_ENCODER_AAC:
574                audioEncoder = createAudioSource();
575                break;
576            default:
577                LOGE("Unsupported audio encoder: %d", mAudioEncoder);
578                return UNKNOWN_ERROR;
579        }
580
581        if (audioEncoder == NULL) {
582            return UNKNOWN_ERROR;
583        }
584
585        mWriter->addSource(audioEncoder);
586    }
587    if (mVideoSource == VIDEO_SOURCE_DEFAULT
588            || mVideoSource == VIDEO_SOURCE_CAMERA) {
589
590        int64_t token = IPCThreadState::self()->clearCallingIdentity();
591        if (mCamera == 0) {
592            mCamera = Camera::connect(0);
593            mCamera->lock();
594        }
595
596        // Set the actual video recording frame size
597        CameraParameters params(mCamera->getParameters());
598        params.setPreviewSize(mVideoWidth, mVideoHeight);
599        params.setPreviewFrameRate(mFrameRate);
600        String8 s = params.flatten();
601        CHECK_EQ(OK, mCamera->setParameters(s));
602        CameraParameters newCameraParams(mCamera->getParameters());
603
604        // Check on video frame size
605        int frameWidth = 0, frameHeight = 0;
606        newCameraParams.getPreviewSize(&frameWidth, &frameHeight);
607        if (frameWidth  < 0 || frameWidth  != mVideoWidth ||
608            frameHeight < 0 || frameHeight != mVideoHeight) {
609            LOGE("Failed to set the video frame size to %dx%d",
610                    mVideoWidth, mVideoHeight);
611            IPCThreadState::self()->restoreCallingIdentity(token);
612            return UNKNOWN_ERROR;
613        }
614
615        // Check on video frame rate
616        int frameRate = newCameraParams.getPreviewFrameRate();
617        if (frameRate < 0 || (frameRate - mFrameRate) != 0) {
618            LOGE("Failed to set frame rate to %d fps. The actual "
619                 "frame rate is %d", mFrameRate, frameRate);
620        }
621
622        CHECK_EQ(OK, mCamera->setPreviewDisplay(mPreviewSurface));
623        IPCThreadState::self()->restoreCallingIdentity(token);
624
625        sp<CameraSource> cameraSource =
626            CameraSource::CreateFromCamera(mCamera);
627
628        CHECK(cameraSource != NULL);
629
630        sp<MetaData> enc_meta = new MetaData;
631        enc_meta->setInt32(kKeyBitRate, mVideoBitRate);
632        enc_meta->setInt32(kKeySampleRate, mFrameRate);  // XXX: kKeySampleRate?
633
634        switch (mVideoEncoder) {
635            case VIDEO_ENCODER_H263:
636                enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
637                break;
638
639            case VIDEO_ENCODER_MPEG_4_SP:
640                enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
641                break;
642
643            case VIDEO_ENCODER_H264:
644                enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
645                break;
646
647            default:
648                CHECK(!"Should not be here, unsupported video encoding.");
649                break;
650        }
651
652        sp<MetaData> meta = cameraSource->getFormat();
653
654        int32_t width, height, stride, sliceHeight;
655        CHECK(meta->findInt32(kKeyWidth, &width));
656        CHECK(meta->findInt32(kKeyHeight, &height));
657        CHECK(meta->findInt32(kKeyStride, &stride));
658        CHECK(meta->findInt32(kKeySliceHeight, &sliceHeight));
659
660        enc_meta->setInt32(kKeyWidth, width);
661        enc_meta->setInt32(kKeyHeight, height);
662        enc_meta->setInt32(kKeyIFramesInterval, mIFramesInterval);
663        enc_meta->setInt32(kKeyStride, stride);
664        enc_meta->setInt32(kKeySliceHeight, sliceHeight);
665
666        OMXClient client;
667        CHECK_EQ(client.connect(), OK);
668
669        sp<MediaSource> encoder =
670            OMXCodec::Create(
671                    client.interface(), enc_meta,
672                    true /* createEncoder */, cameraSource);
673
674        CHECK(mOutputFd >= 0);
675        mWriter->addSource(encoder);
676    }
677
678    {
679        // MPEGWriter specific handling
680        MPEG4Writer *writer = ((MPEG4Writer *) mWriter.get());  // mWriter is an MPEGWriter
681        writer->setInterleaveDuration(mInterleaveDurationUs);
682    }
683
684    if (mMaxFileDurationUs != 0) {
685        mWriter->setMaxFileDuration(mMaxFileDurationUs);
686    }
687    if (mMaxFileSizeBytes != 0) {
688        mWriter->setMaxFileSize(mMaxFileSizeBytes);
689    }
690    mWriter->setListener(mListener);
691    mWriter->start();
692    return OK;
693}
694
695status_t StagefrightRecorder::stop() {
696    if (mWriter == NULL) {
697        return UNKNOWN_ERROR;
698    }
699
700    mWriter->stop();
701    mWriter = NULL;
702
703    return OK;
704}
705
706status_t StagefrightRecorder::close() {
707    stop();
708
709    if (mCamera != 0) {
710        int64_t token = IPCThreadState::self()->clearCallingIdentity();
711        if ((mFlags & FLAGS_HOT_CAMERA) == 0) {
712            LOGV("Camera was cold when we started, stopping preview");
713            mCamera->stopPreview();
714        }
715        mCamera->unlock();
716        mCamera = NULL;
717        IPCThreadState::self()->restoreCallingIdentity(token);
718        mFlags = 0;
719    }
720    return OK;
721}
722
723status_t StagefrightRecorder::reset() {
724    stop();
725
726    // No audio or video source by default
727    mAudioSource = AUDIO_SOURCE_LIST_END;
728    mVideoSource = VIDEO_SOURCE_LIST_END;
729
730    // Default parameters
731    mOutputFormat  = OUTPUT_FORMAT_THREE_GPP;
732    mAudioEncoder  = AUDIO_ENCODER_AMR_NB;
733    mVideoEncoder  = VIDEO_ENCODER_H263;
734    mVideoWidth    = 176;
735    mVideoHeight   = 144;
736    mFrameRate     = 20;
737    mVideoBitRate  = 192000;
738    mSampleRate    = 8000;
739    mAudioChannels = 1;
740    mAudioBitRate  = 12200;
741    mInterleaveDurationUs = 0;
742    mIFramesInterval = 1;
743
744    mOutputFd = -1;
745    mFlags = 0;
746
747    return OK;
748}
749
750status_t StagefrightRecorder::getMaxAmplitude(int *max) {
751    *max = 0;
752
753    return OK;
754}
755
756}  // namespace android
757