blob: 115db1ad290427a471ac7195c1bac238a9e4ca42 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Jean-Baptiste Queru4506c622010-07-29 17:35:37 -07002 ** Copyright 2008, The Android Open Source Project
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 **
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 "MediaRecorderService"
19#include <utils/Log.h>
20
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <dirent.h>
24#include <unistd.h>
25#include <string.h>
26#include <cutils/atomic.h>
Andreas Huberea6a38c2009-11-16 15:43:38 -080027#include <cutils/properties.h> // for property_get
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include <android_runtime/ActivityManager.h>
Mathias Agopian07952722009-05-19 19:08:10 -070029#include <binder/IPCThreadState.h>
30#include <binder/IServiceManager.h>
31#include <binder/MemoryHeapBase.h>
32#include <binder/MemoryBase.h>
Andreas Huberbfb9fb12009-12-03 11:31:19 -080033
Dave Sparks17612fc2009-03-27 19:56:11 -070034#include <utils/String16.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
Gloria Wang608a2632009-10-29 15:46:37 -070036#include <media/AudioTrack.h>
37
Dima Zavin34bb4192011-05-11 14:15:23 -070038#include <system/audio.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040#include "MediaRecorderClient.h"
Gloria Wang608a2632009-10-29 15:46:37 -070041#include "MediaPlayerService.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Andreas Huberea6a38c2009-11-16 15:43:38 -080043#include "StagefrightRecorder.h"
44
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045namespace android {
46
Dave Sparks17612fc2009-03-27 19:56:11 -070047const char* cameraPermission = "android.permission.CAMERA";
Dave Sparks6690dc52009-05-20 19:20:31 -070048const char* recordAudioPermission = "android.permission.RECORD_AUDIO";
Dave Sparks17612fc2009-03-27 19:56:11 -070049
50static bool checkPermission(const char* permissionString) {
51#ifndef HAVE_ANDROID_OS
52 return true;
53#endif
54 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
55 bool ok = checkCallingPermission(String16(permissionString));
56 if (!ok) LOGE("Request requires %s", permissionString);
57 return ok;
58}
59
Wu-cheng Li42419ce2011-06-01 17:22:24 +080060status_t MediaRecorderClient::setCamera(const sp<ICamera>& camera,
61 const sp<ICameraRecordingProxy>& proxy)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062{
63 LOGV("setCamera");
64 Mutex::Autolock lock(mLock);
65 if (mRecorder == NULL) {
66 LOGE("recorder is not initialized");
67 return NO_INIT;
68 }
Wu-cheng Li42419ce2011-06-01 17:22:24 +080069 return mRecorder->setCamera(camera, proxy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070}
71
Jamie Gennis85cfdd02010-08-10 16:37:53 -070072status_t MediaRecorderClient::setPreviewSurface(const sp<Surface>& surface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073{
74 LOGV("setPreviewSurface");
75 Mutex::Autolock lock(mLock);
76 if (mRecorder == NULL) {
77 LOGE("recorder is not initialized");
78 return NO_INIT;
79 }
80 return mRecorder->setPreviewSurface(surface);
81}
82
83status_t MediaRecorderClient::setVideoSource(int vs)
84{
85 LOGV("setVideoSource(%d)", vs);
Dave Sparks17612fc2009-03-27 19:56:11 -070086 if (!checkPermission(cameraPermission)) {
87 return PERMISSION_DENIED;
88 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 Mutex::Autolock lock(mLock);
90 if (mRecorder == NULL) {
91 LOGE("recorder is not initialized");
Gloria Wang608a2632009-10-29 15:46:37 -070092 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 }
94 return mRecorder->setVideoSource((video_source)vs);
95}
96
97status_t MediaRecorderClient::setAudioSource(int as)
98{
99 LOGV("setAudioSource(%d)", as);
Dave Sparks6690dc52009-05-20 19:20:31 -0700100 if (!checkPermission(recordAudioPermission)) {
101 return PERMISSION_DENIED;
102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 Mutex::Autolock lock(mLock);
104 if (mRecorder == NULL) {
105 LOGE("recorder is not initialized");
Gloria Wang608a2632009-10-29 15:46:37 -0700106 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700108 return mRecorder->setAudioSource((audio_source_t)as);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109}
110
111status_t MediaRecorderClient::setOutputFormat(int of)
112{
113 LOGV("setOutputFormat(%d)", of);
114 Mutex::Autolock lock(mLock);
115 if (mRecorder == NULL) {
116 LOGE("recorder is not initialized");
117 return NO_INIT;
118 }
119 return mRecorder->setOutputFormat((output_format)of);
120}
121
122status_t MediaRecorderClient::setVideoEncoder(int ve)
123{
124 LOGV("setVideoEncoder(%d)", ve);
125 Mutex::Autolock lock(mLock);
126 if (mRecorder == NULL) {
127 LOGE("recorder is not initialized");
128 return NO_INIT;
129 }
130 return mRecorder->setVideoEncoder((video_encoder)ve);
131}
132
133status_t MediaRecorderClient::setAudioEncoder(int ae)
134{
135 LOGV("setAudioEncoder(%d)", ae);
136 Mutex::Autolock lock(mLock);
137 if (mRecorder == NULL) {
138 LOGE("recorder is not initialized");
139 return NO_INIT;
140 }
141 return mRecorder->setAudioEncoder((audio_encoder)ae);
142}
143
144status_t MediaRecorderClient::setOutputFile(const char* path)
145{
146 LOGV("setOutputFile(%s)", path);
147 Mutex::Autolock lock(mLock);
148 if (mRecorder == NULL) {
149 LOGE("recorder is not initialized");
150 return NO_INIT;
151 }
152 return mRecorder->setOutputFile(path);
153}
154
155status_t MediaRecorderClient::setOutputFile(int fd, int64_t offset, int64_t length)
156{
157 LOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length);
158 Mutex::Autolock lock(mLock);
159 if (mRecorder == NULL) {
160 LOGE("recorder is not initialized");
161 return NO_INIT;
162 }
163 return mRecorder->setOutputFile(fd, offset, length);
164}
165
Nipun Kwatrab33a5ae2010-08-26 17:20:53 -0700166status_t MediaRecorderClient::setOutputFileAuxiliary(int fd)
167{
168 LOGV("setOutputFileAuxiliary(%d)", fd);
169 Mutex::Autolock lock(mLock);
170 if (mRecorder == NULL) {
171 LOGE("recorder is not initialized");
172 return NO_INIT;
173 }
174 return mRecorder->setOutputFileAuxiliary(fd);
175}
176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177status_t MediaRecorderClient::setVideoSize(int width, int height)
178{
179 LOGV("setVideoSize(%dx%d)", width, height);
180 Mutex::Autolock lock(mLock);
181 if (mRecorder == NULL) {
182 LOGE("recorder is not initialized");
183 return NO_INIT;
184 }
185 return mRecorder->setVideoSize(width, height);
186}
187
188status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second)
189{
190 LOGV("setVideoFrameRate(%d)", frames_per_second);
191 Mutex::Autolock lock(mLock);
192 if (mRecorder == NULL) {
193 LOGE("recorder is not initialized");
194 return NO_INIT;
195 }
196 return mRecorder->setVideoFrameRate(frames_per_second);
197}
198
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700199status_t MediaRecorderClient::setParameters(const String8& params) {
200 LOGV("setParameters(%s)", params.string());
201 Mutex::Autolock lock(mLock);
202 if (mRecorder == NULL) {
203 LOGE("recorder is not initialized");
204 return NO_INIT;
205 }
206 return mRecorder->setParameters(params);
207}
208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209status_t MediaRecorderClient::prepare()
210{
211 LOGV("prepare");
212 Mutex::Autolock lock(mLock);
213 if (mRecorder == NULL) {
214 LOGE("recorder is not initialized");
215 return NO_INIT;
216 }
217 return mRecorder->prepare();
218}
219
220
221status_t MediaRecorderClient::getMaxAmplitude(int* max)
222{
223 LOGV("getMaxAmplitude");
224 Mutex::Autolock lock(mLock);
225 if (mRecorder == NULL) {
226 LOGE("recorder is not initialized");
227 return NO_INIT;
228 }
229 return mRecorder->getMaxAmplitude(max);
230}
231
232status_t MediaRecorderClient::start()
233{
234 LOGV("start");
235 Mutex::Autolock lock(mLock);
236 if (mRecorder == NULL) {
237 LOGE("recorder is not initialized");
238 return NO_INIT;
239 }
240 return mRecorder->start();
241
242}
243
244status_t MediaRecorderClient::stop()
245{
246 LOGV("stop");
247 Mutex::Autolock lock(mLock);
248 if (mRecorder == NULL) {
249 LOGE("recorder is not initialized");
250 return NO_INIT;
251 }
252 return mRecorder->stop();
253}
254
255status_t MediaRecorderClient::init()
256{
257 LOGV("init");
258 Mutex::Autolock lock(mLock);
259 if (mRecorder == NULL) {
260 LOGE("recorder is not initialized");
261 return NO_INIT;
262 }
263 return mRecorder->init();
264}
265
266status_t MediaRecorderClient::close()
267{
268 LOGV("close");
269 Mutex::Autolock lock(mLock);
270 if (mRecorder == NULL) {
271 LOGE("recorder is not initialized");
272 return NO_INIT;
273 }
274 return mRecorder->close();
275}
276
277
278status_t MediaRecorderClient::reset()
279{
280 LOGV("reset");
281 Mutex::Autolock lock(mLock);
282 if (mRecorder == NULL) {
283 LOGE("recorder is not initialized");
284 return NO_INIT;
285 }
286 return mRecorder->reset();
287}
288
289status_t MediaRecorderClient::release()
290{
291 LOGV("release");
292 Mutex::Autolock lock(mLock);
293 if (mRecorder != NULL) {
294 delete mRecorder;
295 mRecorder = NULL;
Gloria Wang608a2632009-10-29 15:46:37 -0700296 wp<MediaRecorderClient> client(this);
297 mMediaPlayerService->removeMediaRecorderClient(client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 }
299 return NO_ERROR;
300}
301
Gloria Wang608a2632009-10-29 15:46:37 -0700302MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303{
304 LOGV("Client constructor");
305 mPid = pid;
James Dong8ec2d9a2010-11-10 18:42:40 -0800306 mRecorder = new StagefrightRecorder;
Gloria Wang608a2632009-10-29 15:46:37 -0700307 mMediaPlayerService = service;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308}
309
310MediaRecorderClient::~MediaRecorderClient()
311{
312 LOGV("Client destructor");
313 release();
314}
315
James Dongfe1bafe2010-06-25 17:06:47 -0700316status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317{
318 LOGV("setListener");
319 Mutex::Autolock lock(mLock);
320 if (mRecorder == NULL) {
321 LOGE("recorder is not initialized");
322 return NO_INIT;
323 }
324 return mRecorder->setListener(listener);
325}
326
James Dong929642e2010-07-08 11:16:11 -0700327status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) const {
328 if (mRecorder != NULL) {
329 return mRecorder->dump(fd, args);
330 }
331 return OK;
332}
333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334}; // namespace android