StagefrightRecorder.cpp revision 30ab66297501757d745b9ae10da61adcd891f497
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 <media/stagefright/CameraSource.h> 24#include <media/stagefright/MPEG4Writer.h> 25#include <media/stagefright/MediaDebug.h> 26#include <media/stagefright/MediaDefs.h> 27#include <media/stagefright/MetaData.h> 28#include <media/stagefright/OMXClient.h> 29#include <media/stagefright/OMXCodec.h> 30#include <ui/ICamera.h> 31#include <ui/ISurface.h> 32#include <utils/Errors.h> 33 34namespace android { 35 36StagefrightRecorder::StagefrightRecorder() { 37 reset(); 38} 39 40StagefrightRecorder::~StagefrightRecorder() { 41 stop(); 42 43 if (mOutputFd >= 0) { 44 ::close(mOutputFd); 45 mOutputFd = -1; 46 } 47} 48 49status_t StagefrightRecorder::init() { 50 return OK; 51} 52 53status_t StagefrightRecorder::setAudioSource(audio_source as) { 54 mAudioSource = as; 55 56 return OK; 57} 58 59status_t StagefrightRecorder::setVideoSource(video_source vs) { 60 mVideoSource = vs; 61 62 return OK; 63} 64 65status_t StagefrightRecorder::setOutputFormat(output_format of) { 66 mOutputFormat = of; 67 68 return OK; 69} 70 71status_t StagefrightRecorder::setAudioEncoder(audio_encoder ae) { 72 mAudioEncoder = ae; 73 74 return OK; 75} 76 77status_t StagefrightRecorder::setVideoEncoder(video_encoder ve) { 78 mVideoEncoder = ve; 79 80 return OK; 81} 82 83status_t StagefrightRecorder::setVideoSize(int width, int height) { 84 mVideoWidth = width; 85 mVideoHeight = height; 86 87 return OK; 88} 89 90status_t StagefrightRecorder::setVideoFrameRate(int frames_per_second) { 91 mFrameRate = frames_per_second; 92 93 return OK; 94} 95 96status_t StagefrightRecorder::setCamera(const sp<ICamera> &camera) { 97 mCamera = camera; 98 99 return OK; 100} 101 102status_t StagefrightRecorder::setPreviewSurface(const sp<ISurface> &surface) { 103 mPreviewSurface = surface; 104 105 return OK; 106} 107 108status_t StagefrightRecorder::setOutputFile(const char *path) { 109 // We don't actually support this at all, as the media_server process 110 // no longer has permissions to create files. 111 112 return UNKNOWN_ERROR; 113} 114 115status_t StagefrightRecorder::setOutputFile(int fd, int64_t offset, int64_t length) { 116 // These don't make any sense, do they? 117 CHECK_EQ(offset, 0); 118 CHECK_EQ(length, 0); 119 120 if (mOutputFd >= 0) { 121 ::close(mOutputFd); 122 } 123 mOutputFd = dup(fd); 124 125 return OK; 126} 127 128status_t StagefrightRecorder::setParameters(const String8 ¶ms) { 129 mParams = params; 130 131 return OK; 132} 133 134status_t StagefrightRecorder::setListener(const sp<IMediaPlayerClient> &listener) { 135 mListener = listener; 136 137 return OK; 138} 139 140status_t StagefrightRecorder::prepare() { 141 return OK; 142} 143 144status_t StagefrightRecorder::start() { 145 if (mWriter != NULL) { 146 return UNKNOWN_ERROR; 147 } 148 149 if (mVideoSource == VIDEO_SOURCE_CAMERA) { 150 CHECK(mCamera != NULL); 151 152 sp<CameraSource> cameraSource = 153 CameraSource::CreateFromICamera(mCamera); 154 155 CHECK(cameraSource != NULL); 156 157 cameraSource->setPreviewSurface(mPreviewSurface); 158 159 sp<MetaData> enc_meta = new MetaData; 160 switch (mVideoEncoder) { 161 case VIDEO_ENCODER_H263: 162 enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263); 163 break; 164 165 case VIDEO_ENCODER_MPEG_4_SP: 166 enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4); 167 break; 168 169 case VIDEO_ENCODER_H264: 170 enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC); 171 break; 172 173 default: 174 CHECK(!"Should not be here, unsupported video encoding."); 175 break; 176 } 177 178 sp<MetaData> meta = cameraSource->getFormat(); 179 180 int32_t width, height; 181 CHECK(meta->findInt32(kKeyWidth, &width)); 182 CHECK(meta->findInt32(kKeyHeight, &height)); 183 184 enc_meta->setInt32(kKeyWidth, width); 185 enc_meta->setInt32(kKeyHeight, height); 186 187 OMXClient client; 188 CHECK_EQ(client.connect(), OK); 189 190 sp<MediaSource> encoder = 191 OMXCodec::Create( 192 client.interface(), enc_meta, 193 true /* createEncoder */, cameraSource); 194 195 CHECK(mOutputFd >= 0); 196 mWriter = new MPEG4Writer(dup(mOutputFd)); 197 mWriter->addSource(encoder); 198 mWriter->start(); 199 } 200 201 return OK; 202} 203 204status_t StagefrightRecorder::stop() { 205 if (mWriter == NULL) { 206 return UNKNOWN_ERROR; 207 } 208 209 mWriter->stop(); 210 mWriter = NULL; 211 212 return OK; 213} 214 215status_t StagefrightRecorder::close() { 216 stop(); 217 218 return OK; 219} 220 221status_t StagefrightRecorder::reset() { 222 stop(); 223 224 mAudioSource = AUDIO_SOURCE_LIST_END; 225 mVideoSource = VIDEO_SOURCE_LIST_END; 226 mOutputFormat = OUTPUT_FORMAT_LIST_END; 227 mAudioEncoder = AUDIO_ENCODER_LIST_END; 228 mVideoEncoder = VIDEO_ENCODER_LIST_END; 229 mVideoWidth = -1; 230 mVideoHeight = -1; 231 mFrameRate = -1; 232 mOutputFd = -1; 233 234 return OK; 235} 236 237status_t StagefrightRecorder::getMaxAmplitude(int *max) { 238 return UNKNOWN_ERROR; 239} 240 241} // namespace android 242