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