1 /*
2 ** Copyright 2008, The Android Open Source Project
3 **
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>
27 #include <cutils/properties.h> // for property_get
28 #include <binder/IPCThreadState.h>
29 #include <binder/IServiceManager.h>
30 #include <binder/MemoryHeapBase.h>
31 #include <binder/MemoryBase.h>
32
33 #include <utils/String16.h>
34
35 #include <system/audio.h>
36
37 #include "MediaRecorderClient.h"
38 #include "MediaPlayerService.h"
39
40 #include "StagefrightRecorder.h"
41 #include <gui/IGraphicBufferProducer.h>
42
43 namespace android {
44
45 const char* cameraPermission = "android.permission.CAMERA";
46 const char* recordAudioPermission = "android.permission.RECORD_AUDIO";
47
checkPermission(const char * permissionString)48 static 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) ALOGE("Request requires %s", permissionString);
55 return ok;
56 }
57
58
querySurfaceMediaSource()59 sp<IGraphicBufferProducer> MediaRecorderClient::querySurfaceMediaSource()
60 {
61 ALOGV("Query SurfaceMediaSource");
62 Mutex::Autolock lock(mLock);
63 if (mRecorder == NULL) {
64 ALOGE("recorder is not initialized");
65 return NULL;
66 }
67 return mRecorder->querySurfaceMediaSource();
68 }
69
70
71
setCamera(const sp<ICamera> & camera,const sp<ICameraRecordingProxy> & proxy)72 status_t MediaRecorderClient::setCamera(const sp<ICamera>& camera,
73 const sp<ICameraRecordingProxy>& proxy)
74 {
75 ALOGV("setCamera");
76 Mutex::Autolock lock(mLock);
77 if (mRecorder == NULL) {
78 ALOGE("recorder is not initialized");
79 return NO_INIT;
80 }
81 return mRecorder->setCamera(camera, proxy);
82 }
83
setPreviewSurface(const sp<IGraphicBufferProducer> & surface)84 status_t MediaRecorderClient::setPreviewSurface(const sp<IGraphicBufferProducer>& surface)
85 {
86 ALOGV("setPreviewSurface");
87 Mutex::Autolock lock(mLock);
88 if (mRecorder == NULL) {
89 ALOGE("recorder is not initialized");
90 return NO_INIT;
91 }
92 return mRecorder->setPreviewSurface(surface);
93 }
94
setVideoSource(int vs)95 status_t MediaRecorderClient::setVideoSource(int vs)
96 {
97 ALOGV("setVideoSource(%d)", vs);
98 // Check camera permission for sources other than SURFACE
99 if (vs != VIDEO_SOURCE_SURFACE && !checkPermission(cameraPermission)) {
100 return PERMISSION_DENIED;
101 }
102 Mutex::Autolock lock(mLock);
103 if (mRecorder == NULL) {
104 ALOGE("recorder is not initialized");
105 return NO_INIT;
106 }
107 return mRecorder->setVideoSource((video_source)vs);
108 }
109
setAudioSource(int as)110 status_t MediaRecorderClient::setAudioSource(int as)
111 {
112 ALOGV("setAudioSource(%d)", as);
113 if (!checkPermission(recordAudioPermission)) {
114 return PERMISSION_DENIED;
115 }
116 Mutex::Autolock lock(mLock);
117 if (mRecorder == NULL) {
118 ALOGE("recorder is not initialized");
119 return NO_INIT;
120 }
121 return mRecorder->setAudioSource((audio_source_t)as);
122 }
123
setOutputFormat(int of)124 status_t MediaRecorderClient::setOutputFormat(int of)
125 {
126 ALOGV("setOutputFormat(%d)", of);
127 Mutex::Autolock lock(mLock);
128 if (mRecorder == NULL) {
129 ALOGE("recorder is not initialized");
130 return NO_INIT;
131 }
132 return mRecorder->setOutputFormat((output_format)of);
133 }
134
setVideoEncoder(int ve)135 status_t MediaRecorderClient::setVideoEncoder(int ve)
136 {
137 ALOGV("setVideoEncoder(%d)", ve);
138 Mutex::Autolock lock(mLock);
139 if (mRecorder == NULL) {
140 ALOGE("recorder is not initialized");
141 return NO_INIT;
142 }
143 return mRecorder->setVideoEncoder((video_encoder)ve);
144 }
145
setAudioEncoder(int ae)146 status_t MediaRecorderClient::setAudioEncoder(int ae)
147 {
148 ALOGV("setAudioEncoder(%d)", ae);
149 Mutex::Autolock lock(mLock);
150 if (mRecorder == NULL) {
151 ALOGE("recorder is not initialized");
152 return NO_INIT;
153 }
154 return mRecorder->setAudioEncoder((audio_encoder)ae);
155 }
156
setOutputFile(const char * path)157 status_t MediaRecorderClient::setOutputFile(const char* path)
158 {
159 ALOGV("setOutputFile(%s)", path);
160 Mutex::Autolock lock(mLock);
161 if (mRecorder == NULL) {
162 ALOGE("recorder is not initialized");
163 return NO_INIT;
164 }
165 return mRecorder->setOutputFile(path);
166 }
167
setOutputFile(int fd,int64_t offset,int64_t length)168 status_t MediaRecorderClient::setOutputFile(int fd, int64_t offset, int64_t length)
169 {
170 ALOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length);
171 Mutex::Autolock lock(mLock);
172 if (mRecorder == NULL) {
173 ALOGE("recorder is not initialized");
174 return NO_INIT;
175 }
176 return mRecorder->setOutputFile(fd, offset, length);
177 }
178
setVideoSize(int width,int height)179 status_t MediaRecorderClient::setVideoSize(int width, int height)
180 {
181 ALOGV("setVideoSize(%dx%d)", width, height);
182 Mutex::Autolock lock(mLock);
183 if (mRecorder == NULL) {
184 ALOGE("recorder is not initialized");
185 return NO_INIT;
186 }
187 return mRecorder->setVideoSize(width, height);
188 }
189
setVideoFrameRate(int frames_per_second)190 status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second)
191 {
192 ALOGV("setVideoFrameRate(%d)", frames_per_second);
193 Mutex::Autolock lock(mLock);
194 if (mRecorder == NULL) {
195 ALOGE("recorder is not initialized");
196 return NO_INIT;
197 }
198 return mRecorder->setVideoFrameRate(frames_per_second);
199 }
200
setParameters(const String8 & params)201 status_t MediaRecorderClient::setParameters(const String8& params) {
202 ALOGV("setParameters(%s)", params.string());
203 Mutex::Autolock lock(mLock);
204 if (mRecorder == NULL) {
205 ALOGE("recorder is not initialized");
206 return NO_INIT;
207 }
208 return mRecorder->setParameters(params);
209 }
210
prepare()211 status_t MediaRecorderClient::prepare()
212 {
213 ALOGV("prepare");
214 Mutex::Autolock lock(mLock);
215 if (mRecorder == NULL) {
216 ALOGE("recorder is not initialized");
217 return NO_INIT;
218 }
219 return mRecorder->prepare();
220 }
221
222
getMaxAmplitude(int * max)223 status_t MediaRecorderClient::getMaxAmplitude(int* max)
224 {
225 ALOGV("getMaxAmplitude");
226 Mutex::Autolock lock(mLock);
227 if (mRecorder == NULL) {
228 ALOGE("recorder is not initialized");
229 return NO_INIT;
230 }
231 return mRecorder->getMaxAmplitude(max);
232 }
233
start()234 status_t MediaRecorderClient::start()
235 {
236 ALOGV("start");
237 Mutex::Autolock lock(mLock);
238 if (mRecorder == NULL) {
239 ALOGE("recorder is not initialized");
240 return NO_INIT;
241 }
242 return mRecorder->start();
243
244 }
245
stop()246 status_t MediaRecorderClient::stop()
247 {
248 ALOGV("stop");
249 Mutex::Autolock lock(mLock);
250 if (mRecorder == NULL) {
251 ALOGE("recorder is not initialized");
252 return NO_INIT;
253 }
254 return mRecorder->stop();
255 }
256
init()257 status_t MediaRecorderClient::init()
258 {
259 ALOGV("init");
260 Mutex::Autolock lock(mLock);
261 if (mRecorder == NULL) {
262 ALOGE("recorder is not initialized");
263 return NO_INIT;
264 }
265 return mRecorder->init();
266 }
267
close()268 status_t MediaRecorderClient::close()
269 {
270 ALOGV("close");
271 Mutex::Autolock lock(mLock);
272 if (mRecorder == NULL) {
273 ALOGE("recorder is not initialized");
274 return NO_INIT;
275 }
276 return mRecorder->close();
277 }
278
279
reset()280 status_t MediaRecorderClient::reset()
281 {
282 ALOGV("reset");
283 Mutex::Autolock lock(mLock);
284 if (mRecorder == NULL) {
285 ALOGE("recorder is not initialized");
286 return NO_INIT;
287 }
288 return mRecorder->reset();
289 }
290
release()291 status_t MediaRecorderClient::release()
292 {
293 ALOGV("release");
294 Mutex::Autolock lock(mLock);
295 if (mRecorder != NULL) {
296 delete mRecorder;
297 mRecorder = NULL;
298 wp<MediaRecorderClient> client(this);
299 mMediaPlayerService->removeMediaRecorderClient(client);
300 }
301 return NO_ERROR;
302 }
303
MediaRecorderClient(const sp<MediaPlayerService> & service,pid_t pid)304 MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid)
305 {
306 ALOGV("Client constructor");
307 mPid = pid;
308 mRecorder = new StagefrightRecorder;
309 mMediaPlayerService = service;
310 }
311
~MediaRecorderClient()312 MediaRecorderClient::~MediaRecorderClient()
313 {
314 ALOGV("Client destructor");
315 release();
316 }
317
setListener(const sp<IMediaRecorderClient> & listener)318 status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener)
319 {
320 ALOGV("setListener");
321 Mutex::Autolock lock(mLock);
322 if (mRecorder == NULL) {
323 ALOGE("recorder is not initialized");
324 return NO_INIT;
325 }
326 return mRecorder->setListener(listener);
327 }
328
setClientName(const String16 & clientName)329 status_t MediaRecorderClient::setClientName(const String16& clientName) {
330 ALOGV("setClientName(%s)", String8(clientName).string());
331 Mutex::Autolock lock(mLock);
332 if (mRecorder == NULL) {
333 ALOGE("recorder is not initialized");
334 return NO_INIT;
335 }
336 return mRecorder->setClientName(clientName);
337 }
338
dump(int fd,const Vector<String16> & args) const339 status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) const {
340 if (mRecorder != NULL) {
341 return mRecorder->dump(fd, args);
342 }
343 return OK;
344 }
345
346 }; // namespace android
347