blob: 6f80b35275a38115634b2c8f9e20f5f73951976c [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"
Pannag Sanketi897e27b2011-07-01 17:39:39 -070044#include <gui/ISurfaceTexture.h>
Andreas Huberea6a38c2009-11-16 15:43:38 -080045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046namespace android {
47
Dave Sparks17612fc2009-03-27 19:56:11 -070048const char* cameraPermission = "android.permission.CAMERA";
Dave Sparks6690dc52009-05-20 19:20:31 -070049const char* recordAudioPermission = "android.permission.RECORD_AUDIO";
Dave Sparks17612fc2009-03-27 19:56:11 -070050
51static bool checkPermission(const char* permissionString) {
52#ifndef HAVE_ANDROID_OS
53 return true;
54#endif
55 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
56 bool ok = checkCallingPermission(String16(permissionString));
57 if (!ok) LOGE("Request requires %s", permissionString);
58 return ok;
59}
60
Pannag Sanketi897e27b2011-07-01 17:39:39 -070061
62sp<ISurfaceTexture> MediaRecorderClient::querySurfaceMediaSource()
63{
64 LOGV("Query SurfaceMediaSource");
65 Mutex::Autolock lock(mLock);
66 if (mRecorder == NULL) {
67 LOGE("recorder is not initialized");
68 return NULL;
69 }
70 return mRecorder->querySurfaceMediaSource();
71}
72
73
74
Wu-cheng Li42419ce2011-06-01 17:22:24 +080075status_t MediaRecorderClient::setCamera(const sp<ICamera>& camera,
76 const sp<ICameraRecordingProxy>& proxy)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077{
78 LOGV("setCamera");
79 Mutex::Autolock lock(mLock);
80 if (mRecorder == NULL) {
81 LOGE("recorder is not initialized");
82 return NO_INIT;
83 }
Wu-cheng Li42419ce2011-06-01 17:22:24 +080084 return mRecorder->setCamera(camera, proxy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085}
86
Jamie Gennis85cfdd02010-08-10 16:37:53 -070087status_t MediaRecorderClient::setPreviewSurface(const sp<Surface>& surface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088{
89 LOGV("setPreviewSurface");
90 Mutex::Autolock lock(mLock);
91 if (mRecorder == NULL) {
92 LOGE("recorder is not initialized");
93 return NO_INIT;
94 }
95 return mRecorder->setPreviewSurface(surface);
96}
97
98status_t MediaRecorderClient::setVideoSource(int vs)
99{
100 LOGV("setVideoSource(%d)", vs);
Dave Sparks17612fc2009-03-27 19:56:11 -0700101 if (!checkPermission(cameraPermission)) {
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->setVideoSource((video_source)vs);
110}
111
112status_t MediaRecorderClient::setAudioSource(int as)
113{
114 LOGV("setAudioSource(%d)", as);
Dave Sparks6690dc52009-05-20 19:20:31 -0700115 if (!checkPermission(recordAudioPermission)) {
116 return PERMISSION_DENIED;
117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 Mutex::Autolock lock(mLock);
119 if (mRecorder == NULL) {
120 LOGE("recorder is not initialized");
Gloria Wang608a2632009-10-29 15:46:37 -0700121 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700123 return mRecorder->setAudioSource((audio_source_t)as);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124}
125
126status_t MediaRecorderClient::setOutputFormat(int of)
127{
128 LOGV("setOutputFormat(%d)", of);
129 Mutex::Autolock lock(mLock);
130 if (mRecorder == NULL) {
131 LOGE("recorder is not initialized");
132 return NO_INIT;
133 }
134 return mRecorder->setOutputFormat((output_format)of);
135}
136
137status_t MediaRecorderClient::setVideoEncoder(int ve)
138{
139 LOGV("setVideoEncoder(%d)", ve);
140 Mutex::Autolock lock(mLock);
141 if (mRecorder == NULL) {
142 LOGE("recorder is not initialized");
143 return NO_INIT;
144 }
145 return mRecorder->setVideoEncoder((video_encoder)ve);
146}
147
148status_t MediaRecorderClient::setAudioEncoder(int ae)
149{
150 LOGV("setAudioEncoder(%d)", ae);
151 Mutex::Autolock lock(mLock);
152 if (mRecorder == NULL) {
153 LOGE("recorder is not initialized");
154 return NO_INIT;
155 }
156 return mRecorder->setAudioEncoder((audio_encoder)ae);
157}
158
159status_t MediaRecorderClient::setOutputFile(const char* path)
160{
161 LOGV("setOutputFile(%s)", path);
162 Mutex::Autolock lock(mLock);
163 if (mRecorder == NULL) {
164 LOGE("recorder is not initialized");
165 return NO_INIT;
166 }
167 return mRecorder->setOutputFile(path);
168}
169
170status_t MediaRecorderClient::setOutputFile(int fd, int64_t offset, int64_t length)
171{
172 LOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length);
173 Mutex::Autolock lock(mLock);
174 if (mRecorder == NULL) {
175 LOGE("recorder is not initialized");
176 return NO_INIT;
177 }
178 return mRecorder->setOutputFile(fd, offset, length);
179}
180
181status_t MediaRecorderClient::setVideoSize(int width, int height)
182{
183 LOGV("setVideoSize(%dx%d)", width, height);
184 Mutex::Autolock lock(mLock);
185 if (mRecorder == NULL) {
186 LOGE("recorder is not initialized");
187 return NO_INIT;
188 }
189 return mRecorder->setVideoSize(width, height);
190}
191
192status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second)
193{
194 LOGV("setVideoFrameRate(%d)", frames_per_second);
195 Mutex::Autolock lock(mLock);
196 if (mRecorder == NULL) {
197 LOGE("recorder is not initialized");
198 return NO_INIT;
199 }
200 return mRecorder->setVideoFrameRate(frames_per_second);
201}
202
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700203status_t MediaRecorderClient::setParameters(const String8& params) {
204 LOGV("setParameters(%s)", params.string());
205 Mutex::Autolock lock(mLock);
206 if (mRecorder == NULL) {
207 LOGE("recorder is not initialized");
208 return NO_INIT;
209 }
210 return mRecorder->setParameters(params);
211}
212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213status_t MediaRecorderClient::prepare()
214{
215 LOGV("prepare");
216 Mutex::Autolock lock(mLock);
217 if (mRecorder == NULL) {
218 LOGE("recorder is not initialized");
219 return NO_INIT;
220 }
221 return mRecorder->prepare();
222}
223
224
225status_t MediaRecorderClient::getMaxAmplitude(int* max)
226{
227 LOGV("getMaxAmplitude");
228 Mutex::Autolock lock(mLock);
229 if (mRecorder == NULL) {
230 LOGE("recorder is not initialized");
231 return NO_INIT;
232 }
233 return mRecorder->getMaxAmplitude(max);
234}
235
236status_t MediaRecorderClient::start()
237{
238 LOGV("start");
239 Mutex::Autolock lock(mLock);
240 if (mRecorder == NULL) {
241 LOGE("recorder is not initialized");
242 return NO_INIT;
243 }
244 return mRecorder->start();
245
246}
247
248status_t MediaRecorderClient::stop()
249{
250 LOGV("stop");
251 Mutex::Autolock lock(mLock);
252 if (mRecorder == NULL) {
253 LOGE("recorder is not initialized");
254 return NO_INIT;
255 }
256 return mRecorder->stop();
257}
258
259status_t MediaRecorderClient::init()
260{
261 LOGV("init");
262 Mutex::Autolock lock(mLock);
263 if (mRecorder == NULL) {
264 LOGE("recorder is not initialized");
265 return NO_INIT;
266 }
267 return mRecorder->init();
268}
269
270status_t MediaRecorderClient::close()
271{
272 LOGV("close");
273 Mutex::Autolock lock(mLock);
274 if (mRecorder == NULL) {
275 LOGE("recorder is not initialized");
276 return NO_INIT;
277 }
278 return mRecorder->close();
279}
280
281
282status_t MediaRecorderClient::reset()
283{
284 LOGV("reset");
285 Mutex::Autolock lock(mLock);
286 if (mRecorder == NULL) {
287 LOGE("recorder is not initialized");
288 return NO_INIT;
289 }
290 return mRecorder->reset();
291}
292
293status_t MediaRecorderClient::release()
294{
295 LOGV("release");
296 Mutex::Autolock lock(mLock);
297 if (mRecorder != NULL) {
298 delete mRecorder;
299 mRecorder = NULL;
Gloria Wang608a2632009-10-29 15:46:37 -0700300 wp<MediaRecorderClient> client(this);
301 mMediaPlayerService->removeMediaRecorderClient(client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
303 return NO_ERROR;
304}
305
Gloria Wang608a2632009-10-29 15:46:37 -0700306MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307{
308 LOGV("Client constructor");
309 mPid = pid;
James Dong8ec2d9a2010-11-10 18:42:40 -0800310 mRecorder = new StagefrightRecorder;
Gloria Wang608a2632009-10-29 15:46:37 -0700311 mMediaPlayerService = service;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312}
313
314MediaRecorderClient::~MediaRecorderClient()
315{
316 LOGV("Client destructor");
317 release();
318}
319
James Dongfe1bafe2010-06-25 17:06:47 -0700320status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321{
322 LOGV("setListener");
323 Mutex::Autolock lock(mLock);
324 if (mRecorder == NULL) {
325 LOGE("recorder is not initialized");
326 return NO_INIT;
327 }
328 return mRecorder->setListener(listener);
329}
330
James Dong929642e2010-07-08 11:16:11 -0700331status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) const {
332 if (mRecorder != NULL) {
333 return mRecorder->dump(fd, args);
334 }
335 return OK;
336}
337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338}; // namespace android