blob: be6a8be6b23371e41c2a001ecccc31390302fc23 [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
34#ifndef NO_OPENCORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include <media/PVMediaRecorder.h>
Andreas Huberbfb9fb12009-12-03 11:31:19 -080036#endif
37
Dave Sparks17612fc2009-03-27 19:56:11 -070038#include <utils/String16.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Gloria Wang608a2632009-10-29 15:46:37 -070040#include <media/AudioTrack.h>
41
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042#include "MediaRecorderClient.h"
Gloria Wang608a2632009-10-29 15:46:37 -070043#include "MediaPlayerService.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Andreas Huberea6a38c2009-11-16 15:43:38 -080045#include "StagefrightRecorder.h"
46
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047namespace android {
48
Dave Sparks17612fc2009-03-27 19:56:11 -070049const char* cameraPermission = "android.permission.CAMERA";
Dave Sparks6690dc52009-05-20 19:20:31 -070050const char* recordAudioPermission = "android.permission.RECORD_AUDIO";
Dave Sparks17612fc2009-03-27 19:56:11 -070051
52static bool checkPermission(const char* permissionString) {
53#ifndef HAVE_ANDROID_OS
54 return true;
55#endif
56 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
57 bool ok = checkCallingPermission(String16(permissionString));
58 if (!ok) LOGE("Request requires %s", permissionString);
59 return ok;
60}
61
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062status_t MediaRecorderClient::setCamera(const sp<ICamera>& camera)
63{
64 LOGV("setCamera");
65 Mutex::Autolock lock(mLock);
66 if (mRecorder == NULL) {
67 LOGE("recorder is not initialized");
68 return NO_INIT;
69 }
70 return mRecorder->setCamera(camera);
71}
72
Jamie Gennis85cfdd02010-08-10 16:37:53 -070073status_t MediaRecorderClient::setPreviewSurface(const sp<Surface>& surface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074{
75 LOGV("setPreviewSurface");
76 Mutex::Autolock lock(mLock);
77 if (mRecorder == NULL) {
78 LOGE("recorder is not initialized");
79 return NO_INIT;
80 }
81 return mRecorder->setPreviewSurface(surface);
82}
83
84status_t MediaRecorderClient::setVideoSource(int vs)
85{
86 LOGV("setVideoSource(%d)", vs);
Dave Sparks17612fc2009-03-27 19:56:11 -070087 if (!checkPermission(cameraPermission)) {
88 return PERMISSION_DENIED;
89 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 Mutex::Autolock lock(mLock);
91 if (mRecorder == NULL) {
92 LOGE("recorder is not initialized");
Gloria Wang608a2632009-10-29 15:46:37 -070093 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 }
95 return mRecorder->setVideoSource((video_source)vs);
96}
97
98status_t MediaRecorderClient::setAudioSource(int as)
99{
100 LOGV("setAudioSource(%d)", as);
Dave Sparks6690dc52009-05-20 19:20:31 -0700101 if (!checkPermission(recordAudioPermission)) {
102 return PERMISSION_DENIED;
103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 Mutex::Autolock lock(mLock);
105 if (mRecorder == NULL) {
106 LOGE("recorder is not initialized");
Gloria Wang608a2632009-10-29 15:46:37 -0700107 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 }
109 return mRecorder->setAudioSource((audio_source)as);
110}
111
112status_t MediaRecorderClient::setOutputFormat(int of)
113{
114 LOGV("setOutputFormat(%d)", of);
115 Mutex::Autolock lock(mLock);
116 if (mRecorder == NULL) {
117 LOGE("recorder is not initialized");
118 return NO_INIT;
119 }
120 return mRecorder->setOutputFormat((output_format)of);
121}
122
123status_t MediaRecorderClient::setVideoEncoder(int ve)
124{
125 LOGV("setVideoEncoder(%d)", ve);
126 Mutex::Autolock lock(mLock);
127 if (mRecorder == NULL) {
128 LOGE("recorder is not initialized");
129 return NO_INIT;
130 }
131 return mRecorder->setVideoEncoder((video_encoder)ve);
132}
133
134status_t MediaRecorderClient::setAudioEncoder(int ae)
135{
136 LOGV("setAudioEncoder(%d)", ae);
137 Mutex::Autolock lock(mLock);
138 if (mRecorder == NULL) {
139 LOGE("recorder is not initialized");
140 return NO_INIT;
141 }
142 return mRecorder->setAudioEncoder((audio_encoder)ae);
143}
144
145status_t MediaRecorderClient::setOutputFile(const char* path)
146{
147 LOGV("setOutputFile(%s)", path);
148 Mutex::Autolock lock(mLock);
149 if (mRecorder == NULL) {
150 LOGE("recorder is not initialized");
151 return NO_INIT;
152 }
153 return mRecorder->setOutputFile(path);
154}
155
156status_t MediaRecorderClient::setOutputFile(int fd, int64_t offset, int64_t length)
157{
158 LOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length);
159 Mutex::Autolock lock(mLock);
160 if (mRecorder == NULL) {
161 LOGE("recorder is not initialized");
162 return NO_INIT;
163 }
164 return mRecorder->setOutputFile(fd, offset, length);
165}
166
Nipun Kwatrab33a5ae2010-08-26 17:20:53 -0700167status_t MediaRecorderClient::setOutputFileAuxiliary(int fd)
168{
169 LOGV("setOutputFileAuxiliary(%d)", fd);
170 Mutex::Autolock lock(mLock);
171 if (mRecorder == NULL) {
172 LOGE("recorder is not initialized");
173 return NO_INIT;
174 }
175 return mRecorder->setOutputFileAuxiliary(fd);
176}
177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178status_t MediaRecorderClient::setVideoSize(int width, int height)
179{
180 LOGV("setVideoSize(%dx%d)", width, height);
181 Mutex::Autolock lock(mLock);
182 if (mRecorder == NULL) {
183 LOGE("recorder is not initialized");
184 return NO_INIT;
185 }
186 return mRecorder->setVideoSize(width, height);
187}
188
189status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second)
190{
191 LOGV("setVideoFrameRate(%d)", frames_per_second);
192 Mutex::Autolock lock(mLock);
193 if (mRecorder == NULL) {
194 LOGE("recorder is not initialized");
195 return NO_INIT;
196 }
197 return mRecorder->setVideoFrameRate(frames_per_second);
198}
199
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700200status_t MediaRecorderClient::setParameters(const String8& params) {
201 LOGV("setParameters(%s)", params.string());
202 Mutex::Autolock lock(mLock);
203 if (mRecorder == NULL) {
204 LOGE("recorder is not initialized");
205 return NO_INIT;
206 }
207 return mRecorder->setParameters(params);
208}
209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210status_t MediaRecorderClient::prepare()
211{
212 LOGV("prepare");
213 Mutex::Autolock lock(mLock);
214 if (mRecorder == NULL) {
215 LOGE("recorder is not initialized");
216 return NO_INIT;
217 }
218 return mRecorder->prepare();
219}
220
221
222status_t MediaRecorderClient::getMaxAmplitude(int* max)
223{
224 LOGV("getMaxAmplitude");
225 Mutex::Autolock lock(mLock);
226 if (mRecorder == NULL) {
227 LOGE("recorder is not initialized");
228 return NO_INIT;
229 }
230 return mRecorder->getMaxAmplitude(max);
231}
232
233status_t MediaRecorderClient::start()
234{
235 LOGV("start");
236 Mutex::Autolock lock(mLock);
237 if (mRecorder == NULL) {
238 LOGE("recorder is not initialized");
239 return NO_INIT;
240 }
241 return mRecorder->start();
242
243}
244
245status_t MediaRecorderClient::stop()
246{
247 LOGV("stop");
248 Mutex::Autolock lock(mLock);
249 if (mRecorder == NULL) {
250 LOGE("recorder is not initialized");
251 return NO_INIT;
252 }
253 return mRecorder->stop();
254}
255
256status_t MediaRecorderClient::init()
257{
258 LOGV("init");
259 Mutex::Autolock lock(mLock);
260 if (mRecorder == NULL) {
261 LOGE("recorder is not initialized");
262 return NO_INIT;
263 }
264 return mRecorder->init();
265}
266
267status_t MediaRecorderClient::close()
268{
269 LOGV("close");
270 Mutex::Autolock lock(mLock);
271 if (mRecorder == NULL) {
272 LOGE("recorder is not initialized");
273 return NO_INIT;
274 }
275 return mRecorder->close();
276}
277
278
279status_t MediaRecorderClient::reset()
280{
281 LOGV("reset");
282 Mutex::Autolock lock(mLock);
283 if (mRecorder == NULL) {
284 LOGE("recorder is not initialized");
285 return NO_INIT;
286 }
287 return mRecorder->reset();
288}
289
290status_t MediaRecorderClient::release()
291{
292 LOGV("release");
293 Mutex::Autolock lock(mLock);
294 if (mRecorder != NULL) {
295 delete mRecorder;
296 mRecorder = NULL;
Gloria Wang608a2632009-10-29 15:46:37 -0700297 wp<MediaRecorderClient> client(this);
298 mMediaPlayerService->removeMediaRecorderClient(client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 }
300 return NO_ERROR;
301}
302
Gloria Wang608a2632009-10-29 15:46:37 -0700303MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304{
305 LOGV("Client constructor");
306 mPid = pid;
Andreas Huberea6a38c2009-11-16 15:43:38 -0800307
Andreas Huberea6a38c2009-11-16 15:43:38 -0800308 char value[PROPERTY_VALUE_MAX];
Andreas Huber30cfa202010-08-30 12:46:12 -0700309 if (!property_get("media.stagefright.enable-record", value, NULL)
310 || !strcmp(value, "1") || !strcasecmp(value, "true")) {
Andreas Huberea6a38c2009-11-16 15:43:38 -0800311 mRecorder = new StagefrightRecorder;
312 } else
Andreas Huberbfb9fb12009-12-03 11:31:19 -0800313#ifndef NO_OPENCORE
Andreas Huberea6a38c2009-11-16 15:43:38 -0800314 {
315 mRecorder = new PVMediaRecorder();
316 }
Andreas Huberbfb9fb12009-12-03 11:31:19 -0800317#else
318 {
319 mRecorder = NULL;
320 }
321#endif
Andreas Huberea6a38c2009-11-16 15:43:38 -0800322
Gloria Wang608a2632009-10-29 15:46:37 -0700323 mMediaPlayerService = service;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324}
325
326MediaRecorderClient::~MediaRecorderClient()
327{
328 LOGV("Client destructor");
329 release();
330}
331
James Dongfe1bafe2010-06-25 17:06:47 -0700332status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333{
334 LOGV("setListener");
335 Mutex::Autolock lock(mLock);
336 if (mRecorder == NULL) {
337 LOGE("recorder is not initialized");
338 return NO_INIT;
339 }
340 return mRecorder->setListener(listener);
341}
342
James Dong929642e2010-07-08 11:16:11 -0700343status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) const {
344 if (mRecorder != NULL) {
345 return mRecorder->dump(fd, args);
346 }
347 return OK;
348}
349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350}; // namespace android