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