MediaRecorderClient.cpp revision b33f3407bab0970a7f9241680723a1140b177c50
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    LOGV("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    LOGV("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    LOGV("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    LOGV("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    LOGV("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    LOGV("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    LOGV("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    LOGV("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    LOGV("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    LOGV("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::setOutputFileAuxiliary(int fd)
182{
183    LOGV("setOutputFileAuxiliary(%d)", fd);
184    Mutex::Autolock lock(mLock);
185    if (mRecorder == NULL) {
186        LOGE("recorder is not initialized");
187        return NO_INIT;
188    }
189    return mRecorder->setOutputFileAuxiliary(fd);
190}
191
192status_t MediaRecorderClient::setVideoSize(int width, int height)
193{
194    LOGV("setVideoSize(%dx%d)", width, height);
195    Mutex::Autolock lock(mLock);
196    if (mRecorder == NULL) {
197        LOGE("recorder is not initialized");
198        return NO_INIT;
199    }
200    return mRecorder->setVideoSize(width, height);
201}
202
203status_t MediaRecorderClient::setVideoFrameRate(int frames_per_second)
204{
205    LOGV("setVideoFrameRate(%d)", frames_per_second);
206    Mutex::Autolock lock(mLock);
207    if (mRecorder == NULL) {
208        LOGE("recorder is not initialized");
209        return NO_INIT;
210    }
211    return mRecorder->setVideoFrameRate(frames_per_second);
212}
213
214status_t MediaRecorderClient::setParameters(const String8& params) {
215    LOGV("setParameters(%s)", params.string());
216    Mutex::Autolock lock(mLock);
217    if (mRecorder == NULL) {
218        LOGE("recorder is not initialized");
219        return NO_INIT;
220    }
221    return mRecorder->setParameters(params);
222}
223
224status_t MediaRecorderClient::prepare()
225{
226    LOGV("prepare");
227    Mutex::Autolock lock(mLock);
228    if (mRecorder == NULL) {
229        LOGE("recorder is not initialized");
230        return NO_INIT;
231    }
232    return mRecorder->prepare();
233}
234
235
236status_t MediaRecorderClient::getMaxAmplitude(int* max)
237{
238    LOGV("getMaxAmplitude");
239    Mutex::Autolock lock(mLock);
240    if (mRecorder == NULL) {
241        LOGE("recorder is not initialized");
242        return NO_INIT;
243    }
244    return mRecorder->getMaxAmplitude(max);
245}
246
247status_t MediaRecorderClient::start()
248{
249    LOGV("start");
250    Mutex::Autolock lock(mLock);
251    if (mRecorder == NULL) {
252        LOGE("recorder is not initialized");
253        return NO_INIT;
254    }
255    return mRecorder->start();
256
257}
258
259status_t MediaRecorderClient::stop()
260{
261    LOGV("stop");
262    Mutex::Autolock lock(mLock);
263    if (mRecorder == NULL) {
264        LOGE("recorder is not initialized");
265        return NO_INIT;
266    }
267    return mRecorder->stop();
268}
269
270status_t MediaRecorderClient::init()
271{
272    LOGV("init");
273    Mutex::Autolock lock(mLock);
274    if (mRecorder == NULL) {
275        LOGE("recorder is not initialized");
276        return NO_INIT;
277    }
278    return mRecorder->init();
279}
280
281status_t MediaRecorderClient::close()
282{
283    LOGV("close");
284    Mutex::Autolock lock(mLock);
285    if (mRecorder == NULL) {
286        LOGE("recorder is not initialized");
287        return NO_INIT;
288    }
289    return mRecorder->close();
290}
291
292
293status_t MediaRecorderClient::reset()
294{
295    LOGV("reset");
296    Mutex::Autolock lock(mLock);
297    if (mRecorder == NULL) {
298        LOGE("recorder is not initialized");
299        return NO_INIT;
300    }
301    return mRecorder->reset();
302}
303
304status_t MediaRecorderClient::release()
305{
306    LOGV("release");
307    Mutex::Autolock lock(mLock);
308    if (mRecorder != NULL) {
309        delete mRecorder;
310        mRecorder = NULL;
311        wp<MediaRecorderClient> client(this);
312        mMediaPlayerService->removeMediaRecorderClient(client);
313    }
314    return NO_ERROR;
315}
316
317MediaRecorderClient::MediaRecorderClient(const sp<MediaPlayerService>& service, pid_t pid)
318{
319    LOGV("Client constructor");
320    mPid = pid;
321    mRecorder = new StagefrightRecorder;
322    mMediaPlayerService = service;
323}
324
325MediaRecorderClient::~MediaRecorderClient()
326{
327    LOGV("Client destructor");
328    release();
329}
330
331status_t MediaRecorderClient::setListener(const sp<IMediaRecorderClient>& listener)
332{
333    LOGV("setListener");
334    Mutex::Autolock lock(mLock);
335    if (mRecorder == NULL) {
336        LOGE("recorder is not initialized");
337        return NO_INIT;
338    }
339    return mRecorder->setListener(listener);
340}
341
342status_t MediaRecorderClient::dump(int fd, const Vector<String16>& args) const {
343    if (mRecorder != NULL) {
344        return mRecorder->dump(fd, args);
345    }
346    return OK;
347}
348
349}; // namespace android
350