2 ** Copyright 2008, The Android Open Source Project
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
8 ** http://www.apache.org/licenses/LICENSE-2.0
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.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "MediaRecorderService"
19 #include <utils/Log.h>
21 #include <sys/types.h>
26 #include <cutils/atomic.h>
27 #include <cutils/properties.h> // for property_get
28 #include <android_runtime/ActivityManager.h>
29 #include <binder/IPCThreadState.h>
30 #include <binder/IServiceManager.h>
31 #include <binder/MemoryHeapBase.h>
32 #include <binder/MemoryBase.h>
35 #include <media/PVMediaRecorder.h>
38 #include <utils/String16.h>
40 #include <media/AudioTrack.h>
42 #include "MediaRecorderClient.h"
43 #include "MediaPlayerService.h"
45 #include "StagefrightRecorder.h"
49 const char* cameraPermission = "android.permission.CAMERA";
50 const char* recordAudioPermission = "android.permission.RECORD_AUDIO";
52 static bool checkPermission(const char* permissionString) {
53 #ifndef HAVE_ANDROID_OS
56 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
57 bool ok = checkCallingPermission(String16(permissionString));
58 if (!ok) LOGE("Request requires %s", permissionString);
62 status_t MediaRecorderClient::setCamera(const sp<ICamera>& camera)
65 Mutex::Autolock lock(mLock);
66 if (mRecorder == NULL) {
67 LOGE("recorder is not initialized");
70 return mRecorder->setCamera(camera);
73 status_t MediaRecorderClient::setPreviewSurface(const sp<ISurface>& surface)
75 LOGV("setPreviewSurface");
76 Mutex::Autolock lock(mLock);
77 if (mRecorder == NULL) {
78 LOGE("recorder is not initialized");
81 return mRecorder->setPreviewSurface(surface);
84 status_t MediaRecorderClient::setVideoSource(int vs)
86 LOGV("setVideoSource(%d)", vs);
87 if (!checkPermission(cameraPermission)) {
88 return PERMISSION_DENIED;
90 Mutex::Autolock lock(mLock);
91 if (mRecorder == NULL) {
92 LOGE("recorder is not initialized");
95 return mRecorder->setVideoSource((video_source)vs);
98 status_t MediaRecorderClient::setAudioSource(int as)
100 LOGV("setAudioSource(%d)", as);
101 if (!checkPermission(recordAudioPermission)) {
102 return PERMISSION_DENIED;
104 Mutex::Autolock lock(mLock);
105 if (mRecorder == NULL) {
106 LOGE("recorder is not initialized");
109 return mRecorder->setAudioSource((audio_source)as);
112 status_t MediaRecorderClient::setOutputFormat(int of)
114 LOGV("setOutputFormat(%d)", of);
115 Mutex::Autolock lock(mLock);
116 if (mRecorder == NULL) {
117 LOGE("recorder is not initialized");
120 return mRecorder->setOutputFormat((output_format)of);
123 status_t MediaRecorderClient::setVideoEncoder(int ve)
125 LOGV("setVideoEncoder(%d)", ve);
126 Mutex::Autolock lock(mLock);
127 if (mRecorder == NULL) {
128 LOGE("recorder is not initialized");
131 return mRecorder->setVideoEncoder((video_encoder)ve);
134 status_t MediaRecorderClient::setAudioEncoder(int ae)
136 LOGV("setAudioEncoder(%d)", ae);
137 Mutex::Autolock lock(mLock);
138 if (mRecorder == NULL) {
139 LOGE("recorder is not initialized");
142 return mRecorder->setAudioEncoder((audio_encoder)ae);
145 status_t MediaRecorderClient::setOutputFile(const char* path)
147 LOGV("setOutputFile(%s)", path);
148 Mutex::Autolock lock(mLock);
149 if (mRecorder == NULL) {
150 LOGE("recorder is not initialized");
153 return mRecorder->setOutputFile(path);
156 status_t MediaRecorderClient::setOutputFile(int fd, int64_t offset, int64_t length)
158 LOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length);
159 Mutex::Autolock lock(mLock);
160 if (mRecorder == NULL) {
161 LOGE("recorder is not initialized");
164 return mRecorder->setOutputFile(fd, offset, length);
167 status_t MediaRecorderClient::setVideoSize(int width, int height)
169 LOGV("setVideoSize(%dx%d)", width, height);
170 Mutex::Autolock lock(mLock);
171 if (mRecorder == NULL) {
172 LOGE("recorder is not initialized");
175 return mRecorder->setVideoSize(width, height);
178 status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second)
180 LOGV("setVideoFrameRate(%d)", frames_per_second);
181 Mutex::Autolock lock(mLock);
182 if (mRecorder == NULL) {
183 LOGE("recorder is not initialized");
186 return mRecorder->setVideoFrameRate(frames_per_second);
189 status_t MediaRecorderClient::setParameters(const String8& params) {
190 LOGV("setParameters(%s)", params.string());
191 Mutex::Autolock lock(mLock);
192 if (mRecorder == NULL) {
193 LOGE("recorder is not initialized");
196 return mRecorder->setParameters(params);
199 status_t MediaRecorderClient::prepare()
202 Mutex::Autolock lock(mLock);
203 if (mRecorder == NULL) {
204 LOGE("recorder is not initialized");
207 return mRecorder->prepare();
211 status_t MediaRecorderClient::getMaxAmplitude(int* max)
213 LOGV("getMaxAmplitude");
214 Mutex::Autolock lock(mLock);
215 if (mRecorder == NULL) {
216 LOGE("recorder is not initialized");
219 return mRecorder->getMaxAmplitude(max);
222 status_t MediaRecorderClient::start()
225 Mutex::Autolock lock(mLock);
226 if (mRecorder == NULL) {
227 LOGE("recorder is not initialized");
230 return mRecorder->start();
234 status_t MediaRecorderClient::stop()
237 Mutex::Autolock lock(mLock);
238 if (mRecorder == NULL) {
239 LOGE("recorder is not initialized");
242 return mRecorder->stop();
245 status_t MediaRecorderClient::init()
248 Mutex::Autolock lock(mLock);
249 if (mRecorder == NULL) {
250 LOGE("recorder is not initialized");
253 return mRecorder->init();
256 status_t MediaRecorderClient::close()
259 Mutex::Autolock lock(mLock);
260 if (mRecorder == NULL) {
261 LOGE("recorder is not initialized");
264 return mRecorder->close();
268 status_t MediaRecorderClient::reset()
271 Mutex::Autolock lock(mLock);
272 if (mRecorder == NULL) {
273 LOGE("recorder is not initialized");
276 return mRecorder->reset();
279 status_t MediaRecorderClient::release()
282 Mutex::Autolock lock(mLock);
283 if (mRecorder != NULL) {
286 wp<MediaRecorderClient> client(this);
287 mMediaPlayerService->removeMediaRecorderClient(client);
292 MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid)
294 LOGV("Client constructor");
297 char value[PROPERTY_VALUE_MAX];
298 if (!property_get("media.stagefright.enable-record", value, NULL)
299 || !strcmp(value, "1") || !strcasecmp(value, "true")) {
300 mRecorder = new StagefrightRecorder;
304 mRecorder = new PVMediaRecorder();
312 mMediaPlayerService = service;
315 MediaRecorderClient::~MediaRecorderClient()
317 LOGV("Client destructor");
321 status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener)
324 Mutex::Autolock lock(mLock);
325 if (mRecorder == NULL) {
326 LOGE("recorder is not initialized");
329 return mRecorder->setListener(listener);
332 status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) const {
333 if (mRecorder != NULL) {
334 return mRecorder->dump(fd, args);
339 }; // namespace android