MediaRecorderClient.cpp revision a3f1fa308728976fc9ca1b4f37d26e633b32b9ac
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 <android_runtime/ActivityManager.h>
29#include <binder/IPCThreadState.h>
30#include <binder/IServiceManager.h>
31#include <binder/MemoryHeapBase.h>
32#include <binder/MemoryBase.h>
33
34#include <utils/String16.h>
35
36#include <system/audio.h>
37
38#include "MediaRecorderClient.h"
39#include "MediaPlayerService.h"
40
41#include "StagefrightRecorder.h"
42#include <gui/ISurfaceTexture.h>
43
44namespace android {
45
46const char* cameraPermission = "android.permission.CAMERA";
47const char* recordAudioPermission = "android.permission.RECORD_AUDIO";
48
49static bool checkPermission(const char* permissionString) {
50#ifndef HAVE_ANDROID_OS
51    return true;
52#endif
53    if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
54    bool ok = checkCallingPermission(String16(permissionString));
55    if (!ok) ALOGE("Request requires %s", permissionString);
56    return ok;
57}
58
59
60sp<ISurfaceTexture> MediaRecorderClient::querySurfaceMediaSource()
61{
62    ALOGV("Query SurfaceMediaSource");
63    Mutex::Autolock lock(mLock);
64    if (mRecorder == NULL) {
65        ALOGE("recorder is not initialized");
66        return NULL;
67    }
68    return mRecorder->querySurfaceMediaSource();
69}
70
71
72
73status_t MediaRecorderClient::setCamera(const sp<ICamera>& camera,
74                                        const sp<ICameraRecordingProxy>& proxy)
75{
76    ALOGV("setCamera");
77    Mutex::Autolock lock(mLock);
78    if (mRecorder == NULL) {
79        ALOGE("recorder is not initialized");
80        return NO_INIT;
81    }
82    return mRecorder->setCamera(camera, proxy);
83}
84
85status_t MediaRecorderClient::setPreviewSurface(const sp<Surface>& surface)
86{
87    ALOGV("setPreviewSurface");
88    Mutex::Autolock lock(mLock);
89    if (mRecorder == NULL) {
90        ALOGE("recorder is not initialized");
91        return NO_INIT;
92    }
93    return mRecorder->setPreviewSurface(surface);
94}
95
96status_t MediaRecorderClient::setVideoSource(int vs)
97{
98    ALOGV("setVideoSource(%d)", vs);
99    if (!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
110status_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
124status_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
135status_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
146status_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
157status_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
168status_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
179status_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
190status_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
201status_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
211status_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
223status_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
234status_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
246status_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
257status_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
268status_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
280status_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
291status_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
304MediaRecorderClient::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
312MediaRecorderClient::~MediaRecorderClient()
313{
314    ALOGV("Client destructor");
315    release();
316}
317
318status_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
329status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) const {
330    if (mRecorder != NULL) {
331        return mRecorder->dump(fd, args);
332    }
333    return OK;
334}
335
336}; // namespace android
337