blob: 5a47384fc780f755b4675982844e217d3874a071 [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 Zavin24fc2fb2011-04-19 22:30:36 -070038#include <hardware/audio.h>
39
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060status_t MediaRecorderClient::setCamera(const sp<ICamera>& camera)
61{
62 LOGV("setCamera");
63 Mutex::Autolock lock(mLock);
64 if (mRecorder == NULL) {
65 LOGE("recorder is not initialized");
66 return NO_INIT;
67 }
68 return mRecorder->setCamera(camera);
69}
70
Jamie Gennis85cfdd02010-08-10 16:37:53 -070071status_t MediaRecorderClient::setPreviewSurface(const sp<Surface>& surface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072{
73 LOGV("setPreviewSurface");
74 Mutex::Autolock lock(mLock);
75 if (mRecorder == NULL) {
76 LOGE("recorder is not initialized");
77 return NO_INIT;
78 }
79 return mRecorder->setPreviewSurface(surface);
80}
81
82status_t MediaRecorderClient::setVideoSource(int vs)
83{
84 LOGV("setVideoSource(%d)", vs);
Dave Sparks17612fc2009-03-27 19:56:11 -070085 if (!checkPermission(cameraPermission)) {
86 return PERMISSION_DENIED;
87 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 Mutex::Autolock lock(mLock);
89 if (mRecorder == NULL) {
90 LOGE("recorder is not initialized");
Gloria Wang608a2632009-10-29 15:46:37 -070091 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 }
93 return mRecorder->setVideoSource((video_source)vs);
94}
95
96status_t MediaRecorderClient::setAudioSource(int as)
97{
98 LOGV("setAudioSource(%d)", as);
Dave Sparks6690dc52009-05-20 19:20:31 -070099 if (!checkPermission(recordAudioPermission)) {
100 return PERMISSION_DENIED;
101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 Mutex::Autolock lock(mLock);
103 if (mRecorder == NULL) {
104 LOGE("recorder is not initialized");
Gloria Wang608a2632009-10-29 15:46:37 -0700105 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700107 return mRecorder->setAudioSource((audio_source_t)as);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108}
109
110status_t MediaRecorderClient::setOutputFormat(int of)
111{
112 LOGV("setOutputFormat(%d)", of);
113 Mutex::Autolock lock(mLock);
114 if (mRecorder == NULL) {
115 LOGE("recorder is not initialized");
116 return NO_INIT;
117 }
118 return mRecorder->setOutputFormat((output_format)of);
119}
120
121status_t MediaRecorderClient::setVideoEncoder(int ve)
122{
123 LOGV("setVideoEncoder(%d)", ve);
124 Mutex::Autolock lock(mLock);
125 if (mRecorder == NULL) {
126 LOGE("recorder is not initialized");
127 return NO_INIT;
128 }
129 return mRecorder->setVideoEncoder((video_encoder)ve);
130}
131
132status_t MediaRecorderClient::setAudioEncoder(int ae)
133{
134 LOGV("setAudioEncoder(%d)", ae);
135 Mutex::Autolock lock(mLock);
136 if (mRecorder == NULL) {
137 LOGE("recorder is not initialized");
138 return NO_INIT;
139 }
140 return mRecorder->setAudioEncoder((audio_encoder)ae);
141}
142
143status_t MediaRecorderClient::setOutputFile(const char* path)
144{
145 LOGV("setOutputFile(%s)", path);
146 Mutex::Autolock lock(mLock);
147 if (mRecorder == NULL) {
148 LOGE("recorder is not initialized");
149 return NO_INIT;
150 }
151 return mRecorder->setOutputFile(path);
152}
153
154status_t MediaRecorderClient::setOutputFile(int fd, int64_t offset, int64_t length)
155{
156 LOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length);
157 Mutex::Autolock lock(mLock);
158 if (mRecorder == NULL) {
159 LOGE("recorder is not initialized");
160 return NO_INIT;
161 }
162 return mRecorder->setOutputFile(fd, offset, length);
163}
164
Nipun Kwatrab33a5ae2010-08-26 17:20:53 -0700165status_t MediaRecorderClient::setOutputFileAuxiliary(int fd)
166{
167 LOGV("setOutputFileAuxiliary(%d)", fd);
168 Mutex::Autolock lock(mLock);
169 if (mRecorder == NULL) {
170 LOGE("recorder is not initialized");
171 return NO_INIT;
172 }
173 return mRecorder->setOutputFileAuxiliary(fd);
174}
175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176status_t MediaRecorderClient::setVideoSize(int width, int height)
177{
178 LOGV("setVideoSize(%dx%d)", width, height);
179 Mutex::Autolock lock(mLock);
180 if (mRecorder == NULL) {
181 LOGE("recorder is not initialized");
182 return NO_INIT;
183 }
184 return mRecorder->setVideoSize(width, height);
185}
186
187status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second)
188{
189 LOGV("setVideoFrameRate(%d)", frames_per_second);
190 Mutex::Autolock lock(mLock);
191 if (mRecorder == NULL) {
192 LOGE("recorder is not initialized");
193 return NO_INIT;
194 }
195 return mRecorder->setVideoFrameRate(frames_per_second);
196}
197
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700198status_t MediaRecorderClient::setParameters(const String8& params) {
199 LOGV("setParameters(%s)", params.string());
200 Mutex::Autolock lock(mLock);
201 if (mRecorder == NULL) {
202 LOGE("recorder is not initialized");
203 return NO_INIT;
204 }
205 return mRecorder->setParameters(params);
206}
207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208status_t MediaRecorderClient::prepare()
209{
210 LOGV("prepare");
211 Mutex::Autolock lock(mLock);
212 if (mRecorder == NULL) {
213 LOGE("recorder is not initialized");
214 return NO_INIT;
215 }
216 return mRecorder->prepare();
217}
218
219
220status_t MediaRecorderClient::getMaxAmplitude(int* max)
221{
222 LOGV("getMaxAmplitude");
223 Mutex::Autolock lock(mLock);
224 if (mRecorder == NULL) {
225 LOGE("recorder is not initialized");
226 return NO_INIT;
227 }
228 return mRecorder->getMaxAmplitude(max);
229}
230
231status_t MediaRecorderClient::start()
232{
233 LOGV("start");
234 Mutex::Autolock lock(mLock);
235 if (mRecorder == NULL) {
236 LOGE("recorder is not initialized");
237 return NO_INIT;
238 }
239 return mRecorder->start();
240
241}
242
243status_t MediaRecorderClient::stop()
244{
245 LOGV("stop");
246 Mutex::Autolock lock(mLock);
247 if (mRecorder == NULL) {
248 LOGE("recorder is not initialized");
249 return NO_INIT;
250 }
251 return mRecorder->stop();
252}
253
254status_t MediaRecorderClient::init()
255{
256 LOGV("init");
257 Mutex::Autolock lock(mLock);
258 if (mRecorder == NULL) {
259 LOGE("recorder is not initialized");
260 return NO_INIT;
261 }
262 return mRecorder->init();
263}
264
265status_t MediaRecorderClient::close()
266{
267 LOGV("close");
268 Mutex::Autolock lock(mLock);
269 if (mRecorder == NULL) {
270 LOGE("recorder is not initialized");
271 return NO_INIT;
272 }
273 return mRecorder->close();
274}
275
276
277status_t MediaRecorderClient::reset()
278{
279 LOGV("reset");
280 Mutex::Autolock lock(mLock);
281 if (mRecorder == NULL) {
282 LOGE("recorder is not initialized");
283 return NO_INIT;
284 }
285 return mRecorder->reset();
286}
287
288status_t MediaRecorderClient::release()
289{
290 LOGV("release");
291 Mutex::Autolock lock(mLock);
292 if (mRecorder != NULL) {
293 delete mRecorder;
294 mRecorder = NULL;
Gloria Wang608a2632009-10-29 15:46:37 -0700295 wp<MediaRecorderClient> client(this);
296 mMediaPlayerService->removeMediaRecorderClient(client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 }
298 return NO_ERROR;
299}
300
Gloria Wang608a2632009-10-29 15:46:37 -0700301MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302{
303 LOGV("Client constructor");
304 mPid = pid;
James Dong8ec2d9a2010-11-10 18:42:40 -0800305 mRecorder = new StagefrightRecorder;
Gloria Wang608a2632009-10-29 15:46:37 -0700306 mMediaPlayerService = service;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307}
308
309MediaRecorderClient::~MediaRecorderClient()
310{
311 LOGV("Client destructor");
312 release();
313}
314
James Dongfe1bafe2010-06-25 17:06:47 -0700315status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316{
317 LOGV("setListener");
318 Mutex::Autolock lock(mLock);
319 if (mRecorder == NULL) {
320 LOGE("recorder is not initialized");
321 return NO_INIT;
322 }
323 return mRecorder->setListener(listener);
324}
325
James Dong929642e2010-07-08 11:16:11 -0700326status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) const {
327 if (mRecorder != NULL) {
328 return mRecorder->dump(fd, args);
329 }
330 return OK;
331}
332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333}; // namespace android