michael@0: /* michael@0: * Copyright (C) 2009 The Android Open Source Project michael@0: * Copyright (C) 2013 Mozilla Foundation michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #include "nsDebug.h" michael@0: #define DOM_CAMERA_LOG_LEVEL 3 michael@0: #include "CameraCommon.h" michael@0: #include "GonkCameraSource.h" michael@0: #include "GonkRecorder.h" michael@0: michael@0: #define RE_LOGD(fmt, ...) DOM_CAMERA_LOGA("[%s:%d]" fmt,__FILE__,__LINE__, ## __VA_ARGS__) michael@0: #define RE_LOGV(fmt, ...) DOM_CAMERA_LOGI("[%s:%d]" fmt,__FILE__,__LINE__, ## __VA_ARGS__) michael@0: #define RE_LOGI(fmt, ...) DOM_CAMERA_LOGI("[%s:%d]" fmt,__FILE__,__LINE__, ## __VA_ARGS__) michael@0: #define RE_LOGW(fmt, ...) DOM_CAMERA_LOGW("[%s:%d]" fmt,__FILE__,__LINE__, ## __VA_ARGS__) michael@0: #define RE_LOGE(fmt, ...) DOM_CAMERA_LOGE("[%s:%d]" fmt,__FILE__,__LINE__, ## __VA_ARGS__) michael@0: michael@0: #include michael@0: #if defined(MOZ_WIDGET_GONK) && ANDROID_VERSION >= 17 michael@0: # include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #define RES_720P (720 * 1280) michael@0: namespace android { michael@0: michael@0: GonkRecorder::GonkRecorder() michael@0: : mWriter(NULL), michael@0: mOutputFd(-1), michael@0: mAudioSource(AUDIO_SOURCE_CNT), michael@0: mVideoSource(VIDEO_SOURCE_LIST_END), michael@0: mStarted(false) { michael@0: michael@0: RE_LOGV("Constructor"); michael@0: reset(); michael@0: } michael@0: michael@0: GonkRecorder::~GonkRecorder() { michael@0: RE_LOGV("Destructor"); michael@0: stop(); michael@0: } michael@0: michael@0: status_t GonkRecorder::init() { michael@0: RE_LOGV("init"); michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setAudioSource(audio_source_t as) { michael@0: RE_LOGV("setAudioSource: %d", as); michael@0: if (as < AUDIO_SOURCE_DEFAULT || michael@0: as >= AUDIO_SOURCE_CNT) { michael@0: RE_LOGE("Invalid audio source: %d", as); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: if (as == AUDIO_SOURCE_DEFAULT) { michael@0: mAudioSource = AUDIO_SOURCE_MIC; michael@0: } else { michael@0: mAudioSource = as; michael@0: } michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setVideoSource(video_source vs) { michael@0: RE_LOGV("setVideoSource: %d", vs); michael@0: if (vs < VIDEO_SOURCE_DEFAULT || michael@0: vs >= VIDEO_SOURCE_LIST_END) { michael@0: RE_LOGE("Invalid video source: %d", vs); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: if (vs == VIDEO_SOURCE_DEFAULT) { michael@0: mVideoSource = VIDEO_SOURCE_CAMERA; michael@0: } else { michael@0: mVideoSource = vs; michael@0: } michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setOutputFormat(output_format of) { michael@0: RE_LOGV("setOutputFormat: %d", of); michael@0: if (of < OUTPUT_FORMAT_DEFAULT || michael@0: of >= OUTPUT_FORMAT_LIST_END) { michael@0: RE_LOGE("Invalid output format: %d", of); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: if (of == OUTPUT_FORMAT_DEFAULT) { michael@0: mOutputFormat = OUTPUT_FORMAT_THREE_GPP; michael@0: } else { michael@0: mOutputFormat = of; michael@0: } michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setAudioEncoder(audio_encoder ae) { michael@0: RE_LOGV("setAudioEncoder: %d", ae); michael@0: if (ae < AUDIO_ENCODER_DEFAULT || michael@0: ae >= AUDIO_ENCODER_LIST_END) { michael@0: RE_LOGE("Invalid audio encoder: %d", ae); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: if (ae == AUDIO_ENCODER_DEFAULT) { michael@0: mAudioEncoder = AUDIO_ENCODER_AMR_NB; michael@0: } else { michael@0: mAudioEncoder = ae; michael@0: } michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setVideoEncoder(video_encoder ve) { michael@0: RE_LOGV("setVideoEncoder: %d", ve); michael@0: if (ve < VIDEO_ENCODER_DEFAULT || michael@0: ve >= VIDEO_ENCODER_LIST_END) { michael@0: RE_LOGE("Invalid video encoder: %d", ve); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: if (ve == VIDEO_ENCODER_DEFAULT) { michael@0: mVideoEncoder = VIDEO_ENCODER_H263; michael@0: } else { michael@0: mVideoEncoder = ve; michael@0: } michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setVideoSize(int width, int height) { michael@0: RE_LOGV("setVideoSize: %dx%d", width, height); michael@0: if (width <= 0 || height <= 0) { michael@0: RE_LOGE("Invalid video size: %dx%d", width, height); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: // Additional check on the dimension will be performed later michael@0: mVideoWidth = width; michael@0: mVideoHeight = height; michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setVideoFrameRate(int frames_per_second) { michael@0: RE_LOGV("setVideoFrameRate: %d", frames_per_second); michael@0: if ((frames_per_second <= 0 && frames_per_second != -1) || michael@0: frames_per_second > 120) { michael@0: RE_LOGE("Invalid video frame rate: %d", frames_per_second); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: // Additional check on the frame rate will be performed later michael@0: mFrameRate = frames_per_second; michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setOutputFile(const char *path) { michael@0: RE_LOGE("setOutputFile(const char*) must not be called"); michael@0: // We don't actually support this at all, as the media_server process michael@0: // no longer has permissions to create files. michael@0: michael@0: return -EPERM; michael@0: } michael@0: michael@0: status_t GonkRecorder::setOutputFile(int fd, int64_t offset, int64_t length) { michael@0: RE_LOGV("setOutputFile: %d, %lld, %lld", fd, offset, length); michael@0: // These don't make any sense, do they? michael@0: CHECK_EQ(offset, 0ll); michael@0: CHECK_EQ(length, 0ll); michael@0: michael@0: if (fd < 0) { michael@0: RE_LOGE("Invalid file descriptor: %d", fd); michael@0: return -EBADF; michael@0: } michael@0: michael@0: if (mOutputFd >= 0) { michael@0: ::close(mOutputFd); michael@0: } michael@0: mOutputFd = dup(fd); michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: // Attempt to parse an int64 literal optionally surrounded by whitespace, michael@0: // returns true on success, false otherwise. michael@0: static bool safe_strtoi64(const char *s, int64_t *val) { michael@0: char *end; michael@0: michael@0: // It is lame, but according to man page, we have to set errno to 0 michael@0: // before calling strtoll(). michael@0: errno = 0; michael@0: *val = strtoll(s, &end, 10); michael@0: michael@0: if (end == s || errno == ERANGE) { michael@0: return false; michael@0: } michael@0: michael@0: // Skip trailing whitespace michael@0: while (isspace(*end)) { michael@0: ++end; michael@0: } michael@0: michael@0: // For a successful return, the string must contain nothing but a valid michael@0: // int64 literal optionally surrounded by whitespace. michael@0: michael@0: return *end == '\0'; michael@0: } michael@0: michael@0: // Return true if the value is in [0, 0x007FFFFFFF] michael@0: static bool safe_strtoi32(const char *s, int32_t *val) { michael@0: int64_t temp; michael@0: if (safe_strtoi64(s, &temp)) { michael@0: if (temp >= 0 && temp <= 0x007FFFFFFF) { michael@0: *val = static_cast(temp); michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: // Trim both leading and trailing whitespace from the given string. michael@0: static void TrimString(String8 *s) { michael@0: size_t num_bytes = s->bytes(); michael@0: const char *data = s->string(); michael@0: michael@0: size_t leading_space = 0; michael@0: while (leading_space < num_bytes && isspace(data[leading_space])) { michael@0: ++leading_space; michael@0: } michael@0: michael@0: size_t i = num_bytes; michael@0: while (i > leading_space && isspace(data[i - 1])) { michael@0: --i; michael@0: } michael@0: michael@0: s->setTo(String8(&data[leading_space], i - leading_space)); michael@0: } michael@0: michael@0: status_t GonkRecorder::setParamAudioSamplingRate(int32_t sampleRate) { michael@0: RE_LOGV("setParamAudioSamplingRate: %d", sampleRate); michael@0: if (sampleRate <= 0) { michael@0: RE_LOGE("Invalid audio sampling rate: %d", sampleRate); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: // Additional check on the sample rate will be performed later. michael@0: mSampleRate = sampleRate; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParamAudioNumberOfChannels(int32_t channels) { michael@0: RE_LOGV("setParamAudioNumberOfChannels: %d", channels); michael@0: if (channels <= 0 || channels >= 3) { michael@0: RE_LOGE("Invalid number of audio channels: %d", channels); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: // Additional check on the number of channels will be performed later. michael@0: mAudioChannels = channels; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParamAudioEncodingBitRate(int32_t bitRate) { michael@0: RE_LOGV("setParamAudioEncodingBitRate: %d", bitRate); michael@0: if (bitRate <= 0) { michael@0: RE_LOGE("Invalid audio encoding bit rate: %d", bitRate); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: // The target bit rate may not be exactly the same as the requested. michael@0: // It depends on many factors, such as rate control, and the bit rate michael@0: // range that a specific encoder supports. The mismatch between the michael@0: // the target and requested bit rate will NOT be treated as an error. michael@0: mAudioBitRate = bitRate; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParamVideoEncodingBitRate(int32_t bitRate) { michael@0: RE_LOGV("setParamVideoEncodingBitRate: %d", bitRate); michael@0: if (bitRate <= 0) { michael@0: RE_LOGE("Invalid video encoding bit rate: %d", bitRate); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: // The target bit rate may not be exactly the same as the requested. michael@0: // It depends on many factors, such as rate control, and the bit rate michael@0: // range that a specific encoder supports. The mismatch between the michael@0: // the target and requested bit rate will NOT be treated as an error. michael@0: mVideoBitRate = bitRate; michael@0: return OK; michael@0: } michael@0: michael@0: // Always rotate clockwise, and only support 0, 90, 180 and 270 for now. michael@0: status_t GonkRecorder::setParamVideoRotation(int32_t degrees) { michael@0: RE_LOGV("setParamVideoRotation: %d", degrees); michael@0: if (degrees < 0 || degrees % 90 != 0) { michael@0: RE_LOGE("Unsupported video rotation angle: %d", degrees); michael@0: return BAD_VALUE; michael@0: } michael@0: mRotationDegrees = degrees % 360; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParamMaxFileDurationUs(int64_t timeUs) { michael@0: RE_LOGV("setParamMaxFileDurationUs: %lld us", timeUs); michael@0: michael@0: // This is meant for backward compatibility for MediaRecorder.java michael@0: if (timeUs <= 0) { michael@0: RE_LOGW("Max file duration is not positive: %lld us. Disabling duration limit.", timeUs); michael@0: timeUs = 0; // Disable the duration limit for zero or negative values. michael@0: } else if (timeUs <= 100000LL) { // XXX: 100 milli-seconds michael@0: RE_LOGE("Max file duration is too short: %lld us", timeUs); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: if (timeUs <= 15 * 1000000LL) { michael@0: RE_LOGW("Target duration (%lld us) too short to be respected", timeUs); michael@0: } michael@0: mMaxFileDurationUs = timeUs; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParamMaxFileSizeBytes(int64_t bytes) { michael@0: RE_LOGV("setParamMaxFileSizeBytes: %lld bytes", bytes); michael@0: michael@0: // This is meant for backward compatibility for MediaRecorder.java michael@0: if (bytes <= 0) { michael@0: RE_LOGW("Max file size is not positive: %lld bytes. " michael@0: "Disabling file size limit.", bytes); michael@0: bytes = 0; // Disable the file size limit for zero or negative values. michael@0: } else if (bytes <= 1024) { // XXX: 1 kB michael@0: RE_LOGE("Max file size is too small: %lld bytes", bytes); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: if (bytes <= 100 * 1024) { michael@0: RE_LOGW("Target file size (%lld bytes) is too small to be respected", bytes); michael@0: } michael@0: michael@0: if (bytes >= 0xffffffffLL) { michael@0: RE_LOGW("Target file size (%lld bytes) too large to be respected, clipping to 4GB", bytes); michael@0: bytes = 0xffffffffLL; michael@0: } michael@0: michael@0: mMaxFileSizeBytes = bytes; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParamInterleaveDuration(int32_t durationUs) { michael@0: RE_LOGV("setParamInterleaveDuration: %d", durationUs); michael@0: if (durationUs <= 500000) { // 500 ms michael@0: // If interleave duration is too small, it is very inefficient to do michael@0: // interleaving since the metadata overhead will count for a significant michael@0: // portion of the saved contents michael@0: RE_LOGE("Audio/video interleave duration is too small: %d us", durationUs); michael@0: return BAD_VALUE; michael@0: } else if (durationUs >= 10000000) { // 10 seconds michael@0: // If interleaving duration is too large, it can cause the recording michael@0: // session to use too much memory since we have to save the output michael@0: // data before we write them out michael@0: RE_LOGE("Audio/video interleave duration is too large: %d us", durationUs); michael@0: return BAD_VALUE; michael@0: } michael@0: mInterleaveDurationUs = durationUs; michael@0: return OK; michael@0: } michael@0: michael@0: // If seconds < 0, only the first frame is I frame, and rest are all P frames michael@0: // If seconds == 0, all frames are encoded as I frames. No P frames michael@0: // If seconds > 0, it is the time spacing (seconds) between 2 neighboring I frames michael@0: status_t GonkRecorder::setParamVideoIFramesInterval(int32_t seconds) { michael@0: RE_LOGV("setParamVideoIFramesInterval: %d seconds", seconds); michael@0: mIFramesIntervalSec = seconds; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParam64BitFileOffset(bool use64Bit) { michael@0: RE_LOGV("setParam64BitFileOffset: %s", michael@0: use64Bit? "use 64 bit file offset": "use 32 bit file offset"); michael@0: mUse64BitFileOffset = use64Bit; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParamVideoCameraId(int32_t cameraId) { michael@0: RE_LOGV("setParamVideoCameraId: %d", cameraId); michael@0: if (cameraId < 0) { michael@0: return BAD_VALUE; michael@0: } michael@0: mCameraId = cameraId; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParamTrackTimeStatus(int64_t timeDurationUs) { michael@0: RE_LOGV("setParamTrackTimeStatus: %lld", timeDurationUs); michael@0: if (timeDurationUs < 20000) { // Infeasible if shorter than 20 ms? michael@0: RE_LOGE("Tracking time duration too short: %lld us", timeDurationUs); michael@0: return BAD_VALUE; michael@0: } michael@0: mTrackEveryTimeDurationUs = timeDurationUs; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParamVideoEncoderProfile(int32_t profile) { michael@0: RE_LOGV("setParamVideoEncoderProfile: %d", profile); michael@0: michael@0: // Additional check will be done later when we load the encoder. michael@0: // For now, we are accepting values defined in OpenMAX IL. michael@0: mVideoEncoderProfile = profile; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParamVideoEncoderLevel(int32_t level) { michael@0: RE_LOGV("setParamVideoEncoderLevel: %d", level); michael@0: michael@0: // Additional check will be done later when we load the encoder. michael@0: // For now, we are accepting values defined in OpenMAX IL. michael@0: mVideoEncoderLevel = level; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParamMovieTimeScale(int32_t timeScale) { michael@0: RE_LOGV("setParamMovieTimeScale: %d", timeScale); michael@0: michael@0: // The range is set to be the same as the audio's time scale range michael@0: // since audio's time scale has a wider range. michael@0: if (timeScale < 600 || timeScale > 96000) { michael@0: RE_LOGE("Time scale (%d) for movie is out of range [600, 96000]", timeScale); michael@0: return BAD_VALUE; michael@0: } michael@0: mMovieTimeScale = timeScale; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParamVideoTimeScale(int32_t timeScale) { michael@0: RE_LOGV("setParamVideoTimeScale: %d", timeScale); michael@0: michael@0: // 60000 is chosen to make sure that each video frame from a 60-fps michael@0: // video has 1000 ticks. michael@0: if (timeScale < 600 || timeScale > 60000) { michael@0: RE_LOGE("Time scale (%d) for video is out of range [600, 60000]", timeScale); michael@0: return BAD_VALUE; michael@0: } michael@0: mVideoTimeScale = timeScale; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParamAudioTimeScale(int32_t timeScale) { michael@0: RE_LOGV("setParamAudioTimeScale: %d", timeScale); michael@0: michael@0: // 96000 Hz is the highest sampling rate support in AAC. michael@0: if (timeScale < 600 || timeScale > 96000) { michael@0: RE_LOGE("Time scale (%d) for audio is out of range [600, 96000]", timeScale); michael@0: return BAD_VALUE; michael@0: } michael@0: mAudioTimeScale = timeScale; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParamGeoDataLongitude( michael@0: int64_t longitudex10000) { michael@0: michael@0: if (longitudex10000 > 1800000 || longitudex10000 < -1800000) { michael@0: return BAD_VALUE; michael@0: } michael@0: mLongitudex10000 = longitudex10000; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParamGeoDataLatitude( michael@0: int64_t latitudex10000) { michael@0: michael@0: if (latitudex10000 > 900000 || latitudex10000 < -900000) { michael@0: return BAD_VALUE; michael@0: } michael@0: mLatitudex10000 = latitudex10000; michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParameter( michael@0: const String8 &key, const String8 &value) { michael@0: RE_LOGV("setParameter: key (%s) => value (%s)", key.string(), value.string()); michael@0: if (key == "max-duration") { michael@0: int64_t max_duration_ms; michael@0: if (safe_strtoi64(value.string(), &max_duration_ms)) { michael@0: return setParamMaxFileDurationUs(1000LL * max_duration_ms); michael@0: } michael@0: } else if (key == "max-filesize") { michael@0: int64_t max_filesize_bytes; michael@0: if (safe_strtoi64(value.string(), &max_filesize_bytes)) { michael@0: return setParamMaxFileSizeBytes(max_filesize_bytes); michael@0: } michael@0: } else if (key == "interleave-duration-us") { michael@0: int32_t durationUs; michael@0: if (safe_strtoi32(value.string(), &durationUs)) { michael@0: return setParamInterleaveDuration(durationUs); michael@0: } michael@0: } else if (key == "param-movie-time-scale") { michael@0: int32_t timeScale; michael@0: if (safe_strtoi32(value.string(), &timeScale)) { michael@0: return setParamMovieTimeScale(timeScale); michael@0: } michael@0: } else if (key == "param-use-64bit-offset") { michael@0: int32_t use64BitOffset; michael@0: if (safe_strtoi32(value.string(), &use64BitOffset)) { michael@0: return setParam64BitFileOffset(use64BitOffset != 0); michael@0: } michael@0: } else if (key == "param-geotag-longitude") { michael@0: int64_t longitudex10000; michael@0: if (safe_strtoi64(value.string(), &longitudex10000)) { michael@0: return setParamGeoDataLongitude(longitudex10000); michael@0: } michael@0: } else if (key == "param-geotag-latitude") { michael@0: int64_t latitudex10000; michael@0: if (safe_strtoi64(value.string(), &latitudex10000)) { michael@0: return setParamGeoDataLatitude(latitudex10000); michael@0: } michael@0: } else if (key == "param-track-time-status") { michael@0: int64_t timeDurationUs; michael@0: if (safe_strtoi64(value.string(), &timeDurationUs)) { michael@0: return setParamTrackTimeStatus(timeDurationUs); michael@0: } michael@0: } else if (key == "audio-param-sampling-rate") { michael@0: int32_t sampling_rate; michael@0: if (safe_strtoi32(value.string(), &sampling_rate)) { michael@0: return setParamAudioSamplingRate(sampling_rate); michael@0: } michael@0: } else if (key == "audio-param-number-of-channels") { michael@0: int32_t number_of_channels; michael@0: if (safe_strtoi32(value.string(), &number_of_channels)) { michael@0: return setParamAudioNumberOfChannels(number_of_channels); michael@0: } michael@0: } else if (key == "audio-param-encoding-bitrate") { michael@0: int32_t audio_bitrate; michael@0: if (safe_strtoi32(value.string(), &audio_bitrate)) { michael@0: return setParamAudioEncodingBitRate(audio_bitrate); michael@0: } michael@0: } else if (key == "audio-param-time-scale") { michael@0: int32_t timeScale; michael@0: if (safe_strtoi32(value.string(), &timeScale)) { michael@0: return setParamAudioTimeScale(timeScale); michael@0: } michael@0: } else if (key == "video-param-encoding-bitrate") { michael@0: int32_t video_bitrate; michael@0: if (safe_strtoi32(value.string(), &video_bitrate)) { michael@0: return setParamVideoEncodingBitRate(video_bitrate); michael@0: } michael@0: } else if (key == "video-param-rotation-angle-degrees") { michael@0: int32_t degrees; michael@0: if (safe_strtoi32(value.string(), °rees)) { michael@0: return setParamVideoRotation(degrees); michael@0: } michael@0: } else if (key == "video-param-i-frames-interval") { michael@0: int32_t seconds; michael@0: if (safe_strtoi32(value.string(), &seconds)) { michael@0: return setParamVideoIFramesInterval(seconds); michael@0: } michael@0: } else if (key == "video-param-encoder-profile") { michael@0: int32_t profile; michael@0: if (safe_strtoi32(value.string(), &profile)) { michael@0: return setParamVideoEncoderProfile(profile); michael@0: } michael@0: } else if (key == "video-param-encoder-level") { michael@0: int32_t level; michael@0: if (safe_strtoi32(value.string(), &level)) { michael@0: return setParamVideoEncoderLevel(level); michael@0: } michael@0: } else if (key == "video-param-camera-id") { michael@0: int32_t cameraId; michael@0: if (safe_strtoi32(value.string(), &cameraId)) { michael@0: return setParamVideoCameraId(cameraId); michael@0: } michael@0: } else if (key == "video-param-time-scale") { michael@0: int32_t timeScale; michael@0: if (safe_strtoi32(value.string(), &timeScale)) { michael@0: return setParamVideoTimeScale(timeScale); michael@0: } michael@0: } else { michael@0: RE_LOGE("setParameter: failed to find key %s", key.string()); michael@0: } michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: status_t GonkRecorder::setParameters(const String8 ¶ms) { michael@0: RE_LOGV("setParameters: %s", params.string()); michael@0: const char *cparams = params.string(); michael@0: const char *key_start = cparams; michael@0: for (;;) { michael@0: const char *equal_pos = strchr(key_start, '='); michael@0: if (equal_pos == NULL) { michael@0: RE_LOGE("Parameters %s miss a value", cparams); michael@0: return BAD_VALUE; michael@0: } michael@0: String8 key(key_start, equal_pos - key_start); michael@0: TrimString(&key); michael@0: if (key.length() == 0) { michael@0: RE_LOGE("Parameters %s contains an empty key", cparams); michael@0: return BAD_VALUE; michael@0: } michael@0: const char *value_start = equal_pos + 1; michael@0: const char *semicolon_pos = strchr(value_start, ';'); michael@0: String8 value; michael@0: if (semicolon_pos == NULL) { michael@0: value.setTo(value_start); michael@0: } else { michael@0: value.setTo(value_start, semicolon_pos - value_start); michael@0: } michael@0: if (setParameter(key, value) != OK) { michael@0: return BAD_VALUE; michael@0: } michael@0: if (semicolon_pos == NULL) { michael@0: break; // Reaches the end michael@0: } michael@0: key_start = semicolon_pos + 1; michael@0: } michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setListener(const sp &listener) { michael@0: mListener = listener; michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setClientName(const String16& clientName) { michael@0: mClientName = clientName; michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::prepare() { michael@0: if (mVideoSource != VIDEO_SOURCE_LIST_END && mVideoEncoder != VIDEO_ENCODER_LIST_END && michael@0: mVideoHeight && mVideoWidth && // Video recording michael@0: (mVideoHeight * mVideoWidth >= RES_720P)) { michael@0: // TODO: Above check needs to be updated when mMaxFileDurationUs is set from camera app michael@0: RE_LOGV("Video is high resolution so setting 64-bit file offsets"); michael@0: setParam64BitFileOffset(true); michael@0: } michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::start() { michael@0: CHECK_GE(mOutputFd, 0); michael@0: michael@0: // Get UID here for permission checking michael@0: mClientUid = IPCThreadState::self()->getCallingUid(); michael@0: if (mWriter != NULL) { michael@0: RE_LOGE("File writer is not avaialble"); michael@0: return UNKNOWN_ERROR; michael@0: } michael@0: michael@0: status_t status = OK; michael@0: michael@0: switch (mOutputFormat) { michael@0: case OUTPUT_FORMAT_DEFAULT: michael@0: case OUTPUT_FORMAT_THREE_GPP: michael@0: case OUTPUT_FORMAT_MPEG_4: michael@0: status = startMPEG4Recording(); michael@0: break; michael@0: michael@0: case OUTPUT_FORMAT_AMR_NB: michael@0: case OUTPUT_FORMAT_AMR_WB: michael@0: status = startAMRRecording(); michael@0: break; michael@0: michael@0: #if defined(MOZ_WIDGET_GONK) && ANDROID_VERSION >= 17 michael@0: case OUTPUT_FORMAT_AAC_ADIF: michael@0: case OUTPUT_FORMAT_AAC_ADTS: michael@0: status = startAACRecording(); michael@0: break; michael@0: #endif michael@0: michael@0: case OUTPUT_FORMAT_RTP_AVP: michael@0: status = startRTPRecording(); michael@0: break; michael@0: michael@0: case OUTPUT_FORMAT_MPEG2TS: michael@0: status = startMPEG2TSRecording(); michael@0: break; michael@0: michael@0: default: michael@0: RE_LOGE("Unsupported output file format: %d", mOutputFormat); michael@0: status = UNKNOWN_ERROR; michael@0: break; michael@0: } michael@0: michael@0: if ((status == OK) && (!mStarted)) { michael@0: mStarted = true; michael@0: } michael@0: michael@0: return status; michael@0: } michael@0: michael@0: sp GonkRecorder::createAudioSource() { michael@0: sp audioSource = michael@0: new AudioSource( michael@0: mAudioSource, michael@0: mSampleRate, michael@0: mAudioChannels); michael@0: michael@0: status_t err = audioSource->initCheck(); michael@0: michael@0: if (err != OK) { michael@0: RE_LOGE("audio source is not initialized"); michael@0: return NULL; michael@0: } michael@0: michael@0: sp encMeta = new MetaData; michael@0: const char *mime; michael@0: switch (mAudioEncoder) { michael@0: case AUDIO_ENCODER_AMR_NB: michael@0: case AUDIO_ENCODER_DEFAULT: michael@0: mime = MEDIA_MIMETYPE_AUDIO_AMR_NB; michael@0: break; michael@0: case AUDIO_ENCODER_AMR_WB: michael@0: mime = MEDIA_MIMETYPE_AUDIO_AMR_WB; michael@0: break; michael@0: #if defined(MOZ_WIDGET_GONK) && ANDROID_VERSION >= 17 michael@0: case AUDIO_ENCODER_AAC: michael@0: mime = MEDIA_MIMETYPE_AUDIO_AAC; michael@0: encMeta->setInt32(kKeyAACProfile, OMX_AUDIO_AACObjectLC); michael@0: break; michael@0: case AUDIO_ENCODER_HE_AAC: michael@0: mime = MEDIA_MIMETYPE_AUDIO_AAC; michael@0: encMeta->setInt32(kKeyAACProfile, OMX_AUDIO_AACObjectHE); michael@0: break; michael@0: case AUDIO_ENCODER_AAC_ELD: michael@0: mime = MEDIA_MIMETYPE_AUDIO_AAC; michael@0: encMeta->setInt32(kKeyAACProfile, OMX_AUDIO_AACObjectELD); michael@0: break; michael@0: #endif michael@0: default: michael@0: RE_LOGE("Unknown audio encoder: %d", mAudioEncoder); michael@0: return NULL; michael@0: } michael@0: encMeta->setCString(kKeyMIMEType, mime); michael@0: michael@0: int32_t maxInputSize; michael@0: CHECK(audioSource->getFormat()->findInt32( michael@0: kKeyMaxInputSize, &maxInputSize)); michael@0: michael@0: encMeta->setInt32(kKeyMaxInputSize, maxInputSize); michael@0: encMeta->setInt32(kKeyChannelCount, mAudioChannels); michael@0: encMeta->setInt32(kKeySampleRate, mSampleRate); michael@0: encMeta->setInt32(kKeyBitRate, mAudioBitRate); michael@0: if (mAudioTimeScale > 0) { michael@0: encMeta->setInt32(kKeyTimeScale, mAudioTimeScale); michael@0: } michael@0: michael@0: // OMXClient::connect() always returns OK and abort's fatally if michael@0: // it can't connect. michael@0: OMXClient client; michael@0: // CHECK_EQ causes an abort if the given condition fails. michael@0: CHECK_EQ(client.connect(), (status_t)OK); michael@0: sp audioEncoder = michael@0: OMXCodec::Create(client.interface(), encMeta, michael@0: true /* createEncoder */, audioSource); michael@0: mAudioSourceNode = audioSource; michael@0: michael@0: return audioEncoder; michael@0: } michael@0: michael@0: #if defined(MOZ_WIDGET_GONK) && ANDROID_VERSION >= 17 michael@0: status_t GonkRecorder::startAACRecording() { michael@0: // FIXME: michael@0: // Add support for OUTPUT_FORMAT_AAC_ADIF michael@0: CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_AAC_ADTS); michael@0: michael@0: CHECK(mAudioEncoder == AUDIO_ENCODER_AAC || michael@0: mAudioEncoder == AUDIO_ENCODER_HE_AAC || michael@0: mAudioEncoder == AUDIO_ENCODER_AAC_ELD); michael@0: CHECK(mAudioSource != AUDIO_SOURCE_CNT); michael@0: michael@0: mWriter = new AACWriter(mOutputFd); michael@0: status_t status = startRawAudioRecording(); michael@0: if (status != OK) { michael@0: mWriter.clear(); michael@0: mWriter = NULL; michael@0: } michael@0: michael@0: return status; michael@0: } michael@0: #endif michael@0: michael@0: status_t GonkRecorder::startAMRRecording() { michael@0: CHECK(mOutputFormat == OUTPUT_FORMAT_AMR_NB || michael@0: mOutputFormat == OUTPUT_FORMAT_AMR_WB); michael@0: michael@0: if (mOutputFormat == OUTPUT_FORMAT_AMR_NB) { michael@0: if (mAudioEncoder != AUDIO_ENCODER_DEFAULT && michael@0: mAudioEncoder != AUDIO_ENCODER_AMR_NB) { michael@0: RE_LOGE("Invalid encoder %d used for AMRNB recording", michael@0: mAudioEncoder); michael@0: return BAD_VALUE; michael@0: } michael@0: } else { // mOutputFormat must be OUTPUT_FORMAT_AMR_WB michael@0: if (mAudioEncoder != AUDIO_ENCODER_AMR_WB) { michael@0: RE_LOGE("Invlaid encoder %d used for AMRWB recording", michael@0: mAudioEncoder); michael@0: return BAD_VALUE; michael@0: } michael@0: } michael@0: michael@0: mWriter = new AMRWriter(mOutputFd); michael@0: status_t status = startRawAudioRecording(); michael@0: if (status != OK) { michael@0: mWriter.clear(); michael@0: mWriter = NULL; michael@0: } michael@0: return status; michael@0: } michael@0: michael@0: status_t GonkRecorder::startRawAudioRecording() { michael@0: if (mAudioSource >= AUDIO_SOURCE_CNT) { michael@0: RE_LOGE("Invalid audio source: %d", mAudioSource); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: status_t status = BAD_VALUE; michael@0: if (OK != (status = checkAudioEncoderCapabilities())) { michael@0: return status; michael@0: } michael@0: michael@0: sp audioEncoder = createAudioSource(); michael@0: if (audioEncoder == NULL) { michael@0: return UNKNOWN_ERROR; michael@0: } michael@0: michael@0: CHECK(mWriter != 0); michael@0: mWriter->addSource(audioEncoder); michael@0: michael@0: if (mMaxFileDurationUs != 0) { michael@0: mWriter->setMaxFileDuration(mMaxFileDurationUs); michael@0: } michael@0: if (mMaxFileSizeBytes != 0) { michael@0: mWriter->setMaxFileSize(mMaxFileSizeBytes); michael@0: } michael@0: mWriter->setListener(mListener); michael@0: mWriter->start(); michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::startRTPRecording() { michael@0: return INVALID_OPERATION; michael@0: } michael@0: michael@0: status_t GonkRecorder::startMPEG2TSRecording() { michael@0: CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_MPEG2TS); michael@0: michael@0: sp writer = new MPEG2TSWriter(mOutputFd); michael@0: michael@0: if (mAudioSource != AUDIO_SOURCE_CNT) { michael@0: #if defined(MOZ_WIDGET_GONK) && ANDROID_VERSION >= 17 michael@0: if (mAudioEncoder != AUDIO_ENCODER_AAC && michael@0: mAudioEncoder != AUDIO_ENCODER_HE_AAC && michael@0: mAudioEncoder != AUDIO_ENCODER_AAC_ELD) { michael@0: return ERROR_UNSUPPORTED; michael@0: } michael@0: #endif michael@0: status_t err = setupAudioEncoder(writer); michael@0: michael@0: if (err != OK) { michael@0: return err; michael@0: } michael@0: } michael@0: michael@0: if (mVideoSource < VIDEO_SOURCE_LIST_END) { michael@0: if (mVideoEncoder != VIDEO_ENCODER_H264) { michael@0: return ERROR_UNSUPPORTED; michael@0: } michael@0: michael@0: sp mediaSource; michael@0: status_t err = setupMediaSource(&mediaSource); michael@0: if (err != OK) { michael@0: return err; michael@0: } michael@0: michael@0: sp encoder; michael@0: err = setupVideoEncoder(mediaSource, mVideoBitRate, &encoder); michael@0: michael@0: if (err != OK) { michael@0: return err; michael@0: } michael@0: michael@0: writer->addSource(encoder); michael@0: } michael@0: michael@0: if (mMaxFileDurationUs != 0) { michael@0: writer->setMaxFileDuration(mMaxFileDurationUs); michael@0: } michael@0: michael@0: if (mMaxFileSizeBytes != 0) { michael@0: writer->setMaxFileSize(mMaxFileSizeBytes); michael@0: } michael@0: michael@0: mWriter = writer; michael@0: michael@0: return mWriter->start(); michael@0: } michael@0: michael@0: void GonkRecorder::clipVideoFrameRate() { michael@0: RE_LOGV("clipVideoFrameRate: encoder %d", mVideoEncoder); michael@0: int minFrameRate = mEncoderProfiles->getVideoEncoderParamByName( michael@0: "enc.vid.fps.min", mVideoEncoder); michael@0: int maxFrameRate = mEncoderProfiles->getVideoEncoderParamByName( michael@0: "enc.vid.fps.max", mVideoEncoder); michael@0: if (mFrameRate < minFrameRate && minFrameRate != -1) { michael@0: RE_LOGW("Intended video encoding frame rate (%d fps) is too small" michael@0: " and will be set to (%d fps)", mFrameRate, minFrameRate); michael@0: mFrameRate = minFrameRate; michael@0: } else if (mFrameRate > maxFrameRate && maxFrameRate != -1) { michael@0: RE_LOGW("Intended video encoding frame rate (%d fps) is too large" michael@0: " and will be set to (%d fps)", mFrameRate, maxFrameRate); michael@0: mFrameRate = maxFrameRate; michael@0: } michael@0: } michael@0: michael@0: void GonkRecorder::clipVideoBitRate() { michael@0: RE_LOGV("clipVideoBitRate: encoder %d", mVideoEncoder); michael@0: int minBitRate = mEncoderProfiles->getVideoEncoderParamByName( michael@0: "enc.vid.bps.min", mVideoEncoder); michael@0: int maxBitRate = mEncoderProfiles->getVideoEncoderParamByName( michael@0: "enc.vid.bps.max", mVideoEncoder); michael@0: if (mVideoBitRate < minBitRate && minBitRate != -1) { michael@0: RE_LOGW("Intended video encoding bit rate (%d bps) is too small" michael@0: " and will be set to (%d bps)", mVideoBitRate, minBitRate); michael@0: mVideoBitRate = minBitRate; michael@0: } else if (mVideoBitRate > maxBitRate && maxBitRate != -1) { michael@0: RE_LOGW("Intended video encoding bit rate (%d bps) is too large" michael@0: " and will be set to (%d bps)", mVideoBitRate, maxBitRate); michael@0: mVideoBitRate = maxBitRate; michael@0: } michael@0: } michael@0: michael@0: void GonkRecorder::clipVideoFrameWidth() { michael@0: RE_LOGV("clipVideoFrameWidth: encoder %d", mVideoEncoder); michael@0: int minFrameWidth = mEncoderProfiles->getVideoEncoderParamByName( michael@0: "enc.vid.width.min", mVideoEncoder); michael@0: int maxFrameWidth = mEncoderProfiles->getVideoEncoderParamByName( michael@0: "enc.vid.width.max", mVideoEncoder); michael@0: if (mVideoWidth < minFrameWidth && minFrameWidth != -1) { michael@0: RE_LOGW("Intended video encoding frame width (%d) is too small" michael@0: " and will be set to (%d)", mVideoWidth, minFrameWidth); michael@0: mVideoWidth = minFrameWidth; michael@0: } else if (mVideoWidth > maxFrameWidth && maxFrameWidth != -1) { michael@0: RE_LOGW("Intended video encoding frame width (%d) is too large" michael@0: " and will be set to (%d)", mVideoWidth, maxFrameWidth); michael@0: mVideoWidth = maxFrameWidth; michael@0: } michael@0: } michael@0: michael@0: status_t GonkRecorder::checkVideoEncoderCapabilities() { michael@0: michael@0: // Dont clip for time lapse capture as encoder will have enough michael@0: // time to encode because of slow capture rate of time lapse. michael@0: clipVideoBitRate(); michael@0: clipVideoFrameRate(); michael@0: clipVideoFrameWidth(); michael@0: clipVideoFrameHeight(); michael@0: setDefaultProfileIfNecessary(); michael@0: return OK; michael@0: } michael@0: michael@0: // Set to use AVC baseline profile if the encoding parameters matches michael@0: // CAMCORDER_QUALITY_LOW profile; this is for the sake of MMS service. michael@0: void GonkRecorder::setDefaultProfileIfNecessary() { michael@0: RE_LOGV("setDefaultProfileIfNecessary"); michael@0: michael@0: camcorder_quality quality = CAMCORDER_QUALITY_LOW; michael@0: michael@0: int64_t durationUs = mEncoderProfiles->getCamcorderProfileParamByName( michael@0: "duration", mCameraId, quality) * 1000000LL; michael@0: michael@0: int fileFormat = mEncoderProfiles->getCamcorderProfileParamByName( michael@0: "file.format", mCameraId, quality); michael@0: michael@0: int videoCodec = mEncoderProfiles->getCamcorderProfileParamByName( michael@0: "vid.codec", mCameraId, quality); michael@0: michael@0: int videoBitRate = mEncoderProfiles->getCamcorderProfileParamByName( michael@0: "vid.bps", mCameraId, quality); michael@0: michael@0: int videoFrameRate = mEncoderProfiles->getCamcorderProfileParamByName( michael@0: "vid.fps", mCameraId, quality); michael@0: michael@0: int videoFrameWidth = mEncoderProfiles->getCamcorderProfileParamByName( michael@0: "vid.width", mCameraId, quality); michael@0: michael@0: int videoFrameHeight = mEncoderProfiles->getCamcorderProfileParamByName( michael@0: "vid.height", mCameraId, quality); michael@0: michael@0: int audioCodec = mEncoderProfiles->getCamcorderProfileParamByName( michael@0: "aud.codec", mCameraId, quality); michael@0: michael@0: int audioBitRate = mEncoderProfiles->getCamcorderProfileParamByName( michael@0: "aud.bps", mCameraId, quality); michael@0: michael@0: int audioSampleRate = mEncoderProfiles->getCamcorderProfileParamByName( michael@0: "aud.hz", mCameraId, quality); michael@0: michael@0: int audioChannels = mEncoderProfiles->getCamcorderProfileParamByName( michael@0: "aud.ch", mCameraId, quality); michael@0: michael@0: if (durationUs == mMaxFileDurationUs && michael@0: fileFormat == mOutputFormat && michael@0: videoCodec == mVideoEncoder && michael@0: videoBitRate == mVideoBitRate && michael@0: videoFrameRate == mFrameRate && michael@0: videoFrameWidth == mVideoWidth && michael@0: videoFrameHeight == mVideoHeight && michael@0: audioCodec == mAudioEncoder && michael@0: audioBitRate == mAudioBitRate && michael@0: audioSampleRate == mSampleRate && michael@0: audioChannels == mAudioChannels) { michael@0: if (videoCodec == VIDEO_ENCODER_H264) { michael@0: RE_LOGI("Force to use AVC baseline profile"); michael@0: setParamVideoEncoderProfile(OMX_VIDEO_AVCProfileBaseline); michael@0: } michael@0: } michael@0: } michael@0: michael@0: status_t GonkRecorder::checkAudioEncoderCapabilities() { michael@0: clipAudioBitRate(); michael@0: clipAudioSampleRate(); michael@0: clipNumberOfAudioChannels(); michael@0: return OK; michael@0: } michael@0: michael@0: void GonkRecorder::clipAudioBitRate() { michael@0: RE_LOGV("clipAudioBitRate: encoder %d", mAudioEncoder); michael@0: michael@0: int minAudioBitRate = michael@0: mEncoderProfiles->getAudioEncoderParamByName( michael@0: "enc.aud.bps.min", mAudioEncoder); michael@0: if (minAudioBitRate != -1 && mAudioBitRate < minAudioBitRate) { michael@0: RE_LOGW("Intended audio encoding bit rate (%d) is too small" michael@0: " and will be set to (%d)", mAudioBitRate, minAudioBitRate); michael@0: mAudioBitRate = minAudioBitRate; michael@0: } michael@0: michael@0: int maxAudioBitRate = michael@0: mEncoderProfiles->getAudioEncoderParamByName( michael@0: "enc.aud.bps.max", mAudioEncoder); michael@0: if (maxAudioBitRate != -1 && mAudioBitRate > maxAudioBitRate) { michael@0: RE_LOGW("Intended audio encoding bit rate (%d) is too large" michael@0: " and will be set to (%d)", mAudioBitRate, maxAudioBitRate); michael@0: mAudioBitRate = maxAudioBitRate; michael@0: } michael@0: } michael@0: michael@0: void GonkRecorder::clipAudioSampleRate() { michael@0: RE_LOGV("clipAudioSampleRate: encoder %d", mAudioEncoder); michael@0: michael@0: int minSampleRate = michael@0: mEncoderProfiles->getAudioEncoderParamByName( michael@0: "enc.aud.hz.min", mAudioEncoder); michael@0: if (minSampleRate != -1 && mSampleRate < minSampleRate) { michael@0: RE_LOGW("Intended audio sample rate (%d) is too small" michael@0: " and will be set to (%d)", mSampleRate, minSampleRate); michael@0: mSampleRate = minSampleRate; michael@0: } michael@0: michael@0: int maxSampleRate = michael@0: mEncoderProfiles->getAudioEncoderParamByName( michael@0: "enc.aud.hz.max", mAudioEncoder); michael@0: if (maxSampleRate != -1 && mSampleRate > maxSampleRate) { michael@0: RE_LOGW("Intended audio sample rate (%d) is too large" michael@0: " and will be set to (%d)", mSampleRate, maxSampleRate); michael@0: mSampleRate = maxSampleRate; michael@0: } michael@0: } michael@0: michael@0: void GonkRecorder::clipNumberOfAudioChannels() { michael@0: RE_LOGV("clipNumberOfAudioChannels: encoder %d", mAudioEncoder); michael@0: michael@0: int minChannels = michael@0: mEncoderProfiles->getAudioEncoderParamByName( michael@0: "enc.aud.ch.min", mAudioEncoder); michael@0: if (minChannels != -1 && mAudioChannels < minChannels) { michael@0: RE_LOGW("Intended number of audio channels (%d) is too small" michael@0: " and will be set to (%d)", mAudioChannels, minChannels); michael@0: mAudioChannels = minChannels; michael@0: } michael@0: michael@0: int maxChannels = michael@0: mEncoderProfiles->getAudioEncoderParamByName( michael@0: "enc.aud.ch.max", mAudioEncoder); michael@0: if (maxChannels != -1 && mAudioChannels > maxChannels) { michael@0: RE_LOGW("Intended number of audio channels (%d) is too large" michael@0: " and will be set to (%d)", mAudioChannels, maxChannels); michael@0: mAudioChannels = maxChannels; michael@0: } michael@0: } michael@0: michael@0: void GonkRecorder::clipVideoFrameHeight() { michael@0: RE_LOGV("clipVideoFrameHeight: encoder %d", mVideoEncoder); michael@0: int minFrameHeight = mEncoderProfiles->getVideoEncoderParamByName( michael@0: "enc.vid.height.min", mVideoEncoder); michael@0: int maxFrameHeight = mEncoderProfiles->getVideoEncoderParamByName( michael@0: "enc.vid.height.max", mVideoEncoder); michael@0: if (minFrameHeight != -1 && mVideoHeight < minFrameHeight) { michael@0: RE_LOGW("Intended video encoding frame height (%d) is too small" michael@0: " and will be set to (%d)", mVideoHeight, minFrameHeight); michael@0: mVideoHeight = minFrameHeight; michael@0: } else if (maxFrameHeight != -1 && mVideoHeight > maxFrameHeight) { michael@0: RE_LOGW("Intended video encoding frame height (%d) is too large" michael@0: " and will be set to (%d)", mVideoHeight, maxFrameHeight); michael@0: mVideoHeight = maxFrameHeight; michael@0: } michael@0: } michael@0: michael@0: // Set up the appropriate MediaSource depending on the chosen option michael@0: status_t GonkRecorder::setupMediaSource( michael@0: sp *mediaSource) { michael@0: if (mVideoSource == VIDEO_SOURCE_DEFAULT michael@0: || mVideoSource == VIDEO_SOURCE_CAMERA) { michael@0: sp cameraSource; michael@0: status_t err = setupCameraSource(&cameraSource); michael@0: if (err != OK) { michael@0: return err; michael@0: } michael@0: *mediaSource = cameraSource; michael@0: } else if (mVideoSource == VIDEO_SOURCE_GRALLOC_BUFFER) { michael@0: return BAD_VALUE; michael@0: } else { michael@0: return INVALID_OPERATION; michael@0: } michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setupCameraSource( michael@0: sp *cameraSource) { michael@0: status_t err = OK; michael@0: if ((err = checkVideoEncoderCapabilities()) != OK) { michael@0: return err; michael@0: } michael@0: Size videoSize; michael@0: videoSize.width = mVideoWidth; michael@0: videoSize.height = mVideoHeight; michael@0: bool useMeta = true; michael@0: char value[PROPERTY_VALUE_MAX]; michael@0: if (property_get("debug.camcorder.disablemeta", value, NULL) && michael@0: atoi(value)) { michael@0: useMeta = false; michael@0: } michael@0: michael@0: *cameraSource = GonkCameraSource::Create( michael@0: mCameraHw, videoSize, mFrameRate, useMeta); michael@0: if (*cameraSource == NULL) { michael@0: return UNKNOWN_ERROR; michael@0: } michael@0: michael@0: if ((*cameraSource)->initCheck() != OK) { michael@0: (*cameraSource).clear(); michael@0: *cameraSource = NULL; michael@0: return NO_INIT; michael@0: } michael@0: michael@0: // When frame rate is not set, the actual frame rate will be set to michael@0: // the current frame rate being used. michael@0: if (mFrameRate == -1) { michael@0: int32_t frameRate = 0; michael@0: CHECK ((*cameraSource)->getFormat()->findInt32( michael@0: kKeyFrameRate, &frameRate)); michael@0: RE_LOGI("Frame rate is not explicitly set. Use the current frame " michael@0: "rate (%d fps)", frameRate); michael@0: mFrameRate = frameRate; michael@0: } michael@0: michael@0: CHECK(mFrameRate != -1); michael@0: michael@0: mIsMetaDataStoredInVideoBuffers = michael@0: (*cameraSource)->isMetaDataStoredInVideoBuffers(); michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setupVideoEncoder( michael@0: sp cameraSource, michael@0: int32_t videoBitRate, michael@0: sp *source) { michael@0: source->clear(); michael@0: michael@0: sp enc_meta = new MetaData; michael@0: enc_meta->setInt32(kKeyBitRate, videoBitRate); michael@0: enc_meta->setInt32(kKeyFrameRate, mFrameRate); michael@0: michael@0: switch (mVideoEncoder) { michael@0: case VIDEO_ENCODER_H263: michael@0: enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263); michael@0: break; michael@0: michael@0: case VIDEO_ENCODER_MPEG_4_SP: michael@0: enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4); michael@0: break; michael@0: michael@0: case VIDEO_ENCODER_H264: michael@0: enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC); michael@0: break; michael@0: michael@0: default: michael@0: CHECK(!"Should not be here, unsupported video encoding."); michael@0: break; michael@0: } michael@0: michael@0: sp meta = cameraSource->getFormat(); michael@0: michael@0: int32_t width, height, stride, sliceHeight, colorFormat; michael@0: CHECK(meta->findInt32(kKeyWidth, &width)); michael@0: CHECK(meta->findInt32(kKeyHeight, &height)); michael@0: CHECK(meta->findInt32(kKeyStride, &stride)); michael@0: CHECK(meta->findInt32(kKeySliceHeight, &sliceHeight)); michael@0: CHECK(meta->findInt32(kKeyColorFormat, &colorFormat)); michael@0: michael@0: enc_meta->setInt32(kKeyWidth, width); michael@0: enc_meta->setInt32(kKeyHeight, height); michael@0: enc_meta->setInt32(kKeyIFramesInterval, mIFramesIntervalSec); michael@0: enc_meta->setInt32(kKeyStride, stride); michael@0: enc_meta->setInt32(kKeySliceHeight, sliceHeight); michael@0: enc_meta->setInt32(kKeyColorFormat, colorFormat); michael@0: if (mVideoTimeScale > 0) { michael@0: enc_meta->setInt32(kKeyTimeScale, mVideoTimeScale); michael@0: } michael@0: if (mVideoEncoderProfile != -1) { michael@0: enc_meta->setInt32(kKeyVideoProfile, mVideoEncoderProfile); michael@0: } michael@0: if (mVideoEncoderLevel != -1) { michael@0: enc_meta->setInt32(kKeyVideoLevel, mVideoEncoderLevel); michael@0: } michael@0: michael@0: // OMXClient::connect() always returns OK and abort's fatally if michael@0: // it can't connect. michael@0: OMXClient client; michael@0: // CHECK_EQ causes an abort if the given condition fails. michael@0: CHECK_EQ(client.connect(), (status_t)OK); michael@0: michael@0: uint32_t encoder_flags = 0; michael@0: if (mIsMetaDataStoredInVideoBuffers) { michael@0: #if defined(MOZ_WIDGET_GONK) && ANDROID_VERSION >= 17 michael@0: encoder_flags |= OMXCodec::kStoreMetaDataInVideoBuffers; michael@0: #else michael@0: encoder_flags |= OMXCodec::kHardwareCodecsOnly; michael@0: encoder_flags |= OMXCodec::kStoreMetaDataInVideoBuffers; michael@0: encoder_flags |= OMXCodec::kOnlySubmitOneInputBufferAtOneTime; michael@0: #endif michael@0: } michael@0: michael@0: sp encoder = OMXCodec::Create( michael@0: client.interface(), enc_meta, michael@0: true /* createEncoder */, cameraSource, michael@0: NULL, encoder_flags); michael@0: if (encoder == NULL) { michael@0: RE_LOGW("Failed to create the encoder"); michael@0: // When the encoder fails to be created, we need michael@0: // release the camera source due to the camera's lock michael@0: // and unlock mechanism. michael@0: cameraSource->stop(); michael@0: return UNKNOWN_ERROR; michael@0: } michael@0: michael@0: *source = encoder; michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setupAudioEncoder(const sp& writer) { michael@0: status_t status = BAD_VALUE; michael@0: if (OK != (status = checkAudioEncoderCapabilities())) { michael@0: return status; michael@0: } michael@0: michael@0: switch(mAudioEncoder) { michael@0: case AUDIO_ENCODER_AMR_NB: michael@0: case AUDIO_ENCODER_AMR_WB: michael@0: #if defined(MOZ_WIDGET_GONK) && ANDROID_VERSION >= 17 michael@0: case AUDIO_ENCODER_AAC: michael@0: case AUDIO_ENCODER_HE_AAC: michael@0: case AUDIO_ENCODER_AAC_ELD: michael@0: #endif michael@0: break; michael@0: michael@0: default: michael@0: RE_LOGE("Unsupported audio encoder: %d", mAudioEncoder); michael@0: return UNKNOWN_ERROR; michael@0: } michael@0: michael@0: sp audioEncoder = createAudioSource(); michael@0: if (audioEncoder == NULL) { michael@0: return UNKNOWN_ERROR; michael@0: } michael@0: michael@0: writer->addSource(audioEncoder); michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setupMPEG4Recording( michael@0: int outputFd, michael@0: int32_t videoWidth, int32_t videoHeight, michael@0: int32_t videoBitRate, michael@0: int32_t *totalBitRate, michael@0: sp *mediaWriter) { michael@0: mediaWriter->clear(); michael@0: *totalBitRate = 0; michael@0: status_t err = OK; michael@0: sp writer = new MPEG4Writer(outputFd); michael@0: michael@0: if (mVideoSource < VIDEO_SOURCE_LIST_END) { michael@0: michael@0: sp mediaSource; michael@0: err = setupMediaSource(&mediaSource); michael@0: if (err != OK) { michael@0: return err; michael@0: } michael@0: michael@0: sp encoder; michael@0: err = setupVideoEncoder(mediaSource, videoBitRate, &encoder); michael@0: if (err != OK) { michael@0: return err; michael@0: } michael@0: michael@0: writer->addSource(encoder); michael@0: *totalBitRate += videoBitRate; michael@0: } michael@0: michael@0: // Audio source is added at the end if it exists. michael@0: // This help make sure that the "recoding" sound is suppressed for michael@0: // camcorder applications in the recorded files. michael@0: if (mAudioSource != AUDIO_SOURCE_CNT) { michael@0: err = setupAudioEncoder(writer); michael@0: if (err != OK) return err; michael@0: *totalBitRate += mAudioBitRate; michael@0: } michael@0: michael@0: if (mInterleaveDurationUs > 0) { michael@0: reinterpret_cast(writer.get())-> michael@0: setInterleaveDuration(mInterleaveDurationUs); michael@0: } michael@0: if (mLongitudex10000 > -3600000 && mLatitudex10000 > -3600000) { michael@0: reinterpret_cast(writer.get())-> michael@0: setGeoData(mLatitudex10000, mLongitudex10000); michael@0: } michael@0: if (mMaxFileDurationUs != 0) { michael@0: writer->setMaxFileDuration(mMaxFileDurationUs); michael@0: } michael@0: if (mMaxFileSizeBytes != 0) { michael@0: writer->setMaxFileSize(mMaxFileSizeBytes); michael@0: } michael@0: michael@0: mStartTimeOffsetMs = mEncoderProfiles->getStartTimeOffsetMs(mCameraId); michael@0: if (mStartTimeOffsetMs > 0) { michael@0: reinterpret_cast(writer.get())-> michael@0: setStartTimeOffsetMs(mStartTimeOffsetMs); michael@0: } michael@0: michael@0: writer->setListener(mListener); michael@0: *mediaWriter = writer; michael@0: return OK; michael@0: } michael@0: michael@0: void GonkRecorder::setupMPEG4MetaData(int64_t startTimeUs, int32_t totalBitRate, michael@0: sp *meta) { michael@0: (*meta)->setInt64(kKeyTime, startTimeUs); michael@0: (*meta)->setInt32(kKeyFileType, mOutputFormat); michael@0: (*meta)->setInt32(kKeyBitRate, totalBitRate); michael@0: (*meta)->setInt32(kKey64BitFileOffset, mUse64BitFileOffset); michael@0: if (mMovieTimeScale > 0) { michael@0: (*meta)->setInt32(kKeyTimeScale, mMovieTimeScale); michael@0: } michael@0: if (mTrackEveryTimeDurationUs > 0) { michael@0: (*meta)->setInt64(kKeyTrackTimeStatus, mTrackEveryTimeDurationUs); michael@0: } michael@0: michael@0: char value[PROPERTY_VALUE_MAX]; michael@0: if (property_get("debug.camcorder.rotation", value, 0) > 0 && atoi(value) >= 0) { michael@0: mRotationDegrees = atoi(value); michael@0: RE_LOGI("Setting rotation to %d", mRotationDegrees ); michael@0: } michael@0: michael@0: if (mRotationDegrees != 0) { michael@0: (*meta)->setInt32(kKeyRotation, mRotationDegrees); michael@0: } michael@0: } michael@0: michael@0: status_t GonkRecorder::startMPEG4Recording() { michael@0: int32_t totalBitRate; michael@0: status_t err = setupMPEG4Recording( michael@0: mOutputFd, mVideoWidth, mVideoHeight, michael@0: mVideoBitRate, &totalBitRate, &mWriter); michael@0: if (err != OK) { michael@0: return err; michael@0: } michael@0: michael@0: //systemTime() doesn't give correct time because michael@0: //HAVE_POSIX_CLOCKS is not defined for utils/Timers.cpp michael@0: //so, using clock_gettime directly michael@0: #include michael@0: struct timespec t; michael@0: clock_gettime(CLOCK_MONOTONIC, &t); michael@0: int64_t startTimeUs = int64_t(t.tv_sec)*1000000000LL + t.tv_nsec; michael@0: startTimeUs = startTimeUs / 1000; michael@0: sp meta = new MetaData; michael@0: setupMPEG4MetaData(startTimeUs, totalBitRate, &meta); michael@0: michael@0: err = mWriter->start(meta.get()); michael@0: if (err != OK) { michael@0: return err; michael@0: } michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::pause() { michael@0: RE_LOGV("pause"); michael@0: if (mWriter == NULL) { michael@0: return UNKNOWN_ERROR; michael@0: } michael@0: mWriter->pause(); michael@0: michael@0: if (mStarted) { michael@0: mStarted = false; michael@0: } michael@0: michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::stop() { michael@0: RE_LOGV("stop"); michael@0: status_t err = OK; michael@0: michael@0: if (mWriter != NULL) { michael@0: err = mWriter->stop(); michael@0: mWriter.clear(); michael@0: } michael@0: michael@0: if (mOutputFd >= 0) { michael@0: ::close(mOutputFd); michael@0: mOutputFd = -1; michael@0: } michael@0: michael@0: if (mStarted) { michael@0: mStarted = false; michael@0: } michael@0: michael@0: michael@0: return err; michael@0: } michael@0: michael@0: status_t GonkRecorder::close() { michael@0: RE_LOGV("close"); michael@0: stop(); michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::reset() { michael@0: RE_LOGV("reset"); michael@0: stop(); michael@0: michael@0: // No audio or video source by default michael@0: mAudioSource = AUDIO_SOURCE_CNT; michael@0: mVideoSource = VIDEO_SOURCE_LIST_END; michael@0: michael@0: // Default parameters michael@0: mOutputFormat = OUTPUT_FORMAT_THREE_GPP; michael@0: mAudioEncoder = AUDIO_ENCODER_AMR_NB; michael@0: mVideoEncoder = VIDEO_ENCODER_H263; michael@0: mVideoWidth = 176; michael@0: mVideoHeight = 144; michael@0: mFrameRate = -1; michael@0: mVideoBitRate = 192000; michael@0: mSampleRate = 8000; michael@0: mAudioChannels = 1; michael@0: mAudioBitRate = 12200; michael@0: mInterleaveDurationUs = 0; michael@0: mIFramesIntervalSec = 1; michael@0: mAudioSourceNode = 0; michael@0: mUse64BitFileOffset = false; michael@0: mMovieTimeScale = -1; michael@0: mAudioTimeScale = -1; michael@0: mVideoTimeScale = -1; michael@0: mCameraId = 0; michael@0: mStartTimeOffsetMs = -1; michael@0: mVideoEncoderProfile = -1; michael@0: mVideoEncoderLevel = -1; michael@0: mMaxFileDurationUs = 0; michael@0: mMaxFileSizeBytes = 0; michael@0: mTrackEveryTimeDurationUs = 0; michael@0: mIsMetaDataStoredInVideoBuffers = false; michael@0: mEncoderProfiles = MediaProfiles::getInstance(); michael@0: mRotationDegrees = 0; michael@0: mLatitudex10000 = -3600000; michael@0: mLongitudex10000 = -3600000; michael@0: michael@0: mOutputFd = -1; michael@0: mCameraHw.clear(); michael@0: //TODO: May need to register a listener eventually michael@0: //if someone is interested in recorder events for now michael@0: //default to no listener registered michael@0: mListener = NULL; michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::getMaxAmplitude(int *max) { michael@0: RE_LOGV("getMaxAmplitude"); michael@0: michael@0: if (max == NULL) { michael@0: RE_LOGE("Null pointer argument"); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: if (mAudioSourceNode != 0) { michael@0: *max = mAudioSourceNode->getMaxAmplitude(); michael@0: } else { michael@0: *max = 0; michael@0: } michael@0: michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::dump( michael@0: int fd, const Vector& args) const { michael@0: RE_LOGV("dump"); michael@0: const size_t SIZE = 256; michael@0: char buffer[SIZE]; michael@0: String8 result; michael@0: if (mWriter != 0) { michael@0: mWriter->dump(fd, args); michael@0: } else { michael@0: snprintf(buffer, SIZE, " No file writer\n"); michael@0: result.append(buffer); michael@0: } michael@0: snprintf(buffer, SIZE, " Recorder: %p\n", this); michael@0: snprintf(buffer, SIZE, " Output file (fd %d):\n", mOutputFd); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " File format: %d\n", mOutputFormat); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Max file size (bytes): %lld\n", mMaxFileSizeBytes); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Max file duration (us): %lld\n", mMaxFileDurationUs); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " File offset length (bits): %d\n", mUse64BitFileOffset? 64: 32); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Interleave duration (us): %d\n", mInterleaveDurationUs); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Progress notification: %lld us\n", mTrackEveryTimeDurationUs); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Audio\n"); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Source: %d\n", mAudioSource); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Encoder: %d\n", mAudioEncoder); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Bit rate (bps): %d\n", mAudioBitRate); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Sampling rate (hz): %d\n", mSampleRate); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Number of channels: %d\n", mAudioChannels); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Max amplitude: %d\n", mAudioSourceNode == 0? 0: mAudioSourceNode->getMaxAmplitude()); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Video\n"); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Source: %d\n", mVideoSource); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Camera Id: %d\n", mCameraId); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Camera object address: %p\n", mCameraHw.get()); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Start time offset (ms): %d\n", mStartTimeOffsetMs); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Encoder: %d\n", mVideoEncoder); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Encoder profile: %d\n", mVideoEncoderProfile); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Encoder level: %d\n", mVideoEncoderLevel); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " I frames interval (s): %d\n", mIFramesIntervalSec); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Frame size (pixels): %dx%d\n", mVideoWidth, mVideoHeight); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Frame rate (fps): %d\n", mFrameRate); michael@0: result.append(buffer); michael@0: snprintf(buffer, SIZE, " Bit rate (bps): %d\n", mVideoBitRate); michael@0: result.append(buffer); michael@0: ::write(fd, result.string(), result.size()); michael@0: return OK; michael@0: } michael@0: michael@0: status_t GonkRecorder::setCamera(const sp& aCameraHw) { michael@0: mCameraHw = aCameraHw; michael@0: return OK; michael@0: } michael@0: michael@0: } // namespace android