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