blob: 1a1780c29fca0e7ee287958782faf9d9608e6eab [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038#include "MediaRecorderClient.h"
Gloria Wang608a2632009-10-29 15:46:37 -070039#include "MediaPlayerService.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Andreas Huberea6a38c2009-11-16 15:43:38 -080041#include "StagefrightRecorder.h"
42
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043namespace android {
44
Dave Sparks17612fc2009-03-27 19:56:11 -070045const char* cameraPermission = "android.permission.CAMERA";
Dave Sparks6690dc52009-05-20 19:20:31 -070046const char* recordAudioPermission = "android.permission.RECORD_AUDIO";
Dave Sparks17612fc2009-03-27 19:56:11 -070047
48static bool checkPermission(const char* permissionString) {
49#ifndef HAVE_ANDROID_OS
50 return true;
51#endif
52 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
53 bool ok = checkCallingPermission(String16(permissionString));
54 if (!ok) LOGE("Request requires %s", permissionString);
55 return ok;
56}
57
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058status_t MediaRecorderClient::setCamera(const sp<ICamera>& camera)
59{
60 LOGV("setCamera");
61 Mutex::Autolock lock(mLock);
62 if (mRecorder == NULL) {
63 LOGE("recorder is not initialized");
64 return NO_INIT;
65 }
66 return mRecorder->setCamera(camera);
67}
68
Jamie Gennis85cfdd02010-08-10 16:37:53 -070069status_t MediaRecorderClient::setPreviewSurface(const sp<Surface>& surface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070{
71 LOGV("setPreviewSurface");
72 Mutex::Autolock lock(mLock);
73 if (mRecorder == NULL) {
74 LOGE("recorder is not initialized");
75 return NO_INIT;
76 }
77 return mRecorder->setPreviewSurface(surface);
78}
79
80status_t MediaRecorderClient::setVideoSource(int vs)
81{
82 LOGV("setVideoSource(%d)", vs);
Dave Sparks17612fc2009-03-27 19:56:11 -070083 if (!checkPermission(cameraPermission)) {
84 return PERMISSION_DENIED;
85 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 Mutex::Autolock lock(mLock);
87 if (mRecorder == NULL) {
88 LOGE("recorder is not initialized");
Gloria Wang608a2632009-10-29 15:46:37 -070089 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 }
91 return mRecorder->setVideoSource((video_source)vs);
92}
93
94status_t MediaRecorderClient::setAudioSource(int as)
95{
96 LOGV("setAudioSource(%d)", as);
Dave Sparks6690dc52009-05-20 19:20:31 -070097 if (!checkPermission(recordAudioPermission)) {
98 return PERMISSION_DENIED;
99 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 Mutex::Autolock lock(mLock);
101 if (mRecorder == NULL) {
102 LOGE("recorder is not initialized");
Gloria Wang608a2632009-10-29 15:46:37 -0700103 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 }
105 return mRecorder->setAudioSource((audio_source)as);
106}
107
108status_t MediaRecorderClient::setOutputFormat(int of)
109{
110 LOGV("setOutputFormat(%d)", of);
111 Mutex::Autolock lock(mLock);
112 if (mRecorder == NULL) {
113 LOGE("recorder is not initialized");
114 return NO_INIT;
115 }
116 return mRecorder->setOutputFormat((output_format)of);
117}
118
119status_t MediaRecorderClient::setVideoEncoder(int ve)
120{
121 LOGV("setVideoEncoder(%d)", ve);
122 Mutex::Autolock lock(mLock);
123 if (mRecorder == NULL) {
124 LOGE("recorder is not initialized");
125 return NO_INIT;
126 }
127 return mRecorder->setVideoEncoder((video_encoder)ve);
128}
129
130status_t MediaRecorderClient::setAudioEncoder(int ae)
131{
132 LOGV("setAudioEncoder(%d)", ae);
133 Mutex::Autolock lock(mLock);
134 if (mRecorder == NULL) {
135 LOGE("recorder is not initialized");
136 return NO_INIT;
137 }
138 return mRecorder->setAudioEncoder((audio_encoder)ae);
139}
140
141status_t MediaRecorderClient::setOutputFile(const char* path)
142{
143 LOGV("setOutputFile(%s)", path);
144 Mutex::Autolock lock(mLock);
145 if (mRecorder == NULL) {
146 LOGE("recorder is not initialized");
147 return NO_INIT;
148 }
149 return mRecorder->setOutputFile(path);
150}
151
152status_t MediaRecorderClient::setOutputFile(int fd, int64_t offset, int64_t length)
153{
154 LOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length);
155 Mutex::Autolock lock(mLock);
156 if (mRecorder == NULL) {
157 LOGE("recorder is not initialized");
158 return NO_INIT;
159 }
160 return mRecorder->setOutputFile(fd, offset, length);
161}
162
Nipun Kwatrab33a5ae2010-08-26 17:20:53 -0700163status_t MediaRecorderClient::setOutputFileAuxiliary(int fd)
164{
165 LOGV("setOutputFileAuxiliary(%d)", fd);
166 Mutex::Autolock lock(mLock);
167 if (mRecorder == NULL) {
168 LOGE("recorder is not initialized");
169 return NO_INIT;
170 }
171 return mRecorder->setOutputFileAuxiliary(fd);
172}
173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174status_t MediaRecorderClient::setVideoSize(int width, int height)
175{
176 LOGV("setVideoSize(%dx%d)", width, height);
177 Mutex::Autolock lock(mLock);
178 if (mRecorder == NULL) {
179 LOGE("recorder is not initialized");
180 return NO_INIT;
181 }
182 return mRecorder->setVideoSize(width, height);
183}
184
185status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second)
186{
187 LOGV("setVideoFrameRate(%d)", frames_per_second);
188 Mutex::Autolock lock(mLock);
189 if (mRecorder == NULL) {
190 LOGE("recorder is not initialized");
191 return NO_INIT;
192 }
193 return mRecorder->setVideoFrameRate(frames_per_second);
194}
195
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700196status_t MediaRecorderClient::setParameters(const String8& params) {
197 LOGV("setParameters(%s)", params.string());
198 Mutex::Autolock lock(mLock);
199 if (mRecorder == NULL) {
200 LOGE("recorder is not initialized");
201 return NO_INIT;
202 }
203 return mRecorder->setParameters(params);
204}
205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206status_t MediaRecorderClient::prepare()
207{
208 LOGV("prepare");
209 Mutex::Autolock lock(mLock);
210 if (mRecorder == NULL) {
211 LOGE("recorder is not initialized");
212 return NO_INIT;
213 }
214 return mRecorder->prepare();
215}
216
217
218status_t MediaRecorderClient::getMaxAmplitude(int* max)
219{
220 LOGV("getMaxAmplitude");
221 Mutex::Autolock lock(mLock);
222 if (mRecorder == NULL) {
223 LOGE("recorder is not initialized");
224 return NO_INIT;
225 }
226 return mRecorder->getMaxAmplitude(max);
227}
228
229status_t MediaRecorderClient::start()
230{
231 LOGV("start");
232 Mutex::Autolock lock(mLock);
233 if (mRecorder == NULL) {
234 LOGE("recorder is not initialized");
235 return NO_INIT;
236 }
237 return mRecorder->start();
238
239}
240
241status_t MediaRecorderClient::stop()
242{
243 LOGV("stop");
244 Mutex::Autolock lock(mLock);
245 if (mRecorder == NULL) {
246 LOGE("recorder is not initialized");
247 return NO_INIT;
248 }
249 return mRecorder->stop();
250}
251
252status_t MediaRecorderClient::init()
253{
254 LOGV("init");
255 Mutex::Autolock lock(mLock);
256 if (mRecorder == NULL) {
257 LOGE("recorder is not initialized");
258 return NO_INIT;
259 }
260 return mRecorder->init();
261}
262
263status_t MediaRecorderClient::close()
264{
265 LOGV("close");
266 Mutex::Autolock lock(mLock);
267 if (mRecorder == NULL) {
268 LOGE("recorder is not initialized");
269 return NO_INIT;
270 }
271 return mRecorder->close();
272}
273
274
275status_t MediaRecorderClient::reset()
276{
277 LOGV("reset");
278 Mutex::Autolock lock(mLock);
279 if (mRecorder == NULL) {
280 LOGE("recorder is not initialized");
281 return NO_INIT;
282 }
283 return mRecorder->reset();
284}
285
286status_t MediaRecorderClient::release()
287{
288 LOGV("release");
289 Mutex::Autolock lock(mLock);
290 if (mRecorder != NULL) {
291 delete mRecorder;
292 mRecorder = NULL;
Gloria Wang608a2632009-10-29 15:46:37 -0700293 wp<MediaRecorderClient> client(this);
294 mMediaPlayerService->removeMediaRecorderClient(client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 }
296 return NO_ERROR;
297}
298
Gloria Wang608a2632009-10-29 15:46:37 -0700299MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300{
301 LOGV("Client constructor");
302 mPid = pid;
James Dong8ec2d9a2010-11-10 18:42:40 -0800303 mRecorder = new StagefrightRecorder;
Gloria Wang608a2632009-10-29 15:46:37 -0700304 mMediaPlayerService = service;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305}
306
307MediaRecorderClient::~MediaRecorderClient()
308{
309 LOGV("Client destructor");
310 release();
311}
312
James Dongfe1bafe2010-06-25 17:06:47 -0700313status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314{
315 LOGV("setListener");
316 Mutex::Autolock lock(mLock);
317 if (mRecorder == NULL) {
318 LOGE("recorder is not initialized");
319 return NO_INIT;
320 }
321 return mRecorder->setListener(listener);
322}
323
James Dong929642e2010-07-08 11:16:11 -0700324status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) const {
325 if (mRecorder != NULL) {
326 return mRecorder->dump(fd, args);
327 }
328 return OK;
329}
330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331}; // namespace android