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